| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This chapter describes routines for performing least squares fits to experimental data using linear combinations of functions. The data may be weighted or unweighted, i.e. with known or unknown errors. For weighted data the functions compute the best fit parameters and their associated covariance matrix. For unweighted data the covariance matrix is estimated from the scatter of the points, giving a variance-covariance matrix.
The functions are divided into separate versions for simple one- or two-parameter regression and multiple-parameter fits. The functions are declared in the header file ‘gsl_fit.h’.
| 36.1 Overview | ||
| 36.2 Linear regression | ||
| 36.3 Linear fitting without a constant term | ||
| 36.4 Multi-parameter fitting | ||
| 36.5 Examples | ||
| 36.6 References and Further Reading |
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Least-squares fits are found by minimizing
(chi-squared), the weighted sum of squared residuals over
experimental datapoints
for the model
,
The
parameters of the model are
. The
weight factors
are given by
,
where
is the experimental error on the data-point
. The errors are assumed to be
gaussian and uncorrelated.
For unweighted data the chi-squared sum is computed without any weight factors.
The fitting routines return the best-fit parameters
and their
covariance matrix. The covariance matrix measures the
statistical errors on the best-fit parameters resulting from the
errors on the data,
, and is defined
as
where
denotes an average over the gaussian error distributions of the underlying datapoints.
The covariance matrix is calculated by error propagation from the data
errors
. The change in a fitted parameter
caused by a small change in the data
is given
by
allowing the covariance matrix to be written in terms of the errors on the data,
For uncorrelated data the fluctuations of the underlying datapoints satisfy
, giving a
corresponding parameter covariance matrix of
When computing the covariance matrix for unweighted data, i.e. data with unknown errors,
the weight factors
in this sum are replaced by the single estimate
, where
is the computed variance of the
residuals about the best-fit model,
.
This is referred to as the variance-covariance matrix.
The standard deviations of the best-fit parameters are given by the
square root of the corresponding diagonal elements of
the covariance matrix,
.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The functions described in this section can be used to perform
least-squares fits to a straight line model,
.
This function computes the best-fit linear regression coefficients
(c0,c1) of the model
for the dataset
(x, y), two vectors of length n with strides
xstride and ystride. The errors on y are assumed unknown so
the variance-covariance matrix for the
parameters (c0, c1) is estimated from the scatter of the
points around the best-fit line and returned via the parameters
(cov00, cov01, cov11).
The sum of squares of the residuals from the best-fit line is returned
in sumsq.
This function computes the best-fit linear regression coefficients
(c0,c1) of the model
for the weighted
dataset (x, y), two vectors of length n with strides
xstride and ystride. The vector w, of length n
and stride wstride, specifies the weight of each datapoint. The
weight is the reciprocal of the variance for each datapoint in y.
The covariance matrix for the parameters (c0, c1) is
computed using the weights and returned via the parameters
(cov00, cov01, cov11). The weighted sum of squares
of the residuals from the best-fit line,
, is returned in
chisq.
This function uses the best-fit linear regression coefficients
c0,c1 and their covariance
cov00,cov01,cov11 to compute the fitted function
y and its standard deviation y_err for the model
at the point x.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The functions described in this section can be used to perform
least-squares fits to a straight line model without a constant term,
.
This function computes the best-fit linear regression coefficient
c1 of the model
for the datasets (x,
y), two vectors of length n with strides xstride and
ystride. The errors on y are assumed unknown so the
variance of the parameter c1 is estimated from
the scatter of the points around the best-fit line and returned via the
parameter cov11. The sum of squares of the residuals from the
best-fit line is returned in sumsq.
This function computes the best-fit linear regression coefficient
c1 of the model
for the weighted datasets
(x, y), two vectors of length n with strides
xstride and ystride. The vector w, of length n
and stride wstride, specifies the weight of each datapoint. The
weight is the reciprocal of the variance for each datapoint in y.
The variance of the parameter c1 is computed using the weights
and returned via the parameter cov11. The weighted sum of
squares of the residuals from the best-fit line,
, is
returned in chisq.
This function uses the best-fit linear regression coefficient c1
and its covariance cov11 to compute the fitted function
y and its standard deviation y_err for the model
at the point x.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The functions described in this section perform least-squares fits to a
general linear model,
where
is a vector of
observations,
is an
by
matrix of
predictor variables, and the elements of the vector
are the
unknown best-fit parameters which are to be estimated. The chi-squared value is given by
.
This formulation can be used for fits to any number of functions and/or
variables by preparing the
-by-
matrix
appropriately. For example, to fit to a
-th order polynomial in
x, use the following matrix,
where the index
runs over the observations and the index
runs from 0 to
.
To fit to a set of
sinusoidal functions with fixed frequencies
,
, …,
, use,
To fit to
independent variables
,
, …,
, use,
where
is the
-th value of the predictor variable
.
The functions described in this section are declared in the header file ‘gsl_multifit.h’.
The solution of the general linear least-squares system requires an
additional working space for intermediate results, such as the singular
value decomposition of the matrix
.
This function allocates a workspace for fitting a model to n observations using p parameters.
This function frees the memory associated with the workspace w.
These functions compute the best-fit parameters c of the model
for the observations y and the matrix of predictor
variables X. The variance-covariance matrix of the model
parameters cov is estimated from the scatter of the observations
about the best-fit. The sum of squares of the residuals from the
best-fit,
, is returned in chisq.
The best-fit is found by singular value decomposition of the matrix
X using the preallocated workspace provided in work. The
modified Golub-Reinsch SVD algorithm is used, with column scaling to
improve the accuracy of the singular values. Any components which have
zero singular value (to machine precision) are discarded from the fit.
In the second form of the function the components are discarded if the
ratio of singular values
falls below the user-specified
tolerance tol, and the effective rank is returned in rank.
This function computes the best-fit parameters c of the weighted
model
for the observations y with weights w
and the matrix of predictor variables X. The covariance matrix of
the model parameters cov is computed with the given weights. The
weighted sum of squares of the residuals from the best-fit,
, is returned in chisq.
The best-fit is found by singular value decomposition of the matrix
X using the preallocated workspace provided in work. Any
components which have zero singular value (to machine precision) are
discarded from the fit. In the second form of the function the
components are discarded if the ratio of singular values
falls below the user-specified tolerance tol, and the effective
rank is returned in rank.
This function uses the best-fit multilinear regression coefficients
c and their covariance matrix
cov to compute the fitted function value
y and its standard deviation y_err for the model
at the point x.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The following program computes a least squares straight-line fit to a simple dataset, and outputs the best-fit line and its associated one standard-deviation error bars.
|
The following commands extract the data from the output of the program
and display it using the GNU plotutils graph utility,
$ ./demo > tmp
$ more tmp
# best fit: Y = -106.6 + 0.06 X
# covariance matrix:
# [ 39602, -19.9
# -19.9, 0.01]
# chisq = 0.8
$ for n in data fit hi lo ;
do
grep "^$n" tmp | cut -d: -f2 > $n ;
done
$ graph -T X -X x -Y y -y 0 20 -m 0 -S 2 -Ie data
-S 0 -I a -m 1 fit -m 2 hi -m 2 lo
|
The next program performs a quadratic fit
to a weighted dataset using the generalised linear fitting function
gsl_multifit_wlinear. The model matrix
for a quadratic
fit is given by,
where the column of ones corresponds to the constant term
.
The two remaining columns corresponds to the terms
and
.
The program reads n lines of data in the format (x, y, err) where err is the error (standard deviation) in the value y.
|
A suitable set of data for fitting can be generated using the following
program. It outputs a set of points with gaussian errors from the curve
in the region
.
|
The data can be prepared by running the resulting executable program,
$ ./generate > exp.dat $ more exp.dat 0.1 0.97935 0.110517 0.2 1.3359 0.12214 0.3 1.52573 0.134986 0.4 1.60318 0.149182 0.5 1.81731 0.164872 0.6 1.92475 0.182212 .... |
To fit the data use the previous program, with the number of data points given as the first argument. In this case there are 19 data points.
$ ./fit 19 < exp.dat 0.1 0.97935 +/- 0.110517 0.2 1.3359 +/- 0.12214 ... # best fit: Y = 1.02318 + 0.956201 X + 0.876796 X^2 # covariance matrix: [ +1.25612e-02, -3.64387e-02, +1.94389e-02 -3.64387e-02, +1.42339e-01, -8.48761e-02 +1.94389e-02, -8.48761e-02, +5.60243e-02 ] # chisq = 23.0987 |
The parameters of the quadratic fit match the coefficients of the
expansion of
, taking into account the errors on the
parameters and the
difference between the exponential and
quadratic functions for the larger values of
. The errors on
the parameters are given by the square-root of the corresponding
diagonal elements of the covariance matrix. The chi-squared per degree
of freedom is 1.4, indicating a reasonable fit to the data.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
A summary of formulas and techniques for least squares fitting can be found in the “Statistics” chapter of the Annual Review of Particle Physics prepared by the Particle Data Group,
The Review of Particle Physics is available online at the website given above.
The tests used to prepare these routines are based on the NIST Statistical Reference Datasets. The datasets and their documentation are available from NIST at the following website,
http://www.nist.gov/itl/div898/strd/index.html.
| [ << ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This document was generated by Autobuild on September, 26 2007 using texi2html 1.78.