-
Notifications
You must be signed in to change notification settings - Fork 1
/
chi_squared.m
33 lines (29 loc) · 1.32 KB
/
chi_squared.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
function [chi2, chi2_old] = chi_squared(y,fit,P,eb)
% returns *reduced* chi^2 value for use in data modelling
% "y" is a vector of data, "fit" is a vector of model values (size(fit)=size(y)), P is the number of
% parameters fit in the model, and eb is a vector of error bars (1-to-1 correspondnce with y)
% Ref: John R. Taylor, "An Introduction to Error Analysis", (2nd ed., 1997)
% 11/11/01 Mike Scarpulla. Please direct questions or comments to scarps@uclink.berkeley.edu
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if nargin<3
error('Wrong number of arguments passed to "chi_squared"')
end
% if error bars are not availible, evaluate chi^2 by normalizing deviation^2 by magnitude of data.
% This assumes that the STDEV of a value scales as SQRT(value). USE WITH THIS CAVEAT IN MIND
if nargin==3
% N = max(size(y));
% terms = ((y-fit).^2)./abs(y);
% chi2 = sum(terms);
% chi2_old = 1/(N-P)*sum(terms);
N = max(size(y));
terms = sqrt((y-fit).^2)./abs(y);
chi2 = sum(terms)/N;
chi2_old = 1/(N-P)*sum(terms);
end
%if error bars are availible, normalize the deviation to the expectred error
if nargin==4
N = max(size(y));
terms = ((y-fit)./eb).^2;
chi2 = sum(terms);
chi2_old = 1/(N-P)*sum(terms);
end