
    _Mh%-                         d dl Z d dlZddlmZ ddlmZ ddlmZ	 g dZ
 edg d	          Z ed
ddg          ZddZd ZddZdS )    N   )distributions   )_make_tuple_bunch)siegelslopes)_find_repeatstheilslopesr   TheilslopesResultslope	intercept	low_slope
high_slopeSiegelslopesResultr   r   ffffff?separatec           	         |dvrt          d| d          t          j        | t          d                                          } |)t          j        t          |           t                    }n~t          j        |t          d                                          }t          |          t          |           k    r0t          dt          |            d	t          |           d
          |ddt          j        f         |z
  }| ddt          j        f         | z
  }||dk             ||dk             z  }|j        sd}t          j
        |t          d           |                                 t          j        |          }|dk    rt          j        | ||z  z
            }	n,t          j        |           |t          j        |          z  z
  }	|dk    rd|z
  }t          j                            |dz            }
t#          |          \  }}t#          |           \  }}t          |          }t          |           }d||dz
  z  d|z  dz   z  t%          d |D                       z
  t%          d |D                       z
  z  }	 t          j        |          }t)          t+          t          j        ||
|z  z
  dz                      t          |          dz
            }t/          t+          t          j        ||
|z  z   dz                      dz
  d          }|||g         }n/# t           t0          f$ r t          j        t          j        f}Y nw xY wt5          ||	|d         |d                   S )a  
    Computes the Theil-Sen estimator for a set of points (x, y).

    `theilslopes` implements a method for robust linear regression.  It
    computes the slope as the median of all slopes between paired values.

    Parameters
    ----------
    y : array_like
        Dependent variable.
    x : array_like or None, optional
        Independent variable. If None, use ``arange(len(y))`` instead.
    alpha : float, optional
        Confidence degree between 0 and 1. Default is 95% confidence.
        Note that `alpha` is symmetric around 0.5, i.e. both 0.1 and 0.9 are
        interpreted as "find the 90% confidence interval".
    method : {'joint', 'separate'}, optional
        Method to be used for computing estimate for intercept.
        Following methods are supported,

            * 'joint': Uses np.median(y - slope * x) as intercept.
            * 'separate': Uses np.median(y) - slope * np.median(x)
                          as intercept.

        The default is 'separate'.

        .. versionadded:: 1.8.0

    Returns
    -------
    result : ``TheilslopesResult`` instance
        The return value is an object with the following attributes:

        slope : float
            Theil slope.
        intercept : float
            Intercept of the Theil line.
        low_slope : float
            Lower bound of the confidence interval on `slope`.
        high_slope : float
            Upper bound of the confidence interval on `slope`.

    See Also
    --------
    siegelslopes : a similar technique using repeated medians

    Notes
    -----
    The implementation of `theilslopes` follows [1]_. The intercept is
    not defined in [1]_, and here it is defined as ``median(y) -
    slope*median(x)``, which is given in [3]_. Other definitions of
    the intercept exist in the literature such as  ``median(y - slope*x)``
    in [4]_. The approach to compute the intercept can be determined by the
    parameter ``method``. A confidence interval for the intercept is not
    given as this question is not addressed in [1]_.

    For compatibility with older versions of SciPy, the return value acts
    like a ``namedtuple`` of length 4, with fields ``slope``, ``intercept``,
    ``low_slope``, and ``high_slope``, so one can continue to write::

        slope, intercept, low_slope, high_slope = theilslopes(y, x)

    References
    ----------
    .. [1] P.K. Sen, "Estimates of the regression coefficient based on
           Kendall's tau", J. Am. Stat. Assoc., Vol. 63, pp. 1379-1389, 1968.
    .. [2] H. Theil, "A rank-invariant method of linear and polynomial
           regression analysis I, II and III",  Nederl. Akad. Wetensch., Proc.
           53:, pp. 386-392, pp. 521-525, pp. 1397-1412, 1950.
    .. [3] W.L. Conover, "Practical nonparametric statistics", 2nd ed.,
           John Wiley and Sons, New York, pp. 493.
    .. [4] https://en.wikipedia.org/wiki/Theil%E2%80%93Sen_estimator

    Examples
    --------
    >>> import numpy as np
    >>> from scipy import stats
    >>> import matplotlib.pyplot as plt

    >>> x = np.linspace(-5, 5, num=150)
    >>> y = x + np.random.normal(size=x.size)
    >>> y[11:15] += 10  # add outliers
    >>> y[-5:] -= 7

    Compute the slope, intercept and 90% confidence interval.  For comparison,
    also compute the least-squares fit with `linregress`:

    >>> res = stats.theilslopes(y, x, 0.90, method='separate')
    >>> lsq_res = stats.linregress(x, y)

    Plot the results. The Theil-Sen regression line is shown in red, with the
    dashed red lines illustrating the confidence interval of the slope (note
    that the dashed red lines are not the confidence interval of the regression
    as the confidence interval of the intercept is not included). The green
    line shows the least-squares fit for comparison.

    >>> fig = plt.figure()
    >>> ax = fig.add_subplot(111)
    >>> ax.plot(x, y, 'b.')
    >>> ax.plot(x, res[1] + res[0] * x, 'r-')
    >>> ax.plot(x, res[1] + res[2] * x, 'r--')
    >>> ax.plot(x, res[1] + res[3] * x, 'r--')
    >>> ax.plot(x, lsq_res[1] + lsq_res[0] * x, 'g-')
    >>> plt.show()

    )jointr   z-method must be either 'joint' or 'separate'.'z' is invalid.T)dtypecopyNr   Incompatible lengths ! (<>)r   z"All `x` coordinates are identical.r   )
stacklevelr   g      ?g      ?g       @gqq?r      c              3   8   K   | ]}||d z
  z  d|z  dz   z  V  dS r   r   r   N .0ks     `/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/scipy/stats/_stats_mstats_common.py	<genexpr>ztheilslopes.<locals>.<genexpr>   7      ??1acacAg.??????    c              3   8   K   | ]}||d z
  z  d|z  dz   z  V  dS r   r   r    s     r#   r$   ztheilslopes.<locals>.<genexpr>   r%   r&   r   )
ValueErrornparrayfloatravelarangelennewaxissizewarningswarnRuntimeWarningsortmedianr   normppfr   sumsqrtminintroundmax
IndexErrornanr
   )yxalphamethoddeltaxdeltayslopesmsgmedslopemedinterz_nxrepsnyrepsntnysigsqsigmaRuRldeltas                        r#   r	   r	      so   V *** 3#3 3 3 4 4 	4 	%d+++1133AyIc!ffE***HQe$///5577q66SVVKAKK#a&&KKKLLL qqq"*}!Fqqq"*}!FFQJ&!"44F; 92c>a8888
KKMMMy  H9QA-..9Q<<(RYq\\"99s{{U
urz**Aa  IAva  IAv	VB	QBR2a4[AbDF+???????@???????@ AE!RXrAeG|R/00113v;;q=AARXrAeG|R/0011A5q99Bx 
# ! ! ! ! 8x',QxE!HF F F Fs   B!L. .)MMc                 H   t          |           dk    r>t          j        dt          j                  t          j        dt          j                  fS t          j        | t          j                                                  } |                                  t          j        dg| dd          | d d         k    f          }| |         }t          j        t          j	        |          | j
        gfz             }t          j        |          }|dk    }||         ||         fS )Nr   Tr   )r.   r)   r*   float64intpasarrayr,   r4   concatenatenonzeror0   diff)arrchangeunique
change_idxfreqatleast2s         r#   r   r      s    
3xx1}}x2:&&BG(<(<<< *S"*
%
%
+
+
-
-CHHJJJ ^dVSWCRC%89::F[F
6 2 2sxj] BCCJ7:DaxH(T(^++r&   hierarchicalc                    |dvrt          d          t          j        |                                           } |)t          j        t          |           t                    }n}t          j        |t                                                    }t          |          t          |           k    r0t          dt          |            dt          |           d          t          j        || t          j                  }| 	                    |          |	                    |          }} t          | ||          \  }}t          ||          S )	az  
    Computes the Siegel estimator for a set of points (x, y).

    `siegelslopes` implements a method for robust linear regression
    using repeated medians (see [1]_) to fit a line to the points (x, y).
    The method is robust to outliers with an asymptotic breakdown point
    of 50%.

    Parameters
    ----------
    y : array_like
        Dependent variable.
    x : array_like or None, optional
        Independent variable. If None, use ``arange(len(y))`` instead.
    method : {'hierarchical', 'separate'}
        If 'hierarchical', estimate the intercept using the estimated
        slope ``slope`` (default option).
        If 'separate', estimate the intercept independent of the estimated
        slope. See Notes for details.

    Returns
    -------
    result : ``SiegelslopesResult`` instance
        The return value is an object with the following attributes:

        slope : float
            Estimate of the slope of the regression line.
        intercept : float
            Estimate of the intercept of the regression line.

    See Also
    --------
    theilslopes : a similar technique without repeated medians

    Notes
    -----
    With ``n = len(y)``, compute ``m_j`` as the median of
    the slopes from the point ``(x[j], y[j])`` to all other `n-1` points.
    ``slope`` is then the median of all slopes ``m_j``.
    Two ways are given to estimate the intercept in [1]_ which can be chosen
    via the parameter ``method``.
    The hierarchical approach uses the estimated slope ``slope``
    and computes ``intercept`` as the median of ``y - slope*x``.
    The other approach estimates the intercept separately as follows: for
    each point ``(x[j], y[j])``, compute the intercepts of all the `n-1`
    lines through the remaining points and take the median ``i_j``.
    ``intercept`` is the median of the ``i_j``.

    The implementation computes `n` times the median of a vector of size `n`
    which can be slow for large vectors. There are more efficient algorithms
    (see [2]_) which are not implemented here.

    For compatibility with older versions of SciPy, the return value acts
    like a ``namedtuple`` of length 2, with fields ``slope`` and
    ``intercept``, so one can continue to write::

        slope, intercept = siegelslopes(y, x)

    References
    ----------
    .. [1] A. Siegel, "Robust Regression Using Repeated Medians",
           Biometrika, Vol. 69, pp. 242-244, 1982.

    .. [2] A. Stein and M. Werman, "Finding the repeated median regression
           line", Proceedings of the Third Annual ACM-SIAM Symposium on
           Discrete Algorithms, pp. 409-413, 1992.

    Examples
    --------
    >>> import numpy as np
    >>> from scipy import stats
    >>> import matplotlib.pyplot as plt

    >>> x = np.linspace(-5, 5, num=150)
    >>> y = x + np.random.normal(size=x.size)
    >>> y[11:15] += 10  # add outliers
    >>> y[-5:] -= 7

    Compute the slope and intercept.  For comparison, also compute the
    least-squares fit with `linregress`:

    >>> res = stats.siegelslopes(y, x)
    >>> lsq_res = stats.linregress(x, y)

    Plot the results. The Siegel regression line is shown in red. The green
    line shows the least-squares fit for comparison.

    >>> fig = plt.figure()
    >>> ax = fig.add_subplot(111)
    >>> ax.plot(x, y, 'b.')
    >>> ax.plot(x, res[1] + res[0] * x, 'r-')
    >>> ax.plot(x, lsq_res[1] + lsq_res[0] * x, 'g-')
    >>> plt.show()

    )rc   r   z/method can only be 'hierarchical' or 'separate'Nr   r   r   r   )r   r   )r(   r)   rY   r,   r-   r.   r+   result_typefloat32astypesiegelslopes_pythranr   )r@   rA   rC   r   rH   rI   s         r#   r   r      s   @ 111JKKK

1AyIc!ffE***Jq&&&,,..q66SVVKAKK#a&&KKKLLLN1a,,E88E??AHHUOOqA-aF;;HhHAAAAr&   )Nr   r   )Nrc   )r1   numpyr)    r   _lib._bunchr   _stats_pythranr   rh   __all__r
   r   r	   r   r   r&   r#   <module>rn      s               + + + + + + @ @ @ @ @ @
:
:
: &%&9'B 'B 'BC C  '&';(/'=? ? \F \F \F \F~, , ,&lB lB lB lB lB lBr&   