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

Code before:
            # keepdims=True doesn't currently work for lazy arrays
            return _stats_py.trim_mean(x, proportiontocut, axis=-1)[..., xp.newaxis]

    Nis = [sample.shape[-1] for sample in samples]
    Ycis = [func(sample) for sample in samples]
    Ntot = sum(Nis)

    # compute Zij's
    Zijs = [xp.abs(sample - Yc) for sample, Yc in zip(samples, Ycis)]

    # compute Zbari
    Zbaris = [xp.mean(Zij, axis=-1, keepdims=True) for Zij in Zijs]
    Zbar = sum(Ni*Zbari for Ni, Zbari in zip(Nis, Zbaris)) / Ntot

    # compute numerator and denominator
    dfd = (Ntot - k)
    numer = dfd * sum(Ni * (Zbari - Zbar)**2
                      for Ni, Zbari in zip(Nis, Zbaris))
    dfn = (k - 1.0)
    denom = dfn * sum(xp.sum((Zij - Zbari)**2, axis=-1, keepdims=True)
                      for Zij, Zbari in zip(Zijs, Zbaris))

    W = numer / denom
    W = xp.squeeze(W, axis=-1)
    dfn, dfd = xp.asarray(dfn, dtype=W.dtype), xp.asarray(dfd, dtype=W.dtype)
    pval = _get_pvalue(W, _SimpleF(dfn, dfd), 'greater', xp=np)
    return LeveneResult(W[()], pval[()])


def _apply_func(x, g, func):
    # g is list of indices into x
    #  separating x into different groups
    #  func should be applied over the groups
    g = unique(r_[0, g, len(x)])
    output = [func(x[g[k]:g[k+1]]) for k in range(len(g) - 1)]

    return asarray(output)


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


@xp_capabilities(np_only=True)
@_axis_nan_policy_factory(FlignerResult, n_samples=None)
def fligner(*samples, center='median', proportiontocut=0.05):
    r"""Perform Fligner-Killeen test for equality of variance.

    Fligner's test tests the null hypothesis that all input samples
    are from populations with equal variances.  Fligner-Killeen's test is
    distribution free when populations are identical [2]_.


Review comment (bug):
```python3
import torch as xp
from scipy import special
x = xp.asarray(7., dtype=xp.float32)
special.fdtrc(5., 6., x).dtype  # torch.float64
```
I'd think this should either fail (e.g. `special_gammainc(): argument 'input' (position 1) must be Tensor, not float`) or maintain the dtype of the array argument. Is this intended @steppi?

Anyway, that's why I explicitly convert `dfn` and `dfd` to arrays of the same dtype.

Code after:
            # keepdims=True doesn't currently work for lazy arrays
            return _stats_py.trim_mean(x, proportiontocut, axis=-1)[..., xp.newaxis]

    Nis = [sample.shape[-1] for sample in samples]
    Ycis = [func(sample) for sample in samples]
    Ntot = sum(Nis)

    # compute Zij's
    Zijs = [xp.abs(sample - Yc) for sample, Yc in zip(samples, Ycis)]

    # compute Zbari
    Zbaris = [xp.mean(Zij, axis=-1, keepdims=True) for Zij in Zijs]
    Zbar = sum(Ni*Zbari for Ni, Zbari in zip(Nis, Zbaris)) / Ntot

    # compute numerator and denominator
    dfd = (Ntot - k)
    numer = dfd * sum(Ni * (Zbari - Zbar)**2
                      for Ni, Zbari in zip(Nis, Zbaris))
    dfn = (k - 1.0)
    denom = dfn * sum(xp.sum((Zij - Zbari)**2, axis=-1, keepdims=True)
                      for Zij, Zbari in zip(Zijs, Zbaris))

    W = numer / denom
    W = xp.squeeze(W, axis=-1)
    dfn, dfd = xp.asarray(dfn, dtype=W.dtype), xp.asarray(dfd, dtype=W.dtype)
    pval = _get_pvalue(W, _SimpleF(dfn, dfd), 'greater', xp=xp)
    return LeveneResult(W[()], pval[()])


def _apply_func(x, g, func):
    # g is list of indices into x
    #  separating x into different groups
    #  func should be applied over the groups
    g = unique(r_[0, g, len(x)])
    output = [func(x[g[k]:g[k+1]]) for k in range(len(g) - 1)]

    return asarray(output)


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


@xp_capabilities(np_only=True)
@_axis_nan_policy_factory(FlignerResult, n_samples=None)
def fligner(*samples, center='median', proportiontocut=0.05):
    r"""Perform Fligner-Killeen test for equality of variance.

    Fligner's test tests the null hypothesis that all input samples
    are from populations with equal variances.  Fligner-Killeen's test is
    distribution free when populations are identical [2]_.