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

Code before:
        >>> lb, ub = res.confidence_interval(0.9)
        >>> lb, ub
        (1.9542373206359396, 2.293318740264183)
        """

        alternative = self._alternative
        p = self._p
        x = np.sort(self._x, axis=-1)
        n = x.shape[-1]
        bd = stats.binom(n, p)
        shape = p.shape

        if confidence_level <= 0 or confidence_level >= 1:
            message = "`confidence_level` must be a number between 0 and 1."
            raise ValueError(message)

        if n == 0:
            zeros = np.zeros(p.shape, dtype=x.dtype)
            low, high = zeros, zeros
        elif alternative == 'less':
            p = 1 - confidence_level
            low = np.full(shape, -np.inf)
            high_index = bd.isf(p).astype(int)
            valid_index = high_index < n
            high_index[~valid_index] = 0
            high = np.where(valid_index, np.take_along_axis(x, high_index), np.nan)
        elif alternative == 'greater':
            p = 1 - confidence_level
            low_index = (bd.ppf(p)).astype(int) - 1
            valid_index = low_index >= 0
            low_index[~valid_index] = 0
            low = np.where(valid_index, np.take_along_axis(x, low_index), np.nan)
            high = np.full(shape, np.inf)
        elif alternative == 'two-sided':
            p = (1 - confidence_level) / 2
            low_index = (bd.ppf(p)).astype(int) - 1
            valid_index = low_index >= 0
            low_index[~valid_index] = 0
            low = np.where(valid_index, np.take_along_axis(x, low_index), np.nan)
            high_index = bd.isf(p).astype(int)
            valid_index = high_index < n
            high_index[~valid_index] = 0
            high = np.where(valid_index, np.take_along_axis(x, high_index), np.nan)

        args = self._axis, self._axis_none, self._keepdims, self._ndim, self._nan_out
        low = _quantile_test_postprocess(low, *args)
        high = _quantile_test_postprocess(high, *args)
        return ConfidenceInterval(low, high)


def quantile_test_iv(x, q, p, alternative, axis, keepdims):

Review comment (suggestion):
`take_along_axis` doesn't like invalid indices. If the index is out of bounds, the result is supposed to be NaN, so we use an arbitrary valid index and NaN-out the result. 

Code after:
        Examples
        --------
        >>> import numpy as np
        >>> import scipy.stats as stats
        >>> p = 0.75  # quantile of interest
        >>> q = 0  # hypothesized value of the quantile
        >>> x = np.exp(np.arange(0, 1.01, 0.01))
        >>> res = stats.quantile_test(x, q=q, p=p, alternative='less')
        >>> lb, ub = res.confidence_interval()
        >>> lb, ub
        (-inf, 2.293318740264183)
        >>> res = stats.quantile_test(x, q=q, p=p, alternative='two-sided')
        >>> lb, ub = res.confidence_interval(0.9)
        >>> lb, ub
        (1.9542373206359396, 2.293318740264183)
        """

        alternative = self._alternative
        p = self._p
        x = np.sort(self._x, axis=-1)
        n = x.shape[-1]
        bd = stats.binom(n, p)
        shape = p.shape

        if confidence_level <= 0 or confidence_level >= 1:
            message = "`confidence_level` must be a number between 0 and 1."
            raise ValueError(message)

        if n == 0:
            zeros = np.zeros(p.shape, dtype=x.dtype)
            low, high = zeros, zeros
        elif alternative == 'less':
            p = 1 - confidence_level
            low = np.full(shape, -np.inf)
            high_index = bd.isf(p).astype(int)
            valid_index = high_index < n
            high_index[~valid_index] = 0
            x_high = np.take_along_axis(x, high_index, axis=-1)
            high = np.where(valid_index, x_high, np.nan)
        elif alternative == 'greater':
            p = 1 - confidence_level
            low_index = (bd.ppf(p)).astype(int) - 1
            valid_index = low_index >= 0
            low_index[~valid_index] = 0
            x_low = np.take_along_axis(x, low_index, axis=-1)
            low = np.where(valid_index, x_low, np.nan)
            high = np.full(shape, np.inf)
        elif alternative == 'two-sided':
            p = (1 - confidence_level) / 2
            low_index = (bd.ppf(p)).astype(int) - 1
            valid_index = low_index >= 0