Ë
    ¢�Djó   ã                   ó8   — d dl Zd dlmZ d dlmZ  G d„ d«      Zy)é    N)Úmad)Úminimize_scalarc                   óH   — e Zd ZdZdd„Zdd„Zdd„Zdddd	ifd
„Zdd	ifd„Zy)ÚBoxCoxz<
    Mixin class to allow for a Box-Cox transformation.
    Nc                 ó>  — t        j                  |«      }t        j                  |dk  «      rt        d«      ‚|€ | j                  |fd|i|¤Ž}t        j
                  |d«      rt        j                  |«      }||fS t        j                  ||«      dz
  |z  }||fS )a„  
        Performs a Box-Cox transformation on the data array x. If lmbda is None,
        the indicated method is used to estimate a suitable lambda parameter.

        Parameters
        ----------
        x : array_like
        lmbda : float
            The lambda parameter for the Box-Cox transform. If None, a value
            will be estimated by means of the specified method.
        method : {'guerrero', 'loglik'}
            The method to estimate the lambda parameter. Will only be used if
            lmbda is None, and defaults to 'guerrero', detailed in Guerrero
            (1993). 'loglik' maximizes the profile likelihood.
        **kwargs
            Options for the specified method.
            * For 'guerrero', this entails window_length, the grouping
              parameter, scale, the dispersion measure, and options, to be
              passed to the optimizer.
            * For 'loglik': options, to be passed to the optimizer.

        Returns
        -------
        y : array_like
            The transformed series.
        lmbda : float
            The lmbda parameter used to transform the series.

        References
        ----------
        Guerrero, Victor M. 1993. "Time-series analysis supported by power
        transformations". `Journal of Forecasting`. 12 (1): 37-48.

        Guerrero, Victor M. and Perera, Rafael. 2004. "Variance Stabilizing
        Power Transformation for Time Series," `Journal of Modern Applied
        Statistical Methods`. 3 (2): 357-369.

        Box, G. E. P., and D. R. Cox. 1964. "An Analysis of Transformations".
        `Journal of the Royal Statistical Society`. 26 (2): 211-252.
        r   zNon-positive x.Úmethodç        ç      ð?)ÚnpÚasarrayÚanyÚ
ValueErrorÚ_est_lambdaÚiscloseÚlogÚpower)ÚselfÚxÚlmbdar   ÚkwargsÚys         ú^C:\Crop_Prediction\Backend\crop-ai-system\venv\Lib\site-packages\statsmodels/base/transform.pyÚtransform_boxcoxzBoxCox.transform_boxcox   s£   € ôR �J‰J�q‹Mˆä�6‰6�!�q‘&Œ>ÜÐ.Ó/Ð/àˆ=Ø$�D×$Ñ$ Qñ /Ø,2ð/à'-ñ/ˆEô
 �:‰:�e˜RÔ Ü—‘�q“	ˆAð �%ˆxˆô —‘˜!˜UÓ# bÑ(¨EÑ1ˆAà�%ˆxˆó    c                 ó  — |j                  «       }t        j                  |«      }|dk(  rNt        j                  |d«      rt        j                  |«      }|S t        j
                  ||z  dz   d|z  «      }|S t        d|› d�«      ‚)a  
        Back-transforms the Box-Cox transformed data array, by means of the
        indicated method. The provided argument lmbda should be the lambda
        parameter that was used to initially transform the data.

        Parameters
        ----------
        x : array_like
            The transformed series.
        lmbda : float
            The lambda parameter that was used to transform the series.
        method : {'naive'}
            Indicates the method to be used in the untransformation. Defaults
            to 'naive', which reverses the transformation.

            NOTE: 'naive' is implemented natively, while other methods may be
            available in subclasses!

        Returns
        -------
        y : array_like
            The untransformed series.
        Únaiver	   é   r
   úMethod 'ú' not understood.)Úlowerr   r   r   Úexpr   r   )r   r   r   r   r   s        r   Úuntransform_boxcoxzBoxCox.untransform_boxcoxF   s‚   € ð0 —‘“ˆÜ�J‰J�q‹Mˆà�WÒÜ�z‰z˜% Ô$Ü—F‘F˜1“I�ð ˆô	 —H‘H˜U Q™Y¨™]¨B°©JÓ7�ð ˆô ˜x¨ xÐ/@ÐAÓBÐBr   c                 ó>  — |j                  «       }t        |«      dk7  r#t        dj                  t        |«      «      «      ‚|d   |d   k\  rt        d«      ‚|dk(  r | j                  |fd|i|¤Ž}|S |dk(  r | j
                  |fd|i|¤Ž}|S t        d	|› d
�«      ‚)aÎ  
        Computes an estimate for the lambda parameter in the Box-Cox
        transformation using method.

        Parameters
        ----------
        x : array_like
            The untransformed data.
        bounds : tuple
            Numeric 2-tuple, that indicate the solution space for the lambda
            parameter. Default (-1, 2).
        method : {'guerrero', 'loglik'}
            The method by which to estimate lambda. Defaults to 'guerrero', but
            the profile likelihood ('loglik') is also available.
        **kwargs
            Options for the specified method.
            * For 'guerrero': window_length (int), the seasonality/grouping
              parameter. Scale ({'mad', 'sd'}), the dispersion measure. Options
              (dict), to be passed to the optimizer.
            * For 'loglik': Options (dict), to be passed to the optimizer.

        Returns
        -------
        lmbda : float
            The lambda parameter.
        é   z#Bounds of length {} not understood.r   r   z Lower bound exceeds upper bound.ÚguerreroÚboundsÚloglikr   r   )r    Úlenr   ÚformatÚ_guerrero_cvÚ_loglik_boxcox)r   r   r&   r   r   r   s         r   r   zBoxCox._est_lambdak   sÄ   € ð6 —‘“ˆäˆv‹;˜!ÒÜÐBß$™f¤S¨£[Ó1ó3ð 3à�A‰Y˜& ™)Ò#ÜÐ?Ó@Ð@à�ZÒØ%�D×%Ñ% aÑA°ÐA¸&ÑAˆEð ˆð �xÒØ'�D×'Ñ'¨ÑC°&ÐC¸FÑCˆEð ˆô ˜x¨ xÐ/@ÐAÓBÐBr   é   ÚsdÚmaxiteré   c                 óˆ  ‡‡— t        |«      }t        ||z  «      }t        j                  ||||z  z
  | ||f«      }t        j                  |d«      Š|j                  «       }|dk(  rt        j                  |dd¬«      Šn"|dk(  rt        |d¬«      Šnt        d|› d�«      ‚ˆˆfd„}	t        |	|d	|¬
«      }
|
j                  S )aß  
        Computes lambda using guerrero's coefficient of variation. If no
        seasonality is present in the data, window_length is set to 4 (as
        per Guerrero and Perera, (2004)).

        NOTE: Seasonality-specific auxiliaries *should* provide their own
        seasonality parameter.

        Parameters
        ----------
        x : array_like
        bounds : tuple
            Numeric 2-tuple, that indicate the solution space for the lambda
            parameter.
        window_length : int
            Seasonality/grouping parameter. Default 4, as per Guerrero and
            Perera (2004). NOTE: this indicates the length of the individual
            groups, not the total number of groups!
        scale : {'sd', 'mad'}
            The dispersion measure to be used. 'sd' indicates the sample
            standard deviation, but the more robust 'mad' is also available.
        options : dict
            The options (as a dict) to be passed to the optimizer.
        r   r-   ©Úddofr   )ÚaxiszScale 'r   c                 ó¸   •— t        j                  ‰t        j                  ‰d| z
  «      «      }t        j                  |d¬«      t        j                  |«      z  S )Nr   r1   )r   Údivider   ÚstdÚmean)r   ÚratÚ
dispersionr7   s     €€r   Úoptimz"BoxCox._guerrero_cv.<locals>.optimÁ   s@   ø€ Ü—)‘)˜J¬¯©°°q¸5±yÓ(AÓBˆCÜ—6‘6˜# AÔ&¬¯©°«Ñ5Ð5r   Úbounded©r&   r   Úoptions)r(   Úintr   Úreshaper7   r    r6   r   r   r   r   )r   r   r&   Úwindow_lengthÚscaler=   ÚnobsÚgroupsÚgrouped_datar:   Úresr9   r7   s              @@r   r*   zBoxCox._guerrero_cv—   sË   ù€ ô4 �1‹vˆÜ�T˜MÑ)Ó*ˆô —z‘z ! D¨F°]Ñ,BÑ$CÀTÐ"JØ#)¨=Ð"9ó;ˆä�w‰w�| QÓ'ˆà—‘“ˆØ�DŠ=ÜŸ™ ¨a°aÔ8‰JØ�eŠ^Ü˜\°Ô2‰Jä˜w u gÐ->Ð?Ó@Ð@õ	6ô ˜eØ%+Ø%.Ø&-ô/ˆð �u‰uˆr   c                 ó¶   ‡ ‡‡‡— t        j                  t        j                  ‰«      «      Št        ‰«      Šˆˆ ˆˆfd„}t	        ||d|¬«      }|j
                  S )a~  
        Taken from the Stata manual on Box-Cox regressions, where this is the
        special case of 'lhs only'. As an estimator for the variance, the
        sample variance is used, by means of the well-known formula.

        Parameters
        ----------
        x : array_like
        options : dict
            The options (as a dict) to be passed to the optimizer.
        c                 óœ   •— ‰j                  ‰| «      \  }} d| z
  ‰z  ‰dz  t        j                  t        j                  |«      «      z  z   S )Nr   g       @)r   r   r   Úvar)r   r   rB   r   Úsum_xr   s     €€€€r   r:   z$BoxCox._loglik_boxcox.<locals>.optimÚ   sG   ø€ Ø×,Ñ,¨Q°Ó6‰HˆAˆuØ˜‘I Ñ&¨$°©)´r·v±v¼b¿f¹fÀQ»iÓ7HÑ)HÑHÐHr   r;   r<   )r   Úsumr   r(   r   r   )r   r   r&   r=   r:   rE   rB   rI   s   ``    @@r   r+   zBoxCox._loglik_boxcoxË   sL   û€ ô —‘”r—v‘v˜a“yÓ!ˆÜ�1‹vˆ÷	Iô ˜eØ%+Ø%.Ø&-ô/ˆð �u‰uˆr   )Nr%   )r   ))éÿÿÿÿr$   r%   )	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r"   r   r*   r+   © r   r   r   r      s;   „ ñó9óv#óJ*ðX 56¸TØ'¨˜_ó2ðh 2;¸B°ô r   r   )Únumpyr   Ústatsmodels.robustr   Úscipy.optimizer   r   rP   r   r   ú<module>rT      s   ðÛ Ý "Ý *÷\ò \r   