
    M/Pha                        d dl Zd Z G d d          Z G d de          Z G d de          Z G d	 d
e          Z G d de          Z G d de          Z G d de          Z	 G d de          Z
 G d de          Z	 	 ddZdS )    Nc                 .    | j         dk    dz  dz
  }|| z  S )zabsolute 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signs     X/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/statsmodels/robust/norms.py_cabsr
      s"     FaK1q D!8O    c                   0    e Zd ZdZd Zd Zd Zd Z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                     t           )z|
        The robust criterion estimator function.

        Abstract method:

        -2 loglike used in M-estimator
        NotImplementedErrorselfzs     r	   rhozRobustNorm.rho*   
     "!r   c                     t           )z
        Derivative of rho.  Sometimes referred to as the influence function.

        Abstract method:

        psi = rho'
        r   r   s     r	   psizRobustNorm.psi4   r   r   c                     t           )z_
        Returns the value of psi(z) / z

        Abstract method:

        psi(z) / z
        r   r   s     r	   weightszRobustNorm.weights>   r   r   c                     t           )z
        Derivative of psi.  Used to obtain robust covariance matrix.

        See statsmodels.rlm for more information.

        Abstract method:

        psi_derive = psi'
        r   r   s     r	   	psi_derivzRobustNorm.psi_derivH   s
     "!r   c                 ,    |                      |          S zH
        Returns the value of estimator rho applied to an input
        r   r   s     r	   __call__zRobustNorm.__call__T        xx{{r   N)	__name__
__module____qualname____doc__r   r   r   r   r    r   r	   r   r      si         2" " "" " "" " "
" 
" 
"    r   r   c                   *    e Zd ZdZd Zd Zd Zd ZdS )LeastSquaresz
    Least squares rho for M-estimation and its derived functions.

    See Also
    --------
    statsmodels.robust.norms.RobustNorm
    c                     |dz  dz  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   s     r	   r   zLeastSquares.rhod   s     !tczr   c                 *    t          j        |          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   s     r	   r   zLeastSquares.psiu   s    " z!}}r   c                 r    t          j        |          }t          j        |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   s     r	   r   zLeastSquares.weights   s'    " JqMMwqw
+++r   c                 J    t          j        |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/   r0   r   s     r	   r   zLeastSquares.psi_deriv   s     wqw
+++r   N)r!   r"   r#   r$   r   r   r   r   r%   r   r	   r'   r'   [   sZ           "  &, , ,(, , , , ,r   r'   c                   8    e Zd ZdZd
dZd Zd Zd Zd Z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                     || _         d S N)t)r   r7   s     r	   __init__zHuberT.__init__       r   c                     t          j        |          }t          j        t          j        |          | j                  S )zE
        Huber's T is defined piecewise over the range for z
        )r+   r,   
less_equalabsr7   r   s     r	   _subsetzHuberT._subset   s-     JqMM}RVAYY///r   c                     t          j        |          }|                     |          }|dz  |dz  z  d|z
  t          j        |          | j        z  d| j        dz  z  z
  z  z   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<   r7   r   r   tests      r	   r   z
HuberT.rho   sf      JqMM||As
QT!TbfQii$&03?BCD 	Er   c                     t          j        |          }|                     |          }||z  d|z
  | j        z  t          j        |          z  z   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=   r7   r   r?   s      r	   r   z
HuberT.psi   sG    $ JqMM||Aax1t8tv-

:::r   c                     t          j        |          }t          j        |          }|                     |          }t          j        |          }d||<   |d|z
  | j        z  |z  z   }|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_1dr=   r<   r7   )r   r   
z_isscalarr@   abszvs         r	   r   zHuberT.weights   su    $ [^^
M!||AvayyT
AH&-- 	!Ar   c                     t          j        t          j        |          | j                                      t
                    S )z
        The derivative of Huber's t psi function

        Notes
        -----
        Used to estimate the robust covariance matrix.
        )r+   r;   r<   r7   astypefloatr   s     r	   r   zHuberT.psi_deriv  s.     }RVAYY//66u===r   N)r4   
r!   r"   r#   r$   r8   r=   r   r   r   r   r%   r   r	   r3   r3      s            0 0 0E E E*; ; ;,  <> > > > >r   r3   c                   2    e Zd ZdZd	dZd Zd Zd Z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                     || _         d S r6   ar   rR   s     r	   r8   zRamsayE.__init__)  r9   r   c                     t          j        |          }dt          j        | j         t          j        |          z            d| j        t          j        |          z  z   z  z
  | j        dz  z  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,   exprR   r<   r   s     r	   r   zRamsayE.rho,  se     JqMMBFDF7RVAYY.//TVbfQii'') ),0FAI6 	6r   c                     t          j        |          }|t          j        | j         t          j        |          z            z  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\|)
        rU   r   s     r	   r   zRamsayE.psi>  s7      JqMM2646'BF1II-....r   c                     t          j        |          }t          j        | j         t          j        |          z            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\|)
        rU   r   s     r	   r   zRamsayE.weightsQ  s2    " JqMMvtvgq		)***r   c                     | j         }t          j        | t          j        |          z            }| |z  t          j        |          z  }|}d}||z  ||z  z   S )z
        The derivative of Ramsay's Ea psi function.

        Notes
        -----
        Used to estimate the robust covariance matrix.
        r   )rR   r+   rV   r<   r   )r   r   rR   r   dxydys          r	   r   zRamsayE.psi_derive  s\     FFA2q		>""R!Vbgajj 2vBr   N)rO   )	r!   r"   r#   r$   r8   r   r   r   r   r%   r   r	   rN   rN     sn            6 6 6$/ / /&+ + +(    r   rN   c                   8    e Zd ZdZd
dZd Zd Zd Zd Z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                     || _         d S r6   rQ   rS   s     r	   r8   zAndrewWave.__init__  r9   r   c                     t          j        |          }t          j        t          j        |          | j        t           j        z            S )zI
        Andrew's wave is defined piecewise over the range of z.
        )r+   r,   r;   r<   rR   pir   s     r	   r=   zAndrewWave._subset  s3     JqMM}RVAYY777r   c                     | j         }t          j        |          }|                     |          }||dz  z  dt          j        ||z            z
  z  d|z
  |dz  z  dz  z   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   )rR   r+   r,   r=   cosr   r   rR   r@   s       r	   r   zAndrewWave.rho  sg    ( FJqMM||Aq!tq26!a%==01TQT!A%& 	'r   c                     | j         }t          j        |          }|                     |          }||z  t          j        ||z            z  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
        )rR   r+   r,   r=   sinre   s       r	   r   zAndrewWave.psi  sB    & FJqMM||Aax"&Q--''r   c                    | j         }t          j        |          }|                     |          }||z  }t          j        |          t          j        t          j                  j        k     }t          j        |          rCt          j	        |          }| }||         }||         t          j
        |          z  |z  ||<   n|t          j
        |          z  |z  }|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
        )rR   r+   r,   r=   r<   finfodoubleepsany	ones_likerg   )r   r   rR   r@   ratiosmallr   larges           r	   r   zAndrewWave.weights  s    $ FJqMM||AAu 3 3 776%== 	3l5))GFE%LE!%[26%==85@GENNRVE]]*U2Gr   c                 j    |                      |          }|t          j        || j        z            z  S )z
        The derivative of Andrew's wave psi function

        Notes
        -----
        Used to estimate the robust covariance matrix.
        )r=   r+   rd   rR   r?   s      r	   r   zAndrewWave.psi_deriv  s.     ||AbfQZ((((r   N)r_   rL   r%   r   r	   r^   r^   u  s~            8 8 8' ' '4( ( (0  @
) 
) 
) 
) 
)r   r^   c                   8    e Zd ZdZd
dZd Zd Zd Zd Z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                     || _         d S r6   cr   rw   s     r	   r8   zTrimmedMean.__init__  r9   r   c                     t          j        |          }t          j        t          j        |          | j                  S )zN
        Least trimmed mean is defined piecewise over the range of z.
        )r+   r,   r;   r<   rw   r   s     r	   r=   zTrimmedMean._subset  s-    
 JqMM}RVAYY///r   c                     t          j        |          }|                     |          }||dz  z  dz  d|z
  | j        dz  z  dz  z   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=   rw   r?   s      r	   r   zTrimmedMean.rho  sL    " JqMM||Aad{S AH	#9C#???r   c                 ^    t          j        |          }|                     |          }||z  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?   s      r	   r   zTrimmedMean.psi  s)    $ JqMM||Aaxr   c                 X    t          j        |          }|                     |          }|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
        r}   r?   s      r	   r   zTrimmedMean.weights2  s%    $ JqMM||Ar   c                 0    |                      |          }|S )z
        The derivative of least trimmed mean psi function

        Notes
        -----
        Used to estimate the robust covariance matrix.
        )r=   r?   s      r	   r   zTrimmedMean.psi_derivH  s     ||Ar   N)rt   rL   r%   r   r	   rs   rs     s            0 0 0@ @ @*  ,  ,	 	 	 	 	r   rs   c                   8    e Zd ZdZddZd Zd Zd Zd	 Z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
    rt         @       @c                 0    || _         || _        || _        d S r6   )rR   brw   )r   rR   r   rw   s       r	   r8   zHampel.__init__f  s    r   c                 `   t          j        t          j        |                    }t          j        || j                  }t          j        || j                  t          j        || j                  z  }t          j        || j                  t          j        || j                  z  }|||fS )zL
        Hampel's function is defined piecewise over the range of z
        )r+   r<   r,   r;   rR   r   greaterrw   )r   r   t1t2t3s        r	   r=   zHampel._subsetk  s     F2:a==!!]1df%%]1df%%
1df(=(==]1df%%
1df(=(==2rzr   c                 &   | j         | j        | j        }}}t          j        |          }t          j        |          }|                     |          \  }}}||z   }	t          j        |j        d          }
t          j	        |j
        |
          }t          j        |          }||         dz  dz  ||<   |||         z  |dz  dz  z
  ||<   ||||         z
  dz  z  ||z
  z  dz  ||<   ||	xx         |||z   |z
  z  dz  z  cc<   |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
        rK   dtyper   r)   g      r   rR   r   rw   r+   rD   rE   r=   promote_typesr   zerosr/   r<   )r   r   rR   r   rw   rF   r   r   r   t34dtrH   s               r	   r   z
Hampel.rhou  s"   ( &$&$&a1[^^
M!\\!__
BRjagw//HQWB'''F1II"q3"QrUQTCZ'"Q2YN"a!e,5"	#!q1uqy/C'' 	!Ar   c                    | j         | j        | j        }}}t          j        |          }t          j        |          }|                     |          \  }}}t          j        |j        d          }	t          j	        |j
        |	          }
t          j        |          }t          j        |          }||         |
|<   |||         z  |
|<   |||         z  |||         z
  z  ||z
  z  |
|<   |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
        rK   r   r   rR   r   rw   r+   rD   rE   r=   r   r   r   r/   r   r<   )r   r   rR   r   rw   rF   r   r   r   r   rH   szas                r	   r   z
Hampel.psi  s    , &$&$&a1[^^
M!\\!__
Bagw//HQWB'''GAJJVAYY""AbE	"AbE	QBZ(AE2" 	!Ar   c                    | j         | j        | j        }}}t          j        |          }t          j        |          }|                     |          \  }}}t          j        |j        d          }	t          j	        |j
        |	          }
d|
|<   t          j        |          }|||         z  |
|<   ||         }|||z
  z  |||z
  z  z  |
|<   |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
        rK   r   rC   r   r   )r   r   rR   r   rw   rF   r   r   r   r   rH   abs_zabs_zt3s                r	   r   zHampel.weights  s    , &$&$&a1[^^
M!\\!__
Bagw//HQWB'''"q		E"I")Q[!WA%67" 	!Ar   c                    | j         | j        | j        }}}t          j        |          }t          j        |          }|                     |          \  }}}t          j        |j        d          }	t          j	        |j
        |	          }
d|
|<   ||         }|t          j        |          z  |z   t          j        |          ||z
  z  z  |
|<   |r|
d         }
|
S )zGDerivative of psi function, second derivative of rho function.
        rK   r   rC   r   r   )r   r   rR   r   rw   rF   r   _r   r   dzt3s               r	   r   zHampel.psi_deriv  s     &$&$&a1[^^
M!LLOO	Aragw//HQWB'''"ebgcll"S()RVC[[AE-BC" 	!Ar   N)rt   r   r   rL   r%   r   r	   r   r   T  s         "   
  ' ' 'R' ' 'R' ' 'R    r   r   c                   8    e Zd ZdZd
dZd Zd Zd Zd Z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                     || _         d S r6   rv   rx   s     r	   r8   zTukeyBiweight.__init__  r9   r   c                     t          j        t          j        |                    }t          j        || j                  S )zK
        Tukey's biweight is defined piecewise over the range of z
        )r+   r<   r,   r;   rw   r   s     r	   r=   zTukeyBiweight._subset  s/     F2:a==!!}Q'''r   c                     |                      |          }| j        dz  dz  }d|| j        z  dz  z
  dz   |z  |z  |z   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=   rw   )r   r   subsetfactors       r	   r   zTukeyBiweight.rho  sN      aRa$&j1_$q((61F:VCCr   c                     t          j        |          }|                     |          }|d|| j        z  dz  z
  dz  z  |z  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   r{   r   r   r   s      r	   r   zTukeyBiweight.psi3  sD    & JqMMaATVa'!++f44r   c                 X    |                      |          }d|| j        z  dz  z
  dz  |z  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   r   s      r	   r   zTukeyBiweight.weightsJ  s2    & aQZ!O#a'&00r   c                     |                      |          }|d|| j        z  dz  z
  dz  d|dz  z  | j        dz  z  d|| j        z  dz  z
  z  z
  z  S )z
        The derivative of Tukey's biweight psi function

        Notes
        -----
        Used to estimate the robust covariance matrix.
        r   r      r   r   s      r	   r   zTukeyBiweight.psi_deriv`  sg     a!qx!m+a/adF4619,AdfHq=AB C 	Cr   N)r   rL   r%   r   r	   r   r     s            ( ( (D D D(5 5 5.1 1 1,
C 
C 
C 
C 
Cr   r   c                   <    e Zd ZdZd Zd Zd Zd Zd Zd Z	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                 "    || _         || _        d S r6   )q	base_norm)r   r   r   s      r	   r8   zMQuantileNorm.__init__  s    "r   c                     t          |          }|dk     }t          j        |          }d| j        z
  ||<   | j        || <   |S )Nr   r   )lenr+   emptyr   )r   r   nobsmask_negqqs        r	   _get_qzMQuantileNorm._get_q  sD    1vvEXd^^46z8H9	r   c                 f    |                      |          }|| j                            |          z  S )z
        The robust criterion function for MQuantileNorm.

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

        Returns
        -------
        rho : ndarray
        )r   r   r   r   r   r   s      r	   r   zMQuantileNorm.rho  s.     [[^^DN&&q))))r   c                 f    |                      |          }|| j                            |          z  S )z
        The psi function for MQuantileNorm estimator.

        The analytic derivative of rho

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

        Returns
        -------
        psi : ndarray
        )r   r   r   r   s      r	   r   zMQuantileNorm.psi  s.     [[^^DN&&q))))r   c                 f    |                      |          }|| j                            |          z  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
        )r   r   r   r   s      r	   r   zMQuantileNorm.weights  s.     [[^^DN**1----r   c                 f    |                      |          }|| j                            |          z  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.
        )r   r   r   r   s      r	   r   zMQuantileNorm.psi_deriv  s.    " [[^^DN,,Q////r   c                 ,    |                      |          S r   r   r   s     r	   r   zMQuantileNorm.__call__  r    r   N)r!   r"   r#   r$   r8   r   r   r   r   r   r   r%   r   r	   r   r   m  s        / /b# # #  * * * * * *$. . .$0 0 0(    r   r      ư>c           	         |t                      }|t          j        | |          }n|}t          |          D ]}|                    | |z
  |z            }	t          j        |	| z  |          t          j        |	|          z  }
t          j        t          j        t          j        ||
z
            ||z                      r|
c S |
}t          d|z            )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)
r3   r+   medianranger   sumalllessr<   
ValueError)rR   scalenormaxisinitialmaxitertolmur   Wnmus              r	   estimate_locationr     s    B |xxYq$7^^  LL!B$&&fQqS$"&D//16"'"&c**ECK8899 	JJJBB
M      r   )Nr   Nr   r   )numpyr+   r
   r   r'   r3   rN   r^   rs   r   r   r   r   r%   r   r	   <module>r      s      
  H H H H H H H HVN, N, N, N, N,: N, N, N,bj> j> j> j> j>Z j> j> j>\X X X X Xj X X Xvt) t) t) t) t) t) t) t)pd d d d d* d d dNn n n n nZ n n nbeC eC eC eC eCJ eC eC eCPK K K K KJ K K K\ <@&-1  1  1  1  1  1 r   