Repository: scipy/scipy
File: scipy/stats/_stats_py.py (Python)

Code before:

    >>> from scipy.stats import skewtest
    >>> skewtest([1, 2, 3, 4, 5, 6, 7, 8])
    SkewtestResult(statistic=1.0108048609177787, pvalue=0.3121098361421897)
    >>> skewtest([2, 8, 0, 4, 1, 9, 9, 0])
    SkewtestResult(statistic=0.44626385374196975, pvalue=0.6554066631275459)
    >>> skewtest([1, 2, 3, 4, 5, 6, 7, 8000])
    SkewtestResult(statistic=3.571773510360407, pvalue=0.0003545719905823133)
    >>> skewtest([100, 100, 100, 100, 100, 100, 100, 101])
    SkewtestResult(statistic=3.5717766638478072, pvalue=0.000354567720281634)
    >>> skewtest([1, 2, 3, 4, 5, 6, 7, 8], alternative='less')
    SkewtestResult(statistic=1.0108048609177787, pvalue=0.8439450819289052)
    >>> skewtest([1, 2, 3, 4, 5, 6, 7, 8], alternative='greater')
    SkewtestResult(statistic=1.0108048609177787, pvalue=0.15605491807109484)

    For a more detailed example, see :ref:`hypothesis_skewtest`.
    """
    xp = array_namespace(a)
    a, axis = _chk_asarray(a, axis, xp=xp)

    b2 = skew(a, axis, _no_deco=True)

    n = xp.asarray(_length_nonmasked(a, axis), dtype=b2.dtype, device=xp_device(a))
    n = xpx.at(n, n < 8).set(xp.nan)

    with np.errstate(divide='ignore', invalid='ignore'):
        y = b2 * xp.sqrt(((n + 1) * (n + 3)) / (6.0 * (n - 2)))
        beta2 = (3.0 * (n**2 + 27*n - 70) * (n+1) * (n+3) /
                 ((n-2.0) * (n+5) * (n+7) * (n+9)))
        W2 = -1 + xp.sqrt(2 * (beta2 - 1))
        delta = 1 / xp.sqrt(0.5 * xp.log(W2))
        alpha = xp.sqrt(2.0 / (W2 - 1))
        y = xp.where(y == 0, 1., y)
        Z = delta * xp.log(y / alpha + xp.sqrt((y / alpha)**2 + 1))
        pvalue = _get_pvalue(Z, _SimpleNormal(), alternative, xp=xp)

    Z = Z[()] if Z.ndim == 0 else Z
    pvalue = pvalue[()] if pvalue.ndim == 0 else pvalue
    return SkewtestResult(Z, pvalue)


KurtosistestResult = namedtuple('KurtosistestResult', ('statistic', 'pvalue'))


@xp_capabilities(jax_jit=False)
@_axis_nan_policy_factory(KurtosistestResult, n_samples=1, too_small=4)
def kurtosistest(a, axis=0, nan_policy='propagate', alternative='two-sided'):
    r"""Test whether a dataset has normal kurtosis.

    This function tests the null hypothesis that the kurtosis
    of the population from which the sample was drawn is that

Review comment (suggestion):
Small sample warnings are emitted by the decorator for eager backends; no warnings are emitted for lazy backends (so we can remove restrictions on Dask arrays and JAX JIT).

Code after:
    >>> from scipy.stats import skewtest
    >>> skewtest([1, 2, 3, 4, 5, 6, 7, 8])
    SkewtestResult(statistic=1.0108048609177787, pvalue=0.3121098361421897)
    >>> skewtest([2, 8, 0, 4, 1, 9, 9, 0])
    SkewtestResult(statistic=0.44626385374196975, pvalue=0.6554066631275459)
    >>> skewtest([1, 2, 3, 4, 5, 6, 7, 8000])
    SkewtestResult(statistic=3.571773510360407, pvalue=0.0003545719905823133)
    >>> skewtest([100, 100, 100, 100, 100, 100, 100, 101])
    SkewtestResult(statistic=3.5717766638478072, pvalue=0.000354567720281634)
    >>> skewtest([1, 2, 3, 4, 5, 6, 7, 8], alternative='less')
    SkewtestResult(statistic=1.0108048609177787, pvalue=0.8439450819289052)
    >>> skewtest([1, 2, 3, 4, 5, 6, 7, 8], alternative='greater')
    SkewtestResult(statistic=1.0108048609177787, pvalue=0.15605491807109484)

    For a more detailed example, see :ref:`hypothesis_skewtest`.
    """
    xp = array_namespace(a)
    a, axis = _chk_asarray(a, axis, xp=xp)

    b2 = skew(a, axis, _no_deco=True)

    n = xp.asarray(_length_nonmasked(a, axis), dtype=b2.dtype, device=xp_device(a))
    n = xpx.at(n, n < 8).set(xp.nan)

    with np.errstate(divide='ignore', invalid='ignore'):
        y = b2 * xp.sqrt(((n + 1) * (n + 3)) / (6.0 * (n - 2)))
        beta2 = (3.0 * (n**2 + 27*n - 70) * (n+1) * (n+3) /
                 ((n-2.0) * (n+5) * (n+7) * (n+9)))
        W2 = -1 + xp.sqrt(2 * (beta2 - 1))
        delta = 1 / xp.sqrt(0.5 * xp.log(W2))
        alpha = xp.sqrt(2.0 / (W2 - 1))
        y = xp.where(y == 0, 1., y)
        Z = delta * xp.log(y / alpha + xp.sqrt((y / alpha)**2 + 1))
        pvalue = _get_pvalue(Z, _SimpleNormal(), alternative, xp=xp)

    Z = Z[()] if Z.ndim == 0 else Z
    pvalue = pvalue[()] if pvalue.ndim == 0 else pvalue
    return SkewtestResult(Z, pvalue)


KurtosistestResult = namedtuple('KurtosistestResult', ('statistic', 'pvalue'))


@xp_capabilities()
@_axis_nan_policy_factory(KurtosistestResult, n_samples=1, too_small=4)
def kurtosistest(a, axis=0, nan_policy='propagate', alternative='two-sided'):
    r"""Test whether a dataset has normal kurtosis.

    This function tests the null hypothesis that the kurtosis
    of the population from which the sample was drawn is that
    of the normal distribution.