scipp.nanvar#

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

Compute the variance of the input values ignoring NaN’s.

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

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

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

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

  • dim (Union[None, str, Sequence[str]], 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.nanvar() 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 non-NaN input values.

Raises:

See also

scipp.nanmean

Compute the arithmetic mean ignoring NaN’s.

scipp.nanstd

Compute the standard deviation, ignoring NaN’s.

scipp.var

Compute the variance without special handling of NaN’s.

Examples

nanvar is available as a method:

>>> x = sc.array(dims=['x'], values=[np.nan, 5, 2, 3])
>>> x.nanvar(ddof=0)
<scipp.Variable> ()    float64  [dimensionless]  1.55556
>>> x.nanvar(ddof=1)
<scipp.Variable> ()    float64  [dimensionless]  2.33333