B
    ZvP                 @   s   d dl mZ d dlZG dd deZG dd deZG dd deZG d	d
 d
eZG dd deZ	G dd deZ
G dd deZG dd deZdddZdS )    )rangeNc               @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )
RobustNorma1  
    The parent class for the norms used for robust regression.

    Lays out the methods expected of the robust norms to be used
    by statsmodels.RLM.

    Parameters
    ----------
    None :
        Some subclasses have optional tuning constants.

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

    DC Montgomery, EA Peck. 'Introduction to Linear Regression Analysis',
        John Wiley and Sons, Inc., New York, 2001.

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

    See Also
    --------
    statsmodels.rlm for more information on how the estimators are used
    and the inputs for the methods of RobustNorm and subclasses.

    Notes
    -----
    Currently only M-estimators are available.
    c             C   s   t dS )z|
        The robust criterion estimator function.

        Abstract method:

        -2 loglike used in M-estimator
        N)NotImplementedError)selfz r   7lib/python3.7/site-packages/statsmodels/robust/norms.pyrho&   s    zRobustNorm.rhoc             C   s   t dS )z
        Derivative of rho.  Sometimes referred to as the influence function.

        Abstract method:

        psi = rho'
        N)r   )r   r   r   r   r   psi0   s    zRobustNorm.psic             C   s   t dS )z_
        Returns the value of psi(z) / z

        Abstract method:

        psi(z) / z
        N)r   )r   r   r   r   r   weights:   s    zRobustNorm.weightsc             C   s   t dS )z
        Deriative of psi.  Used to obtain robust covariance matrix.

        See statsmodels.rlm for more information.

        Abstract method:

        psi_derive = psi'
        N)r   )r   r   r   r   r   	psi_derivD   s    
zRobustNorm.psi_derivc             C   s
   |  |S )zH
        Returns the value of estimator rho applied to an input
        )r	   )r   r   r   r   r   __call__P   s    zRobustNorm.__call__N)	__name__
__module____qualname____doc__r	   r
   r   r   r   r   r   r   r   r      s   


r   c               @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )LeastSquaresz
    Least squares rho for M-estimation and its derived functions.

    See also
    --------
    statsmodels.robust.norms.RobustNorm for the methods.
    c             C   s   |d d S )z
        The least squares estimator rho function

        Parameters
        -----------
        z : array
            1d array

        Returns
        -------
        rho : array
            rho(z) = (1/2.)*z**2
           g      ?r   )r   r   r   r   r   r	   `   s    zLeastSquares.rhoc             C   s
   t |S )a	  
        The psi function for the least squares estimator

        The analytic derivative of rho

        Parameters
        ----------
        z : array-like
            1d array

        Returns
        -------
        psi : array
            psi(z) = z
        )npasarray)r   r   r   r   r   r
   q   s    zLeastSquares.psic             C   s   t |}t |jt jS )a>  
        The least squares estimator weighting function for the IRLS algorithm.

        The psi function scaled by the input z

        Parameters
        ----------
        z : array-like
            1d array

        Returns
        -------
        weights : array
            weights(z) = np.ones(z.shape)
        )r   r   onesshapefloat64)r   r   r   r   r   r      s    
zLeastSquares.weightsc             C   s   t |jt jS )z
        The derivative of the least squares psi function.

        Returns
        -------
        psi_deriv : array
            ones(z.shape)

        Notes
        -----
        Used to estimate the robust covariance matrix.
        )r   r   r   r   )r   r   r   r   r   r      s    zLeastSquares.psi_derivN)r   r   r   r   r	   r
   r   r   r   r   r   r   r   V   s
   r   c               @   sB   e Zd ZdZdddZdd Zdd Zd	d
 Zdd Zdd Z	dS )HuberTz
    Huber's T for M estimation.

    Parameters
    ----------
    t : float, optional
        The tuning constant for Huber's t function. The default value is
        1.345.

    See also
    --------
    statsmodels.robust.norms.RobustNorm
    Q?c             C   s
   || _ d S )N)t)r   r   r   r   r   __init__   s    zHuberT.__init__c             C   s   t |}t t || jS )zE
        Huber's T is defined piecewise over the range for z
        )r   r   
less_equalfabsr   )r   r   r   r   r   _subset   s    
zHuberT._subsetc             C   sJ   t |}| |}|d |d  d| t || j d| jd     S )a6  
        The robust criterion function for Huber's t.

        Parameters
        ----------
        z : array-like
            1d array

        Returns
        -------
        rho : array
            rho(z) = .5*z**2            for \|z\| <= t

            rho(z) = \|z\|*t - .5*t**2    for \|z\| > t
        g      ?r      )r   r   r   r   r   )r   r   testr   r   r   r	      s    

z
HuberT.rhoc             C   s4   t |}| |}|| d| | j t |  S )aC  
        The psi function for Huber's t estimator

        The analytic derivative of rho

        Parameters
        ----------
        z : array-like
            1d array

        Returns
        -------
        psi : array
            psi(z) = z      for \|z\| <= t

            psi(z) = sign(z)*t for \|z\| > t
        r    )r   r   r   r   sign)r   r   r!   r   r   r   r
      s    

z
HuberT.psic             C   s<   t |}| |}t |}d||< |d| | j |  S )a_  
        Huber's t weighting function for the IRLS algorithm

        The psi function scaled by z

        Parameters
        ----------
        z : array-like
            1d array

        Returns
        -------
        weights : array
            weights(z) = 1          for \|z\| <= t

            weights(z) = t/\|z\|      for \|z\| > t
        g      ?r    )r   r   r   r   r   )r   r   r!   Zabszr   r   r   r      s
    


zHuberT.weightsc             C   s   t t || jS )z
        The derivative of Huber's t psi function

        Notes
        -----
        Used to estimate the robust covariance matrix.
        )r   r   r   r   )r   r   r   r   r   r     s    zHuberT.psi_derivN)r   )
r   r   r   r   r   r   r	   r
   r   r   r   r   r   r   r      s   
r   c               @   s:   e Zd ZdZdddZdd Zdd Zd	d
 Zdd ZdS )RamsayEz
    Ramsay's Ea for M estimation.

    Parameters
    ----------
    a : float, optional
        The tuning constant for Ramsay's Ea function.  The default value is
        0.3.

    See also
    --------
    statsmodels.robust.norms.RobustNorm
    333333?c             C   s
   || _ d S )N)a)r   r%   r   r   r   r     s    zRamsayE.__init__c             C   sD   t |}dt | j t | d| jt |    | jd  S )a  
        The robust criterion function for Ramsay's Ea.

        Parameters
        ----------
        z : array-like
            1d array

        Returns
        -------
        rho : array
            rho(z) = a**-2 * (1 - exp(-a*\|z\|)*(1 + a*\|z\|))
        r    r   )r   r   expr%   r   )r   r   r   r   r   r	      s    
zRamsayE.rhoc             C   s&   t |}|t | j t |  S )a  
        The psi function for Ramsay's Ea estimator

        The analytic derivative of rho

        Parameters
        ----------
        z : array-like
            1d array

        Returns
        -------
        psi : array
            psi(z) = z*exp(-a*\|z\|)
        )r   r   r&   r%   r   )r   r   r   r   r   r
   2  s    
zRamsayE.psic             C   s"   t |}t | j t | S )a   
        Ramsay's Ea weighting function for the IRLS algorithm

        The psi function scaled by z

        Parameters
        ----------
        z : array-like
            1d array

        Returns
        -------
        weights : array
            weights(z) = exp(-a*\|z\|)
        )r   r   r&   r%   r   )r   r   r   r   r   r   E  s    
zRamsayE.weightsc             C   sJ   t | j t | |d t | j t |  | j  t |  S )z
        The derivative of Ramsay's Ea psi function.

        Notes
        -----
        Used to estimate the robust covariance matrix.
        r   )r   r&   r%   r   )r   r   r   r   r   r   Y  s    	zRamsayE.psi_derivN)r$   )	r   r   r   r   r   r	   r
   r   r   r   r   r   r   r#     s   
r#   c               @   sB   e Zd ZdZdddZdd Zdd Zd	d
 Zdd Zdd Z	dS )
AndrewWavea   
    Andrew's wave for M estimation.

    Parameters
    ----------
    a : float, optional
        The tuning constant for Andrew's Wave function.  The default value is
        1.339.

    See also
    --------
    statsmodels.robust.norms.RobustNorm
    Cl?c             C   s
   || _ d S )N)r%   )r   r%   r   r   r   r   t  s    zAndrewWave.__init__c             C   s$   t |}t t || jt j S )zI
        Andrew's wave is defined piecewise over the range of z.
        )r   r   r   r   r%   Zpi)r   r   r   r   r   r   w  s    
zAndrewWave._subsetc             C   sD   | j }t|}| |}|| dt||   d| d |  S )a>  
        The robust criterion function for Andrew's wave.

        Parameters
        ----------
        z : array-like
            1d array

        Returns
        -------
        rho : array
            rho(z) = a*(1-cos(z/a))     for \|z\| <= a*pi

            rho(z) = 2*a                for \|z\| > a*pi
        r    r   )r%   r   r   r   cos)r   r   r%   r!   r   r   r   r	   ~  s
    

zAndrewWave.rhoc             C   s,   | j }t|}| |}|t||  S )aP  
        The psi function for Andrew's wave

        The analytic derivative of rho

        Parameters
        ----------
        z : array-like
            1d array

        Returns
        -------
        psi : array
            psi(z) = sin(z/a)       for \|z\| <= a*pi

            psi(z) = 0              for \|z\| > a*pi
        )r%   r   r   r   sin)r   r   r%   r!   r   r   r   r
     s    

zAndrewWave.psic             C   s4   | j }t|}| |}|t||  ||  S )aw  
        Andrew's wave weighting function for the IRLS algorithm

        The psi function scaled by z

        Parameters
        ----------
        z : array-like
            1d array

        Returns
        -------
        weights : array
            weights(z) = sin(z/a)/(z/a)     for \|z\| <= a*pi

            weights(z) = 0                  for \|z\| > a*pi
        )r%   r   r   r   r*   )r   r   r%   r!   r   r   r   r     s    

zAndrewWave.weightsc             C   s$   |  |}|t|| j  | j S )z
        The derivative of Andrew's wave psi function

        Notes
        -----
        Used to estimate the robust covariance matrix.
        )r   r   r)   r%   )r   r   r!   r   r   r   r     s    	
zAndrewWave.psi_derivN)r(   )
r   r   r   r   r   r   r	   r
   r   r   r   r   r   r   r'   e  s   
r'   c               @   sB   e Zd ZdZdddZdd Zdd Zd	d
 Zdd Zdd Z	dS )TrimmedMeana  
    Trimmed mean function for M-estimation.

    Parameters
    ----------
    c : float, optional
        The tuning constant for Ramsay's Ea function.  The default value is
        2.0.

    See also
    --------
    statsmodels.robust.norms.RobustNorm
           @c             C   s
   || _ d S )N)c)r   r-   r   r   r   r     s    zTrimmedMean.__init__c             C   s   t |}t t || jS )zN
        Least trimmed mean is defined piecewise over the range of z.
        )r   r   r   r   r-   )r   r   r   r   r   r     s    
zTrimmedMean._subsetc             C   s$   t |}| |}||d  d S )a5  
        The robust criterion function for least trimmed mean.

        Parameters
        ----------
        z : array-like
            1d array

        Returns
        -------
        rho : array
            rho(z) = (1/2.)*z**2    for \|z\| <= c

            rho(z) = 0              for \|z\| > c
        r   g      ?)r   r   r   )r   r   r!   r   r   r   r	     s    

zTrimmedMean.rhoc             C   s   t |}| |}|| S )aP  
        The psi function for least trimmed mean

        The analytic derivative of rho

        Parameters
        ----------
        z : array-like
            1d array

        Returns
        -------
        psi : array
            psi(z) = z              for \|z\| <= c

            psi(z) = 0              for \|z\| > c

        )r   r   r   )r   r   r!   r   r   r   r
      s    

zTrimmedMean.psic             C   s   t |}| |}|S )am  
        Least trimmed mean weighting function for the IRLS algorithm

        The psi function scaled by z

        Parameters
        ----------
        z : array-like
            1d array

        Returns
        -------
        weights : array
            weights(z) = 1             for \|z\| <= c

            weights(z) = 0             for \|z\| > c

        )r   r   r   )r   r   r!   r   r   r   r     s    

zTrimmedMean.weightsc             C   s   |  |}|S )z
        The derivative of least trimmed mean psi function

        Notes
        -----
        Used to estimate the robust covariance matrix.
        )r   )r   r   r!   r   r   r   r   .  s    
zTrimmedMean.psi_derivN)r,   )
r   r   r   r   r   r   r	   r
   r   r   r   r   r   r   r+     s   
r+   c               @   sB   e Zd ZdZdddZdd Zd	d
 Zdd Zdd Zdd Z	dS )Hampela;  

    Hampel function for M-estimation.

    Parameters
    ----------
    a : float, optional
    b : float, optional
    c : float, optional
        The tuning constants for Hampel's function.  The default values are
        a,b,c = 2, 4, 8.

    See also
    --------
    statsmodels.robust.norms.RobustNorm
           @      @       @c             C   s   || _ || _|| _d S )N)r%   br-   )r   r%   r2   r-   r   r   r   r   K  s    zHampel.__init__c             C   s`   t t |}t || j}t || jt || j }t || jt || j }|||fS )zL
        Hampel's function is defined piecewise over the range of z
        )r   r   r   r   r%   r2   Zgreaterr-   )r   r   t1t2t3r   r   r   r   P  s
    zHampel._subsetc       	      C   s   t |}| j}| j}| j}| |\}}}||d  d ||| |d d    |||| |d d   ||  d|d  d    d| | | | || |   }|S )a  
        The robust criterion function for Hampel's estimator

        Parameters
        ----------
        z : array-like
            1d array

        Returns
        -------
        rho : array
            rho(z) = (1/2.)*z**2                    for \|z\| <= a

            rho(z) = a*\|z\| - 1/2.*a**2              for a < \|z\| <= b

            rho(z) = a*(c*\|z\|-(1/2.)*z**2)/(c-b)    for b < \|z\| <= c

            rho(z) = a*(b + c - a)                  for \|z\| > c
        r   g      ?   g      @r    )r   r   r%   r2   r-   r   )	r   r   r%   r2   r-   r3   r4   r5   vr   r   r   r	   Z  s    
  Z"z
Hampel.rhoc       
      C   sx   t |}| j}| j}| j}| |\}}}t |}t |}||| || |  || | ||  ||    }	|	S )a  
        The psi function for Hampel's estimator

        The analytic derivative of rho

        Parameters
        ----------
        z : array-like
            1d array

        Returns
        -------
        psi : array
            psi(z) = z                            for \|z\| <= a

            psi(z) = a*sign(z)                    for a < \|z\| <= b

            psi(z) = a*sign(z)*(c - \|z\|)/(c-b)    for b < \|z\| <= c

            psi(z) = 0                            for \|z\| > c
        )r   r   r%   r2   r-   r   r"   r   )
r   r   r%   r2   r-   r3   r4   r5   sr7   r   r   r   r
   x  s    
  

 z
Hampel.psic       	      C   s   t |}| j}| j}| j}| |\}}}||| t |  || |t |  t |||    }d|t t |< |S )a  
        Hampel weighting function for the IRLS algorithm

        The psi function scaled by z

        Parameters
        ----------
        z : array-like
            1d array

        Returns
        -------
        weights : array
            weights(z) = 1                            for \|z\| <= a

            weights(z) = a/\|z\|                        for a < \|z\| <= b

            weights(z) = a*(c - \|z\|)/(\|z\|*(c-b))      for b < \|z\| <= c

            weights(z) = 0                            for \|z\| > c

        g      ?)	r   r   r%   r2   r-   r   r   whereZisnan)	r   r   r%   r2   r-   r3   r4   r5   r7   r   r   r   r     s    
  *zHampel.weightsc             C   sB   |  |\}}}||| jt| |  t|| j| j    S )N)r   r%   r   r"   r   r-   r2   )r   r   r3   r4   r5   r   r   r   r     s    zHampel.psi_derivN)r/   r0   r1   )
r   r   r   r   r   r   r	   r
   r   r   r   r   r   r   r.   9  s   

  r.   c               @   sB   e Zd ZdZdddZdd Zdd Zd	d
 Zdd Zdd Z	dS )TukeyBiweighta  

    Tukey's biweight function for M-estimation.

    Parameters
    ----------
    c : float, optional
        The tuning constant for Tukey's Biweight.  The default value is
        c = 4.685.

    Notes
    -----
    Tukey's biweight is sometime's called bisquare.
    =
ףp@c             C   s
   || _ d S )N)r-   )r   r-   r   r   r   r     s    zTukeyBiweight.__init__c             C   s   t t |}t || jS )zK
        Tukey's biweight is defined piecewise over the range of z
        )r   r   r   r   r-   )r   r   r   r   r   r     s    zTukeyBiweight._subsetc             C   s4   |  |}d|| j d  d  | | jd  d S )a\  
        The robust criterion function for Tukey's biweight estimator

        Parameters
        ----------
        z : array-like
            1d array

        Returns
        -------
        rho : array
            rho(z) = -(1 - (z/c)**2)**3 * c**2/6.   for \|z\| <= R

            rho(z) = 0                              for \|z\| > R
        r    r      g      @)r   r-   )r   r   subsetr   r   r   r	     s    
zTukeyBiweight.rhoc             C   s2   t |}| |}|d|| j d  d  | S )ap  
        The psi function for Tukey's biweight estimator

        The analytic derivative of rho

        Parameters
        ----------
        z : array-like
            1d array

        Returns
        -------
        psi : array
            psi(z) = z*(1 - (z/c)**2)**2        for \|z\| <= R

            psi(z) = 0                           for \|z\| > R
        r    r   )r   r   r   r-   )r   r   r=   r   r   r   r
     s    

zTukeyBiweight.psic             C   s$   |  |}d|| j d  d | S )a|  
        Tukey's biweight weighting function for the IRLS algorithm

        The psi function scaled by z

        Parameters
        ----------
        z : array-like
            1d array

        Returns
        -------
        weights : array
            psi(z) = (1 - (z/c)**2)**2          for \|z\| <= R

            psi(z) = 0                          for \|z\| > R
        r    r   )r   r-   )r   r   r=   r   r   r   r     s    
zTukeyBiweight.weightsc             C   sL   |  |}|d|| j d  d d|d  | jd  d|| j d     S )z
        The derivative of Tukey's biweight psi function

        Notes
        -----
        Used to estimate the robust covariance matrix.
        r    r      )r   r-   )r   r   r=   r   r   r   r     s    
*zTukeyBiweight.psi_derivN)r;   )
r   r   r   r   r   r   r	   r
   r   r   r   r   r   r   r:     s   
r:      ư>c          	   C   s   |dkrt  }|dkr$t| |}n|}xft|D ]Z}|| | | }	t|	|  |t|	| }
ttt||
 || r|
S |
}q2W t	d| dS )a  
    M-estimator of location using self.norm and a current
    estimator of scale.

    This iteratively finds a solution to

    norm.psi((a-mu)/scale).sum() == 0

    Parameters
    ----------
    a : array
        Array over which the location parameter is to be estimated
    scale : array
        Scale parameter to be used in M-estimator
    norm : RobustNorm, optional
        Robust norm used in the M-estimator.  The default is HuberT().
    axis : int, optional
        Axis along which to estimate the location parameter.  The default is 0.
    initial : array, optional
        Initial condition for the location parameter.  Default is None, which
        uses the median of a.
    niter : int, optional
        Maximum number of iterations.  The default is 30.
    tol : float, optional
        Toleration for convergence.  The default is 1e-06.

    Returns
    --------
    mu : array
        Estimate of location
    Nz6location estimator failed to converge in %d iterations)
r   r   Zmedianr   r   sumZalltrueZlessr   
ValueError)r%   ZscaleZnormZaxisinitialmaxiterZtolZmuiterWZnmur   r   r   estimate_location#  s    ! rG   )Nr   Nr?   r@   )Zstatsmodels.compat.pythonr   Znumpyr   objectr   r   r   r#   r'   r+   r.   r:   rG   r   r   r   r   <module>   s   PQgWlh g 