
    !gF                         d Z ddlZddlmc mZ ddlmZ ddl	m
Z
mZ ddlmZmZ  G d dej                        Z G d d	ej"                        Z G d
 dej&                        Z ej*                  ee       y)zL
Markov switching autoregression models

Author: Chad Fulton
License: BSD-3
    N)lagmat)markov_switchingmarkov_regression)constrain_stationary_univariate!unconstrain_stationary_univariatec                        e Zd ZdZ	 	 	 	 d fd	Zd Zd Zd Zed        Z	d Z
ddZed	        Zed
        Z fdZ fdZ xZS )MarkovAutoregressionah  
    Markov switching regression model

    Parameters
    ----------
    endog : array_like
        The endogenous variable.
    k_regimes : int
        The number of regimes.
    order : int
        The order of the autoregressive lag polynomial.
    trend : {'n', 'c', 't', 'ct'}
        Whether or not to include a trend. To include an constant, time trend,
        or both, set `trend='c'`, `trend='t'`, or `trend='ct'`. For no trend,
        set `trend='n'`. Default is a constant.
    exog : array_like, optional
        Array of exogenous regressors, shaped nobs x k.
    exog_tvtp : array_like, optional
        Array of exogenous or lagged variables to use in calculating
        time-varying transition probabilities (TVTP). TVTP is only used if this
        variable is provided. If an intercept is desired, a column of ones must
        be explicitly included in this array.
    switching_ar : bool or iterable, optional
        If a boolean, sets whether or not all autoregressive coefficients are
        switching across regimes. If an iterable, should be of length equal
        to `order`, where each element is a boolean describing whether the
        corresponding coefficient is switching. Default is True.
    switching_trend : bool or iterable, optional
        If a boolean, sets whether or not all trend coefficients are
        switching across regimes. If an iterable, should be of length equal
        to the number of trend variables, where each element is
        a boolean describing whether the corresponding coefficient is
        switching. Default is True.
    switching_exog : bool or iterable, optional
        If a boolean, sets whether or not all regression coefficients are
        switching across regimes. If an iterable, should be of length equal
        to the number of exogenous variables, where each element is
        a boolean describing whether the corresponding coefficient is
        switching. Default is True.
    switching_variance : bool, optional
        Whether or not there is regime-specific heteroskedasticity, i.e.
        whether or not the error term has a switching variance. Default is
        False.

    Notes
    -----
    This model is new and API stability is not guaranteed, although changes
    will be made in a backwards compatible way if possible.

    The model can be written as:

    .. math::

        y_t = a_{S_t} + x_t' \beta_{S_t} + \phi_{1, S_t}
        (y_{t-1} - a_{S_{t-1}} - x_{t-1}' \beta_{S_{t-1}}) + \dots +
        \phi_{p, S_t} (y_{t-p} - a_{S_{t-p}} - x_{t-p}' \beta_{S_{t-p}}) +
        \varepsilon_t \\
        \varepsilon_t \sim N(0, \sigma_{S_t}^2)

    i.e. the model is an autoregression with where the autoregressive
    coefficients, the mean of the process (possibly including trend or
    regression effects) and the variance of the error term may be switching
    across regimes.

    The `trend` is accommodated by prepending columns to the `exog` array. Thus
    if `trend='c'`, the passed `exog` array should not already have a column of
    ones.

    See the notebook `Markov switching autoregression
    <../examples/notebooks/generated/markov_autoregression.html>`__
    for an overview.

    References
    ----------
    Kim, Chang-Jin, and Charles R. Nelson. 1999.
    "State-Space Models with Regime Switching:
    Classical and Gibbs-Sampling Approaches with Applications".
    MIT Press Books. The MIT Press.
    c                 
   || _         | j                   du s| j                   du r| j                   g|z  | _         n#t        | j                         |k(  st        d      t        |   ||||||||	|
|||       | j
                  | j                  k  rt        d      t        || j                        | j                  d  | _        | xj
                  | j                  z  c_        | j                  | _
        | j                  | j                  d  | _	        | j                  dkD  r/| j                  | _        | j                  | j                  d  | _        | j                  j                  | j                  | j                        \  | j                  _	        | j                  _        | j                  j                   :| j                  j                   | j                  d  | j                  j"                  d<   | j$                  J| j&                  r | j$                  d | j                    | _        n| j$                  | j                  d  | _        | j                   | j(                  d<   t+        d d d       g| j                  d	z   z  | _        y )
NTFz*Invalid iterable passed to `switching_ar`.)
trendexogorder	exog_tvtpswitching_trendswitching_exogswitching_variancedatesfreqmissingzAMust have more observations than the order of the autoregression.r   
row_labelsautoregressive   )switching_arlen
ValueErrorsuper__init__nobsr   r   exog_arendog
orig_endog_k_exogr   	orig_exogdata_convert_endog_exogr   _cache_index_index_generated
parametersslice_predict_slices)selfr   	k_regimesr   r   r   r   r   r   r   r   r   r   r   	__class__s                 s/var/www/dash_apps/app1/venv/lib/python3.12/site-packages/statsmodels/tsa/regime_switching/markov_autoregression.pyr   zMarkovAutoregression.__init__d   s!    ) $(9(9U(B!%!2!2 3e ;DT&&'50IJJ 	9EE)1T 	 	 99

" 4 5 5 eTZZ0= 			TZZ	**ZZ

,
<<!!YYDN		$**+.DI II))$**dii@ 	(		 99+		$$TZZ[1 II\*;;"$$"kk,DJJ;7"kk$**+6 -1,=,=() !&dD$ 78DJJNK    c                 x   t        j                  |d      }| j                  dkD  r_g }t        | j                        D ]E  }|| j
                  |df      }|j                  t        j                  | j                  |             G t        j                  | j                  f| j                  dz   z  | j                  fz   t        j                  t         j                  |j                              }t        | j                        D ]/  }|| j
                  |df      }| j                  dd }||d<   t!        |      }| j                  dkD  r||xx   |   | j                  d z  cc<   t        d| j                  dz         D ]  }t        | j                        D ]  }	| j                  dd }||d<   |	||<   t!        |      }| j                  |z
  }
| }| j                  dkD  r-||xx   ||dz
     | j"                  |
| |	   |
| z
  z  z  cc<   u||xx   ||dz
     | j"                  |
| z  z  cc<     2 |S )aw  
        In-sample prediction, conditional on the current and previous regime

        Parameters
        ----------
        params : array_like
            Array of parameters at which to create predictions.

        Returns
        -------
        predict : array_like
            Array of predictions conditional on current, and possibly past,
            regimes
        r   )ndminr   r   dtyper   N)nparrayr!   ranger,   r(   appenddotr"   zerosr   r   promote_typesfloat64r3   r*   tupler    )r+   paramsxbicoeffspredict	ar_coeffsixjkstartends               r.   predict_conditionalz(MarkovAutoregression.predict_conditional   s1    &* <<!B4>>* :6	 :;		"&&89: ((^^a0DII<?""2::v||<> t~~& 	8Atq2B/BCDI %%a(BBqErB||ar!uTZZ[11 1djj1n- 8t~~. 8A --a0BBqEBqErB JJNE"C||a'y1~ OOE#6AuS9II(K K  y1~ OOE#6(8 888	8< r/   c                 >    | j                   | j                  |      z
  S N)r   rH   )r+   r=   s     r.   _residzMarkovAutoregression._resid   s    zzD44V<<<r/   c                 6   | j                  |      }|| j                  d      j                         }| j                  r#t	        j
                  || j                  ddf      }d|dz  z  |z  dt	        j                  dt        j                  z  |z        z  z
  }|S )z~
        Compute loglikelihoods conditional on the current period's regime and
        the last `self.order` regimes.
        variancer   g         g      ?)	rK   r(   squeezer   r4   reshaper,   logpi)r+   r=   residrM   conditional_loglikelihoodss        r.   _conditional_loglikelihoodsz0MarkovAutoregression._conditional_loglikelihoods   s     F# $//*56>>@""zz(T^^Q,BCH 5!8Oh&rvva"%%i(6J/K)KK 	# *)r/   c                     dt         t        fiS )Nfit)MarkovAutoregressionResults"MarkovAutoregressionResultsWrapper)r+   s    r.   _res_classesz!MarkovAutoregression._res_classes   s    3:< = 	=r/   c                 P   t         j                  j                  | |      \  }}t        j                  |j
                        }d}| j                  dkD  rq| j                  || j                  | j                  | j                  j                  d   |      }t        | j                        D ]  }||   || j                  |df   <    | j                  dkD  r| j                  dkD  r| j                  ||      \  }}nh| j                  || j                  | j                   | j                  j                  d         }| j#                  || j                  | j                   ||      }t        | j                        D ]  }||   || j                  |df   <    ||| j                  d   <   ||fS )z
        EM iteration
        Nr   r   r   rM   )r   MarkovSwitching_em_iterationr4   sqrtsmoothed_marginal_probabilitiesr!   _em_exogr   r   r(   	switchingr6   r,   r   _em_autoregressiver   _em_variance)	r+   params0resultparams1tmpr@   r?   rB   rM   s	            r.   r]   z"MarkovAutoregression._em_iteration   s   
 +::HH' ggf<<= <<!]]64::tyy#'??#<#<V#DcKF4>>* @6<Qi6	23@ ::>||a&*&=&=F'$#	8 !MMDJJOO--.>?A	  ,,DJJiF4>>* M@I!+;(;<=M3;GDOOJ/0wr/   c           	      n   |t        j                  |j                        }t        j                  | j                  | j
                  | j                  z   f      }| j                  |dd | j                  dkD  rHt        | j                        D ]0  }||xx   t        j                  | j                  ||         z  cc<   2 t        j                  | j                  f| j                  fz         }t        j                  | j                  f      }t        j                  | j
                  | j                  f      }t        | j                        D ]8  }||| j                  df   }	t        ||   | j                        | j                  d }||   |	z  }
||   dddf   |z  }t        j                  t         j                  j                  |      |
      ||<   | j                  rl|	t        j                  |||         z
  }t        j                   |dz  |j                  |   z        t        j                   |j                  |         z  ||<   |
t        j                  |||         z
  }t        j                   |dz        ||<   ; | j                  s|j!                         | j
                  z  }||fS )zG
        EM step for autoregressive coefficients and variances
        Nr   rN   )r4   r^   r_   r9   r,   r   r   r    r!   r6   r8   r"   r   linalgpinvr   sum)r+   re   betasrg   rS   r?   r@   rM   r   r   	tmp_endogtmp_exog	tmp_resids                r.   rb   z'MarkovAutoregression._em_autoregressive  s8    ;''&@@AC$..$))djj*@AB??a<<!4>>* =aBFF4>>58<<=
 4>>+tzzm;<88T^^-.xxDJJ/0t~~& 	3A!TZZ[.)E%(DJJ/

<DAI1vag-Hryy~~h7CF1I&&!BFF4$;;	!vvqL6#I#I!#LL NFF6AA!DE F &x(CC	 ffY\2	3$ &&||~		1Hxr/   c                    t         j                  j                  j                  |       }| j                  j                         }| j                  dkD  r9| j                  dkD  r*t        j                  | j                  | j                  f   }n7| j                  dkD  r| j                  }n| j                  dkD  r| j                  }| j                  dkD  s| j                  dkD  r`t        j                  t        j                  j                        |      }t        j                  |t        j                  ||      z
        }nt        j                  |      }| j                  dkD  rt        j                   | j"                        rLt%        | j&                        D ]3  }d| j                   || j&                  z  z  || j(                  |df   <   5 nd| j                   || j(                  d   <   | j                  dkD  rt        j                   | j*                        rLt%        | j&                        D ]3  }| j                  d || j&                  z  z  || j(                  |df   <   5 n| j                  d || j(                  d   <   | j,                  r7t        j.                  |dz  || j&                        || j(                  d   <   |S ||| j(                  d   <   |S )zP
        (array) Starting parameters for maximum likelihood estimation.
        r   Nr   r   g      $@)numrM   )r   r\   start_paramsfgetr   copyr!   r   r4   c_r   r   r8   ri   rj   varanyswitching_coeffsr6   r,   r(   r   r   linspace)r+   r=   r   r   betarM   r?   s          r.   rr   z!MarkovAutoregression.start_paramsC  s^    "11>>CCDI 

!<<!

Q55DLL01D\\A99DZZ!^<<D<<!tzzA~66"))...6DvvebffT4&889Hvve}H <<!vvd++,t~~. DA]dll+q4>>/AB 4??1f956D 37}2Etv./ ::>vvd''(t~~. DAT\\]+q4>>/AB 4??1.>+>?@D =A<Ot'789 ""HsNH$..I 4??:./
  3;F4??:./r/   c                 8   t        j                  t        j                  j                  j                  |       t              }t        j                  | j                        rgt        | j                        D ]?  }t        | j                        D cg c]  }d|dz   |fz   c}|| j                  |df   <   A |j                         S t        | j                        D cg c]
  }d|dz   z   c}|| j                  d   <   |j                         S c c}w c c}w )z
        (list of str) List of human readable parameter names (for parameters
        actually included in the model).
        r2   z
ar.L%d[%d]r   r   zar.L%d)r4   r5   r   MarkovRegressionparam_namesrs   objectrw   r   r6   r,   r   r(   tolist)r+   r}   r?   rD   s       r.   r}   z MarkovAutoregression.param_namesu  s    hh..::??E
 66$##$4>>* H5:4::5FEH01LAaC8+EHDOOA/?,?@AH !!## +0

*;>=%&AaC >=K(89: !!##EH>=s   D Dc                     t         |   |      }t        | j                        D ]$  }| j                  |df   }t        ||         ||<   & |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.
        r   )r   transform_paramsr6   r,   r(   r   )r+   unconstrainedconstrainedr?   sr-   s        r.   r   z%MarkovAutoregression.transform_params  sc    $ g. t~~& 	"A#3 34A<a "KN	"
 r/   c                     t         |   |      }t        | j                        D ]$  }| j                  |df   }t        ||         ||<   & |S )a  
        Transform constrained parameters used in likelihood evaluation
        to unconstrained parameters used by the optimizer

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

        Returns
        -------
        unconstrained : array_like
            Array of unconstrained parameters used by the optimizer.
        r   )r   untransform_paramsr6   r,   r(   r   )r+   r   r   r?   r   r-   s        r.   r   z'MarkovAutoregression.untransform_params  sc    " 2 t~~& 	 A#3 34A@A  M!	 
 r/   )
cNNTTFFNNnonerJ   )__name__
__module____qualname____doc__r   rH   rK   rU   propertyrZ   r]   rb   rr   r}   r   r   __classcell__)r-   s   @r.   r	   r	      s    N` AEDH:?068Lt=~=*$ = =!F' R / /b $ $*> r/   r	   c                       e Zd ZdZy)rX   a  
    Class to hold results from fitting a Markov switching autoregression model

    Parameters
    ----------
    model : MarkovAutoregression instance
        The fitted model instance
    params : ndarray
        Fitted parameters
    filter_results : HamiltonFilterResults or KimSmootherResults instance
        The underlying filter and, optionally, smoother output
    cov_type : str
        The type of covariance matrix estimator to use. Can be one of 'approx',
        'opg', 'robust', or 'none'.

    Attributes
    ----------
    model : Model instance
        A reference to the model that was fit.
    filter_results : HamiltonFilterResults or KimSmootherResults instance
        The underlying filter and, optionally, smoother 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 and not used by the model or its results.
    N)r   r   r   r    r/   r.   rX   rX     s    8 	r/   rX   c                       e Zd Zy)rY   N)r   r   r   r   r/   r.   rY   rY     s    r/   rY   )r   numpyr4   statsmodels.base.wrapperbasewrapperwrapstatsmodels.tsa.tsatoolsr    statsmodels.tsa.regime_switchingr   r    statsmodels.tsa.statespace.toolsr   r   r|   r	   MarkovRegressionResultsrX   MarkovRegressionResultsWrapperrY   populate_wrapperr   r/   r.   <module>r      sx     ' ' +)Hs,== sl	"3"K"K 	@	88	   813r/   