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_nd_fit  (  x ,  cov=None ,  p0=None ,  axis=None ,  optimize_cov=False ,  **kwargs  )[source]

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

The algorithm is described in qspec's publication.

Parameters:
xarray_like

The data vectors $\vec{\mu}_i$. Must have shape (k, n), where k is the number of data points and n is the number of dimensions of each point.

covarray_like | None

The covariance matrices $\mathbf{\Sigma}_i$ of the data vectors. Must have shape (k, n, n). Use covariance_matrix to construct covariance matrices.

p0array_like | None

The start parameters for the linear fit. Must have shape (2 * n, ). The first n elements specify the origin vector of the straight line, the second n elements specify the direction of the straight line.

axisint | None

The index of the vector component of the n-dimensional vectors that are fixed for fitting. This is required since a straight line in n dimensions is fully described by 2 * (n - 1) parameters. If None, the best axis is determined from the data, and the direction vector of the straight line is normalized.

optimize_covbool

If True, the origin vector of the straight line is optimized to yield the smallest covariances.

kwargsAny

Additional keyword arguments are accepted for compatibility with linear_nd_monte_carlo and ignored.

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

The optimized parameters and their covariances. The resulting shapes are (2 * n, ) and (2 * n, 2 * n).

Raises:
numpy.linalg.LinAlgError

If a covariance matrix cannot be inverted.