
p7]c           @` s  d  Z  d d l m Z m Z m Z d d l m Z d d l m 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& j' j( Z) d e f d     YZ* d e f d     YZ+ d e f d     YZ, e) j- e, e+  d S(   sC   
Dynamic factor model

Author: Chad Fulton
License: Simplified-BSD
i    (   t   divisiont   absolute_importt   print_function(   t   warn(   t   OrderedDictNi   (   t   MLEModelt
   MLEResultst   MLEResultsWrapper(   t   is_invertiblet   prepare_exogt   constrain_stationary_univariatet!   unconstrain_stationary_univariatet!   constrain_stationary_multivariatet#   unconstrain_stationary_multivariate(   t   PCA(   t   OLS(   t   VAR(   t   Bunch(   t   _is_using_pandas(   t   lagmat(   t   cache_readonly(   t   ValueWarningt   DynamicFactorc           B` s   e  Z d  Z d d e d e d  Z d   Z d   Z d   Z	 e d  Z
 d   Z d	   Z d
   Z d   Z d   Z d   Z e d    Z e d    Z e d    Z d   Z d   Z e e d  Z RS(   s  
    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 : boolean, 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 : boolean, 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 : boolean
        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 : boolean, 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_1 f_{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.

    i    t   diagonalc	      	   ` s  |   _  |   _ |   _ |   _ | o3 | d k   _ |   _ t |  \   _ }   j d k   _ t	 | d   s t j | d d } n  | j d k r | j d n d }
 t d   j    j   _   j |
   _   j }   j }   j d k r|   j 7} | |
 7} n  | d k r4d } d } n  |
 d k  rOt d   n    j |
 k rmt d   n    j d k rt d   n  |	 j d d  t t    j | d | d | d | |	   j d k rt   j _ n  t     _   j     j     j     j     j    t!   j j"      _#   f d   } d } | d |  \   _$ } | d |  \   _% } | d |  \   _& } | d |  \   _' } | d |  \   _( } d  S(   Ni    t   ordert   Ci   i   sE   The dynamic factors model is only valid for multivariate time series.sG   Number of factors must be less than the number of endogenous variables.t   scalarR   t   unstructureds3   Invalid error covariance matrix type specification.t   initializationt
   stationaryt   exogt   k_statest   k_posdefc         ` s5     j  |  } t j | | | !} | | 7} | | f S(   N(   t
   parameterst   npt   s_(   t   keyt   offsett   lengtht   param_slice(   t   self(    sH   lib/python2.7/site-packages/statsmodels/tsa/statespace/dynamic_factor.pyt   _slice   s    
t   factor_loadingst	   error_covt   factor_transitiont   error_transition(   R   R   R   ()   t   enforce_stationarityt	   k_factorst   factor_ordert   error_ordert	   error_vart   error_cov_typeR	   t   k_exogt   mle_regressionR   t   NoneR"   t
   asanyarrayt   ndimt   shapet   maxt   _factor_ordert   _error_ordert
   ValueErrort
   setdefaultt   superR   t   __init__t   Falset   ssmt   _time_invariantR   R!   t   _initialize_loadingst   _initialize_exogt   _initialize_error_covt   _initialize_factor_transitiont   _initialize_error_transitiont   sumt   valuest   k_paramst   _params_loadingst   _params_exogt   _params_error_covt   _params_factor_transitiont   _params_error_transition(   R(   t   endogR/   R0   R   R1   R2   R3   R.   t   kwargst   k_endogR   R    R)   R%   (    (   R(   sH   lib/python2.7/site-packages/statsmodels/tsa/statespace/dynamic_factor.pyR@      s\    					"			




c         C` s   |  j  |  j |  j d <|  j d k rp |  j } |  j |  j  } t j |  j   |  j d d  d   | |  f <n  t j d d  d   d  |  j  f |  _	 d  S(   NR*   i    t   design(
   RS   R/   R!   R1   R;   R"   t   eyeRB   R#   t   _idx_loadings(   R(   t   startt   end(    (    sH   lib/python2.7/site-packages/statsmodels/tsa/statespace/dynamic_factor.pyRD      s    	1c         C` sw   |  j  |  j |  j d <|  j  d k rK t j |  j |  j f  |  j d <n  t j d d  |  j  d  d   f |  _ d  S(   NR   i    t   obs_intercept(	   R4   RS   R!   R"   t   zerost   nobsRB   R#   t	   _idx_exog(   R(   (    (    sH   lib/python2.7/site-packages/statsmodels/tsa/statespace/dynamic_factor.pyRE      s    %c         C` sd   |  j  d k r" |  j d t  n> |  j  d k rD |  j d t  n |  j  d k r` |  j   n  d  S(   NR   R   R   (   R3   t   _initialize_error_cov_diagonalt   TrueRA   t"   _initialize_error_cov_unstructured(   R(   (    (    sH   lib/python2.7/site-packages/statsmodels/tsa/statespace/dynamic_factor.pyRF   	  s    c         C` s   | r d n |  j  |  j d <|  j  } |  j } t j |  } |  j d k rq d } | d | | d | f } n d } | f | |  _ d  S(   Ni   R+   i    t	   state_covt   obs_cov(   RS   R!   R/   R"   t   diag_indicesR1   t   _idx_error_cov(   R(   R   RS   R/   t   idxt   matrix(    (    sH   lib/python2.7/site-packages/statsmodels/tsa/statespace/dynamic_factor.pyR]     s    		c         C` s   |  j  } t | | d d  |  j d <t j |  j   |  _ |  j d k r |  j } |  j |  j  } t j d | |  | |  f |  _	 n% t j d d  d   d  d   f |  _	 d  S(   Ni   i   R+   i    R`   Ra   (
   RS   t   intR!   R"   t   tril_indicest   _idx_lower_error_covR1   R/   R#   Rc   (   R(   RS   RW   RX   (    (    sH   lib/python2.7/site-packages/statsmodels/tsa/statespace/dynamic_factor.pyR_   "  s    		(c         C` s
  |  j  |  j } |  j } |  j  |  j d |  j d <|  j d k r |  j  d k r t j | |  |  j d | |  d  | |  f <n  t j |  |  j d d  |  d  |  f <t j |  |  j d d  |  d  |  f <n  t j d d  |  d  |  f |  _ d  S(   Ni   R,   i    t
   transitiont	   selectionR`   (   R0   R/   R!   R"   RU   RB   R#   t   _idx_factor_transition(   R(   R   R/   (    (    sH   lib/python2.7/site-packages/statsmodels/tsa/statespace/dynamic_factor.pyRG   3  s    	6+.c         C` s   |  j  d k r |  j   n |  j } |  j } |  j } |  j } t j d | | |  | | |  f } t j |  |  j	 | <t j d | | | |  | | | |  f } t j | |  |  j	 | <|  j
 r |  j   n
 |  j   d  S(   Ni    Rj   Ri   (   R1   t(   _initialize_error_transition_white_noiseRS   R/   R;   R<   R"   R#   RU   RB   R2   t    _initialize_error_transition_vart'   _initialize_error_transition_individual(   R(   RS   R/   R;   R<   R)   (    (    sH   lib/python2.7/site-packages/statsmodels/tsa/statespace/dynamic_factor.pyRH   J  s$    						c         C` s6   d |  j  d <t j d d d  d d  f |  _ d  S(   Ni    R-   Ri   (   R!   R"   R#   t   _idx_error_transition(   R(   (    (    sH   lib/python2.7/site-packages/statsmodels/tsa/statespace/dynamic_factor.pyRl   h  s    c         C` s]   |  j  } |  j } |  j } | | |  j d <t j d | | |  | | |  f |  _ d  S(   NR-   Ri   (   RS   R;   R<   R!   R"   R#   Ro   (   R(   RS   R;   R<   (    (    sH   lib/python2.7/site-packages/statsmodels/tsa/statespace/dynamic_factor.pyRm   q  s    			c         C` sd  |  j  } |  j } | |  j d <t j t j |  |  j  } |  j } |  j t j g  t	 |  j  D] } | | ^ qb |  } | d c | 7<| d c | 7<| j
   } | d c | 8<| d c |  j 8<| 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(   NR-   i    i   Ri   (   RS   R<   R!   R"   t   tileRb   R1   R;   t   repeatt   ranget   copyt   lexsortt   _idx_error_diagR#   Ro   (   R(   RS   R<   Rd   t	   row_shiftt   it   col_inct   idx_diag(    (    sH   lib/python2.7/site-packages/statsmodels/tsa/statespace/dynamic_factor.pyRn     s     			---c         C` s   i t  t f d 6S(   Nt   fit(   t   DynamicFactorResultst   DynamicFactorResultsWrapper(   R(   (    (    sH   lib/python2.7/site-packages/statsmodels/tsa/statespace/dynamic_factor.pyt   _res_classes  s    c         C` s[  t  j |  j d t  j } |  j j   } |  j d k r t | d |  j } t | | j	  } | j
   } | j j j   | |  j <| t  j | j	 | j j  } n  |  j d k r t | d |  j } | j
   } | j j j   | |  j <| j } n  t } |  j d k r|  j d k rt | j	  } | j
 d |  j d d  d d	  } | j j j   | |  j <| |  j j |  j |  j |  j  j j |  j |  j |  j  j }	 t d g t |	   } n |  j d k rn|  j d k rn| j	 |  j }
 t | j	 |  j d
 d } t  j j |  j |
  } t t  j  d | j!   f  } | d  d   d f | |  j <n  | r|  j" rt# d   n  |  j$ d k rB|  j% d k r| j& d d  j'   | |  j( <qW|  j% d k r| j& d d  | |  j( <qW|  j% d k rWt  j) | j* d d   } | |  j+ j   | |  j( <qWnt |  } | j
 d |  j$ d d  d d	  } t  j, | j j  j   j |  j- |  j$ |  j-  j j |  j- |  j- |  j$  j }	 t d g t |	   } | r|  j" rt# d   n  |  j. r(t  j, | j j  j   | |  j/ <n | j j |  j0 | |  j/ <|  j% d k rp| j1 j2   j'   | |  j( <n |  j% d k r| j1 j2   | |  j( <n |  j% d k rWy t  j j3 | j1  } WnG t  j j4 k
 r	t  j5 | j1 j6 d  | j1 j2   j'   d } n Xt  j5 | j1 j6 d  | j1 j2   j'   d } | |  j+ j   | |  j( <n  | S(   Nt   dtypei    t   ncompR   i   t   maxlagst   ict   trendt   nct   trimt   boths`   Non-stationary starting autoregressive parameters found with `enforce_stationarity` set to True.R   t   axisR   R   sf   Non-stationary starting error autoregressive parameters found with `enforce_stationarity` set to True.g      ?(7   R"   RZ   RK   t   float64RQ   Rs   R/   R   R   t   factorsRz   t   paramst   Tt   ravelRL   t   dott   loadingsR4   R   RM   t   residR^   R0   R   R6   RO   t   reshapeR   t   listR   t   linalgt   pinvt   r_t   squeezeR.   R=   R1   R3   t   vart   meanRN   t   diagt   stdRh   t   arrayRS   R2   RP   Ru   t   sigma_uR   t   choleskyt   LinAlgErrorRU   R9   (   R(   R   RQ   t   res_pcat   mod_olst   res_olsR   t   mod_factorst   res_factorst   coefficient_matricest   Yt   Xt	   params_art
   cov_factort
   mod_errorst
   res_errors(    (    sH   lib/python2.7/site-packages/statsmodels/tsa/statespace/dynamic_factor.pyt   start_params  s    "	  " 		%c      
   C` s  g  } |  j  } | g  t |  j  D]4 } t |  j  D] } d | d | | f ^ q5 q" 7} | g  t |  j  D]7 } t |  j  D]! } d |  j | | | f ^ q qp 7} |  j d k r | d g 7} n |  j d k r| g  t |  j  D] } d | | ^ q 7} n{ |  j d k r| g  t |  j  D]O } t | d  D]8 } | | k rcd	 | | n d
 | | | | f ^ qCq/7} n  | g  t |  j  D]Q } t |  j  D]; } t |  j  D]% } d | d | d | d f ^ qqq7} |  j rj| g  t |  j  D]Q } t |  j	  D]; } t |  j  D]% } d | d | | | | f ^ q5q"q7} nU | g  t |  j  D]; } t |  j	  D]% } d | d | | | | f ^ qq}7} | S(   Ns   loading.f%d.%si   s
   beta.%s.%sR   t   sigma2R   s	   sigma2.%sR   s   sqrt.var.%ss   sqrt.cov.%s.%ss   L%d.f%d.f%ds   L%d.e(%s).e(%s)(
   t   endog_namesRr   RS   R/   R4   t
   exog_namesR3   R0   R2   R1   (   R(   t   param_namesR   Rw   t   jt   k(    (    sH   lib/python2.7/site-packages/statsmodels/tsa/statespace/dynamic_factor.pyR   (  sJ    	58+S?	B<c         C` s  t  j | d d } | j } t  j | j d | } | |  j | |  j <| |  j | |  j <|  j d k r | |  j d | |  j <n& |  j d k r | |  j | |  j <n  |  j	 r;|  j
 d k r;| |  j j |  j |  j  } |  j d	 d
 |  j  d
 |  j  f j } t | |  \ } } | j   | |  j <n | |  j | |  j <|  j	 rm|  j d k rm|  j r| |  j j |  j |  j  } |  j } |  j |  j }	 |  j d	 | |	  | |	  f j } t | |  \ } } | j   | |  j <q| |  j j   }
 xO t |  j  D]> } | |  j } | d |  j }	 t |
 | |	 ! |
 | |	 +qW|
 | |  j <n | |  j | |  j <| S(   sZ  
        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
            evalation.

        Notes
        -----
        Constrains the factor transition to be stationary and variances to be
        positive.
        t   ndmini   R~   R   R   i   R   i    R`   N(   R   R   (   R"   R   R~   RZ   R9   RL   RM   R3   RN   R.   R0   RO   R   R/   R;   RB   t   realR   R   R1   R2   RP   RS   R<   Rs   Rr   R
   (   R(   t   unconstrainedR~   t   constrainedt   unconstrained_matricest   covR   t   varianceRW   RX   t   coefficientsRw   (    (    sH   lib/python2.7/site-packages/statsmodels/tsa/statespace/dynamic_factor.pyt   transform_paramse  sF    	+		%c         C` s  t  j | d d } | j } t  j | j d | } | |  j | |  j <| |  j | |  j <|  j d k r | |  j d | |  j <n& |  j d k r | |  j | |  j <n  |  j	 r;|  j
 d k r;| |  j j |  j |  j  } |  j d	 d
 |  j  d
 |  j  f j } t | |  \ } } | j   | |  j <n | |  j | |  j <|  j	 rm|  j d k rm|  j r| |  j j |  j |  j  } |  j } |  j |  j }	 |  j d	 | |	  | |	  f j } t | |  \ } } | j   | |  j <q| |  j j   }
 xO t |  j  D]> } | |  j } | d |  j }	 t |
 | |	 ! |
 | |	 +qW|
 | |  j <n | |  j | |  j <| S(   s  
        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 evalution, to be
            transformed.

        Returns
        -------
        unconstrained : array_like
            Array of unconstrained parameters used by the optimizer.
        R   i   R~   R   R   g      ?R   i    R`   N(   R   R   (   R"   R   R~   RZ   R9   RL   RM   R3   RN   R.   R0   RO   R   R/   R;   RB   R   R   R   R1   R2   RP   RS   R<   Rs   Rr   R   (   R(   R   R~   R   t   constrained_matricesR   R   R   RW   RX   R   Rw   (    (    sH   lib/python2.7/site-packages/statsmodels/tsa/statespace/dynamic_factor.pyt   untransform_params  sH    	+		%c         C` s  t  t |   j | d | d | } | |  j j |  j |  j  |  j |  j <|  j	 d k r | |  j
 j |  j |  j	  j } t j |  j |  j |  j |  j <n  |  j d	 k r | |  j |  j |  j <ni |  j d k r5t j |  j |  j f d | j } | |  j | |  j <t j | | j  |  j |  j <n  | |  j j |  j |  j |  j  |  j |  j <|  j r| |  j j |  j |  j  |  j |  j <n | |  j |  j |  j <d S(
   s6  
        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 : boolean, 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.

        t   transformedt   complex_stepi    R   R   R   R~   N(   R   R   (   R?   R   t   updateRL   R   RS   R/   RB   RV   R4   RM   R   R"   R   R   R\   R3   RN   Rc   RZ   R~   Rh   RO   R0   Rk   R2   RP   R<   Ro   (   R(   R   R   R   t   exog_paramst   error_cov_lower(    (    sH   lib/python2.7/site-packages/statsmodels/tsa/statespace/dynamic_factor.pyR   $  s(    ,)%"#	N(   t   __name__t
   __module__t   __doc__R6   RA   R^   R@   RD   RE   RF   R]   R_   RG   RH   Rl   Rm   Rn   t   propertyR}   R   R   R   R   R   (    (    (    sH   lib/python2.7/site-packages/statsmodels/tsa/statespace/dynamic_factor.pyR      s(   p	]										(y=	c	\R{   c           B` s   e  Z d  Z d d  Z e d    Z e d    Z d	 d	 d	 d  Z	 d	 d	 e
 d	 d	 d  Z d d	 e d  Z e j j e _ RS(
   sk  
    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 : array
        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
    t   opgc         K` s  t  t |   j | | | | |  t j |  _ t i |  j j d 6|  j j	 d 6|  j j
 d 6|  j j d 6|  j j d 6|  j j d 6|  j j d 6|  j j d 6  |  _ d  |  _ |  j j d	 k r%t j |  j |  j j  } |  j j
 } |  j j } | j | | |  j j | | |  j |  _ n  d  |  _ |  j j d	 k rt j |  j |  j j  } |  j j }	 |  j j }
 |  j j r| j |	 |
 |	  j j |	 |	 |
  j |  _ qt j |	 |	 |
 f  } | | |  j j <| j j |
 |	 |	  |  _ n  d  S(
   NRS   R.   R/   R0   R1   R2   R3   R4   i    (   R?   R{   R@   R"   t   inft   df_residR   t   modelRS   R.   R/   R0   R1   R2   R3   R4   t   specificationR6   t   coefficient_matrices_varR   R   RO   R   R   t   coefficient_matrices_errorRP   RZ   Ru   (   R(   R   R   t   filter_resultst   cov_typeRR   t	   ar_paramsR/   R0   RS   R1   t   mat(    (    sH   lib/python2.7/site-packages/statsmodels/tsa/statespace/dynamic_factor.pyR@     s>    		c         C` s   d } |  j } | j d k r d } | j } |  j } t d | j | | !d | j | |  | |  f d d d d d |  } |  j d k	 r |  j | | !| _ n  |  j	 d k	 r |  j	 | |  | |  f | _
 q n  | S(   s  
        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
        i    t   filteredt   filtered_covt   smoothedt   smoothed_covR%   N(   R6   R   R/   R   R   t   filtered_statet   filtered_state_covt   smoothed_stateR   t   smoothed_state_covR   (   R(   t   outt   specR%   RX   t   res(    (    sH   lib/python2.7/site-packages/statsmodels/tsa/statespace/dynamic_factor.pyR     s     				(c   	      C` s   d d l  m } |  j } t j | j | j f  } |  j d k rI d n d } xz t	 | j  D]i } | |  j
 | |  } xI t	 | j  D]8 } |  j j | } t | |  j   j | | | f <q Wq_ W| S(   sF  
        Coefficients of determination (:math:`R^2`) from regressions of
        individual estimated factors on endogenous variables.

        Returns
        -------
        coefficients_of_determination : array
            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 cofficients 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

        i    (   t   add_constantR   R   N(   t   statsmodels.toolsR   R   R"   RZ   RS   R/   R   R6   Rr   R   R   RQ   R   Rz   t   rsquared(	   R(   R   R   R   t   whichRw   R   R   RQ   (    (    sH   lib/python2.7/site-packages/statsmodels/tsa/statespace/dynamic_factor.pyt   coefficients_of_determination  s    	*c         C` sN  d d l  m } m } |   | | |  } |  j } | d k rS | j d k } n  |  j } d } t j | j  }	 x | j	 D] }
 | j
 | j d |  } | j d  | j d d | d d  | j |	 |
  } | r| d j   } | j j |	 | d	  | j j |  j j  n  | j d
 d  | j j g   | d 7} q~ W| S(   sM  
        Plot the coefficients of determination

        Parameters
        ----------
        endog_labels : boolean, 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 : Matplotlib Figure instance, 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

        i    (   t   _import_mplt   create_mpl_figi   i   t   titles	   Factor %it   ylabels   $R^2$i   t   xlabels   Endogenous variablesN(   i    i   (   t   statsmodels.graphics.utilsR   R   R   R6   RS   R   R"   t   arangeR   t   add_subplotR/   t   set_ylimt   sett   bart	   get_widtht   xaxist	   set_tickst   set_ticklabelsR   R   (   R(   t   endog_labelst   figt   figsizeR   R   R   R   t   plot_idxt	   locationst   coeffst   axt   barst   width(    (    sH   lib/python2.7/site-packages/statsmodels/tsa/statespace/dynamic_factor.pyt"   plot_coefficients_of_determination  s,     		c         K` s  | d k r |  j j d } n  |  j j | | | d t \ } } }	 }
 |	 rj|  j j d k rj|  j j j j d |	 } t	 j
 | |  j j f  } |  j j d k rC| d k r t d   n  t	 j |  } |	 |  j j f } | j | k st d t |  t | j  f   n  t	 j |  j j j j | j f j } n  t | d |  j j d |  j j d | d |  j j d	 |  j j d
 |  j j d |  j j } | j |  j  x |  j j j   D] } | d k rqn  t | j |  } | j d d k rt  | j  d k r6| d d  |	 d  f | | <qc| d d  d d  |	 d  f | | <qqWn. |  j j d k r| d k	 rt! d t"  n  t# t$ |   j% d | d | d | d | d | |  S(   sQ  
        In-sample prediction and out-of-sample forecasting

        Parameters
        ----------
        start : int, str, or datetime, optional
            Zero-indexed observation number at which to start forecasting, ie.,
            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, ie.,
            the first forecast is start. 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.
        exog : array_like, optional
            If the model includes exogenous regressors, you must provide
            exactly enough out-of-sample values for the exogenous variables if
            end is beyond the last observation in the sample.
        dynamic : boolean, 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.
        **kwargs
            Additional arguments may required for forecasting beyond the end
            of the sample. See `FilterResults.predict` for more details.

        Returns
        -------
        forecast : array
            Array of out of sample forecasts.
        i    t   silents~   Out-of-sample forecasting in a model with a regression component requires additional exogenous values via the `exog` argument.sP   Provided exogenous values are not of the appropriate shape. Required %s, got %s.R/   R0   R   R1   R2   R3   R.   t   obsii   i   Ns_   Exogenous array provided to predict, but additional data not required. `exog` argument ignored.RW   RX   t   dynamict   index(&   R6   R   t   _indext   _get_prediction_indexR^   R4   t   datat
   orig_endogR9   R"   RZ   RS   R=   R   t   strt   c_t	   orig_exogR   R   R/   R0   R1   R2   R3   R.   R   R   R   t   shapest   keyst   getattrRB   t   lenR   R   R?   R{   t   get_prediction(   R(   RW   RX   R   R   R   RR   t   _startt   _endt   _out_of_samplet   prediction_indexR[   RQ   t   required_exog_shapeR   t   nameR   (    (    sH   lib/python2.7/site-packages/statsmodels/tsa/statespace/dynamic_factor.pyR  ^  sP    '*	($4
g?c   &   	   ` s  d d l  m  |  j } g  } | j d k r | j d k rV d | j | j f } n d | j } | j |  | j d k r | j d | j  q n | j d | j  | j d k r | j r d n d } | j d	 | | j f  n  t	 t
 |   j d
   d | d | d |  } | rt j t |  j   }	 t    f d  }
 |  j j } |  j j } |  j j } |  j j } |  j j } |  j j } |	 |  j j } g  } |	 |  j j } g  } x t |  D] } | | | | d | !} | j |  | | | | d | !} | j |  t j | | g  } d |  j j | } |
 |  | |  } | j j |  qW|	 |  j j } g  } | d k rxo t |  D]^ } | | } | | | | !} | j |  d | d } |
 |  | |  } | j j |  qWn  g  } | j d k r|	 |  j j } x t |  D] } | j ra| | } | d | }  n | | j } | d | j }  | | |  !}! | j |!  d |  j j | } |
 |  |! |  } | j j |  q7Wn  |	 |  j j }" |
 |  |" d d t } | j j |  g  }# xW | | | | |" g f D]= }$ t j |$  j    }$ t |$  d k r-|# j |$  q-q-Wt j |#  }# t j t! t" |	  j# t" |#     }% t |%  d k r|
 |  |% d d t } | j j |  qn  | S(   Ni    (   t   summary_paramss#   DynamicFactor(factors=%d, order=%d)s   StaticFactor(factors=%d)s   %d regressorss   SUR(%d regressors)R   t   ARs   %s(%d) errorst   alphaRW   t
   model_namet   display_paramsc         ` s   |  |  j  | |  j | |  j | |  j | |  j    | f } g  t j |  j j  | j	   D]. } | r d j
 | j d  d   n | ^ qa }  | d d  d | d   d t d | S(   Nt   .it   ynamet   xnameR  t   use_tR   (   R   t   bset   zvaluest   pvaluest   conf_intR"   R   R   R   t   tolistt   joint   splitR6   RA   (   R(   t   maskR   t	   strip_endR   R  R   (   R  R  (    sH   lib/python2.7/site-packages/statsmodels/tsa/statespace/dynamic_factor.pyt
   make_table  s    Qi   s   Results for equation %ss   Results for factor equation f%ds    Results for error equation e(%s)s   Error covariance matrixR  s   Other parameters($   t   statsmodels.iolib.summaryR  R   R/   R0   t   appendR4   R1   R2   R?   R{   t   summaryR"   R   R  R   R^   R   RS   R;   R<   RL   RM   Rr   t   concatenateR   t   tablesRO   RP   RN   RA   R   t   flattenR   R   t
   difference(&   R(   R  RW   t   separate_paramsR   R  t
   model_typet
   error_typeR#  t   indicesR   RS   R4   R/   R0   R;   R<   t   loading_indicest   loading_maskst   exog_indicest
   exog_masksRw   t   loading_maskt	   exog_maskR  R   t   tablet   factor_indicest   factor_maskst   factor_maskt   error_maskst   error_indicesRX   t
   error_maskt   error_cov_maskt   maskst   mt   inverse_mask(    (   R  R  sH   lib/python2.7/site-packages/statsmodels/tsa/statespace/dynamic_factor.pyR#    s    	

	
	*	N(   R   R   R   R@   R   R   R   R   R6   R   RA   R  R^   R#  R   (    (    (    sH   lib/python2.7/site-packages/statsmodels/tsa/statespace/dynamic_factor.pyR{   z  s   5,+@bR|   c           B` s>   e  Z i  Z e j e j e  Z i  Z e j e j e  Z RS(    (	   R   R   t   _attrst   wrapt   union_dictsR   t   _wrap_attrst   _methodst   _wrap_methods(    (    (    sH   lib/python2.7/site-packages/statsmodels/tsa/statespace/dynamic_factor.pyR|   J  s   	(.   R   t
   __future__R    R   R   t   warningsR   t   collectionsR   t   numpyR"   t   mlemodelR   R   R   t   toolsR   R	   R
   R   R   R   t   statsmodels.multivariate.pcaR   t#   statsmodels.regression.linear_modelR   t#   statsmodels.tsa.vector_ar.var_modelR   t   statsmodels.tools.toolsR   t   statsmodels.tools.dataR   t   statsmodels.tsa.tsatoolsR   t   statsmodels.tools.decoratorsR   t   statsmodels.tools.sm_exceptionsR   t   statsmodels.base.wrappert   baset   wrapperR>  R   R{   R|   t   populate_wrapper(    (    (    sH   lib/python2.7/site-packages/statsmodels/tsa/statespace/dynamic_factor.pyt   <module>   s0   .   ^ 	