
    M/Ph0                        d Z ddlZddlmZ ddlmZ ddlm	Z	m
Z
 ddlmZ ddlmZ  ej        d	          dej        fd
Z ej        d	           ej        d          z
  dfdZd ej        d           ej        d          z  z  dfdZd ej        d           ej        d          z  z  fdZ G d d          Z e            Z G d d          Z e            ZdS )a  
Support and standalone functions for Robust Linear Models

References
----------
PJ Huber.  'Robust Statistics' John Wiley and Sons, Inc., New York, 1981.

R Venables, B Ripley. 'Modern Applied Statistics in S'
    Springer, New York, 2002.

C Croux, PJ Rousseeuw, 'Time-efficient algorithms for two highly robust
estimators of scale' Computational statistics. Physica, Heidelberg, 1992.
    N)norm)tools)
array_like
float_like   )norms)_qn      ?c                     t          | dd          } t          |d          }| j        sd}nVt          |          r7|t	          j        || |          }n. ||                                           }nt          |d          }t	          j        | |z
            |z  }|j        sV||j        dk    rt          j	        S t          |j                  }|                    |           t	          j        |          S t	          j        ||          S )	a  
    The Median Absolute Deviation along given axis of an array

    Parameters
    ----------
    a : array_like
        Input array.
    c : float, optional
        The normalization constant.  Defined as scipy.stats.norm.ppf(3/4.),
        which is approximately 0.6745.
    axis : int, optional
        The default is 0. Can also be None.
    center : callable or float
        If a callable is provided, such as the default `np.median` then it
        is expected to be called center(a). The axis argument will be applied
        via np.apply_over_axes. Otherwise, provide a float.

    Returns
    -------
    mad : float
        `mad` = median(abs(`a` - center))/`c`
    aNndimcg        centerr   axis)r   r   sizecallablenpapply_over_axesravelabsr   nanlistshapepopemptymedian)r   r   r   r   
center_valerrr   s          X/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/statsmodels/robust/scale.pymadr"      s   . 	1c%%%A1cA6 2

	&		 2+FAt<<JJ		**JJ11
6!j.!!Q
&C8 #<38q==6MOOEIIdOOO8E??"9St$$$$          ?c                 0   t          | dd          } t          |d          }| j        dk    rt          d          | j        dk    rt
          j        S t          j        | ddg|	          }t          j        t          j	        |d	          |z            S )
a#  
    The normalized interquartile range along given axis of an array

    Parameters
    ----------
    a : array_like
        Input array.
    c : float, optional
        The normalization constant, used to get consistent estimates of the
        standard deviation at the normal distribution.  Defined as
        scipy.stats.norm.ppf(3/4.) - scipy.stats.norm.ppf(1/4.), which is
        approximately 1.349.
    axis : int, optional
        The default is 0. Can also be None.

    Returns
    -------
    The normalized interquartile range
    r   Nr   r   r   $a should have at least one dimensionr$   r
   r   )
r   r   r   
ValueErrorr   r   r   quantilesqueezediff)r   r   r   	quantiless       r!   iqrr,   E   s    ( 	1c%%%A1cAv{{?@@@	
1vKD$<d;;;	z"')!444q8999r#      g      ?c                 4   t          | ddt          j        dd          } t          |d          }| j        dk    rt          d          | j        dk    rt          j        S t          j        t          || |	          }|j        dk    rt          |          S |S )
a  
    Computes the Qn robust estimator of scale

    The Qn scale estimator is a more efficient alternative to the MAD.
    The Qn scale estimator of an array a of length n is defined as
    c * {abs(a[i] - a[j]): i<j}_(k), for k equal to [n/2] + 1 choose 2. Thus,
    the Qn estimator is the k-th order statistic of the absolute differences
    of the array. The optional constant is used to normalize the estimate
    as explained below. The implementation follows the algorithm described
    in Croux and Rousseeuw (1992).

    Parameters
    ----------
    a : array_like
        Input array.
    c : float, optional
        The normalization constant. The default value is used to get consistent
        estimates of the standard deviation at the normal distribution.
    axis : int, optional
        The default is 0.

    Returns
    -------
    {float, ndarray}
        The Qn robust estimator of scale
    r   NTC)r   dtype
contiguousorderr   r   r&   )r   arrr   )r   r   float64r   r   r'   r   r   apply_along_axisr	   float)r   r   r   outs       r!   qn_scaler8   e   s    6 		3TC	 	 	A 	1cAv{{?@@@	
1v!#Da1===8q==::
r#   c                    t          j        |           } | j        d         }| j        dk    rt           j        S t          |dz  dz             }t          ||dz
  z  dz            }t          j        |d          }t          j        | |d                  | |d                  z
            }t          j        ||dz
            |dz
           }||z  }|S )a  
    A naive implementation of the Qn robust estimator of scale, used solely
    to test the faster, more involved one

    Parameters
    ----------
    a : array_like
        Input array.
    c : float, optional
        The normalization constant, used to get consistent estimates of the
        standard deviation at the normal distribution.  Defined as
        1/(np.sqrt(2) * scipy.stats.norm.ppf(5/8)), which is 2.219144.

    Returns
    -------
    The Qn robust estimator of scale
    r   r-   r   )k)kth)	r   r)   r   r   r   inttriu_indicesr   	partition)r   r   nhr:   idxdiffsoutputs           r!   	_qn_naiverD      s    $ 	
1A	
Av{{vQ
OOQUa  oa1%%%qQy1SV9,--eQ///A6Vr#   c                   (    e Zd ZdZd
dZddZd	 ZdS )Hubera.  
    Huber's proposal 2 for estimating location and scale jointly.

    Parameters
    ----------
    c : float, optional
        Threshold used in threshold for chi=psi**2.  Default value is 1.5.
    tol : float, optional
        Tolerance for convergence.  Default value is 1e-08.
    maxiter : int, optional0
        Maximum number of iterations.  Default value is 30.
    norm : statsmodels.robust.norms.RobustNorm, optional
        A robust norm used in M estimator of location. If None,
        the location estimator defaults to a one-step
        fixed point version of the M-estimator using Huber's T.

    call
        Return joint estimates of Huber's scale and location.

    Examples
    --------
    >>> import numpy as np
    >>> import statsmodels.api as sm
    >>> chem_data = np.array([2.20, 2.20, 2.4, 2.4, 2.5, 2.7, 2.8, 2.9, 3.03,
    ...        3.03, 3.10, 3.37, 3.4, 3.4, 3.4, 3.5, 3.6, 3.7, 3.7, 3.7, 3.7,
    ...        3.77, 5.28, 28.95])
    >>> sm.robust.scale.huber(chem_data)
    (array(3.2054980819923693), array(0.67365260010478967))
          ?:0yE>   Nc                     || _         || _        || _        || _        dt	          j        |          z  dz
  }||dz  d|z
  z  z   d|z  t	          j        |          z  z
  | _        d S Nr-   r   )r   maxitertolr   Gaussiancdfpdfgamma)selfr   rM   rL   r   tmps         r!   __init__zHuber.__init__   sh    	(,q//!A%16QW--AQ0GG


r#   r   c                 n   t          j        |          }|)|j        d         dz
  }t          j        ||          }d}n|j        d         }|}d}|t	          ||          }n|}t          j        |||j                  }t          j        |||j                  }|                     ||||||          S )a2  
        Compute Huber's proposal 2 estimate of scale, using an optional
        initial value of scale and an optional estimate of mu. If mu
        is supplied, it is not reestimated.

        Parameters
        ----------
        a : ndarray
            1d array
        mu : float or None, optional
            If the location mu is supplied then it is not reestimated.
            Default is None, which means that it is estimated.
        initscale : float or None, optional
            A first guess on scale.  If initscale is None then the standardized
            median absolute deviation of a is used.

        Notes
        -----
        `Huber` minimizes the function

        sum(psi((a[i]-mu)/scale)**2)

        as a function of (mu, scale), where

        psi(x) = np.clip(x, -self.c, self.c)
        Nr   r   r   TF)r   asarrayr   r   r"   r   	unsqueeze_estimate_both)rR   r   mu	initscaler   r?   est_muscales           r!   __call__zHuber.__call__   s    6 JqMM:
QA14(((BFF
ABF%%%EEEtQW55_Rqw//""1eRvqAAAr#   c           
         t          | j                  D ]*}|r| j        Nt          j        ||| j        |z  z
  || j        |z  z                                 |          |j        |         z  }n>t          j	        ||| j        ||| j        | j
                  }n|                                }t          j        |||j                  }t          j        t          j        ||z
  |z            | j                  }	|	                    |          }
t          j        |	||z
  dz  z  |          }|| j        z  |j        |         |
z
  | j        dz  z  z
  }t          j        ||z            }t          j        |||j                  }t          j        t          j        t          j        ||z
            || j
        z                      }t          j        t          j        t          j        ||z
            || j
        z                      }|r|s|}|}|                                |                                fc S t'          d| j        z            )ad  
        Estimate scale and location simultaneously with the following
        pseudo_loop:

        while not_converged:
            mu, scale = estimate_location(a, scale, mu), estimate_scale(a, scale, mu)

        where estimate_location is an M-estimator and estimate_scale implements
        the check used in Section 5.5 of Venables & Ripley
        Nr-   zJjoint estimation of location and scale failed to converge in %d iterations)rangerL   r   r   clipr   sumr   r   estimate_locationrM   r)   r   rW   
less_equalr   rQ   sqrtallr'   )rR   r   r\   rY   r   r[   r?   _nmusubsetcard	scale_numscale_denomnscaletest1test2s                   r!   rX   zHuber._estimate_both  s;    t|$$ (	7 (	7A #9$
 rDFUN2B%4G #d))'$-( C  15$)T2t|TX CC
 jjll/#tQW55C]261r6U*:#;#;TVDDF::d##DvSQ6==Idj.AGDMD,@DFaK+OOKWY455F_VT17;;FFbfUV^44ftx6GHH E FbfR#X..0ABB E  7e 7{{}}fnn&6&66666+-1\:
 
 	
r#   )rG   rH   rI   N)NNr   )__name__
__module____qualname____doc__rT   r]   rX    r#   r!   rF   rF      s^         <H H H H+B +B +B +BZ7
 7
 7
 7
 7
r#   rF   c                        e Zd ZdZddZd ZdS )	
HuberScalea  
    Huber's scaling for fitting robust linear models.

    Huber's scale is intended to be used as the scale estimate in the
    IRLS algorithm and is slightly different than the `Huber` class.

    Parameters
    ----------
    d : float, optional
        d is the tuning constant for Huber's scale.  Default is 2.5
    tol : float, optional
        The convergence tolerance
    maxiter : int, optiona
        The maximum number of iterations.  The default is 30.

    Methods
    -------
    call
        Return's Huber's scale computed as below

    Notes
    -----
    Huber's scale is the iterative solution to

    scale_(i+1)**2 = 1/(n*h)*sum(chi(r/sigma_i)*sigma_i**2

    where the Huber function is

    chi(x) = (x**2)/2       for \|x\| < d
    chi(x) = (d**2)/2       for \|x\| >= d

    and the Huber constant h = (n-p)/n*(d**2 + (1-d**2)*
    scipy.stats.norm.cdf(d) - .5 - d*sqrt(2*pi)*exp(-0.5*d**2)
          @rH   rI   c                 0    || _         || _        || _        d S N)drM   rL   )rR   ry   rM   rL   s       r!   rT   zHuberScale.__init__d  s    r#   c           	      *   
 ||z   j         dz  d j         dz  z
  t          j         j                   z  z   dz
   j         t          j        dt          j        z            z  t          j        d j         dz  z            z  z
  z  }t                    } fd
 
fd}t          j        |g}d}t          j	        ||dz
           ||         z
             j
        k    r| j        k     rt          j        d||z  z  t          j         ||d                             z  |d         dz  z            }	|                    |	           |dz  }t          j	        ||dz
           ||         z
             j
        k    r| j        k     |d         S )Nr-   r   g      ?g      c                 b    t          j        t          j        | z            j                  S rx   )r   lessr   ry   )xresidrR   s    r!   rh   z#HuberScale.__call__.<locals>.subsetv  s%    726%!),,df555r#   c                 l     |           | z  dz  z  dz  d |           z
  j         dz  dz  z  z   S rK   )ry   )sr~   rR   rh   s    r!   chiz HuberScale.__call__.<locals>.chiy  sK    6!99	a//!3q66!99}!a7  r#   )ry   rN   rO   r   rd   piexpr"   infr   rM   rL   ra   append)rR   df_residnobsr~   r@   r   r   	scalehistniterrl   rh   s   `  `      @r!   r]   zHuberScale.__call__i  s    !tv{?hl46&:&::; &BGAI../"&!9K2L2LLM 	
 JJ	6 	6 	6 	6 	6 	6	 	 	 	 	 	 	
 VQK	F9UQY')E*::;;dhFF$$W!8&Yr]++,,- B-1$% F V$$$QJE F9UQY')E*::;;dhFF$$ }r#   N)rv   rH   rI   )ro   rp   rq   rr   rT   r]   rs   r#   r!   ru   ru   @  sB        ! !F   
$ $ $ $ $r#   ru   )rr   numpyr   scipy.statsr   rN   statsmodels.toolsr   statsmodels.tools.validationr   r    r   r	   ppfr   r"   r,   rd   r8   rD   rF   huberru   hubers_scalers   r#   r!   <module>r      s        ( ( ( ( ( ( # # # # # # ? ? ? ? ? ? ? ?             X\'""29 *% *% *% *%Z X\%  <8<#6#66Q : : : :@ wrwqzzLHL$7$778q ' ' ' 'T 

\X\%%8%889    @K
 K
 K
 K
 K
 K
 K
 K
\ 	M M M M M M M M` z||r#   