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

Code before:
    statistic: float
    statistic_type: int
    pvalue: float
    _alternative: list[str] = field(repr=False)
    _x : np.ndarray = field(repr=False)
    _p : float = field(repr=False)
    _statistic : float = field(repr=False)
    _statistic_type : int = field(repr=False)
    _pvalue : float = field(repr=False)
    _axis : int = field(repr=False)
    _axis_none : bool = field(repr=False)
    _keepdims : bool = field(repr=False)
    _ndim : int = field(repr=False)
    _nan_out : bool = field(repr=False)


    def confidence_interval(self, confidence_level=0.95):
        """
        Compute the confidence interval of the quantile.

        Parameters
        ----------
        confidence_level : float, default: 0.95
            Confidence level for the computed confidence interval
            of the quantile. Default is 0.95.

        Returns
        -------
        ci : ``ConfidenceInterval`` object
            The object has attributes ``low`` and ``high`` that hold the
            lower and upper bounds of the confidence interval.

        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

Review comment (suggestion):
The `confidence_interval` method draws the endpoints of the confidence intervals from the original data, so we store `_statistic`, `_statistic_type`, and `_pvalue` without any dimensions reduced away to keep the dimensions lined up with the data stored in `_x`. It might be possible to just reshape the `statistic`, `statistic_type`, and `pvalue` attributes as needed so we don't need to store copies of the data, but that is an enhancement that can come later. This PR is complex enough; let's focus on correctness. The rest of the attributes get used, too:
- `_keepdims` is needed to remember whether we need to reduce dimensions away
- `_axis_none` remembers whether the user passed `axis=None`, which means that the data was raveled
- `_ndim` is to remember the original number of dimensions in case the user passed `axis=None` and `keepdims=True`
- `_axis` to make sure we move the axes as they should be if `keepdims=True`
- `_nan_out` remembers where to inject NaNs in the result

Code after:
        ``1`` or ``2`` depending on which of ``T1`` or ``T2`` was used to
        calculate the p-value respectively. ``T1`` corresponds to the
        ``"greater"`` alternative hypothesis and ``T2`` to the ``"less"``.  For
        the ``"two-sided"`` case, the statistic type that leads to smallest
        p-value is used.  For significant tests, ``statistic_type = 1`` means
        there is evidence that the population quantile is significantly greater
        than the hypothesized value and ``statistic_type = 2`` means there is
        evidence that it is significantly less than the hypothesized value.

    pvalue : float
        The p-value of the hypothesis test.
    """
    statistic: float
    statistic_type: int
    pvalue: float
    _alternative: list[str] = field(repr=False)
    _x : np.ndarray = field(repr=False)
    _p : float = field(repr=False)
    _statistic : float = field(repr=False)
    _statistic_type : int = field(repr=False)
    _pvalue : float = field(repr=False)
    _axis : int = field(repr=False)
    _axis_none : bool = field(repr=False)
    _keepdims : bool = field(repr=False)
    _ndim : int = field(repr=False)
    _nan_out : bool = field(repr=False)


    def confidence_interval(self, confidence_level=0.95):
        """
        Compute the confidence interval of the quantile.

        Parameters
        ----------
        confidence_level : float, default: 0.95
            Confidence level for the computed confidence interval
            of the quantile. Default is 0.95.

        Returns
        -------
        ci : ``ConfidenceInterval`` object
            The object has attributes ``low`` and ``high`` that hold the
            lower and upper bounds of the confidence interval.

        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))