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

Code before:
    inv_sq_se = 1 / standard_errors_squared
    weights = inv_sq_se / xp.sum(inv_sq_se, axis=0, keepdims=True)

    # (3) determine variance-weighted estimate of the common mean
    # Consider replacing with vecdot when data-apis/array-api#910 is resolved
    var_w = xp.sum(weights * means, axis=0, keepdims=True)

    # (4) determine one-sample t statistic for each group
    t_stats = _demean(means, var_w, axis=0, xp=xp) / standard_errors

    # calculate parameters to be used in transformation
    v = xp.asarray(lengths, dtype=t_stats.dtype) - 1
    # align along 0th axis, which corresponds with separate samples
    v = xp.reshape(v, (-1,) + (1,)*(t_stats.ndim-1))
    a = v - .5
    b = 48 * a**2
    c = (a * xp.log(1 + (t_stats ** 2)/v))**.5

    # (8) perform a normalizing transformation on t statistic
    z = (c + ((c**3 + 3*c)/b) -
         ((4*c**7 + 33*c**5 + 240*c**3 + 855*c) /
          (b**2*10 + 8*b*c**4 + 1000*b)))

    # (9) calculate statistic
    A = xp.vecdot(z, z, axis=-z.ndim)
    A = A[()] if A.ndim == 1 else A  # data-apis/array-api-compat#355

    # "[the p value is determined from] central chi-square random deviates
    # with k - 1 degrees of freedom". Alexander, Govern (94)
    df = xp.asarray(len(samples) - 1, dtype=A.dtype)
    chi2 = _SimpleChi2(df)
    p = _get_pvalue(A, chi2, alternative='greater', symmetric=False, xp=xp)
    return AlexanderGovernResult(A, p)


def _alexandergovern_input_validation(samples, nan_policy, axis, xp):
    if len(samples) < 2:
        raise TypeError(f"2 or more inputs required, got {len(samples)}")

    for sample in samples:
        if sample.shape[axis] <= 1:
            raise ValueError("Input sample size must be greater than one.")

    samples = [xp.moveaxis(sample, axis, -1) for sample in samples]

    return samples


def _pearsonr_fisher_ci(r, n, confidence_level, alternative):
    """
    Compute the confidence interval for Pearson's R.

Review comment (suggestion):
```suggestion
    A = A[()] if A.ndim == 0 else A  # data-apis/array-api-compat#355
```

Code after:
    inv_sq_se = 1 / standard_errors_squared
    weights = inv_sq_se / xp.sum(inv_sq_se, axis=0, keepdims=True)

    # (3) determine variance-weighted estimate of the common mean
    # Consider replacing with vecdot when data-apis/array-api#910 is resolved
    var_w = xp.sum(weights * means, axis=0, keepdims=True)

    # (4) determine one-sample t statistic for each group
    t_stats = _demean(means, var_w, axis=0, xp=xp) / standard_errors

    # calculate parameters to be used in transformation
    v = xp.asarray(lengths, dtype=t_stats.dtype) - 1
    # align along 0th axis, which corresponds with separate samples
    v = xp.reshape(v, (-1,) + (1,)*(t_stats.ndim-1))
    a = v - .5
    b = 48 * a**2
    c = (a * xp.log(1 + (t_stats ** 2)/v))**.5

    # (8) perform a normalizing transformation on t statistic
    z = (c + ((c**3 + 3*c)/b) -
         ((4*c**7 + 33*c**5 + 240*c**3 + 855*c) /
          (b**2*10 + 8*b*c**4 + 1000*b)))

    # (9) calculate statistic
    A = xp.vecdot(z, z, axis=-z.ndim)
    A = A[()] if A.ndim == 0 else A  # data-apis/array-api-compat#355

    # "[the p value is determined from] central chi-square random deviates
    # with k - 1 degrees of freedom". Alexander, Govern (94)
    df = xp.asarray(len(samples) - 1, dtype=A.dtype)
    chi2 = _SimpleChi2(df)
    p = _get_pvalue(A, chi2, alternative='greater', symmetric=False, xp=xp)
    return AlexanderGovernResult(A, p)


def _alexandergovern_input_validation(samples, nan_policy, axis, xp):
    if len(samples) < 2:
        raise TypeError(f"2 or more inputs required, got {len(samples)}")

    for sample in samples:
        if sample.shape[axis] <= 1:
            raise ValueError("Input sample size must be greater than one.")

    samples = [xp.moveaxis(sample, axis, -1) for sample in samples]

    return samples


def _pearsonr_fisher_ci(r, n, confidence_level, alternative):
    """
    Compute the confidence interval for Pearson's R.