Skip to content

Commit

Permalink
Re #11617. Added property for chi^2 / DOF.
Browse files Browse the repository at this point in the history
  • Loading branch information
mantid-roman committed May 21, 2015
1 parent 8f220b0 commit e3f0280
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 37 deletions.
45 changes: 25 additions & 20 deletions Code/Mantid/Framework/CurveFitting/src/CalculateChiSquared.cpp
Expand Up @@ -37,10 +37,11 @@ const std::string CalculateChiSquared::summary() const {
//----------------------------------------------------------------------------------------------
/// Initialize the algorithm's properties.
void CalculateChiSquared::initConcrete() {
declareProperty("DivideByDOF", false, "Flag to divide the chi^2 by the "
"number of degrees of freedom (NofData "
"- nOfParams).");
declareProperty("ChiSquared", 0.0, "Output value of chi squared.");
declareProperty("ChiSquared", 0.0, "Output value of chi squared.", Direction::Output);
declareProperty("ChiSquaredDividedByDOF", 0.0,
"Output value of chi squared divided by the "
"number of degrees of freedom (NofData "
"- nOfParams).", Direction::Output);
}

//----------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -70,25 +71,29 @@ void CalculateChiSquared::execConcrete() {
}
g_log.debug() << "Chi squared " << chiSquared << std::endl;

// Store the result.
setProperty("ChiSquared", chiSquared);

// Divide by the DOF
bool divideByDOF = getProperty("DivideByDOF");
if (divideByDOF) {
// Get the number of free fitting parameters
size_t nParams = 0;
for(size_t i = 0; i < m_function->nParams(); ++i) {
if ( !m_function->isFixed(i) ) nParams += 1;
}
double dof = static_cast<double>(domain->size()) - static_cast<double>(nParams);
g_log.debug() << "DOF " << dof << std::endl;
if (dof <= 0.0) {
dof = 1.0;
g_log.warning() << "DOF has a non-positive value, changing to 1,0." << std::endl;
}
chiSquared /= dof;
g_log.debug() << "Chi squared / DOF " << chiSquared << std::endl;
// Get the number of free fitting parameters
size_t nParams = 0;
for (size_t i = 0; i < m_function->nParams(); ++i) {
if (!m_function->isFixed(i))
nParams += 1;
}
double dof =
static_cast<double>(domain->size()) - static_cast<double>(nParams);
g_log.debug() << "DOF " << dof << std::endl;
if (dof <= 0.0) {
dof = 1.0;
g_log.warning() << "DOF has a non-positive value, changing to 1,0."
<< std::endl;
}
chiSquared /= dof;
g_log.debug() << "Chi squared / DOF " << chiSquared << std::endl;

// Store the result.
setProperty("ChiSquared", chiSquared);
setProperty("ChiSquaredDividedByDOF", chiSquared);
}

} // namespace CurveFitting
Expand Down
26 changes: 9 additions & 17 deletions Code/Mantid/Framework/CurveFitting/test/CalculateChiSquaredTest.h
Expand Up @@ -133,7 +133,6 @@ class CalculateChiSquaredTest : public CxxTest::TestSuite {
Tester tester;
tester.set1DFunction();
tester.set1DSpectrumValues();
tester.setDivideByDOF();
tester.runAlgorithm();
tester.check1DSpectrum();
TS_ASSERT_DELTA(tester.chiSquared, 236.5, 1.0);
Expand All @@ -143,7 +142,6 @@ class CalculateChiSquaredTest : public CxxTest::TestSuite {
Tester tester(1);
tester.set1DFunction();
tester.set1DSpectrumValues();
tester.setDivideByDOF();
tester.runAlgorithm();
tester.check1DSpectrum();
TS_ASSERT_DELTA(tester.chiSquared, 184.0, 1.0);
Expand All @@ -153,7 +151,6 @@ class CalculateChiSquaredTest : public CxxTest::TestSuite {
Tester tester(3,3);
tester.set1DFunction();
tester.set1DSpectrumValues();
tester.setDivideByDOF();
tester.runAlgorithm();
tester.check1DSpectrum();
TS_ASSERT_DELTA(tester.chiSquared, 5069.0, 1.0);
Expand All @@ -163,7 +160,6 @@ class CalculateChiSquaredTest : public CxxTest::TestSuite {
Tester tester(3,2);
tester.set1DFunction();
tester.set1DSpectrumValues();
tester.setDivideByDOF();
tester.runAlgorithm();
tester.check1DSpectrum();
TS_ASSERT_DELTA(tester.chiSquared, 7151.0, 1.0);
Expand All @@ -187,7 +183,6 @@ class CalculateChiSquaredTest : public CxxTest::TestSuite {
double StartX;
double EndX;
bool ignoreInvalidData;
bool divideByDOF;

void makeXValues() {
size_t dlt = isHisto ? 1 : 0;
Expand Down Expand Up @@ -227,12 +222,13 @@ class CalculateChiSquaredTest : public CxxTest::TestSuite {
public:
// algorithm output
double chiSquared;
double chiSquaredDividedByDOF;
bool isExecuted;

Tester(size_t np = 3, size_t nd = 10, bool histo = true)
: nParams(np), nData(nd), isHisto(histo), workspaceIndex(0), xMin(-10),
xMax(10), StartX(EMPTY_DBL()), EndX(EMPTY_DBL()),
ignoreInvalidData(false), divideByDOF(false), chiSquared(-1), isExecuted(false) {
ignoreInvalidData(false), chiSquared(-1), chiSquaredDividedByDOF(-1), isExecuted(false) {
makeXValues();
}

Expand All @@ -243,16 +239,17 @@ class CalculateChiSquaredTest : public CxxTest::TestSuite {
TS_ASSERT_THROWS_NOTHING(alg.setProperty("Function", function));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("InputWorkspace", workspace));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("IgnoreInvalidData", ignoreInvalidData));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("DivideByDOF", divideByDOF));
if (dynamic_cast<IFunction1D *>(function.get())) {
TS_ASSERT_THROWS_NOTHING(alg.setProperty("WorkspaceIndex", workspaceIndex));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("StartX", StartX));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("EndX", EndX));
}
TS_ASSERT_THROWS_NOTHING(alg.execute());
isExecuted = alg.isExecuted();
if (isExecuted)
if (isExecuted) {
chiSquared = alg.getProperty("ChiSquared");
chiSquaredDividedByDOF = alg.getProperty("ChiSquaredDividedByDOF");
}
}

void setXRange_All() {
Expand All @@ -278,10 +275,6 @@ class CalculateChiSquaredTest : public CxxTest::TestSuite {
ignoreInvalidData = true;
}

void setDivideByDOF() {
divideByDOF = true;
}

void set1DFunction() {
const std::string fun =
"name=UserFunction,Formula=a+b*x+c*x^2,a=1,b=1,c=1";
Expand Down Expand Up @@ -347,13 +340,12 @@ class CalculateChiSquaredTest : public CxxTest::TestSuite {
sum2 += tmp * tmp;
}
}
if (divideByDOF) {
double dof = double(nData) - double(nParams);
if (dof <= 0.0) dof = 1.0;
sum2 /= dof;
}
TS_ASSERT_DIFFERS(sum2, 0);
TS_ASSERT_DELTA(sum2, chiSquared, 1e-10);
double dof = double(nData) - double(nParams);
if (dof <= 0.0) dof = 1.0;
sum2 /= dof;
TS_ASSERT_DELTA(sum2, chiSquaredDividedByDOF, 1e-10);
}

void checkFailed() {
Expand Down

0 comments on commit e3f0280

Please sign in to comment.