
    DjB                        d dl mZ d dlmZ d dlmZ d dlmZm	Z	 d dl
ZddlmZ ddlmZ dd	lmZmZmZ d
dlmZ d
dlmZ g dZ G d dee      Z G d dee      Z G d de      Z G d de      Z G d de      Zy)    )BaseEstimator)LinearRegression)api)ABCMetaabstractmethodN   )DTYPE)
deprecated)cdiffcheck_endog   )approx)C_tseries_pp_sum)ADFTestKPSSTestPPTestc                   ,    e Zd Zed        Zed        Zy)_BaseStationarityTestc                 .    | | j                   d   dk(  ryy)Nr   FT)shape)xs    _C:\Crop_Prediction\Backend\crop-ai-system\venv\Lib\site-packages\pmdarima/arima/stationarity.py
_base_casez _BaseStationarityTest._base_case    s     I1771:?    c                     | j                   d   }||kD  rt        d      t        t        |dz
  dd            D cg c]  \  }}| |||z
    }}}t	        j
                  |      S c c}}w )Nr   zk cannot exceed y dimr   )r   
ValueError	enumeraterangenpasarray)r   knijrowss         r   _embedz_BaseStationarityTest._embed'   sv     GGAJq5455 $-U1q5"b-A#B
1aAaAJ
 
 zz$	
s   A&N)__name__
__module____qualname__staticmethodr   r(    r   r   r   r      s(      
  
 r   r   )	metaclassc                   D    e Zd ZdZd Zed        Z ed      d        Zy)_DifferencingStationarityTestu   Provides the base class for stationarity tests such as the
    Kwiatkowski–Phillips–Schmidt–Shin, Augmented Dickey-Fuller and the
    Phillips–Perron tests. These tests are used to determine whether a time
    series is stationary.
    c                     || _         y )Nalpha)selfr3   s     r   __init__z&_DifferencingStationarityTest.__init__<   s	    
r   c                      y)a  Test whether the time series is stationary or it needs differencing.

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

        Returns
        -------
        pval : float
            The computed P-value of the test.

        sig : bool
            Whether the P-value is significant at the ``alpha`` level.
            More directly, whether to difference the time series.
        Nr-   r4   r   s     r   should_diffz)_DifferencingStationarityTest.should_diff?   s    r   r8   )use_insteadc                 $    | j                  |      S )a  Test whether the time series is stationary.

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

        Returns
        -------
        pval : float
            The computed P-value of the test.

        sig : bool
            Whether the P-value is significant at the ``alpha`` level.
            More directly, whether to difference the time series.
        )r8   r7   s     r   is_stationaryz+_DifferencingStationarityTest.is_stationaryS   s    $ ""r   N)	r)   r*   r+   __doc__r5   r   r8   r
   r;   r-   r   r   r0   r0   6   s9    
  & M*# +#r   r0   c                   H     e Zd ZdZddhZ edddd      Zd
 fd	Zd	 Z xZ	S )r   u  Conduct a KPSS test for stationarity.

    In econometrics, Kwiatkowski–Phillips–Schmidt–Shin (KPSS) tests are used
    for testing a null hypothesis that an observable time series is stationary
    around a deterministic trend (i.e. trend-stationary) against the
    alternative of a unit root.

    Parameters
    ----------
    alpha : float, optional (default=0.05)
        Level of the test

    null : str, optional (default='level')
        Whether to fit the linear model on the one vector, or an arange.
        If ``null`` is 'trend', a linear model is fit on an arange, if
        'level', it is fit on the one vector.

    lshort : bool, optional (default=True)
        Whether or not to truncate the ``l`` value in the C code.

    Notes
    -----
    This test is generally used indirectly via the
    :func:`pmdarima.arima.ndiffs` function, which computes the
    differencing term, ``d``.

    References
    ----------
    .. [1] R's tseries KPSS test source code: http://bit.ly/2eJP1IU
    trendnull{Gz?皙?皙?皙?c                 J    t         t        |   |       || _        || _        y Nr2   )superr   r5   r?   lshort)r4   r3   r?   rG   	__class__s       r   r5   zKPSSTest.__init__   s#    h&U&3	r   c                    | j                  |      st        j                  dfS t        |t        d      }|j
                  d   }| j                  }|dk(  r4t        j                  |      j                  |d      }t        dddd	      }nQ|d
k(  r4t        j                  |      j                  |d      }t        dddd      }nt        d| j                  z        t               j                  ||      }||j                  |      z
  }t        j                   |      }||z  j#                         |dz  z  }	||z  j#                         |z  }
| j$                  r(t'        t        j(                  d|dz  dz  z              }n't'        t        j(                  d|dz  dz  z              }t+        ||||
      }
|	|
z  }t-        || j.                  |d      \  }}|d   |d   | j0                  k  fS )  Test whether the time series is stationary or needs differencing.

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

        Returns
        -------
        pval : float
            The computed P-value of the test.

        sig : bool
            Whether the P-value is significant at the ``alpha`` level.
            More directly, whether to difference the time series.
        Fdtypepreserve_seriesr   r>   r   gS?gI+?g㥛 ?gX9v?levelgS?g|?5^?goʡ?gh|?5?znull must be one of %rr      d         ?   xoutrule)r   r!   nanr   r	   r   r?   arangereshaper   onesr   _validr   fitpredictcumsumsumrG   inttruncr   r   tablepr3   )r4   r   r$   r?   ttablelmesetas2l_stat_pvals                  r   r8   zKPSSTest.should_diff   s   " q!665=  >GGAJ yy 7?		!$$Q*A eUE51EW_
""1a(A eUE51E5CDD ##Aq)

1IIaL1ukkmq!t$!e[[]Q ;;RXXa1s7t"3345BRXXbAG#4456B aB+Rx $Q?4
 AwQ$**,,,r   )rB   rN   T)
r)   r*   r+   r<   rZ   r   ra   r5   r8   __classcell__rH   s   @r   r   r   h   s/    < vFtUD$'FD-r   r   c            
            e Zd ZdZ ej
                  g d      Zej                  d   Z e	dddddd	      Z
 e	d
ddddddd      Zd fd	Zed        Zed        Zd Z xZS )r   u  Conduct an ADF test for stationarity.

    In statistics and econometrics, an augmented Dickey–Fuller test (ADF)
    tests the null hypothesis of a unit root is present in a time series
    sample. The alternative hypothesis is different depending on which version
    of the test is used, but is usually stationarity or trend-stationarity. It
    is an augmented version of the Dickey–Fuller test for a larger and more
    complicated set of time series models.

    Parameters
    ----------
    alpha : float, optional (default=0.05)
        Level of the test

    k : int, optional (default=None)
        The drift parameter. If ``k`` is None, it will be set to:
        ``np.trunc(np.power(x.shape[0] - 1, 1 / 3.0))``

    Notes
    -----
    This test is generally used indirectly via the
    :func:`pmdarima.arima.ndiffs` function, which computes the
    differencing term, ``d``.

    ADF test does not perform as close to the R code as do the KPSS and PP
    tests. This is due to the fact that is has to use statsmodels OLS
    regression for std err estimates rather than the more robust sklearn
    LinearRegression.

    References
    ----------
    .. [1] https://wikipedia.org/wiki/Augmented_Dickey–Fuller_test
    .. [2] R's tseries ADF source code: https://bit.ly/2EnvM5V
    ))gQgggQ	g=
ףp=gg      g333333ÿ)ggffffffg      gq=
ףp	g
ףp=
gףp=
g(\gQο)g)\(gףp=
gg333333	gQggףp=
gQѿ)gQgQgq=
ףp
ףp=
	gGzgq=
ףpg{Gzgףp=
ӿ)gףp=
gq=
ףpg\(\rp   gףp=
g(\gg{GzԿ)gGzgHzGgHzGg(\g      gGzgQgQտr      2   rP        順 r@   rA   rB   rC   ?ffffff?333333?Gz?c                 b    t         t        |   |       || _        ||dk  rt	        d      y y )Nr2   r   z#k must be a positive integer (>= 0))rF   r   r5   r#   r   )r4   r3   r#   rH   s      r   r5   zADFTest.__init__  s:    gt%E%2=QUBCC #=r   c                    |j                   d   }|d d df   }t        j                  |dz
  |      }| |   }|dz  }|j                   d   }t        j                  t        j                  |      j                  |df      |j                  |df      |j                  |df      g      }	|dkD  r"|d d d|f   }
t        j                  |	|
g      }	t        j                  ||	d      j                  d      S )Nr   r   T)hasconstqr)method)	r   r!   rW   hstackrY   rX   smOLSr[   )r   yzr#   r$   ytttxt1_nXyt1s              r   _olszADFTest._ols  s    GGAJq!tWYYq1ua  e 	a YYq\IIrwwr{**B73{{B7+zz2q'*, - q5AqsF)C		1c(#A vvb!d+//t/<<r   c                 D    | j                   }| j                  d   |d   z  S )Nr   )bseparams)resstderrss     r   _ols_std_errorzADFTest._ols_std_error*  s"    ''zz!}wqz))r   c                    | j                  |      st        j                  dfS t        |t        d      }| j
                  }|9t        j                  t        j                  |j                  d   dz
  d            }t        |      dz   }t        |      }|j                  d   }| j                  ||      j                  }| j                  ||||      }| j                  |      }t        j                  t!        | j"                        D cg c]/  }t%        | j&                  | j(                  dd|f   |d      d   1 c}      }	t%        |	| j*                  |d      \  }
}|d   }||| j,                  kD  fS c c}w )	rJ   FrK   Nr   r   gUUUUUU?r   rS   )r   r!   rV   r   r	   r#   r`   powerr   r_   r   r(   Tr   r   arrayr    tablenr   tableTrc   ra   r3   )r4   r   r#   r   r$   r   r   STATr%   tableiplrk   interpolrl   s                r   r8   zADFTest.should_diff/  sT   " q!665=  > FF9!''!*q.':;A FQJGGGAJKK1 ii1a#""3' 884;;') 4;;

1a4 0qqA!D) *
 Xt{{AF8 {
 TDJJ&&&+)s   4E9)rB   N)r)   r*   r+   r<   r!   r   rc   r   r   r   r   ra   r5   r,   r   r   r8   rm   rn   s   @r   r   r      s    !D BHH  E [[^Fr2sCf-FtUD$dE4@FD = =6 * *<'r   r   c            
            e Zd ZdZ ej
                  g d      j                   Zej                  d   Z	 e
dddddd	      j                  e      Z e
d
ddddddd      Zd fd	Zd Z xZS )r   u6  Conduct a PP test for stationarity.

    In statistics, the Phillips–Perron test (named after Peter C. B.
    Phillips and Pierre Perron) is a unit root test. It is used in time series
    analysis to test the null hypothesis that a time series is integrated of
    order 1. It builds on the Dickey–Fuller test of the null hypothesis
    ``p = 0``.

    Parameters
    ----------
    alpha : float, optional (default=0.05)
        Level of the test

    lshort : bool, optional (default=True)
        Whether or not to truncate the ``l`` value in the C code.

    Notes
    -----
    This test is generally used indirectly via the
    :func:`pmdarima.arima.ndiffs` function, which computes the
    differencing term, ``d``.

    The R code allows for two types of tests: 'Z(alpha)' and 'Z(t_alpha)'.
    Since sklearn does not allow extraction of std errors from the linear
    model fit, ``t_alpha`` is much more difficult to achieve, so we do not
    allow that variant.

    References
    ----------
    .. [1] R's tseries PP test source code: http://bit.ly/2wbzx6V
    ))g     6@g333339@gffffff;@gffffff<@gfffff<@g     =@)gfffff3@gffffff6@g7@gffffff8@g8@g9@)gfffff1@g3@g333334@gL5@g     5@g5@)g333333/@g0@g     1@g      2@g2@gL2@)gHzG@gGz@gQ@g      @gGz@g)\(@)gGz@g@g(\@gQ@g333333@gHzG@)g{Gz?g(\?gGz?{Gz?r   gp=
ף?)gQ?g?g      ?g=
ףp=?gzG?gףp=
?r   rq   rr   rP   rs   rt   ru   r@   rA   rB   rC   rv   rw   rx   ry   c                 <    t         t        |   |       || _        y rE   )rF   r   r5   rG   )r4   r3   rG   rH   s      r   r5   zPPTest.__init__  s    fd$5$1r   c                    | j                  |      st        j                  dfS t        |t        d      }| j                  |d      }|dddf   }|dddf   }|j                  d   }t        j                  |      dz   |dz  z
  }t        j                  t        j                  |      ||g      j                  }t               j                  ||      }|j                  }	||j                  |      z
  }
|
|
z  j                         t!        |      z  }| j"                  sdnd	}t%        t        j&                  |t        j(                  |d
z  d      z              }t+        |
|||      }||z  }|t        j                  |      dz   z  j                         }||dz
  z  |dz  j                         z  dz  }||dz  z  }||dz   z  |z  |j                         z  }||dz   z  d|z  dz   z  |j                         dz  z  dz  }||z
  |z   |z
  }|	d   }||dz
  z  |dz  d|z  z  ||z
  z  z
  }t        j                  t-        | j.                        D cg c]/  }t1        | j2                  | j4                  dd|f   |d      d   1 c}      }t1        || j6                  |d      \  }}|d   }||| j8                  kD  fS c c}w )rJ   FrK   r   r   Nr   g       @rR   rO   g      Y@rQ   g      (@g      @   g      8@rS   )r   r!   rV   r   r	   r(   r   rW   r   rY   r   r   r[   coef_r\   r^   floatrG   r_   r`   r   r   r    r   r   r   rc   ra   r3   )r4   r   r   r   r   r$   r   r   r   coefussqruscalarri   ssqrtln2syt11ntrm1trm2trm3trm4dxr3   r   r%   r   rk   r   rl   s                                r   r8   zPPTest.should_diff  s   " q!665=  >
 KK1q!tW1g HHQKiilQ1s7+HHbggaj"c*+-- $$Q+yy QQa(;;A&288AIt#<<=>!!QE2 U1)*//1R!V}q~~//$6 FaK  AE{V#cggi/QUq1uqy)SWWY!^<CD[4$& QEAI!q&TBY!76E>!JJ884;;') 4;;

1a4 0qqA!D) *
 Xt{{AF8{ TDJJ&&&)s   4K)rB   T)r)   r*   r+   r<   r!   r   r   rc   r   r   r   astyper	   r   ra   r5   r8   rm   rn   s   @r   r   r   n  s|    > RXX 	 	 	q		E [[^Fr2sCf-44U;FtUD$dE4@F
L'r   r   )sklearn.baser   sklearn.linear_modelr   statsmodelsr   r   abcr   r   numpyr!   compat.numpyr	   
decoratorsr
   utils.arrayr   r   r   r   _arimar   __all__r   r0   r   r   r   r-   r   r   <module>r      s~    ' 1 ! '    # . . 
 % MW  ./#$9W /#dl-, l-^T'+ T'n@'* @'r   