12using namespace Kernel;
22int CalculateChiSquared::version()
const {
return 1; }
25const std::string CalculateChiSquared::summary()
const {
26 return "Calculate chi squared for a function and a data set in a workspace.";
31void CalculateChiSquared::initConcrete() {
34 "Output value of chi squared divided by the "
35 "number of degrees of freedom (NofData "
39 "Output value of chi squared divided by the "
40 "number of data points).",
44 "Output value of weighted chi squared divided by the "
45 "number of degrees of freedom (NofData "
49 "Output value of weighted chi squared divided by the "
50 "number of data points).",
53 "Option to use the weighted chi squared "
54 "in error estimation. Default is false.");
59void CalculateChiSquared::execConcrete() {
73 for (
size_t i = 0; i <
m_function->nParams(); ++i) {
82 double chiSquared = 0.0;
83 double chiSquaredWeighted = 0.0;
86 g_log.
notice() <<
"Chi squared " << chiSquared <<
"\n"
87 <<
"Chi squared weighted " << chiSquaredWeighted <<
"\n";
91 setProperty(
"chiSquaredWeighted", chiSquaredWeighted);
94 double nData = dof +
static_cast<double>(nParams);
95 const double chiSquaredNData = chiSquared / nData;
96 const double chiSquaredWeightedNData = chiSquaredWeighted / nData;
97 g_log.
notice() <<
"Chi squared / NData " << chiSquaredNData <<
"\n"
98 <<
"Chi squared weighed / NData " << chiSquaredWeightedNData <<
"\n"
99 <<
"NParams " << nParams <<
"\n";
102 setProperty(
"ChiSquaredDividedByNData", chiSquaredNData);
103 setProperty(
"ChiSquaredWeightedDividedByNData", chiSquaredWeightedNData);
108 g_log.
warning(
"DOF has a non-positive value, changing to 1.0");
110 const double chiSquaredDOF = chiSquared / dof;
111 const double chiSquaredWeightedDOF = chiSquaredWeighted / dof;
112 g_log.
notice() <<
"Chi squared / DOF " << chiSquaredDOF <<
"\n"
113 <<
"Chi squared weighed / DOF " << chiSquaredWeightedDOF <<
"\n"
114 <<
"DOF " << dof <<
"\n";
117 setProperty(
"ChiSquaredDividedByDOF", chiSquaredDOF);
118 setProperty(
"ChiSquaredWeightedDividedByDOF", chiSquaredWeightedDOF);
141 chiSquaredWeighted = 0.0;
142 dof = -
static_cast<double>(nParams);
143 for (
size_t i = 0; i < values.
size(); ++i) {
149 chiSquaredWeighted +=
tmp *
tmp;
#define DECLARE_ALGORITHM(classname)
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
Base class that represents the domain of a function.
A class to store values calculated by a function.
double getFitWeight(size_t i) const
Get a fitting weight.
double getFitData(size_t i) const
Get a fitting data value.
size_t size() const
Return the number of values.
double getCalculated(size_t i) const
Get i-th calculated value.
This is an interface to a fitting function - a semi-abstarct class.
virtual void function(const FunctionDomain &domain, FunctionValues &values) const =0
Evaluates the function for all arguments in the domain.
Calculate chi squared for a function and a data set in a workspace.
static void calcChiSquared(const API::IFunction &fun, size_t nParams, const API::FunctionDomain &domain, API::FunctionValues &values, double &chiSquared, double &chiSquaredWeighted, double &dof)
Caclculate the chi squared, weighted chi squared and the number of degrees of freedom.
std::shared_ptr< API::IDomainCreator > m_domainCreator
Pointer to a domain creator.
std::shared_ptr< API::IFunction > m_function
Pointer to the fitting function.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void notice(const std::string &msg)
Logs at notice level.
void warning(const std::string &msg)
Logs at warning level.
std::shared_ptr< FunctionValues > FunctionValues_sptr
typedef for a shared pointer
std::shared_ptr< FunctionDomain > FunctionDomain_sptr
typedef for a shared pointer
@ Output
An output workspace.