linear_regression(xdata, ydata, weights=None, p0=None, intercept_origin=True, **kwargs)[source]

Use linear least squares to fit a function, f, to data. This method is a generalized version of scipy.optimize.curve_fit(); allowing for Ordinary Least Square and Weighted Least Square regressions:

  • OLS through origin: linear_regression(xdata, ydata)

  • OLS with any intercept: linear_regression(xdata, ydata, intercept_origin=False)

  • WLS through origin: linear_regression(xdata, ydata, weights)

  • WLS with any intercept: linear_regression(xdata, ydata, weights, intercept_origin=False)

If the expected values of slope (and intercept) are different from 0.0, provide the p0 value(s).

  • xdata – The independent variable where the data is measured.

  • ydata – The dependent data - nominally f(xdata, …)

  • weights – If not None, the uncertainties in the ydata array. These are used as weights in the least-squares problem. If None, the uncertainties are assumed to be 1. In SciPy vocabulary, our weights are 1/sigma.

  • p0 – Initial guess for the parameters. If None, then the initial values will all be 0 (Different from SciPy where all are 1)

  • intercept_origin – If True: solves y=a*x (default); if False: solves y=a*x+b.

Extra keword arguments will be passed to scipy.optimize.curve_fit().

Return type:



(slope, std_slope) if intercept_origin is True; (slope, intercept, std_slope, std_intercept) if False.