o
    0Gf                     @   s
  d Z ddl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  mZ ddlmZ d	d
lmZmZ d	dlmZmZmZ d	dlmZ d	dlm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z& G dd deZ'G dd deZ(G dd deZ)e*e)e( dS )zs
Vector Autoregressive Moving Average with eXogenous regressors model

Author: Chad Fulton
License: Simplified-BSD
    N)warn)Appender)Bunch)_is_using_pandas)	var_model)EstimationWarning   )INVERT_UNIVARIATESOLVE_LU)MLEModel
MLEResultsMLEResultsWrapper)Initialization)is_invertibleconcatprepare_exog!constrain_stationary_multivariate#unconstrain_stationary_multivariateprepare_trend_specprepare_trend_datac                       s   e Zd ZdZ				d fd	d
	Zd ddZedd Zedd Zedd Z	dd Z
dd Z fddZ		d!ddZejdd Zeejj				d" fdd	Z  ZS )#VARMAXuM  
    Vector Autoregressive Moving Average with eXogenous regressors model

    Parameters
    ----------
    endog : array_like
        The observed time-series process :math:`y`, , shaped nobs x k_endog.
    exog : array_like, optional
        Array of exogenous regressors, shaped nobs x k.
    order : iterable
        The (p,q) order of the model for the number of AR and MA parameters to
        use.
    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 a constant trend component.
    error_cov_type : {'diagonal', 'unstructured'}, optional
        The structure of the covariance matrix of the error term, where
        "unstructured" puts no restrictions on the matrix and "diagonal"
        requires it to be a diagonal matrix (uncorrelated errors). Default is
        "unstructured".
    measurement_error : bool, optional
        Whether or not to assume the endogenous observations `endog` were
        measured with error. 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.
    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.
    **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
    ----------
    order : iterable
        The (p,q) order of the model for the number of AR and MA parameters to
        use.
    trend : str{'n','c','t','ct'} or iterable
        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`.
    error_cov_type : {'diagonal', 'unstructured'}, optional
        The structure of the covariance matrix of the error term, where
        "unstructured" puts no restrictions on the matrix and "diagonal"
        requires it to be a diagonal matrix (uncorrelated errors). Default is
        "unstructured".
    measurement_error : bool, optional
        Whether or not to assume the endogenous observations `endog` were
        measured with error. 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.

    Notes
    -----
    Generically, the VARMAX model is specified (see for example chapter 18 of
    [1]_):

    .. math::

        y_t = A(t) + A_1 y_{t-1} + \dots + A_p y_{t-p} + B x_t + \epsilon_t +
        M_1 \epsilon_{t-1} + \dots M_q \epsilon_{t-q}

    where :math:`\epsilon_t \sim N(0, \Omega)`, and where :math:`y_t` is a
    `k_endog x 1` vector. Additionally, this model allows considering the case
    where the variables are measured with error.

    Note that in the full VARMA(p,q) case there is a fundamental identification
    problem in that the coefficient matrices :math:`\{A_i, M_j\}` are not
    generally unique, meaning that for a given time series process there may
    be multiple sets of matrices that equivalently represent it. See Chapter 12
    of [1]_ for more information. Although this class can be used to estimate
    VARMA(p,q) models, a warning is issued to remind users that no steps have
    been taken to ensure identification in this case.

    References
    ----------
    .. [1] Lütkepohl, Helmut. 2007.
       New Introduction to Multiple Time Series Analysis.
       Berlin: Springer.
    Nr   r   cunstructuredFTr   c
                    s*  | _ | _| _| _| _t|d  _t|d  _|dvr%td jdkr3 jdkr3td jdkrB jdkrBt	dt
 | _|	 _t j\ _ _ jjdko] jd dk _t|\ _} jdk _t|d svt|}t jd}| j  _|jd }|}| j }|
dd |
d	ttB  t j|f|||d
|
  jdks jdkr jsd j _!i  _" j# j  j"d<  j#d  j  j"d<  j#d  j  j"d<  j# j  j"d<  j dkr j# j"d< n j dkrt j# j#d  d  j"d<  j# j  j"d< t$ j"%  _&t' j j j(d  jd}|d d  _)|dd   _* jdkr> jrD jdkrPt+ j, j(f j d< t- j#}d j d| <  jdkrt- jd  j# }|d  j# |d f}d j d| < t- jd  j# }|d |d  j#  |d | j#  f}d j d| < t- j#}d j d| < |d | j#  |d f} jdkrd j d| <  jr jdkrtj.dd |d d f  _/n jdks jdkrtj.dd |d df  _/ jdkrtj.dd |d d f  _0ntj.dd ||d f  _0 j dkr/dt- j#  _1n j dkr<t2 j# _3 jrIdt- j#  _4 fdd}d}|d|\ _5}|d|\ _6}|d|\ _7}|d|\ _8}|d|\ _9}|d|\ _:}d  _;  j<g d t=|
>  7  _<d S )!Nr   r   )diagonalr   z3Invalid error covariance matrix type specification.zNInvalid VARMAX(p,q) specification; at least one p,q must be greater than zero.zcEstimation of VARMA(p,q) models is not generically robust, due especially to identification issues.initialization
stationaryZinversion_method)exogk_statesk_posdefFtrend   armaZ
regressionr   	state_covr   obs_cov)offsetstate_intercept)Zdesign)
transition)Z	selectionr)   )r$   )r%   c                    s,    j |  }tj|||  }||7 }||fS N)
parametersnps_)keyr&   lengthZparam_sliceself Alib/python3.10/site-packages/statsmodels/tsa/statespace/varmax.py_slice   s   
zVARMAX.__init__.<locals>._slice)orderr    error_cov_typemeasurement_errorenforce_stationarityenforce_invertibility)?r6   r7   r8   r9   r5   intk_ark_ma
ValueErrorr   r   r    trend_offsetr   polynomial_trendk_trendsize_trend_is_constr   k_exogmle_regressionr   r,   Z
asanyarraymaxZ_k_ordershape
setdefaultr	   r
   super__init__ssmZ_time_invariantr+   k_endogsumvaluesk_paramsr   nobs_trend_data_final_trendzerosr   Zdiag_indicesr-   _idx_state_intercept_idx_transition_idx_state_covZtril_indices_idx_lower_state_cov_idx_obs_cov_params_trend
_params_ar
_params_ma_params_regression_params_state_cov_params_obs_cov_final_exogZ
_init_keyslistkeys)r1   endogr   r5   r    r6   r7   r8   r9   r>   kwargsZ	_min_k_arrK   r   r   Z
trend_dataidxr4   r&   	__class__r0   r3   rI      s   




 
zVARMAX.__init__c                 K   s   | j |fd|i|S )Nr   )Z_clone_from_init_kwds)r1   ra   r   rb   r2   r2   r3   clone7  s   zVARMAX.clonec                 C   s   dt tfiS )Nfit)VARMAXResultsVARMAXResultsWrapperr0   r2   r2   r3   _res_classes:  s   zVARMAX._res_classesc                 C   s  t j| jt jd}t| j }| }t j	|
 dd}d }| jdkr4| jdkr4t j| j| jf }n| jdkr=| j}n| jdkrE| j}t t |rdt jt |dd }|| }|d urd|| }t d}t d}| jdksx| jdkrt j||}|t ||8 }| jdkr|d | j j}| jdkr|| jd  j}g }| jdkr| jnd}	t|}
|
j|	d dd}| jdkrt |jj }|j}| jdkr| jr| | j| j | jj | j| j| jj}t!dgt"|  }|st#d	 |d9 }g }| j$dkrKt|}|j| j$d dd}t |jj }| j%rK| | j| j$ | jj | j| j| j$j}t!dgt"|  }|sKt#d
 |d9 }| jdkr| jdks[| j&r| | j| j | jj | j| j| jj}t '| jt j(|dd }| jdkrt ||}| j&dkrt ||}| jdkr| || j)< | jdkr||| j*< | j$dkr||| j+< | j&r| || j,< | j-dkr|j./ || j0< n| j-dkrt j1|j.}|| j2  || j0< | j3r| j$dkr|j./ || j4< |S |j./ || j4< |S )NdtypeW)Zrequirementsr   r   )Zaxisn)ZmaxlagsZicr    z\Non-stationary starting autoregressive parameters found. Using zeros as starting parameters.z\Non-stationary starting moving-average parameters found. Using zeros as starting parameters.r   r   )5r,   rR   rN   Zfloat64pdZ	DataFramera   copyZinterpolateZrequireZbfillr@   rC   c_rP   r   anyZisnanZlinalgZpinvdotTrK   r;   r   VARrg   arrayparamsravelZresidr8   reshaper   r_   r   r<   r9   rD   eyerL   rX   rY   rZ   r[   r6   Zsigma_ur   r\   ZcholeskyrV   r7   r]   )r1   rw   ra   r   masktrend_paramsexog_paramsZtrendexog_params	ar_paramsr;   Zmod_arZres_arcoefficient_matricesr   	ma_paramsZmod_maZres_maZ
invertibletmpZ
cov_factorr2   r2   r3   start_params>  s   








 

zVARMAX.start_paramsc                    sf  g }j  tj ts g jdkrLtjD ]3}j d D ])}|dkr1|d |  g7 }q!|dkr?|d |  g7 }q!|d| | f g7 }q!q| fddtjD 7 }| fddtjD 7 }| fd	dtjD 7 }jd
kr| fddtjD 7 }njdkr| fddtjD 7 }j	r| fddtjD 7 }|S )Nr   zintercept.%sr   zdrift.%sztrend.%d.%sc              	      F   g | ]}t jD ]}t jD ]}d |d  |  | f qq	qS )z	L%d.%s.%sr   )ranger;   rK   .0jikendog_namesr1   r2   r3   
<listcomp>      
z&VARMAX.param_names.<locals>.<listcomp>c              	      r   )zL%d.e(%s).%sr   )r   r<   rK   r   r   r2   r3   r     r   c                    s2   g | ]}t jD ]}d j|  | q	qS )z
beta.{}.{})r   rC   formatZ
exog_namesr   r   r   r   r2   r3   r     s    r   c                       g | ]}d  |  qS )z	sigma2.%sr2   r   r   r   r2   r3   r         
r   c                    sF   g | ]}t |d  D ]}||krd |  n	d |  | q
qS )r   zsqrt.var.%szsqrt.cov.{}.{})r   r   r   r   r2   r3   r     s    
c                    r   )zmeasurement_variance.%sr2   r   r   r2   r3   r     r   )
r   
isinstancer_   r@   r   rK   r?   Znonzeror6   r7   )r1   param_namesr   r   r2   r   r3   r     sF   
	


zVARMAX.param_namesc                 C   s  t j|dd}t j|j|jd}|| j || j< | jdkrm| jrm| jdkr0t 	|| j
 d }n | jdkrPt j| jd j|jd}|| j
 || j< t ||j}|| j | j| j| j }t||\}}| || j< n|| j || j< | jdkr| jrt j| j|jd}|| j | j| j| j }t||\}}| || j< n|| j || j< || j || j< | jdkr|| j
 d || j
< n| jdkr|| j
 || j
< | jr|| j d || j< |S )	a[  
        Transform unconstrained parameters used by the optimizer to constrained
        parameters used in likelihood evaluation

        Parameters
        ----------
        unconstrained : array_like
            Array of unconstrained parameters used by the optimizer, to be
            transformed.

        Returns
        -------
        constrained : array_like
            Array of constrained parameters which may be used in likelihood
            evaluation.

        Notes
        -----
        Constrains the factor transition to be stationary and variances to be
        positive.
        r   Zndminrk   r   r   r!   r   r$   )r,   rv   rR   rF   rl   rX   r;   r8   r6   diagr\   rJ   rV   rs   rt   rY   ry   rK   r   rx   r<   r9   rz   rZ   r[   r7   r]   )r1   unconstrainedconstrainedr$   state_cov_lowercoefficientsr   variancer2   r2   r3   transform_params  sR   






zVARMAX.transform_paramsc                 C   s  t j|dd}t j|j|jd}|| j || j< | jdkrk| jrk| jdkr.t 	|| j
 }n | jdkrNt j| jd j|jd}|| j
 || j< t ||j}|| j | j| j| j }t||\}}| || j< n|| j || j< | jdkr| jrt j| j|jd}|| j | j| j| j }t||\}}| || j< n|| j || j< || j || j< | jdkr|| j
 d || j
< n| jdkr|| j
 || j
< | jr|| j d || j< |S )	a  
        Transform constrained parameters used in likelihood evaluation
        to unconstrained parameters used by the optimizer.

        Parameters
        ----------
        constrained : array_like
            Array of constrained parameters used in likelihood evaluation, to
            be transformed.

        Returns
        -------
        unconstrained : array_like
            Array of unconstrained parameters used by the optimizer.
        r   r   rk   r   r   r   r$   g      ?)r,   rv   rR   rF   rl   rX   r;   r8   r6   r   r\   rJ   rV   rs   rt   rY   ry   rK   r   rx   r<   r9   rz   rZ   r[   r7   r]   )r1   r   r   r$   r   r   Zunconstrained_matricesr   r2   r2   r3   untransform_paramsR  sR   






zVARMAX.untransform_paramsc                    s   t  | tt| j d d }dd t| j|D \}}}}}}| j	rN| j
dkrN| jdks8| j
dkrN||}t||dk}|rN|sNtd| jrt| jdkrv| js^| jdkrz||}t||dk}|rx|s|tdd S d S d S d S d S )Nr'   c                 s   s    | ]}|  V  qd S r*   )tolist)r   Zarrr2   r2   r3   	<genexpr>  s    
z2VARMAX._validate_can_fix_params.<locals>.<genexpr>r   r   zCannot fix individual autoregressive parameters when `enforce_stationarity=True`. In this case, must either fix all autoregressive parameters or none.zCannot fix individual moving average parameters when `enforce_invertibility=True`. In this case, must either fix all moving average parameters or none.)rH   _validate_can_fix_paramsr,   Zcumsumr_   r+   rM   Zarray_splitr   r8   r;   rK   
issupersetlenintersectionr=   r9   r<   )r1   r   Zix_Zar_namesZma_namesZfix_allZfix_anyrd   r2   r3   r     s6   

zVARMAX._validate_can_fix_paramsc                 C   sT  | j |||d}| jr>|| j | j| jj}t| j	dd  |}|j| j
| j< | jd ur>t| j|| j
dd | jdf< | jdkr| jsWtjd|jd}|| j
dd d f< || j | j| jj}| jri|}nt| jdd  |}| j
| j  |j7  < | jd ur| jd jdkr| j
dd | jdd f  t| j|j7  < | jr| jd u rtjtj|jd}	|	| j
dd | jdf< || j | j| j| j }
|| j | j| j| j }tj|
|f | j
| j< | jdkr|| j | j
| j< n$| jdkrtj| j
d	 j |jd}|| j || j!< t||j| j
d	< | j"r(|| j# | j
| j$< d S d S )
N)transformedincludes_fixedr   r(   r'   r   rk   r   r   r$   )%Zhandle_paramsrD   r[   ry   rK   rC   rt   r,   rs   r   rJ   rS   r^   r@   rv   rl   rX   rB   rP   rQ   stopnanrY   r;   rZ   r<   rq   rT   r6   r\   rU   rR   rF   rV   r7   r]   rW   )r1   rw   r   r   Zcomplex_stepr}   	interceptZzeror|   r   r"   r#   r   r2   r2   r3   update  st   







zVARMAX.updatec                 c   s    | j }| jdkrE|durBt|}|jdkr|dd }zt|dd | jf}W n tyA   tdt| jft|jf w || _ z	dV  W || _ dS || _ w )a8  
        Set the final state intercept value using out-of-sample `exog` / trend

        Parameters
        ----------
        exog : ndarray
            Out-of-sample `exog` values, usually produced by
            `_validate_out_of_sample_exog` to ensure the correct shape (this
            method does not do any additional validation of its own).
        out_of_sample : int
            Number of out-of-sample periods.

        Notes
        -----
        We need special handling for simulating or forecasting with `exog` or
        trend, because if we had these then the last predicted_state has been
        set to NaN since we did not have the appropriate `exog` to create it.
        Since we handle trend in the same way as `exog`, we still have this
        issue when only trend is used without `exog`.
        r   Nr!   r   zPProvided exogenous values are not of the appropriate shape. Required %s, got %s.)	r^   rC   r,   Z
atleast_1dndimry   r=   strrF   )r1   r   cache_valuer2   r2   r3   _set_final_exog   s(   



zVARMAX._set_final_exogc                    s\   |  | t j||f|||||||	|
||d
|}W d    |S 1 s'w   Y  |S )N)
measurement_shocksstate_shocksinitial_stateanchorrepetitionsr   extend_modelextend_kwargsr   r   )r   rH   simulate)r1   rw   nsimulationsr   r   r   r   r   r   r   r   r   r   rb   outrd   r2   r3   r   )  s    
zVARMAX.simulate)Nr   r   r   FTTr   r*   )TFF)
NNNNNNNNTF)__name__
__module____qualname____doc__rI   rf   propertyrj   r   r   r   r   r   r   
contextlibcontextmanagerr   r   r   r   __classcell__r2   r2   rd   r3   r      s<    d 
4

 
?SM
C

(r   c                       s   e Zd ZdZ		d fdd	ZdddZejdd Zejd	d
 Z	e
ejj		d fdd	Ze
ejj				d fdd	Z		dddZe
ejjd fdd	Z  ZS )rh   a  
    Class to hold results from fitting an VARMAX model.

    Parameters
    ----------
    model : VARMAX instance
        The fitted model instance

    Attributes
    ----------
    specification : dictionary
        Dictionary including all attributes from the VARMAX model instance.
    coefficient_matrices_var : ndarray
        Array containing autoregressive lag polynomial coefficient matrices,
        ordered from lowest degree to highest.
    coefficient_matrices_vma : ndarray
        Array containing moving average lag polynomial coefficients,
        ordered from lowest degree to highest.

    See Also
    --------
    statsmodels.tsa.statespace.kalman_filter.FilterResults
    statsmodels.tsa.statespace.mlemodel.MLEResults
    Nc                    s$  t  j|||||fi | tdi | jj| jj| jj| jj| jj| jj	| jj
| jj| jj| jj| jjd| _d | _d | _| jj
dkrft| j| jj }| jj}| jj
}	|||	 |j|||	j| _| jjdkrt| j| jj }
| jj}| jj}|
|| |j|||j| _d S d S )N)r6   r7   r8   r9   r>   r5   r;   r<   r    r@   rC   r   r2   )rH   rI   r   modelr6   r7   r8   r9   r>   r5   r;   r<   r    r@   rC   specificationZcoefficient_matrices_varZcoefficient_matrices_vmar,   rv   rw   rY   rK   ry   rt   rZ   )r1   r   rw   filter_resultsZcov_typeZcov_kwdsrb   r~   rK   r;   r   r<   rd   r2   r3   rI   S  sF   
zVARMAXResults.__init__c           
      K   s   |d ur | j | j| j|d d d}|j}|jd }|jd }n
| jd }| jd }|d| j| jj  | jj|fd|i|}t	|j
d||d|j_| jd urY|| j}	|	S || j}	|	S )	Nr   )r   ).r   ).r'   r>   r   Zknownconstantstationary_cov)get_predictionrO   Zprediction_resultspredicted_statepredicted_state_covrG   r   r>   rf   r   r   rJ   r   smoother_resultsZsmoothrw   filter)
r1   ra   r   rb   ZfcastZfcast_resultsr   Zinitial_state_covmodresr2   r2   r3   extend  s$   




zVARMAXResults.extendc                 c   s    | j }||G | jjdddf }|| j |dd|jdf | jjd|jdf< zdV  W || jjdddf< n|| jjdddf< w W d   dS 1 sSw   Y  dS )az  
        Set the final state intercept value using out-of-sample `exog` / trend

        Parameters
        ----------
        exog : ndarray
            Out-of-sample `exog` values, usually produced by
            `_validate_out_of_sample_exog` to ensure the correct shape (this
            method does not do any additional validation of its own).
        out_of_sample : int
            Number of out-of-sample periods.

        Notes
        -----
        This context manager calls the model-level context manager and
        additionally updates the last element of filter_results.state_intercept
        appropriately.
        Nr'   r(   )r   r   r   r(   r   rw   rK   )r1   r   r   r   r2   r2   r3   r     s   ."zVARMAXResults._set_final_exogc              
   c   sN   |o| j jdk}|rt| j jdd td| j jfg}| j jdkr5t| j jdd |dd g}nd}| j j| j	 d }| j j
|||d}| jjdddf }| jjdddddf }	|jj||	d |j| jdddd	}
|
jdddf | jjdddf< zdV  W |rtj| jjdddf< dS dS |rtj| jjdddf< w )
a  
        Set the final predicted state value using out-of-sample `exog` / trend

        Parameters
        ----------
        exog : ndarray
            Out-of-sample `exog` values, usually produced by
            `_validate_out_of_sample_exog` to ensure the correct shape (this
            method does not do any additional validation of its own).
        out_of_sample : int
            Number of out-of-sample periods.

        Notes
        -----
        We need special handling for forecasting with `exog`, because
        if we had these then the last predicted_state has been set to NaN since
        we did not have the appropriate `exog` to create it.
        r   r'   Nr   )r   r>   r   T)r   r   Z
return_ssm)r   rC   r   ra   r,   rR   rK   r   r>   rO   rf   r   r   r   rJ   Zinitialize_knownr   rw   r   )r1   r   out_of_sampleflagZ	tmp_endogZtmp_exogZtmp_trend_offsetZtmp_modr   r   Ztmp_resr2   r2   r3   _set_final_predicted_state  s:   "

z(VARMAXResults._set_final_predicted_stateF	predictedc                    s   |d u rd}| j j|||dd\}}	}
}| j ||
}i }| j jdkr,| j j| j |d< | |8 | ||
 t j	d|||||||d|}W d    n1 sTw   Y  W d    |S W d    |S 1 slw   Y  |S )Nr   T)Zsilentr>   )startenddynamicinformation_setindexr   r   r2   )
r   Z_get_prediction_index_validate_out_of_sample_exogr@   r>   rO   r   r   rH   r   )r1   r   r   r   r   r   r   rb   Z_startZ_endr   r   r   r   rd   r2   r3   r     s4   


zVARMAXResults.get_predictionc
                    s   |d u s|dkrd}n|dkr| j }n	| j|\}}}|dk r%| j | }|| j kr.tdt|| | j  d}| j||}| || t j|f||||||||	d|
}W d    |S 1 sdw   Y  |S )Nr   r   r   z4Cannot anchor simulation after the estimated sample.)r   r   r   r   r   r   r   r   )	rO   r   Z_get_index_locr=   rE   r   r   rH   r   )r1   r   r   r   r   r   r   r   r   r   rb   Zilocr   r   r   rd   r2   r3   r     s4   


zVARMAXResults.simulatec                 C   s   d }| j |j  }| jjdkr|dkr| jj| d  }t '}	|	|j| |	||| | j	j
|j	||||d}
W d    |
S 1 sIw   Y  |
S )Nr   )r   r   revisions_details_startstate_index)rO   r   rC   r   r   	ExitStackenter_contextr   r   r   Znews)r1   Zpreviousr   r   Zperiodsr   r   r   r   stackr   r2   r2   r3   _news_previous_results(  s$   

		z$VARMAXResults._news_previous_results皙?Tc              	      s^  ddl m | j}|jdkr|jdkrd}d|j|j}n|jdkr+d}d|j }nd}d|j }|jdkr;|d7 }|| g}|jdkrJ|d	 |j	rR|d
 t
 j ||| d}|rtt| j}d fdd	}	| jj}
| jj}| jj}| jj}| jj}g }t|
D ]}g }d}|dkr|t|||
|  |
 ||
| 7 }|dkr||
 | }|d |
 | }||t||  |||
d  7 }|dkr||
 | }|d |
 | }||t||  |||
d  7 }|dkr||t|| |d |   ||
| 7 }| jj	r#|tj| jj| d dd t|}|| | jj}t|ts:|g}d||  }|	| ||}|j| qtt| j| jj }|	| |ddd}|j| g }||gfD ]}t| }t|dkr|| qnt|}ttt|t|}t|dkr|	| |ddd}|j| |S )Nr   )summary_paramsZVARMAz({},{})ru   z(%s)ZVMAXr   zmeasurement error)alphar   
model_nameZdisplay_paramsTc                    s   | | j | | j| | j| | j| |  | f}g }t| jj| 	 D ]!}|r7d
|dd d }n|}|| jv rBd| }|| q&|d | d|dS )N.r'   z
%s (fixed)F)ZynameZxnamer   Zuse_ttitle)rw   ZbseZzvaluesZpvaluesZconf_intr,   rv   datar   r   joinsplitZfixed_paramsappend)r1   r{   r   	strip_endr   r   nameZ
param_namer   r   r2   r3   
make_tablef  s   
z)VARMAXResults.summary.<locals>.make_tabler   r!   r   zResults for equation %szError covariance matrixF)r   zOther parameters)T)Zstatsmodels.iolib.summaryr   r   r;   r<   r   rC   r@   r   r7   rH   summaryr,   Zaranger   rw   r   rK   r   rv   rN   Zconcatenater   r   r_   Ztablesr\   Zflattenset
difference)r1   r   r   Zseparate_paramsspecr   r5   r   indicesr   rK   r;   r<   r@   rC   Zendog_masksr   Zmasksr&   r   r{   r   r   tableZstate_cov_maskmZinverse_maskrd   r   r3   r   E  s   












zVARMAXResults.summary)NNr*   )NNFr   NN)NNNNNNNN)FN)r   NT)r   r   r   r   rI   r   r   r   r   r   r   r   r   r   r   r   r   r2   r2   rd   r3   rh   :  s2    
,


0
!

rh   c                   @   s0   e Zd Zi ZeejeZi Zeej	eZ	dS )ri   N)
r   r   r   _attrswrapZunion_dictsr   Z_wrap_attrsZ_methodsZ_wrap_methodsr2   r2   r2   r3   ri     s    ri   )+r   r   warningsr   Zpandasro   Znumpyr,   Zstatsmodels.compat.pandasr   Zstatsmodels.tools.toolsr   Zstatsmodels.tools.datar   Zstatsmodels.tsa.vector_arr   Zstatsmodels.base.wrapperbasewrapperr   Zstatsmodels.tools.sm_exceptionsr   Zkalman_filterr	   r
   Zmlemodelr   r   r   r   r   Ztoolsr   r   r   r   r   r   r   r   rh   ri   Zpopulate_wrapperr2   r2   r2   r3   <module>   s8    $      !   