Skip to main content
`Ctrl`+`K`
scipy.stats.

# kstat

scipy.stats.kstat(data, n=2, *, axis=None, nan_policy='propagate', keepdims=False)

  Return the n th k-statistic ( `1<=n<=4` so far).

  The n th k-statistic `k_n` is the unique symmetric unbiased estimator of the n th cumulant \(\kappa_n\) [1] [2].

  Parameters:
    data array_like

      Input array.

    n int, {1, 2, 3, 4}, optional

      Default is equal to 2.

    axis int or None, default: None

      If an int, the axis of the input along which to compute the statistic. The statistic of each axis-slice (e.g. row) of the input will appear in a corresponding element of the output. If `None`, the input will be raveled before computing the statistic.

    nan_policy{‘propagate’, ‘omit’, ‘raise’}

      Defines how to handle input NaNs.

      - `propagate`: if a NaN is present in the axis slice (e.g. row) along which the statistic is computed, the corresponding entry of the output will be NaN.

      - `omit`: NaNs will be omitted when performing the calculation. If insufficient data remains in the axis slice along which the statistic is computed, the corresponding entry of the output will be NaN.

      - `raise`: if a NaN is present, a `ValueError` will be raised.

    keepdims bool, default: False

      If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.

  Returns:
    kstat float

      The n th k-statistic.

  See also

  `kstatvar`

    Returns an unbiased estimator of the variance of the k-statistic

  `moment`

    Returns the n-th central moment about the mean for a sample.

  Notes

  For a sample size \(n\), the first few k-statistics are given by

  \[\begin{split}k_1 &= \frac{S_1}{n}, \\ k_2 &= \frac{nS_2 - S_1^2}{n(n-1)}, \\ k_3 &= \frac{2S_1^3 - 3nS_1S_2 + n^2S_3}{n(n-1)(n-2)}, \\ k_4 &= \frac{-6S_1^4 + 12nS_1^2S_2 - 3n(n-1)S_2^2 - 4n(n+1)S_1S_3 + n^2(n+1)S_4}{n (n-1)(n-2)(n-3)},\end{split}\]

  where

  \[S_r \equiv \sum_{i=1}^n X_i^r,\]

  and \(X_i\) is the \(i\) th data point.

  Beginning in SciPy 1.9, `np.matrix` inputs (not recommended for new code) are converted to `np.ndarray` before the calculation is performed. In this case, the output will be a scalar or `np.ndarray` of appropriate shape rather than a 2D `np.matrix`. Similarly, while masked elements of masked arrays are ignored, the output will be a scalar or `np.ndarray` rather than a masked array with `mask=False`.

  Array API Standard Support

  `kstat` has experimental support for Python Array API Standard compatible backends in addition to NumPy. Please consider testing these features by setting an environment variable `SCIPY_ARRAY_API=1` and providing CuPy, PyTorch, JAX, or Dask arrays as array arguments. The following combinations of backend and device (or other capability) are supported.

  Library | CPU | GPU
  --- | --- | ---
  NumPy | ✅ | n/a
  CuPy | n/a | ✅
  PyTorch | ✅ | ✅
  JAX | ✅ | ✅
  Dask | ✅ | n/a

  > See Support for the array API standard for more information.

  References

  [1]

  http://mathworld.wolfram.com/k-Statistic.html

  [2]

  http://mathworld.wolfram.com/Cumulant.html

  Examples

      >>> from scipy import stats
      >>> from numpy.random import default_rng
      >>> rng = default_rng()

  As sample size increases, n-th moment and n-th k-statistic converge to the same number (although they aren’t identical). In the case of the normal distribution, they converge to zero.

      >>> for i in range(2,8):
      ...     x = rng.normal(size=10**i)
      ...     m, k = stats.moment(x, 3), stats.kstat(x, 3)
      ...     print(f"{i=}: {m=:.3g}, {k=:.3g}, {(m-k)=:.3g}")
      i=2: m=-0.631, k=-0.651, (m-k)=0.0194  # random
      i=3: m=0.0282, k=0.0283, (m-k)=-8.49e-05
      i=4: m=-0.0454, k=-0.0454, (m-k)=1.36e-05
      i=6: m=7.53e-05, k=7.53e-05, (m-k)=-2.26e-09
      i=7: m=0.00166, k=0.00166, (m-k)=-4.99e-09
      i=8: m=-2.88e-06 k=-2.88e-06, (m-k)=8.63e-13