scipp.std#

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

Compute the standard deviation of the input values.

This function computes the standard deviation of the input values which is not related to the x.variances property but instead defined as

\[\mathsf{std}(x)^2 = \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 standard deviation. If not given, the standard deviation over a flattened version of the array is calculated.

  • ddof (int) –

    ‘Delta degrees of freedom’. For sample standard deviations, set ddof=1 to obtain an unbiased estimator. For normally distributed variables, set ddof=0 to obtain a maximum likelihood estimate. See numpy.std() 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 standard deviation of the input values.

Raises:

See also

scipp.stddevs

Compute the standard deviations from the stored variances of a scipp.Variable.

scipp.mean

Compute the arithmetic mean.

scipp.var

Compute the variance.

scipp.nanstd

Ignore NaN’s when calculating the standard deviation.

Examples

std is available as a method:

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

Select a dimension to reduce:

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