
    >h.2                        S r SSKrSSKJr  SSKJr  SSKJ	r	J
r
  SSKJr  SSKJr  \R                  " S	5      S\R                  4S
 jr\R                  " S	5      \R                  " S5      -
  S4S jrS\R$                  " S5      \R                  " S5      -  -  S4S jrS\R$                  " S5      \R                  " S5      -  -  4S jr " S S5      r\" 5       r " S S5      r\" 5       rg)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                 2   [        U SSS9n [        US5      nU R                  (       d  SnON[        U5      (       a2  Ub  [        R
                  " X0U5      nO#U" U R                  5       5      nO[        US5      n[        R                  " X-
  5      U-  nUR                  (       d_  Ub  UR                  S:X  a  [        R                  $ [        UR                  5      nUR                  U5        [        R                  " U5      $ [        R                  " XRS9$ )	aZ  
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          kC:\Users\julio\OneDrive\Documentos\Trabajo\Ideas Frescas\venv\Lib\site-packages\statsmodels/robust/scale.pymadr"      s    . 	1c%A1cA66
	&		++Ft<J	*J1
66!.!Q
&C88<388q=66MOEIIdO88E?"99S$$          ?c                 .   [        U SSS9n [        US5      nU R                  S:X  a  [        S5      eU R                  S:X  a  [
        R                  $ [
        R                  " U SS/US	9n[
        R                  " [
        R                  " USS	9U-  5      $ )
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vv{?@@	
1vvKKD$<d;	zz"'')!4q899r#      g      ?c           	      6   [        U SS[        R                  SSS9n [        US5      nU R                  S:X  a  [        S5      eU R                  S:X  a  [        R                  $ [        R                  " [        X US	9nUR                  S:X  a  [        U5      $ U$ )
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vv{?@@	
1vv!!#D1=88q=:
r#   c                    [         R                  " U 5      n U R                  S   nU R                  S:X  a  [         R                  $ [        US-  S-   5      n[        X3S-
  -  S-  5      n[         R                  " USS9n[         R                  " XS      XS      -
  5      n[         R                  " XdS-
  S9US-
     nX-  nU$ )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vv{vvQ
OUa ooa1%qQy1V9,-eQ/A6r#   c                   2    \ rS rSrSrSS jrS	S jrS rSrg)
Huber   a  
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))
Nc                     Xl         X0l        X l        X@l        S[        R
                  " U5      -  S-
  nXQS-  SU-
  -  -   SU-  [        R                  " U5      -  -
  U l        g Nr-   r   )r   maxitertolr   Gaussiancdfpdfgamma)selfr   rK   rJ   r   tmps         r!   __init__Huber.__init__   sZ    	(,,q/!A%6QW--AQ0GG
r#   c                 t   [         R                  " U5      nUc)  UR                  S   S-
  n[         R                  " XS9nSnOUR                  S   nUnSnUc
  [	        XS9nOUn[
        R                  " XtUR                  5      n[
        R                  " X$UR                  5      nU R                  XX$Xe5      $ )a  
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)
r   r   r   TF)r   asarrayr   r   r"   r   	unsqueeze_estimate_both)rP   r   mu	initscaler   r?   est_muscales           r!   __call__Huber.__call__   s    6 JJqM:
QA1(BF
ABF%EEQWW5__Rqww/""1RvAAr#   c           
      Z   [        U R                  5       GHz  nU(       a  U R                  cV  [        R                  " XU R
                  U-  -
  X0R
                  U-  -   5      R                  U5      UR                  U   -  nOI[        R                  " XU R                  XCU R                  U R                  5      nOUR                  5       n[        R                  " XUR                  5      n[        R                  " [        R                  " X-
  U-  5      U R
                  5      n	U	R                  U5      n
[        R                  " XU-
  S-  -  U5      nX`R                   -  UR                  U   U
-
  U R
                  S-  -  -
  n[        R"                  " X-  5      n[        R                  " XUR                  5      n[        R$                  " [        R                  " [        R                  " X--
  5      XR                  -  5      5      n[        R$                  " [        R                  " [        R                  " X8-
  5      XR                  -  5      5      nU(       a  U(       d  UnUnGM[  UR                  5       UR                  5       4s  $    ['        SU R                  -  5      e)a,  
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
r-   zJjoint estimation of location and scale failed to converge in %d iterations)rangerJ   r   r   clipr   sumr   r   estimate_locationrK   r)   r   rV   
less_equalr   rO   sqrtallr'   )rP   r   r[   rX   r   rZ   r?   _nmusubsetcard	scale_numscale_denomnscaletest1test2s                   r!   rW   Huber._estimate_both  s    t||$A99$
 DFFUN2B%4G#d)''$-(   11$))Tt||TXXC
 jjl//#QWW5C]]26616U*:#;TVVDF::d#DvSQ6=Ijj.AGGDMD,@DFFaK+OOKWWY45F__V177;FFFbffU^4fxx6GHE FFbffRX.0ABE e{{}fnn&666Q %R +-1\\:
 	
r#   )r   rO   rJ   r   rK   )g      ?:0yE>   N)NNr   )	__name__
__module____qualname____firstlineno____doc__rR   r\   rW   __static_attributes__ r#   r!   rF   rF      s    <H+BZ7
r#   rF   c                   (    \ rS rSrSrSS jrS rSrg)
HuberScalei@  a6  
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)
c                 (    Xl         X l        X0l        g N)drK   rJ   )rP   r}   rK   rJ   s       r!   rR   HuberScale.__init__d  s    r#   c           	      n  ^ ^^
 UU-  T R                   S-  ST R                   S-  -
  [        R                  " T R                   5      -  -   S-
  T R                   [        R                  " S[        R
                  -  5      -  [        R                  " ST R                   S-  -  5      -  -
  -  n[        T5      nUU 4S jm
UU U
4S jn[        R                  U/nSn[        R                  " XxS-
     Xx   -
  5      T R                  :  a  UT R                  :  a  [        R                  " SX$-  -  [        R                  " U" US   5      5      -  US   S-  -  5      n	UR                  U	5        US-  n[        R                  " XxS-
     Xx   -
  5      T R                  :  a  UT R                  :  a  M  US   $ )Nr-   r   g      ?g      c                 t   > [         R                  " [         R                  " TU -  5      TR                  5      $ r|   )r   lessr   r}   )xresidrP   s    r!   rh   #HuberScale.__call__.<locals>.subsetv  s%    77266%!),dff55r#   c                 j   > T" U 5      TU -  S-  -  S-  ST" U 5      -
  TR                   S-  S-  -  -   $ rI   )r}   )sr   rP   rh   s    r!   chi HuberScale.__call__.<locals>.chiy  sE    !9	a//!3q6!9}!a7  r#   )r}   rL   rM   r   rd   piexpr"   infr   rK   rJ   ra   append)rP   df_residnobsr   r@   r   r   	scalehistniterrl   rh   s   `  `      @r!   r\   HuberScale.__call__i  s    !tvv{?hll466&::; &&BGGAI./"&&!9K2LLM 	
 J	6	
 VVQK	FF9QY')*::;dhhF$WW8&&Yr]+,- B-1$%F V$QJE FF9QY')*::;dhhF$ }r#   )r}   rJ   rK   N)g      @rp   rq   )rr   rs   rt   ru   rv   rR   r\   rw   rx   r#   r!   rz   rz   @  s    !F
$r#   rz   )rv   numpyr   scipy.statsr   rL   statsmodels.toolsr   statsmodels.tools.validationr   r    r   r	   ppfr   r"   r,   rd   r8   rD   rF   huberrz   hubers_scalerx   r#   r!   <module>r      s     ( # ?   \\'"299 *%Z \\% 8<<#66Q :@ rwwqzHLL$778q 'T 
X\\%%889 @K
 K
\ 	M M` |r#   