Ë
    £�Dj¾e  ã                   óâ   — 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 )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Úsigns     ú\C:\Crop_Prediction\Backend\crop-ai-system\venv\Lib\site-packages\statsmodels/robust/norms.pyÚ_cabsr
      s$   € ð �F‰F�a‰K˜1Ñ˜qÑ €DØ�!‰8€Oó    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   ó   € ð �x‰x˜‹{Ð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   € ð �!‰t�c‰zÐ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‰z˜!‹}Ð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*   € ô" �J‰J�q‹MˆÜ�w‰w�q—w‘w¤§
¡
Ó+Ð+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   € ô �w‰w�q—w‘w¤§
¡
Ó+Ð+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.   € ô �J‰J�q‹MˆÜ�}‰}œRŸV™V A›Y¨¯©Ó/Ð/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   € ô  �J‰J�q‹MˆØ�|‰|˜A‹ˆØ�s‘
˜Q ™TÑ!Ø�T‘œbŸf™f Q›i¨$¯&©&Ñ0°3¸¿¹À¹±?Ñ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   € ô$ �J‰J�q‹MˆØ�|‰|˜A‹ˆØ�a‰x˜1˜t™8 t§v¡vÑ-´·±¸³
Ñ:Ñ:Ð: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   € ô$ —[‘[ “^ˆ
Ü�M‰M˜!Óˆà�|‰|˜A‹ˆÜ�v‰v�a‹yˆØˆˆ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,   € ô �}‰}œRŸV™V A›Y¨¯©Ó/×6Ñ6´uÓ=Ð=r   N)g…ëQ¸…õ?©
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   € ô �J‰J�q‹MˆØ”B—F‘F˜DŸF™F˜7¤R§V¡V¨A£YÑ.Ó/Ø�T—V‘VœbŸf™f Q›iÑ'Ñ'ñ)ñ )Ø,0¯F©F°A©Iñ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   € ô  �J‰J�q‹MˆØ”2—6‘6˜4Ÿ6™6˜'¤B§F¡F¨1£IÑ-Ó.Ñ.Ð.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   € ô" �J‰J�q‹MˆÜ�v‰v�t—v‘v�g¤§¡ 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]   € ð �F‰FˆÜ�F‰F�A�2œŸ™˜q›	‘>Ó"ˆØˆR�!‰V”b—g‘g˜a“jÑ ˆØˆØˆØ�2‰v˜˜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   € ô �J‰J�q‹MˆÜ�}‰}œRŸV™V A›Y¨¯©´·±©Ó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   € ð( �F‰FˆÜ�J‰J�q‹MˆØ�|‰|˜A‹ˆØ�q˜!‘t‘˜q¤2§6¡6¨!¨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   € ð& �F‰FˆÜ�J‰J�q‹MˆØ�|‰|˜A‹ˆØ�a‰xœ"Ÿ&™&  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Â   € ð$ �F‰FˆÜ�J‰J�q‹MˆØ�|‰|˜A‹ˆØ�A‘ˆÜ—‘�u“¤§¡¬¯©Ó 3× 7Ñ 7Ñ7ˆÜ�6‰6�%Œ=Ü—l‘l 5Ó)ˆGØ�FˆEØ˜%‘LˆEØ! %™[¬2¯6©6°%«=Ñ8¸5Ñ@ˆG�E‰Nð ˆð œRŸV™V E›]Ñ*¨UÑ2ˆGØˆ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‹ˆØ”b—f‘f˜Q §¡™ZÓ(Ñ(Ð(r   N)g�•C‹lõ?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.   € ô
 �J‰J�q‹MˆÜ�}‰}œRŸV™V A›Y¨¯©Ó/Ð/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   € ô" �J‰J�q‹MˆØ�|‰|˜A‹ˆØ�a˜‘d‰{˜SÑ  A¨¡H°·±¸±	Ñ#9¸CÑ#?Ñ?Ð?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'   € ô$ �J‰J�q‹MˆØ�|‰|˜A‹ˆØ�a‰xˆ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#   € ô$ �J‰J�q‹MˆØ�|‰|˜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Š   € ô �F‰F”2—:‘:˜a“=Ó!ˆÜ�]‰]˜1˜dŸf™fÓ%ˆÜ�]‰]˜1˜dŸf™fÓ%¬¯
©
°1°d·f±fÓ(=Ñ=ˆÜ�]‰]˜1˜dŸf™fÓ%¬¯
©
°1°d·f±fÓ(=Ñ=ˆØ�2�rˆzÐ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  € ð( —&‘&˜$Ÿ&™& $§&¡&ˆaˆ1ˆä—[‘[ “^ˆ
Ü�M‰M˜!Óˆà—\‘\ !“_‰
ˆˆB�Ø�R‘ˆjˆÜ×Ñ˜aŸg™g wÓ/ˆÜ�H‰H�Q—W‘W BÔ'ˆÜ�F‰F�1‹IˆØ�"‘�q‘˜3‘ˆˆ"‰à�Q�r‘U‘˜Q ™T C™ZÑ'ˆˆ"‰Ø�Q˜˜2™‘Y ‘NÑ" a¨!¡eÑ,°Ñ5ˆˆ"‰Ø	ˆ#‹�!�q˜1‘u˜q‘y‘/ 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å   € ð, —&‘&˜$Ÿ&™& $§&¡&ˆaˆ1ˆä—[‘[ “^ˆ
Ü�M‰M˜!Óˆà—\‘\ !“_‰
ˆˆB�Ü×Ñ˜aŸg™g wÓ/ˆÜ�H‰H�Q—W‘W BÔ'ˆÜ�G‰G�A‹JˆÜ�V‰V�A‹Yˆà�"‘ˆˆ"‰Ø�A�b‘E‘	ˆˆ"‰Ø�A�b‘E‘	˜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Ö   € ð, —&‘&˜$Ÿ&™& $§&¡&ˆaˆ1ˆä—[‘[ “^ˆ
Ü�M‰M˜!Óˆà—\‘\ !“_‰
ˆˆB�ä×Ñ˜aŸg™g wÓ/ˆÜ�H‰H�Q—W‘W BÔ'ˆØˆˆ"‰Ü—‘�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Ì   € ð —&‘&˜$Ÿ&™& $§&¡&ˆaˆ1ˆä—[‘[ “^ˆ
Ü�M‰M˜!Óˆà—L‘L “O‰	ˆˆAˆrä×Ñ˜aŸg™g wÓ/ˆÜ�H‰H�Q—W‘W BÔ'ˆØˆˆ"‰Ø�‰eˆØ”b—g‘g˜c“lÑ" SÑ(Ð)¬R¯V©V°C«[¸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/   € ô �F‰F”2—:‘:˜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˜$Ÿ&™&‘j 1‘_Ñ$ qÑ(Ð(¨6Ñ1°FÑ:¸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   € ô& �J‰J�q‹MˆØ—‘˜a“ˆØ�A˜˜TŸV™V™ 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 ™d™F 4§6¡6¨1¡9Ñ,°°A°d·f±f±H¸q±=±Ñ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   € ä�1‹vˆØ˜‘EˆÜ�X‰X�d‹^ˆØ˜4Ÿ6™6‘zˆˆ8‰ØŸ™ˆˆHˆ9‰Øˆ	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+   € ð �[‰[˜‹^ˆØ�D—N‘N×&Ñ& 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+   € ð �[‰[˜‹^ˆØ�D—N‘N×&Ñ& 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+   € ð �[‰[˜‹^ˆØ�D—N‘N×*Ñ*¨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+   € ð" �[‰[˜‹^ˆØ�D—N‘N×,Ñ,¨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ˆà€Ü�Y‰Y�q˜$Ó‰àˆä�7‹^ò ˆØ�L‰L˜!˜B™$ ™Ó&ˆÜ�f‰f�Q�q‘S˜$Ó¤"§&¡&¨¨D£/Ñ1ˆÜ�6‰6”"—'‘'œ"Ÿ&™&  c¡Ó*¨E°C©KÓ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   