Next: , Previous: , Up: Internal Routines   [Contents][Index]

#### 15.5.362 lsq

```lsq(x, y [, weights=w, cov=cov, err=err, chisq=chisq, /formal, /reduce])```

Returns a linear least-squares fit of `y` to `x`. `x` must have either the same dimensional structure as `y`, or the same plus one more dimension. In the former case, the number of parameters to be fitted is taken equal to 1, otherwise it is equal to the number of elements in the last dimension of `x`. The model to be fitted is ```y = a(0)*x(...,0) + a(1)*x(...,1) + ...```, where `a` stands for the parameters sought from the fit, which are returned by this function.

Optionally, a weight can be assigned to each data point, in `w`. These weights should be equal to the inverse of the variance associated with each data point.

If `cov` is specified, then by default a covariance matrix of the parameters of the fit is returned in it, with the variances on the diagonal and the covariances in the off-diagonal elements. If `/reduce` is specified, then a reduced covariance matrix is returned instead, in which the diagonal elements contain the standard deviations, the off-diagonal part that contains most of the first dimension contains cross-correlation coefficients, and the off-diagonal part that contains most of the second dimension contains the covariances (the same as when `/reduce` is not specified). The matrix is square in any case.

If `err` is specified, then the weighted root-mean-square value of the residual errors (after subtraction of the model values) is returned in it.

If `chisq` is specified, then the chi-square value of the fit is returned in it.

The magnitudes of the (co)variances in `cov` and of `chisq` are only meaningful if proper weights were specified in `w` as described above. In that case, chi-square should ideally be equal to the number of data points minus one. If the chi-square is much smaller than that, then the variances `1/w` were apparently overestimated. If the chi-square is much greater than that, then either the variances were underestimated, or the linear model is not suitable for the data.

If no weights were specified, or if the proper normalization of the values specified in `w` is not known, then you can derive formal estimates for the errors on the model parameters by specifying the `/formal` keyword. In that case, the variance on the data values is assumed equal to `err^2*n/(n - 1)`, where `n` is the number of data points (taken to be equal to the sum of all elements of `w` if `w` is specified), and the variances on the model parameters are scaled accordingly. (The correction factor involving `n` is necessary to get an unbiased estimator.) This corresponds to forcing chi-square equal to the ideal value of `n - 1`.