scipy.stats.

# gstd#

scipy.stats.gstd(a, axis=0, ddof=1)[source]#

Calculate the geometric standard deviation of an array.

The geometric standard deviation describes the spread of a set of numbers where the geometric mean is preferred. It is a multiplicative factor, and so a dimensionless quantity.

It is defined as the exponential of the standard deviation of the natural logarithms of the observations.

Parameters:
aarray_like

An array containing finite, strictly positive, real numbers.

Deprecated since version 1.14.0: Support for masked array input was deprecated in SciPy 1.14.0 and will be removed in version 1.16.0.

axisint, tuple or None, optional

Axis along which to operate. Default is 0. If None, compute over the whole array a.

ddofint, optional

Degree of freedom correction in the calculation of the geometric standard deviation. Default is 1.

Returns:
gstdndarray or float

An array of the geometric standard deviation. If axis is None or a is a 1d array a float is returned.

gmean

Geometric mean

numpy.std

Standard deviation

gzscore

Geometric standard score

Notes

Mathematically, the sample geometric standard deviation $$s_G$$ can be defined in terms of the natural logarithms of the observations $$y_i = \log(x_i)$$:

$s_G = \exp(s), \quad s = \sqrt{\frac{1}{n - d} \sum_{i=1}^n (y_i - \bar y)^2}$

where $$n$$ is the number of observations, $$d$$ is the adjustment ddof to the degrees of freedom, and $$\bar y$$ denotes the mean of the natural logarithms of the observations. Note that the default ddof=1 is different from the default value used by similar functions, such as numpy.std and numpy.var.

When an observation is infinite, the geometric standard deviation is NaN (undefined). Non-positive observations will also produce NaNs in the output because the natural logarithm (as opposed to the complex logarithm) is defined and finite only for positive reals. The geometric standard deviation is sometimes confused with the exponential of the standard deviation, exp(std(a)). Instead, the geometric standard deviation is exp(std(log(a))).

References

[1]

“Geometric standard deviation”, Wikipedia, https://en.wikipedia.org/wiki/Geometric_standard_deviation.

[2]

Kirkwood, T. B., “Geometric means and measures of dispersion”, Biometrics, vol. 35, pp. 908-909, 1979

Examples

Find the geometric standard deviation of a log-normally distributed sample. Note that the standard deviation of the distribution is one; on a log scale this evaluates to approximately exp(1).

>>> import numpy as np
>>> from scipy.stats import gstd
>>> rng = np.random.default_rng()
>>> sample = rng.lognormal(mean=0, sigma=1, size=1000)
>>> gstd(sample)
2.810010162475324


Compute the geometric standard deviation of a multidimensional array and of a given axis.

>>> a = np.arange(1, 25).reshape(2, 3, 4)
>>> gstd(a, axis=None)
2.2944076136018947
>>> gstd(a, axis=2)
array([[1.82424757, 1.22436866, 1.13183117],
[1.09348306, 1.07244798, 1.05914985]])
>>> gstd(a, axis=(1,2))
array([2.12939215, 1.22120169])