o
    þ0Gf’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   ú8lib/python3.10/site-packages/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   ó   t ‚)z|
        The robust criterion estimator function.

        Abstract method:

        -2 loglike used in M-estimator
        ©ÚNotImplementedError©ÚselfÚzr   r   r   Úrho*   ó   zRobustNorm.rhoc                 C   r   )z„
        Derivative of rho.  Sometimes referred to as the influence function.

        Abstract method:

        psi = rho'
        r   r   r   r   r   Úpsi4   r   zRobustNorm.psic                 C   r   )z_
        Returns the value of psi(z) / z

        Abstract method:

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

        See statsmodels.rlm for more information.

        Abstract method:

        psi_derive = psi'
        r   r   r   r   r   Ú	psi_derivH   s   
zRobustNorm.psi_derivc                 C   ó
   |   |¡S ©zH
        Returns the value of estimator rho applied to an input
        ©r   r   r   r   r   Ú__call__T   ó   
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   ˆ   ó   
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                   @   óB   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   ó
   || _ d S ©N)Út)r   r,   r   r   r   Ú__init__»   ó   
zHuberT.__init__c                 C   ó   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"   r2   r1   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"   r2   r,   r   r3   r   r   r   r   Ú   s   

 z
HuberT.psic                 C   sV   t  |¡}t  |¡}|  |¡}t  |¡}d||< |d| | j |  }|r)|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_1dr2   r1   r,   )r   r   Ú
z_isscalarr4   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!   r0   r1   r,   ZastypeÚfloatr   r   r   r   r     s   zHuberT.psi_derivN)r)   ©
r   r   r   r   r-   r2   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   r*   r+   ©Úa©r   r?   r   r   r   r-   )  r.   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"   Úexpr?   r1   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\|)
        rA   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\|)
        rA   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   )r?   r!   rB   r1   r   )r   r   r?   r   ZdxÚyZdyr   r   r   r   e  s   zRamsayE.psi_derivN)r=   )	r   r   r   r   r-   r   r   r   r   r   r   r   r   r<     s    
r<   c                   @   r'   )Ú
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   r*   r+   r>   r@   r   r   r   r-   ƒ  r.   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"   r0   r1   r?   Zpir   r   r   r   r2   †  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   )r?   r!   r"   r2   Úcos©r   r   r?   r4   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
        )r?   r!   r"   r2   ÚsinrG   r   r   r   r   §  s   

zAndrewWave.psic                 C   s   | j }t |¡}|  |¡}|| }t |¡t tj¡jk }t |¡r=t 	|¡}| }|| }|| t 
|¡ | ||< |S |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
        )r?   r!   r"   r2   r1   ZfinfoZdoubleZepsÚanyZ	ones_likerH   )r   r   r?   r4   ZratioZ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.
        )r2   r!   rF   r?   r3   r   r   r   r   ß  s   
	zAndrewWave.psi_derivN)rE   r;   r   r   r   r   rD   u  s    
 rD   c                   @   r'   )Ú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   r*   r+   ©Úc©r   rM   r   r   r   r-   ü  r.   zTrimmedMean.__init__c                 C   r/   )zN
        Least trimmed mean is defined piecewise over the range of z.
        )r!   r"   r0   r1   rM   r   r   r   r   r2   ÿ  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"   r2   rM   r3   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"   r2   r3   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
        rP   r3   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.
        )r2   r3   r   r   r   r   H  s   
zTrimmedMean.psi_derivN)rK   r;   r   r   r   r   rJ   í  s    
rJ   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
    rK   ç      @ç       @c                 C   s   || _ || _|| _d S r+   )r?   ÚbrM   )r   r?   rT   rM   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!   r1   r"   r0   r?   rT   ZgreaterrM   )r   r   Út1Út2Út3r   r   r   r2   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  < |rv|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
        r:   ©Údtyper   r    g      à¿r   ©r?   rT   rM   r!   r6   r7   r2   Úpromote_typesrY   Úzerosr$   r1   )r   r   r?   rT   rM   r8   rU   rV   rW   Zt34Údtr9   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
        r:   rX   r   ©r?   rT   rM   r!   r6   r7   r2   r[   rY   r\   r$   r   r1   )r   r   r?   rT   rM   r8   rU   rV   rW   r]   r9   Ú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 |¡}|||  |
|< || }|||  |||   |
|< |rU|
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
        r:   rX   r5   r   rZ   )r   r   r?   rT   rM   r8   rU   rV   rW   r]   r9   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 |¡||   |
|< |rO|
d }
|
S )zGDerivative of psi function, second derivative of rho function.
        r:   rX   r5   r   r^   )r   r   r?   rT   rM   r8   rU   Ú_rW   r]   ÚdZzt3r   r   r   r   ð  s   

*zHampel.psi_derivN)rK   rR   rS   r;   r   r   r   r   rQ   T  s    

)))rQ   c                   @   r'   )Ú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   r*   r+   rL   rN   r   r   r   r-     r.   zTukeyBiweight.__init__c                 C   s   t  t  |¡¡}t  || j¡S )zK
        Tukey's biweight is defined piecewise over the range of z
        )r!   r1   r"   r0   rM   r   r   r   r   r2     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   é   ©r2   rM   )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   rO   ©r   r   rf   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   re   rg   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   é   re   rg   r   r   r   r   `  s   
&ÿzTukeyBiweight.psi_derivN)rc   r;   r   r   r   r   rb     s    
rb   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   rj   rk   r   r   r   r-   Ÿ  s   
zMQuantileNorm.__init__c                 C   s8   t |ƒ}|dk }t |¡}d| j ||< | j|| < |S )Nr   r   )Úlenr!   Úemptyrj   )r   r   ZnobsZmask_negÚqqr   r   r   Ú_get_q£  s   
zMQuantileNorm._get_qc                 C   ó   |   |¡}|| j |¡ S )zÌ
        The robust criterion function for MQuantileNorm.

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

        Returns
        -------
        rho : ndarray
        )ro   rk   r   ©r   r   rn   r   r   r   r   ¬  s   
zMQuantileNorm.rhoc                 C   rp   )zñ
        The psi function for MQuantileNorm estimator.

        The analytic derivative of rho

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

        Returns
        -------
        psi : ndarray
        )ro   rk   r   rq   r   r   r   r   ¼  ó   
zMQuantileNorm.psic                 C   rp   )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
        )ro   rk   r   rq   r   r   r   r   Î  rr   zMQuantileNorm.weightsc                 C   rp   )a  
        The derivative of MQuantileNorm function

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

        Returns
        -------
        psi_deriv : ndarray

        Notes
        -----
        Used to estimate the robust covariance matrix.
        )ro   rk   r   rq   r   r   r   r   à  r&   zMQuantileNorm.psi_derivc                 C   r   r   r   r   r   r   r   r   ô  r   zMQuantileNorm.__call__N)r   r   r   r   r-   ro   r   r   r   r   r   r   r   r   r   ri   m  s    1	ri   é   çíµ ÷Æ°>c              	   C   sœ   |du rt ƒ }|du rt | |¡}n|}t|ƒD ]/}| | | | ¡}	t |	|  |¡t |	|¡ }
t t t ||
 ¡|| ¡¡rE|
  S |
}qt	d| ƒ‚)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lessr1   Ú
ValueError)r?   ZscaleZnormZaxisÚinitialÚmaxiterZtolZmur`   ÚWZnmur   r   r   Úestimate_locationû  s   ! ÿr|   )Nr   Nrs   rt   )Znumpyr!   r	   r
   r   r(   r<   rD   rJ   rQ   rb   ri   r|   r   r   r   r   Ú<module>   s     
KQn[xg 2h ÿ