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_monte_carlo  (  x ,  cov=None ,  axis=None ,  optimize_cov=False ,  n_samples=None ,  n_accepted=None ,  optimize_sampling=True ,  return_samples=False ,  method='py' ,  report=False ,  **kwargs  )[source]

Maximum likelihood Monte-Carlo sampling of 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 the supplementary material of [Gebert et al., Phys. Rev. Lett. 115, 053003 (2015)].

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. If None, all covariance matrices are identity matrices.

axisint_like | 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.

n_samplesint_like | None

The number of samples generated for each data point. If None and method == "cpp", samples are generated until n_accepted samples get accepted.

n_acceptedint_like | None

The number of samples to be accepted for each data point. Only available if method == "cpp".

optimize_samplingbool

Whether to optimize the data sampling for acceptance efficiency.

return_samplesbool

Whether to also return the generated points $\vec{p}_i$ with shape (n_accepted, k, n).

methodstr

The method to generate the collinear points. Can be one of {"py", "cpp"}. The "py" version is faster but only allows to specify n_samples. The "cpp" version is slower but allows to specify both n_accepted and n_samples.

reportbool

Whether to print the result of the fit.

kwargsAny

Additional keyword arguments to be passed to the chosen method. "py": {}, "cpp": {seed: None}.

Returns:
(popt, pcov, p)Any

The optimized parameters and their covariances. If return_samples is True, also the generated points $\vec{p}_i$ are returned. The resulting shapes are (2 * n, ), (2 * n, 2 * n) and (n_accepted, k, n).

Raises:
ValueError

If method is not supported.