⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fitting.texi

📁 开放gsl矩阵运算
💻 TEXI
📖 第 1 页 / 共 2 页
字号:
@cindex fitting@cindex least squares fit@cindex regression, least squaresThis chapter describes routines for performing least squares fits toexperimental data using linear combinations of functions. The data maybe weighted or unweighted.  For weighted data the functions compute thebest fit parameters and their associated covariance matrix.  Forunweighted data the covariance matrix is estimated from the scatter ofthe points, giving a variance-covariance matrix. The functions aredivided into separate versions for simple one- or two-parameterregression and multiple-parameter fits.  The functions are declared inthe 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 regressionThe functions described in this section can be used to performleast-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 ofsquared 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@noindentfor the parameters @math{c_0}, @math{c_1}.  For unweighted data thesum is computed with @math{w_i = 1}.@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 theparameters (@var{c0}, @var{c1}) is estimated from the scatter of thepoints around the best-fit line and returned via the parameters(@var{cov00}, @var{cov01}, @var{cov11}).  The sum of squares of theresiduals 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 weighteddatasets (@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. Theweight is the reciprocal of the variance for each datapoint in @var{y}.The covariance matrix for the parameters (@var{c0}, @var{c1}) isestimated from weighted data and returned via the parameters(@var{cov00}, @var{cov01}, @var{cov11}).  The weighted sum of squares ofthe 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 termThe functions described in this section can be used to performleast-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 minimizingthe 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@noindentfor the parameter @math{c_1}.  For unweighted data the sum iscomputed 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 fromthe scatter of the points around the best-fit line and returned via theparameter @var{cov11}.  The sum of squares of the residuals from thebest-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. Theweight is the reciprocal of the variance for each datapoint in @var{y}.The variance of the parameter @var{c1} is estimated from the weighteddata and returned via the parameters @var{cov11}.  The weighted sum ofsquares of the residuals from the best-fit line, @math{\chi^2}, isreturned 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 fittingThe functions described in this section perform least-squares fits to ageneral 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 ofpredictor variables, and @math{c} are the @math{p} unknown best-fitparameters, which are to be estimated.The best-fit is found by minimizing the weighted sums of squaredresiduals, @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@noindentwith respect to the parameters @math{c}. The weights are specified bythe diagonal elements of the @math{n} by @math{n} matrix @math{W}.  Forunweighted data @math{W} is replaced by the identity matrix.This formulation can be used for fits to any number of functions and/orvariables 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@exampleX_@{ij@} = x_i^j@end example@end ifinfo@noindentwhere 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@exampleX_@{ij@} = sin(\omega_j x_i)@end example@end ifinfo@noindentTo fit to @math{p} independent variables @math{x_1}, @math{x_2}, @dots{},@math{x_p}, use,@tex\beforedisplay$$X_{ij} = x_j(i)$$\afterdisplay@end tex@ifinfo@exampleX_@{ij@} = x_j(i)@end example@end ifinfo@noindentwhere @math{x_j(i)} is the @math{i}-th value of the predictor variable@math{x_j}.The functions described in this section are declared in the header file@file{gsl_multifit.h}.The solution of the general linear least-squares system requires anadditional working space for intermediate results, such as the singularvalue decomposition of the matrix @math{X}.@deftypefun {gsl_multifit_linear_workspace *} gsl_multifit_linear_alloc (size_t @var{n}, size_t @var{p})This function allocates a workspace for fitting a model to @var{n}observations using @var{p} parameters.@end deftypefun@deftypefun void gsl_multifit_linear_free (gsl_multifit_linear_workspace * @var{work})This function frees the memory associated with the workspace @var{w}.@end deftypefun@deftypefun int gsl_multifit_linear (const gsl_matrix * @var{X}, const gsl_vector * @var{y}, gsl_vector * @var{c}, gsl_matrix * @var{cov}, double * @var{chisq}, gsl_multifit_linear_workspace * @var{work})This function computes the best-fit parameters @var{c} of the model@math{y = X c} for the observations @var{y} and the matrix of predictorvariables @var{X}.  The variance-covariance matrix of the modelparameters @var{cov} is estimated from the scatter of the observationsabout the best-fit.  The sum of squares of the residuals from thebest-fit, @math{\chi^2}, is returned in @var{chisq}. The best-fit is found by singular value decomposition of the matrix@var{X} using the preallocated workspace provided in @var{work}. Themodified Golub-Reinsch SVD algorithm is used, with column scaling toimprove the accuracy of the singular values. Any components which havezero singular value (to machine precision) are discarded from the fit.@end deftypefun@deftypefun int gsl_multifit_wlinear (const gsl_matrix * @var{X}, const gsl_vector * @var{w}, const gsl_vector * @var{y}, gsl_vector * @var{c}, gsl_matrix * @var{cov}, double * @var{chisq}, gsl_multifit_linear_workspace * @var{work})This function computes the best-fit parameters @var{c} of the model@math{y = X c} for the observations @var{y} and the matrix of predictorvariables @var{X}.  The covariance matrix of the model parameters@var{cov} is estimated from the weighted data.  The weighted sum ofsquares of the residuals from the best-fit, @math{\chi^2}, is returnedin @var{chisq}.The best-fit is found by singular value decomposition of the matrix@var{X} using the preallocated workspace provided in @var{work}. Anycomponents which have zero singular value (to machine precision) arediscarded from the fit.@end deftypefun@node Fitting Examples@section ExamplesThe following program computes a least squares straight-line fit to asimple (fictitious) dataset, and outputs the best-fit line and itsassociated one standard-deviation error bars.@example#include <stdio.h>#include <gsl/gsl_fit.h>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -