U
    Kvf’a  ã                   @   s¬   d dl Zdd„ ZG dd„ dƒ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 )é    Nc                 C   s   | j dkd d }||  S )z®absolute value function that changes complex sign based on real sign

    This could be useful for complex step derivatives of functions that
    need abs. Not yet used.
    r   é   é   )Úreal)ÚxÚsign© r   ú</tmp/pip-unpacked-wheel-2v6byqio/statsmodels/robust/norms.pyÚ_cabs   s    r	   c                   @   s8   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )Ú
RobustNormaZ  
    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.

    See Also
    --------
    statsmodels.rlm

    Notes
    -----
    Currently only M-estimators are available.

    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.
    c                 C   s   t ‚dS )z|
        The robust criterion estimator function.

        Abstract method:

        -2 loglike used in M-estimator
        N©ÚNotImplementedError©ÚselfÚzr   r   r   Ú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'
        Nr   r   r   r   r   Úpsi4   s    zRobustNorm.psic                 C   s   t ‚dS )z_
        Returns the value of psi(z) / z

        Abstract method:

        psi(z) / z
        Nr   r   r   r   r   Úweights>   s    zRobustNorm.weightsc                 C   s   t ‚dS )z¶
        Derivative of psi.  Used to obtain robust covariance matrix.

        See statsmodels.rlm for more information.

        Abstract method:

        psi_derive = psi'
        Nr   r   r   r   r   Ú	psi_derivH   s    
zRobustNorm.psi_derivc                 C   s
   |   |¡S ©zH
        Returns the value of estimator rho applied to an input
        ©r   r   r   r   r   Ú__call__T   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
    c                 C   s   |d d S )zâ
        The least squares estimator rho function

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

        Returns
        -------
        rho : ndarray
            rho(z) = (1/2.)*z**2
        r   ç      à?r   r   r   r   r   r   d   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 : ndarray
            psi(z) = z
        )ÚnpÚasarrayr   r   r   r   r   u   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 : ndarray
            weights(z) = np.ones(z.shape)
        )r   r   ÚonesÚshapeÚfloat64r   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 : ndarray
            ones(z.shape)

        Notes
        -----
        Used to estimate the robust covariance matrix.
        )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   [   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Úabsr%   r   r   r   r   Ú_subset¾   s    
zHuberT._subsetc                 C   sJ   t  |¡}|  |¡}|d |d  d| t  |¡| j d| jd     S )a8  
        The robust criterion function for Huber's t.

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

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

            rho(z) = \|z\|*t - .5*t**2    for \|z\| > t
        r   r   r   )r   r   r)   r(   r%   ©r   r   Útestr   r   r   r   Å   s
    

$ÿz
HuberT.rhoc                 C   s4   t  |¡}|  |¡}|| d| | j t  |¡  S )aE  
        The psi function for Huber's t estimator

        The analytic derivative of rho

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

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

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

        The psi function scaled by z

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

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

            weights(z) = t/\|z\|      for \|z\| > t
        ç      ð?r   r   )r   ÚisscalarÚ
atleast_1dr)   r(   r%   )r   r   Ú
z_isscalarr+   ZabszÚvr   r   r   r   ð   s    



zHuberT.weightsc                 C   s   t  t  |¡| j¡ t¡S )zŽ
        The derivative of Huber's t psi function

        Notes
        -----
        Used to estimate the robust covariance matrix.
        )r   r'   r(   r%   ZastypeÚfloatr   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 r$   ©Úa©r   r6   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 : ndarray
            rho(z) = a**-2 * (1 - exp(-a*\|z\|)*(1 + a*\|z\|))
        r   r   ©r   r   Úexpr6   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 : ndarray
            psi(z) = z*exp(-a*\|z\|)
        r8   r   r   r   r   r   >  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 : ndarray
            weights(z) = exp(-a*\|z\|)
        r8   r   r   r   r   r   Q  s    
zRamsayE.weightsc                 C   sH   | j }t | t |¡ ¡}| | t |¡ }|}d}|| ||  S )z‘
        The derivative of Ramsay's Ea psi function.

        Notes
        -----
        Used to estimate the robust covariance matrix.
        r   )r6   r   r9   r(   r   )r   r   r6   r   ZdxÚyZdyr   r   r   r   e  s    zRamsayE.psi_derivN)r4   )	r   r   r   r   r&   r   r   r   r   r   r   r   r   r3     s   
r3   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
    ç•C‹lõ?c                 C   s
   || _ d S r$   r5   r7   r   r   r   r&   ƒ  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(   r6   Úpir   r   r   r   r)   †  s    
zAndrewWave._subsetc                 C   sL   | j }t |¡}|  |¡}||d  dt || ¡  d| |d  d  S )a{  
        The robust criterion function for Andrew's wave.

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

        Returns
        -------
        rho : ndarray
            The elements of rho are defined as:

            .. math::

                rho(z) & = a^2 *(1-cos(z/a)), |z| \leq a\pi \\
                rho(z) & = 2a, |z|>q\pi
        r   r   )r6   r   r   r)   Úcos©r   r   r6   r+   r   r   r   r     s    

ÿzAndrewWave.rhoc                 C   s0   | j }t |¡}|  |¡}|| t || ¡ S )aR  
        The psi function for Andrew's wave

        The analytic derivative of rho

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

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

            psi(z) = 0              for \|z\| > a*pi
        )r6   r   r   r)   Úsinr?   r   r   r   r   §  s    

zAndrewWave.psic                 C   sŽ   | j }t |¡}|  |¡}|| }t |¡t tj¡jk }t |¡rxt 	|¡}| }|| }|| t 
|¡ | ||< n|t 
|¡ | }|S )a}  
        Andrew's wave weighting function for the IRLS algorithm

        The psi function scaled by z

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

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

            weights(z) = 0                    for \|z\| > a*pi
        )r6   r   r   r)   r(   ZfinfoÚdoubleZepsÚanyZ	ones_liker@   )r   r   r6   r+   ZratioÚsmallr   Zlarger   r   r   r   ¿  s    



zAndrewWave.weightsc                 C   s   |   |¡}|t || j ¡ S )z’
        The derivative of Andrew's wave psi function

        Notes
        -----
        Used to estimate the robust covariance matrix.
        )r)   r   r>   r6   r*   r   r   r   r   ß  s    	
zAndrewWave.psi_derivN)r<   r2   r   r   r   r   r;   u  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 r$   ©Úc©r   rG   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(   rG   r   r   r   r   r)   ÿ  s    
zTrimmedMean._subsetc                 C   s:   t  |¡}|  |¡}||d  d d| | jd  d  S )aA  
        The robust criterion function for least trimmed mean.

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

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

            rho(z) = (1/2.)*c**2              for \|z\| > c
        r   r   r   ©r   r   r)   rG   r*   r   r   r   r     s    

zTrimmedMean.rhoc                 C   s   t  |¡}|  |¡}|| S )aQ  
        The psi function for least trimmed mean

        The analytic derivative of rho

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

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

            psi(z) = 0              for \|z\| > c
        ©r   r   r)   r*   r   r   r   r     s    

zTrimmedMean.psic                 C   s   t  |¡}|  |¡}|S )an  
        Least trimmed mean weighting function for the IRLS algorithm

        The psi function scaled by z

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

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

            weights(z) = 0             for \|z\| > c
        rJ   r*   r   r   r   r   2  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   H  s    
zTrimmedMean.psi_derivN)rE   r2   r   r   r   r   rD   í  s   
rD   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
    rE   ç      @ç       @c                 C   s   || _ || _|| _d S r$   )r6   ÚbrG   )r   r6   rN   rG   r   r   r   r&   f  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'   r6   rN   ZgreaterrG   )r   r   Út1Út2Út3r   r   r   r)   k  s
    zHampel._subsetc                 C   sð   | j | j| j  }}}t |¡}t |¡}|  |¡\}}}||B  }	t |jd¡}
tj	|j
|
d}t |¡}|| d d ||< |||  |d d  ||< ||||  d  ||  d ||< ||	  ||| |  d 7  < |rì|d }|S )aí  
        The robust criterion function for Hampel's estimator

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

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

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

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

            rho(z) = a*(b + c - a) / 2                 for \|z\| > c
        r1   ©Údtyper   r   g      à¿r   ©r6   rN   rG   r   r-   r.   r)   Úpromote_typesrS   Úzerosr    r(   )r   r   r6   rN   rG   r/   rO   rP   rQ   Zt34Údtr0   r   r   r   r   u  s    



$ z
Hampel.rhoc                 C   s¼   | j | j| j  }}}t |¡}t |¡}|  |¡\}}}t |jd¡}	tj	|j
|	d}
t |¡}t |¡}|| |
|< |||  |
|< |||  |||   ||  |
|< |r¸|
d }
|
S )aû  
        The psi function for Hampel's estimator

        The analytic derivative of rho

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

        Returns
        -------
        psi : ndarray
            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
        r1   rR   r   ©r6   rN   rG   r   r-   r.   r)   rU   rS   rV   r    r   r(   )r   r   r6   rN   rG   r/   rO   rP   rQ   rW   r0   ÚsZzar   r   r   r   ž  s    



$z
Hampel.psic                 C   s®   | j | j| j  }}}t |¡}t |¡}|  |¡\}}}t |jd¡}	tj	|j
|	d}
d|
|< t |¡}|||  |
|< || }|||  |||   |
|< |rª|
d }
|
S )a$  
        Hampel weighting function for the IRLS algorithm

        The psi function scaled by z

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

        Returns
        -------
        weights : ndarray
            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
        r1   rR   r,   r   rT   )r   r   r6   rN   rG   r/   rO   rP   rQ   rW   r0   Zabs_zZabs_zt3r   r   r   r   Ç  s    


zHampel.weightsc                 C   s¢   | j | j| j  }}}t |¡}t |¡}|  |¡\}}}t |jd¡}	tj	|j
|	d}
d|
|< || }|t |¡ |  t |¡||   |
|< |rž|
d }
|
S )zGDerivative of psi function, second derivative of rho function.
        r1   rR   r,   r   rX   )r   r   r6   rN   rG   r/   rO   Ú_rQ   rW   ÚdZzt3r   r   r   r   ð  s    

*zHampel.psi_derivN)rE   rL   rM   r2   r   r   r   r   rK   T  s   

)))rK   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 r$   rF   rH   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'   rG   r   r   r   r   r)     s    zTukeyBiweight._subsetc                 C   s<   |   |¡}| jd d }d|| j d  d  | | | S )a^  
        The robust criterion function for Tukey's biweight estimator

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

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

            rho(z) = 0                              for \|z\| > R
        r   g      @r   é   ©r)   rG   )r   r   ÚsubsetZfactorr   r   r   r     s    
zTukeyBiweight.rhoc                 C   s2   t  |¡}|  |¡}|d|| j d  d  | S )ar  
        The psi function for Tukey's biweight estimator

        The analytic derivative of rho

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

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

            psi(z) = 0                           for \|z\| > R
        r   r   rI   ©r   r   r`   r   r   r   r   3  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 : ndarray
            psi(z) = (1 - (z/c)**2)**2          for \|z\| <= R

            psi(z) = 0                          for \|z\| > R
        r   r   r_   ra   r   r   r   r   J  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_   ra   r   r   r   r   `  s    
&ÿzTukeyBiweight.psi_derivN)r]   r2   r   r   r   r   r\     s   
r\   c                   @   sH   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dS )ÚMQuantileNormu•  M-quantiles objective function based on a base norm

    This norm has the same asymmetric structure as the objective function
    in QuantileRegression but replaces the L1 absolute value by a chosen
    base norm.

        rho_q(u) = abs(q - I(q < 0)) * rho_base(u)

    or, equivalently,

        rho_q(u) = q * rho_base(u)  if u >= 0
        rho_q(u) = (1 - q) * rho_base(u)  if u < 0


    Parameters
    ----------
    q : float
        M-quantile, must be between 0 and 1
    base_norm : RobustNorm instance
        basic norm that is transformed into an asymmetric M-quantile norm

    Notes
    -----
    This is mainly for base norms that are not redescending, like HuberT or
    LeastSquares. (See Jones for the relationship of M-quantiles to quantiles
    in the case of non-redescending Norms.)

    Expectiles are M-quantiles with the LeastSquares as base norm.

    References
    ----------

    .. [*] Bianchi, Annamaria, and Nicola Salvati. 2015. â€œAsymptotic Properties
       and Variance Estimators of the M-Quantile Regression Coefficients
       Estimators.â€ Communications in Statistics - Theory and Methods 44 (11):
       2416â€“29. doi:10.1080/03610926.2013.791375.

    .. [*] Breckling, Jens, and Ray Chambers. 1988. â€œM-Quantiles.â€
       Biometrika 75 (4): 761â€“71. doi:10.2307/2336317.

    .. [*] Jones, M. C. 1994. â€œExpectiles and M-Quantiles Are Quantiles.â€
       Statistics & Probability Letters 20 (2): 149â€“53.
       doi:10.1016/0167-7152(94)90031-0.

    .. [*] Newey, Whitney K., and James L. Powell. 1987. â€œAsymmetric Least
       Squares Estimation and Testing.â€ Econometrica 55 (4): 819â€“47.
       doi:10.2307/1911031.
    c                 C   s   || _ || _d S r$   )ÚqÚ	base_norm)r   rd   re   r   r   r   r&   Ÿ  s    zMQuantileNorm.__init__c                 C   s8   t |ƒ}|dk }t |¡}d| j ||< | j|| < |S )Nr   r   )Úlenr   Úemptyrd   )r   r   ZnobsZmask_negÚqqr   r   r   Ú_get_q£  s    
zMQuantileNorm._get_qc                 C   s   |   |¡}|| j |¡ S )zÌ
        The robust criterion function for MQuantileNorm.

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

        Returns
        -------
        rho : ndarray
        )ri   re   r   ©r   r   rh   r   r   r   r   ¬  s    
zMQuantileNorm.rhoc                 C   s   |   |¡}|| j |¡ S )zñ
        The psi function for MQuantileNorm estimator.

        The analytic derivative of rho

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

        Returns
        -------
        psi : ndarray
        )ri   re   r   rj   r   r   r   r   ¼  s    
zMQuantileNorm.psic                 C   s   |   |¡}|| j |¡ S )a	  
        MQuantileNorm weighting function for the IRLS algorithm

        The psi function scaled by z, psi(z) / z

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

        Returns
        -------
        weights : ndarray
        )ri   re   r   rj   r   r   r   r   Î  s    
zMQuantileNorm.weightsc                 C   s   |   |¡}|| j |¡ S )a  
        The derivative of MQuantileNorm function

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

        Returns
        -------
        psi_deriv : ndarray

        Notes
        -----
        Used to estimate the robust covariance matrix.
        )ri   re   r   rj   r   r   r   r   à  s    
zMQuantileNorm.psi_derivc                 C   s
   |   |¡S r   r   r   r   r   r   r   ô  s    zMQuantileNorm.__call__N)r   r   r   r   r&   ri   r   r   r   r   r   r   r   r   r   rc   m  s   1	rc   é   çíµ ÷Æ°>c              	   C   s    |dkrt ƒ }|dkr$t | |¡}n|}t|ƒD ]^}| | | | ¡}	t |	|  |¡t |	|¡ }
t t t ||
 ¡|| ¡¡rŠ|
  S |
}q0t	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 : ndarray
        Array over which the location parameter is to be estimated
    scale : ndarray
        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 : ndarray, 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 : ndarray
        Estimate of location
    Nz6location estimator failed to converge in %d iterations)
r"   r   ZmedianÚranger   ÚsumÚallZlessr(   Ú
ValueError)r6   ZscaleZnormZaxisÚinitialÚmaxiterZtolÚmurZ   ÚWZnmur   r   r   Úestimate_locationû  s    ! ÿru   )Nr   Nrk   rl   )Znumpyr   r	   r
   r   r"   r3   r;   rD   rK   r\   rc   ru   r   r   r   r   Ú<module>   s    
KQn[xg 2h     ÿ