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

Code before:

    plow = np.where(k == 0, 0.0, plow)
    phigh = np.where(k == n, 1.0, phigh)
    return plow, phigh


def _binom_wilson_conf_int(k, n, confidence_level, alternative, correction):
    # This function assumes that the arguments have already been validated.
    # In particular, `alternative` must be one of 'two-sided', 'less' or
    # 'greater'.
    p = k / n
    if alternative == 'two-sided':
        z = ndtri(0.5 + 0.5*confidence_level)
    else:
        z = ndtri(confidence_level)

    # For reference, the formulas implemented here are from
    # Newcombe (1998) (ref. [3] in the proportion_ci docstring).
    denom = 2*(n + z**2)
    center = (2*n*p + z**2)/denom
    q = 1 - p

    if correction:
        with np.errstate(divide='ignore', invalid='ignore'):
            dlo = (1 + z*np.sqrt(z**2 - 2 - 1/n + 4*p*(n * q + 1)))/denom
            dhi = (1 + z*np.sqrt(z**2 + 2 - 1/n + 4*p*(n * q - 1)))/denom
    else:
        delta = z / denom * np.sqrt(4 * n * p * q + z ** 2)
        dlo, dhi = delta, delta

    lo = np.where((k == 0) | (alternative == 'less'), 0.0, center - dlo)
    hi = np.where((k == n) | (alternative == 'greater'), 1.0, center + dhi)
    return lo, hi


@xp_capabilities(np_only=True)
def binomtest(k, n, p=0.5, alternative='two-sided'):
    """
    Perform a test that the probability of success is p.

    The binomial test [1]_ is a test of the null hypothesis that the
    probability of success in a Bernoulli experiment is `p`.

    Details of the test can be found in many texts on statistics, such
    as section 24.5 of [2]_.

    The documentation is written as though the function accepts and returns Python
    scalars, but the function is vectorized to work elementwise with NumPy arrays.

    Parameters
    ----------

Review comment (suggestion):
```suggestion
            dlo = (1 + z*np.sqrt(z**2 - 2 - 1/n + 4*p*(n*q + 1)))/denom
            dhi = (1 + z*np.sqrt(z**2 + 2 - 1/n + 4*p*(n*q - 1)))/denom
```

Code after:
    plow = (np.zeros(k.shape, dtype=np.float64) if alternative == 'less' else
            find_root(lambda p, k, n: binom._sf(k-1, n, p) - alpha, init, args=args).x)
    phigh = (np.ones(k.shape, dtype=np.float64) if alternative == 'greater' else
             find_root(lambda p, k, n: binom._cdf(k, n, p) - alpha, init, args=args).x)

    plow = np.where(k == 0, 0.0, plow)
    phigh = np.where(k == n, 1.0, phigh)
    return plow, phigh


def _binom_wilson_conf_int(k, n, confidence_level, alternative, correction):
    # This function assumes that the arguments have already been validated.
    # In particular, `alternative` must be one of 'two-sided', 'less' or
    # 'greater'.
    p = k / n
    if alternative == 'two-sided':
        z = ndtri(0.5 + 0.5*confidence_level)
    else:
        z = ndtri(confidence_level)

    # For reference, the formulas implemented here are from
    # Newcombe (1998) (ref. [3] in the proportion_ci docstring).
    denom = 2*(n + z**2)
    center = (2*n*p + z**2)/denom
    q = 1 - p

    if correction:
        with np.errstate(divide='ignore', invalid='ignore'):
            dlo = (1 + z*np.sqrt(z**2 - 2 - 1/n + 4*p*(n*q + 1)))/denom
            dhi = (1 + z*np.sqrt(z**2 + 2 - 1/n + 4*p*(n*q - 1)))/denom
    else:
        delta = z / denom * np.sqrt(4*n*p*q + z**2)
        dlo, dhi = delta, delta

    lo = np.where((k == 0) | (alternative == 'less'), 0.0, center - dlo)
    hi = np.where((k == n) | (alternative == 'greater'), 1.0, center + dhi)
    return lo, hi


@xp_capabilities(np_only=True)
def binomtest(k, n, p=0.5, alternative='two-sided'):
    """
    Perform a test that the probability of success is p.

    The binomial test [1]_ is a test of the null hypothesis that the
    probability of success in a Bernoulli experiment is `p`.

    Details of the test can be found in many texts on statistics, such
    as section 24.5 of [2]_.

    The documentation is written as though the function accepts and returns Python