fitting.texi
来自「用于VC.net的gsl的lib库文件包」· TEXI 代码 · 共 421 行 · 第 1/2 页
TEXI
421 行
@cindex fitting
@cindex least squares fit
@cindex regression, least squares
@cindex weighted linear fits
This chapter describes routines for performing least squares fits to
experimental data using linear combinations of functions. The data may
be weighted or unweighted. 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 @file{gsl_fit.h}
@menu
* Linear regression::
* Linear fitting without a constant term::
* Multi-parameter fitting::
* Fitting Examples::
* Fitting References and Further Reading::
@end menu
@node Linear regression
@section Linear regression
@cindex linear regression
The functions described in this section can be used to perform
least-squares fits to a straight line model, @math{Y = c_0 + c_1 X}.
For weighted data the best-fit is found by minimizing the weighted sum of
squared residuals, @math{\chi^2},
@tex
\beforedisplay
$$
\chi^2 = \sum_i w_i (y_i - (c_0 + c_1 x_i))^2
$$
\afterdisplay
@end tex
@ifinfo
@example
\chi^2 = \sum_i w_i (y_i - (c_0 + c_1 x_i))^2
@end example
@end ifinfo
@noindent
for the parameters @math{c_0}, @math{c_1}. For unweighted data the
sum is computed with @math{w_i = 1}.
@cindex covariance matrix, from linear regression
@deftypefun int gsl_fit_linear (const double * @var{x}, const size_t @var{xstride}, const double * @var{y}, const size_t @var{ystride}, size_t @var{n}, double * @var{c0}, double * @var{c1}, double * @var{cov00}, double * @var{cov01}, double * @var{cov11}, double * @var{sumsq})
This function computes the best-fit linear regression coefficients
(@var{c0},@var{c1}) of the model @math{Y = c_0 + c_1 X} for the datasets
(@var{x}, @var{y}), two vectors of length @var{n} with strides
@var{xstride} and @var{ystride}. The variance-covariance matrix for the
parameters (@var{c0}, @var{c1}) is estimated from the scatter of the
points around the best-fit line and returned via the parameters
(@var{cov00}, @var{cov01}, @var{cov11}). The sum of squares of the
residuals from the best-fit line is returned in @var{sumsq}.
@end deftypefun
@deftypefun int gsl_fit_wlinear (const double * @var{x}, const size_t @var{xstride}, const double * @var{w}, const size_t @var{wstride}, const double * @var{y}, const size_t @var{ystride}, size_t @var{n}, double * @var{c0}, double * @var{c1}, double * @var{cov00}, double * @var{cov01}, double * @var{cov11}, double * @var{chisq})
This function computes the best-fit linear regression coefficients
(@var{c0},@var{c1}) of the model @math{Y = c_0 + c_1 X} for the weighted
datasets (@var{x}, @var{y}), two vectors of length @var{n} with strides
@var{xstride} and @var{ystride}. The vector @var{w}, of length @var{n}
and stride @var{wstride}, specifies the weight of each datapoint. The
weight is the reciprocal of the variance for each datapoint in @var{y}.
The covariance matrix for the parameters (@var{c0}, @var{c1}) is
estimated from weighted data and returned via the parameters
(@var{cov00}, @var{cov01}, @var{cov11}). The weighted sum of squares of
the residuals from the best-fit line, @math{\chi^2}, is returned in
@var{chisq}.
@end deftypefun
@deftypefun int gsl_fit_linear_est (double @var{x}, double @var{c0}, double @var{c1}, double @var{c00}, double @var{c01}, double @var{c11}, double *@var{y}, double *@var{y_err})
This function uses the best-fit linear regression coefficients
@var{c0},@var{c1} and their estimated covariance
@var{cov00},@var{cov01},@var{cov11} to compute the fitted function
@var{y} and its standard deviation @var{y_err} for the model @math{Y =
c_0 + c_1 X} at the point @var{x}.
@end deftypefun
@node Linear fitting without a constant term
@section Linear fitting without a constant term
The functions described in this section can be used to perform
least-squares fits to a straight line model without a constant term,
@math{Y = c_1 X}. For weighted data the best-fit is found by minimizing
the weighted sum of squared residuals, @math{\chi^2},
@tex
\beforedisplay
$$
\chi^2 = \sum_i w_i (y_i - c_1 x_i)^2
$$
\afterdisplay
@end tex
@ifinfo
@example
\chi^2 = \sum_i w_i (y_i - c_1 x_i)^2
@end example
@end ifinfo
@noindent
for the parameter @math{c_1}. For unweighted data the sum is
computed with @math{w_i = 1}.
@deftypefun int gsl_fit_mul (const double * @var{x}, const size_t @var{xstride}, const double * @var{y}, const size_t @var{ystride}, size_t @var{n}, double * @var{c1}, double * @var{cov11}, double * @var{sumsq})
This function computes the best-fit linear regression coefficient
@var{c1} of the model @math{Y = c_1 X} for the datasets (@var{x},
@var{y}), two vectors of length @var{n} with strides @var{xstride} and
@var{ystride}. The variance of the parameter @var{c1} is estimated from
the scatter of the points around the best-fit line and returned via the
parameter @var{cov11}. The sum of squares of the residuals from the
best-fit line is returned in @var{sumsq}.
@end deftypefun
@deftypefun int gsl_fit_wmul (const double * @var{x}, const size_t @var{xstride}, const double * @var{w}, const size_t @var{wstride}, const double * @var{y}, const size_t @var{ystride}, size_t @var{n}, double * @var{c1}, double * @var{cov11}, double * @var{sumsq})
This function computes the best-fit linear regression coefficient
@var{c1} of the model @math{Y = c_1 X} for the weighted datasets
(@var{x}, @var{y}), two vectors of length @var{n} with strides
@var{xstride} and @var{ystride}. The vector @var{w}, of length @var{n}
and stride @var{wstride}, specifies the weight of each datapoint. The
weight is the reciprocal of the variance for each datapoint in @var{y}.
The variance of the parameter @var{c1} is estimated from the weighted
data and returned via the parameters @var{cov11}. The weighted sum of
squares of the residuals from the best-fit line, @math{\chi^2}, is
returned in @var{chisq}.
@end deftypefun
@deftypefun int gsl_fit_mul_est (double @var{x}, double @var{c1}, double @var{c11}, double *@var{y}, double *@var{y_err})
This function uses the best-fit linear regression coefficient @var{c1}
and its estimated covariance @var{cov11} to compute the fitted function
@var{y} and its standard deviation @var{y_err} for the model @math{Y =
c_1 X} at the point @var{x}.
@end deftypefun
@node Multi-parameter fitting
@section Multi-parameter fitting
@cindex multi-parameter regression
@cindex fits, multi-parameter linear
The functions described in this section perform least-squares fits to a
general linear model, @math{y = X c} where @math{y} is a vector of
@math{n} observations, @math{X} is an @math{n} by @math{p} matrix of
predictor variables, and @math{c} are the @math{p} unknown best-fit
parameters, which are to be estimated.
The best-fit is found by minimizing the weighted sums of squared
residuals, @math{\chi^2},
@tex
\beforedisplay
$$
\chi^2 = (y - X c)^T W (y - X c)
$$
\afterdisplay
@end tex
@ifinfo
@example
\chi^2 = (y - X c)^T W (y - X c)
@end example
@end ifinfo
@noindent
with respect to the parameters @math{c}. The weights are specified by
the diagonal elements of the @math{n} by @math{n} matrix @math{W}. For
unweighted data @math{W} is replaced by the identity matrix.
This formulation can be used for fits to any number of functions and/or
variables by preparing the @math{n}-by-@math{p} matrix @math{X}
appropriately. For example, to fit to a @math{p}-th order polynomial in
@var{x}, use the following matrix,
@tex
\beforedisplay
$$
X_{ij} = x_i^j
$$
\afterdisplay
@end tex
@ifinfo
@example
X_@{ij@} = x_i^j
@end example
@end ifinfo
@noindent
where the index @math{i} runs over the observations and the index
@math{j} runs from 0 to @math{p-1}.
To fit to a set of @math{p} sinusoidal functions with fixed frequencies
@math{\omega_1}, @math{\omega_2}, @dots{}, @math{\omega_p}, use,
@tex
\beforedisplay
$$
X_{ij} = \sin(\omega_j x_i)
$$
\afterdisplay
@end tex
@ifinfo
@example
X_@{ij@} = sin(\omega_j x_i)
@end example
@end ifinfo
@noindent
To fit to @math{p} independent variables @math{x_1}, @math{x_2}, @dots{},
@math{x_p}, use,
@tex
\beforedisplay
$$
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?