Ë
    ý�Dj;#  ã                   ól   — d dl ZddlmZ ddlmZ ddlmZ ddlmZ d	gZ	d
„ Z
d„ Zd„ Z G d„ d	ee«      Zy)é    Né   )ÚBaseExogFeaturizeré   )ÚUpdatableMixiné   )Úcheck_is_fitted)ÚC_fourier_termsÚFourierFeaturizerc                 óN   — t        j                  t         j                  | z  «      S ©N)ÚnpÚsinÚpi©Úxs    úgC:\Crop_Prediction\Backend\crop-ai-system\venv\Lib\site-packages\pmdarima/preprocessing/exog/fourier.pyú<lambda>r      ó   € ”2—6‘6œ"Ÿ%™% !™)Ó$€ ó    c                 óN   — t        j                  t         j                  | z  «      S r   )r   Úcosr   r   s    r   r   r      r   r   c                 óX   — t        | |«      }t        j                  |«      j                  S r   )r	   r   ÚasarrayÚT)ÚpÚtimesÚXs      r   Ú_fourier_termsr      s"   € ô
 	˜˜5Ó!€AÜ�:‰:�a‹=�?‰?Ðr   c                   óH   ‡ — e Zd ZdZdˆ fd„	Zd„ Zd„ Zd	d„Zd
d„Zd	d„Z	ˆ xZ
S )r
   aÕ
  Fourier terms for modeling seasonality

    This transformer creates an exogenous matrix containing terms from a
    Fourier series, up to order ``k``. It is based on ``R::forecast code`` [1].
    In practice, it permits us to fit a seasonal time series *without* seasonal
    order (i.e., ``seasonal=False``) by supplying decomposed seasonal Fourier
    terms as an exogenous array.

    The advantages of this technique, per Hyndman [2]:

        * It allows any length seasonality
        * The seasonal pattern is smooth for small values of K (but more wiggly
          seasonality can be handled by increasing K)
        * The short-term dynamics are easily handled with a simple ARMA error

    The disadvantage is that the seasonal periodicity of the time series is
    assumed to be fixed.

    Functionally, this is a featurizer. This means that exogenous features are
    *derived* from ``y``, as opposed to transforming an existing exog array.
    It also behaves slightly differently in the :func:`transform` stage than
    most other exogenous transformers in that ``exog`` is not a required arg,
    and it takes ``**kwargs``. See the :func:`transform` docstr for more info.

    Parameters
    ----------
    m : int
        The seasonal periodicity of the endogenous vector, y.

    k : int, optional (default=None)
        The number of sine and cosine terms (each) to include. I.e., if ``k``
        is 2, 4 new features will be generated. ``k`` must not exceed ``m/2``,
        which is the default value if not set. The value of ``k`` can be
        selected by minimizing the AIC.

    prefix : str or None, optional (default=None)
        The feature prefix

    Examples
    --------
    >>> import pandas as pd
    >>> from pmdarima.preprocessing import FourierFeaturizer
    >>> from pmdarima.datasets import load_wineind
    >>> y = load_wineind()
    >>> trans = FourierFeaturizer(12, 4)
    >>> y_prime, X = trans.fit_transform(y)
    >>> X.head()
       FOURIER_S12-0     FOURIER_C12-0    ...     FOURIER_S12-3  FOURIER_C12-3
    0       0.500000      8.660254e-01    ...      8.660254e-01           -0.5
    1       0.866025      5.000000e-01    ...     -8.660255e-01           -0.5
    2       1.000000     -4.371139e-08    ...      1.748456e-07            1.0
    3       0.866025     -5.000001e-01    ...      8.660253e-01           -0.5
    4       0.500000     -8.660254e-01    ...     -8.660255e-01           -0.5

    Notes
    -----
    * Helpful for long seasonal periods (large ``m``) where ``seasonal=True``
      seems to take a very long time to fit a model.

    References
    ----------
    .. [1] https://github.com/robjhyndman/forecast/blob/master/R/season.R
    .. [2] https://robjhyndman.com/hyndsight/longseasonality/
    c                 ó@   •— || _         || _        t        ‰| �  |«       y r   )ÚmÚkÚsuperÚ__init__)Úselfr!   r"   ÚprefixÚ	__class__s       €r   r$   zFourierFeaturizer.__init__\   s   ø€ ØˆŒØˆŒä‰Ñ˜Õ r   c                 ó&   — | j                   }|€d}|S )NÚFOURIER)r&   )r%   Úpfxs     r   Ú_get_prefixzFourierFeaturizer._get_prefixb   s   € Ø�k‰kˆØˆ;ØˆCØˆ
r   c           	      ó´   — | j                  «       }t        |j                  d   «      D �cg c]"  }d||dz  dk(  rdnd| j                  |dz  fz  ‘Œ$ c}S c c}w )Nr   z
%s_%s%i-%ir   r   ÚSÚC)r+   ÚrangeÚshaper!   )r%   r   r*   Úis       r   Ú_get_feature_namesz$FourierFeaturizer._get_feature_namesh   sk   € Ø×ÑÓ ˆô ˜1Ÿ7™7 1™:Ó&ö(ð ð ØØ˜1‘u ’z‘ sØ—‘Ø�Q‘ð	ó ò(ð 	(ùò (s   «'Ac                 óV  — | j                  ||d¬«      \  }}| j                  }| j                  }|€|dz  }d|z  |kD  s|dk  rt        d«      ‚t	        j
                  |«      dz   |z  j                  t        j                  «      }|| _        || _	        |j                  d   | _        | S )a›  Fit the transformer

        Computes the periods of all the Fourier terms. The values of ``y`` are
        not actually used; only the periodicity is used when computing Fourier
        terms.

        Parameters
        ----------
        y : array-like or None, shape=(n_samples,)
            The endogenous (time-series) array.

        X : array-like or None, shape=(n_samples, n_features), optional
            The exogenous array of additional covariates. If specified, the
            Fourier terms will be column-bound on the right side of the matrix.
            Otherwise, the Fourier terms will be returned as the new exogenous
            array.
        T©Únull_allowedr   r   z2k must be a positive integer not greater than m//2r   )Ú
_check_y_Xr!   r"   Ú
ValueErrorr   ÚarangeÚastypeÚfloat64Úp_Úk_r0   Ún_)r%   Úyr   Ú_r!   r"   r   s          r   ÚfitzFourierFeaturizer.fits   s«   € ð& �‰˜q !°$ˆÓ7‰ˆˆ1à�F‰FˆØ�F‰FˆØˆ9Ø�Q‘ˆAØˆq‰5�1Š9˜˜AšÜð )ó *ð *ô
 �i‰i˜‹l˜QÑ !Ñ#×+Ñ+¬B¯J©JÓ7ˆð
 ˆŒØˆŒØ—'‘'˜!‘*ˆŒàˆr   c                 ó�  — t        | d«       | j                  ||d¬«      \  }}|r3|�1||j                  d   k7  rt        d|› d|j                  d   › d�«      ‚t	        j
                  | j                  |z   t        j                  ¬	«      d
z   }t        | j                  |«      }|r|| d…dd…f   }| j                  ||«      }||fS )aà  Create Fourier term features

        When an ARIMA is fit with an exogenous array, it must be forecasted
        with one also. Since at ``predict`` time in a pipeline we won't have
        ``y`` (and we may not yet have an ``exog`` array), we have to know how
        far into the future for which to compute Fourier terms (hence
        ``n_periods``).

        This method will compute the Fourier features for a given frequency and
        ``k`` term. Note that the ``y`` values are not used to compute these,
        so this does not pose a risk of data leakage.

        Parameters
        ----------
        y : array-like or None, shape=(n_samples,)
            The endogenous (time-series) array. This is unused and technically
            optional for the Fourier terms, since it uses the pre-computed
            ``n`` to calculate the seasonal Fourier terms.

        X : array-like or None, shape=(n_samples, n_features), optional
            The exogenous array of additional covariates. If specified, the
            Fourier terms will be column-bound on the right side of the matrix.
            Otherwise, the Fourier terms will be returned as the new exogenous
            array.

        n_periods : int, optional (default=0)
            The number of periods in the future to forecast. If ``n_periods``
            is 0, will compute the Fourier features for the training set.
            ``n_periods`` corresponds to the number of samples that will be
            returned.
        r;   Tr4   Nr   zBIf n_periods and X are specified, n_periods must match dims of X (z != ú))Údtyper   )r   r6   r0   r7   r   r8   r=   r:   r   r;   Ú_safe_hstack)r%   r>   r   Ú	n_periodsÚkwargsr?   r   Ú	X_fouriers           r   Ú	transformzFourierFeaturizer.transform�   sÐ   € ô@ 	˜˜dÔ#Ø�‰˜q !°$ˆÓ7‰ˆˆ1á˜˜Ø˜AŸG™G A™JÒ&Ü ð"Ø"+ ¨D°·±¸±°¸Að?óð ô
 —	‘	˜$Ÿ'™' IÑ-´R·Z±ZÔ@À1ÑDˆÜ" 4§7¡7¨EÓ2ˆ	ñ Ø! 9 *¡+ªq .Ñ1ˆIà×Ñ˜a Ó+ˆØ�!ˆtˆr   c                 óÄ   — t        | d«       | j                  |«        | j                  ||fdt        |«      i|¤Ž\  }}| xj                  t        |«      z  c_        ||fS )aM  Update the params and return the transformed arrays

        Since no parameters really get updated in the Fourier featurizer, all
        we do is compose forecasts for ``n_periods=len(y)`` and then update
        ``n_``.

        Parameters
        ----------
        y : array-like or None, shape=(n_samples,)
            The endogenous (time-series) array.

        X : array-like or None, shape=(n_samples, n_features)
            The exogenous array of additional covariates.

        **kwargs : keyword args
            Keyword arguments required by the transform function.
        r;   rE   )r   Ú_check_endogrH   Úlenr=   )r%   r>   r   rF   r?   ÚXts         r   Úupdate_and_transformz&FourierFeaturizer.update_and_transformÔ   s]   € ô$ 	˜˜dÔ#à×Ñ˜!ÔØ�—‘˜q !Ñ@¬s°1«vÐ@¸Ñ@‰ˆˆ2ð 	�Š”3�q“6Ñ�Ø�"ˆuˆr   )NNr   )Nr   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r$   r+   r2   r@   rH   rM   Ú__classcell__)r'   s   @r   r
   r
      s)   ø„ ñ?õB!òò	(ó(óT3÷nr   )Únumpyr   Úbaser   r   Úcompatr   Ú_fourierr	   Ú__all__ÚsinpiÚcospir   r
   © r   r   ú<module>r[      s>   ðó å $Ý !Ý %Ý %àÐ
€á	$€Ù	$€òôSÐ*¨Nõ Sr   