
    M/Ph7V                    l   d Z ddlmZ ddlZddl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mZ ddlmZmZ ddlmZmZmZmZ dd	lmZ dd
lmZmZmZm Z  ddl!m"c m#Z$ ddl%m&c m"c m'Z( ddl)m*Z* ddl+m&c m"c m,Z- ddl.m/Z/ ddl0m1Z1 ddl2m3Z3 ddl4m5Z5m6Z6m7Z7 ddl8m9Z9 ddl:m;Z;m<Z<m=Z=m>Z> d Z?d dZ@ G d de-jA                  ZB G d de-jC                  ZD G d de$jE                  ZF e$jG        eFeD            G d de(jH                  ZH G d de$jE                  ZI e$jG        eIeH           dS )!z@
State Space Model

Author: Chad Fulton
License: Simplified-BSD
    )is_int_indexN)SimpleNamespace)norm)pinv_extendedBunch)PrecisionWarningValueWarning)_get_epsilonapprox_hess_csapprox_fprime_csapprox_fprime)cache_readonly)aicaiccbichqic)
PandasData   )NewsResults)SimulationSmoother)SmootherResults)INVERT_UNIVARIATESOLVE_LUMEMORY_CONSERVE)Initialization)prepare_exogconcat
_safe_condget_impact_datesc                    g }t          |          dk    rt          |d         t                    r	|d         }nt          t          | |                    }t	          t          |                     D ]7}|                    |                    | |         ||                              8|                                D ]\  }}||v rt          d|z            nTt	          t          |                     D ]7}|                    |	                    | |         ||                              8t          |          |fz   S )Nr   z7loglike() got multiple values for keyword argument '%s')len
isinstancedictziprangeappendgetitems	TypeErrorpoptuple)	namesdefaultsargskwargsoutput_argsflagsinamevalues	            c/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/statsmodels/tsa/statespace/mlemodel.py_handle_argsr6   (   sN   K 4yy1}}d1gt$$ 	+GEE UD))**Es5zz"" 	A 	AAuyyq8A;??@@@@ ;;== 	9 	9KD%v~~ !137!8 9 9 9 	9
 s5zz"" 	B 	BAvzz%(HQK@@AAAA	))    datac           	      D   d }t          |t          j        t          j        f          r|j        }|l|                     |          sYt          | dd           }t          |dd           }||||k    rt          d|d|d|d          t          d|z            d S d S )NfreqzGiven zZ does not have an index that extends the index of the model. Expected index frequency is "z", but got "z".zDGiven %s does not have an index that extends the index of the model.)r"   pdSeries	DataFrameindexequalsgetattr
ValueError)desired_indexdtatitlegiven_indexdesired_freq
given_freqs         r5   _check_indexrH   A   s    K#	2<011  i}';';K'H'H}fd;;[&$77
%)?
*** !&|||ZZZ	A B B B  ')./ 0 0 0 r7   c                       e Zd ZdZdP fd	Zd Zd Zd Zd Zd Z	 fd	Z
dQd
Zd ZdQdZdQdZdQdZdQdZdQdZd ZdQdZd Zed             Zej        d             Zed             Zej        d             Zed             Zej        d             Zed             Zej        d             Zd Zej        d             Z	 	 	 	 	 dR fd$	ZdQd%Z ed&             Z!	 	 dSd'Z"	 	 	 	 dTd(Z#	 	 	 	 dUd)Z$g d*Z%g d+Z&d, Z'	 	 d+d-Z(dQd.Z)	 	 	 	 	 dVd/Z*	 	 	 	 dWd0Z+	 	 dXd1Z,d2 Z-dYd3Z.dZd4Z/	 	 d+d5Z0g d6Z1g d7Z2d8 Z3	 	 	 d[d:Z4g d;Z5g d<Z6d= Z7d> Z8d? Z9dYd@Z:dA Z;edB             Z<edC             Z=edD             Z>dYdEZ?dF Z@dG ZA	 	 d+dHZB	 	 d+dIZCdJ ZD	 	 d\dKZE	 	 	 	 	 	 	 d]dLZF	 	 	 	 d^dNZGeHdQdO            ZI xZJS )_MLEModela  
    State space model for maximum likelihood estimation

    Parameters
    ----------
    endog : array_like
        The observed time-series process :math:`y`
    k_states : int
        The dimension of the unobserved state process.
    exog : array_like, optional
        Array of exogenous regressors, shaped nobs x k. Default is no
        exogenous regressors.
    dates : array_like of datetime, optional
        An array-like object of datetime objects. If a Pandas object is given
        for endog, it is assumed to have a DateIndex.
    freq : str, optional
        The frequency of the time-series. A Pandas offset or 'B', 'D', 'W',
        'M', 'A', or 'Q'. This is optional if dates are given.
    **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
    ----------
    ssm : statsmodels.tsa.statespace.kalman_filter.KalmanFilter
        Underlying state space representation.

    See Also
    --------
    statsmodels.tsa.statespace.mlemodel.MLEResults
    statsmodels.tsa.statespace.kalman_filter.KalmanFilter
    statsmodels.tsa.statespace.representation.Representation

    Notes
    -----
    This class wraps the state space model with Kalman filtering to add in
    functionality for maximum likelihood estimation. In particular, it adds
    the concept of updating the state space representation based on a defined
    set of parameters, through the `update` method or `updater` attribute (see
    below for more details on which to use when), and it adds a `fit` method
    which uses a numerical optimizer to select the parameters that maximize
    the likelihood of the model.

    The `start_params` `update` method must be overridden in the
    child class (and the `transform` and `untransform` methods, if needed).
    Nc                 @   t                                          ||||d           || _        |                                 \  | _        | _        | j        j        d         | _        || _         | j	        di | d| _
        d | _        d | _        d | _        d | _        d S )Nnone)endogexogdatesr:   missingr   F )super__init___init_kwargsprepare_datarM   rN   shapenobsk_statesinitialize_statespace_has_fixed_params_fixed_params_params_index_fixed_params_index_free_params_index)selfrM   rX   rN   rO   r:   r/   	__class__s          r5   rS   zMLEModel.__init__   s     	u4$4!' 	 	) 	) 	)
 # !% 1 1 3 3
DI J$Q'	  	#",,V,,, "'!!#' "&r7   c                    t          j        t          j        | j        j        d          d                                          }| j        j        }|t          j        |          }|j        dk    r|j        d         df|_        ||fS )zH
        Prepare data for use in the state space representation
        T)copyCW)requirementsNr   r   )	nprequirearrayr8   
orig_endogrb   	orig_exogndimrV   )r_   rM   rN   s      r5   rU   zMLEModel.prepare_data   s     
HTY)555D
 
 

$&& 	 y"8D>>D :?? ;q>1-EKd{r7   c                     | j         j        }t          |j        d         | j        fd|j        d         i|| _        | j                            |           | j        j        | _        dS )z
        Initialize the state space representation

        Parameters
        ----------
        **kwargs
            Additional keyword arguments to pass to the state space class
            constructor.
        r   rW   r   N)rM   Tr   rV   rX   ssmbindk_endog)r_   r/   rM   s      r5   rY   zMLEModel.initialize_statespace   st     
 &ek!ndm E E+0;q>E=CE E 	e x'r7   c                    | j         rt          | j        t          j                  rCt          j        | j        d         t          | j                  dz   | j        j                  }nVt          | j        t          j                  rBt          j	        | j        d         t          | j                  dz   | j        j                  }nt          t          | j        t          j                  rt	 | j        j        }| j        j        }| j        j        }n4# t          $ r' | j        j        }| j        j        }| j        j        }Y nw xY wt          j        |||z   |          }n[t'          | j                  r@| j        d         dz   }t          j        | j                                        |gz             }nt          |S )Nr   r   )startperiodsr:   )_index_datesr"   _indexr;   DatetimeIndex
date_ranger!   r:   PeriodIndexperiod_rangeNotImplementedError
RangeIndexrq   stopstepAttributeError_start_stop_stepr   Indextolist)r_   r>   rq   r|   r}   r4   s         r5   _get_index_with_final_statez$MLEModel._get_index_with_final_state   s     	&$+r'788 	*+a.#dk2B2BQ2F)+ + + DK88 *+a.#dk2B2BQ2F)+ + + *)R]33 	&)){'{'! ) ) )*{({() M%d;;EE$+&& 	& KOa'EHT[//11UG;<<EE%%s   2$D .EEc                 8    | j                             ||          S N)rm   __setitem__)r_   keyr4   s      r5   r   zMLEModel.__setitem__   s    x##C///r7   c                 6    | j                             |          S r   )rm   __getitem__)r_   r   s     r5   r   zMLEModel.__getitem__   s    x##C(((r7   c                     t                                                      }|                                D ]4\  }}|-t          | j        |          rt          | j        |          ||<   5|S r   )rR   _get_init_kwdsr(   hasattrrm   r@   )r_   kwdsr   r4   r`   s       r5   r   zMLEModel._get_init_kwds   sc    ww%%''**,, 	3 	3JC}3!7!7}#DHc22S	r7   c                      t          d          )a  
        Clone state space model with new data and optionally new specification

        Parameters
        ----------
        endog : array_like
            The observed time-series process :math:`y`
        k_states : int
            The dimension of the unobserved state process.
        exog : array_like, optional
            Array of exogenous regressors, shaped nobs x k. Default is no
            exogenous regressors.
        kwargs
            Keyword arguments to pass to the new model class to change the
            model specification.

        Returns
        -------
        model : MLEModel subclass

        Notes
        -----
        This method must be implemented
        z[This method is not implemented in the base class and must be set up by each specific model.rz   )r_   rM   rN   r/   s       r5   clonezMLEModel.clone   s    2 " #, - - 	-r7   c                     |                                  }|                    |           t          | dd          dk    r%|                    dd           t	          d           | j        |fi |}|S )Nk_exogr   rN   zpCloning a model with an exogenous component requires specifying a new exogenous array using the `exog` argument.)r   updater@   r'   rA   r`   )r_   rM   r/   
use_kwargsmods        r5   _clone_from_init_kwdszMLEModel._clone_from_init_kwds  s     ((**
&!!! 41%%))fjj.F.F.N 5 6 6 6 dnU11j11
r7   c                 ,     | j         j        |fi | dS )a`  
        Set the filtering method

        The filtering method controls aspects of which Kalman filtering
        approach will be used.

        Parameters
        ----------
        filter_method : int, optional
            Bitmask value to set the filter method to. See notes for details.
        **kwargs
            Keyword arguments may be used to influence the filter method by
            setting individual boolean flags. See notes for details.

        Notes
        -----
        This method is rarely used. See the corresponding function in the
        `KalmanFilter` class for details.
        N)rm   set_filter_method)r_   filter_methodr/   s      r5   r   zMLEModel.set_filter_method+  s'    ( 	#"=;;F;;;;;r7   c                 ,     | j         j        |fi | dS )a  
        Set the inversion method

        The Kalman filter may contain one matrix inversion: that of the
        forecast error covariance matrix. The inversion method controls how and
        if that inverse is performed.

        Parameters
        ----------
        inversion_method : int, optional
            Bitmask value to set the inversion method to. See notes for
            details.
        **kwargs
            Keyword arguments may be used to influence the inversion method by
            setting individual boolean flags. See notes for details.

        Notes
        -----
        This method is rarely used. See the corresponding function in the
        `KalmanFilter` class for details.
        N)rm   set_inversion_method)r_   inversion_methodr/   s      r5   r   zMLEModel.set_inversion_methodA  (    , 	&%&6AA&AAAAAr7   c                 ,     | j         j        |fi | dS )a  
        Set the numerical stability method

        The Kalman filter is a recursive algorithm that may in some cases
        suffer issues with numerical stability. The stability method controls
        what, if any, measures are taken to promote stability.

        Parameters
        ----------
        stability_method : int, optional
            Bitmask value to set the stability method to. See notes for
            details.
        **kwargs
            Keyword arguments may be used to influence the stability method by
            setting individual boolean flags. See notes for details.

        Notes
        -----
        This method is rarely used. See the corresponding function in the
        `KalmanFilter` class for details.
        N)rm   set_stability_method)r_   stability_methodr/   s      r5   r   zMLEModel.set_stability_methodY  r   r7   c                 ,     | j         j        |fi | dS )a  
        Set the memory conservation method

        By default, the Kalman filter computes a number of intermediate
        matrices at each iteration. The memory conservation options control
        which of those matrices are stored.

        Parameters
        ----------
        conserve_memory : int, optional
            Bitmask value to set the memory conservation method to. See notes
            for details.
        **kwargs
            Keyword arguments may be used to influence the memory conservation
            method by setting individual boolean flags.

        Notes
        -----
        This method is rarely used. See the corresponding function in the
        `KalmanFilter` class for details.
        N)rm   set_conserve_memory)r_   conserve_memoryr/   s      r5   r   zMLEModel.set_conserve_memoryq  s'    , 	%$_???????r7   c                 ,     | j         j        |fi | dS )au  
        Set the smoother output

        The smoother can produce several types of results. The smoother output
        variable controls which are calculated and returned.

        Parameters
        ----------
        smoother_output : int, optional
            Bitmask value to set the smoother output to. See notes for details.
        **kwargs
            Keyword arguments may be used to influence the smoother output by
            setting individual boolean flags.

        Notes
        -----
        This method is rarely used. See the corresponding function in the
        `KalmanSmoother` class for details.
        N)rm   set_smoother_output)r_   smoother_outputr/   s      r5   r   zMLEModel.set_smoother_output  s'    ( 	%$_???????r7   c                 <    | j                             ||           dS )zInitialize knownN)rm   initialize_known)r_   initial_stateinitial_state_covs      r5   r   zMLEModel.initialize_known  s!    !!-1BCCCCCr7   c                 :    | j                             |           dS )zInitialize approximate diffuseN)rm   initialize_approximate_diffuse)r_   variances     r5   r   z'MLEModel.initialize_approximate_diffuse  s    //99999r7   c                 8    | j                                          dS )zInitialize stationaryN)rm   initialize_stationaryr_   s    r5   r   zMLEModel.initialize_stationary  s    &&(((((r7   c                     | j         j        S r   rm   initializationr   s    r5   r   zMLEModel.initialization  s    x&&r7   c                     || j         _        d S r   r   r_   r4   s     r5   r   zMLEModel.initialization  s    "'r7   c                     | j         j        S r   rm   initial_variancer   s    r5   r   zMLEModel.initial_variance  s    x((r7   c                     || j         _        d S r   r   r   s     r5   r   zMLEModel.initial_variance  s    $)!!!r7   c                     | j         j        S r   rm   loglikelihood_burnr   s    r5   r   zMLEModel.loglikelihood_burn  s    x**r7   c                     || j         _        d S r   r   r   s     r5   r   zMLEModel.loglikelihood_burn  s    &+###r7   c                     | j         j        S r   rm   	tolerancer   s    r5   r   zMLEModel.tolerance  s    x!!r7   c                     || j         _        d S r   r   r   s     r5   r   zMLEModel.tolerance  s    "r7   c                 F    |D ]}|| j         vrt          d|z            d S )Nz$Invalid parameter name passed: "%s".)param_namesrA   )r_   r   
param_names      r5   _validate_can_fix_paramsz!MLEModel._validate_can_fix_params  sK    % 	/ 	/J!111 !G#-". / / / 2	/ 	/r7   c              #      K   t           j                  } j        @i  _        t          t	           j        t          j        |                               _         j                                        } j	        } j
        } j        }t          |                                          t           j                                                  z  }                     |            j                            |            fd j        D              _        d _	         fd j                                        D              _
        t!          t          t          j        |                                         j
                             _        	 dV  | _	        | _        | _
        | _        dS # | _	        | _        | _
        | _        w xY w)a  
        Fix parameters to specific values (context manager)

        Parameters
        ----------
        params : dict
            Dictionary describing the fixed parameter values, of the form
            `param_name: fixed_value`. See the `param_names` property for valid
            parameter names.

        Examples
        --------
        >>> mod = sm.tsa.SARIMAX(endog, order=(1, 0, 1))
        >>> with mod.fix_params({'ar.L1': 0.5}):
                res = mod.fit()
        Nc                 >    i | ]}|j         v |j         |         S rQ   )r[   .0r3   r_   s     r5   
<dictcomp>z'MLEModel.fix_params.<locals>.<dictcomp>  s;     + + +/3t))) $$T*)))r7   Tc                 *    g | ]}j         |         S rQ   )r\   )r   r   r_   s     r5   
<listcomp>z'MLEModel.fix_params.<locals>.<listcomp>   s7     $J $J $J(+ %)$6s$; $J $J $Jr7   )r!   r   r[   r#   r$   re   aranger\   rb   rZ   r]   r^   setkeysr   r   list
difference)r_   paramsk_paramscache_fixed_paramscache_has_fixed_paramscache_fixed_params_indexcache_free_params_indexall_fixed_param_namess   `       r5   
fix_paramszMLEModel.fix_params  s     $ t'(( %!#D!%D$bi&9&9::"< "<D "/4466!%!7#'#; "&"9 T%7%<%<%>%>!?!?? 	 	%%&;<<< 	!!&)))+ + + +7;7G+ + +
 "&$J $J $J $J/3/A/F/F/H/H$J $J $J "&	(##$$//0HII#K #K	>EEE &<D"!3D'?D$&=D### &<D"!3D'?D$&=D#====s   F; ;GTFlbfgs2   r      c                 F   || j         }d}d}|3|dk    r-|                    dd           |                    dd           n|d}|| j        j         }n|r| j        j        rt	          d          |                     |d|	          }|r|                     |          }| j        r|| j                 }| j        r&t          |          d
k    rt          g dd          }nldfd|                                D             }|i }|                    dd||d           |||d<   |f} t                      j        |f|||||	|
dd|}|r|                     |j        dd	          S |r+| j        j        }| j                            t$                     | j        j        s| j        j        s| j        j        r| j        }n| j        } ||j        dd||          }||_        |j        |_        |j        |_        |r| j                            |           |S )a  
        Fits the model by maximum likelihood via Kalman filter.

        Parameters
        ----------
        start_params : array_like, optional
            Initial guess of the solution for the loglikelihood maximization.
            If None, the default is given by Model.start_params.
        transformed : bool, optional
            Whether or not `start_params` is already transformed. Default is
            True.
        includes_fixed : bool, optional
            If parameters were previously fixed with the `fix_params` method,
            this argument describes whether or not `start_params` also includes
            the fixed parameters, in addition to the free parameters. Default
            is False.
        cov_type : str, optional
            The `cov_type` keyword governs the method for calculating the
            covariance matrix of parameter estimates. Can be one of:

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

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

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

            - 'approx_complex_step' : bool, optional - If True, numerical
              approximations are computed using complex-step methods. If False,
              numerical approximations are computed using finite difference
              methods. Default is True.
            - 'approx_centered' : bool, optional - If True, numerical
              approximations computed using finite difference methods use a
              centered approximation. Default is False.
        method : str, optional
            The `method` determines which solver from `scipy.optimize`
            is used, and it can be chosen from among the following strings:

            - 'newton' for Newton-Raphson
            - 'nm' for Nelder-Mead
            - 'bfgs' for Broyden-Fletcher-Goldfarb-Shanno (BFGS)
            - 'lbfgs' for limited-memory BFGS with optional box constraints
            - 'powell' for modified Powell's method
            - 'cg' for conjugate gradient
            - 'ncg' for Newton-conjugate gradient
            - 'basinhopping' for global basin-hopping solver

            The explicit arguments in `fit` are passed to the solver,
            with the exception of the basin-hopping solver. Each
            solver has several optional arguments that are not the same across
            solvers. See the notes section below (or scipy.optimize) for the
            available arguments and for the list of explicit arguments that the
            basin-hopping solver supports.
        maxiter : int, optional
            The maximum number of iterations to perform.
        full_output : bool, optional
            Set to True to have all available output in the Results object's
            mle_retvals attribute. The output is dependent on the solver.
            See LikelihoodModelResults notes section for more information.
        disp : bool, optional
            Set to True to print convergence messages.
        callback : callable callback(xk), optional
            Called after each iteration, as callback(xk), where xk is the
            current parameter vector.
        return_params : bool, optional
            Whether or not to return only the array of maximizing parameters.
            Default is False.
        optim_score : {'harvey', 'approx'} or None, optional
            The method by which the score vector is calculated. 'harvey' uses
            the method from Harvey (1989), 'approx' uses either finite
            difference or complex step differentiation depending upon the
            value of `optim_complex_step`, and None uses the built-in gradient
            approximation of the optimizer. Default is None. This keyword is
            only relevant if the optimization method uses the score.
        optim_complex_step : bool, optional
            Whether or not to use complex step differentiation when
            approximating the score; if False, finite difference approximation
            is used. Default is True. This keyword is only relevant if
            `optim_score` is set to 'harvey' or 'approx'.
        optim_hessian : {'opg', 'oim', 'approx'}, optional
            The method by which the Hessian is numerically approximated. 'opg'
            uses outer product of gradients, 'oim' uses the information
            matrix formula from Harvey (1989), and 'approx' uses numerical
            approximation. This keyword is only relevant if the
            optimization method uses the Hessian matrix.
        low_memory : bool, optional
            If set to True, techniques are applied to substantially reduce
            memory usage. If used, some features of the results object will
            not be available (including smoothed results and in-sample
            prediction), although out-of-sample forecasting is possible.
            Default is False.
        **kwargs
            Additional keyword arguments to pass to the optimizer.

        Returns
        -------
        results
            Results object holding results from fitting a state space model.

        See Also
        --------
        statsmodels.base.model.LikelihoodModel.fit
        statsmodels.tsa.statespace.mlemodel.MLEResults
        statsmodels.tsa.statespace.structural.UnobservedComponentsResults
        NTr   approx_gradepsilongh㈵>approxHCannot use complex step derivatives when data or parameters are complex.transformedincludes_fixedr   )r   mle_retvalsmle_settings)concentrate_scaleenforce_stationarityenforce_invertibilityc                 $    i | ]\  }}|v	||S rQ   rQ   )r   kvdisallows      r5   r   z MLEModel.fit.<locals>.<dictcomp>  s)    KKKtq!(9J9Ja9J9J9Jr7   F)r   r   score_methodapprox_complex_stephessian_method)methodfargsmaxiterfull_outputdispcallbackskip_hessian)r   r   cov_typecov_kwds)start_params
setdefaultrm   _complex_endogrA   handle_paramsuntransform_paramsrZ   r^   r!   r   r(   r   rR   fitr   r   r   r   memory_no_predictedmemory_no_gainmemory_no_smoothingfiltersmoothmlefitr   r   )r_   r   r   r   r   r   r   r   r   r   r   return_paramsoptim_scoreoptim_complex_stepoptim_hessianr1   
low_memoryr/   r  r   r   funcresr   r`   s                          @r5   r   zMLEModel.fit  s   x ,LK!N 6W#4#4mT222i.... "K %%)X%<!< 	<DH$; 	< ; < < < )),D9G * I I  	A22<@@L ! 	A'(?@L ! 	>c,&7&71&<&<"$(,. . .FFH
 LKKKv||~~KKKF}LL$"' +'9	     (*7&'HE UWW[ >f',)0-8&*X.2> >
 7=> >F  	%%fm5: & < < <
  >"&(":,,_=== , #0G #x3#{{$v}% (8= = =C  CJ$0CO%2C  >,,_===Jr7   c                 |    |                      |          5   | j        |fi |}ddd           n# 1 swxY w Y   |S )a2  
        Fit the model with some parameters subject to equality constraints.

        Parameters
        ----------
        constraints : dict
            Dictionary of constraints, of the form `param_name: fixed_value`.
            See the `param_names` property for valid parameter names.
        start_params : array_like, optional
            Initial guess of the solution for the loglikelihood maximization.
            If None, the default is given by Model.start_params.
        **fit_kwds : keyword arguments
            fit_kwds are used in the optimization of the remaining parameters.

        Returns
        -------
        results : Results instance

        Examples
        --------
        >>> mod = sm.tsa.SARIMAX(endog, order=(1, 0, 1))
        >>> res = mod.fit_constrained({'ar.L1': 0.5})
        N)r   r   )r_   constraintsr   fit_kwdsr  s        r5   fit_constrainedzMLEModel.fit_constrained  s    0 __[)) 	5 	5$(<44844C	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5
s   155c                 "    dt           t          fiS )Nr   )
MLEResultsMLEResultsWrapperr   s    r5   _res_classeszMLEModel._res_classes  s    
$5677r7   c                     |sPi }|||d<   |||d<   || j         d         d         }|| j         d         d         } || ||fi |}	 ||	          }|S )Nr   r   r   r   r   )r  )
r_   r   result
return_rawr   r   results_classwrapper_classresult_kwargsr  s
             r5   _wrap_resultszMLEModel._wrap_results  s     	(M#,4j)#,4j)$ $ 1% 8 ;$ $ 1% 8 ;-ffFFFFC"]3''Fr7   c           	      $   |                      |||          }|                     |dd|           | j        | j        _        |rt          t
          z  |d<   |
r
t          |d<    | j        j        dd|i|}| 	                    |||||||	          S )a  
        Kalman filtering

        Parameters
        ----------
        params : array_like
            Array of parameters at which to evaluate the loglikelihood
            function.
        transformed : bool, optional
            Whether or not `params` is already transformed. Default is True.
        return_ssm : bool,optional
            Whether or not to return only the state space output or a full
            results object. Default is to return a full results object.
        cov_type : str, optional
            See `MLEResults.fit` for a description of covariance matrix types
            for results object.
        cov_kwds : dict or None, optional
            See `MLEResults.get_robustcov_results` for a description required
            keywords for alternative covariance estimators
        low_memory : bool, optional
            If set to True, techniques are applied to substantially reduce
            memory usage. If used, some features of the results object will
            not be available (including in-sample prediction), although
            out-of-sample forecasting is possible. Default is False.
        **kwargs
            Additional keyword arguments to pass to the Kalman filter. See
            `KalmanFilter.filter` for more details.
        r   Tr   r   complex_stepr   r   r  rQ   )
r   r   r   r8   r   r   r   rm   r  r  )r_   r   r   r   r  r   r   
return_ssmr  results_wrapper_classr  r/   r  s                r5   r  zMLEModel.filter  s    @ ##F3A $ C CFT!- 	 	/ 	/ 	/ !% 0	 	F):X)EF%&  	8(7F$% !EElEfEE !!&&*h"*M"79 9 	9r7   c
           	         |                      |||          }|                     |dd|           | j        | j        _        |rt          t
          z  |
d<    | j        j        dd|i|
}|                     |||||||	          S )a  
        Kalman smoothing

        Parameters
        ----------
        params : array_like
            Array of parameters at which to evaluate the loglikelihood
            function.
        transformed : bool, optional
            Whether or not `params` is already transformed. Default is True.
        return_ssm : bool,optional
            Whether or not to return only the state space output or a full
            results object. Default is to return a full results object.
        cov_type : str, optional
            See `MLEResults.fit` for a description of covariance matrix types
            for results object.
        cov_kwds : dict or None, optional
            See `MLEResults.get_robustcov_results` for a description required
            keywords for alternative covariance estimators
        **kwargs
            Additional keyword arguments to pass to the Kalman filter. See
            `KalmanFilter.filter` for more details.
        r   Tr  r   r  rQ   )	r   r   r   r8   r   r   rm   r  r  )r_   r   r   r   r  r   r   r   r  r!  r/   r  s               r5   r  zMLEModel.smoothP  s    6 ##F3A $ C CFT!- 	 	/ 	/ 	/ !% 0	 	F):X)EF%& !EElEfEE !!&&*h"*M"79 9 	9r7   r  )TFFc                    t          t          j        t          j        g|R i |\  }}}}|                     |||          }|                     |dd|           |rt          t          z  |d<    | j        j	        dd|i|}|S )a  
        Loglikelihood evaluation

        Parameters
        ----------
        params : array_like
            Array of parameters at which to evaluate the loglikelihood
            function.
        transformed : bool, optional
            Whether or not `params` is already transformed. Default is True.
        **kwargs
            Additional keyword arguments to pass to the Kalman filter. See
            `KalmanFilter.filter` for more details.

        See Also
        --------
        update : modifies the internal state of the state space model to
                 reflect new params

        Notes
        -----
        [1]_ recommend maximizing the average likelihood to avoid scale issues;
        this is done automatically by the base Model fit method.

        References
        ----------
        .. [1] Koopman, Siem Jan, Neil Shephard, and Jurgen A. Doornik. 1999.
           Statistical Algorithms for Models in State Space Using SsfPack 2.2.
           Econometrics Journal 2 (1): 107-60. doi:10.1111/1368-423X.00023.
        r   Tr  r   r  rQ   )
r6   rJ   _loglike_param_names_loglike_param_defaultsr   r   r   r   rm   loglike)r_   r   r.   r/   r   r   r  r&  s           r5   r&  zMLEModel.loglike  s    > =I)8+K== = == =9^\6 ##F3A $ C CFT!- 	 	/ 	/ 	/  	F):X)EF%&"$("GGGGG
 r7   c                     |                      |||          }|rt          t          z  |d<   |                     |dd|            | j        j        dd|i|S )a  
        Loglikelihood evaluation

        Parameters
        ----------
        params : array_like
            Array of parameters at which to evaluate the loglikelihood
            function.
        transformed : bool, optional
            Whether or not `params` is already transformed. Default is True.
        **kwargs
            Additional keyword arguments to pass to the Kalman filter. See
            `KalmanFilter.filter` for more details.

        See Also
        --------
        update : modifies the internal state of the Model to reflect new params

        Notes
        -----
        [1]_ recommend maximizing the average likelihood to avoid scale issues;
        this is done automatically by the base Model fit method.

        References
        ----------
        .. [1] Koopman, Siem Jan, Neil Shephard, and Jurgen A. Doornik. 1999.
           Statistical Algorithms for Models in State Space Using SsfPack 2.2.
           Econometrics Journal 2 (1): 107-60. doi:10.1111/1368-423X.00023.
        r   r   Tr  r  rQ   )r   r   r   r   rm   
loglikeobs)r_   r   r   r   r  r/   s         r5   r(  zMLEModel.loglikeobs  s    > ##F3A $ C C
  	F):X)EF%&FT!- 	 	/ 	/ 	/ #tx"GGGGGGr7   c                 *     | j         j        dd|i|S )a  
        Retrieve a simulation smoother for the state space model.

        Parameters
        ----------
        simulation_output : int, optional
            Determines which simulation smoother output is calculated.
            Default is all (including state and disturbances).
        **kwargs
            Additional keyword arguments, used to set the simulation output.
            See `set_simulation_output` for more details.

        Returns
        -------
        SimulationSmoothResults
        simulation_outputrQ   )rm   simulation_smoother)r_   r*  r/   s      r5   r+  zMLEModel.simulation_smoother  s2    " ,tx+ ; ;/;39; ; 	;r7   c                    t          j        |d          }||}|s|rt          d          |rt          t          z  |d<   |-|                     ||||            | j        j        dd|i|}t          |          }t          j	        | j
        | j        |f          }	t          j	        | j
        | j
        | j        |f          }
|rt          |dd |          }t          j        |          dz  |z  }t          |          D ]v\  }}|                     ||z   ||d	            | j        j        ddd	i|}|j        j        ||         z  |	d d d d |f<   |j        j        ||         z  |
d d d d d d |f<   wn|st          |dd |          }t          j	        |ft$                    }t'          |          D ]}||         ||<   |                     ||z   ||d
            | j        j        ddd
i|}|j        |j        z
  ||         z  |	d d d d |f<   |j        |j        z
  ||         z  |
d d d d d d |f<   d||<   nt          |dd |          dz  }t          j	        |ft$                    }t'          |          D ]}||         ||<   |                     ||z   ||d
            | j        j        ddd
i|}|                     ||z
  ||d
            | j        j        ddd
i|}|j        |j        z
  d||         z  z  |	d d d d |f<   |j        |j        z
  d||         z  z  |
d d d d d d |f<   d||<   |	|
fS )Nr   ndminrCannot use complex-step approximations to calculate the observed_information_matrix with untransformed parameters.r   r  r     y              ?TFg                  @rQ   )re   rg   rA   r   r   r   rm   r  r!   zerosro   rW   r
   identity	enumerateforecasts_errorimagforecasts_error_covfloatr%   )r_   r   r   r   r   approx_centeredr  r/   npartials_forecasts_errorpartials_forecasts_error_covr   
incrementsr2   ih_resei_res1_res2s                      r5   $_forecasts_error_partial_derivativesz-MLEModel._forecasts_error_partial_derivatives  s   
 &*** &"- 	@2 	@ ? @ @ @  	F):X)EF%& ;KKK'5%8  : : : "$(/MM/BMfMMC KK
 HdlDIq122 	! HdlDL$)Q?@@ 	%  7	"61dA66GQ",w6J":..  2FRK[+9)-  / / / 'txCCDCFCC (-
: )AAAq1
 ,1GAJ> -QQQ111aZ88 ! &	"61dA66G1$&&B1XX  
1FRK[+9  O O O&txDDEDVDD (3+>>'!*5M(AAAq1 ,+,/6qz<:,QQQ111aZ8 1 #61dA66;G1$&&B1XX  
1FRK[+9  O O O'EEUEfEEFRK[+9  O O O'EEUEfEE *U-BB^% )AAAq1
 .1JJ^% -QQQ111aZ8 1')EEEr7   c                    t          j        |d          }t          |          }||}|s|rt          d          |                     |||          }|                     |dd|           |rt          t          z  |d<    | j        j	        dd	|i|}| j        j
        }	|j                                        }
 | j        |f|||||d
|\  }}t          j        | j        | j        | j        |f|	          }t          j        ||f|	          }t          j        | j        j        |j                  }t)          || j                  D ]`}t           j                            |j        dddd|f                   |
dddd|f<   t)          |          D ]=}t          j        |
dddd|f         |dddd||f                   |dddd||f<   >t)          |          D ]}t)          |          D ]}|||fxx         dt          j        t          j        |dddd||f         |dddd||f                             z  z  cc<   |||fxx         t          j        |dd||f         t          j        |
dddd|f         |dd||f                             z  cc<   Ɛb|| j        | j        j        z
  z  S )a  
        Observed information matrix

        Parameters
        ----------
        params : array_like, optional
            Array of parameters at which to evaluate the loglikelihood
            function.
        **kwargs
            Additional keyword arguments to pass to the Kalman filter. See
            `KalmanFilter.filter` for more details.

        Notes
        -----
        This method is from Harvey (1989), which shows that the information
        matrix only depends on terms from the gradient. This implementation is
        partially analytic and partially numeric approximation, therefore,
        because it uses the analytic formula for the information matrix, with
        numerically computed elements of the gradient.

        References
        ----------
        Harvey, Andrew C. 1990.
        Forecasting, Structural Time Series Models and the Kalman Filter.
        Cambridge University Press.
        r   r-  Nr/  r   Tr  r   r  r   r   r   r:  r  dtype      ?rQ   )re   rg   r!   rA   r   r   r   r   rm   r  rH  r8  rb   rD  r3  ro   rW   maximumr   nobs_diffuser%   linalginvdottraceinner)r_   r   r   r   r   r:  r/   r;  r  rH  inv_forecasts_error_covr<  r=  tmpinformation_matrixdtr2   js                      r5   observed_information_matrixz$MLEModel.observed_information_matrixU  s   < &*** KK &"- 	@2 	@ ? @ @ @
 ##F3A $ C CFT!4 	 	6 	6 	6  	F):X)EF%&dhoII+>I&II #&"9">">"@"@ 6D5D$/$7 /SD D =CD D 	? "> hdlDIqAOOOXq!fE:::Jtx2C4DEEq$)$$ 	 	A	c5aaaAg>?? $AAAqqq!G, 1XX  "$&+AAAqqq!G40AAAq!<# #AAAqqq!QJ 1XX 
 
q 	 	A&q!t,,,bhrvc!!!QQQ1*o.1!!!QQQ1*o(? (? @ @ @,,, 'q!t,,,0Aq96qqq!!!Qw?71a@B B1 1 ,,,,	
 "TY1L%LMMr7   c                     ||}|s|rt          d           | j        |f|||d|                                }t          j        ||          | j        | j        j        z
  z  S )a/  
        Outer product of gradients information matrix

        Parameters
        ----------
        params : array_like, optional
            Array of parameters at which to evaluate the loglikelihood
            function.
        **kwargs
            Additional arguments to the `loglikeobs` method.

        References
        ----------
        Berndt, Ernst R., Bronwyn Hall, Robert Hall, and Jerry Hausman. 1974.
        Estimation and Inference in Nonlinear Structural Models.
        NBER Chapters. National Bureau of Economic Research, Inc.
        Nr/  )r   r   r   )rA   	score_obs	transposere   rP  rW   rm   r   )r_   r   r   r   r   r/   rY  s          r5   opg_information_matrixzMLEModel.opg_information_matrix  s    , &"- 	@2 	@ ? @ @ @ #DN6 -{2@7J- - &,- - .7Y[[ 	
 HY	**Y446	
r7   c                     t          |dd t          |                    }d|d<   d|d<   t          || j        ||          S )Nr2  Tr   r  r   r/   )r
   r!   r   r&  )r_   r   r/   r   s       r5   _score_complex_stepzMLEModel._score_complex_step  sS     vr4V== $}!%~g'-/ / / 	/r7   c                 <    d|d<   t          || j        ||          S )NTr   r/   centered)r   r&  )r_   r   r:  r/   s       r5   _score_finite_differencez!MLEModel._score_finite_difference  s.     $}VT\&&57 7 7 	7r7   c                 N     | j         |fd|i|}t          j        |d          S )Nr   r   axis)_score_obs_harveyre   sum)r_   r   r   r/   rY  s        r5   _score_harveyzMLEModel._score_harvey  sH    *D*G G(;G?EG G	via((((r7   c                 "   t          j        |d          }t          |          }|                     |d||           |rt          t
          z  |d<   d|v r|d=  | j        j        dd|i|} | j        |fd||||d|\  }}	t          j	        | j
        |f          }
| j        }t          | j
                  D ]N}t           j                            |j        d	d	d	d	|f                   }t          |          D ]	}|
||fxx         t          j        t          j        t          j        ||	d	d	d	d	||f                   t          j        |          t          j        |t          j        |j        d	d	|f         |j        d	d	|f                             z
                      z  cc<   |
||fxx         d
t          j        |d	d	||f         t          j        ||j        d	d	|f                             z  z  cc<   P|
 dz  S )a\  
        Score

        Parameters
        ----------
        params : array_like, optional
            Array of parameters at which to evaluate the loglikelihood
            function.
        **kwargs
            Additional keyword arguments to pass to the Kalman filter. See
            `KalmanFilter.filter` for more details.

        Notes
        -----
        This method is from Harvey (1989), section 3.4.5

        References
        ----------
        Harvey, Andrew C. 1990.
        Forecasting, Structural Time Series Models and the Kalman Filter.
        Cambridge University Press.
        r   r-  Tr  r   r   r  rF  Nr0  r2  rQ   )re   rg   r!   r   r   r   rm   r  rD  r3  rW   ro   r%   rL  rM  r8  rO  rN  eyeouterr6  )r_   r   r   r:  r   r/   r;  r  r<  r=  partialsro   rU  rQ  r2   s                  r5   rf  zMLEModel._score_obs_harvey  s   2 &***KK 	F^!4 	 	6 	6 	6 	F):X)EF%&F""}%dhoII+>I&II 6D5D$($7 /SD D =CD D 	? "> 8TYN++,ty!! 	P 	PA&(imm+AAAqqq!G4'6 '6# 1XX P PA"(26F27111a
CE EVG__V3HS%8A%>%(%8A%>@ @A AA,C ,C #D #D D A!bf,QQQ1W5F2C4G14MNN'P 'P #P PP y2~r7   )r   r   r   r   r:  )TFr   NFc                 P   t          t          j        t          j        g|R i |\  }}}}}}d|v r|                    d          }|| j        j         }|r| j        j        rt          d          |                     ||||           }	|r|	}n|	\  }}
|dk    rd|d<    | j	        |fd|i|}nG|d	k    r|rd|d<    | j
        |fi |}n+|d	k    rd|d<    | j        |fd
|i|}nt          d          |st          j        |
|          }| j        r|s|| j                 }|S )a4  
        Compute the score function at params.

        Parameters
        ----------
        params : array_like
            Array of parameters at which to evaluate the score.
        *args
            Additional positional arguments to the `loglike` method.
        **kwargs
            Additional keyword arguments to the `loglike` method.

        Returns
        -------
        score : ndarray
            Score, evaluated at `params`.

        Notes
        -----
        This is a numerical approximation, calculated using first-order complex
        step differentiation on the `loglike` method.

        Both args and kwargs are necessary because the optimizer from
        `fit` must call this function and only supports passing arguments via
        args (for example `scipy.optimize.fmin_l_bfgs`).
        r   Nr   )r   r   return_jacobianharveyTr   r   r   r:  zInvalid score method.)r6   rJ   _score_param_names_score_param_defaultsr*   rm   r   rA   r   rh  r^  rb  rz   re   rN  rZ   r^   )r_   r   r.   r/   r   r   r   r   r:  outtransform_scorescores               r5   rt  zMLEModel.score2  s   : 4!7J:>J J JBHJ J	"nf.A	& vZZ))F&&*h&="= 	<48#: 	< ; < < <   N +O ! - -  	*FF&)#FOX'+F#$&D&K K,?KCIK KEEx$7'+F#$,D,V>>v>>EEx'+F#$1D1C C(7C;AC CEE &&=>>> 	3F?E22E! 	3. 	3$12Er7   r   c                    |s|rt          d          || j        j         }|r| j        j        rt          d          |                     |d|          }||d<   d|d<   |dk    r | j        |fd	|i|}ns|d
k    r?|r=t          |ddt          |                    }	d|d<   t          || j        |	|          }n.|d
k    rt          || j        ||          }nt          d          |S )a/  
        Compute the score per observation, evaluated at params

        Parameters
        ----------
        params : array_like
            Array of parameters at which to evaluate the score.
        **kwargs
            Additional arguments to the `loglike` method.

        Returns
        -------
        score : ndarray
            Score per observation, evaluated at `params`.

        Notes
        -----
        This is a numerical approximation, calculated using first-order complex
        step differentiation on the `loglikeobs` method.
        zpCannot use complex-step approximations to calculate the score at each observation with untransformed parameters.Nr   Tr   r   r   ro  r   r   r2  r  r]  r`  zInvalid scoreobs method.)rA   rm   r   r   rf  r
   r!   r   r(  r   rz   )
r_   r   r   r   r   r   r:  r/   rt  r   s
             r5   rY  zMLEModel.score_obs{  s   .  	@2 	@ ? @ @ @ &&*h&="= 	<48#: 	< ; < < < ##F3A $ C C +}#' X*D*K K,?KCIK KEEx$7"62tS[[AAG%)F>"$VT_g,24 4 4EEx!&$/&+:< < <EE &&@AAAr7   )r   r   r   r:  )Tr   NFc                    t          t          j        t          j        g|R i |\  }}}}}d|v r|                    d          }|s|rt          d          || j        j         }|r| j        j        rt          d          |dk    r | j        |f|||d|}nY|dk    r | j	        |f|||d|}n@|dk    r|r | j
        |fd	|i|}n'|dk    r | j        |f||d
|}nt          d          |S )a  
        Hessian matrix of the likelihood function, evaluated at the given
        parameters

        Parameters
        ----------
        params : array_like
            Array of parameters at which to evaluate the hessian.
        *args
            Additional positional arguments to the `loglike` method.
        **kwargs
            Additional keyword arguments to the `loglike` method.

        Returns
        -------
        hessian : ndarray
            Hessian matrix evaluated at `params`

        Notes
        -----
        This is a numerical approximation.

        Both args and kwargs are necessary because the optimizer from
        `fit` must call this function and only supports passing arguments via
        args (for example `scipy.optimize.fmin_l_bfgs`).
        r   z^Cannot use complex-step approximations to calculate the hessian with untransformed parameters.Nr   oim)r   r   r:  opgr   r   )r   r:  z#Invalid Hessian calculation method.)r6   rJ   _hessian_param_names_hessian_param_defaultsr*   rA   rm   r   _hessian_oim_hessian_opg_hessian_complex_step_hessian_finite_differencerz   )	r_   r   r.   r/   r   r   r   r:  hessians	            r5   r  zMLEModel.hessian  s   8 6!9** * *"(* * 	JV0/6 vZZ))F 	-2 	- , - - - &&*h&="= 	<48#: 	< ; < < < U??'d';$/$7 /; ; 4:; ;GG u__'d';$/$7 /; ; 4:; ;GG x$70d0; ;$/;39; ;GGx5d5;$/ /; ;39; ;GG &&KLLLr7   c                       | j         |fi | S )zT
        Hessian matrix computed using the Harvey (1989) information matrix
        )rW  r_   r   r/   s      r5   r{  zMLEModel._hessian_oim  s"     10BB6BBBBr7   c                       | j         |fi | S )zi
        Hessian matrix computed using the outer product of gradients
        information matrix
        )r[  r  s      r5   r|  zMLEModel._hessian_opg  s"    
 ,+F==f====r7   c                 J   t          j        |d          }t          j        dt                     |s t          |dd t          |                    }n"t          |dd t          |                    dz  }t          || j        |||          }|| j	        | j
        j        z
  z  S )Nr   r-  zkCalculation of the Hessian using finite differences is usually subject to substantial approximation errors.r1     r0  )r   r/   ra  )re   rg   warningswarnr   r
   r!   r   rb  rW   rm   r   )r_   r   r:  r/   r   r  s         r5   r~  z#MLEModel._hessian_finite_difference	  s    &*** !"2	4 	4 	4  	E"61dCKK@@GG"61dCKK@@1DG(E(/)8: : : $)dh&AABBr7   c                     t          |ddt          |                    }d|d<   d|d<   t          || j        ||          }|| j        | j        j        z
  z  S )zy
        Hessian matrix computed by second-order complex-step differentiation
        on the `loglike` function.
        g      @NTr   r  r]  )r
   r!   r   r&  rW   rm   r   )r_   r   r/   r   r  s        r5   r}  zMLEModel._hessian_complex_step  sl     vr4V== $}!%~ DL'&B B B $)dh&AABBr7   c                 >    t          | d          r| j        S t          )zP
        (array) Starting parameters for maximum likelihood estimation.
        _start_params)r   r  rz   r   s    r5   r   zMLEModel.start_params)  s%    
 4)) 	&%%%%r7   c                     t          | d          r| j        S 	 d t          t          | j                            D             }n# t
          $ r g }Y nw xY w|S )z
        (list of str) List of human readable parameter names (for parameters
        actually included in the model).
        _param_namesc                     g | ]}d |z  S )zparam.%drQ   r   r2   s     r5   r   z(MLEModel.param_names.<locals>.<listcomp>=  s    OOOAaOOOr7   )r   r  r%   r!   r   rz   r_   r,   s     r5   r   zMLEModel.param_names3  su     4(( 	$$OOs4;L7M7M1N1NOOO&   Ls   +A AAc                 p    t          | d          r| j        S d t          | j                  D             }|S )zS
        (list of str) List of human readable names for unobserved states.
        _state_namesc                     g | ]}d |z  S )zstate.%drQ   r  s     r5   r   z(MLEModel.state_names.<locals>.<listcomp>J  s    BBBZ!^BBBr7   )r   r  r%   rX   r  s     r5   state_nameszMLEModel.state_namesB  sB    
 4(( 	C$$BBU4=-A-ABBBEr7   c                 0    t          || j        |          S )a  
        Jacobian matrix for the parameter transformation function

        Parameters
        ----------
        unconstrained : array_like
            Array of unconstrained parameters used by the optimizer.

        Returns
        -------
        jacobian : ndarray
            Jacobian matrix of the transformation, evaluated at `unconstrained`

        See Also
        --------
        transform_params

        Notes
        -----
        This is a numerical approximation using finite differences. Note that
        in general complex step methods cannot be used because it is not
        guaranteed that the `transform_params` method is a real function (e.g.
        if Cholesky decomposition is used).
        )ra  )r   transform_params)r_   unconstrainedr:  s      r5   transform_jacobianzMLEModel.transform_jacobianM  s%    2 ]D,A&57 7 7 	7r7   c                 .    t          j        |d          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
        -----
        This is a noop in the base class, subclasses should override where
        appropriate.
        r   r-  re   rg   )r_   r  s     r5   r  zMLEModel.transform_paramsi  s    , xQ////r7   c                 .    t          j        |d          S )aB  
        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.

        Notes
        -----
        This is a noop in the base class, subclasses should override where
        appropriate.
        r   r-  r  )r_   constraineds     r5   r   zMLEModel.untransform_params  s    * x1----r7   c                    t          j        |d          }t          j        |j        t           j                  r|                    t           j                  }|sO| j        rHt          | j	                  }t          j
        ||j                  t           j        z  }||| j        <   |}|sc|s5| j        r.t          | j                                                  || j        <   |r|                     |          }|                     |          }|s5| j        r.t          | j                                                  || j        <   |r||fn|S )zN
        Ensure model parameters satisfy shape and other requirements
        r   r-  rG  )re   rg   
issubdtyperH  integerastypefloat64rZ   r!   r   r3  nanr^   r   r[   valuesr]   r  r  )r_   r   r   r   rn  r   
new_paramsrs  s           r5   r   zMLEModel.handle_params  sZ   
 &*** =rz22 	/]]2:..F 	 $"8 	 4+,,H(&,???"&HJ28Jt./F 	3
 " 7d&< 7+224455 t/0  B"&"9"9&"A"A**622F 	3$"8 	3T'..0011 4+, -<G((Gr7   c                 2    |                      |||          S )aB  
        Update the parameters of the model

        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.

        Notes
        -----
        Since Model is a base class, this method should be overridden by
        subclasses to perform actual updating steps.
        )r   r   r   )r   )r_   r   r   r   r  s        r5   r   zMLEModel.update  s+    . !![1? " A A 	Ar7   c           	         t          | dd          }|r|dk    r|t          d          t          j        |          }|| j        f}	 |                    |          }ng# t          $ r6 t          dt          |          dt          |j                  d          w xY w|dk    r|d}t          j	        dt                     |S )	a  
        Validate given `exog` as satisfactory for out-of-sample operations

        Parameters
        ----------
        exog : array_like or None
            New observations of exogenous regressors, if applicable.
        out_of_sample : int
            Number of new observations required.

        Returns
        -------
        exog : array or None
            A numpy array of shape (out_of_sample, k_exog) if the model
            contains an `exog` component, or None if it does not.
        r   r   Nz|Out-of-sample operations in a model with a regression component require additional exogenous values via the `exog` argument.zEProvided exogenous values are not of the appropriate shape. Required z, got .zWExogenous array provided, but additional data is not required. `exog` argument ignored.)r@   rA   re   rg   r   reshapestrrV   r  r  r	   )r_   rN   out_of_sampler   required_exog_shapes        r5   _validate_out_of_sample_exogz%MLEModel._validate_out_of_sample_exog  s   " x++ 	(VaZZ|  "5 6 6 6 8D>>D#0$+">6||$788 6 6 6 j$'(;$<$<$<$<$'
OOOO"5 6 6 66
 aZZD,DM G&( ( ( s   	A A Bc                    |                      ||          }|i }t          | dd          dk    r3t          | d          r#|                    d| j        | j        z               | j        dt          j        || j	        f          |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 )a  
        Get updated time-varying state space system matrices

        Parameters
        ----------
        params : array_like
            Array of parameters used to construct the time-varying system
            matrices.
        exog : array_like or None
            New observations of exogenous regressors, if applicable.
        out_of_sample : int
            Number of new observations required.
        extend_kwargs : dict, optional
            Dictionary of keyword arguments to pass to the state space model
            constructor. For example, for an SARIMAX state space model, this
            could be used to pass the `concentrate_scale=True` keyword
            argument. Any arguments that are not explicitly set in this
            dictionary will be copied from the current model instance.
        transformed : bool, optional
            Whether or not `start_params` is already transformed. Default is
            True.
        includes_fixed : bool, optional
            If parameters were previously fixed with the `fix_params` method,
            this argument describes whether or not `start_params` also includes
            the fixed parameters, in addition to the free parameters. Default
            is False.
        Nk_trendr   trend_offset)rM   rN   r   obsrs   r   ).r   .rQ   )r  r@   r   r   r  rW   r   re   r3  ro   r   rm   shapesr   rV   any)r_   r   rN   r  extend_kwargsr   r   r/   
mod_extendr3   originalextendedsoses                 r5   $_get_extension_time_varying_matricesz-MLEModel._get_extension_time_varying_matrices  s   > 00}EE  M 4A&&**wt^/L/L*$$ 1DI =? ? ?  TZ (M4<899  
 	&k)7 	 	9 	9 	9
 HO((** 
	> 
	>Du}}tx..Hz~t44H#B#Ba266!GG	QF8F+x/??@@ !/'m^__(<=tr7   c                    |                      |||           ||dk    rd}nC|dk    r| j        }n5|                     |          \  }}}t          |t                    r|j        }|dk     r
| j        |z   }|| j        k    rt          d          |dk    r|t          d          t          ||z   | j        z
  d          }|	| j        dup| j	        j
         }	|r|	r | j        ||||
f||d|}|8t          j        |          }|j        dk     rt          j        |          j        }t#          | j        ||z             }||z   |z
  } | j	        j        t          j        || j        f          f||d	|}|d
n|}t          j        || j        |f          }d}t+          |          D ]_}d}|*|j        d
         d
k    r|dddf         }n|dd|f         }|                    ||||||||d|
  
        \  }}}||dddd|f<   `t          | j        t2                    }d}|r!|                     |||z   d
z
            \  }}}}|e| j        d
k    r,|ddddf         }|rt7          j        ||| j                  }n|dddddf         }|rt7          j        ||| j                  }n|r|j        } | j        }!t          |!t>                    s|!g}!t6          j         !                    |!t          j"        | d                   g          }"t7          j        |#                    | d         | d
         | d         z            ||"          }|S )a  
        Simulate a new time series following the state space model

        Parameters
        ----------
        params : array_like
            Array of parameters to use in constructing the state space
            representation to use when simulating.
        nsimulations : int
            The number of observations to simulate. If the model is
            time-invariant this can be any number. If the model is
            time-varying, then this number must be less than or equal to the
            number of observations.
        measurement_shocks : array_like, optional
            If specified, these are the shocks to the measurement equation,
            :math:`\varepsilon_t`. If unspecified, these are automatically
            generated using a pseudo-random number generator. If specified,
            must be shaped `nsimulations` x `k_endog`, where `k_endog` is the
            same as in the state space model.
        state_shocks : array_like, optional
            If specified, these are the shocks to the state equation,
            :math:`\eta_t`. If unspecified, these are automatically
            generated using a pseudo-random number generator. If specified,
            must be shaped `nsimulations` x `k_posdef` where `k_posdef` is the
            same as in the state space model.
        initial_state : array_like, optional
            If specified, this is the initial state vector to use in
            simulation, which should be shaped (`k_states` x 1), where
            `k_states` is the same as in the state space model. If unspecified,
            but the model has been initialized, then that initialization is
            used. This must be specified if `anchor` is anything other than
            "start" or 0 (or else you can use the `simulate` method on a
            results object rather than on the model object).
        anchor : int, str, or datetime, optional
            First period for simulation. The simulation will be conditional on
            all existing datapoints prior to the `anchor`.  Type depends on the
            index of the given `endog` in the model. Two special cases are the
            strings 'start' and 'end'. `start` refers to beginning the
            simulation at the first period of the sample, and `end` refers to
            beginning the simulation at the first period after the sample.
            Integer values can run from 0 to `nobs`, or can be negative to
            apply negative indexing. Finally, if a date/time index was provided
            to the model, then this argument can be a date string to parse or a
            datetime type. Default is 'start'.
        repetitions : int, optional
            Number of simulated paths to generate. Default is 1 simulated path.
        exog : array_like, optional
            New observations of exogenous regressors, if applicable.
        transformed : bool, optional
            Whether or not `params` is already transformed. Default is
            True.
        includes_fixed : bool, optional
            If parameters were previously fixed with the `fix_params` method,
            this argument describes whether or not `params` also includes
            the fixed parameters, in addition to the free parameters. Default
            is False.
        pretransformed_measurement_shocks : bool, optional
            If `measurement_shocks` is provided, this flag indicates whether it
            should be directly used as the shocks. If False, then it is assumed
            to contain draws from the standard Normal distribution that must be
            transformed using the `obs_cov` covariance matrix. Default is True.
        pretransformed_state_shocks : bool, optional
            If `state_shocks` is provided, this flag indicates whether it
            should be directly used as the shocks. If False, then it is assumed
            to contain draws from the standard Normal distribution that must be
            transformed using the `state_cov` covariance matrix. Default is
            True.
        pretransformed_initial_state : bool, optional
            If `initial_state` is provided, this flag indicates whether it
            should be directly used as the initial_state. If False, then it is
            assumed to contain draws from the standard Normal distribution that
            must be transformed using the `initial_state_cov` covariance
            matrix. Default is True.
        random_state : {None, int, Generator, RandomState}, optional
            If `seed` is None (or `np.random`), the
            class:``~numpy.random.RandomState`` singleton is used.
            If `seed` is an int, a new class:``~numpy.random.RandomState``
            instance is used, seeded with `seed`.
            If `seed` is already a class:``~numpy.random.Generator`` or
            class:``~numpy.random.RandomState`` instance then that instance is
            used.

        Returns
        -------
        simulated_obs : ndarray
            An array of simulated observations. If `repetitions=None`, then it
            will be shaped (nsimulations x k_endog) or (nsimulations,) if
            `k_endog=1`. Otherwise it will be shaped
            (nsimulations x k_endog x repetitions). If the model was given
            Pandas input then the output will be a Pandas object. If
            `k_endog > 1` and `repetitions` is not None, then the output will
            be a Pandas DataFrame that has a MultiIndex for the columns, with
            the first level containing the names of the `endog` variables and
            the second level containing the repetition number.

        See Also
        --------
        impulse_responses
            Impulse response functions
        r   Nrq   r   end/Cannot anchor simulation outside of the sample.zWIf `anchor` is after the start of the sample, must provide a value for `initial_state`.r0  rq   r  r   T)!pretransformed_measurement_shockspretransformed_state_shockspretransformed_initial_state	simulatorreturn_simulatorrandom_state)r>   r3   r>   columns)$r   rW   _get_index_locr"   slicerq   rA   maxrN   rm   time_invariantr  re   rg   rj   
atleast_2drl   minextendr3  ro   r%   rV   simulater8   r   _get_prediction_indexr;   r<   endog_namesr=   r   
MultiIndexfrom_productr   r  )#r_   r   nsimulationsmeasurement_shocksstate_shocksr   anchorrepetitionsrN   extend_modelr  r   r   r  r  r  r  r/   iloc_r  r  nextend	sim_model_repetitionssimr  r2   initial_state_variatesrr  
use_pandasr>   rV   r  r  s#                                      r5   r  zMLEModel.simulate?  sf   Z 	F#1 	 	3 	3 	3 >Vw..DDu__9DD,,V44JD!Q$&& "z!889t#D$)NOOO!88- J K K K D</$);Q??9D0O8O4OL 	\ 	>T>m]'   F $H]33M!A%% "m < < > $)TL011%+#DHOBHgt|-D$E$E C*.CC C;AC C	 (/qq[hdlLABB	|$$ 	 	A%)"( &q)Q..-:111a4-@**-:111a4-@*
 !* 2 20,&5,G-I#d) !3 !+ !+CI C111aLL  	:66
 	/!77d\)A-/ /NAq!U |q  !!!Q'l M)Cu4;KLLLC!!!QQQ'l A,s%/3/?A A AC 	=IE*Kk400 ,*mm00+24)E!H2E2E2G H HG,s{{58U1Xa5HII%*G= = =C 
r7   r   c                    |                      ||
|           d}| j        j        j        d         dk    r8| j        j        j        d         dk    r| j        j        j        d         dk    rd}||dk    rd}nF|dk    r| j        dz
  }n5|                     |          \  }}}t          |t                    r|j
        }|dk     r
| j        |z   }|| j        k    rt          d          | j        j        j        d         | j        j        j        d         cxk    oY| j        j        j        d         cxk    o=| j        j        j        d         cxk    o!| j        j        j        d         cxk    odk    nc }t          |||z   dz   z   | j        z
  d          }|| j        duo| }|r|r | j        ||||	f|
|d|}t#          | j        ||z   |z             }|||z   dz   z   |z
  }d	|vr9| j        j        j        d         dk    rt'          j        | j        |f          |d	<   d
|vr9| j        j        j        d         dk    rt'          j        | j        |f          |d
<   d|vr?| j        j        j        d         dk    r$t'          j        | j        | j        |f          |d<   d|vre| j        j        j        d         dk    rJt'          j        | j        j        | j        j        |f          }| ddddd||dz   f         |dd<   ||d<   d|vr`| j        j        j        d         dk    rEt'          j        | j        | j        j        |f          }| ddddd||dz   f         |dd<   ||d<    | j        j        t'          j        || j        f          f||d|}t          | j        t8                    }t;          |          t<          u r+|st          d          | j                             |          }|!                    ||||          }|j        d         dk    r|dddf         }|rB| j        dk    rtE          j#        || j                  }ntE          j$        || j                  }|S )a  
        Impulse response function

        Parameters
        ----------
        params : array_like
            Array of model parameters.
        steps : int, optional
            The number of steps for which impulse responses are calculated.
            Default is 1. Note that for time-invariant models, the initial
            impulse is not counted as a step, so if `steps=1`, the output will
            have 2 entries.
        impulse : int, str or array_like
            If an integer, the state innovation to pulse; must be between 0
            and `k_posdef-1`. If a str, it indicates which column of df
            the unit (1) impulse is given.
            Alternatively, a custom impulse vector may be provided; must be
            shaped `k_posdef x 1`.
        orthogonalized : bool, optional
            Whether or not to perform impulse using orthogonalized innovations.
            Note that this will also affect custum `impulse` vectors. Default
            is False.
        cumulative : bool, optional
            Whether or not to return cumulative impulse responses. Default is
            False.
        anchor : int, str, or datetime, optional
            Time point within the sample for the state innovation impulse. Type
            depends on the index of the given `endog` in the model. Two special
            cases are the strings 'start' and 'end', which refer to setting the
            impulse at the first and last points of the sample, respectively.
            Integer values can run from 0 to `nobs - 1`, or can be negative to
            apply negative indexing. Finally, if a date/time index was provided
            to the model, then this argument can be a date string to parse or a
            datetime type. Default is 'start'.
        exog : array_like, optional
            New observations of exogenous regressors for our-of-sample periods,
            if applicable.
        transformed : bool, optional
            Whether or not `params` is already transformed. Default is
            True.
        includes_fixed : bool, optional
            If parameters were previously fixed with the `fix_params` method,
            this argument describes whether or not `params` also includes
            the fixed parameters, in addition to the free parameters. Default
            is False.
        **kwargs
            If the model has time-varying design or transition matrices and the
            combination of `anchor` and `steps` implies creating impulse
            responses for the out-of-sample period, then these matrices must
            have updated values provided for the out-of-sample steps. For
            example, if `design` is a time-varying component, `nobs` is 10,
            `anchor=1`, and `steps` is 15, a (`k_endog` x `k_states` x 7)
            matrix must be provided with the new design matrix values.

        Returns
        -------
        impulse_responses : ndarray
            Responses for each endogenous variable due to the impulse
            given by the `impulse` argument. For a time-invariant model, the
            impulse responses are given for `steps + 1` elements (this gives
            the "initial impulse" followed by `steps` responses for the
            important cases of VAR and SARIMAX models), while for time-varying
            models the impulse responses are only given for `steps` elements
            (to avoid having to unexpectedly provide updated time-varying
            matrices).

        See Also
        --------
        simulate
            Simulate a time series according to the given state space model,
            optionally with specified series for the innovations.

        Notes
        -----
        Intercepts in the measurement and state equation are ignored when
        calculating impulse responses.

        TODO: add an option to allow changing the ordering for the
              orthogonalized option. Will require permuting matrices when
              constructing the extended model.
        r   r   r0  r   Nrq   r  z6Cannot anchor impulse responses outside of the sample.obs_interceptstate_interceptobs_cov	state_cov	selectionr  zEndog must be pd.DataFrame.r3   r  )%r   rm   _designrV   _transition
_selectionrW   r  r"   r  rq   rA   _obs_cov
_state_covr  rN   r  r  _obs_interceptre   r3  ro   _state_interceptrX   k_posdefr  emptyr8   r   typer  r  r>   impulse_responsesr;   r<   r=   )r_   r   stepsimpulseorthogonalized
cumulativer  rN   r  r  r   r   r/   additional_stepsr  r  r  r  r  r  rR  r  r  irfss                           r5   r  zMLEModel.impulse_responses  sj   l 	F#1 	 	3 	3 	3 H"1%**$*1-22#)!,11  >Vw..DDu__9q=DD,,V44JD!Q$&& "z!889t#D49 ( ) ) ) H"1%):)@)C . . . .H &q). . . .-1X-@-Fq-I. . . .H%a(. . . .,-. . . . 	 E,,q01DI=qB B9D0G5GL 	\ 	>T>m]'   F $)TE\,<<==%"22Q67#=6))'-a0144&(hg/F&G&GF?#V++)/2Q66(*$-1I(J(JF$%F""tx'8'>q'AA'E'E "$,g)N O OF9 f$$)<)B1)E)I)I(DH-tx/@'JKKC+qqq!!!T$(]:;CF"%F;f$$)<)B1)E)I)I(DM48+<gFGGC+qqq!!!T$(]:;CF"%F; $DHOBHgt|-D$E$E C*.CC C;AC C	  	:66
==C @ !>???&,,W55G**7NJ8 8 :a=A1:D  	D|q  yD,<===|D$2BCCCr7   c                     t           )z8
        Not implemented for state space models
        r   )clsformular8   subsets       r5   from_formulazMLEModel.from_formula  s
    
 "!r7   )NNNr   )NTFNNr   r   r   r   NFNNNNF)NNNN)	TFFNNFNNF)TFFNNFNN)TFNFN)TFNF)TFN)F)T)r   TFNF)NTF)NNNNNNNNTFTTTN)
r   r   FFNNNNTF)K__name__
__module____qualname____doc__rS   rU   rY   r   r   r   r   r   r   r   r   r   r   r   r   r   r   propertyr   setterr   r   r   r   
contextlibcontextmanagerr   r   r  r  r  r  r  r$  r%  r&  r(  r+  rD  rW  r[  r^  rb  rh  rf  rp  rq  rt  rY  ry  rz  r  r{  r|  r~  r}  r   r   r  r  r  r   r   r   r  r  r  r  classmethodr  __classcell__r`   s   @r5   rJ   rJ   U   s       . .`' ' ' ' ' '6  "( ( (.$ $ $L0 0 0) ) )    - - - -:  < < < <,B B B B0B B B B0@ @ @ @0@ @ @ @,D D D: : : :) ) ) ' ' X' ( ( ( ) ) X) * * * + + X+ , , , " " X" # # #/ / / :> :> :>x GLBD@EEI#(	V V V V V Vp   8 8 8 X8 BFGK   & ?D;?/36;59 59 59 59n ?D;?/3%),9 ,9 ,9 ,9\ MLL2220 0 0d CH %*H *H *H *HX; ; ; ;( HL<AAE=B15	`F `F `F `FD ?C388<49ZN ZN ZN ZNx :>IM$
 $
 $
 $
L/ / /7 7 7 7) ) ) )
 =A@EA A A AFD D D@@@G G GR >B<@"'6 6 6 6pF F F;;;C C CJC C C> > >C C C C$C C C & & X&   X   X7 7 7 780 0 00. . .. FK&+"H "H "H "HH ?D!A A A A4' ' 'T >B-2> > > >@ AE?C;?FK37-1AEP P P Pd :;IMFJ;@| | | || " " " [" " " " "r7   rJ   c                   .   e Zd ZdZdCdZdDdZed             Zed             Zed             Z		 	 dEdZ
ed             ZdEdZed             ZdEdZed             Zed             Z	 	 dEdZed             Z	 	 dEdZed             ZdFd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d              Zed!             Z ed"             Z!ed#             Z"d$ Z#	 	 dGd&Z$dHd'Z%	 	 	 	 dId)Z&dJd+Z'	 	 dKd,Z(dJd-Z)	 	 	 	 	 	 	 	 dLd.Z*	 	 dMd0Z+dHd1Z,	 	 dCd2Z-	 	 dHd3Z.	 dHd4Z/	 dHd5Z0	 dHd6Z1	 	 	 	 dNd8Z2	 	 dOd:Z3	 	 dPd;Z4dCd<Z5	 	 dPd=Z6	 	 	 dQd@Z7	 	 	 	 dRdBZ8dS )Sr  a  
    Class to hold results from fitting a state space model.

    Parameters
    ----------
    model : MLEModel instance
        The fitted model instance
    params : ndarray
        Fitted parameters
    filter_results : KalmanFilter instance
        The underlying state space model and Kalman filter output

    Attributes
    ----------
    model : Model instance
        A reference to the model that was fit.
    filter_results : KalmanFilter instance
        The underlying state space model and Kalman filter output
    nobs : float
        The number of observations used to fit the model.
    params : ndarray
        The parameters of the model.
    scale : float
        This is currently set to 1.0 unless the model uses concentrated
        filtering.

    See Also
    --------
    MLEModel
    statsmodels.tsa.statespace.kalman_filter.FilterResults
    statsmodels.tsa.statespace.representation.FrozenRepresentation
    Nc           
      H    |j          _         |j        }t          j                             ||d |            j        j         _         j        j         _         j        j         _         j        rO j        j	        
                                 _	        t           j	                                                   _        nd  _	        g  _         fd j         j        pg D              _        | _        t!          |t"                    r| _        nd  _         j        j         _         j        j         _         j        dk    r j        dk    rt-          j        d            j         j        z
   _         j        j        }|dn)t5          j        t5          j        |          dk               _         j        j        tA           j                  z
  }	|	 j        z    j        j!        z    _"         j         j"        z
   _#        tI           d          si  _%        ||j&        rdnd}| _'        i  _(        |i }|)                    d	d
           _*        |)                    dd           _+        	 d  _,          j-        d|d
d| ng# t4          j.        j/        $ rP d _,        tA           j                  }
t5          j0        |
|
f          t4          j1        z   _2        d j%        d<   Y nw xY w j        3                     j        d
d
           g d}|D ]'}ti           |tk           j        |d                      ( j        j6        rd  _7        d  _8         j        j9        rd  _:         j        j;        rd  _<         j        j=        rd  _>         j        j?        rd  _@         j        jA        rd  _B         j        jC        r	  j        jD        r	  j        jE        rd  _F        t                       _H        t!           j         t                    } j        jJ        } j        jK        } j<         j        j;        rd  jH        _L        n]|rE j        M                                }t          jO         j<        jP        ||           jH        _L        n j<        jP         jH        _L         j>         j        j=        rd  jH        _Q        n|r j        M                                }t5          jR         j>        d          }t          jO        t5          jS        ||jT        d         |jT        d         z  |jT        d         f          t          jU        V                    ||g          W                                |           jH        _Q        n$t5          jR         j>        d           jH        _Q         j@         j        j?        rd  jH        _X        nD|r,t          jO         j@        jP        ||           jH        _X        n j@        jP         jH        _X         jB         j        jA        rd  jH        _Y        n|rt5          jR         jB        d          }t          jO        t5          jS        ||jT        d         |jT        d         z  |jT        d         f          t          jU        V                    ||g          W                                |           jH        _Y        n$t5          jR         jB        d           jH        _Y         jZ        d  jH        _[        nD|r,t          jO         jZ        jP        ||           jH        _[        n jZ        jP         jH        _[         j\        d  jH        _]        n|rt5          jR         j\        d          }t          jO        t5          jS        ||jT        d         |jT        d         z  |jT        d         f          t          jU        V                    ||g          W                                |           jH        _]        n$t5          jR         j\        d           jH        _]        tk           dg            _^         j^        _                    dg            j`        _                    |            j`        _                    ddg           d S )Nnormalized_cov_paramsscalec                 0    g | ]}|j         v rd |z  n|S )z
%s (fixed))fixed_paramsr   s     r5   r   z'MLEResults.__init__.<locals>.<listcomp>	  s@     7 7 7 $(4+<#<#<L4$7 7 7r7   r   zCare should be used when applying a loglikelihood burn to a model with exact diffuse initialization. Some results objects, e.g. degrees of freedom, expect only one of the two to be set.r   r   r   rx  r   Tr:  F)r   use_selfzHCovariance matrix could not be calculated: singular. information matrix.r   r   )filtered_statefiltered_state_covpredicted_statepredicted_state_cov	forecastsr6  r8  standardized_forecasts_errorforecasts_error_diffuse_covpredicted_diffuse_state_covscaled_smoothed_estimatorscaled_smoothed_estimator_covsmoothing_errorsmoothed_statesmoothed_state_covsmoothed_state_autocov smoothed_measurement_disturbancesmoothed_state_disturbance$smoothed_measurement_disturbance_covsmoothed_state_disturbance_covr  )r0  r   r   r0  _data_attr_modelrm   filter_resultssmoother_resultsrQ   )ar8   r  tsbaseTimeSeriesModelResultsrS   modelrZ   r]   r^   r[   rb   r   r   r  r   r"  r"   r   r#  rW   rK  r   r  r  nobs_effectiveinitial_diffuse_state_covre   rg  diagonalk_diffuse_statesr   sizer!   filter_concentrateddf_modeldf_residr   r   memory_no_likelihoodr   _cacher*   _cov_approx_complex_step_cov_approx_centered_rank_get_robustcov_resultsrL  LinAlgErrorr3  r  cov_params_defaultr   setattrr@   memory_no_forecast_meanr  r6  memory_no_forecast_covr8  memory_no_predicted_meanr  memory_no_predicted_covr  memory_no_filtered_meanr  memory_no_filtered_covr  r  r  memory_no_std_forecastr  r   _statesr   ru   r  	predictedr   r;   r=   rl   predicted_covrZ  r  rV   r  r  	swaplevelfilteredfiltered_covr  smoothedr  smoothed_covr!  r  
_data_attr)r_   r&  r   resultsr   r   r/   r  Pk_free_paramsr   extra_arraysr3   r  r>   r  extended_indexrR  s   `                 r5   rS   zMLEResults.__init__  s   J	%..tUFEI5: 	/ 	< 	< 	<
 "&!=#':#A "&*"? ! 	#!%!9!>!>!@!@D $T%7%<%<%>%> ? ?D!%D "D7 7 7 7.4"7 7 7
 &g// 	)$+D!!$(D! ',	 /<q  T%<q%@%@M C D D D #i$*AA9%&YBF2;q>>Q;N4O4O (3t/@+A+AA&)>>.BC+dm; tZ(( 	DM#*#?JxxUH   HLL.55 	%$,LL1BE$J$J!
	(DJ'D' 4D 4 4*24 4 4 4y$ 	( 	( 	(DJ4;''H&(h(/C&D&Drv&MD#' M*%%%		( 	
$+4MMM. . . ! 	J 	JDD$(;T4 H HIIII 6 	(!DN#'D 5 	,'+D$7 	(#'D 6 	,'+D$6 	'"&D5 	+&*D#- 	2 	5 	504D- '((	:66

!*(
  (#< )%)DL"" 	<!ZCCEEN%'\$&ng&O &O &ODL"" &*%9%;DL"$,#; -)-DL&& 
	5!ZCCEEN,t7CCC)+
31	!!<cil KLLm00#W-/ //8y{{	*! *! *!DL&& *,()*5 *5DL& '#; ($(DL!! 	:$&L#%UG%E %E %EDL!! %)$7$9DL!#+#: ,(,DL%% 	4,t6	BBC(*
31	!!<cil KLLm00%1ABBLLNN)! )! )!DL%%
 )+')4 )4DL% &$(DL!! 	:$&L#%UG%E %E %EDL!! %)$7$9DL!"*(,DL%% 	4,t6	BBC(*
31	!!<cil KLLm00%1ABBLLNN)! )! )!DL%%
 )+')4 )4DL% !(.@" E E$$eW---|,,, 02DEFFFFFs   7J A!K32K3rx  c                    ddl m} |                    dd          }|r| }nt          ||_	        i |_
        | j        }|rd}n| j        rd}nd}t          | j                  }|dk    r,t          j        d	          |_        d|_        d
|j
        d<   nC|dk    r|d         |_	        |d         |_        |d         |j
        d<   t          | j                  dk    r t          j        | j        | j                  }	nt          j        d         }	t          j                            |j        |	                   |_        n|dk    rKt          j        ||f          t          j        z  |_        t          j        |_        |d         |j
        d<   nG| j	        dk    r2|j        |_        |d                             |          |j
        d<   n
| j	        dk    r1|j        |_        |d                             |          |j
        d<   n| j	        dk    r1|j        |_        |d                             |          |j
        d<   n| j	        dk    s| j	        dk    r1|j        |_        |d                             |          |j
        d<   nK| j	        dk    r1|j        |_        |d                             |          |j
        d<   nt          d          |S )a  
        Create new results instance with specified covariance estimator as
        default

        Note: creating new results instance currently not supported.

        Parameters
        ----------
        cov_type : str
            the type of covariance matrix estimator to use. See Notes below
        kwargs : depends on cov_type
            Required or optional arguments for covariance calculation.
            See Notes below.

        Returns
        -------
        results : results instance
            This method creates a new results instance with the requested
            covariance as the default covariance of the parameters.
            Inferential statistics like p-values and hypothesis tests will be
            based on this covariance matrix.

        Notes
        -----
        The following covariance types and required or optional arguments are
        currently available:

        - 'opg' for the outer product of gradient estimator
        - 'oim' for the observed information matrix estimator, calculated
          using the method of Harvey (1989)
        - 'approx' for the observed information matrix estimator,
          calculated using a numerical approximation of the Hessian matrix.
          Uses complex step approximation by default, or uses finite
          differences if `approx_complex_step=False` in the `cov_kwds`
          dictionary.
        - 'robust' for an approximate (quasi-maximum likelihood) covariance
          matrix that may be valid even in the presence of some
          misspecifications. Intermediate calculations use the 'oim'
          method.
        - 'robust_approx' is the same as 'robust' except that the
          intermediate calculations use the 'approx' method.
        - 'none' for no covariance matrix calculation.
        r   )descriptionsr  Fr	  zcomplex-stepzcentered finite differenceszfinite differences)r   r   zNo parameters estimated.descriptioncustomcustom_cov_typecustom_cov_paramscustom_description.rL   r   )approx_typerw  OIMrx  OPGrobust
robust_oimz
robust-OIMrobust_approxzrobust-approxzInvalid covariance matrix type.)statsmodels.base.covtyperN  r*   rz   r`   r&  r   r
  r  r   r   r1  r2  r!   re   r3  r6  r3  r  ix_r^   s_rL  matrix_rankr  cov_params_approxformatcov_params_oimcov_params_opgcov_params_robust_oimcov_params_robust_approx)
r_   r   r/   rN  r  r  r   approx_type_strr   masks
             r5   r4  z!MLEResults._get_robustcov_results	  s   X 	:99999::j%00 	"CC%%   #; 	3,OO& 	3;OO2Ot{##q==%'Xf%5%5C"CI*DCL''!!!"34CL%+,?%@C"*01E*FCL'4$%%))vd5t7NOOuSz	--c.DT.JKKCII%'Xx.B%C%Cbf%LC"CI*6v*>CL'']h&&%(%:C"*6x*@*G*G<K +H +M +MCL'']e##%(%7C"*6u*=*D*D<K +E +M +MCL'']e##%(%7C"*6u*=*D*D<K +E +M +MCL'']h&&$-<*G*G%(%>C"*6|*D*K*K<K +L +M +MCL'']o--%(%AC"*6*G*N*N<K +O +M +MCL'' &&GHHH
r7   c                 B    t          | j        | j        | j                  S )z6
        (float) Akaike Information Criterion
        )r   llfr'  r-  r   s    r5   r   zMLEResults.aic<
      
 48T0$-@@@r7   c                 B    t          | j        | j        | j                  S )zS
        (float) Akaike Information Criterion with small sample correction
        )r   rg  r'  r-  r   s    r5   r   zMLEResults.aiccC
  s    
 DHd14=AAAr7   c                 B    t          | j        | j        | j                  S )z5
        (float) Bayes Information Criterion
        )r   rg  r'  r-  r   s    r5   r   zMLEResults.bicJ
  rh  r7   TFc           	      0   | j         | j                            | j        ddd||          z  }t	          | j                  dk    r^t          j        | j        | j                  }t          ||                   \  }}t          j
        |          t          j        z  }|||<   nt          |          \  }}| j                            | j        dd           | j        6t          j                            t          j        |                    | _        | S )NTr   )r   r   r   r   r   r:  r   r   r'  r&  r  r   r!   r  re   r[  r^   r   
zeros_liker  r   r3  rL  r]  diagr_   r   r:  evaluated_hessianre  rR  singular_valuesneg_covs           r5   _cov_params_approxzMLEResults._cov_params_approxQ
  s    /$*2D2D;D1D+ 3E 3- 3- - t !!A%%6$143JKKD%23DT3J%K%K"S/m$566?GGDMM)67H)I)I&Wo
$+4MMM:..rw/G/GHHDJxr7   c                 B    |                      | j        | j                  S )z
        (array) The variance / covariance matrix. Computed using the numerical
        Hessian approximated by complex step or finite differences methods.
        )rs  r1  r2  r   s    r5   r^  zMLEResults.cov_params_approxg
  s*     &&t'D'+'@B B 	Br7   c           	      0   | j         | j                            | j        ddd||          z  }t	          | j                  dk    r^t          j        | j        | j                  }t          ||                   \  }}t          j
        |          t          j        z  }|||<   nt          |          \  }}| j                            | j        dd           | j        6t          j                            t          j        |                    | _        | S )Nrw  Tr   r   r   r   r:  r   r   rl  ro  s           r5   _cov_params_oimzMLEResults._cov_params_oimp
  s    /$*2D2DK45H+ 3E 3- 3- -
 t !!A%%6$143JKKD%23DT3J%K%K"S/m$566?GGDMM)67H)I)I&Wo
$+4MMM:..rw/G/GHHDJxr7   c                 B    |                      | j        | j                  S )zq
        (array) The variance / covariance matrix. Computed using the method
        from Harvey (1989).
        )rw  r1  r2  r   s    r5   r`  zMLEResults.cov_params_oim
  '     ##D$A$($=? ? 	?r7   c                    | j         | j                            | j        dd||          z  }| j        d uot          | j                  dk    }|r6t          j        |          t          j        z  }t          j	        d          }nt          | j
                  dk    r^t          j        | j        | j                  }t          ||                   \  }}t          j        |          t          j        z  }|||<   nt          |          \  }}| j                            | j        dd           | j        @|rd| _        n6t          j                            t          j        |                    | _        | S )NT)r   r   r   r:  r   r   )r'  r&  r|  r   r^   r!   re   rm  r  r  r  r[  r   r   r3  rL  r]  rn  )	r_   r   r:  rp  no_free_paramsrr  rq  re  rR  s	            r5   _cov_params_opgzMLEResults._cov_params_opg
  so    /$*2I2IKT$ 3+ 3J 3- 3- -
 1= <d566!; 	  		Jm$566?G hqkkOO"##a''6$143JKKD%23DT3J%K%K"S/m$566?GGDMM)67H)I)I&Wo
$+4MMM: M

Y2227?3K3KLL
xr7   c                 B    |                      | j        | j                  S )zy
        (array) The variance / covariance matrix. Computed using the outer
        product of gradients method.
        )r|  r1  r2  r   s    r5   ra  zMLEResults.cov_params_opg
  ry  r7   c                     | j         S )zj
        (array) The QMLE variance / covariance matrix. Alias for
        `cov_params_robust_oim`
        )rb  r   s    r5   cov_params_robustzMLEResults.cov_params_robust
  s     ))r7   c           	         |                      ||          }| j        | j                            | j        ddd||          z  }t          | j                  dk    rt          j        | j	        | j	                  }t          j
        |          t          j        z  }||         }||         }t          t          j        t          j        ||          |                    \  }}|||<   n8t          t          j        t          j        ||          |                    \  }}| j                            | j        dd           | j        6t          j                            t          j        |                    | _        |S )Nr   r:  rw  Trv  r   r   r|  r'  r&  r  r   r!   r  re   r[  r^   rm  r  r   rN  r   r3  rL  r]  rn  	r_   r   r:  cov_opgrp  re  
cov_paramsrR  rq  s	            r5   _cov_params_robust_oimz!MLEResults._cov_params_robust_oim
  s   &&;N7F ' H H !/$*2D2DK45H+ 3E 3- 3- -
 t !!A%%6$143JKKD'899BFBJdmG 1$ 7#0rv/99;LMM$O $O C  #Jt,9rv/99;LMM-O -O)Z 	
$+4MMM:..rw/G/GHHDJr7   c                 B    |                      | j        | j                  S )z
        (array) The QMLE variance / covariance matrix. Computed using the
        method from Harvey (1989) as the evaluated hessian.
        )r  r1  r2  r   s    r5   rb  z MLEResults.cov_params_robust_oim
  s*     **4+H+/+DF F 	Fr7   c                    |                      ||          }| j        | j                            | j        ddd|          z  }t          | j                  dk    rt          j        | j	        | j	                  }t          j
        |          t          j        z  }||         }||         }t          t          j        t          j        ||          |                    \  }}|||<   n8t          t          j        t          j        ||          |                    \  }}| j                            | j        dd           | j        6t          j                            t          j        |                    | _        |S )Nr  Tr   )r   r   r   r   r   r   r  r  s	            r5   _cov_params_robust_approxz$MLEResults._cov_params_robust_approx
  s   &&;N7F ' H H !/$*2D2DKT$1D 3E 3F 3F F t !!A%%6$143JKKD'899BFBJdmG 1$ 7#0rv/99;LMM$O $O C  #Jt,9rv/99;LMM-O -O)Z 	
$+4MMM:..rw/G/GHHDJr7   c                 B    |                      | j        | j                  S )z
        (array) The QMLE variance / covariance matrix. Computed using the
        numerical Hessian as the evaluated hessian.
        )r  r1  r2  r   s    r5   rc  z#MLEResults.cov_params_robust_approx
  s*     --d.K.2.GI I 	Ir7   standardc           	         |                                 }|                                 }|dk    rt          | |          }n|dk    r| j        j        j        d         dk    rt          d          | j        j        dddddf         }|dk    rLt          j        t          j        	                    |          d         d	| j
        z  | j        z  z             }n|d
k    rbt          j        t          j        	                    |          d         | j
        t          j        | j                  z  | j        z  z             }n|dk    rwt          j        t          j        	                    |          d         d	| j
        z  t          j        t          j        | j                            z  | j        z  z             }nt          d          t          d          |S )uk  
        Information criteria

        Parameters
        ----------
        criteria : {'aic', 'bic', 'hqic'}
            The information criteria to compute.
        method : {'standard', 'lutkepohl'}
            The method for information criteria computation. Default is
            'standard' method; 'lutkepohl' computes the information criteria
            as in Lütkepohl (2007). See Notes for formulas.

        Notes
        -----
        The `'standard'` formulas are:

        .. math::

            AIC & = -2 \log L(Y_n | \hat \psi) + 2 k \\
            BIC & = -2 \log L(Y_n | \hat \psi) + k \log n \\
            HQIC & = -2 \log L(Y_n | \hat \psi) + 2 k \log \log n \\

        where :math:`\hat \psi` are the maximum likelihood estimates of the
        parameters, :math:`n` is the number of observations, and `k` is the
        number of estimated parameters.

        Note that the `'standard'` formulas are returned from the `aic`, `bic`,
        and `hqic` results attributes.

        The `'lutkepohl'` formulas are (Lütkepohl, 2010):

        .. math::

            AIC_L & = \log | Q | + \frac{2 k}{n} \\
            BIC_L & = \log | Q | + \frac{k \log n}{n} \\
            HQIC_L & = \log | Q | + \frac{2 k \log \log n}{n} \\

        where :math:`Q` is the state covariance matrix. Note that the Lütkepohl
        definitions do not apply to all state space models, and should be used
        with care outside of SARIMAX and VARMAX models.

        References
        ----------
        .. [*] Lütkepohl, Helmut. 2007. *New Introduction to Multiple Time*
           *Series Analysis.* Berlin: Springer.
        r  	lutkepohlrs   r   uZ   Cannot compute Lütkepohl statistics for models with time-varying state covariance matrix.Nr   r   r0  r   r   zInvalid information criteriaz/Invalid information criteria computation method)lowerr@   r"  r  rV   rA   re   squeezerL  slogdetr-  r'  log)r_   criteriar   rr  covs        r5   info_criteriazMLEResults.info_criteria  s   ^ >>##Z$))CC{""",226::  ", - - - %/111a8C5  j!2!23!7!7!:!"T]!2T5H!H"I J JU""j!2!23!7!7!:!%8K1L1L!L!%!4"5"5 6 6 V##j!2!23!7!7!:!"T]!2!#t/B(C(C!D!D"E!%!4"5"5 6 6
 !!?@@@ NOOO
r7   c                 d    | j         }|n%|j        d         dk    r|dddf         }n|j        }|S )zW
        (array) The predicted values of the model. An (nobs x k_endog) array.
        Nr   r   )r  rV   rl   )r_   fittedvaluess     r5   r  zMLEResults.fittedvaluesW  sH     ~"a'''111-LL'>Lr7   c                 B    t          | j        | j        | j                  S )z<
        (float) Hannan-Quinn Information Criterion
        )r   rg  r'  r-  r   s    r5   r   zMLEResults.hqich  s     DHd14=AAAr7   c                     | j         j        S zY
        (float) The value of the log-likelihood function evaluated at `params`.
        )r"  llf_obsr   s    r5   r  zMLEResults.llf_obsq  s    
 "**r7   c                     | j         j        S r  )r"  rg  r   s    r5   rg  zMLEResults.llfx  s    
 "&&r7   c                     | j         j        S )zj
        (float) The number of observations during which the likelihood is not
        evaluated.
        )r"  r   r   s    r5   r   zMLEResults.loglikelihood_burn  s     "55r7   c                 X    t          j        t          j        | j                            S )z-
        (float) Mean absolute error
        )re   meanabsresidr   s    r5   maezMLEResults.mae  s     
 wrvdj))***r7   c                      | j         | j        z  S )z,
        (float) Mean squared error
        )sserW   r   s    r5   msezMLEResults.mse  s    
 x$)##r7   c                 D   t          j        | j                  t           j        z  }t          j        |t
                    }d|| j        <   |t          j        | j                   z  }t          j	        t          j
        | j        |                             dz  ||<   |S )z
        (array) The p-values associated with the z-statistics of the
        coefficients. Note that the coefficients are assumed to have a Normal
        distribution.
        rG  Tr0  )re   rm  zvaluesr  	ones_likeboolr^   isnanr   sfr  )r_   pvaluesre  s      r5   r  zMLEResults.pvalues  s     ---6|G4000(,T$%$,''''t|D'9 : :;;a?r7   c                 d    | j         }|n%|j        d         dk    r|dddf         }n|j        }|S )zI
        (array) The model residuals. An (nobs x k_endog) array.
        Nr   r   )r6  rV   rl   )r_   r  s     r5   r  zMLEResults.resid  sD     $=[^q  !QQQ$KEEGEr7   c                 t    | j         j        r&| j         j        st          j        dt
                     | j        S )NzVNo supported index is available. The `states` DataFrame uses a generated integer index)r&  _index_generated_index_noner  r  r	   r?  r   s    r5   stateszMLEResults.states  sB    :& 	(tz/E 	(M F&( ( ( |r7   c                 :    t          j        | j        dz            S )z/
        (float) Sum of squared errors
        r0  )re   rg  r  r   s    r5   r  zMLEResults.sse  s    
 vdj!m$$$r7   c                      | j         | j        z  S )z@
        (array) The z-statistics for the coefficients.
        )r   bser   s    r5   r  zMLEResults.zvalues  s    
 {TX%%r7   c                    |d}| j         t          d          |dk    rddlm} t	          j        | j        | j                  }g }t          | j	        j
                  D ]Q}| j        j         ||df         }t	          j        |           }|                     |||                              Rnt          d          t	          j        |          S )a{  
        Test for normality of standardized residuals.

        Null hypothesis is normality.

        Parameters
        ----------
        method : {'jarquebera', None}
            The statistical test for normality. Must be 'jarquebera' for
            Jarque-Bera normality test. If None, an attempt is made to select
            an appropriate test.

        See Also
        --------
        statsmodels.stats.stattools.jarque_bera
            The Jarque-Bera test of normality.

        Notes
        -----
        Let `d` = max(loglikelihood_burn, nobs_diffuse); this test is
        calculated ignoring the first `d` residuals.

        In the case of missing data, the maintained hypothesis is that the
        data are missing completely at random. This test is then run on the
        standardized residuals excluding those corresponding to missing
        observations.
        N
jarqueberaWCannot compute test statistic when standardized forecast errors have not been computed.r   )jarque_berazInvalid normality test method.)r  rA   statsmodels.stats.stattoolsr  re   rJ  r   rK  r%   r&  ro   r"  r  r&   rz   rg   )r_   r   r  rT  outputr2   r  re  s           r5   test_normalityzMLEResults.test_normality  s    8 >!F,4 H I I I \!!??????
42D4EFFAF4:-.. 8 8+HABBO'kk%+6677778
 &&FGGGxr7   	two-sidedc                    |d}| j         t          d          |dk    rddlm} | j        j         }t          j        | j        | j                  }| j	        |z
  }t          t          j        |dz                      }g }	g }
t          | j        j                  D ]H} ||||df         |||          \  }}|	                    |           |
                    |           It
          j        |	|
f         }nt#          d          |S )	a
  
        Test for heteroskedasticity of standardized residuals

        Tests whether the sum-of-squares in the first third of the sample is
        significantly different than the sum-of-squares in the last third
        of the sample. Analogous to a Goldfeld-Quandt test. The null hypothesis
        is of no heteroskedasticity.

        Parameters
        ----------
        method : {'breakvar', None}
            The statistical test for heteroskedasticity. Must be 'breakvar'
            for test of a break in the variance. If None, an attempt is
            made to select an appropriate test.
        alternative : str, 'increasing', 'decreasing' or 'two-sided'
            This specifies the alternative for the p-value calculation. Default
            is two-sided.
        use_f : bool, optional
            Whether or not to compare against the asymptotic distribution
            (chi-squared) or the approximate small-sample distribution (F).
            Default is True (i.e. default is to compare against an F
            distribution).

        Returns
        -------
        output : ndarray
            An array with `(test_statistic, pvalue)` for each endogenous
            variable. The array is then sized `(k_endog, 2)`. If the method is
            called as `het = res.test_heteroskedasticity()`, then `het[0]` is
            an array of size 2 corresponding to the first endogenous variable,
            where `het[0][0]` is the test statistic, and `het[0][1]` is the
            p-value.

        See Also
        --------
        statsmodels.tsa.stattools.breakvar_heteroskedasticity_test

        Notes
        -----
        The null hypothesis is of no heteroskedasticity.

        For :math:`h = [T/3]`, the test statistic is:

        .. math::

            H(h) = \sum_{t=T-h+1}^T  \tilde v_t^2
            \Bigg / \sum_{t=d+1}^{d+1+h} \tilde v_t^2

        where :math:`d` = max(loglikelihood_burn, nobs_diffuse)` (usually
        corresponding to diffuse initialization under either the approximate
        or exact approach).

        This statistic can be tested against an :math:`F(h,h)` distribution.
        Alternatively, :math:`h H(h)` is asymptotically distributed according
        to :math:`\chi_h^2`; this second test can be applied by passing
        `use_f=True` as an argument.

        See section 5.4 of [1]_ for the above formula and discussion, as well
        as additional details.

        TODO

        - Allow specification of :math:`h`

        References
        ----------
        .. [1] Harvey, Andrew C. 1990. *Forecasting, Structural Time Series*
               *Models and the Kalman Filter.* Cambridge University Press.
        Nbreakvarr  r   ) breakvar_heteroskedasticity_testr1  )subset_lengthalternativeuse_fz'Invalid heteroskedasticity test method.)r  rA   statsmodels.tsa.stattoolsr  r"  re   rJ  r   rK  rW   introundr%   r&  ro   r&   c_rz   )r_   r   r  r  r  r  rT  r'  htest_statisticsp_valuesr2   test_statisticp_valuer  s                  r5   test_heteroskedasticityz"MLEResults.test_heteroskedasticity  sa   N >F,4 H I I I Z      'DE
42D4EFFA "Y]NBH^a/0011A OH4:-.. ) )*J*J!QRR%L"# +	+ + +'  &&~666((((U?H45FF% '1 2 2 2 r7   c                    |d}| j         t          d          |dk    s|dk    r7ddlm} t	          j        | j        | j                  }| j        |z
  }g }|Bt          | j
        dd          }|rt          d|z  |d	z            }nt          d
|d	z            }d}	|r t          d| j        | j        z
  dz
            }	|dk    rddgnddg}
t          | j
        j                  D ]c} || j        j         |         |d         ||dk    |	          }|                    t	          j        |          dd|
f         j                   dt          j        |         }nt-          d          |S )a  
        Ljung-Box test for no serial correlation of standardized residuals

        Null hypothesis is no serial correlation.

        Parameters
        ----------
        method : {'ljungbox', 'boxpierce', None}
            The statistical test for serial correlation. If None, an attempt is
            made to select an appropriate test.
        lags : None, int or array_like
            If lags is an integer then this is taken to be the largest lag
            that is included, the test result is reported for all smaller lag
            length.
            If lags is a list or array, then all lags are included up to the
            largest lag in the list, however only the tests for the lags in the
            list are reported.
            If lags is None, then the default maxlag is min(10, nobs // 5) for
            non-seasonal models and min(2*m, nobs // 5) for seasonal time
            series where m is the seasonal period.
        df_adjust : bool, optional
            If True, the degrees of freedom consumed by the model is subtracted
            from the degrees-of-freedom used in the test so that the adjusted
            dof for the statistics are lags - model_df. In an ARMA model, this
            value is usually p+q where p is the AR order and q is the MA order.
            When using df_adjust, it is not possible to use tests based on
            fewer than model_df lags.
        Returns
        -------
        output : ndarray
            An array with `(test_statistic, pvalue)` for each endogenous
            variable and each lag. The array is then sized
            `(k_endog, 2, lags)`. If the method is called as
            `ljungbox = res.test_serial_correlation()`, then `ljungbox[i]`
            holds the results of the Ljung-Box test (as would be returned by
            `statsmodels.stats.diagnostic.acorr_ljungbox`) for the `i` th
            endogenous variable.

        See Also
        --------
        statsmodels.stats.diagnostic.acorr_ljungbox
            Ljung-Box test for serial correlation.

        Notes
        -----
        Let `d` = max(loglikelihood_burn, nobs_diffuse); this test is
        calculated ignoring the first `d` residuals.

        Output is nan for any endogenous variable which has missing values.
        Nljungboxr  	boxpiercer   )acorr_ljungboxseasonal_periodsr0  r   
   r   r1  )lagsr  model_dfz'Invalid serial correlation test method.)r  rA   statsmodels.stats.diagnosticr  re   rJ  r   rK  rW   r@   r&  r  r  r-  r*  r%   ro   r"  r&   asarrayrl   r  rz   )r_   r   	df_adjustr  r  rT  r'  r  r  r  colsr2   rH  s                r5   test_serial_correlationz"MLEResults.test_serial_correlationh  s   f >F,4 H I I I Z6[#8#8CCCCCC
42D4EFFA "Y]NF |#*4:7I1#M#M # 8q#33^q5HIIDDr>Q#677DH Mq$-$2G"G!"KLL#{22Aq66AD4:-.. > >(.'DQGK&K*?%' ' ' bj11!!!T':<====U6]FF% '1 2 2 2r7   r@  c
           	      $   |d}| j                             |||          \  }}}}t          |t          t          j        t          j        f          r#| j                             |          \  }}}||z
  }|| j         j	        dup| j
        j         }|r |r | j         j        | j        |||	fddd|
}
| j                             | j        dd            | j
        j        |||z   dz   |fi |
}t!          t#          | ||||                    S )a  
        In-sample prediction and out-of-sample forecasting

        Parameters
        ----------
        start : int, str, or datetime, optional
            Zero-indexed observation number at which to start forecasting,
            i.e., the first forecast is start. Can also be a date string to
            parse or a datetime type. Default is the the zeroth observation.
        end : int, str, or datetime, optional
            Zero-indexed observation number at which to end forecasting, i.e.,
            the last forecast is end. Can also be a date string to
            parse or a datetime type. However, if the dates index does not
            have a fixed frequency, end must be an integer index if you
            want out of sample prediction. Default is the last observation in
            the sample.
        dynamic : bool, int, str, or datetime, optional
            Integer offset relative to `start` at which to begin dynamic
            prediction. Can also be an absolute date string to parse or a
            datetime type (these are not interpreted as offsets).
            Prior to this observation, true endogenous values will be used for
            prediction; starting with this observation and continuing through
            the end of prediction, forecasted endogenous values will be used
            instead.
        information_set : str, optional
            The information set to condition each prediction on. Default is
            "predicted", which computes predictions of period t values
            conditional on observed data through period t-1; these are
            one-step-ahead predictions, and correspond with the typical
            `fittedvalues` results attribute. Alternatives are "filtered",
            which computes predictions of period t values conditional on
            observed data through period t, and "smoothed", which computes
            predictions of period t values conditional on the entire dataset
            (including also future observations t+1, t+2, ...).
        signal_only : bool, optional
            Whether to compute predictions of only the "signal" component of
            the observation equation. Default is False. For example, the
            observation equation of a time-invariant model is
            :math:`y_t = d + Z \alpha_t + \varepsilon_t`, and the "signal"
            component is then :math:`Z \alpha_t`. If this argument is set to
            True, then predictions of the "signal" :math:`Z \alpha_t` will be
            returned. Otherwise, the default is for predictions of :math:`y_t`
            to be returned.
        **kwargs
            Additional arguments may required for forecasting beyond the end
            of the sample. See `FilterResults.predict` for more details.

        Returns
        -------
        predictions : PredictionResults
            PredictionResults instance containing in-sample predictions /
            out-of-sample forecasts and results including confidence intervals.

        See Also
        --------
        forecast
            Out-of-sample forecasts.
        predict
            In-sample predictions and out-of-sample forecasts.
        get_forecast
            Out-of-sample forecasts and results including confidence intervals.
        Nr   Tr   r   )information_setsignal_only
row_labels)r&  r  r"   r  dtdatetimer;   	Timestampr  rN   r"  r  r  r   r   predictPredictionResultsWrapperPredictionResults)r_   rq   r  dynamicr  r  r>   rN   r  r  r/   r  prediction_indexr  prediction_resultss                  r5   get_predictionzMLEResults.get_prediction  s   D =E J,,UC?? 	4sM#3 gR[",?@@ 	& J55g>>MGQoG
  JO47 C $ 3 BB  	A\ 	ADTZDT=-A A A9?A AF
 	
$+4MMM
 9T083&*G? ?7=? ? ((9$o#0@)B )B )B C C 	Cr7   r   c                 x    t          |t                    r| j        |z   dz
  }n|} | j        d| j        ||d|S )a  
        Out-of-sample forecasts and prediction intervals

        Parameters
        ----------
        steps : int, str, or datetime, optional
            If an integer, the number of steps to forecast from the end of the
            sample. Can also be a date string to parse or a datetime type.
            However, if the dates index does not have a fixed frequency, steps
            must be an integer. Default is 1.
        signal_only : bool, optional
            Whether to compute forecasts of only the "signal" component of
            the observation equation. Default is False. For example, the
            observation equation of a time-invariant model is
            :math:`y_t = d + Z \alpha_t + \varepsilon_t`, and the "signal"
            component is then :math:`Z \alpha_t`. If this argument is set to
            True, then forecasts of the "signal" :math:`Z \alpha_t` will be
            returned. Otherwise, the default is for forecasts of :math:`y_t`
            to be returned.
        **kwargs
            Additional arguments may required for forecasting beyond the end
            of the sample. See `FilterResults.predict` for more details.

        Returns
        -------
        forecasts : PredictionResults
            PredictionResults instance containing out-of-sample forecasts and
            results including confidence intervals.

        See also
        --------
        forecast
            Out-of-sample forecasts.
        predict
            In-sample predictions and out-of-sample forecasts.
        get_prediction
            In-sample predictions / out-of-sample forecasts and results
            including confidence intervals.
        r   rq   r  r  rQ   )r"   r  rW   r  r_   r  r  r/   r  s        r5   get_forecastzMLEResults.get_forecast.  sg    P eS!! 	)e#a'CCC"t" F/:F F>DF F 	Fr7   c                 6     | j         |||f||d|}|j        S )a=  
        In-sample prediction and out-of-sample forecasting

        Parameters
        ----------
        start : {int, str,datetime}, optional
            Zero-indexed observation number at which to start forecasting,
            i.e., the first forecast is start. Can also be a date string to
            parse or a datetime type. Default is the zeroth observation.
        end : {int, str,datetime}, optional
            Zero-indexed observation number at which to end forecasting, i.e.,
            the last forecast is end. Can also be a date string to
            parse or a datetime type. However, if the dates index does not
            have a fixed frequency, end must be an integer index if you
            want out of sample prediction. Default is the last observation in
            the sample.
        dynamic : {bool, int, str,datetime}, optional
            Integer offset relative to `start` at which to begin dynamic
            prediction. Can also be an absolute date string to parse or a
            datetime type (these are not interpreted as offsets).
            Prior to this observation, true endogenous values will be used for
            prediction; starting with this observation and continuing through
            the end of prediction, forecasted endogenous values will be used
            instead.
        information_set : str, optional
            The information set to condition each prediction on. Default is
            "predicted", which computes predictions of period t values
            conditional on observed data through period t-1; these are
            one-step-ahead predictions, and correspond with the typical
            `fittedvalues` results attribute. Alternatives are "filtered",
            which computes predictions of period t values conditional on
            observed data through period t, and "smoothed", which computes
            predictions of period t values conditional on the entire dataset
            (including also future observations t+1, t+2, ...).
        signal_only : bool, optional
            Whether to compute predictions of only the "signal" component of
            the observation equation. Default is False. For example, the
            observation equation of a time-invariant model is
            :math:`y_t = d + Z \alpha_t + \varepsilon_t`, and the "signal"
            component is then :math:`Z \alpha_t`. If this argument is set to
            True, then predictions of the "signal" :math:`Z \alpha_t` will be
            returned. Otherwise, the default is for predictions of :math:`y_t`
            to be returned.
        **kwargs
            Additional arguments may be required for forecasting beyond the end
            of the sample. See ``FilterResults.predict`` for more details.

        Returns
        -------
        predictions : array_like
            In-sample predictions / Out-of-sample forecasts. (Numpy array or
            Pandas Series or DataFrame, depending on input and dimensions).
            Dimensions are `(npredict x k_endog)`.

        See Also
        --------
        forecast
            Out-of-sample forecasts.
        get_forecast
            Out-of-sample forecasts and results including confidence intervals.
        get_prediction
            In-sample predictions / out-of-sample forecasts and results
            including confidence intervals.
        )r  r  )r  predicted_mean)r_   rq   r  r  r  r  r/   r  s           r5   r  zMLEResults.predict]  sD    F 1T03/1@#/ /'-/ / "00r7   c                 x    t          |t                    r| j        |z   dz
  }n|} | j        d| j        ||d|S )a  
        Out-of-sample forecasts

        Parameters
        ----------
        steps : int, str, or datetime, optional
            If an integer, the number of steps to forecast from the end of the
            sample. Can also be a date string to parse or a datetime type.
            However, if the dates index does not have a fixed frequency, steps
            must be an integer. Default is 1.
        signal_only : bool, optional
            Whether to compute forecasts of only the "signal" component of
            the observation equation. Default is False. For example, the
            observation equation of a time-invariant model is
            :math:`y_t = d + Z \alpha_t + \varepsilon_t`, and the "signal"
            component is then :math:`Z \alpha_t`. If this argument is set to
            True, then forecasts of the "signal" :math:`Z \alpha_t` will be
            returned. Otherwise, the default is for forecasts of :math:`y_t`
            to be returned.
        **kwargs
            Additional arguments may required for forecasting beyond the end
            of the sample. See `FilterResults.predict` for more details.

        Returns
        -------
        forecast : array_like
            Out-of-sample forecasts (Numpy array or Pandas Series or DataFrame,
            depending on input and dimensions).
            Dimensions are `(steps x k_endog)`.

        See Also
        --------
        predict
            In-sample predictions and out-of-sample forecasts.
        get_forecast
            Out-of-sample forecasts and results including confidence intervals.
        get_prediction
            In-sample predictions / out-of-sample forecasts and results
            including confidence intervals.
        r   r  rQ   )r"   r  rW   r  r  s        r5   forecastzMLEResults.forecast  s_    R eS!! 	)e#a'CCCt| &$)+ & &$& & 	&r7   c                    ||dk    rd}nH|dk    r| j         }n:| j                            |          \  }}}t          |t                    r|j        }|dk     r
| j         |z   }|| j         k    rt          d          ddlm} |	                    |          }|=| j
        dd|f         | j        dddd|f         f}|dn|} |j        |d|ij        }| j        j        r| j        nd}| j        j                            |          5   | j        j        | j        |f||||||d	d	||	|
|||d
|}ddd           n# 1 swxY w Y   |S )a  
        Simulate a new time series following the state space model

        Parameters
        ----------
        nsimulations : int
            The number of observations to simulate. If the model is
            time-invariant this can be any number. If the model is
            time-varying, then this number must be less than or equal to the
            number
        measurement_shocks : array_like, optional
            If specified, these are the shocks to the measurement equation,
            :math:`\varepsilon_t`. If unspecified, these are automatically
            generated using a pseudo-random number generator. If specified,
            must be shaped `nsimulations` x `k_endog`, where `k_endog` is the
            same as in the state space model.
        state_shocks : array_like, optional
            If specified, these are the shocks to the state equation,
            :math:`\eta_t`. If unspecified, these are automatically
            generated using a pseudo-random number generator. If specified,
            must be shaped `nsimulations` x `k_posdef` where `k_posdef` is the
            same as in the state space model.
        initial_state : array_like, optional
            If specified, this is the initial state vector to use in
            simulation, which should be shaped (`k_states` x 1), where
            `k_states` is the same as in the state space model. If unspecified,
            but the model has been initialized, then that initialization is
            used. This must be specified if `anchor` is anything other than
            "start" or 0.
        anchor : int, str, or datetime, optional
            Starting point from which to begin the simulations; type depends on
            the index of the given `endog` model. Two special cases are the
            strings 'start' and 'end', which refer to starting at the beginning
            and end of the sample, respectively. If a date/time index was
            provided to the model, then this argument can be a date string to
            parse or a datetime type. Otherwise, an integer index should be
            given. Default is 'start'.
        repetitions : int, optional
            Number of simulated paths to generate. Default is 1 simulated path.
        exog : array_like, optional
            New observations of exogenous regressors, if applicable.
        pretransformed_measurement_shocks : bool, optional
            If `measurement_shocks` is provided, this flag indicates whether it
            should be directly used as the shocks. If False, then it is assumed
            to contain draws from the standard Normal distribution that must be
            transformed using the `obs_cov` covariance matrix. Default is True.
        pretransformed_state_shocks : bool, optional
            If `state_shocks` is provided, this flag indicates whether it
            should be directly used as the shocks. If False, then it is assumed
            to contain draws from the standard Normal distribution that must be
            transformed using the `state_cov` covariance matrix. Default is
            True.
        pretransformed_initial_state : bool, optional
            If `initial_state` is provided, this flag indicates whether it
            should be directly used as the initial_state. If False, then it is
            assumed to contain draws from the standard Normal distribution that
            must be transformed using the `initial_state_cov` covariance
            matrix. Default is True.
        random_state : {None, int, Generator, RandomState}, optional
            If `seed` is None (or `np.random`), the
            class:``~numpy.random.RandomState`` singleton is used.
            If `seed` is an int, a new class:``~numpy.random.RandomState``
            instance is used, seeded with `seed`.
            If `seed` is already a class:``~numpy.random.Generator`` or
            class:``~numpy.random.RandomState`` instance then that instance is
            used.

        Returns
        -------
        simulated_obs : ndarray
            An array of simulated observations. If `repetitions=None`, then it
            will be shaped (nsimulations x k_endog) or (nsimulations,) if
            `k_endog=1`. Otherwise it will be shaped
            (nsimulations x k_endog x repetitions). If the model was given
            Pandas input then the output will be a Pandas object. If
            `k_endog > 1` and `repetitions` is not None, then the output will
            be a Pandas DataFrame that has a MultiIndex for the columns, with
            the first level containing the names of the `endog` variables and
            the second level containing the repetition number.

        See Also
        --------
        impulse_responses
            Impulse response functions
        Nrq   r   r  r  )r+  r   r+  T)r  r  r   r  r  rN   r   r   r  r  r  r  r  r  )rW   r&  r  r"   r  rq   rA   statsmodels.tsa.statespacer+  check_random_stater  r  multivariate_normalrl   r"  r,  r  rm   fixed_scaler  r   )r_   r  r  r  r   r  r  rN   r  r  r  r  r  r  r/   r  r  r+  initial_state_momentsr  r  r  s                         r5   r  zMLEResults.simulate  s   | >Vw..DDu__9DD226::JD!Q$&& "z!889t#D$)NOOO 	CBBBBB*==lKK  $QQQW-(AAAt4%6! !, 311L<L<&;-9; ;;<  #1EO

4Z^''.. 	5 	5%$*%\5#5);T )5,G-I)5 5 .45 5C	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 
s   )EEEr   c                 0   | j         j        r| j        nd}| j        j                            |          5   | j        j        | j        ||||fi |}t          |t          j
        t          j        f          r|j        }ddd           n# 1 swxY w Y   |S )a  
        Impulse response function

        Parameters
        ----------
        steps : int, optional
            The number of steps for which impulse responses are calculated.
            Default is 1. Note that for time-invariant models, the initial
            impulse is not counted as a step, so if `steps=1`, the output will
            have 2 entries.
        impulse : int, str or array_like
            If an integer, the state innovation to pulse; must be between 0
            and `k_posdef-1`. If a str, it indicates which column of df
            the unit (1) impulse is given.
            Alternatively, a custom impulse vector may be provided; must be
            shaped `k_posdef x 1`.
        orthogonalized : bool, optional
            Whether or not to perform impulse using orthogonalized innovations.
            Note that this will also affect custum `impulse` vectors. Default
            is False.
        cumulative : bool, optional
            Whether or not to return cumulative impulse responses. Default is
            False.
        anchor : int, str, or datetime, optional
            Time point within the sample for the state innovation impulse. Type
            depends on the index of the given `endog` in the model. Two special
            cases are the strings 'start' and 'end', which refer to setting the
            impulse at the first and last points of the sample, respectively.
            Integer values can run from 0 to `nobs - 1`, or can be negative to
            apply negative indexing. Finally, if a date/time index was provided
            to the model, then this argument can be a date string to parse or a
            datetime type. Default is 'start'.
        exog : array_like, optional
            New observations of exogenous regressors, if applicable.
        **kwargs
            If the model has time-varying design or transition matrices and the
            combination of `anchor` and `steps` implies creating impulse
            responses for the out-of-sample period, then these matrices must
            have updated values provided for the out-of-sample steps. For
            example, if `design` is a time-varying component, `nobs` is 10,
            `anchor=1`, and `steps` is 15, a (`k_endog` x `k_states` x 7)
            matrix must be provided with the new design matrix values.

        Returns
        -------
        impulse_responses : ndarray
            Responses for each endogenous variable due to the impulse
            given by the `impulse` argument. For a time-invariant model, the
            impulse responses are given for `steps + 1` elements (this gives
            the "initial impulse" followed by `steps` responses for the
            important cases of VAR and SARIMAX models), while for time-varying
            models the impulse responses are only given for `steps` elements
            (to avoid having to unexpectedly provide updated time-varying
            matrices).

        See Also
        --------
        simulate
            Simulate a time series according to the given state space model,
            optionally with specified series for the innovations.

        Notes
        -----
        Intercepts in the measurement and state equation are ignored when
        calculating impulse responses.
        N)r"  r,  r  r&  rm   r  r  r   r"   r;   r<   r=   r  )r_   r  r  r  r  r/   r  r  s           r5   r  zMLEResults.impulse_responsesa  s    H #1EO

4Z^''.. 	# 	#/4:/UG0>
: :28: :D $BL 9:: #{	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# s   A
BBBc                    |i }|rZ|                     d| j                   | j        r*|                     dd            |j        | j        fi |}n |j        di |}nd|v rt          d          d|v rt          d          | j        dk    rd|d<   n4d	|d<   | j        | j        d
| j	        
                    dd          z  d|d<   | j        |j        }n|j        }| j        rW|                    | j                  5  |                     dd            || j        fi |}d d d            n# 1 swxY w Y   n || j        fi |}|S )Nr   r   Tr   zeCannot specify covariance type in `fit_kwargs` unless refitting parameters (not available in extend).r   zrCannot specify covariance keyword arguments in `fit_kwargs` unless refitting parameters (not available in extend).rL   rP  zqParameters and standard errors were estimated using a different dataset and were then applied to this dataset. %srO  zUnknown.)rQ  rR  rS  rQ   )r   r   rZ   r  r[   r   rA   r   r6  r   r'   r#  r  r  r   )r_   r   refit
fit_kwargsr  r  s         r5   _applyzMLEResults._apply  s   J (	6!!.$+>>>% ,%%&6===)c)$*<KK
KKcg++
++Z''  "J K K K Z''  "J K K K }&&)/
:&&)1
:&'+})-)@' -++M:FFG	*I *I
:& $0zz% 6^^D$677 : :))*:DAAA$t{99j99C: : : : : : : : : : : : : : : d4;55*55
s   %D44D8;D8c                    t          |t          t          f           }|r@t          |          }|t          |          n|}||k    r| j                            d|dz
            \  }}}}	t          j        |          }|j        dk     rt          j	        |          j
        }|j        dk    s|j        d         | j        j        k    rt          d| j        j         d          t          j        ||z
  | j        j        f          t          j        z  }
t!          j        t          j        ||
gd          |	| j        j                  } | j        |f|d	d
|}| j        j                                        }|j        j                                        }|dk    s(|+|j        | j        k    s|j        | j        k    r||k    r|}| }nB|dk    s(|+|j        | j        k     s|j        | j        k    r||k     r| }|}nt          d          |j        j                            |j        j                  }t          |          dk    rt          d          |||fS )Nr   r   r  r0  z-Invalid shape for `comparison`. Must contain z	 columns.rd  r  TrN   copy_initializationupdatedpreviouszCould not automatically determine the type of comparison requested to compute the News, so it must be specified as "updated" or "previous", using the `comparison_type` keyword argumentzThe index associated with the updated results is not a superset of the index associated with the previous results, and so these datasets do not appear to be related. Can only compute the news by comparing this results set to previous results objects.)r"   r  r  r!   r&  r  re   r  rj   r  rl   rV   ro   rA   r3  r  r;   r=   concatenater  applyr"  rP   rg  rW   ru   r   )r_   
comparisonrN   comparison_typer/   comparison_dataset
nobs_endog	nobs_exogr  ixextranmissingnmissing_comparisonr  r  diffs                   r5   _get_previous_updatedz MLEResults._get_previous_updated  s    ",%67"9 "9 9 	HZJ%)%5D			:I:%%"j>>Q ? 0 01a  Z
33
?Q&&!#z!:!:!<JOq(("(+tz/AAA$ &O151C&O &O &O P P P)j"8"&*"4"6 7 79;@\NJ#6Q???r J24 4 4

 $J HT8<H H@FH HJ &.2244(7?CCEEy((_-D$)++DI--/// GHH++0G$)++DI--///G!HH 1 2 2 2 ~$//0DEEt99q== 1 2 2 2 "444r7   c                 N    | j                             |j         ||||          }|S )N)rq   r  revisions_details_startstate_index)r#  news)r_   r  rq   r  rr   r  r  rr  s           r5   _news_previous_resultsz!MLEResults._news_previous_results$  s8     #((%U$;# ) % % 
r7   c                 8    |                     | |||||          S )Nr  r  )r  )r_   r  rq   r  rr   r  r  s          r5   _news_updated_resultsz MLEResults._news_updated_results.  s.    --%g$;# . % % 	%r7   c                 h    |                      ||d          }|                     ||||||          S NTr  r  )r   r  )	r_   rM   rq   r  rr   rN   r  r  r  s	            r5   _news_previous_datazMLEResults._news_previous_data5  sE    ::e$D:II**eS'$;# + % % 	%r7   c                 h    |                      ||d          }|                     ||||||          S r  )r   r  )	r_   rM   rq   r  rr   rN   r  r  r  s	            r5   _news_updated_datazMLEResults._news_updated_data=  sE    **U4*HH))UC$;# * % % 	%r7   绽|=c           	         | j         t          d          |
t          j        t          j        |
t
                              }
|
d         dk     rt          d          |
d         | j        j        k    r&t          d|
d          d| j        j         d	          t          |	t
          t          f          s | j        
                    |	|	          \  }	}}} | j        |f||d
|\  }}}t          |j        |j        ||||          \  }}}|t          j        ||dz             }|j        j        p|j        j         }|r||j        k    r|r7|t%          |          dk    r"||u rt          d          t          d          |}| j        j        dk    r9t          j        ||j        z
  dz   | j        j        f          t          j        z  }n-t          j        ||j        z
  dz   f          t          j        z  } |j        |fd|i|}|                    |||dz   ||	|
          }|st1          || |||||          }|S )u  
        Compute impacts from updated data (news and revisions)

        Parameters
        ----------
        comparison : array_like or MLEResults
            An updated dataset with updated and/or revised data from which the
            news can be computed, or an updated or previous results object
            to use in computing the news.
        impact_date : int, str, or datetime, optional
            A single specific period of impacts from news and revisions to
            compute. Can also be a date string to parse or a datetime type.
            This argument cannot be used in combination with `start`, `end`, or
            `periods`. Default is the first out-of-sample observation.
        impacted_variable : str, list, array, or slice, optional
            Observation variable label or slice of labels specifying that only
            specific impacted variables should be shown in the News output. The
            impacted variable(s) describe the variables that were *affected* by
            the news. If you do not know the labels for the variables, check
            the `endog_names` attribute of the model instance.
        start : int, str, or datetime, optional
            The first period of impacts from news and revisions to compute.
            Can also be a date string to parse or a datetime type. Default is
            the first out-of-sample observation.
        end : int, str, or datetime, optional
            The last period of impacts from news and revisions to compute.
            Can also be a date string to parse or a datetime type. Default is
            the first out-of-sample observation.
        periods : int, optional
            The number of periods of impacts from news and revisions to
            compute.
        exog : array_like, optional
            Array of exogenous regressors for the out-of-sample period, if
            applicable.
        comparison_type : {None, 'previous', 'updated'}
            This denotes whether the `comparison` argument represents a
            *previous* results object or dataset or an *updated* results object
            or dataset. If not specified, then an attempt is made to determine
            the comparison type.
        revisions_details_start : bool, int, str, or datetime, optional
            The period at which to beging computing the detailed impacts of
            data revisions. Any revisions prior to this period will have their
            impacts grouped together. If a negative integer, interpreted as
            an offset from the end of the dataset. If set to True, detailed
            impacts are computed for all revisions, while if set to False, all
            revisions are grouped together. Default is False. Note that for
            large models, setting this to be near the beginning of the sample
            can cause this function to be slow.
        state_index : array_like, optional
            An optional index specifying a subset of states to use when
            constructing the impacts of revisions and news. For example, if
            `state_index=[0, 1]` is passed, then only the impacts to the
            observed variables arising from the impacts to the first two
            states will be returned. Default is to use all states.
        return_raw : bool, optional
            Whether or not to return only the specific output or a full
            results object. Default is to return a full results object.
        tolerance : float, optional
            The numerical threshold for determining zero impact. Default is
            that any impact less than 1e-10 is assumed to be zero.

        Returns
        -------
        NewsResults
            Impacts of data revisions and news on estimates

        References
        ----------
        .. [1] Bańbura, Marta, and Michele Modugno.
               "Maximum likelihood estimation of factor models on datasets with
               arbitrary pattern of missing data."
               Journal of Applied Econometrics 29, no. 1 (2014): 133-160.
        .. [2] Bańbura, Marta, Domenico Giannone, and Lucrezia Reichlin.
               "Nowcasting."
               The Oxford Handbook of Economic Forecasting. July 8, 2011.
        .. [3] Bańbura, Marta, Domenico Giannone, Michele Modugno, and Lucrezia
               Reichlin.
               "Now-casting and the real-time data flow."
               In Handbook of economic forecasting, vol. 2, pp. 195-237.
               Elsevier, 2013.
        Nz4Cannot compute news without Kalman smoother results.rG  r   z1Cannot include negative indexes in `state_index`.rs   zGiven state index z5 is too large for the number of states in the model (z).)rN   r  )previous_modelupdated_modelimpact_daterq   r  rr   r   )rq   r|   zIf providing an updated dataset as the `comparison` with a time-varying model, then the `end` period cannot be beyond the end of that updated dataset.zIf providing an previous dataset as the `comparison` with a time-varying model, then the `end` period cannot be beyond the end of the (updated) results object.rN   r  )r  )r#  rA   re   sortrg   r  r&  rX   r"   r  r  r  r   r;   r{   r"  r  rW   r!   ro   r3  r  r&   r  r   )r_   r  r  impacted_variablerq   r  rr   rN   r  r  r  r  r   r/   r  r  r  r  r  time_varyingupdated_origr  news_resultss                          r5   r  zMLEResults.newsE  s,   l  ( ) * * * "'"(;c"B"B"BCCK1~!!  "3 4 4 42$*"555  ">k"o "> ">&*j&9"> "> "> ? ? ? 1C;?? 	G
00+-DF F -#Q1 1K0J1N!?1N 1NFL1N 1N-'- (8#>#5c7(L (L (L$s$ #!}5sQwGGG %3B B#2AC 	FC7<// " 1t'73v;;??(($ &I J J J
 % &0 1 1 1 #L z!A%%#"4q"8"&*"4"6 7 79;@ #"4q"8!:;;bfD)l)%EEdEfEEG 55eS1Wg$;# 6 % %
  	8&dGX7H&68 8 8L r7   r  c                 X   | j                             ||          \  }}}}| j        j        }| j        j        dk    r|g}|dk    r:t
          j                            | j        j        | j        j	        gddg          }n/t
          j                            || j        j	        gddg          }t
          j                            || j        j	        gdd	g          }	t
          j                            || j        j	        gd
d	g          }
t
          j                            | j        j        | j        j	        gdd	g          }t          j
        | j        j        d          }|j        }t          j        |                    |d         |d         z  |d         |d         z  d          ||	          }|j        }t          j        |                    |d         |d         z  |d         |d         z  d          ||
          }|j        }t          j        |                    |d         |d         z  |d         |d         z  d          ||          }|j        }t          j        |                    |d         |d         z  |d         d          ||          }||||fS )a  
        Decompose smoothed output into contributions from observations

        Parameters
        ----------
        decomposition_of : {"smoothed_state", "smoothed_signal"}
            The object to perform a decomposition of. If it is set to
            "smoothed_state", then the elements of the smoothed state vector
            are decomposed into the contributions of each observation. If it
            is set to "smoothed_signal", then the predictions of the
            observation vector based on the smoothed state vector are
            decomposed. Default is "smoothed_state".
        state_index : array_like, optional
            An optional index specifying a subset of states to use when
            constructing the decomposition of the "smoothed_signal". For
            example, if `state_index=[0, 1]` is passed, then only the
            contributions of observed variables to the smoothed signal arising
            from the first two states will be returned. Note that if not all
            states are used, the contributions will not sum to the smoothed
            signal. Default is to use all states.

        Returns
        -------
        data_contributions : pd.DataFrame
            Contributions of observations to the decomposed object. If the
            smoothed state is being decomposed, then `data_contributions` is
            shaped `(k_states x nobs, k_endog x nobs)` with a `pd.MultiIndex`
            index corresponding to `state_to x date_to` and `pd.MultiIndex`
            columns corresponding to `variable_from x date_from`. If the
            smoothed signal is being decomposed, then `data_contributions` is
            shaped `(k_endog x nobs, k_endog x nobs)` with `pd.MultiIndex`-es
            corresponding to `variable_to x date_to` and
            `variable_from x date_from`.
        obs_intercept_contributions : pd.DataFrame
            Contributions of the observation intercept to the decomposed
            object. If the smoothed state is being decomposed, then
            `obs_intercept_contributions` is
            shaped `(k_states x nobs, k_endog x nobs)` with a `pd.MultiIndex`
            index corresponding to `state_to x date_to` and `pd.MultiIndex`
            columns corresponding to `obs_intercept_from x date_from`. If the
            smoothed signal is being decomposed, then
            `obs_intercept_contributions` is shaped
            `(k_endog x nobs, k_endog x nobs)` with `pd.MultiIndex`-es
            corresponding to `variable_to x date_to` and
            `obs_intercept_from x date_from`.
        state_intercept_contributions : pd.DataFrame
            Contributions of the state intercept to the decomposed
            object. If the smoothed state is being decomposed, then
            `state_intercept_contributions` is
            shaped `(k_states x nobs, k_states x nobs)` with a `pd.MultiIndex`
            index corresponding to `state_to x date_to` and `pd.MultiIndex`
            columns corresponding to `state_intercept_from x date_from`. If the
            smoothed signal is being decomposed, then
            `state_intercept_contributions` is shaped
            `(k_endog x nobs, k_states x nobs)` with `pd.MultiIndex`-es
            corresponding to `variable_to x date_to` and
            `state_intercept_from x date_from`.
        prior_contributions : pd.DataFrame
            Contributions of the prior to the decomposed object. If the
            smoothed state is being decomposed, then `prior_contributions` is
            shaped `(nobs x k_states, k_states)`, with a `pd.MultiIndex`
            index corresponding to `state_to x date_to` and columns
            corresponding to elements of the prior mean (aka "initial state").
            If the smoothed signal is being decomposed, then
            `prior_contributions` is shaped `(nobs x k_endog, k_states)`,
            with a `pd.MultiIndex` index corresponding to
            `variable_to x date_to` and columns corresponding to elements of
            the prior mean.

        Notes
        -----
        Denote the smoothed state at time :math:`t` by :math:`\alpha_t`. Then
        the smoothed signal is :math:`Z_t \alpha_t`, where :math:`Z_t` is the
        design matrix operative at time :math:`t`.
        )decomposition_ofr  r   r  state_todate_to)r,   variable_tovariable_from	date_fromobs_intercept_fromstate_intercept_frominitial_state_fromr  r   r0  r1  F)orderr  )r#  get_smoothed_decompositionr&  r  ro   r;   r  r  r  ru   r   rV   r=   r  )r_   r$  r  data_contributionsobs_intercept_contributionsstate_intercept_contributionsprior_contributionsr  contributions_tocontributions_from obs_intercept_contributions_from"state_intercept_contributions_fromprior_contributions_fromrV   s                 r5   r/  z%MLEResults.get_smoothed_decomposition  sA   ^ !<<!1{ = L L	=	8	&(;
 j,:""&-K///!}99'):;!9-  :  /  /  "}99dj/0$i0  :  2  2  ]77$*+,"K0 8 2 2 ,.=+E+E$*+,'5 ,F ,7 ,7( .0]-G-GZ#TZ%67);7 .H .9 .9* $&8DJ,B1E$G $G $G  #(\&&a58#U1Xa%8 ' E E",>@ @ @
 ,1&(l'//a58#U1Xa%8 0 E E",L'N 'N 'N#
 .3(*)11a58#U1Xa%8 2 E E",N)P )P )P%
 $) l''a58(;U1X.1 ( 3 3",DF F F
 #$?-/BD 	Dr7   c                    | j         }| j         t          |          z   dz
  }| j                            ||          \  }	}	}	}
t	          | j        j        t                    rt          |
|d           t          | j        j        j	        |gdd          }|Gt          |          \  }	}t          |
|d           t          | j        j        j        |gdd          }nd}t	          | j        j        t                    rd}t          |          dz
  }| j                            ||          \  }	}	}	}| j        j        }t	          |t                    s|g}t          j        |||          }|!t          j        ||| j        j                  }|r/t#          j        | j                  }|                    d	|            | j        j        |fd
|i|}|                     |||          }|S )aw  
        Recreate the results object with new data appended to the original data

        Creates a new result object applied to a dataset that is created by
        appending new data to the end of the model's original data. The new
        results can then be used for analysis or forecasting.

        Parameters
        ----------
        endog : array_like
            New observations from the modeled time-series process.
        exog : array_like, optional
            New observations of exogenous regressors, if applicable.
        refit : bool, optional
            Whether to re-fit the parameters, based on the combined dataset.
            Default is False (so parameters from the current results object
            are used to create the new results object).
        copy_initialization : bool, optional
            Whether or not to copy the initialization from the current results
            set to the new model. Default is False
        fit_kwargs : dict, optional
            Keyword arguments to pass to `fit` (if `refit=True`) or `filter` /
            `smooth`.
        copy_initialization : bool, optional
        **kwargs
            Keyword arguments may be used to modify model specification
            arguments when created the new model object.

        Returns
        -------
        results
            Updated Results object, that includes results from both the
            original dataset and the new dataset.

        Notes
        -----
        The `endog` and `exog` arguments to this method must be formatted in
        the same way (e.g. Pandas Series versus Numpy array) as were the
        `endog` and `exog` arrays passed to the original model.

        The `endog` argument to this method should consist of new observations
        that occurred directly after the last element of `endog`. For any other
        kind of dataset, see the `apply` method.

        This method will apply filtering to all of the original data as well
        as to the new data. To apply filtering only to the new data (which
        can be much faster if the original dataset is large), see the `extend`
        method.

        See Also
        --------
        statsmodels.tsa.statespace.mlemodel.MLEResults.extend
        statsmodels.tsa.statespace.mlemodel.MLEResults.apply

        Examples
        --------
        >>> index = pd.period_range(start='2000', periods=2, freq='Y')
        >>> original_observations = pd.Series([1.2, 1.5], index=index)
        >>> mod = sm.tsa.SARIMAX(original_observations)
        >>> res = mod.fit()
        >>> print(res.params)
        ar.L1     0.9756
        sigma2    0.0889
        dtype: float64
        >>> print(res.fittedvalues)
        2000    0.0000
        2001    1.1707
        Freq: A-DEC, dtype: float64
        >>> print(res.forecast(1))
        2002    1.4634
        Freq: A-DEC, dtype: float64

        >>> new_index = pd.period_range(start='2002', periods=1, freq='Y')
        >>> new_observations = pd.Series([0.9], index=new_index)
        >>> updated_res = res.append(new_observations)
        >>> print(updated_res.params)
        ar.L1     0.9756
        sigma2    0.0889
        dtype: float64
        >>> print(updated_res.fittedvalues)
        2000    0.0000
        2001    1.1707
        2002    1.4634
        Freq: A-DEC, dtype: float64
        >>> print(updated_res.forecast(1))
        2003    0.878
        Freq: A-DEC, dtype: float64
        r   `endog`r   T)re  	allow_mixNz`exog`r  r   rN   r  r  )rW   r!   r&  r  r"   r8   r   rH   r   rh   r   ri   r  r   r;   r=   
exog_namesr   from_resultsr"  r   r   r  )r_   rM   rN   r  r  r  r/   rq   r  r  	append_ix	new_endognew_exog	new_indexr  initr   r  s                     r5   r&   zMLEResults.append}  s   t 	i#e**$q(!Z==eSII1a djoz22 	6E9555 DJO6>Q%)+ + +	 "4((GAtD(333tz8$?a(,. . .HH H djoz22 	GEi..1$C!%!A!A%!M!MAq!Y j,Ggt,, $")Yi-46 6 6I #<	04
0EG G G  	6!.t/BCCD.555djyBBxB6BBkk#UzkBB
r7   c                 (   | j         }| j         t          |          z   dz
  }| j                            ||          \  }}}}t	          | j        j        t                    rLt          ||d           | j        j        }	t	          |	t                    s|	g}	t          j        |||	          } | j        j        |fd|i|}
t          |
j        d| j        d         | j        d                   |
j        _        |                     |
d|	          }|S )
a
  
        Recreate the results object for new data that extends the original data

        Creates a new result object applied to a new dataset that is assumed to
        follow directly from the end of the model's original data. The new
        results can then be used for analysis or forecasting.

        Parameters
        ----------
        endog : array_like
            New observations from the modeled time-series process.
        exog : array_like, optional
            New observations of exogenous regressors, if applicable.
        fit_kwargs : dict, optional
            Keyword arguments to pass to `filter` or `smooth`.
        **kwargs
            Keyword arguments may be used to modify model specification
            arguments when created the new model object.

        Returns
        -------
        results
            Updated Results object, that includes results only for the new
            dataset.

        See Also
        --------
        statsmodels.tsa.statespace.mlemodel.MLEResults.append
        statsmodels.tsa.statespace.mlemodel.MLEResults.apply

        Notes
        -----
        The `endog` argument to this method should consist of new observations
        that occurred directly after the last element of the model's original
        `endog` array. For any other kind of dataset, see the `apply` method.

        This method will apply filtering only to the new data provided by the
        `endog` argument, which can be much faster than re-filtering the entire
        dataset. However, the returned results object will only have results
        for the new data. To retrieve results for both the new data and the
        original data, see the `append` method.

        Examples
        --------
        >>> index = pd.period_range(start='2000', periods=2, freq='Y')
        >>> original_observations = pd.Series([1.2, 1.5], index=index)
        >>> mod = sm.tsa.SARIMAX(original_observations)
        >>> res = mod.fit()
        >>> print(res.params)
        ar.L1     0.9756
        sigma2    0.0889
        dtype: float64
        >>> print(res.fittedvalues)
        2000    0.0000
        2001    1.1707
        Freq: A-DEC, dtype: float64
        >>> print(res.forecast(1))
        2002    1.4634
        Freq: A-DEC, dtype: float64

        >>> new_index = pd.period_range(start='2002', periods=1, freq='Y')
        >>> new_observations = pd.Series([0.9], index=new_index)
        >>> updated_res = res.extend(new_observations)
        >>> print(updated_res.params)
        ar.L1     0.9756
        sigma2    0.0889
        dtype: float64
        >>> print(updated_res.fittedvalues)
        2002    1.4634
        Freq: A-DEC, dtype: float64
        >>> print(updated_res.forecast(1))
        2003    0.878
        Freq: A-DEC, dtype: float64
        r   r:  r  rN   known).rs   )constantstationary_covFr<  )rW   r!   r&  r  r"   r8   r   rH   r  r   r;   r=   r   r   rX   r  r  rm   r   r  )r_   rM   rN   r  r/   rq   r  r  	extend_ixr  r   r  s               r5   r  zMLEResults.extend  s   V 	i#e**$q(!Z==eSII1adjoz22 	JE9555 j,Ggt,, $")LiIIIEdju::4:6::!/L'D,@,I3G<"> "> "> kk#UzkBB
r7   c                      | j         j        |fd|i|}|r%t          j        | j                  }||j        _        |                     |||          }	|	S )a	  
        Apply the fitted parameters to new data unrelated to the original data

        Creates a new result object using the current fitted parameters,
        applied to a completely new dataset that is assumed to be unrelated to
        the model's original data. The new results can then be used for
        analysis or forecasting.

        Parameters
        ----------
        endog : array_like
            New observations from the modeled time-series process.
        exog : array_like, optional
            New observations of exogenous regressors, if applicable.
        refit : bool, optional
            Whether to re-fit the parameters, using the new dataset.
            Default is False (so parameters from the current results object
            are used to create the new results object).
        copy_initialization : bool, optional
            Whether or not to copy the initialization from the current results
            set to the new model. Default is False
        fit_kwargs : dict, optional
            Keyword arguments to pass to `fit` (if `refit=True`) or `filter` /
            `smooth`.
        **kwargs
            Keyword arguments may be used to modify model specification
            arguments when created the new model object.

        Returns
        -------
        results
            Updated Results object, that includes results only for the new
            dataset.

        See Also
        --------
        statsmodels.tsa.statespace.mlemodel.MLEResults.append
        statsmodels.tsa.statespace.mlemodel.MLEResults.apply

        Notes
        -----
        The `endog` argument to this method should consist of new observations
        that are not necessarily related to the original model's `endog`
        dataset. For observations that continue that original dataset by follow
        directly after its last element, see the `append` and `extend` methods.

        Examples
        --------
        >>> index = pd.period_range(start='2000', periods=2, freq='Y')
        >>> original_observations = pd.Series([1.2, 1.5], index=index)
        >>> mod = sm.tsa.SARIMAX(original_observations)
        >>> res = mod.fit()
        >>> print(res.params)
        ar.L1     0.9756
        sigma2    0.0889
        dtype: float64
        >>> print(res.fittedvalues)
        2000    0.0000
        2001    1.1707
        Freq: A-DEC, dtype: float64
        >>> print(res.forecast(1))
        2002    1.4634
        Freq: A-DEC, dtype: float64

        >>> new_index = pd.period_range(start='1980', periods=3, freq='Y')
        >>> new_observations = pd.Series([1.4, 0.3, 1.2], index=new_index)
        >>> new_res = res.apply(new_observations)
        >>> print(new_res.params)
        ar.L1     0.9756
        sigma2    0.0889
        dtype: float64
        >>> print(new_res.fittedvalues)
        1980    1.1707
        1981    1.3659
        1982    0.2927
        Freq: A-DEC, dtype: float64
        Freq: A-DEC, dtype: float64
        >>> print(new_res.forecast(1))
        1983    1.1707
        Freq: A-DEC, dtype: float64
        rN   r<  )r&  r   r   r>  r"  rm   r   r  )
r_   rM   rN   r  r  r  r/   r   rC  r  s
             r5   r   zMLEResults.applyh  sg    f dju::4:6:: 	*!.t/BCCD%)CG"kk#UzkBB
r7   r     c	                    ddl m}	m}
  |	              |
||          }t          j        | j        | j                  }t          |t                    r| j	        j
                            |          }t          | j        d          r!| j        j        | j        j        |d         }nt          j        | j        |z
            }t#          j        | j        j        ||df         |          }|j        d         t-          ||          k     rt/          d          |                    d          }|                                                    |           |                    d|d         |d	         d
           |                    |d         |d	                    | j	        j
        |         }t;          |          |k    r|d|dz
           dz   }|                    d| d           |                                }|                    d          }|                    |ddd           ddl m!}m"}  ||          }d}t          j#        |d         |d                   }|                    | ||          d           |                    | |j$        |          d           |                    |           |%                                 |                    d           |                    d          }ddl&m'}  ||d|           |                    d            |                    d!          }dd"l(m)} |i } ||f||||d#| |                    d$           |S )%a  
        Diagnostic plots for standardized residuals of one endogenous variable

        Parameters
        ----------
        variable : int, optional
            Index of the endogenous variable for which the diagnostic plots
            should be created. Default is 0.
        lags : int, optional
            Number of lags to include in the correlogram. Default is 10.
        fig : Figure, optional
            If given, subplots are created in this figure instead of in a new
            figure. Note that the 2x2 grid will be created in the provided
            figure using `fig.add_subplot()`.
        figsize : tuple, optional
            If a figure is created, this argument allows specifying a size.
            The tuple is (width, height).
        auto_ylims : bool, optional
            If True, adjusts automatically the y-axis limits to ACF values.
        bartlett_confint : bool, default True
            Confidence intervals for ACF values are generally placed at 2
            standard errors around r_k. The formula used for standard error
            depends upon the situation. If the autocorrelations are being used
            to test for randomness of residuals as part of the ARIMA routine,
            the standard errors are determined assuming the residuals are white
            noise. The approximate formula for any lag is that standard error
            of each r_k = 1/sqrt(N). See section 9.4 of [1] for more details on
            the 1/sqrt(N) result. For more elementary discussion, see section
            5.3.2 in [2].
            For the ACF of raw data, the standard error at a lag k is
            found as if the right model was an MA(k-1). This allows the
            possible interpretation that if all autocorrelations past a
            certain lag are within the limits, the model might be an MA of
            order defined by the last significant autocorrelation. In this
            case, a moving average model is assumed for the data and the
            standard errors for the confidence intervals should be
            generated using Bartlett's formula. For more details on
            Bartlett formula result, see section 7.2 in [1].+
        acf_kwargs : dict, optional
            Optional dictionary of keyword arguments that are directly passed
            on to the correlogram Matplotlib plot produced by plot_acf().

        Returns
        -------
        Figure
            Figure instance with diagnostic plots

        See Also
        --------
        statsmodels.graphics.gofplots.qqplot
        statsmodels.graphics.tsaplots.plot_acf

        Notes
        -----
        Produces a 2x2 plot grid with the following plots (ordered clockwise
        from top left):

        1. Standardized residuals over time
        2. Histogram plus estimated density of standardized residuals, along
           with a Normal(0,1) density plotted for reference.
        3. Normal Q-Q plot, with Normal reference line.
        4. Correlogram

        References
        ----------
        [1] Brockwell and Davis, 1987. Time Series Theory and Methods
        [2] Brockwell and Davis, 2010. Introduction to Time Series and
        Forecasting, 2nd edition.
        r   )_import_mplcreate_mpl_figrO   Nr>   zLength of endogenous variable must be larger the the number of lags used in the model and the number of observations burned in the log-likelihood calculation.   )axrs   rI  alphar1  ...zStandardized residual for ""   THistz#FFFFFF)densitylabel	edgecolor)gaussian_kder   )g\(\g\(\@r   KDE)rX  zN(0,1)z Histogram plus estimated density   )qqplots)linerP  z
Normal Q-Q   )plot_acf)rP  r  
auto_ylimsbartlett_confintCorrelogram)*statsmodels.graphics.utilsrL  rM  re   rJ  r   rK  r"   r  r&  r  r>   r   r8   rO   r   rW   r;   r<   r"  r  rV   r  rA   add_subplotdropnaplothlinesset_xlimr!   	set_titlehistscipy.statsrZ  r   linspacepdflegendstatsmodels.graphics.gofplotsr]  statsmodels.graphics.tsaplotsra  )r_   variabler  figfigsizetruncate_endog_namesrb  rc  
acf_kwargsrL  rM  rT  r  r  rP  r3   resid_nonmissingrZ  r   kdexlimxr]  ra  s                           r5   plot_diagnosticszMLEResults.plot_diagnostics  s   P 	KJJJJJJJnS'**Jt.0ABB h$$ 	>z-33H==H 49g&& 	*49?+F$BB49q=))B	<Xqrr\J   ;q>C4LL((<   __S!!r"""
		!RUBrF#	...
BqE2b6"""z%h/t99+++1-112U:D
:4:::;;;
 !<<>>__S!!
 $f# 	 	% 	% 	% 	32222222l+,, KQa))
33q66'''
848A;;h///
D
		
7888 __S!!888888cb1111
\""" __S!!::::::J 	B2DZ"2	B 	B6@	B 	B 	B
]###
r7   皙?c           	        * ddl m} ddlm} ddlm} | j        }|d}|d}| j        j        rS| j        j        }||         }d|j	        |j
        |j        fz  g}|d         }|d	d|j	        |j
        |j        fz  z   gz  }n&t          |          d
t          | j                  z   g}||j        j        }|| j        j        dk    rdnd}| j        j        }t%          |t&                    s|g}d |D             }|dur|**fd|D             }|t(          j        }d}| j        j        |k    r!| j        j        dz
  }d|d         z   d| dz   }t%          |t&                    s|g}dg}|                    d|d         gf           t/          dt1          |                    D ]#}|                    dd||         z   gf           $|ddd|d         gfd|d         gfgz  }d| j        gfdd| j        z  gfg}t5          | d          r |                    dd| j        z  gf           |d d| j        z  gfd!d| j        z  gfd"d| j        z  gfgz  }| j        ,| j        j         r |                    d#d| j!        z  gf           t5          | d$          r'| j"        }|d%k    rd&}|                    d'|gf           |	||	z  }|
||
z  } |            }|#                    | ||||(           d}t1          | j$                  dk    r%|r#|%                    | || j&        d)           |dz  }|r	 | '                    d*+          }n=# tP          $ r0 t)          j)        | j        j        d,f          t(          j*        z  }Y nw xY w	 | +                    d-dg.          }n># tP          $ r1 t)          j)        | j        j        d,df          t(          j*        z  }Y nw xY w	 | ,                    d/+          }n=# tP          $ r0 t)          j)        | j        j        d0f          t(          j*        z  }Y nw xY w| j        j        |k    rd1 }d2 ||ddddf                   fd3 ||ddddf                   fd4 ||dddf                   fd5 ||dddf                   fg}d6 ||dddf                   fd7 ||dddf                   fd8 ||ddd,f                   fd9 ||ddd:f                   fg} |#                    | || d;           n g d<}!t[          j.        t(          j/        |dddd,df         |dddd,f         |dddd0f         f         ||!=          }"	 |"0                    d>           }"n&# tb          $ r |"2                    d?           }"Y nw xY wd@|"j3        _4        |"5                                }"|"j6        }#|"j7        8                                }$d}%dA} ||#|$|%||B          }&|j9        :                    ||&           g }'t5          | d$          r)dC| j;        v r |'                    | j;        dC                    | j<        t1          | j$                  t1          | j=                  z
  k     rx| >                                }(t1          | j=                  dk    r't)          j?        | j@        | j@                  })|(|)         }(|'                    dDt          |(          z             |'rDdE t          |'          D             }'|':                    ddF           |C                    |'           |S )Ga  
        Summarize the Model

        Parameters
        ----------
        alpha : float, optional
            Significance level for the confidence intervals. Default is 0.05.
        start : int, optional
            Integer of the start observation. Default is 0.
        model_name : str
            The name of the model used. Default is to use model class name.

        Returns
        -------
        summary : Summary instance
            This holds the summary table and text, which can be printed or
            converted to various output formats.

        See Also
        --------
        statsmodels.iolib.summary.Summary
        r   )Summary)SimpleTable)
fmt_paramsNzStatespace Model Resultsz%02d-%02d-%02drs   z- z - r   FrJ  c                 ,    g | ]}t          |          S rQ   )r  r   r3   s     r5   r   z&MLEResults.summary.<locals>.<listcomp>  s    999Ts4yy999r7   c                 T    g | ]$}t          |          k    r|n|d          dz   %S )NrS  )r!   )r   r3   r;  s     r5   r   z&MLEResults.summary.<locals>.<listcomp>  sH     4 4 4# $'t99>>44tBQBx%7G 4 4 4r7   rT  z", and z more)zDep. Variable:NzModel: z+ )zDate:N)zTime:NzSample:zNo. Observations:zLog Likelihoodz%#5.3frsquaredz
R-squared:z%#8.3fAICBICHQICScaler   rL   zNot computedzCovariance Type:)gleftgrightrD   yname)rR  xnameuse_tr  )r   r0  r  )r   r  r  r  c                 B    d                     d | D                       gS )Nz, c                     g | ]}|d S )z.2frQ   r  s     r5   r   z8MLEResults.summary.<locals>.<lambda>.<locals>.<listcomp>  s    999a!zz999r7   )join)rg   s    r5   <lambda>z$MLEResults.summary.<locals>.<lambda>  s'    II995999::, r7   zLjung-Box (L1) (Q):zProb(Q):zHeteroskedasticity (H):zProb(H) (two-sided):zJarque-Bera (JB):z	Prob(JB):zSkew:z	Kurtosis:r1  )r  r  rD   )zLjungBox
(L1) (Q)zProb(Q)zHet.(H)zProb(H)zJarque
Bera(JB)zProb(JB)SkewKurtosisr  c                 8    t          j        |           rdnd| z  S Nr  z%.2fr;   isnullnums    r5   r  z$MLEResults.summary.<locals>.<lambda>      ")C..$JBBfsl r7   c                 8    t          j        |           rdnd| z  S r  r  r  s    r5   r  z$MLEResults.summary.<locals>.<lambda>  r  r7   zResidual of
Dep. variablezResidual diagnostics:)txt_fmtrD   rO  zmCovariance matrix is singular or near-singular, with condition number %6.3g. Standard errors may be unstable.c                 *    g | ]\  }}d |dz    d| S )[r   z] rQ   )r   r2   texts      r5   r   z&MLEResults.summary.<locals>.<listcomp>  s@     6 6 6 D )Q(($(( 6 6 6r7   z	Warnings:)Dstatsmodels.iolib.summaryr  statsmodels.iolib.tabler  !statsmodels.iolib.tableformattingr  r&  rt   ru   monthdayyearr  rW   r`   r  ro   r  r"   r   re   infr&   r%   r!   rg  r   r  r   r   r   r"  r,  r  r   add_table_2colsr   add_table_paramsr   r  	Exceptionr3  r  r  r  r;   r=   r  mapr~   applymapr>   r3   reset_indexr  r  r   tablesinsertr   r3  r  r  r[  r^   r   r5  add_extra_txt)+r_   rR  rq   rD   
model_namedisplay_paramsdisplay_diagnosticsrv  display_max_endogextra_top_leftextra_top_rightr  r  r  r&  r  rT  sampler  r  r   top_leftr2   	top_rightr   summarytable_ixhetlbjb
format_str
diagn_leftdiagn_rightr  r8   params_dataparams_headerparams_stubstableetextr  re  r;  s+                                             @r5   r  zMLEResults.summaryV  s^	   4 	655555777777@@@@@@ 
=.E=E:" 	:"B5	A&!'15!&)AABF2At.!'15!&1IIIJJFF%jj%#di.."89F 1J  ',0J,>!,C,C55 j,+t,, 	(&-K99[999u,,$A4 4 4 4'24 4 4K $ ": 111
"Q&A+a.(+=Q+=+=+==E *d++ 	&$J,-JqM?3444q#j//** 	: 	:AOOR$A"6!789999$&)	
 	
 !49+.48 345
	 4$$ 	IlX-E,FGHHHX()*X()*h*+,. 	.	 +#7 ,g4:(='>?@@@4$$ 	>}H6!!)OO/(<===%&H&(I'))HY&+5 	  	: 	: 	:t{aN$$T+/+;5 % J J JMH  :	7A22*2EE A A Ah
 2A677"&@AC111#1NN C C CXtz11a899BFBC@(((== @ @ @Xtz11566?@ z!%666 
 +JJr!!!Q(|,D,DEBqqq!RxL!9!9:.

3qqq!t90E0EF+ZZAAAqD	-B-BC	E
 !4ZZ111a45I5IJ +ZZ111a4-A-AB 'Bqqq!tH)=)=> +ZZ111a4-A-AB! ''J/:" ( F F F FO O O |E"QQQBY-QQQUR2A2Y>?%w8 8 888JJ DD &   ==JJ DDD #?
''))"k $ 3 3 5 5#/#&e5 5 5 %%h666 4$$ 	7$-)G)GLL}5666:T[))C0A,B,BBCC**J4$%%))vd5t7NOO'-
LL &(2:(>(>? @ @ @  	)6 6$-e$4$46 6 6ELLK(((!!%(((sH   5M 7NN
N# #8OO"O9 97P32P3.V  V('V()NN)rx  )TF)r  )r  T)FN)	NNFr@  FNNNN)r   F)NNFr@  F)NNNNNNNNTTTN)r   r   FF)NNNNNNNFNFr  )r  N)NFNF)r   r  NNrJ  FFN)
r}  NNNTTNNNN)9r  r  r  r  rS   r4  r   r   r   r   rs  r^  rw  r`  r|  ra  r  r  rb  r  rc  r  r  r   r  rg  r   r  r  r  r  r   r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r/  r&   r  r   r|  r  rQ   r7   r5   r  r    s        @RG RG RG RGhn n n n` A A ^A B B ^B A A ^A 6:+0   , B B ^B   & ? ? ^?   8 ? ? ^? * * ^* :>/4   < F F ^F =A27   > I I ^IM M M M^   ^  B B ^B + + ^+ ' ' ^' 6 6 ^6 + + ^+ $ $ ^$   ^   ^    X % % ^% & & ^&.  .  . ` ;F&*l l l l\\ \ \ \| <A@E;?%)fC fC fC fCP-F -F -F -F^ 5:9>F1 F1 F1 F1P.& .& .& .&` 9=?C;?#37-1.2"J J J JX DI%*L L L L\. . . .` 6:.2C5 C5 C5 C5L 8=+/    JN% % % % HL% % % % GK% % % % DH6:;@;@o o o ob ;K/3ED ED ED EDN @D#(I I I IV^ ^ ^ ^@ ?C"'[ [ [ [z GK=B<@O O O Ob EI9==A59G G G G G Gr7   r  c            	           e Zd ZdddddddddZ ej        ej        j        e          ZdddZ	 ej        ej        j
        e	          Z
dS )r  r  r  )r  r^  r6  r`  ra  r  rc  rb  rO   ynames)r  r  N)r  r  r  _attrswrapunion_dictsr$  TimeSeriesResultsWrapper_wrap_attrs_methods_wrap_methodsrQ   r7   r5   r  r     s        "#"$)!&	 	F #$"6#B#N#)+ +K % H %D$'5xA AMMMr7   r  c                   R     e Zd ZdZ	 	 d fd	Zed             Zd fd		ZddZ xZ	S )r  a  
    Prediction result from MLE models

    Parameters
    ----------
    model : MLEModel
        The models used to make the prediction
    prediction_results : kalman_filter.PredictionResults instance
        Results object from prediction after fitting or filtering a state space
        model.
    row_labels : iterable
        Row labels for the predicted data.
    information_set : str
        Name of information set
    signal_only : bool
        Whether the prediction is for the signal only

    Attributes
    ----------
    model : MLEModel
        The models used to make the prediction
    prediction_results : kalman_filter.PredictionResults instance
        Results object from prediction after fitting or filtering a state space
        model.
    information_set : str
        Name of information set
    signal_only : bool
        Whether the prediction is for the signal only
    Nr@  Fc                    |j         j        dk    r,t          j        |j        d         |j         j                  }n*t          j        |j        j        |j         j                  }t          |j	        
                    ||                    | _         || _        || _        || _        |j        j        \  }}| j        j        }	|dk    r#|	j        s|s| j        j        }
n{| j        j        }
nn|dk    r#|	j        s|s| j        j        }
nR| j        j        }
nE|d	k    r|s| j        j        }
n0| j        j        }
n#t1          j        ||f          t0          j        z  }
|
j        d         dk    r|
dd d f         }
n|
                                }
|dk    r#|	j        s|s| j        j        }n|| j        j        }no|dk    r#|	j        s|s| j        j        }nS| j        j         }nF|d	k    r|s| j        j!        }n1| j        j"        }n$t1          j        |||f          t0          j        z  }|j        d         dk    r|ddd d f         }n|                                }tG                      $                    |
|d
|           d S )Nr   r   r  r  )rM   predict_datesr8   r@  rC  rE  r   )distr  )%r&  ro   r;   r<   rM   r  r=   rl   r   r8   r`   r  r  r  rV   rH  r8  r  predicted_signalr<  filtered_forecastsfiltered_signalsmoothed_forecastssmoothed_signalre   r3  r  rZ  r9  r8  predicted_signal_covfiltered_forecasts_error_covfiltered_signal_covsmoothed_forecasts_error_covsmoothed_signal_covrR   rS   )r_   r&  r  r  r  r  rM   ro   rW   r  r  var_pred_meanr`   s               r5   rS   zPredictionResults.__init__T  s   ;!##I06q9#(;#:< < <EE L!3!9!;).)@B B BE
 4 4z !5 !3 !3 4 4 4
"4.& +06%-k))#2M) J!%!8!B!%!8!I
**33N* I!%!8!K!%!8!H
** I!%!8!K!%!8!HXwo66?N"a''+AqqqD1NN+5577Nk))#2L) M $ 7 K $ 7 L
**33N* L+H  !% 7 K
** L+H  !% 7 KHgw%=>>GMq!Q&&)!Q'2MM)3355M 	$$. 	 	0 	0 	0 	0 	0r7   c                     | j                                         }t          j        ||dk     <   |j        dk    rt          j        |          }n+t          j        |j                                                  }|S )Nr   r   )r  rb   re   r  rj   sqrtrl   r)  )r_   r  se_means      r5   r  zPredictionResults.se_mean  sj     *//11+-6ma'(""gm,,GGgmo668899Gr7   endpointr}  c                 >   | j         }d| _          t                      j        |fi |}|| _         | j        et	          j        || j                  }| j        j        j	        }t          |          t          ur|g}d |D             d |D             z   }||_        |S )NFrN  c                     g | ]}d | S )zlower rQ   r  s     r5   r   z.PredictionResults.conf_int.<locals>.<listcomp>      999$otoo999r7   c                     g | ]}d | S )zupper rQ   r  s     r5   r   z.PredictionResults.conf_int.<locals>.<listcomp>  r  r7   )_use_pandasrR   conf_int_row_labelsr;   r=   r  r&  r8   r  r  r   r  )	r_   r   rR  r   r  r  r  r,   r`   s	           r5   r  zPredictionResults.conf_int  s     & #577#E22T22& '|HDODDDH Z_+FF||4'' 99&99999&999:E$Hr7   r   c                 f   t          j        |                     |                    }| j        }d| _        i }| j        j        dk    r(| j        j        j        }| j        |d<   | j	        |d<   d}nQ| j        j        j        |         }| j        d d |f         |d<   | j	        d d |f         |d<   | j        j
        d         }|| _        |d d |f         |d<   |d d ||z   f         |d<   t          j        || j        t          |                                                    }||j        _        |S )	NrQ  Fr   r  mean_semean_ci_lowermean_ci_upperr  )re   r  r  r  r  rj   r&  r8   r  r  rV   r;   r=   r  r   r   r  r3   )	r_   rM   rR  ci_meanr  
to_includer  ro   r  s	            r5   summary_framezPredictionResults.summary_frame  sE    *T]]]7788& 
#q((JO*E!%!4Jv$(LJy!GGJO*51E!%!4QQQX!>Jv$(LE$:Jy!)/2G&&-aaah&7
?#&-aaa5.@&A
?#
 l:T-=#'
(9(9#:#:< < < 
r7   )Nr@  F)r  r}  )r   r}  )
r  r  r  r  rS   r   r  r  r  r  r  s   @r5   r  r  6  s         : >B:?E0 E0 E0 E0 E0 E0N   X     .       r7   r  c                   ^    e Zd ZddddZ ej        e          Zi Z ej        e          ZdS )r  rO   )r  r  t_valuesN)	r  r  r  r  r  r  r  r  r  rQ   r7   r5   r  r    sN        ! F
 #$"6**KH$D$X..MMMr7   r  r  )Jr  statsmodels.compat.pandasr   r  r  r  r  typesr   numpyre   pandasr;   rm  r   statsmodels.tools.toolsr   r   statsmodels.tools.sm_exceptionsr   r	   statsmodels.tools.numdiffr
   r   r   r   statsmodels.tools.decoratorsr   statsmodels.tools.eval_measuresr   r   r   r   statsmodels.base.wrapperbasewrapperr  statsmodels.tsa.base.predictiontsa
predictionpredstatsmodels.base.datar   statsmodels.tsa.base.tsa_model	tsa_modelr$  r  r   r+  r   kalman_smootherr   kalman_filterr   r   r   r   r   toolsr   r   r   r   r6   rH   TimeSeriesModelrJ   r%  r  ResultsWrapperr  populate_wrapperr  r  rQ   r7   r5   <module>r     s    3 2 2 2 2 2          ! ! ! ! ! !               8 8 8 8 8 8 8 8 J J J J J J J JH H H H H H H H H H H H 7 7 7 7 7 7 @ @ @ @ @ @ @ @ @ @ @ @ ' ' ' ' ' ' ' ' ' . . . . . . . . . . . . , , , , , , / / / / / / / / / / / /       3 3 3 3 3 3 , , , , , , G G G G G G G G G G * * * * * * E E E E E E E E E E E E* * *20 0 0 0(!" !" !" !" !"v% !" !" !"DDF) F) F) F) F). F) F) F)RRA A A A A+ A A A&  ' 4 4 4c c c c c. c c cL	/ 	/ 	/ 	/ 	/t2 	/ 	/ 	/  .0A B B B B Br7   