Go to the first, previous, next, last section, table of contents.

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},

for the parameters @math{c_0}, @math{c_1}. For unweighted data the sum is computed with @math{w_i = 1}.

Function: int gsl_fit_linear (const double * x, const size_t xstride, const double * y, const size_t ystride, size_t n, double * c0, double * c1, double * cov00, double * cov01, double * cov11, double * sumsq)
This function computes the best-fit linear regression coefficients (c0,c1) of the model @math{Y = c_0 + c_1 X} for the datasets (x, y), two vectors of length n with strides xstride and ystride. 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.

Function: int gsl_fit_wlinear (const double * x, const size_t xstride, const double * w, const size_t wstride, const double * y, const size_t ystride, size_t n, double * c0, double * c1, double * cov00, double * cov01, double * cov11, double * chisq)
This function computes the best-fit linear regression coefficients (c0,c1) of the model @math{Y = c_0 + c_1 X} 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 covariance matrix for the parameters (c0, c1) is estimated from weighted data and returned via the parameters (cov00, cov01, cov11). The weighted sum of squares of the residuals from the best-fit line, @math{\chi^2}, is returned in chisq.

Function: int gsl_fit_linear_est (double x, double c0, double c1, double c00, double c01, double c11, double *y, double *y_err)
This function uses the best-fit linear regression coefficients c0,c1 and their estimated covariance cov00,cov01,cov11 to compute the fitted function y and its standard deviation y_err for the model @math{Y = c_0 + c_1 X} at the point x.


Go to the first, previous, next, last section, table of contents.