
    M/Ph                     :   d Z ddlZddlmZmZmZ ddlmZm	Z	m
Z
mZmZmZ ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmc m Z! ddl"m#Z#  G d de          Z$ G d de          Z% G d de          Z& e!j'        e&e%           dS )zC
Dynamic factor model

Author: Chad Fulton
License: Simplified-BSD
    N   )MLEModel
MLEResultsMLEResultsWrapper)is_invertibleprepare_exogconstrain_stationary_univariate!unconstrain_stationary_univariate!constrain_stationary_multivariate#unconstrain_stationary_multivariate)PCA)OLS)VAR)ARIMA)Bunch)_is_using_pandas)lagmat)cache_readonly)Appenderc                        e Zd ZdZ	 	 	 d fd	Zd Zd	 Zd
 ZddZd Z	d Z
d Zd Zd Zd ZddZed             Zed             Zed             Zed             Zd Zd Z fdZ	 	 ddZ xZS )DynamicFactoruu  
    Dynamic factor model

    Parameters
    ----------
    endog : array_like
        The observed time-series process :math:`y`
    exog : array_like, optional
        Array of exogenous regressors for the observation equation, shaped
        nobs x k_exog.
    k_factors : int
        The number of unobserved factors.
    factor_order : int
        The order of the vector autoregression followed by the factors.
    error_cov_type : {'scalar', 'diagonal', 'unstructured'}, optional
        The structure of the covariance matrix of the observation error term,
        where "unstructured" puts no restrictions on the matrix, "diagonal"
        requires it to be any diagonal matrix (uncorrelated errors), and
        "scalar" requires it to be a scalar times the identity matrix. Default
        is "diagonal".
    error_order : int, optional
        The order of the vector autoregression followed by the observation
        error component. Default is None, corresponding to white noise errors.
    error_var : bool, optional
        Whether or not to model the errors jointly via a vector autoregression,
        rather than as individual autoregressions. Has no effect unless
        `error_order` is set. Default is False.
    enforce_stationarity : bool, optional
        Whether or not to transform the AR parameters to enforce stationarity
        in the autoregressive component of the model. Default is True.
    **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
    ----------
    exog : array_like, optional
        Array of exogenous regressors for the observation equation, shaped
        nobs x k_exog.
    k_factors : int
        The number of unobserved factors.
    factor_order : int
        The order of the vector autoregression followed by the factors.
    error_cov_type : {'diagonal', 'unstructured'}
        The structure of the covariance matrix of the error term, where
        "unstructured" puts no restrictions on the matrix and "diagonal"
        requires it to be a diagonal matrix (uncorrelated errors).
    error_order : int
        The order of the vector autoregression followed by the observation
        error component.
    error_var : bool
        Whether or not to model the errors jointly via a vector autoregression,
        rather than as individual autoregressions. Has no effect unless
        `error_order` is set.
    enforce_stationarity : bool, optional
        Whether or not to transform the AR parameters to enforce stationarity
        in the autoregressive component of the model. Default is True.

    Notes
    -----
    The dynamic factor model considered here is in the so-called static form,
    and is specified:

    .. math::

        y_t & = \Lambda f_t + B x_t + u_t \\
        f_t & = A_1 f_{t-1} + \dots + A_p f_{t-p} + \eta_t \\
        u_t & = C_1 u_{t-1} + \dots + C_q u_{t-q} + \varepsilon_t

    where there are `k_endog` observed series and `k_factors` unobserved
    factors. Thus :math:`y_t` is a `k_endog` x 1 vector and :math:`f_t` is a
    `k_factors` x 1 vector.

    :math:`x_t` are optional exogenous vectors, shaped `k_exog` x 1.

    :math:`\eta_t` and :math:`\varepsilon_t` are white noise error terms. In
    order to identify the factors, :math:`Var(\eta_t) = I`. Denote
    :math:`Var(\varepsilon_t) \equiv \Sigma`.

    Options related to the unobserved factors:

    - `k_factors`: this is the dimension of the vector :math:`f_t`, above.
      To exclude factors completely, set `k_factors = 0`.
    - `factor_order`: this is the number of lags to include in the factor
      evolution equation, and corresponds to :math:`p`, above. To have static
      factors, set `factor_order = 0`.

    Options related to the observation error term :math:`u_t`:

    - `error_order`: the number of lags to include in the error evolution
      equation; corresponds to :math:`q`, above. To have white noise errors,
      set `error_order = 0` (this is the default).
    - `error_cov_type`: this controls the form of the covariance matrix
      :math:`\Sigma`. If it is "dscalar", then :math:`\Sigma = \sigma^2 I`. If
      it is "diagonal", then
      :math:`\Sigma = \text{diag}(\sigma_1^2, \dots, \sigma_n^2)`. If it is
      "unstructured", then :math:`\Sigma` is any valid variance / covariance
      matrix (i.e. symmetric and positive definite).
    - `error_var`: this controls whether or not the errors evolve jointly
      according to a VAR(q), or individually according to separate AR(q)
      processes. In terms of the formulation above, if `error_var = False`,
      then the matrices :math:C_i` are diagonal, otherwise they are general
      VAR matrices.

    References
    ----------
    .. [*] Lütkepohl, Helmut. 2007.
       New Introduction to Multiple Time Series Analysis.
       Berlin: Springer.
    Nr   FdiagonalTc	                     | _         | _        | _        | _        |o|dk     _        | _        t          |          \   _        } j        dk     _        t          |d           st          j        |d          }|j        dk    r|j        d         nd}
t          d j                   j        z   _         j        |
z   _         j        } j        } j        dk    r| j        z  }||
z  }d _        |dk    rd}d}d _        |
dk     rt%          d           j        |
k    rt%          d	           j        d
vrt%          d          |	                    dd            t)                      j        |f|||d|	  j        dk    rd j        _        i  _                                                                                                                                                                                  t=           j                                                   _          fd}d} |d|          \   _!        } |d|          \   _"        } |d|          \   _#        } |d|          \   _$        } |d|          \   _%        } xj&        g dtO          |	(                                          z   z  c_&        d S )Nr   C)orderr   FT   zEThe dynamic factors model is only valid for multivariate time series.zGNumber of factors must be less than the number of endogenous variables.)scalarr   unstructuredz3Invalid error covariance matrix type specification.initialization
stationary)exogk_statesk_posdefc                 ^    j         |          }t          j        |||z            }||z  }||fS N)
parametersnps_)keyoffsetlengthparam_sliceselfs       i/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/statsmodels/tsa/statespace/dynamic_factor.py_slicez&DynamicFactor.__init__.<locals>._slice   s9    _S)F%v 67KfF&&    factor_loadingsr!   	error_covfactor_transitionerror_transition)	k_factorsfactor_ordererror_order	error_varerror_cov_typeenforce_stationarity))r:   r5   r6   r7   r8   r9   r   k_exogmle_regressionr   r'   
asanyarrayndimshapemax_factor_order_error_order_unused_state
ValueError
setdefaultsuper__init__ssm_time_invariantr&   _initialize_loadings_initialize_exog_initialize_error_cov_initialize_factor_transition_initialize_error_transitionsumvaluesk_params_params_loadings_params_exog_params_error_cov_params_factor_transition_params_error_transition
_init_keyslistkeys)r-   endogr5   r6   r!   r7   r8   r9   r:   kwargsk_endogr"   r#   r/   r*   	__class__s   `              r.   rG   zDynamicFactor.__init__   so   
 %9! #( '"6{Q, +400d #kAo  t,, 	4M%s333E %*JNN%+a.. D$566G ,w6 %>a))HH #q==HH!%D Q;; : ; ; ; >W$$ 9 : : : &LLL / 0 0 0 	*L999 		
x(	
 	
FL	
 	
 	

 ;??',DH$ !!###""$$$**,,,))+++DO224455	' 	' 	' 	' 	' (./@&(I(I%v$*F66$:$:!6)/V)D)D&F&// 	/& F%v.. 	.%v 	 4 4 46:6;;==6I6IJ 	Jr0   c                    | j         | j        z  | j        d<   | j        dk    r>| j        }| j        | j         z   }t          j        | j                   | j        dd d ||f<   t
          j        dd d d | j        f         | _	        d S )Nr1   r   design)
r\   r5   r&   r7   rA   r'   eyerH   r(   _idx_loadings)r-   startends      r.   rJ   z"DynamicFactor._initialize_loadings   s    -1\DN-J)* a&E$t|3C/1vdl/C/CDHXqqq%)+,  U8QQQ#?@r0   c                     | j         | j        z  | j        d<   | j         dk    r(t          j        | j        | j        f          | j        d<   t          j        dd | j        d d f         | _        d S )Nr!   r   obs_intercept)	r;   r\   r&   r'   zerosnobsrH   r(   	_idx_exogr-   s    r.   rK   zDynamicFactor._initialize_exog  sh    "&+"< ;??(*$,	1J(K(KDH_% qqq@Ar0   c                     | j         dk    r|                     d           d S | j         dk    r|                     d           d S | j         dk    r|                                  d S d S )Nr   T)r   r   Fr   )r9   _initialize_error_cov_diagonal"_initialize_error_cov_unstructuredri   s    r.   rL   z#DynamicFactor._initialize_error_cov  s    (**//t/<<<<< J..//u/===== N223355555 32r0   c                     |rdn| j         | j        d<   | j         }| j        }t          j        |          }| j        dk    rd}|d         |z   |d         |z   f}nd}|f|z   | _        d S )Nr   r2   r   	state_covobs_cov)r\   r&   r5   r'   diag_indicesr7   _idx_error_cov)r-   r   r\   r5   idxmatrixs         r.   rk   z,DynamicFactor._initialize_error_cov_diagonal  s    ,2'Dqq$
 ,N	og&&a Fq6I%s1v	'9:CCF%i#or0   c                 N   | j         }t          ||dz   z  dz            | j        d<   t          j        | j                   | _        | j        dk    r6| j        }| j        | j         z   }t          j        d||||f         | _	        d S t          j        dd d d d f         | _	        d S )Nr   r   r2   r   rn   ro   )
r\   intr&   r'   tril_indices_idx_lower_error_covr7   r5   r(   rq   )r-   r\   rb   rc   s       r.   rl   z0DynamicFactor._initialize_error_cov_unstructured&  s    ,'*7gk+BQ+F'G'G$
 %'ODL$A$A!aNE.4</Ck59eCi78  #%%	111aaa"8Dr0   c                    | j         | j        z  }| j        }| j         | j        dz  z  | j        d<   | j        dk    rz| j         dk    r)t          j        ||z
            | j        d||d ||z
  f<   t          j        |          | j        dd |d |f<   t          j        |          | j        dd |d |f<   t          j        dd |d |f         | _        d S )Nr   r3   r   
transition	selectionrn   )r6   r5   r&   r'   r`   rH   r(   _idx_factor_transition)r-   r   r5   s      r.   rM   z+DynamicFactor._initialize_factor_transition7  s    !DN2N	  11 	+,
 >A 1$$F59,-- y8J9J8JJK =?F9<M<MDH[*9*jyj89 =?F9<M<MDH[*9*jyj89 ')eL*9*fuf,L&M###r0   c                    | j         dk    r|                                  d S | j        }| j        }| j        }| j        }t          j        d|||z   |||z   f         }t          j        |          | j	        |<   t          j        d||z   ||z   |||z   |z
  f         }t          j        ||z
            | j	        |<   | j
        r|                                  d S |                                  d S )Nr   rz   ry   )r7   (_initialize_error_transition_white_noiser\   r5   rA   rB   r'   r(   r`   rH   r8    _initialize_error_transition_var'_initialize_error_transition_individual)r-   r\   r5   rA   rB   r/   s         r.   rN   z*DynamicFactor._initialize_error_transitionN  s   q  99;;;;; lGI .M,LU;()@@$Y%889 :F  "vgDHVU'(DDml:WDDEFF  "vlW&<==DHV ~ ?5577777<<>>>>>r0   c                 V    d| j         d<   t          j        dddddf         | _        d S )Nr   r4   ry   )r&   r'   r(   _idx_error_transitionri   s    r.   r}   z6DynamicFactor._initialize_error_transition_white_noisel  s5    ./*+
 &(U<1ac+A%B"""r0   c                     | j         }| j        }| j        }||z  | j        d<   t          j        d|||z   |||z   f         | _        d S )Nr4   ry   )r\   rA   rB   r&   r'   r(   r   )r-   r\   rA   rB   s       r.   r~   z.DynamicFactor._initialize_error_transition_varu  sf    ,*( /;W.D*+ &(U-'11-,667&8"""r0   c                    | j         | j        }|| j        d<   t          j        t          j                  | j                  }| j        }| j        t          j        fdt          | j                  D                       z   }|dxx         |z  cc<   |dxx         |z  cc<   |
                                }|dxx         |z  cc<   |dxx         | j        z  cc<   |d d t          j        |d         |d         f          f         }|d         |d         f| _        |d d t          j        |d         |d         f          f         }t          j        d|d         |d         f         | _        d S )Nr4   c                     g | ]}|z  S  r   ).0ir\   s     r.   
<listcomp>zIDynamicFactor._initialize_error_transition_individual.<locals>.<listcomp>  s    :::QQ[:::r0   r   r   ry   )r\   rB   r&   r'   tilerp   r7   rA   repeatrangecopylexsort_idx_error_diagr(   r   )r-   rB   rr   	row_shiftcol_incidx_diagr\   s         @r.   r   z5DynamicFactor._initialize_error_transition_individual  s   ,( /;*+ gbog..0@AA&	$ry::::%(8"9"9:::G(E (E EA)A' 88::y t))AAArz8A;*DEEEF (Xa[9
 !!!RZQQ 01112%'U<QQ+G%H"""r0   c                 "     | j         |fd|i|S )Nr!   )_clone_from_init_kwds)r-   rZ   r!   r[   s       r.   clonezDynamicFactor.clone  s"    )t)%EEdEfEEEr0   c                 "    dt           t          fiS )Nfit)DynamicFactorResultsDynamicFactorResultsWrapperri   s    r.   _res_classeszDynamicFactor._res_classes  s    ,.IJKKr0   c                 >   t          j        | j        t           j                  }| j                                        }t          j        t          j        |          d           }||         }| j        dk    r| j	        |         }| j
        dk    rt          || j
                  }t          ||j                  }|                                }|j        j                                        || j        <   |t          j        |j        |j        j                  z
  }| j        dk    rRt          ||          }|                                }|j        j                                        || j        <   |j        }d}| j
        dk    r| j        dk    rt1          |j                  }	|	                    | j        d d	          }
|
j        j                                        || j        <   || j                                     | j
        | j        z  | j
                  j                            | j
        | j
        | j                  j        }t7          dgt9          |           z             }n| j
        dk    r| j        dk    r|j        | j        d          }t;          |j        | j        d
          }t           j                            |                              |          }t7          t           j         d|!                                 f                   }|d d df         || j        <   |s| j"        rtG          d          | j$        dk    r| j%        dk    r2|&                    d          '                                || j(        <   n| j%        dk    r |&                    d          || j(        <   n| j%        dk    rOt          j)        |*                    d                    }|| j+                                                 || j(        <   n| j,        rt1          |          }|                    | j$        d d	          }t          j-        |j        j                                                                      | j.        | j$        z  | j.                  j                            | j.        | j.        | j$                  j        }t7          dgt9          |           z             }|s| j"        rtG          d          t          j-        |j        j                                                  || j/        <   | j%        dk    r5|j0        1                                '                                || j(        <   n| j%        dk    r#|j0        1                                || j(        <   n| j%        dk    r	 t           j        2                    |j0                  }nm# t           j        j3        $ rV t          j4        |j0        j5        d                   |j0        1                                '                                dz  z  }Y nw xY wt          j4        |j0        j5        d                   |j0        1                                '                                dz  z  }|| j+                                                 || j(        <   ng }g }tm          | j.                  D ]}to          |d d |f         | j$        ddfdd          }|                    d          }||j        d | j$                 8                                z  }||j        dd          8                                z  }t           j         |         || j/        <   t           j         |         || j(        <   |S )Ndtyper   )axisr   )ncomp)r!   Tn)maxlagsictrendboth)trimz`Non-stationary starting autoregressive parameters found with `enforce_stationarity` set to True.r   r   r   zfNon-stationary starting error autoregressive parameters found with `enforce_stationarity` set to True.      ?)r   r   r:   burg)method)9r'   rf   rQ   float64rZ   r   anyisnanr;   r!   r5   r   r   factorsr   paramsTravelrR   dotloadingsrS   residr6   r   rU   reshaper   rX   r   linalgpinvr_squeezer:   rD   r7   r9   varmeanrT   diagstdrw   r8   arrayr\   rV   sigma_ur   choleskyLinAlgErrorr`   r?   r   r   tolist)r-   r   rZ   maskr!   res_pcamod_olsres_olsr    mod_factorsres_factorscoefficient_matricesYX	params_ar
cov_factor
mod_errors
res_errorserror_ar_paramserror_cov_paramsr   	mod_error	res_errors                          r.   start_paramszDynamicFactor.start_params  s4   $-rz:::
!!rxQ////d;??9T?D >A%t~666G%11GkkmmG -4N,<,B,B,D,DF4() BF7?G4D4FGGGE ;??%d+++GkkmmG(/(8(>(>(@(@F4$%ME 
>A$"3a"7"7go..K%//$2C03 * 5 5K "$**,, 412
 t56>>NT%66 gdndnd6GHH	 ! 'sT3G2G-H-H'HIIJJ^aD$5$9$9 1 2 23Aw(9GGGA	q))--a00I&ruQ1B1B1D1D0D-D'EFFJ5>qqq!t_F412  	.d7 	. - . . .
 q  "h..161B1B1G1G1I1It-..$
22161B1Bt-..$66WUYYAY%6%677
t89??AA t-.^ 1	EUJ#0@T.1 ( 3 3J
 *,--3355==L4#33T\ gdlDL$2BCCA	 ! 'sT3G2G-H-H'HIIJ I$"; I  "H I I I HZ.0117799 401 "h..&//116688 t-..$
221;1C1L1L1N1Nt-..$66C!#!3!3J4F!G!GJJy, C C C!#
(:(@(C!D!D"*3355::<<cA"CJJJC  VJ$6$<Q$?@@&//116688#=?
 t89??AA t-. !O!4<(( C C!%1+d6F15M(+$H H H	%MMM88	9#34ET5E4E#F#M#M#O#OO I$4RSS$9$@$@$B$BB  46E/4JF401-/U3C-DF4)*s   9$W A'YYc                 l    g } j         | fdt           j                  D             z  }| fdt           j                  D             z  } j        dk    r|dgz  }n[ j        dk    r$|fdt           j                  D             z  }n, j        dk    r!|d t           j                  D             z  }| fd	t           j                  D             z  } j        r%| fd
t           j                  D             z  }n$| fdt           j                  D             z  }|S )Nc                 ^    g | ])}t          j                  D ]}d |dz   |         fz  *S )zloading.f%d.%sr   r   r5   r   r   jendog_namesr-   s      r.   r   z-DynamicFactor.param_names.<locals>.<listcomp>?  s\     
 
 
4>**
 
  ![^44
 
 
 
r0   c                 p    g | ]2}t          j                  D ]}d j        |          d|          3S )zbeta..)r   r;   
exog_namesr   s      r.   r   z-DynamicFactor.param_names.<locals>.<listcomp>G  se     
 
 
4;''
 
  :DOA&99Q99
 
 
 
r0   r   sigma2r   c                 &    g | ]}d |         z  S )z	sigma2.%sr   )r   r   r   s     r.   r   z-DynamicFactor.param_names.<locals>.<listcomp>Q  s2        k!n,  r0   r   c                 R    g | ]$}t          |d z             D ]}d|d z   |d z   fz  %S )r   zcov.chol[%d,%d])r   )r   r   r   s      r.   r   z-DynamicFactor.param_names.<locals>.<listcomp>V  sZ       qs   "QUAEN2   r0   c           	          g | ]A}t          j                  D ]*}t          j                  D ]}d |dz   |dz   |dz   fz  +BS )zL%d.f%d.f%dr   )r   r6   r5   )r   r   r   kr-   s       r.   r   z-DynamicFactor.param_names.<locals>.<listcomp>]  s     
 
 
4,--
 
 4>**	
 
  QqS!A#qsO+
 
 
 
 
r0   c           	          g | ]G}t          j                  D ]0}t          j                  D ]}d |dz   |         |         fz  1HS zL%d.e(%s).e(%s)r   )r   r7   r\   )r   r   r   r   r   r-   s       r.   r   z-DynamicFactor.param_names.<locals>.<listcomp>f  s       t/00  t|,,	   "QqS+a.+a.$II    r0   c                 l    g | ]0}t          j                  D ]}d |dz   |         |         fz  1S r   )r   r7   )r   r   r   r   r-   s      r.   r   z-DynamicFactor.param_names.<locals>.<listcomp>m  sc       t/00   "QqS+a.+a.$II   r0   )r   r   r\   r9   r5   r8   )r-   param_namesr   s   ` @r.   r   zDynamicFactor.param_names9  s   & 	 
 
 
 
 
4<((
 
 
 	
 	 
 
 
 
 
4<((
 
 
 	
 (**H:%KK J..    t|,,   KK  N22  t|,,   K 	 
 
 
 
4>**
 
 
 	
 > 	     t|,,   KK      t|,,   K r0   c                      g } j         | fdt          t          d j                            D             z  } j        dk    r$| fdt           j                  D             z  } j        r|dgz  }|S )Nc                 n    g | ]1}t          j                  D ]}|d k    rd|dz   z  n	d|dz   |fz  2S )r   zf%dr   zf%d.L%dr   )r   r   r   r-   s      r.   r   z-DynamicFactor.state_names.<locals>.<listcomp>{  sk     , , ,4>**, ,  #$q&&eq1uooyAE1:/E, , , ,r0   r   r   c                 z    g | ]7}t          j                  D ] }|d k    rd|         z  nd|         |fz  !8S )r   ze(%s)z	e(%s).L%d)r   r\   r   s      r.   r   z-DynamicFactor.state_names.<locals>.<listcomp>  sq     . . . t|,,	. .  01Avv'KN**"k!na%88. . . .r0   dummy)r   r   r@   r6   r7   rC   )r-   namesr   s   ` @r.   state_nameszDynamicFactor.state_namesu  s    & 	 , , , ,3q$"34455, , , 	,
 a . . . . . t/00. . . .E  	gYEr0   c                    t          j        |d          }|j        }t          j        |j        |          }|| j                 || j        <   || j                 || j        <   | j        dv r|| j                 dz  || j        <   n | j        dk    r|| j                 || j        <   | j	        r| j
        dk    r~|| j                                     | j        | j                  }| j        dd	| j        d	| j        f         j        }t#          ||          \  }}|                                || j        <   n|| j                 || j        <   | j	        r| j        dk    r| j        r|| j                                     | j        | j                  }| j        }| j        | j        z   }	| j        d||	||	f         j        }t#          ||          \  }}|                                || j        <   n|| j                                                 }
t3          | j                  D ]5}|| j        z  }|dz   | j        z  }	t5          |
||	                   |
||	<   6|
|| j        <   n|| j                 || j        <   |S )
a[  
        Transform unconstrained parameters used by the optimizer to constrained
        parameters used in likelihood evaluation

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

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

        Notes
        -----
        Constrains the factor transition to be stationary and variances to be
        positive.
        r   ndminr   r   r   r   r   r   rn   N)r'   r   r   rf   r?   rR   rS   r9   rT   r:   r6   rU   r   r5   rA   rH   realr   r   r7   r8   rV   r\   rB   r   r   r	   )r-   unconstrainedr   constrainedunconstrained_matricescovr   variancerb   rc   coefficientsr   s               r.   transform_paramszDynamicFactor.transform_params  s   , a888#h}2%@@@
 $/0 	D)* $+, 	D%&
 "888d45q8 .//  N22d45 ./
 $ 	?):Q)>)> d<=EEND$68 8 # (;$.HINC12H#NN + ( %**,, 677 d<= 67
 $ 	>)9A)=)= ~ J!$"?@HHd&79 9 ' nt|3h{E#IuSy@AF5.5 5 /$h )..00 D9::
 "$"?@EEGG t|,, 1 1A 00Eq5D$44C.M$U3Y//1 /1Ls++=ID9:: d;< 56 r0   c                    t          j        |d          }|j        }t          j        |j        |          }|| j                 || j        <   || j                 || j        <   | j        dv r|| j                 dz  || j        <   n | j        dk    r|| j                 || j        <   | j	        r| j
        dk    r~|| j                                     | j        | j                  }| j        dd	| j        d	| j        f         j        }t#          ||          \  }}|                                || j        <   n|| j                 || j        <   | j	        r| j        dk    r| j        r|| j                                     | j        | j                  }| j        }| j        | j        z   }	| j        d||	||	f         j        }t#          ||          \  }}|                                || j        <   n|| j                                                 }
t3          | j                  D ]5}|| j        z  }|dz   | j        z  }	t5          |
||	                   |
||	<   6|
|| j        <   n|| j                 || j        <   |S )
a  
        Transform constrained parameters used in likelihood evaluation
        to unconstrained parameters used by the optimizer.

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

        Returns
        -------
        unconstrained : array_like
            Array of unconstrained parameters used by the optimizer.
        r   r   r   r   r   r   r   rn   N)r'   r   r   rf   r?   rR   rS   r9   rT   r:   r6   rU   r   r5   rA   rH   r   r   r   r7   r8   rV   r\   rB   r   r   r
   )r-   r   r   r   constrained_matricesr   r   r   rb   rc   r   r   s               r.   untransform_paramsz DynamicFactor.untransform_params  s     h{!444!!2%@@@
 -. 	d+, )* 	d'(
 "888D23S8 $011  N22D23 $01
 $ 	=):Q)>)> D:;CCND$68 8 ! (;$.HINC3(#/ / + ( %**,, $899 D:; $89
 $ 	<)9A)=)= ~ L =>FFd&79 9 % nt|3h{E#IuSy@AF7,c3 3 /$h )..00 d;<<
   =>CCEE t|,, 6 6A 00Eq5D$44C9(s35 5 !s++ @Ld;<< D9: $78 r0   c                    t                                          |           t          j        t	          | j                                                            d d         }d t          j        | j        |          D             \  }}}}}| j	        ro| j
        dk    rd| j        dk    s| j
        dk    rN|                    |          }t          |                    |                    dk    }|r|st          d          | j	        rk| j        dk    rb| j        s| j        dk    rT|                    |          }t          |                    |                    dk    }|r|st          d          d S d S d S d S d S )Nr   c              3   >   K   | ]}|                                 V  d S r%   )r   )r   arrs     r.   	<genexpr>z9DynamicFactor._validate_can_fix_params.<locals>.<genexpr>O  sK       FJ FJ CJJLLFJ FJ FJ FJ FJ FJr0   r   r   zCannot fix individual factor transition parameters when `enforce_stationarity=True`. In this case, must either fix all factor transition parameters or none.zCannot fix individual error transition parameters when `enforce_stationarity=True`. In this case, must either fix all error transition parameters or none.)rF   _validate_can_fix_paramsr'   cumsumrX   r&   rP   array_splitr   r:   r6   r5   
issupersetlenintersectionrD   r7   r8   )	r-   r   ix_factor_transition_nameserror_transition_namesfix_allfix_anyr]   s	           r.   r   z&DynamicFactor._validate_can_fix_paramsK  s   ((555YtDO22445566ss;FJ FJ$&N43CR$H$HFJ FJ FJBAq)+A $ 
	"):Q)>)>~!!T%6%:%:%001HII001HIIJJQN  "7 "$!" " "
 $ 
	")9A)=)=~ 	"!1A!5!5%001GHH001GHHIIAM  "7 "$!" " "
	" 
	")=)=
" "	 "6!5" "r0   c                    |                      |||          }|| j                                     | j        | j                  | j        | j        <   | j        dk    r\|| j                                     | j        | j                  j	        }t          j        | j        |          j	        | j        | j        <   | j        dv r|| j                 | j        | j        <   nn| j        dk    rct          j        | j        | j        f|j                  }|| j                 || j        <   t          j        ||j	                  | j        | j        <   || j                                     | j        | j        | j        z            | j        | j        <   | j        r:|| j                                     | j        | j                  | j        | j        <   dS || j                 | j        | j        <   dS )a2  
        Update the parameters of the model

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

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

        Returns
        -------
        params : array_like
            Array of parameters.

        Notes
        -----
        Let `n = k_endog`, `m = k_factors`, and `p = factor_order`. Then the
        `params` vector has length
        :math:`[n 	imes m] + [n] + [m^2 	imes p]`.
        It is expanded in the following way:

        - The first :math:`n 	imes m` parameters fill out the factor loading
          matrix, starting from the [0,0] entry and then proceeding along rows.
          These parameters are not modified in `transform_params`.
        - The next :math:`n` parameters provide variances for the error_cov
          errors in the observation equation. They fill in the diagonal of the
          observation covariance matrix, and are constrained to be positive by
          `transofrm_params`.
        - The next :math:`m^2 	imes p` parameters are used to create the `p`
          coefficient matrices for the vector autoregression describing the
          factor transition. They are transformed in `transform_params` to
          enforce stationarity of the VAR(p). They are placed so as to make
          the transition matrix a companion matrix for the VAR. In particular,
          we assume that the first :math:`m^2` parameters fill the first
          coefficient matrix (starting at [0,0] and filling along rows), the
          second :math:`m^2` parameters fill the second matrix, etc.
        )transformedincludes_fixedr   r   r   r   N)handle_paramsrR   r   r\   r5   rH   ra   r;   rS   r   r'   r   r!   rh   r9   rT   rq   rf   r   rw   rU   r6   r{   r8   rV   rB   r   )r-   r   r  r  complex_stepexog_paramserror_cov_lowers          r.   updatezDynamicFactor.updatei  s   X ##F3A $ C C 4()11$,OO 	#$
 ;?? !23;;dk+ ++, ')vdi'E'E'GDHT^$ "888t-. HT()) N22 hdl'C-3\; ; ;O t-. D56 (9:: HT()
 412:: 1DN BD D 	,-
 > 	7t45==L$"35 5 HT/000
 t45 HT/000r0   )Nr   Fr   T)Fr%   )TFF)__name__
__module____qualname____doc__rG   rJ   rK   rL   rk   rl   rM   rN   r}   r~   r   r   propertyr   r   r   r   r   r   r   r  __classcell__r]   s   @r.   r   r      s       n n` =A@J&*fJ fJ fJ fJ fJ fJPA A A
B 
B 
B6 6 6. . . ."9 9 9"N N N.? ? ?<C C C8 8 8$&I &I &IPF F F F L L XL @ @ X@D 9 9 X9v   X,a a aFZ Z Zx" " " " "< ?D!S7 S7 S7 S7 S7 S7 S7 S7r0   r   c                        e Zd ZdZd
 fd	Zed             Zed             Z	 	 ddZ	 e
ej        j                  d fd		            Z xZS )r   am  
    Class to hold results from fitting an DynamicFactor model.

    Parameters
    ----------
    model : DynamicFactor instance
        The fitted model instance

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

    See Also
    --------
    statsmodels.tsa.statespace.kalman_filter.FilterResults
    statsmodels.tsa.statespace.mlemodel.MLEResults
    Nc                 >    t                      j        ||||fi | t          j        | _        t          di | j        j        | j        j        | j        j	        | j        j
        | j        j        | j        j        | j        j        | j        j        d| _        d | _        | j        j
        dk    r~t          j        | j        | j        j                           }| j        j	        }| j        j
        }|                    ||z  |          j                            |||          j        | _        d | _        | j        j        dk    rt          j        | j        | j        j                           }| j        j        }	| j        j        }
| j        j        r?|                    |	|
z  |	          j                            |	|	|
          j        | _        d S t          j        |	|	|
z  f          }||| j        j        <   |j                            |
|	|	          | _        d S d S )N)r\   r:   r5   r6   r7   r8   r9   r;   r   r   )rF   rG   r'   infdf_residr   modelr\   r:   r5   r6   r7   r8   r9   r;   specificationcoefficient_matrices_varr   r   rU   r   r   coefficient_matrices_errorrV   rf   r   )r-   r  r   filter_resultscov_typer[   	ar_paramsr5   r6   r\   r7   matr]   s               r.   rG   zDynamicFactorResults.__init__  s
   KKFKKK"  z)$(J$C - J3  :1-"j7 j'&
 &
  & )-%:"Q&&TZ%IJKK 
,I:2L!!)l":IFFHgiL99! ) +/':!A%%TZ%HIJJ j(G*0Kz# B%%g&;WEEG''7K88 /// h;)>?@@2;DJ./EMM+w@@ /// &%r0   c                 &   d}| j         }|j        dk    r|d}|j        }| j        }t          |j        ||         |j        ||||f         dd|          }| j        | j        ||         |_        | j        | j        ||||f         |_	        |S )a  
        Estimates of unobserved factors

        Returns
        -------
        out : Bunch
            Has the following attributes shown in Notes.

        Notes
        -----
        The output is a bunch of the following format:

        - `filtered`: a time series array with the filtered estimate of
          the component
        - `filtered_cov`: a time series array with the filtered estimate of
          the variance/covariance of the component
        - `smoothed`: a time series array with the smoothed estimate of
          the component
        - `smoothed_cov`: a time series array with the smoothed estimate of
          the variance/covariance of the component
        - `offset`: an integer giving the offset in the state vector where
          this component begins
        Nr   )filteredfiltered_covsmoothedsmoothed_covr*   )
r  r5   r!  r   filtered_statefiltered_state_covsmoothed_stater(  smoothed_state_covr)  )r-   outspecr*   rc   ress         r.   r   zDynamicFactorResults.factors	  s    4 !>AF.C%C+F3J7 3F3Js
4JKD	  C
 ".#26#:>&2+F3Js
,BC  
r0   c                    ddl m} | j        }t          j        |j        |j        f          }| j        dnd}t          |j                  D ]s} || j	        |         |                   }t          |j                  D ]@}| j
        j        |         }t          ||                                          j        |||f<   At|S )aH  
        Coefficients of determination (:math:`R^2`) from regressions of
        individual estimated factors on endogenous variables.

        Returns
        -------
        coefficients_of_determination : ndarray
            A `k_endog` x `k_factors` array, where
            `coefficients_of_determination[i, j]` represents the :math:`R^2`
            value from a regression of factor `j` and a constant on endogenous
            variable `i`.

        Notes
        -----
        Although it can be difficult to interpret the estimated factor loadings
        and factors, it is often helpful to use the coefficients of
        determination from univariate regressions to assess the importance of
        each factor in explaining the variation in each endogenous variable.

        In models with many variables and factors, this can sometimes lend
        interpretation to the factors (for example sometimes one factor will
        load primarily on real variables and another on nominal variables).

        See Also
        --------
        plot_coefficients_of_determination
        r   )add_constantNr&  r(  )statsmodels.toolsr2  r  r'   rf   r\   r5   r,  r   r   r!  rZ   r   r   rsquared)	r-   r2  r/  r   whichr   r!   r   rZ   s	            r.   coefficients_of_determinationz2DynamicFactorResults.coefficients_of_determination5  s    : 	322222!xt~ >??"19

zt~&& 	E 	EA<U 3A 677D4<(( E E+1!4%(%5%5%9%9%;%;%DQT""E r0   c                    ddl m}m}  |              |||          }| j        }||j        dk    }| j        }d}t          j        |j                  }	|j        D ]}
|	                    |j
        d|          }|                    d           |                    d|z  d	           |                    |	|
          }|r_|d                                         }|j                            |	|d
z  z              |j                            | j        j                   n0|                    d           |j                            g            |dz  }|S )a5  
        Plot the coefficients of determination

        Parameters
        ----------
        endog_labels : bool, optional
            Whether or not to label the endogenous variables along the x-axis
            of the plots. Default is to include labels if there are 5 or fewer
            endogenous variables.
        fig : Figure, optional
            If given, subplots are created in this figure instead of in a new
            figure. Note that the 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).

        Notes
        -----

        Produces a `k_factors` x 1 plot grid. The `i`th plot shows a bar plot
        of the coefficients of determination associated with factor `i`. The
        endogenous variables are arranged along the x-axis according to their
        position in the `endog` array.

        See Also
        --------
        coefficients_of_determination
        r   )_import_mplcreate_mpl_figN   r   )r   r   z	Factor %iz$R^2$)titleylabelr   zEndogenous variables)xlabel)statsmodels.graphics.utilsr8  r9  r  r\   r6  r'   aranger   add_subplotr5   set_ylimsetbar	get_widthxaxis	set_ticksset_ticklabelsr  r   )r-   endog_labelsfigfigsizer8  r9  r/  r6  plot_idx	locationscoeffsaxbarswidths                 r.   "plot_coefficients_of_determinationz7DynamicFactorResults.plot_coefficients_of_determination_  st   > 	KJJJJJJJnS'**! <1,L )-(J%Idl++	35 	 	FH==BKKFFx/FAAA66)V,,D 'Q))++""9uqy#8999''
(>????4555""2&&&MHH
r0   皙?Tc           	      ~	  & ddl m& | j        }g }|j        dk    re|j        dk    rd|j        |j        fz  }n
d|j        z  }|                    |           |j        dk    r|                    d|j        z             n|                    d|j        z             |j        dk    r*|j        rdnd}|                    d	||j        fz             t                      
                    ||| 
          }|rt          j        t          | j                            }	d&fd	}
| j        j        }| j        j        }| j        j        }| j        j        }| j        j        }| j        j        }|	| j        j                 }g }|	| j        j                 }g }t+          |          D ]}|||z  |dz   |z           }|                    |           |||z  |dz   |z           }|                    |           t          j        ||g          }d| j        j        |         z  } |
| ||          }|j                            |           |	| j        j                 }g }|dk    rht+          |          D ]X}||z  }||||z            }|                    |           d|dz   z  } |
| ||          }|j                            |           Yg }|j        dk    r|	| j        j                 }t+          |          D ]}|j        r||z  }|dz   |z  } n||j        z  }|dz   |j        z  } |||          }!|                    |!           d| j        j        |         z  } |
| |!|          }|j                            |           |	| j        j                 }" |
| |"dd          }|j                            |           g }#|||||"gfD ]P}$t          j        |$                                          }$t          |$          dk    r|#                    |$           Qt          j        |#          }#t          j        t=          t?          |	                               t?          |#                                        }%t          |%          dk    r) |
| |%dd          }|j                            |           |S )Nr   )summary_paramsz#DynamicFactor(factors=%d, order=%d)zStaticFactor(factors=%d)z%d regressorszSUR(%d regressors)r   ARz%s(%d) errors)alpharb   
model_namedisplay_paramsTc                 F   | | j         |         | j        |         | j        |         | j        |         |                               |         f}fdt          j        | j        j                  |         	                                D             } |d |d|          S )Nc                 x    g | ]6}r0d                      |                    d           dd                   n|7S )r   Nr   )joinsplit)r   name	strip_ends     r.   r   zDDynamicFactorResults.summary.<locals>.make_table.<locals>.<listcomp>  sS        7@ICHHTZZ__SbS1222T  r0   F)ynamexnamerV  use_tr;  )
r   bsezvaluespvaluesconf_intr'   r   datar   r   )r-   r   r;  r^  r0  r   rV  rT  s      `  r.   
make_tablez0DynamicFactorResults.summary.<locals>.make_table  s    T[.|D)4<+=}}U++D13    HTY233D9@@BB   &~c[,1eM M M Mr0   r   zResults for equation %szResults for factor equation f%dz Results for error equation e(%s)zError covariance matrixF)r^  zOther parameters)T)!statsmodels.iolib.summaryrT  r  r5   r6   appendr;   r7   r8   rF   summaryr'   r?  r  r   r  r\   rA   rB   rR   rS   r   concatenater   tablesrU   rV   rT   r   flattenrX   rB  
difference)(r-   rV  rb   separate_paramsr/  rW  
model_type
error_typerj  indicesrg  r\   r;   r5   r6   rA   rB   loading_indicesloading_masksexog_indices
exog_masksr   loading_mask	exog_maskr   r;  tablefactor_indicesfactor_masksfactor_maskerror_maskserror_indicesrc   
error_maskerror_cov_maskmasksminverse_maskrT  r]   s(    `                                    @r.   rj  zDynamicFactorResults.summary  so   <<<<<<! 
>A 1$$C#~t/@AB

 8$.H
j))){Q!!/DK"?@@@2T[@AAAa"&.:dJoT=M0NNOOO''//u.. " 
 

  g	-iDK 0 011GM M M M M M M j(GZ&F
,I:2L J4M:2L &dj&ABOM"4:#:;LJ7^^ - - $A	M1q5I2E$EF $$\222
 )VQUf4D)DE	!!),,, ~|Y&?@@1DJ4J14MM"
4u55%%e,,,, %TZ%IJNLay)) 1 1A-E"08M1M"NK ''444 >1EE&Jt[%@@EN))%0000 K!## '
(K Lw 1 1A~ 9 !L 0 1u4 !D$4 4 1u(88!.uSy!9J&&z222 @!Z3A67E&JtZ??EN))%0000 %TZ%ABNJt^8EK K KEN!!%((( E#Z!N#35 $ $HQKK''))q66A::LLOOON5))E8DW)@)@U)L)L$M$MNNL<  1$$"
47I-24 4 4%%e,,,r0   r%   )NNN)rR  NT)r  r  r  r  rG   r  r   r   r6  rQ  r   r   rj  r  r  s   @r.   r   r     s         ,1B 1B 1B 1B 1B 1Bf ) ) X)V ' ' ^'R ?C=A> > > >@ Xj ())E E E E E *)E E E E Er0   r   c                   n    e Zd Zi Z ej        ej        e          Zi Z ej        ej	        e          Z	dS )r   N)
r  r  r  _attrswrapunion_dictsr   _wrap_attrs_methods_wrap_methodsr   r0   r.   r   r   (  sR        F"$"#4#@#)+ +KH$D$%6%D%-/ /MMMr0   r   )(r  numpyr'   mlemodelr   r   r   toolsr   r   r	   r
   r   r   statsmodels.multivariate.pcar   #statsmodels.regression.linear_modelr   #statsmodels.tsa.vector_ar.var_modelr   statsmodels.tsa.arima.modelr   statsmodels.tools.toolsr   statsmodels.tools.datar   statsmodels.tsa.tsatoolsr   statsmodels.tools.decoratorsr   statsmodels.base.wrapperbasewrapperr  statsmodels.compat.pandasr   r   r   r   populate_wrapperr   r0   r.   <module>r     s        = = = = = = = = = =               
 - , , , , , 3 3 3 3 3 3 3 3 3 3 3 3 - - - - - - ) ) ) ) ) ) 3 3 3 3 3 3 + + + + + + 7 7 7 7 7 7 ' ' ' ' ' ' ' ' ' . . . . . .a7 a7 a7 a7 a7H a7 a7 a7Hf f f f f: f f fR/ / / / /"3 / / /  1*, , , , ,r0   