kstat#
- scipy.stats.kstat(data, n=2, *, axis=None, nan_policy='propagate', keepdims=False)[source]#
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:
- dataarray_like
Input array.
- nint, {1, 2, 3, 4}, optional
Default is equal to 2.
- axisint 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, aValueError
will be raised.
- keepdimsbool, 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:
- kstatfloat
The n th k-statistic.
See also
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 tonp.ndarray
before the calculation is performed. In this case, the output will be a scalar ornp.ndarray
of appropriate shape rather than a 2Dnp.matrix
. Similarly, while masked elements of masked arrays are ignored, the output will be a scalar ornp.ndarray
rather than a masked array withmask=False
.References
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