Skip to content

Commit

Permalink
Re #11617. Added algorithm description.
Browse files Browse the repository at this point in the history
  • Loading branch information
mantid-roman committed May 28, 2015
1 parent 28cb000 commit 7b51219
Showing 1 changed file with 58 additions and 14 deletions.
72 changes: 58 additions & 14 deletions Code/Mantid/docs/source/algorithms/CalculateChiSquared-v1.rst
Expand Up @@ -10,35 +10,79 @@
Description
-----------

TODO: Enter a full rst-markup description of your algorithm here.
Calculates a measure of the goodness of a fit in four ways.

The ChiSquared property returns the sum of squares of differences between the calculated and measured values:

:math:`\chi_{1}^{2} = \sum_{i} (y_i - f_i)^2`

where :math:`y_i` and :math:`f_i` are the measured and calculated values at i-th point.

The ChiSquaredDividedByDOF is ChiSquared divided by the number of degrees of freedom (DOF):

:math:`\chi_{2}^{2} = \frac{1}{DOF}\sum_{i} (y_i - f_i)^2`

:math:`DOF = N_d - N_p` where :math:`N_d` is the number of data points used in fitting and :math:`N_p`
is the number of free (not fixed or tied) parameters of the function.

The ChiSquaredWeighted property sums the squares of the differences divided by the data errors:

:math:`\chi_{3}^{2} = \sum_{i} \left(\frac{y_i - f_i}{\sigma_i}\right)^2`

Finally, ChiSquaredWeightedDividedByDOF is

:math:`\chi_{4}^{2} = \chi_{3}^{2} / DOF`

Usage
-----
.. Try not to use files in your examples,
but if you cannot avoid it then the (small) files must be added to
autotestdata\UsageData and the following tag unindented
.. include:: ../usagedata-note.txt
**Example - CalculateChiSquared**

.. testcode:: CalculateChiSquaredExample

# Create a host workspace
ws = CreateWorkspace(DataX=range(0,3), DataY=(0,2))
or
ws = CreateSampleWorkspace()
import numpy as np

# Create a data set
x = np.linspace(0,1,10)
y = 1.0 + 2.0 * x
e = np.sqrt(y)
ws = CreateWorkspace(DataX=x, DataY=y, DataE=e)

# Define a function
func = 'name=LinearBackground,A0=1.1,A1=1.9'

# Calculate the chi squared
chi2,chi2dof,chi2W,chi2Wdof = CalculateChiSquared(func,ws)

print 'Chi squared is %s' % chi2
print 'Chi squared / DOF is %s' % chi2dof
print 'Chi squared weighted is %s' % chi2W
print 'Chi squared weighted / DOF is %s' % chi2Wdof
print

# Define a function that models the data exactly
func = 'name=LinearBackground,A0=1.0,A1=2.0'

wsOut = CalculateChiSquared()
# Calculate the chi squared
chi2,chi2dof,chi2W,chi2Wdof = CalculateChiSquared(func,ws)

# Print the result
print "The output workspace has %i spectra" % wsOut.getNumberHistograms()
print 'Chi squared is %s' % chi2
print 'Chi squared / DOF is %s' % chi2dof
print 'Chi squared weighted is %s' % chi2W
print 'Chi squared weighted / DOF is %s' % chi2Wdof

Output:

.. testoutput:: CalculateChiSquaredExample

The output workspace has ?? spectra
Chi squared is 0.0351851851852
Chi squared / DOF is 0.00439814814815
Chi squared weighted is 0.0266028783977
Chi squared weighted / DOF is 0.00332535979971

Chi squared is 0.0
Chi squared / DOF is 0.0
Chi squared weighted is 0.0
Chi squared weighted / DOF is 0.0

.. categories::

0 comments on commit 7b51219

Please sign in to comment.