
    !ga                         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y)    Nc                 6    | 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     U/var/www/dash_apps/app1/venv/lib/python3.12/site-packages/statsmodels/robust/norms.py_cabsr
      s$     FFaK1q D!8O    c                   .    e Zd ZdZd Zd Zd Zd Zd Zy)
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                 $    | j                  |      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      s     2"""
"r   r   c                   (    e Zd ZdZd Zd Zd Zd Zy)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    " zz!}r   c                     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*    " JJqMwwqww

++r   c                 ^    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     wwqww

++r   N)r!   r"   r#   r$   r   r   r   r   r%   r   r	   r'   r'   [   s    "&,(,r   r'   c                   6    e Zd ZdZd	dZd Zd Zd Zd Zd Z	y)
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
    c                     || _         y N)t)r   r6   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absr6   r   s     r	   _subsetzHuberT._subset   s.     JJqM}}RVVAY//r   c                     t        j                  |      }| 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;   r6   r   r   tests      r	   r   z
HuberT.rho   sh      JJqM||As
QT!TbffQi$&&03?BCD 	Er   c                     t        j                  |      }| 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<   r6   r   r>   s      r	   r   z
HuberT.psi   sG    $ JJqM||Aax1t8tvv-
:::r   c                     t        j                  |      }t        j                  |      }| 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;   r6   )r   r   
z_isscalarr?   abszvs         r	   r   zHuberT.weights   so    $ [[^
MM!||AvvayT
AH&--!Ar   c                     t        j                  t        j                  |      | j                        j	                  t
              S )z
        The derivative of Huber's t psi function

        Notes
        -----
        Used to estimate the robust covariance matrix.
        )r+   r:   r;   r6   astypefloatr   s     r	   r   zHuberT.psi_deriv  s,     }}RVVAY/66u==r   N)gQ?
r!   r"   r#   r$   r7   r<   r   r   r   r   r%   r   r	   r3   r3      s&    0E*;,<>r   r3   c                   0    e Zd ZdZddZd Zd Zd Zd Zy)	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
    c                     || _         y r5   ar   rP   s     r	   r7   zRamsayE.__init__)  r8   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,   exprP   r;   r   s     r	   r   zRamsayE.rho,  sj     JJqMBFFDFF7RVVAY./TVVbffQi'') ),0FFAI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\|)
        rS   r   s     r	   r   zRamsayE.psi>  s8      JJqM266466'BFF1I-...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\|)
        rS   r   s     r	   r   zRamsayE.weightsQ  s3    " JJqMvvtvvg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   )rP   r+   rT   r;   r   )r   r   rP   r   dxydys          r	   r   zRamsayE.psi_derive  s]     FFFFA2q	>"R!Vbggaj 2vBr   N)g333333?)	r!   r"   r#   r$   r7   r   r   r   r   r%   r   r	   rM   rM     s     6$/&+(r   rM   c                   6    e Zd ZdZd	dZd Zd Zd Zd Zd Z	y)

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                     || _         y r5   rO   rQ   s     r	   r7   zAndrewWave.__init__  r8   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;   rP   pir   s     r	   r<   zAndrewWave._subset  s6     JJqM}}RVVAY77r   c                     | j                   }t        j                  |      }| 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   )rP   r+   r,   r<   cosr   r   rP   r?   s       r	   r   zAndrewWave.rho  sg    ( FFJJqM||Aq!tq266!a%=01TQT!A%& 	'r   c                     | j                   }t        j                  |      }| 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
        )rP   r+   r,   r<   sinrb   s       r	   r   zAndrewWave.psi  sB    & FFJJqM||Aax"&&Q-''r   c                    | j                   }t        j                  |      }| j                  |      }||z  }t        j                  |      t        j
                  t        j                        j                  k  }t        j                  |      r@t        j                  |      }| }||   }||   t        j                  |      z  |z  ||<   |S |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
        )rP   r+   r,   r<   r;   finfodoubleepsany	ones_likerd   )r   r   rP   r?   ratiosmallr   larges           r	   r   zAndrewWave.weights  s    $ FFJJqM||AAu 3 7 7766%=ll5)GFE%LE!%[266%=85@GEN  RVVE]*U2Gr   c                 n    | 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+   ra   rP   r>   s      r	   r   zAndrewWave.psi_deriv  s-     ||AbffQZ(((r   N)gCl?rK   r%   r   r	   r\   r\   u  s&    8'4(0@
)r   r\   c                   6    e Zd ZdZd	dZd Zd Zd Zd Zd Z	y)
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                     || _         y r5   cr   rs   s     r	   r7   zTrimmedMean.__init__  r8   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;   rs   r   s     r	   r<   zTrimmedMean._subset  s.    
 JJqM}}RVVAY//r   c                     t        j                  |      }| 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<   rs   r>   s      r	   r   zTrimmedMean.rho  sL    " JJqM||Aad{S AH	#9C#???r   c                 X    t        j                  |      }| 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'    $ JJqM||Aaxr   c                 R    t        j                  |      }| 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
        ry   r>   s      r	   r   zTrimmedMean.weights2  s#    $ JJqM||Ar   c                 (    | j                  |      }|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)       @rK   r%   r   r	   rp   rp     s&    0@*,,	r   rp   c                   6    e Zd ZdZd	dZd Zd Zd Zd Zd Z	y)
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                 .    || _         || _        || _        y r5   )rP   brs   )r   rP   r   rs   s       r	   r7   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:   rP   r   greaterrs   )r   r   t1t2t3s        r	   r<   zHampel._subsetk  s     FF2::a=!]]1dff%]]1dff%

1dff(==]]1dff%

1dff(==2rzr   c                 <   | j                   | j                  | j                  }}}t        j                  |      }t        j
                  |      }| 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
        rJ   dtyper   r)   g      r   rP   r   rs   r+   rC   rD   r<   promote_typesr   zerosr/   r;   )r   r   rP   r   rs   rE   r   r   r   t34dtrG   s               r	   r   z
Hampel.rhou  s   ( &&$&&$&&a1[[^
MM!\\!_
BRjaggw/HHQWWB'FF1I"q3"QrUQTCZ'"Q2YN"a!e,5"	#!q1uqy/C''!Ar   c                 
   | j                   | j                  | j                  }}}t        j                  |      }t        j
                  |      }| 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
        rJ   r   r   rP   r   rs   r+   rC   rD   r<   r   r   r   r/   r   r;   )r   r   rP   r   rs   rE   r   r   r   r   rG   szas                r	   r   z
Hampel.psi  s    , &&$&&$&&a1[[^
MM!\\!_
Baggw/HHQWWB'GGAJVVAY""AbE	"AbE	QBZ(AE2"!Ar   c                    | j                   | j                  | j                  }}}t        j                  |      }t        j
                  |      }| 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
        rJ   r   rB   r   r   )r   r   rP   r   rs   rE   r   r   r   r   rG   abs_zabs_zt3s                r	   r   zHampel.weights  s    , &&$&&$&&a1[[^
MM!\\!_
Baggw/HHQWWB'"q	E"I")Q[!WA%67"!Ar   c                    | j                   | j                  | j                  }}}t        j                  |      }t        j
                  |      }| 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.
        rJ   r   rB   r   r   )r   r   rP   r   rs   rE   r   _r   r   dzt3s               r	   r   zHampel.psi_deriv  s     &&$&&$&&a1[[^
MM!LLO	Araggw/HHQWWB'"ebggcl"S()RVVC[AE-BC"!Ar   N)r|   g      @g       @rK   r%   r   r	   r~   r~   T  s(    "
'R'R'Rr   r~   c                   6    e Zd ZdZd	dZd Zd Zd Zd Zd Z	y)
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.
    c                     || _         y r5   rr   rt   s     r	   r7   zTukeyBiweight.__init__  r8   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:   rs   r   s     r	   r<   zTukeyBiweight._subset  s/     FF2::a=!}}Q''r   c                     | j                  |      }| 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<   rs   )r   r   subsetfactors       r	   r   zTukeyBiweight.rho  sP      aRa$&&j1_$q((61F:VCCr   c                     t        j                  |      }| 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   rw   r   r   r   s      r	   r   zTukeyBiweight.psi3  sD    & JJqMaATVVa'!++f44r   c                 Z    | j                  |      }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                     | j                  |      }|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`  sk     a!qx!m+a/adF46619,AdffHq=AB C 	Cr   N)g=
ףp@rK   r%   r   r	   r   r     s'    (D(5.1,
Cr   r   c                   :    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
y	)
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                      || _         || _        y r5   )q	base_norm)r   r   r   s      r	   r7   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  sF    1vEXXd^466z8H9	r   c                 `    | j                  |      }|| j                  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+     [[^DNN&&q)))r   c                 `    | j                  |      }|| j                  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+     [[^DNN&&q)))r   c                 `    | j                  |      }|| j                  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+     [[^DNN**1---r   c                 `    | j                  |      }|| j                  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+    " [[^DNN,,Q///r   c                 $    | j                  |      S r   r   r   s     r	   r   zMQuantileNorm.__call__  r    r   N)r!   r"   r#   r$   r7   r   r   r   r   r   r   r%   r   r	   r   r   m  s+    /b#* *$.$0(r   r   c           	         |
t               }|t        j                  | |      }n|}t        |      D ]  }|j	                  | |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
    z6location estimator failed to converge in %d iterations)
r3   r+   medianranger   sumalllessr;   
ValueError)rP   scalenormaxisinitialmaxitertolmur   Wnmus              r	   estimate_locationr     s    B |xYYq$7^ LL!B$&ffQqS$"&&D/166"''"&&c*ECK89JB M    r   )Nr   N   gư>)numpyr+   r
   r   r'   r3   rM   r\   rp   r~   r   r   r   r%   r   r	   <module>r      s    
H HVN,: N,bj>Z j>\Xj Xvt) t)pd* dNnZ nbeCJ eCPKJ K\ <@&-1 r   