
    Dj                        d dl mZ d dlZd dlZddlmZ ddlm	Z	 ddl
mZmZ ddlmZ dd	lmZ dd
lmZ g dZej(                  ej*                  ej,                  dZej0                  ej2                  dZd ZddZddZy)    )column_or_1dN   )context_managers)get_callable)diffcheck_endog)DTYPE   )stationarity)seasonality)is_constantndiffsnsdiffs)kpssadfpp)ocsbchc                 D    t        |       } | | d   k(  j                         S )a  Test ``x`` for constancy.

    Determine whether a vector is composed of all of the same elements
    and nothing else.

    Parameters
    ----------
    x : array-like, shape=(n_samples,)
        The time series vector.

    Examples
    --------
    >>> import numpy as np
    >>> x = np.array([1, 2, 3])
    >>> y = np.ones(3)
    >>> [is_constant(x), is_constant(y)]
    [False, True]
    r   )r   all)xs    XC:\Crop_Prediction\Backend\crop-ai-system\venv\Lib\site-packages\pmdarima/arima/utils.pyr   r   %   s"    & 	QA1I??    c                    |dk  rt        d       t        |t              |fi |j                  }t	        | t
        d      } t        |       ryd} ||       }|dk(  rk||k  rf|dz  }t        | |      } t        |       r|S t        |       |k  r&t        j                  dt        |       ||fz         |S  ||       }|dk(  r||k  rf|S )a  Estimate the seasonal differencing term, ``D``.

    Perform a test of seasonality for different levels of ``D`` to
    estimate the number of seasonal differences required to make a given time
    series stationary. Will select the maximum value of ``D`` for which
    the time series is judged seasonally stationary by the statistical test.

    Parameters
    ----------
    x : array-like, shape=(n_samples, [n_features])
        The array to difference.

    m : int
        The number of seasonal periods (i.e., frequency of the
        time series)

    max_D : int, optional (default=2)
        Maximum number of seasonal differences allowed. Must
        be a positive integer. The estimated value of ``D`` will not
        exceed ``max_D``.

    test : str, optional (default='ocsb')
        Type of unit root test of seasonality to use in order
        to detect seasonal periodicity. Valid tests include ("ocsb", "ch").
        Note that the CHTest is very slow for large data.

    Returns
    -------
    D : int
        The estimated seasonal differencing term. This is the maximum value
        of ``D`` such that ``D <= max_D`` and the time series is judged
        seasonally stationary. If the time series is constant, will return 0.
    r   z max_D must be a positive integerFdtypecopyr
   )lagz}Appropriate D value may not have been reached; length of seasonally-differenced array (%i) is shorter than m (%i). Using D=%i)
ValueErrorr   VALID_STESTS#estimate_seasonal_differencing_termr   r	   r   r   lenwarningswarn)r   mmax_DtestkwargstestfuncDdodiffs           r   r   r   <   s    D z;<< 0|D,/<V<	,	, AU/A1~	Aa[F
A+!e)	QNq>H q6A:MM < !VQN+ , H!! A+!e)$ Hr   c           	      t   |dk  rt        d       t        |t              |fi |j                  }t	        | t
        d      } d}t        |       r|S t        j                  t        j                  j                  t         d|t        |       fz        5   ||       \  }}t        j                  |      r
	 ddd       y|ra||k  r\|dz  }t        |       } t        |       r|cddd       S  ||       \  }}t        j                  |      r|dz
  cddd       S |r||k  r\ddd       |S # 1 sw Y   |S xY w)	a  Estimate ARIMA differencing term, ``d``.

    Perform a test of stationarity for different levels of ``d`` to
    estimate the number of differences required to make a given time
    series stationary. Will select the maximum value of ``d`` for which
    the time series is judged stationary by the statistical test.

    Parameters
    ----------
    x : array-like, shape=(n_samples, [n_features])
        The array (time series) to difference.

    alpha : float, optional (default=0.05)
        Level of the test. This is the value above below which the P-value
        will be deemed significant.

    test : str, optional (default='kpss')
        Type of unit root test of stationarity to use in order to
        test the stationarity of the time-series. One of ('kpss', 'adf', 'pp')

    max_d : int, optional (default=2)
        Maximum number of non-seasonal differences allowed. Must
        be a positive integer. The estimated value of ``d`` will not
        exceed ``max_d``.

    Returns
    -------
    d : int
        The estimated differencing term. This is the maximum value of ``d``
        such that ``d <= max_d`` and the time series is judged stationary.
        If the time series is constant, will return 0.

    References
    ----------
    .. [1] R's auto_arima ndiffs function
           https://github.com/robjhyndman/forecast/blob/19b0711e554524bf6435b7524517715658c07699/R/arima.R#L132  # noqa: E501
    r   z max_d must be a positive integerFr   a
  Encountered exception in stationarity test (%r). This can occur in seasonal settings when a large enough `m` coupled with a large enough `D` difference the training array into too few samples for OLS (input contains %i samples). Try fitting on a larger training size)	raise_err	raise_msgNr
   )r   r   VALID_TESTSshould_diffr   r	   r   ctxexcept_and_reraisenplinalgLinAlgErrorr"   isnanr   )	r   alphar'   max_dr(   r)   dpvalr+   s	            r   r   r      sC   L z;<< /|D+.u??KKHAU/A 	
A1~			II!! &
 *.s1v7	
   {f 88D> $ UFA QA1~1 6 $A;LD& xx~1u? $ U%D HED Hs   "D- #D--$D-D--D7)r   r   )g?r   r   )sklearn.utils.validationr   numpyr3   r#    r   r1   utilsr   utils.arrayr   r   compat.numpyr	   r   statest_libr   seatest_lib__all__KPSSTestADFTestPPTestr/   OCSBTestCHTestr    r   r   r    r   r   <module>rJ      sz    2   &   +   ) (   


   


.AHTr   