File Exchange

image thumbnail

Generalized Nonlinear Non-analytic Chi-Square Fitting

version 1.1 (42 KB) by

Performs chi-square fit with uncertainty estimation when measurement errors are known.



View License

fitChiSquare is a generalized chi-square fitting routine for any model function when data measurement errors are known; it returns the model parameters and their uncertainties at the delta chi-square = 1 boundary (68% confidence interval). It also returns the chi-square and degrees of freedom (dof) of the fit. The goodness-of-fit may be estimated by comparing chi-square/dof to 1 (<1 is a good fit; >>1 indicates a poor fit). Alternatively, it returns the fit and measurement errors when the model is known - see ErrorUnknown option.

Type "help fitChiSquare" or see the header for usage.

This function calculates the data variance from reported measurement errors, then calculates the chi-square fit. Then the function finds the projection of the delta chi^2 = 1 contour onto each parameter. In the case that the parameter uncertainties are normally distributed, the delta chi^2 = 1 method gives the 68% confidence limit for the parameters. Monte Carlo or investigations of many data sets should be used to confirm the parameter uncertainties are normally distributed.

Note that when used solely as a model fitter, fitChiSquare will generally run more slowly than fminsearch or lsqnonlin. If you are only interested in data optimization, it is recommended that you use one of the built-in functions.

If one encounters the following error message:

    Unexpected termination flag 0 in non-estimating variable
    minimization during uncertainty estimation

this is because the non-varying parameter minimization routine has encountered its iteration or evaluation limit. Raise UncOptions.MaxFunEvals or UncOptions.MaxIter and try again.

Note: If the user can not use lsqnonlin (i.e., the optimization toolbox is not installed), the program will use the built-in function fminsearch instead. This may reduce the robustness of the calculation.

1. W.H. Press, B.P. Flannery, S.A. Teukolsky, W.T. Vetterling. Numerical Recipes; The Art of Scientific Computing. (Cambridge University Press: Cambridge). 1986.
2. P.R. Bevington, D.K. Robinson. Data Reduction and Error Analysis for the Physical Sciences. (McGraw-Hill: New York). 1992.

Comments and Ratings (15)


Can someone help me with editing the functions? even using the ModelEditor (very convenient) I am not sure how actually add a model.

Any attempt results in that the modeleditor gets broken (error attached below).

I want to add a double exponential fit to the existing fit functions, e.g. p(1)*exp(p(2)*x)+p(3)*exp(p(4)*x).

>> ModelEditor
Index exceeds matrix dimensions.

Error in ModelEditor>ModelEditor_OpeningFcn (line 62)
if nargin && ischar(varargin{1})

Error in gui_mainfcn (line 221)
    feval(gui_State.gui_OpeningFcn, gui_hFigure, [], guidata(gui_hFigure),

Error in ModelEditor (line 43)
    gui_mainfcn(gui_State, varargin{:});

Stephan Koehler

Question: Could you also provide 90% confidence intervals for the fitting parameters, instead of just the 68%?

Stephan Koehler

Hi all! Thanks, Nathaniel, for this really useful tool! I have a problem with one of the options: FitIndex. It looks like this option is not used anywhere. Setting it has no effect on the result (all the parameters undergo fitting, no matter what the value of op.FitIndex is). Investigating the code led me to discover that FitIndex is not explicitly used in fitChiSquare.m, and that "options" is then passed to lsqnonlin or fminsearch, which imho cannot use FitIndex. Can you please enlighten me? In the meantime, I'll try fixing a parameter by giving strict bounds for its value. Thanks! Giorgio De Nunzio

J West

J West (view profile)

Any thoughts on why this sometimes returns parameter uncertainties = 0 (i.e. dParams(i).du = 0, and dParams(i).lVal = params(i))?

This seems to occur for me in cases where the uncertainties are definitely significant, but are also significantly skewed rather than normally distributed (when tested by Monte Carlo simulation).



Dunno (view profile)

The output uncertainty of fit parameters is incorrect. The function is also slow compare to fminsearch. I actually trust the best fit value from fminsearch more than the one from this code, parameter uncertainty can probably be estimated using algorithm of Jone D'Errico.

sissi sissi

if the result turn out :"Adjusted R-square:NAN "
what does it mean ? and how to get a better fitting ?
thank you

Christian G

Great stuff! I only used the core function fitChiSquare so far, because I couldn't get fitChiTool to work. I have R14 SP 1 with all relevant toolboxes, but get the following error:

??? Undefined command/function 'FitChiToolModel'.

Error in ==> FitChiTool>makeParamTable at 152
        model = FitChiToolModel(data,namevec);

ingmar Riedel-Kruse

Very useful!

Nathaniel Brahms

fitChiSquareExample now works properly

F Moisy

When I run the example, fitChiSquareExample.m, I obtain the following error (using R14):

??? Undefined command/function 'fitLibrary'.

Error in ==> fitChiSquareExample at 29

Rob deCarvalho

This is an extremely useful function for anyone who routinely does least squares fitting. It provides the ability of fitting data to a user defined function having both an arbitrary number of independent variables and an arbitrary number of fit parameters. Unlike most fitting routines, it contains an algorithm for estimating the uncertainties in the fit parameters. The statistical methods required to estimate parameter errors for non-linear function are generally quite complex, and it is appearant that a good deal of thought went into creating the fitChiSquare mfile. Once the syntax is figured out, the routine seems to be quite well behaved. Since most people will use this as a "black-box" routine, my one suggestion might be to see a general description of the algorithm and a full discloser of the statistical properties the data must posses for the error estimations to be rigorously correct.

Nathaniel Brahms

Working example is now included

lousy squat

need to include a working example



Minor bug fix, support for 4-parameter syntax.

Updated model library

Added Scale option

Added missing fitLibrary() function to zip file

New version (2.4.1) including:
1. Output syntax change (gof struct)
2. Added eta-square (correlation index)
3. Workaround for MATLAB struct array bug, which could cause a crash

fitChiSquareExample now includes an example model library file

Expanded plotting features and options. See help.

dParams is now a normal array, not a cell array.

New version (2.2) including ErrorUnknown flag.

New version:
Added bound feature, some error checking, updated documentation

Description change

New version (2.0)

Release includes missing between function and an example function

MATLAB Release
MATLAB 7.0.1 (R14SP1)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video