
    M/PhI                    J   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 ddlmZmZmZ ddlm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'  G d de          Z( G d de          Z) G d de          Z* ej+        e*e)           dS )z<
SARIMAX Model

Author: Chad Fulton
License: Simplified-BSD
    )warnN)Appender)Bunch)_is_using_pandas)cache_readonly)SARIMAXSpecification)SARIMAXParams)lagmat   )Initialization)MLEModel
MLEResultsMLEResultsWrapper)companion_matrixdiffis_invertibleconstrain_stationary_univariate!unconstrain_stationary_univariateprepare_exogprepare_trend_specprepare_trend_datac                       e Zd ZdZ	 	 	 	 	 	 	 	 d$ fd		Z fd
Z fdZd%dZed             Z	ed             Z
ed             Zed             Zd%dZed             Ze	 	 d&d            Zed             Zed'd            Zg dZed             Zed             Zed             Zed             Zed             Zed             Zd'dZd Zd  Z fd!Z	 	 d(d"Z	 	 d)d#Z xZ S )*SARIMAXa27  
    Seasonal AutoRegressive Integrated Moving Average with eXogenous regressors
    model

    Parameters
    ----------
    endog : array_like
        The observed time-series process :math:`y`
    exog : array_like, optional
        Array of exogenous regressors, shaped nobs x k.
    order : iterable or iterable of iterables, optional
        The (p,d,q) order of the model for the number of AR parameters,
        differences, and MA parameters. `d` must be an integer
        indicating the integration order of the process, while
        `p` and `q` may either be an integers indicating the AR and MA
        orders (so that all lags up to those orders are included) or else
        iterables giving specific AR and / or MA lags to include. Default is
        an AR(1) model: (1,0,0).
    seasonal_order : iterable, optional
        The (P,D,Q,s) order of the seasonal component of the model for the
        AR parameters, differences, MA parameters, and periodicity.
        `D` must be an integer indicating the integration order of the process,
        while `P` and `Q` may either be an integers indicating the AR and MA
        orders (so that all lags up to those orders are included) or else
        iterables giving specific AR and / or MA lags to include. `s` is an
        integer giving the periodicity (number of periods in season), often it
        is 4 for quarterly data or 12 for monthly data. Default is no seasonal
        effect.
    trend : str{'n','c','t','ct'} or iterable, optional
        Parameter controlling the deterministic trend polynomial :math:`A(t)`.
        Can be specified as a string where 'c' indicates a constant (i.e. a
        degree zero component of the trend polynomial), 't' indicates a
        linear trend with time, and 'ct' is both. Can also be specified as an
        iterable defining the non-zero polynomial exponents to include, in
        increasing order. For example, `[1,1,0,1]` denotes
        :math:`a + bt + ct^3`. Default is to not include a trend component.
    measurement_error : bool, optional
        Whether or not to assume the endogenous observations `endog` were
        measured with error. Default is False.
    time_varying_regression : bool, optional
        Used when an explanatory variables, `exog`, are provided
        to select whether or not coefficients on the exogenous regressors are
        allowed to vary over time. Default is False.
    mle_regression : bool, optional
        Whether or not to use estimate the regression coefficients for the
        exogenous variables as part of maximum likelihood estimation or through
        the Kalman filter (i.e. recursive least squares). If
        `time_varying_regression` is True, this must be set to False. Default
        is True.
    simple_differencing : bool, optional
        Whether or not to use partially conditional maximum likelihood
        estimation. If True, differencing is performed prior to estimation,
        which discards the first :math:`s D + d` initial rows but results in a
        smaller state-space formulation. See the Notes section for important
        details about interpreting results when this option is used. If False,
        the full SARIMAX model is put in state-space form so that all
        datapoints can be used in estimation. Default is False.
    enforce_stationarity : bool, optional
        Whether or not to transform the AR parameters to enforce stationarity
        in the autoregressive component of the model. Default is True.
    enforce_invertibility : bool, optional
        Whether or not to transform the MA parameters to enforce invertibility
        in the moving average component of the model. Default is True.
    hamilton_representation : bool, optional
        Whether or not to use the Hamilton representation of an ARMA process
        (if True) or the Harvey representation (if False). Default is False.
    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 estimated
        by maximum likelihood by one, but standard errors will then not
        be available for the scale parameter.
    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.
    use_exact_diffuse : bool, optional
        Whether or not to use exact diffuse initialization for non-stationary
        states. Default is False (in which case approximate diffuse
        initialization is used).
    **kwargs
        Keyword arguments may be used to provide default values for state space
        matrices or for Kalman filtering options. See `Representation`, and
        `KalmanFilter` for more details.

    Attributes
    ----------
    measurement_error : bool
        Whether or not to assume the endogenous
        observations `endog` were measured with error.
    state_error : bool
        Whether or not the transition equation has an error component.
    mle_regression : bool
        Whether or not the regression coefficients for
        the exogenous variables were estimated via maximum
        likelihood estimation.
    state_regression : bool
        Whether or not the regression coefficients for
        the exogenous variables are included as elements
        of the state space and estimated via the Kalman
        filter.
    time_varying_regression : bool
        Whether or not coefficients on the exogenous
        regressors are allowed to vary over time.
    simple_differencing : bool
        Whether or not to use partially conditional maximum likelihood
        estimation.
    enforce_stationarity : bool
        Whether or not to transform the AR parameters
        to enforce stationarity in the autoregressive
        component of the model.
    enforce_invertibility : bool
        Whether or not to transform the MA parameters
        to enforce invertibility in the moving average
        component of the model.
    hamilton_representation : bool
        Whether or not to use the Hamilton representation of an ARMA process.
    trend : str{'n','c','t','ct'} or iterable
        Parameter controlling the deterministic
        trend polynomial :math:`A(t)`. See the class
        parameter documentation for more information.
    polynomial_ar : ndarray
        Array containing autoregressive lag polynomial lags, ordered from
        lowest degree to highest. The polynomial begins with lag 0.
        Initialized with ones, unless a coefficient is constrained to be
        zero (in which case it is zero).
    polynomial_ma : ndarray
        Array containing moving average lag polynomial lags, ordered from
        lowest degree to highest. Initialized with ones, unless a coefficient
        is constrained to be zero (in which case it is zero).
    polynomial_seasonal_ar : ndarray
        Array containing seasonal moving average lag
        polynomial lags, ordered from lowest degree
        to highest. Initialized with ones, unless a
        coefficient is constrained to be zero (in which
        case it is zero).
    polynomial_seasonal_ma : ndarray
        Array containing seasonal moving average lag
        polynomial lags, ordered from lowest degree
        to highest. Initialized with ones, unless a
        coefficient is constrained to be zero (in which
        case it is zero).
    polynomial_trend : ndarray
        Array containing trend polynomial coefficients,
        ordered from lowest degree to highest. Initialized
        with ones, unless a coefficient is constrained to be
        zero (in which case it is zero).
    k_ar : int
        Highest autoregressive order in the model, zero-indexed.
    k_ar_params : int
        Number of autoregressive parameters to be estimated.
    k_diff : int
        Order of integration.
    k_ma : int
        Highest moving average order in the model, zero-indexed.
    k_ma_params : int
        Number of moving average parameters to be estimated.
    seasonal_periods : int
        Number of periods in a season.
    k_seasonal_ar : int
        Highest seasonal autoregressive order in the model, zero-indexed.
    k_seasonal_ar_params : int
        Number of seasonal autoregressive parameters to be estimated.
    k_seasonal_diff : int
        Order of seasonal integration.
    k_seasonal_ma : int
        Highest seasonal moving average order in the model, zero-indexed.
    k_seasonal_ma_params : int
        Number of seasonal moving average parameters to be estimated.
    k_trend : int
        Order of the trend polynomial plus one (i.e. the constant polynomial
        would have `k_trend=1`).
    k_exog : int
        Number of exogenous regressors.

    Notes
    -----
    The SARIMA model is specified :math:`(p, d, q) \times (P, D, Q)_s`.

    .. math::

        \phi_p (L) \tilde \phi_P (L^s) \Delta^d \Delta_s^D y_t = A(t) +
            \theta_q (L) \tilde \theta_Q (L^s) \zeta_t

    In terms of a univariate structural model, this can be represented as

    .. math::

        y_t & = u_t + \eta_t \\
        \phi_p (L) \tilde \phi_P (L^s) \Delta^d \Delta_s^D u_t & = A(t) +
            \theta_q (L) \tilde \theta_Q (L^s) \zeta_t

    where :math:`\eta_t` is only applicable in the case of measurement error
    (although it is also used in the case of a pure regression model, i.e. if
    p=q=0).

    In terms of this model, regression with SARIMA errors can be represented
    easily as

    .. math::

        y_t & = \beta_t x_t + u_t \\
        \phi_p (L) \tilde \phi_P (L^s) \Delta^d \Delta_s^D u_t & = A(t) +
            \theta_q (L) \tilde \theta_Q (L^s) \zeta_t

    this model is the one used when exogenous regressors are provided.

    Note that the reduced form lag polynomials will be written as:

    .. math::

        \Phi (L) \equiv \phi_p (L) \tilde \phi_P (L^s) \\
        \Theta (L) \equiv \theta_q (L) \tilde \theta_Q (L^s)

    If `mle_regression` is True, regression coefficients are treated as
    additional parameters to be estimated via maximum likelihood. Otherwise
    they are included as part of the state with a diffuse initialization.
    In this case, however, with approximate diffuse initialization, results
    can be sensitive to the initial variance.

    This class allows two different underlying representations of ARMA models
    as state space models: that of Hamilton and that of Harvey. Both are
    equivalent in the sense that they are analytical representations of the
    ARMA model, but the state vectors of each have different meanings. For
    this reason, maximum likelihood does not result in identical parameter
    estimates and even the same set of parameters will result in different
    loglikelihoods.

    The Harvey representation is convenient because it allows integrating
    differencing into the state vector to allow using all observations for
    estimation.

    In this implementation of differenced models, the Hamilton representation
    is not able to accommodate differencing in the state vector, so
    `simple_differencing` (which performs differencing prior to estimation so
    that the first d + sD observations are lost) must be used.

    Many other packages use the Hamilton representation, so that tests against
    Stata and R require using it along with simple differencing (as Stata
    does).

    If `filter_concentrated = True` is used, then the scale of the model is
    concentrated out of the likelihood. A benefit of this is that there the
    dimension of the parameter vector is reduced so that numerical maximization
    of the log-likelihood function may be faster and more stable. If this
    option in a model with measurement error, it is important to note that the
    estimated measurement error parameter will be relative to the scale, and
    is named "snr.measurement_error" instead of "var.measurement_error". To
    compute the variance of the measurement error in this case one would
    multiply `snr.measurement_error` parameter by the scale.

    If `simple_differencing = True` is used, then the `endog` and `exog` data
    are differenced prior to putting the model in state-space form. This has
    the same effect as if the user differenced the data prior to constructing
    the model, which has implications for using the results:

    - Forecasts and predictions will be about the *differenced* data, not about
      the original data. (while if `simple_differencing = False` is used, then
      forecasts and predictions will be about the original data).
    - If the original data has an Int64Index, a new RangeIndex will be created
      for the differenced data that starts from one, and forecasts and
      predictions will use this new index.

    Detailed information about state space models can be found in [1]_. Some
    specific references are:

    - Chapter 3.4 describes ARMA and ARIMA models in state space form (using
      the Harvey representation), and gives references for basic seasonal
      models and models with a multiplicative form (for example the airline
      model). It also shows a state space model for a full ARIMA process (this
      is what is done here if `simple_differencing=False`).
    - Chapter 3.6 describes estimating regression effects via the Kalman filter
      (this is performed if `mle_regression` is False), regression with
      time-varying coefficients, and regression with ARMA errors (recall from
      above that if regression effects are present, the model estimated by this
      class is regression with SARIMA errors).
    - Chapter 8.4 describes the application of an ARMA model to an example
      dataset. A replication of this section is available in an example
      IPython notebook in the documentation.

    References
    ----------
    .. [1] Durbin, James, and Siem Jan Koopman. 2012.
       Time Series Analysis by State Space Methods: Second Edition.
       Oxford University Press.
    Nr   r   r   r   r   r   r   FTr   nonec                    t          |||||d d |||||          | _        t          | j                  | _        | j        j        }| j        j        }|| _        || _        |d         | _        || _        || _        || _	        |	| _
        |
| _        || _        || _        || _        || _        | j        r| j	        rt!          d          d| j        _        | j        j        j        | _        | j                                        | _        d| j        _        | j        j        j        | _        | j                                        | _        d| j        _        | j        j        j        | _        | j                                        | _        d| j        _        | j        j         j        | _!        | j!                                        | _"        || _#        || _$        tK          | j#                  \  | _&        | _'        | j&                                        | _(        | j'        | _)        | j        j*        | _+        | j        j,        | _,        t[          |d                   | _.        | j        j/        | _0        | j        j1        | _1        | j        j2        | j        j        z  | _3        | j        j4        | _4        t[          |d                   | _5        | j        j6        | j        j        z  | _7        | j        j8        | _8        | j.        | _9        | j5        | _:        | j        r/| j
        s(| j9        | j:        cxk    rdk    sn t!          d          tw          | j+        | j3        z   | j0        | j7        z   dz             | _<        | j<        dk    r!| j+        | j3        z   dk    r| j        rd| _<        t{          |          \  | _>        }| j>        | _?        | j	        o|d uo
| j>        dk    | _	        | j	         o|d uo
| j>        dk    | _@        | j@        r| j<        dk    rd| _        | j<        }| j
        s|| j        | j:        z  | j9        z   z  }| j@        r
|| j>        z  }t[          | j<        dk              }|dk    | _A        | j@        r| j        r
|| j>        z  }| j@        r|B                    d	d
           |C                    dd           | _D        | j,        | j1        z   | j4        z   | j8        z   | j)        z   | j        z   t[          | j                   z   | _E        | j	        r| xjE        | j>        z  c_E        || _F        || _G        t          |d           st          jJ        |          }| j9        | _K        | j:        | _L        | j
        r$| j9        dk    s| j:        dk    rd| _9        d| _:        | j9        | j        | j:        z  z   | _M        t          |          | _O        || _P        || _Q         t                      jS        |f|||d| | j        rd| jT        _U        | j>        dk    st          | j&                  dk    rd| jT        _V        | jW        | jT        d<   | jX        | jT        d<   | jY        | jT        d<   | jZ        | jT        d<   | j        r
d| jT        d<   | xj[        g dt          |]                                          z   z  c_[        | jT        j^        | _                                 d S d S )N)exogorderseasonal_ordertrendenforce_stationarityenforce_invertibilityconcentrate_scaledatesfreqmissingvalidate_specification   zModels with time-varying regression coefficients must integrate the coefficients as part of the state vector, so that `mle_regression` must be set to False.r   r   zThe Hamilton representation is only available for models in which there is no differencing integrated into the state vector. Set `simple_differencing` to True or set `hamilton_representation` to FalseTinitial_varianceg    _Bloglikelihood_burn)r   k_statesk_posdefFdesignstate_intercept
transition	selectiong      ?	state_covr   r   )r   r    r!   measurement_errortime_varying_regressionmle_regressionsimple_differencingr"   r#   hamilton_representationr$   trend_offset)`r   _specr	   _paramsr   r    seasonal_periodsr5   r6   r7   r8   r"   r#   r9   r$   use_exact_diffuse
ValueError	ar_paramsar_polycoefpolynomial_arcopy_polynomial_ar	ma_paramsma_polypolynomial_ma_polynomial_maseasonal_ar_paramsseasonal_ar_polypolynomial_seasonal_ar_polynomial_seasonal_arseasonal_ma_paramsseasonal_ma_polypolynomial_seasonal_ma_polynomial_seasonal_mar!   r:   r   polynomial_trendk_trend_polynomial_trend_k_trendmax_ar_orderk_ark_ar_paramsintk_diffmax_ma_orderk_mak_ma_paramsmax_seasonal_ar_orderk_seasonal_ark_seasonal_ar_paramsk_seasonal_diffmax_seasonal_ma_orderk_seasonal_mak_seasonal_ma_params_k_diff_k_seasonal_diffmax_k_orderr   _k_exogk_exogstate_regressionstate_error
setdefaultget_loglikelihood_burnk_params
orig_endog	orig_exogr   np
asanyarrayorig_k_difforig_k_seasonal_diff_k_states_difflennobsr-   r.   super__init__ssmfilter_concentrated_time_invariantinitial_designinitial_state_interceptinitial_transitioninitial_selection
_init_keyslistkeysinitializationinitialize_default)selfendogr   r   r    r!   r5   r6   r7   r8   r"   r#   r9   r$   r:   r>   r%   r&   r'   r(   kwargsr-   r.   	__class__s                          b/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/statsmodels/tsa/statespace/sarimax.pyr{   zSARIMAX.__init__>  sT    *E.d$/u44J	L L L

 %TZ00 
 2
, !/q 1!2'>$,#6 $8!%:"'>$!2!2 ' 	2D,? 	2 1 2 2 2 "$!\16"05577!"!\16"05577*,'&*l&C&H#'+'B'G'G'I'I$*+'&*l&C&H#'+'B'G'G'I'I$ 
(.@.L.L+t|!%!6!;!;!=!= J+	:1%(mmJ+	:1"j>"j9:$(J$C!">!#455"j>"j9:$(J$C!
 { $ 4 ( 	D$2J 	D<405555A5555 C D D D DI(:: I(::Q>@ @=A$)d.@"@A"E"E+ " !  ,D11t l
 ID$4I9I 	 ##MD(8MT\A=M 	
   	*T]a%7%7%)D" =' 	'.1FF& 'H  	%$H t}q())#a<  	%T%A 	%$H
   	80$777 $*::.BD#I#I  t//%&(,(ABM "# D**++	, 	  	*MMT\)MM  t,, 	)M%((E  <$($9!$ 	&L1 5 9 9DL$%D! L4043HHH 	 JJ	   		
x(	
 	
FL	
 	
 	

 ! 	0+/DH( <!s4#899A==',DH$ "0&*&B"#!%!8 $ 6! 	-*,DH&' 	 , , ,
 /36;;==.A.AB 	B 8"*##%%%%% +*    c                    t                                                      \  }}| j        ri| j        dk    s| j        dk    rR|j        d         }t          |                                | j        | j        | j                  }|3t          |                                | j        | j        | j                  }| j	        
                    ||          \  | j	        _        | j	        _        | j	        j        j        d         }| j	        j        $| j	        j        ||z
  d          | j	        j        d<   | j        ]| j        rt#          j        d|dz             | _        n7| j        r| j        d ||z
            | _        n| j        ||z
  d          | _        |j        d         | _        t+          | j        | j        | j        | j                  | _        ||fS )Nr   
row_labelsr   )startstop)rz   prepare_datar8   ru   rv   shaper   rD   r=   data_convert_endog_exogr   r   r   _cache_index_index_int64pd
RangeIndex_index_generatedry   r   rR   rU   r:   _trend_data)r   r   r   orig_length
new_lengthr   s        r   r   zSARIMAX.prepare_data#  s   gg**,,t $ 	Iq  D$=$A$A+a.Kt'72D4IK KEDIIKK)9 5t7LN N
 	--eT:: ,DIOTY^ .q1Jy#/I(z)A)B)BC 	 .{&$ I"$-aj1n"M"M"MDKK* I"&+.Jz1I/J.J"KDKK"&+kJ.F.G.G"HDK KN	 .!4=$)T=NP P d{r   c                    t                                                       t          j        | j                  d         dd         | _        t          j        | j                  d         dd         | _        t          j        | j                  d         dd         | _	        t          j        | j
                  d         dd         | _        | j        }|| j        z   | j        z   }| j        }| j        st          j        d|||f         | _        nt          j        d|||f         | _        |dz  }|| j        z   | j        z   }d}| j        st          j        d|||f         | _        nt          j        d|||f         | _        | j        rT| j        rOt          j        | j                  }d|d         | j         d         |d         | j         d         f| _        dS dS dS )z
        Initialize the SARIMAX model.

        Notes
        -----
        These initialization steps must occur following the parent class
        __init__ function calls.
        r   r   Nr1   r2   r/   r4   )rz   
initializers   nonzerorC   _polynomial_ar_idxrH   _polynomial_ma_idxrL   _polynomial_seasonal_ar_idxrP   _polynomial_seasonal_ma_idxrw   rW   r_   r9   s_transition_ar_params_idxr\   rc   selection_ma_params_idxdesign_ma_params_idxrk   r6   diag_indicesr.   ri   _exog_variance_idx)r   	start_rowend_rowcolidxr   s        r   r   zSARIMAX.initializeM  s    	
 #%*T-?"@"@"CABB"G"$*T-?"@"@"CABB"G+-:',
 ,

,RR,( ,.:',
 ,

,RR,( '	di'$*<<!+ 	lIg$5s:; ))
 lC7)::; ) 	Q	di'$*<<+ 	k9W#4c9: ((
 hYw%667 %
   	?T%A 	?/$-00C'2CFDL=>>4J'*1vt|mnn'='?D###	? 	? 	? 	?r   c                    || j         j        }| j        rd}n(d}| j        | j        }| j        r
|| j        z  }|| _        t          | j        |          }| j        r|	                    d| j
        f|           |	                    | j
        | j
        | j        z   fd           |	                    | j
        | j        z   | j
        | j        z   | j        z   f|           n|	                    d|           || j         _        dS )zInitialize defaultNdiffuseapproximate_diffuse)approximate_diffuse_variancer   
stationary)r|   r+   r>   ro   r-   r"   rh   r,   r   setrw   ri   r   )r   r   diffuse_typek_diffuse_statesinits        r   r   zSARIMAX.initialize_default  sA   '/+/8+D(! 
	;$LL0L '/#'= , 6$5$*:'M)EG G G $ 	)HHa,-|<<<HHd))DM9;!# # # HHd)DM9)DM9DLHJ!# # # # HHT<((("&r   c                    t           j        dg| j        z  dg| j        dz
  z  dgz   | j        z  dg| j        z  dg| j        dz
  z  f         }t          |          dk    rt           j        d         }| j        r| j        dk    rnt           j	        t          j
        t          j        || j                  |j        d         | j        f          j        | j        f         j        dddddf         }n| j        j        dddddf         }|S )zInitial design matrixr   r   N)rs   r_re   r=   rf   rl   rh   rx   rk   c_reshaperepeatry   r   Tr   )r   r/   s     r   r   zSARIMAX.initial_design  s!    C$,SD)A-.!48MMC$""QC4=1+<$=>
 v;;!U1XF
   
	1}q  J	&$)44a$)4  I	 D!!!QQQJ  T111aaaZ0r   c                     | j         dk    r!t          j        | j        | j        f          }nt          j        | j        f          }|S )zInitial state intercept vectorr   )rU   rs   zerosr-   ry   )r   r0   s     r   r   zSARIMAX.initial_state_intercept  sE    
 =1 hty'ABBOO h'788Or   c                    t          j        | j        | j        f          }| j        rP| j         }t          j        | j                  ||d|df<   | j        | j        z    }| j        dk    r| j         nd}n
| j         }d}| j        dk    rSt          | j                  |||||f<   | j        r/t          j	        t          | j                            |||||f<   | j
        dk    rt          | j                  j        }d|d<   t          | j
                  D ]`}| j        || j        z  z   }| j        |dz   | j        z  z   }||||||f<   || j
        dz
  k     rd|||| j        z   dz
  f<   d||| j        f<   a| j        dk    rtt          j        | j                  }d||<   | j        dk    r6| j        }| j        }dg| j        dz
  z  dgz   | j
        z  |d| j        ||f<   | j        }d|d| j        |f<   |S )zInitial transition matrixNr   r   )r   r*   )rs   r   r-   rk   ri   eyerh   r   r9   	transposerf   r=   r   rangere   rw   triu_indices)r   r1   r   endseasonal_companiondr   columns           r   r   zSARIMAX.initial_transition  s|    Xt}dm<==
   	\ME)+)=)=Juvvuvv~& lT]23E#'<!#3#34<--CC ]NEC =1/?/N/NJuSy%)+,+ 35<$T]334 4
59eCi/0  1$$!1$2G!H!H!J()u%4011 ; ;q4+@'@@la!et/D%DD 4F
59eCi/0 t,q000IJJucD,A&AA&EEF :;
5$"5566 <!/$,//CJsO$q(()SD1A56!<)* =DL=%)34 (F01J}}f,-r   c                 (   | j         r| j        s| j        dk    rt          j        dg| j        z  dg| j        dk    z  dg| j        dz
  z  dgd| j        z
  | j        z  z  f         dddf         }t          |          dk    r t          j
        | j        | j        f          }nnt          j
        | j        df          }nRt          j
        | j        | j        f          }| j        dk    rd|d<   t          | j        dd          D ]}d|| | f<   |S )zInitial selection matrixr   r   N)r   r   r*   )rk   r6   r.   rs   r   rw   rh   r7   ri   rx   r   r-   r   )r   r2   is      r   r   zSARIMAX.initial_selection  s;    % 	&$*F 	&}q  EC4./C4=1,-sdma6G/HCA 33t|CDE !!T'		 y>>Q&& "$-)G H HIHdmQ%788		$-!?@@I}q  "#	$4<B// & &$%	1"qb&!!r   c                 "     | j         |fd|i|S )Nr   )_clone_from_init_kwds)r   r   r   r   s       r   clonezSARIMAX.clone,  s"    )t)%EEdEfEEEr   c                 "    dt           t          fiS )Nfit)SARIMAXResultsSARIMAXResultsWrapperr   s    r   _res_classeszSARIMAX._res_classes/  s    (=>??r   r   c                    d|z  }t          ||z   |          }	|dk    rdn)t          |                                d                   dz
  }
|dk    rdn)t          |                                d                   dz
  }d }||z   |z   dk    r	 |dk    rf| |d          }t          | |d          }t          j                            |                              |          }|t	          j        ||          z
  }| |	d          }t	          j        |j	        d         df          }|dk    r:|t          d          t          j        ||d |	dk    r|	 nd d d f         f         }|dk    rQ|                                d         dd          dz
  }t          j        |t          | |          |	d |f         f         }|dk    rT|                                d         dd          dz
  }t          j        |t          ||          |	|z
  d |f         f         }t          j                            |                              |          }|t	          j        ||          z
  }n# t          $ r |d|z  }nd}t          d	|z             t	          j        ||z   |z   | j        
          }t          |           dk    r"t	          j        |dz  dz   | j        
          }nEt          j        t	          j        |dz  | j        
          | t	          j        |           z
  f         }Y nw xY wg }g }g }g }d}|dk    r||||z            }||z  }|dk    r|||
|z            }||
z  }|dk    r||||z            }||z  }|Ut          |          t          d|          k    r ||d          dz                                  }nt	          j        |           }||||fS )N   r   r   both)trimz-Trend data must be provided if `k_trend` > 0.z for %s zqToo few observations to estimate starting parameters%s. All parameters except for variances will be set to zeros.)dtype)rg   rx   r   r
   rs   linalgpinvdotemptyr   r?   r   r   r   r   onesr   meanvar)r   rW   rC   r\   rH   rS   
trend_datawarning_descriptionkrk_params_ark_params_ma	residualsYX	params_arcolsparamsparams_trend	params_maparams_varianceoffsets                         r   _conditional_sum_squaresz SARIMAX._conditional_sum_squares3  sA    HD$199aa#m.C.C.E.Ea.H*I*IA*M199aa#m.C.C.E.Ea.H*I*IA*M	$; 1$$20 !88abb	Auaf555A "	q 1 1 5 5a 8 8I !BF1i$8$8 8I !""IHagaj!_--Q;;!)( *; < < <a,DAEEqbbt,Daaa,G!HHIA!88(002215abb9A=Dat!4!4QRRX!>>?A!88(002215abb9A=Da	4!8!81t!DDEA **..q11q& 1 11		 0 0 0&2*36I*I''*,' !45 6 6 6
 'D.4"7u{KKKu::??
 !#a!(;5; O O OII "qDDD./!0I%0. 		 Q;;!&6)9"9:LgF!88vkF&::;Ik!F!88vkF&::;Ik!F 9~~A{ 3 333#,[\\#:a#?"E"E"G"G"$&--i! 	!s   GI   CL$#L$c           
      
	   | j         }| j        s| j        dk    s| j        dk    rpt	          | j        | j        | j        | j                  }| j        't	          | j        | j        | j        | j                  }nd}|d|j        d         ddf         }n;| j        	                                }| j        | j        	                                nd}|
                                }t          j        t          j        |                    rCt          j        |          
                                 }||         }|||         }|||         }g }| j        dk    rJt          j                            |                              |          }|t          j        ||          z
  }| j        rg }|                     || j        | j        | j        | j        | j        |d          \  }}}}	| j        dk    o)| j        o"t5          t          j        d| f                    }
|
rt9          d           |dz  }| j        dk    o(| j        o!t5          t          j        d|f                    }|rt9          dt<                     |dz  }|                     || j        | j         | j!        | j"        d          \  }}}}| j        dk    o)| j        o"t5          t          j        d| f                    }|rt9          d	           |dz  }| j!        dk    o(| j        o!t5          t          j        d|f                    }|rt9          d
           |dz  }g }| j        r| j#        rdg| j        z  }| j$        rtK          |	          tL          u r}tO          |	          dk    rjtK          |          tL          u rtO          |          dk    s|}	n>| j        dk    rt          j(        ||          }	nt          j(        ||          | j)        z  }	| j*        rdng }t          j+        t          j,        |	                    }	|	j-        r(t          j+        t]          |	d         d                    }	| j/        rg }	t          j        |||||||||	f	         S )zG
        Starting parameters for maximum likelihood estimation
        r   NzARMA and trend)r   r   z\Non-stationary starting autoregressive parameters found. Using zeros as starting parameters.zPNon-invertible starting MA parameters found. Using zeros as starting parameters.zseasonal ARMAzSNon-stationary starting seasonal autoregressive Using zeros as starting parameters.zSNon-invertible starting seasonal moving average Using zeros as starting parameters.g|=)0r   r8   re   rf   r   r   r=   r   r   rD   squeezers   anyisnanri   r   r   r   rk   r   rW   rC   r\   rH   rU   r"   r   r   r   r#   UserWarningr_   rL   rc   rP   r6   rl   typer   rx   innerry   r5   
atleast_1darraysizerg   r$   )r   r   r   r   maskparams_exogr   r   r   r   
invalid_ar
invalid_ma_params_seasonal_arparams_seasonal_maparams_seasonal_varianceinvalid_seasonal_arinvalid_seasonal_maparams_exog_varianceparams_measurement_variances                       r   start_paramszSARIMAX.start_params  s1    %
' 	G<!t4q88T\.0EG GEy$DIt| 143HJ J #OU[^OQQQ$67JJJOO%%E'+y'<49>>###$D 6"(5//"" 	.HUOO++---D$KEDz%'-
 <!)....22599KBF4555E  	K !9949d0$)z 0 : 2 2	y)	 IM 4%4beA	zM2333 	
  	 ? @ @ @NI
 IM 3&3beAyL1222 	
  	 89DF F FNI ))t)4+F"D$?$3 * 5 5 	L13K " =%=beA(:':$:;<<< 	
  	$ 8 9 9 9!#
 " <&<beA'9$9:;;; 	
  	$ 8 9 9 9!#  "  	6T%A 	6$%3#5  	Eo!6!6$!>!>O$$))122d::011Q66":!!"$(5%"8"8"$(5%"8"849"D+/+A&Iaar# -(A(ABB 	L mC0BE,J,JKKO ! 	! O u '

 
	
r   c                    d}| j         dk    r | j         dk    r|rdnd}n|rdnd| j         z  }d}| j        dk    r/| j        dk    r|rdnd	| j        z  }n|rd
nd| j        | j        fz  }| j        }|r,| j         dk    r!| j        dk    r|rdnd||| j        j        fz  S |r | j         dk    r|rdnd|| j        j        fz  S |r | j        dk    r|rdnd|| j        j        fz  S | j        j        S )zNames of endogenous variablesr   r   r   z\DeltaDz	\Delta^%dzD%dz	\Delta_%dzDS%dz\Delta_%d^%dzD%dS%dz%s%s %sz%s.%s.%sz%s %sz%s.%s)rZ   ra   r=   r8   r   ynames)r   latexr   seasonal_diff
endog_diffs        r   endog_nameszSARIMAX.endog_names  sl    ;??{a$)2yys(-85DKG!###q((27"C,,V"&"7"9 6;"H//"&"68M!N"O-
 
	$$+//d.BQ.F.F"'7YYZ=$)*:;< = 	$DK!OO %2WW749+,- . 	$D0144 %2WW7"DI$456 7 9##r   )	r!   r   armaseasonal_arseasonal_maexog_variancemeasurement_variancevariancec                 |    | j         fd| j        D             }d|v r| j        s|                    d           |S )z
        List of parameters actually included in the model, in sorted order.

        TODO Make this an dict with slice or indices as the values.
        c                 ,    g | ]}|         d k    |S )r    ).0r   model_orderss     r   
<listcomp>z'SARIMAX.param_terms.<locals>.<listcomp>I  s3     
 
 
E"Q&& &&&r   r   )r  params_completer7   remove)r   r   r  s     @r   param_termszSARIMAX.param_terms@  sd     (
 
 
 
#3
 
 
 VD$7MM&!!!r   c                 <    | j         }| j        fd|D             S )zq
        List of human readable parameter names (for parameters actually
        included in the model).
        c                 *    g | ]}|         D ]}|S r  r  )r  paramnamemodel_namess      r   r  z'SARIMAX.param_names.<locals>.<listcomp>\  s?     
 
 
K<N
 
48D
 
 
 
r   )r   r%  )r   params_sort_orderr%  s     @r   param_nameszSARIMAX.param_namesT  sA     !,&
 
 
 
/
 
 
 	
r   c                       j         } j        s| j         j        z   j        z   z  }d t          |          D             } j        dk    r* j        r#| fdt           j                  D             z  }|S )Nc                     g | ]}d |z  S )zstate.%dr  )r  r   s     r   r  z'SARIMAX.state_names.<locals>.<listcomp>j  s    <<<Aa<<<r   r   c                 0    g | ]}d j         |         z  S )zbeta.%s)
exog_names)r  r   r   s     r   r  z'SARIMAX.state_names.<locals>.<listcomp>m  s5     4 4 4  $/!"44 4 4 4r   )rh   r8   r=   rf   re   r   ri   rk   )r   k_ar_statesnamess   `  r   state_nameszSARIMAX.state_names`  s     m' 	*D1D4II L) *K<<{););<<<<! 5 4 4 4 4$T\224 4 4 4E r   c                    | j         | j        | j        | j        | j        | j        | j        | j        z   | j        | j        z   | j        r| j        r| j        ndt          | j	                  t          | j
        o| j                   dS )zE
        The orders of each of the polynomials in the model.
        r   r!   r   r  r  r  r  
reduced_ar
reduced_mar  r  r  )rU   ri   rW   r\   r_   rc   rk   r6   rY   r5   rl   r$   r   s    r   r  zSARIMAX.model_ordersr  s     ]L))--)d&88)d&88%O*.*FOT\\MN$'(>$?$?D,KT5K1KLL
 
 	
r   c                 .    |                      d          S )zH
        The plain text names of all possible model parameters.
        Fr  _get_model_namesr   s    r   r%  zSARIMAX.model_names  s    
 $$5$111r   c                 .    |                      d          S )zC
        The latex names of all possible model parameters.
        Tr4  r5  r   s    r   model_latex_nameszSARIMAX.model_latex_names  s    
 $$4$000r   c                 j   d d d d d d d d d d d d}| j         dk    r|rdnd}g |d<   | j                                        d         D ]d}|dk    r|d                             d           $|dk    r|d                             d           F|d                             ||z             e| j        dk    r
| j        |d	<   | j        dk    rS|rd
nd}g |d<   | j                                        d         dd          D ] }|d                             ||z             !| j        dk    rS|rdnd}g |d<   | j	                                        d         dd          D ] }|d                             ||z             !| j
        dk    rS|rdnd}g |d<   | j                                        d         dd          D ] }|d                             ||z             !| j        dk    rS|rdnd}g |d<   | j                                        d         dd          D ] }|d                             ||z             !| j        dk    s| j
        dk    rpt          j        | j        | j                   x}	}	|rdnd}g |d<   |	                                d         dd          D ] }|d                             ||z             !| j        dk    s| j        dk    rmt          j        | j	        | j                  }
|rdnd}g |d<   |
                                d         dd          D ] }|d                             ||z             !| j        r1| j        r*| j        s|rdndn|rdndfd | j        D             |d!<   | j        r| j        s|rd"nd#}n|rd$nd%}|g|d&<   | j        r| j        s|rd'nd(}|g|d)<   |S )*Nr0  r   zt_%dztrend.%dr!   	interceptr   driftr   z	$\phi_%d$zar.L%dr  z$\theta_%d$zma.L%dr  z$\tilde \phi_%d$zar.S.L%dr  z$\tilde \theta_%d$zma.S.L%dr  z	$\Phi_%d$zar.R.L%dr1  z$\Theta_%d$zma.R.L%dr2  z$\sigma_\text{%s}^2$zvar.%sz%$\sigma_\text{%s}^2 / \sigma_\zeta^2$zsnr.%sc                     g | ]}|z  S r  r  )r  	exog_nameexog_var_templates     r   r  z,SARIMAX._get_model_names.<locals>.<listcomp>  s,     & & &2;!I-& & &r   r  z$\sigma_\eta^2$zvar.measurement_errorz $\sigma_\eta^2 / \sigma_\zeta^2$zsnr.measurement_errorr  z$\sigma_\zeta^2$sigma2r  )rU   rR   r   appendri   r+  rW   rC   r\   rH   r_   rL   rc   rP   rs   polymulrk   r6   r$   r5   rl   )r   r  r-  trend_templater   ar_templatema_templateseasonal_ar_templateseasonal_ma_templatereduced_polynomial_arreduced_polynomial_mameas_var_tplvar_tplr>  s                @r   r6  zSARIMAX._get_model_names  s*   !$(
 
 =1',<VV*NE'N*2244Q7 > >66'N))+6666!VV'N))'2222'N)).1*<==== <! OE&M 9q==*/=,,XKE$K'//11!4QRR8 4 4d"";?3333 9q==,1?..xKE$K'//11!4QRR8 4 4d"";?3333 !!(-=$$: ! $&E- 088::1=abbA F Fm$++,@1,DEEEE !!*/?&&Z ! $&E- 088::1=abbA F Fm$++,@1,DEEEE 9q==D.22=?Z"D$?> > = !$9 +0?,,ZK"$E,*2244Q7; < <l#**;?;;;; 9q==D.22$&J"D$?% %! -2A..zK"$E,*2244Q7; < <l#**;?;;;;   
	T%A 
	) #AF &3%=%=*2 "! DI "??! "& & & &?C& & &E/"
 ! 	;) 2+0M''6M  ?D 1::0  .:NE()  	*D$: 	*.3A**G!(	E*r   c                    t          j        |d          }t          j        |j        |j                  }dx}}| j        dk    r#|| j        z  }|||         |||<   || j        z  }| j        r#|| j        z  }|||         |||<   || j        z  }| j        dk    rG|| j        z  }| j	        rt          |||                   |||<   n|||         |||<   || j        z  }| j        dk    rH|| j        z  }| j        rt          |||                    |||<   n|||         |||<   || j        z  }| j        dk    rG|| j        z  }| j	        rt          |||                   |||<   n|||         |||<   || j        z  }| j        dk    rH|| j        z  }| j        rt          |||                    |||<   n|||         |||<   || j        z  }| j        r-| j        r&|| j        z  }|||         dz  |||<   || j        z  }| j        r||         dz  ||<   |dz  }|dz  }| j        r| j        s||         dz  ||<   |S )a  
        Transform unconstrained parameters used by the optimizer to constrained
        parameters used in likelihood evaluation.

        Used primarily to enforce stationarity of the autoregressive lag
        polynomial, invertibility of the moving average lag polynomial, and
        positive variance parameters.

        Parameters
        ----------
        unconstrained : array_like
            Unconstrained parameters used by the optimizer.

        Returns
        -------
        constrained : array_like
            Constrained parameters used in likelihood evaluation.

        Notes
        -----
        If the lag polynomial has non-consecutive powers (so that the
        coefficient is zero on some element of the polynomial), then the
        constraint function is not onto the entire space of invertible
        polynomials, although it only excludes a very small portion very close
        to the invertibility boundary.
        r   ndminr   r   )rs   r   r   r   r   rU   r7   ri   rX   r"   r   r]   r#   r_   r`   rd   rk   r6   r5   rl   r$   )r   unconstrainedconstrainedr   r   s        r   transform_paramszSARIMAX.transform_params  s5   6 a888h}2M4GHH =14= C%259%=Kc	"T]"E  	"4<C%259%=Kc	"T\!E a4##C( B3M%)4LMM E#I&& *7uSy)AE#I&T%%E a4##C) B4]595MNNN E#I&& *7uSy)AE#I&T%%E !!4,,C( B3M%)4LMM E#I&& *7uSy)AE#I&T..E $q((4,,C) B4]595MNNN E#I&& *7uSy)AE#I&T..E   	"T%A 	"4<C%259%=q%@Kc	"T\!E! 	!.u!5q!8KQJE1HC 	9D$: 	9!.u!5q!8K r   c                    t          j        |d          }t          j        |j        |j                  }dx}}| j        dk    r#|| j        z  }|||         |||<   || j        z  }| j        r#|| j        z  }|||         |||<   || j        z  }| j        dk    rG|| j        z  }| j	        rt          |||                   |||<   n|||         |||<   || j        z  }| j        dk    rH|| j        z  }| j        rt          |||                    |||<   n|||         |||<   || j        z  }| j        dk    rG|| j        z  }| j	        rt          |||                   |||<   n|||         |||<   || j        z  }| j        dk    rH|| j        z  }| j        rt          |||                    |||<   n|||         |||<   || j        z  }| j        r-| j        r&|| j        z  }|||         dz  |||<   || j        z  }| j        r||         dz  ||<   |dz  }|dz  }| j        r| j        s||         dz  ||<   |S )a  
        Transform constrained parameters used in likelihood evaluation
        to unconstrained parameters used by the optimizer

        Used primarily to reverse enforcement of stationarity of the
        autoregressive lag polynomial and invertibility of the moving average
        lag polynomial.

        Parameters
        ----------
        constrained : array_like
            Constrained parameters used in likelihood evaluation.

        Returns
        -------
        constrained : array_like
            Unconstrained parameters used by the optimizer.

        Notes
        -----
        If the lag polynomial has non-consecutive powers (so that the
        coefficient is zero on some element of the polynomial), then the
        constraint function is not onto the entire space of invertible
        polynomials, although it only excludes a very small portion very close
        to the invertibility boundary.
        r   rL  r   g      ?)rs   r   r   r   r   rU   r7   ri   rX   r"   r   r]   r#   r_   r`   rd   rk   r6   r5   rl   r$   )r   rO  rN  r   r   s        r   untransform_paramszSARIMAX.untransform_paramsn  s5   6 h{!444!2K4EFF =14= C'259'=M%)$T]"E  	"4<C'259'=M%)$T\!E a4##C( B5k%)6LMM eCi(( ,7uSy+AeCi(T%%E a4##C) B5{597M6MNN eCi(( ,7uSy+AeCi(T%%E !!4,,C( B5k%)6LMM eCi(( ,7uSy+AeCi(T..E $q((4,,C) B5{597M6MNN eCi(( ,7uSy+AeCi(T..E   	"T%A 	"4<C'259'=s'BM%)$T\!E! 	#.u#5s#:M% QJE1HC 	;D$: 	;#.u#5s#:M%  r   c           
         t                                          |           | j        }dd| j        dfdd| j        dfdd| j        dfd	d
| j        dfg}|D ]x\  }}}}t          ||         pg           }|                    |          }	t          |                    |                    dk    }
|r|
r|	st          d|d|d|d          yd S )Nr  autoregressivez`enforce_stationarity=True`r  zseasonal autoregressiver  zmoving averagez`enforce_invertibility=True`r  zseasonal moving averager   zCannot fix individual z parameters when z. Must either fix all z parameters or none.)
rz   _validate_can_fix_paramsr%  r"   r#   r   
issupersetrx   intersectionr?   )r   r'  r%  itemsr$  title	conditioncondition_descr-  fix_allfix_anyr   s              r   rU  z SARIMAX._validate_can_fix_params  s9   ((555& #T%>-/5)+HJ#T%?.05*,JLM 7< 	L 	L2D%ND)/R00E!,,U33G+22599::Q>G LW LW L j-2UUNNNEEE"K L L L	L 	Lr   c                 `
   |                      |||          }d}d}d}d}d}	d}
d}d}d}dx}}|| j        z  }|||         }|| j        z  }| j        r|| j        z  }|||         }|| j        z  }|| j        z  }|||         }|| j        z  }|| j        z  }|||         }|| j        z  }|| j        z  }|||         }	|| j        z  }|| j        z  }|||         }
|| j        z  }| j        r%| j	        r|| j        z  }|||         }|| j        z  }| j
        r||         }|dz  }|dz  }| j        r| j        s||         }| j        dk    r\| j        j        |j        k    r| | j        | j        <   n6| j        j                            |j                  }| || j        <   || _        | j        dk    rZ| j        j        |j        k    r|| j        | j        <   n5| j        j                            |j                  }||| j        <   || _        | j        dk    rY| j        }| j        j        |j        k    r|	 | j        |<   n1| j        j                            |j                  }|	 ||<   || _        | j        dk    rW| j        }| j        j        |j        k    r|
| j        |<   n0| j        j                            |j                  }|
||<   || _        | j        dk    r!t9          j        | j        | j                   }n| j         }| j        dk    r t9          j        | j        | j                  }n| j        }| j        r,t9          j        | j        |          dddf         | j         d<   | j        dk    rt9          j        | j!        |                              |j                  }| j"        s|| j         d| j#        ddf<   nZ| j"        r|t9          j$        |           z  }| j        r | j         xj%        |dddf         z  c_%        n|dddf         | j         d<   | j
        r
|| j         d<   | j        dk    s| j        dk    r|dd         | j         | j&        <   nL| j         j'        j        |j        k    s2| j         d         j                            |j                  | j         d<   | j        dk    s| j        dk    r6| j"        s|dd         | j         | j(        <   n|dd         | j         | j)        <   | j*        dk    r)| j        s|| d	<   | j        r| j	        r|| j         | j+        <   |S )
a  
        Update the parameters of the model

        Updates the representation matrices to fill in the new parameter
        values.

        Parameters
        ----------
        params : array_like
            Array of new parameters.
        transformed : bool, optional
            Whether or not `params` is already transformed. If set to False,
            `transform_params` is called. Default is True..

        Returns
        -------
        params : array_like
            Array of parameters.
        transformedincludes_fixedNr   r   obs_interceptr0   )obs_covr   r   r1   r3   ),handle_paramsrU   r7   ri   rX   r]   r`   rd   rk   r6   r5   rl   r$   rW   rE   r   r   realastyper\   rI   r   r_   r   rM   rc   r   rQ   rs   rA  r   r   r|   r   r9   rw   sumrb  r   r1   r   r   r.   r   )r   r   r`  ra  complex_stepr   r   r   r   r  r  r  r  r   r   r   rC   rH   r   rL   rP   rG  rH  r   s                           r   updatezSARIMAX.update  sI   * ##F3A $ C C 		!!#&*# t}eCi( 	"4<C s+KT\!Et59%	!!t59%	!!t((#E#I.**t((#E#I.**  	"T%A 	"4<C#)%)#4 T\!E! 	*0-'QJE1HC 	,D$: 	,$UmO
 9q=="(FL88@Iz#D$;<< $ 3 8 ? ? M M:Cd56&3#9q=="(FL88?H#D$;<< $ 3 8 ? ? M M9Bd56&3#!!2C+1V\AA5G4G,S11 05<<V\JJ ' 0B.A&s+/E,!!2C+1V\AA4F,S11 05<<V\JJ ' /A&s+/E, !!%'Z#T%A& & %!! &*%8$8!!!$&J#T%A% %!! %)$7!  	P(*ty+(F(FtQQQw(ODH_% =16$*L99@@NND/ >FJ*D,?BCC / ;BF$9#9:::D & >H**d47m;*** 15T111WDH_- ! 	D(CDH_% 9q==D.226KABB6ODHT233$*fl:: &*Xl%;%@%G%G& &DH\" 9q==D.22/ P)!""- 566 7LABB6O23 =1) :*9&'$ I)E I4H01r   c                    |                      ||          }| j        rh| j        j        j        d         |z   }t          j        || j        f          }	|.t
          j        | j        j	        j
        |j
        f         j
        }
n d}
nt          j        || j        f          }	|}
|i }| j        s.| j        dk    r#|                    d| j        | j        z              |                    dd            | j        d|	|
d|}|                    |||           | j        j                                        D ]}|dk    s||v rt)          | j        |          }t)          |j        |          }|j        d	         }|j        d	         }|d
k    s;|d
k    s5|d
k    r?| j        d
k    r4t          j        |d         |d         k              r|d| df         ||<   |S )z
        Get time-varying state space system matrices for extended model

        Notes
        -----
        We need to override this method for SARIMAX because we need some
        special handling in the `simple_differencing=True` case.
        r   Nr:   r(   F)r   r   r_  obsr*   r   ).r   .r  )_validate_out_of_sample_exogr8   r   rq   r   rs   r   k_endogr   rr   r   rS   rm   r:   ry   r   ri  r|   shapesr   getattrr   )r   r   r   out_of_sampleextend_kwargsr`  ra  r   ry   	tmp_endogtmp_exog
mod_extendr$  originalextendedsoses                    r   $_get_extension_time_varying_matricesz,SARIMAX._get_extension_time_varying_matrices  s    00}EE # 		9'-a0=@D$!566I5!4!6!>?A-!>??IH  M' 	?DL1,<,<$$ 1DI =? ? ?  !95AAATZ =(= =.;= =
&k)7 	 	: 	: 	:
 HO((** 
	> 
	>Du}}tx..Hz~t44H#B#Ba266!GG	QF8F+x/??@@ !/'m^__(<=tr   )Nr   r   NFFTFTTFFr   FNNr   TN)r   NN)F)TFF)NTF)!__name__
__module____qualname____doc__r{   r   r   r   propertyr   r   r   r   r   r   staticmethodr   r	  r  r  r   r'  r.  r  r%  r8  r6  rP  rR  rU  ri  ry  __classcell__r   s   @r   r   r      s       \ \| 0948BG:?BFBG@DCGc& c& c& c& c& c&J( ( ( ( (T6? 6? 6? 6? 6?p$' $' $' $'L   X8   X = = X=~   X0F F F F @ @ X@ FJ59W! W! W! \W!r L
 L
 XL
\ $ $ $ X$<  O
   X& 	
 	
 X	
   X" 
 
 X
& 2 2 X2 1 1 X1p p p pdf f fPf f fPL L L L L. ?D!} } } }@ >B-25 5 5 5 5 5 5 5r   r   c                   "    e Zd ZdZd fd	Zd fd	Zed             Zed             Zed             Z	ed             Z
ed	             Zed
             Zed             Zed             Z eej        j                  d fd	            Z xZS )r   aq  
    Class to hold results from fitting an SARIMAX model.

    Parameters
    ----------
    model : SARIMAX instance
        The fitted model instance

    Attributes
    ----------
    specification : dictionary
        Dictionary including all attributes from the SARIMAX model instance.
    polynomial_ar : ndarray
        Array containing autoregressive lag polynomial coefficients,
        ordered from lowest degree to highest. Initialized with ones, unless
        a coefficient is constrained to be zero (in which case it is zero).
    polynomial_ma : ndarray
        Array containing moving average lag polynomial coefficients,
        ordered from lowest degree to highest. Initialized with ones, unless
        a coefficient is constrained to be zero (in which case it is zero).
    polynomial_seasonal_ar : ndarray
        Array containing seasonal autoregressive lag polynomial coefficients,
        ordered from lowest degree to highest. Initialized with ones, unless
        a coefficient is constrained to be zero (in which case it is zero).
    polynomial_seasonal_ma : ndarray
        Array containing seasonal moving average lag polynomial coefficients,
        ordered from lowest degree to highest. Initialized with ones, unless
        a coefficient is constrained to be zero (in which case it is zero).
    polynomial_trend : ndarray
        Array containing trend polynomial coefficients, ordered from lowest
        degree to highest. Initialized with ones, unless a coefficient is
        constrained to be zero (in which case it is zero).
    model_orders : list of int
        The orders of each of the polynomials in the model.
    param_terms : list of str
        List of parameters actually included in the model, in sorted order.

    See Also
    --------
    statsmodels.tsa.statespace.kalman_filter.FilterResults
    statsmodels.tsa.statespace.mlemodel.MLEResults
    Nc                     t                      j        ||||fi | t          j        | _        | j                                        | _        t          di i d| j        j	        d| j        j
        d| j        j        d| j        j        d| j        j        d| j        j        d| j        j        d| j        j        d	| j        j        d
| j        j        d| j        j        d| j        j        d| j        j        d| j        j        d| j        j        d| j        j        d| j        j        | j        j        | j        j        | j        j        | j        j        | j        j        | j        j        | j        j         d| _!        | j        j"        | _#        | j        j$        | _%        | j        j&        | _'        | j        j(        | _)        | j        j*        | _+        t          j,        | j%        | j)                  | _-        t          j,        | j'        | j+                  | _.        | j        j/        | _/        | j        j0        | _0        dx}}| j0        D ]}|dk    r| j        j        }	nF|dk    r| j        j        }	n3|dk    r| j        j1        }	n |dk    r| j        j2        }	n| j/        |         }	||	z  }tg          | d|z  | j4        ||                    ||	z  }g d}
tk          |
          6                    | j0                  D ](}tg          | d|z  t          j7        d                     )| j8        9                    ddg           d S )Nr=   r5   r6   r8   r"   r#   r9   r$   r:   r   r    rZ   ra   rW   r\   r_   rc   )rX   r]   r!   rS   rj   r7   rk   r   r  r  r  r  z
_params_%s)r  r  r  r  r  rq   rr   r  ):rz   r{   rs   infdf_residmodel_get_init_kwds
_init_kwdsr   r=   r5   r6   r8   r"   r#   r9   r$   r:   r   r    rZ   ra   rW   r\   r_   rc   rX   r]   r!   rS   rj   r7   rk   specificationrT   rR   rE   rC   rI   rH   rM   rL   rQ   rP   rA  polynomial_reduced_arpolynomial_reduced_mar  r   r`   rd   setattrr   r   
differencer   _data_attr_modelextend)r   r  r   filter_resultscov_typer   r   r   r$  r   	all_termsr   s              r   r{   zSARIMAXResults.__init__  s   KKFKKK *3355 # " " "&

 ;"&
  !="&
 &tz'I	"&

 "4:#A"&
 #DJ$C"&
 $TZ%E"&
 &tz'I"&
  !="&
 DJ3"&
 TZ%"&
 dj7"&
  dj'!"&
" tz9#"&
$ DJO%"&
& DJO'"&
( TZ5)"&
* TZ5+"&
0  :1:1 Z%z)j'"j7 $
 ;C"&
 "&
 "&
 " "J !%
 <!Z6!Z6&*j&H#&*j&H#%'Z ;&
 &
" &(Z ;&
 &
"
 !J3:1$ 	 	Dt||J*J*&&J3&&J3%d+1HCD,-t{59/EFFFQJEEJJJ		NN--d.>?? 	< 	<DD,-rx{{;;;; 	$$lK%@AAAAAr   c                 x    |                     d| j        dz               t                      j        |fd|i|S )Nr:   r   r   )rm   ry   rz   r  )r   r   r   r   r   s       r   r  zSARIMAXResults.extendd  sB    .$)a-888uww~e99$9&999r   c                 :    t          j        | j                  dz  S )zQ
        (array) Roots of the reduced form autoregressive lag polynomial
        r*   )rs   rootsr  r   s    r   arrootszSARIMAXResults.arrootsh      
 x233R77r   c                 :    t          j        | j                  dz  S )zQ
        (array) Roots of the reduced form moving average lag polynomial
        r*   )rs   r  r  r   s    r   marootszSARIMAXResults.marootso  r  r   c                     | j         }|j        sdS t          j        |j        |j                  dt          j        z  z  S )zj
        (array) Frequency of the roots of the reduced form autoregressive
        lag polynomial
        Nr   )r  r   rs   arctan2imagre  pir   zs     r   arfreqzSARIMAXResults.arfreqv  ;     Lv 	Fz!&!&))QY77r   c                     | j         }|j        sdS t          j        |j        |j                  dt          j        z  z  S )zj
        (array) Frequency of the roots of the reduced form moving average
        lag polynomial
        Nr   )r  r   rs   r  r  re  r  r  s     r   mafreqzSARIMAXResults.mafreq  r  r   c                     | j         S )z
        (array) Autoregressive parameters actually estimated in the model.
        Does not include seasonal autoregressive parameters (see
        `seasonalarparams`) or parameters whose values are constrained to be
        zero.
        )
_params_arr   s    r   arparamszSARIMAXResults.arparams       r   c                     | j         S )z
        (array) Seasonal autoregressive parameters actually estimated in the
        model. Does not include nonseasonal autoregressive parameters (see
        `arparams`) or parameters whose values are constrained to be zero.
        )_params_seasonal_arr   s    r   seasonalarparamszSARIMAXResults.seasonalarparams       ''r   c                     | j         S )z
        (array) Moving average parameters actually estimated in the model.
        Does not include seasonal moving average parameters (see
        `seasonalmaparams`) or parameters whose values are constrained to be
        zero.
        )
_params_mar   s    r   maparamszSARIMAXResults.maparams  r  r   c                     | j         S )z
        (array) Seasonal moving average parameters actually estimated in the
        model. Does not include nonseasonal moving average parameters (see
        `maparams`) or parameters whose values are constrained to be zero.
        )_params_seasonal_mar   s    r   seasonalmaparamszSARIMAXResults.seasonalmaparams  r  r   皙?c                    d}| j         j        | j         j        z   | j         j        z   dk    r| j         j        | j         j        k    r| j         j        }nt          | j         j        j                  }| j         j        | j         j        k    r| j         j        }nt          | j         j        j	                  }| j         j
        rdn| j         j        }d|||fz  }d}| j         j        | j         j        z   | j         j        z   dk    }|r$t          | j         j        | j         j        z            }	|	| j         j        k    r't          | j         j        | j         j        z            }
nt          | j         j        j                  }
t          | j         j        | j         j        z            }	|	| j         j        k    r|	}nt          | j         j        j                  }| j         j        }| j         j
        rd}dt'          |
          |t'          |          | j         j        fz  }|dk    s|dz  }| j         j        j         | | }t-                                          ||d|          S )Nr   r   z(%s, %d, %s)z(%s, %d, %s, %d)xzSARIMAX Results)alphar   rY  
model_name)r  rW   rZ   r\   rX   r   r;   ar_lagsr]   ma_lagsr8   r_   ra   rc   rY   r=   r`   seasonal_ar_lagsseasonal_ma_lagsstrr   r{  rz   summary)r   r  r   r   order_arorder_marZ   r    has_seasonaltmporder_seasonal_arorder_seasonal_mara   r  r   s                 r   r  zSARIMAXResults.summary  sX   
 :?TZ..@1DDz$*"888:?
 0 899z$*"888:?
 0 899 *8OQQdj>OF"h%AAEJ$J&'J$% 	
  	dj.1LLMMCdj555
04:3NNOO "! %))9)J$K$K!dj.1LLMMCdj,,,$'!!$()9)J$K$K! #j8Oz- $"#0"#455"#455#z:<<N B;;
,5NuNnNN
ww#!	  
 
 	
r   rz  )r  N)r{  r|  r}  r~  r{   r  r   r  r  r  r  r  r  r  r  r   r   r  r  r  s   @r   r   r     s       ) )TSB SB SB SB SB SBj: : : : : : 8 8 ^8 8 8 ^8 8 8 ^8 8 8 ^8   ^ ( ( ^(   ^ ( ( ^( Xj ())8
 8
 8
 8
 8
 *)8
 8
 8
 8
 8
r   r   c                   n    e Zd Zi Z ej        ej        e          Zi Z ej        ej	        e          Z	dS )r   N)
r{  r|  r}  _attrswrapunion_dictsr   _wrap_attrs_methods_wrap_methodsr  r   r   r   r     sR        F"$"#4#@#)+ +KH$D$%6%D%-/ /MMMr   r   ),r~  warningsr   numpyrs   pandasr   statsmodels.compat.pandasr   statsmodels.tools.toolsr   statsmodels.tools.datar   statsmodels.tools.decoratorsr   statsmodels.base.wrapperbasewrapperr  #statsmodels.tsa.arima.specificationr   statsmodels.tsa.arima.paramsr	   statsmodels.tsa.tsatoolsr
   r   r   mlemodelr   r   r   toolsr   r   r   r   r   r   r   r   r   r   r   populate_wrapperr  r   r   <module>r     s                  . . . . . . ) ) ) ) ) ) 3 3 3 3 3 3 7 7 7 7 7 7 ' ' ' ' ' ' ' ' ' D D D D D D 6 6 6 6 6 6 + + + + + + * * * * * * = = = = = = = = = =: : : : : : : : : : : : : : : : : : : :B B B B Bh B B BJ6G
 G
 G
 G
 G
Z G
 G
 G
T/ / / / /- / / /  +^ < < < < <r   