scipp.var#

scipp.var(x, dim=None, *, ddof)#

Compute the variance of the input values.

This function computes the variance of the input values which is not the same as the x.variances property but instead defined as

\[\mathsf{var}(x) = \frac1{N - \mathsf{ddof}} \sum_{i=1}^{N}\, {(x_i - \bar{x})}^2\]

where \(x_i\) are the unmasked values of the input and \(\bar{x}\) is the mean, see scipp.mean(). See the ddof parameter description for what value to choose.

Note

Masks are broadcast to the shape of x. This can lead to a large temporary memory usage.

Parameters:
  • x (scipp.typing.VariableLike) – Input data.

  • dim (Union[str, Sequence[str], None], default: None) – Dimension(s) along which to calculate the variance. If not given, the variance over a flattened version of the array is calculated.

  • ddof (int) –

    ‘Delta degrees of freedom’. For sample variances, set ddof=1 to obtain an unbiased estimator. For normally distributed variables, set ddof=0 to obtain a maximum likelihood estimate. See numpy.var() for more details.

    In contrast to NumPy, this is a required parameter in Scipp to avoid potentially hard-to-find mistakes based on implicit assumptions about what the input data represents.

Returns:

Same type as x – The variance of the input values.

Raises:

See also

scipp.variances

Extract the stored variances of a scipp.Variable.

scipp.mean

Compute the arithmetic mean.

scipp.std

Compute the standard deviation.

scipp.nanvar

Ignore NaN’s when calculating the variance.

Examples

var is available as a method:

>>> x = sc.array(dims=['x'], values=[3, 5, 2, 3])
>>> x.var(ddof=0)
<scipp.Variable> ()    float64  [dimensionless]  1.1875
>>> x.var(ddof=1)
<scipp.Variable> ()    float64  [dimensionless]  1.58333

Select a dimension to reduce:

>>> x = sc.array(dims=['x', 'y'], values=[[1, 3, 6], [2, 7, 4]])
>>> x.var('y', ddof=0)
<scipp.Variable> (x: 2)    float64  [dimensionless]  [4.22222, 4.22222]
>>> x.var('x', ddof=0)
<scipp.Variable> (y: 3)    float64  [dimensionless]  [0.25, 4, 1]