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. average  (  a ,  std=None ,  cov=None ,  axis=None  )[source]

The (weighted) average of a sample and its standard error

$$\begin{aligned} \bar{a} &= \frac{\sum_i w_ia_i}{\sum_i w_i},\quad w_i = \frac{1}{\Sigma_{ii}}\\[1ex] \sigma_{\bar{a}}^2 &= \frac{\sum_{ij} w_iw_j\Sigma_{ij}}{\left(\sum_i w_i\right)^2}, \end{aligned}$$

with the covariance matrix $\Sigma$ given by cov or by $\Sigma_{ij} = \delta_{ij}\sigma_i^2$ if std is given. If neither is given, the arithmetic mean and the standard error of the mean are returned.

Parameters:
aarray_like

The sample data.

stdarray_like | None

An array of standard deviations associated with the values in a. If specified, the weighted average of the uncorrelated sample data and its standard error is returned.

covscalar_nd | None

The covariance matrix associated with the values in a. If specified, it overrides std and the weighted average of the correlated sample data and its standard error is returned. If no axis is specified, cov must have shape (a.size, a.size) with elements associated with the values in the flattened a, otherwise cov must have either shape (a.shape[axis], a.shape[axis]) or (..., a.shape[axis - 1], a.shape[axis], a.shape[axis], a.shape[axis + 1], ...).

axisint_like | None

The axis along which the average is computed. If None, a is flattened.

Returns:
(av, av_d)tuple[ndarray, ndarray]

The average and its standard error for a given sample a along the specified axis.

Raises:
ValueError

If the shapes of a and cov do not match.