
    Џkhj4                     ~    d dl Zd dlmZ d dlmZmZmZ d dlm	c m
Z d dlmZmZ d dlmZ d Zdd ddd	d
Zd Zd Zy)    N)betainc)xp_ravelarray_namespace
xp_promote)_broadcast_arrays_contains_nan)_length_nonmaskedc                 Z   t        | |      }|j                  |j                  |       j                  d      st	        d      |j                  |j                  |      j                  d      st	        d      t        | |d|      \  } }| j                  }|d u }t        | j                  |j                        }	|rt        |       } t        |      }d}nHt        j                  |      st        |      |k7  rd}
t	        |
      ||	k\  s||	 k  rd	}
t	        |
      t        |      }h d
}||vrd| }
t	        |
      t        | |d|      }|dvrd}
t	        |
      | j                  |   dk(  rGt        | j                        }d||<   |j                  ||j                  |j                   |            } |j#                  | |      }t%        ||f|      \  }}|du r|j                  |   dk7  rd}
t	        |
      ||j                  |   dk7  n|}|j'                  ||d      }|j'                  ||d      }t)        |d|d      }|j                  ||      }|rW|j+                  |      }|dk(  r|j-                  |d      }n|j/                  | |j0                        }|j3                  |dd      }|j/                  ||      }|j-                  |dk(  d      }t5        j6                  ||      j9                  |j                  d         }|j-                  |      rC|j                  |d      }t5        j6                  ||      j9                  |j                         }nN|j-                  |      r=|dk(  r8|j                  |d      }t5        j6                  ||      j9                  d      }|dkD  |dk  z  |j+                  |      z  }|j-                  |      r8|j                  |d      }t5        j6                  ||      j9                  d      }|||||||||	||fS )N)integralreal floatingz`x` must have real dtype.r   z"`p` must have real floating dtype.T)force_floatingxpr   z"`axis` must be an integer or None.z7`axis` is not compatible with the shapes of the inputs.>
   hazenlinearweibullinverted_cdfmedian_unbiasednormal_unbiasedclosest_observationaveraged_inverted_cdfinterpolated_inverted_cdfharrell-davisz`method` must be one of )xp_omit_okayr   >   FNTz/If specified, `keepdims` must be True or False.   dtypeaxisFzD`keepdims` may be False only if the length of `p` along `axis` is 1.)r   keepdims	propagate)r   r    )copyr         ?)r   isdtypeasarrayr   
ValueErrorr   maxndimr   npiterableintr   shapelistfullnansortr   moveaxisr	   isnananyastypeuint64sumxpxatset)xpmethodr   
nan_policyr    r   r   	axis_noner(   messagemethodscontains_nansr,   ynnansnan_outnon_nann_intp_masks                        Q/var/www/teggl/fontify/venv/lib/python3.12/site-packages/scipy/stats/_quantile.py_quantile_ivrJ   	   s   	A	B::bjjm))+HI455::bjjm))?;=>>a4B7DAqGGEIqvvqvvDQKQK	T	c$i4/6!!
$,DD5LK!!t9D G W,WI6!!!!ZdrJM**C!!
 	wwt}QWWdGGE2::bffE::;
AaV$/DAqE 2X!!'/'7"XH
AtR A
AtR A!RB6A


1E
"Axx{ $ffTf+Giiryy1GFF7dF;E		%'AffQ!V"f-Gq'"&&qwwr{3A66'?

14
(Aq'"&&rvv.AVVD\f7

14
(Aq$##A&A!eA!,F	vvf~JJqtJ$FF1f!!#&az8Q	4QSSS    r   r!   )r<   r   r=   r    c                   t        | |||||      }|\  }}}}}}}}	}
}}|dv rt        |||||      }n|dv rt        ||||      }t        j                  |      j                  |j                        }|	r+|r)d|
dz
  z  |j                  z   }|j                  ||      }d}|j                  |d|      }|s|j                  ||      }|j                  dk(  r|d   S |S )	a  
    Compute the p-th quantile of the data along the specified axis.

    Parameters
    ----------
    x : array_like of real numbers
        Data array.
    p : array_like of float
        Probability or sequence of probabilities of the quantiles to compute.
        Values must be between 0 and 1 (inclusive).
        Must have length 1 along `axis` unless ``keepdims=True``.
    method : str, default: 'linear'
        The method to use for estimating the quantile.
        The available options, numbered as they appear in [1]_, are:

        1. 'inverted_cdf'
        2. 'averaged_inverted_cdf'
        3. 'closest_observation'
        4. 'interpolated_inverted_cdf'
        5. 'hazen'
        6. 'weibull'
        7. 'linear'  (default)
        8. 'median_unbiased'
        9. 'normal_unbiased'

        'harrell-davis' is also available to compute the quantile estimate
        according to [2]_.
        See Notes for details.
    axis : int or None, default: 0
        Axis along which the quantiles are computed.
        ``None`` ravels both `x` and `p` before performing the calculation,
        without checking whether the original shapes were compatible.
    nan_policy : str, default: 'propagate'
        Defines how to handle NaNs in the input data `x`.

        - ``propagate``: if a NaN is present in the axis slice (e.g. row) along
          which the  statistic is computed, the corresponding slice of the output
          will contain NaN(s).
        - ``omit``: NaNs will be omitted when performing the calculation.
          If insufficient data remains in the axis slice along which the
          statistic is computed, the corresponding slice of the output will
          contain NaN(s).
        - ``raise``: if a NaN is present, a ``ValueError`` will be raised.

        If NaNs are present in `p`, a ``ValueError`` will be raised.
    keepdims : bool, optional
        Consider the case in which `x` is 1-D and `p` is a scalar: the quantile
        is a reducing statistic, and the default behavior is to return a scalar.
        If `keepdims` is set to True, the axis will not be reduced away, and the
        result will be a 1-D array with one element.

        The general case is more subtle, since multiple quantiles may be
        requested for each axis-slice of `x`. For instance, if both `x` and `p`
        are 1-D and ``p.size > 1``, no axis can be reduced away; there must be an
        axis to contain the number of quantiles given by ``p.size``. Therefore:

        - By default, the axis will be reduced away if possible (i.e. if there is
          exactly one element of `q` per axis-slice of `x`).
        - If `keepdims` is set to True, the axis will not be reduced away.
        - If `keepdims` is set to False, the axis will be reduced away
          if possible, and an error will be raised otherwise.

    Returns
    -------
    quantile : scalar or ndarray
        The resulting quantile(s). The dtype is the result dtype of `x` and `p`.

    Notes
    -----
    Given a sample `x` from an underlying distribution, `quantile` provides a
    nonparametric estimate of the inverse cumulative distribution function.

    By default, this is done by interpolating between adjacent elements in
    ``y``, a sorted copy of `x`::

        (1-g)*y[j] + g*y[j+1]

    where the index ``j`` and coefficient ``g`` are the integral and
    fractional components of ``p * (n-1)``, and ``n`` is the number of
    elements in the sample.

    This is a special case of Equation 1 of H&F [1]_. More generally,

    - ``j = (p*n + m - 1) // 1``, and
    - ``g = (p*n + m - 1) % 1``,

    where ``m`` may be defined according to several different conventions.
    The preferred convention may be selected using the ``method`` parameter:

    =============================== =============== ===============
    ``method``                      number in H&F   ``m``
    =============================== =============== ===============
    ``interpolated_inverted_cdf``   4               ``0``
    ``hazen``                       5               ``1/2``
    ``weibull``                     6               ``p``
    ``linear`` (default)            7               ``1 - p``
    ``median_unbiased``             8               ``p/3 + 1/3``
    ``normal_unbiased``             9               ``p/4 + 3/8``
    =============================== =============== ===============

    Note that indices ``j`` and ``j + 1`` are clipped to the range ``0`` to
    ``n - 1`` when the results of the formula would be outside the allowed
    range of non-negative indices. When ``j`` is clipped to zero, ``g`` is
    set to zero as well. The ``-1`` in the formulas for ``j`` and ``g``
    accounts for Python's 0-based indexing.

    The table above includes only the estimators from [1]_ that are continuous
    functions of probability `p` (estimators 4-9). SciPy also provides the
    three discontinuous estimators from [1]_ (estimators 1-3), where ``j`` is
    defined as above, ``m`` is defined as follows, and ``g`` is ``0`` when
    ``index = p*n + m - 1`` is less than ``0`` and otherwise is defined below.

    1. ``inverted_cdf``: ``m = 0`` and ``g = int(index - j > 0)``
    2. ``averaged_inverted_cdf``: ``m = 0`` and
       ``g = (1 + int(index - j > 0)) / 2``
    3. ``closest_observation``: ``m = -1/2`` and
       ``g = 1 - int((index == j) & (j%2 == 1))``

    A different strategy for computing quantiles from [2]_, ``method='harrell-davis'``,
    uses a weighted combination of all elements. The weights are computed as:

    .. math::

        w_{n, i} = I_{i/n}(a, b) - I_{(i - 1)/n}(a, b)

    where :math:`n` is the number of elements in the sample,
    :math:`i` are the indices :math:`1, 2, ..., n-1, n` of the sorted elements,
    :math:`a = p (n + 1)`, :math:`b = (1 - p)(n + 1)`,
    :math:`p` is the probability of the quantile, and
    :math:`I` is the regularized, lower incomplete beta function
    (`scipy.special.betainc`).

    Examples
    --------
    >>> import numpy as np
    >>> from scipy import stats
    >>> x = np.asarray([[10, 8, 7, 5, 4],
    ...                 [0, 1, 2, 3, 5]])

    Take the median along the last axis.

    >>> stats.quantile(x, 0.5, axis=-1)
    array([7.,  2.])

    Take a different quantile along each axis.

    >>> stats.quantile(x, [[0.25], [0.75]], axis=-1, keepdims=True)
    array([[5.],
           [3.]])

    Take multiple quantiles along each axis.

    >>> stats.quantile(x, [0.25, 0.75], axis=-1)
    array([[5., 8.],
           [1., 3.]])

    References
    ----------
    .. [1] R. J. Hyndman and Y. Fan,
       "Sample quantiles in statistical packages,"
       The American Statistician, 50(4), pp. 361-365, 1996
    .. [2] Harrell, Frank E., and C. E. Davis.
       "A new distribution-free quantile estimator."
       Biometrika 69.3 (1982): 635-640.

    >	   r   r   r   r   r   r   r   r   r   >   r   )r   r   r   r   r    )rJ   _quantile_hf_quantile_hdr7   r8   r9   r/   r,   reshaper1   squeezer(   )r:   r;   r<   r   r=   r    temprB   rC   r>   r(   rH   r   resr,   s                  rI   quantilerT   d   s   R 1fdJADOSLAq&$
HaD&" C C 1aFB/	$	$1aB'
&&f

!
!"&&
)C XdQh#))+jje$
++c2t
$Cjj4j(hh!m3r7,,rK   c                 0   t        ddddd|d|z
  |dz  dz   |dz  dz   		      }||   }||z  |z   dz
  }|dz  }|dz  }	|d
k(  r |j                  |	dkD  |j                        }	n[|dk(  r&d|j                  |	dkD  |j                        z   dz  }	n0|dk(  r+d|j                  |	dk(  |dz  dk(  z  |j                        z
  }	|dv r9|j                  |	      }	t	        j
                  |	|dk        j                  d      }	d|	|dk  <   |j                  |d|dz
        }|j                  |dz   d|dz
        }
d|	z
  |j                  | |j                  ||j                        d      z  |	|j                  | |j                  |
|j                        d      z  z   S )Nr   g      r#   r      gUUUUUU?   g      ?)	r   r   r   r   r   r   r   r   r   r   r      r   >   r   r   r   g        r   r   )
dictr4   r   r%   r7   r8   r9   cliptake_along_axisint64)rB   r;   rC   r<   r   msmjgjgjp1s              rI   rN   rN   '  s   	1A4()aAcCi1s
DB 	6
A	
1q1B
aA
QAIIq1urxx(	*	*AEBHH--2	(	(AFq1uz2BHH==QQJJqMFF1b1f!!!$Aa!eH
2q1uA
''!a%QU
#CUb((BIIa,B(LL"$$Q		#rxx(@r$JJK LrK   c                    |j                  |dd      d|j                  f   }||dz   z  }d|z
  |dz   z  }|j                  | j                  d   dz   | j                        }t        ||||z        }|ddd f   |dd df   z
  }t        j                  ||j                  |            j                  d      }|j                  || d      }|j                  |dd      S )Nr   r   .r   r   r   )r1   newaxisaranger,   r   r   r7   r8   r2   r9   vecdot)	rB   r;   rC   r   abiwrS   s	            rI   rO   rO   A  s     	Ar1c2::o.A	QUA	
Q1q5A
		!''"+/	1A1a!eA	#qr'
QsCRCx[ Aq"((1+""1%A
))Aqr)
"C;;sAr""rK   )numpyr)   scipy.specialr   scipy._lib._array_apir   r   r   scipy._lib.array_api_extra_libarray_api_extrar7   scipy.stats._axis_nan_policyr   r   scipy.stats._stats_pyr	   rJ   rT   rN   rO   rM   rK   rI   <module>rs      sC     ! G G ( ( I 3XTv &A+PT @-FL4#rK   