
    M/Phc                     ~   d Z 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 ddlmZ ddlmc mZ 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mZ ddlmZ  G d de	j                   Z! ee	j"        j                    G d de	j"                              Z# G d de	j$                  Z% ej&        e%e#           dS )z8
ARIMA model class.

Author: Chad Fulton
License: BSD-3
    )AppenderN)_is_using_pandas)sarimax)MEMORY_CONSERVE)diff)yule_walker)burg)hannan_rissanen)innovationsinnovations_mle)gls)SARIMAXSpecificationc                   X     e Zd ZdZ	 	 	 	 	 d fd		Zed
             Z	 	 	 	 d fd	Z xZS )ARIMAai  
    Autoregressive Integrated Moving Average (ARIMA) model, and extensions

    This model is the basic interface for ARIMA-type models, including those
    with exogenous regressors and those with seasonal components. The most
    general form of the model is SARIMAX(p, d, q)x(P, D, Q, s). It also allows
    all specialized cases, including

    - autoregressive models: AR(p)
    - moving average models: MA(q)
    - mixed autoregressive moving average models: ARMA(p, q)
    - integration models: ARIMA(p, d, q)
    - seasonal models: SARIMA(P, D, Q, s)
    - regression with errors that follow one of the above ARIMA-type models

    Parameters
    ----------
    endog : array_like, optional
        The observed time-series process :math:`y`.
    exog : array_like, optional
        Array of exogenous regressors.
    order : tuple, optional
        The (p,d,q) order of the model for the autoregressive, differences, and
        moving average components. d is always an integer, while p and q may
        either be integers or lists of integers.
    seasonal_order : tuple, optional
        The (P,D,Q,s) order of the seasonal component of the model for the
        AR parameters, differences, MA parameters, and periodicity. Default
        is (0, 0, 0, 0). D and s are always integers, while P and Q
        may either be integers or lists of positive integers.
    trend : str{'n','c','t','ct'} or iterable, optional
        Parameter controlling the deterministic trend. Can be specified as a
        string where 'c' indicates a constant term, 't' indicates a
        linear trend in time, and 'ct' includes both. Can also be specified as
        an iterable defining a polynomial, as in `numpy.poly1d`, where
        `[1,1,0,1]` would denote :math:`a + bt + ct^3`. Default is 'c' for
        models without integration, and no trend for models with integration.
        Note that all trend terms are included in the model as exogenous
        regressors, which differs from how trends are included in ``SARIMAX``
        models.  See the Notes section for a precise definition of the
        treatment of trend terms.
    enforce_stationarity : bool, optional
        Whether or not to require the autoregressive parameters to correspond
        to a stationarity process.
    enforce_invertibility : bool, optional
        Whether or not to require the moving average parameters to correspond
        to an invertible process.
    concentrate_scale : bool, optional
        Whether or not to concentrate the scale (variance of the error term)
        out of the likelihood. This reduces the number of parameters by one.
        This is only applicable when considering estimation by numerical
        maximum likelihood.
    trend_offset : int, optional
        The offset at which to start time trend values. Default is 1, so that
        if `trend='t'` the trend is equal to 1, 2, ..., nobs. Typically is only
        set when the model created by extending a previous dataset.
    dates : array_like of datetime, optional
        If no index is given by `endog` or `exog`, an array-like object of
        datetime objects can be provided.
    freq : str, optional
        If no index is given by `endog` or `exog`, the frequency of the
        time-series may be specified here as a Pandas offset or offset string.
    missing : str
        Available options are 'none', 'drop', and 'raise'. If 'none', no nan
        checking is done. If 'drop', any observations with nans are dropped.
        If 'raise', an error is raised. Default is 'none'.

    Notes
    -----
    This model incorporates both exogenous regressors and trend components
    through "regression with ARIMA errors". This differs from the
    specification estimated using ``SARIMAX`` which treats the trend
    components separately from any included exogenous regressors. The full
    specification of the model estimated here is:

    .. math::

        Y_{t}-\delta_{0}-\delta_{1}t-\ldots-\delta_{k}t^{k}-X_{t}\beta
            & =\epsilon_{t} \\
        \left(1-L\right)^{d}\left(1-L^{s}\right)^{D}\Phi\left(L\right)
        \Phi_{s}\left(L\right)\epsilon_{t}
            & =\Theta\left(L\right)\Theta_{s}\left(L\right)\eta_{t}

    where :math:`\eta_t \sim WN(0,\sigma^2)` is a white noise process, L
    is the lag operator, and :math:`G(L)` are lag polynomials corresponding
    to the autoregressive (:math:`\Phi`), seasonal autoregressive
    (:math:`\Phi_s`), moving average (:math:`\Theta`), and seasonal moving
    average components (:math:`\Theta_s`).

    `enforce_stationarity` and `enforce_invertibility` are specified in the
    constructor because they affect loglikelihood computations, and so should
    not be changed on the fly. This is why they are not instead included as
    arguments to the `fit` method.

    See the notebook `ARMA: Sunspots Data
    <../examples/notebooks/generated/tsa_arma_0.html>`__ and
    `ARMA: Artificial Data <../examples/notebooks/generated/tsa_arma_1.html>`__
    for an overview.

    .. todo:: should concentrate_scale=True by default

    Examples
    --------
    >>> mod = sm.tsa.arima.ARIMA(endog, order=(1, 0, 0))
    >>> res = mod.fit()
    >>> print(res.summary())
    Nr   r   r   r   r   r   r   TF   nonec                 V   |d         dk    p|d         dk    }||sd}n|d}t          |||||d d ||	|
|||          | _        | j        j        j        j        }t          | j        j                  dk    }|rBt          j        | j        j                  }||d         |d         z   k     rt          d          d }|Ft          |d           r|j        d d | j        j        d f         }n|d d | j        j        d f         }t                                          ||d ||||||
|||           || _        || _        || j        | j        j        d          | _        nd | _        | j        j        | _        | j        j        | _        g dfd	| j        D             | _        d S )
Nr   r   cn)exogorderseasonal_ordertrendenforce_stationarityenforce_invertibilityconcentrate_scaletrend_offsetdatesfreqmissingvalidate_specificationa{  In models with integration (`d > 0`) or seasonal integration (`D > 0`), trend terms of lower order than `d + D` cannot be (as they would be eliminated due to the differencing operation). For example, a constant cannot be included in an ARIMA(1, 1, 1) model, but including a linear trend, which would have the same effect as fitting a constant to the differenced data, is allowed.)
r   r   r   r   r   r   r    r!   r"   r#   )measurement_errortime_varying_regressionmle_regressionsimple_differencinghamilton_representationc                     g | ]}|v|	S  r*   ).0keyunuseds     [/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/statsmodels/tsa/arima/model.py
<listcomp>z"ARIMA.__init__.<locals>.<listcomp>   s#    OOO3S=N=N3=N=N=N    )r   _spec_arima_modeldata	orig_exoglentrend_termsnpmin
ValueErrorr   ilock_trendsuper__init__r   _input_exog
exog_names_input_exog_namesk_exog
_init_keys)selfendogr   r   r   r   r   r   r   r   r    r!   r"   r#   
integrated	has_trendlowest_trend
input_exogr-   	__class__s                     @r.   r=   zARIMA.__init__   s%    1X\:^A%6%:
==EE]E 0E.d$/ldG#9; ; ; &+5 (4559	 	$6$"2">??LeAh):::: #$ $ $ 
d++ @!Yqqq$*:*B*C*C'CD

!!!!T%5%=%>%>">?
 	4t5)!5"7/u44J 	 	L 	L 	L 
 &%)_T5E5M5N5N%OD""%)D"
 &-'/- - - POOO$/OOOr0   c                 "    dt           t          fiS )Nfit)ARIMAResultsARIMAResultsWrapper)rC   s    r.   _res_classeszARIMA._res_classes   s    &9:;;r0   c           
         || j                             |           nd}ddg}| j        r||vrt          d| d| d          |i }g }|dk    rg d}n	|dk    rd	g}|D ]/}||v rt          d
|d|d          t	          | |          ||<   0|i }|%|dvrt          d|z            ||d<   ||d<   ||d<   d}d}| j         j        du}|s|dk    r|rM|s|I|dk    rC| j        rt          d          t          | j        f| j        | j	        | j
        d||d|\  }}n|dk    rt          d|z            |                    dd            t                      j        d,|
|||	d|}|
s|j        |_        n| j        }| j         j	        }| j         j
        }| j         j        rt#          j        d|z  d           t'          || j         j        | j         j        | j         j                  }|d         dk    r|d         d|d         f}|d         dk    r|d         d|d         |d         f}| j        r| j                                        |d<   |d k    rt1          |f|d         dd!|\  }}n|d"k    rt3          |f|d         dd!|\  }}ni|dk    r!t5          |f|d         |d         dd#|\  }}nB|d$k    r"t7          |f|d         dd%|\  }}|d&         }n|dk    rt9          |f||dd'|\  }}|| j        r&| j         j        dk    r|j        st          d(          | j         r&| j         j!        dk    r|j"        st          d)          |
r|j#        }n|r+| j$        j%        }| j$        &                    tN                     | j$        j(        s| j$        j)        s| j$        j*        r| j+        }n| j,        } ||j#        d*d*||	+          }||_        |r| j$        &                    |           |S )-a^  
        Fit (estimate) the parameters of the model.

        Parameters
        ----------
        start_params : array_like, optional
            Initial guess of the solution for the loglikelihood maximization.
            If None, the default is given by Model.start_params.
        transformed : bool, optional
            Whether or not `start_params` is already transformed. Default is
            True.
        includes_fixed : bool, optional
            If parameters were previously fixed with the `fix_params` method,
            this argument describes whether or not `start_params` also includes
            the fixed parameters, in addition to the free parameters. Default
            is False.
        method : str, optional
            The method used for estimating the parameters of the model. Valid
            options include 'statespace', 'innovations_mle', 'hannan_rissanen',
            'burg', 'innovations', and 'yule_walker'. Not all options are
            available for every specification (for example 'yule_walker' can
            only be used with AR(p) models).
        method_kwargs : dict, optional
            Arguments to pass to the fit function for the parameter estimator
            described by the `method` argument.
        gls : bool, optional
            Whether or not to use generalized least squares (GLS) to estimate
            regression effects. The default is False if `method='statespace'`
            and is True otherwise.
        gls_kwargs : dict, optional
            Arguments to pass to the GLS estimation fit method. Only applicable
            if GLS estimation is used (see `gls` argument for details).
        cov_type : str, optional
            The `cov_type` keyword governs the method for calculating the
            covariance matrix of parameter estimates. Can be one of:

            - 'opg' for the outer product of gradient estimator
            - 'oim' for the observed information matrix estimator, calculated
              using the method of Harvey (1989)
            - 'approx' for the observed information matrix estimator,
              calculated using a numerical approximation of the Hessian matrix.
            - 'robust' for an approximate (quasi-maximum likelihood) covariance
              matrix that may be valid even in the presence of some
              misspecifications. Intermediate calculations use the 'oim'
              method.
            - 'robust_approx' is the same as 'robust' except that the
              intermediate calculations use the 'approx' method.
            - 'none' for no covariance matrix calculation.

            Default is 'opg' unless memory conservation is used to avoid
            computing the loglikelihood values for each observation, in which
            case the default is 'oim'.
        cov_kwds : dict or None, optional
            A dictionary of arguments affecting covariance matrix computation.

            **opg, oim, approx, robust, robust_approx**

            - 'approx_complex_step' : bool, optional - If True, numerical
              approximations are computed using complex-step methods. If False,
              numerical approximations are computed using finite difference
              methods. Default is True.
            - 'approx_centered' : bool, optional - If True, numerical
              approximations computed using finite difference methods use a
              centered approximation. Default is False.
        return_params : bool, optional
            Whether or not to return only the array of maximizing parameters.
            Default is False.
        low_memory : bool, optional
            If set to True, techniques are applied to substantially reduce
            memory usage. If used, some features of the results object will
            not be available (including smoothed results and in-sample
            prediction), although out-of-sample forecasting is possible.
            Default is False.

        Returns
        -------
        ARIMAResults

        Examples
        --------
        >>> mod = sm.tsa.arima.ARIMA(endog, order=(1, 0, 0))
        >>> res = mod.fit()
        >>> print(res.summary())
        N
statespacer
   z2When parameters have been fixed, only the methods z can be used; got 'z'.)r   r   r   r   r   z'Cannot override model level value for "z" when method="z".)rP   r   z_Estimation method "%s" does not use starting parameters, but `start_params` argument was given.start_paramstransformedincludes_fixedzIGLS estimation is not yet implemented for the case with fixed parameters.F)r   r   r   include_constantarma_estimatorarma_estimator_kwargszkIf `exog` is given and GLS is disabled (`gls=False`), then the only valid method is 'statespace'. Got '%s'.dispr   )return_params
low_memorycov_typecov_kwdszsProvided `endog` series has been differenced to eliminate integration prior to parameter estimation by method "%s".   )
stacklevel)k_diffk_seasonal_diffseasonal_periodsr      fixed_paramsr   )ar_orderdemeanr	   )rc   ma_orderrd   r   )re   rd   )r   r   rd   zNon-stationary autoregressive parameters found with `enforce_stationarity=True`. Consider setting it to False or using a different estimation method, such as method="statespace".zNon-invertible moving average parameters found with `enforce_invertibility=True`. Consider setting it to False or using a different estimation method, such as method="statespace".T)rR   rS   rZ   r[   r*   )-r1   validate_estimator_has_fixed_paramsr9   getattrr   NotImplementedErrorestimate_glsrD   r   r   
setdefaultr<   rK   mlefitfit_detailsis_integratedwarningswarnr   seasonal_diffr`   _fixed_paramscopyr   r	   r
   r   r   r   max_reduced_ar_orderis_stationaryr   max_reduced_ma_orderis_invertibleparamsssmconserve_memoryset_conserve_memoryr   memory_no_predictedmemory_no_gainmemory_no_smoothingfiltersmooth)rC   rQ   rR   rS   methodmethod_kwargsr   
gls_kwargsrZ   r[   rX   rY   methods_with_fixed_paramsrequired_kwargsnameprn   has_exogresrD   r   r   r{   funcrI   s                           r.   rK   z	ARIMA.fit   s   t //7777 "F &23D$E!! 	f4M&M&ML,L LAGL L L    M\!!4 4 4OO(((67O# 	6 	6D}$$ j9=vvv"G H H H")$"5"5M$ J
 #>>>  "+-3"4 5 5 5 -9M.)+6M-(.<M*+ #(4 M	3v--  1S 1S[V|5K5K) -1   ".J""%)Ydj#'#6#)"" "" !	"" "";;
 <''  "F $*"* + + +
 ((333!eggk K"/J%K K<IK K % 1&)jCO
 JE$*E!-<N- 9 <>DE *+- - - - $"2"7$($4$B%)%5%FH H H 8a<<"1Xq%(3E!!$q((&4Q&7N1<M&4Q&7&9N% J040B0G0G0I0In- &&!,"%$)!HU"% "%#"% "%;; 6!!!%e "EeAh-2"E "E6C"E "E;;,,,!0"F$)!H"1Xe"F "F7D"F "F;; =((!,"%$)!HU"% "%#"% "%;
 bE,,,!0"3!&#1 "3 "3 %2"3 "3; =) :(=AAO B  "9 : : : * :(=AAO B  "9 : : :  Bh  B&*h&>OH00AAA H0 'DH4K '87';DD;Dd18d$,xA A A #.  BH00AAA
r0   )Nr   r   NTTFr   NNr   T)NTFNNNNNNFF)	__name__
__module____qualname____doc__r=   propertyrN   rK   __classcell__rI   s   @r.   r   r      s        j jV 0948BF@DCG	TP TP TP TP TP TPl < < X< GLBF8=M M M M M M M M M Mr0   r   c                   `     e Zd Z eej        j        j                  d fd	            Z xZS )rL   NFc                 "   |N| j         j        j        }| j         j        }| j         j        | j         j        _        | j         j        | j         _         t                      j        |f|||d|}||| j         j        _        || j         _        |S )N)r   refit
fit_kwargs)modelr3   r4   r?   r>   r@   r<   append)
rC   rD   r   r   r   kwargsr4   r?   outrI   s
            r.   r   zARIMAResults.append  s     
1I.J(,
(>DJO%$(J$@DJ! eggnU >U(2> >6<> > (1DJO%$.DJ!
r0   )NFN)	r   r   r   r   r   SARIMAXResultsr   r   r   r   s   @r.   rL   rL     s]         Xg$+344     54    r0   rL   c                       e Zd Zi Z ej        ej        j        e          Zi Z	 ej        ej        j
        e	          Z
dS )rM   N)r   r   r   _attrswrapunion_dictsr   SARIMAXResultsWrapper_wrap_attrs_methods_wrap_methodsr*   r0   r.   rM   rM     sV        F"$"%16; ;KH$D$%3X? ?MMMr0   rM   )'r   statsmodels.compat.pandasr   rp   numpyr7   statsmodels.tools.datar   statsmodels.tsa.statespacer   (statsmodels.tsa.statespace.kalman_filterr    statsmodels.tsa.statespace.toolsr   statsmodels.base.wrapperbasewrapperr   ,statsmodels.tsa.arima.estimators.yule_walkerr   %statsmodels.tsa.arima.estimators.burgr	   0statsmodels.tsa.arima.estimators.hannan_rissanenr
   ,statsmodels.tsa.arima.estimators.innovationsr   r   $statsmodels.tsa.arima.estimators.glsr   rk   #statsmodels.tsa.arima.specificationr   SARIMAXr   r   rL   r   rM   populate_wrapperr*   r0   r.   <module>r      s    / . . . . .      3 3 3 3 3 3 . . . . . . D D D D D D 1 1 1 1 1 1 ' ' ' ' ' ' ' ' ' D D D D D D 6 6 6 6 6 6 L L L L L L" " " " " " " " D D D D D D D D D D D DS S S S SGO S S Sl 
'
 
())    7)   *)6? ? ? ? ?'7 ? ? ?  )< 8 8 8 8 8r0   