qspec logo qspec logo

A physics toolbox for laser spectroscopy.

Static Badge Static Badge Static Badge

Static Badge Static Badge

© 2026 Patrick Müller. All rights reserved.

qspec.analyze. linear_fit  (  x ,  y ,  sigma_x=None ,  sigma_y=None ,  corr=None ,  report=False ,  **kwargs  )[source]

Maximum likelihood fit for a straight line through points $\vec{\mu}_i\in\mathbb{R}^2$ with covariances $\mathbf{\Sigma}_i\in\mathbb{R}^{2\times 2}$, assuming $2$-dimensional multivariate normal distributions $\mathcal{N}(\vec{\mu}_i, \mathbf{\Sigma}_i)$. The data points and covariances are given by

$$ \vec{\mu} = \begin{pmatrix}x\\y\end{pmatrix},\quad \mathbf{\Sigma} = \begin{pmatrix} \sigma_x^2 & \rho_{xy}\sigma_x\sigma_y \\ \rho_{xy}\sigma_x\sigma_y & \sigma_y^2 \end{pmatrix}. $$

This is a wrapper for the more general linear_nd_fit function. The algorithm is described in qspec's publication.

Parameters:
xarray_like

The $x$ data.

yarray_like

The $y$ data.

sigma_xarray_like | None

The standard deviation $\sigma_x$ of the x data. If None, it is 1.

sigma_yarray_like | None

The standard deviation $\sigma_y$ of the y data. If None, it is 1.

corrarray_like | None

The correlation coefficients $\rho_{xy}$ between the x and y data. If None, it is 0.

reportbool

Whether to print the result of the fit.

kwargsAny

Additional keyword arguments are accepted for compatibility with the other linear regression algorithms and ignored.

Returns:
(popt, pcov)tuple[ndarray, ndarray]

The best y-intercept and slope and their covariance matrix.

Raises:
numpy.linalg.LinAlgError

If a covariance matrix cannot be inverted.