Skip to content
Snippets Groups Projects
Commit 775b47b5 authored by Julian Lenz's avatar Julian Lenz
Browse files

Added Chi-Square routine

parent c088b9a4
No related branches found
No related tags found
No related merge requests found
......@@ -41,6 +41,33 @@ class Cycler:
return self.getCurrent()
def chisq(r,sigma,redFreedom=0,normalized=False):
'''
Calculates the chi^2 value of residuals r with standard deviations sigma as
is minimized by scipy.optimize.curve_fit. This means it calculates
sum( (r/sigma)**2 )
if r and sigma are one-dimensional and
r.T . sigma^-1 . r
if sigma the two-dimensional covariance matrix.
If normalized=True, the result is divided by the length of r (Default: False).
If redFreedom>0, then normalized=True and the result is divided by len(r)-redFreedom.
'''
factor=1
if redFreedom>0 or normalized:
factor=1/(len(r)-redFreedom)
if len(np.shape(sigma))==1:
return np.sum((r/sigma)**2)*factor
elif len(np.shape(sigma)==2) and np.shape(sigma)[0]==np.shape(sigma)[1]:
return np.dot(r.T,np.dot(np.linalg.inv(sigma),r))*factor
else:
raise Exception('Shape of sigma is wrong.')
def makeTuples(iterable):
ret=[]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment