ó
áp7]c           @` s”   d  Z  d d l m Z m Z m Z d d l Z d d l m Z d d l	 m
 Z
 d Z d Z e e BZ d e f d	 „  ƒ  YZ d
 e f d „  ƒ  YZ d S(   s{   
State Space Representation, Kalman Filter, Smoother, and Simulation Smoother

Author: Chad Fulton
License: Simplified-BSD
i    (   t   divisiont   absolute_importt   print_functionNi   (   t   KalmanSmoother(   t   toolsi   t   SimulationSmootherc           B` s_   e  Z d  Z d d d g Z d d d d „ Z d d d d d „ Z d „  Z d d d d „ Z RS(	   s  
    State space representation of a time series process, with Kalman filter
    and smoother, and with simulation smoother.

    Parameters
    ----------
    k_endog : array_like or integer
        The observed time-series process :math:`y` if array like or the
        number of variables in the process if an integer.
    k_states : int
        The dimension of the unobserved state process.
    k_posdef : int, optional
        The dimension of a guaranteed positive definite covariance matrix
        describing the shocks in the measurement equation. Must be less than
        or equal to `k_states`. Default is `k_states`.
    simulation_smooth_results_class : class, optional
        Default results class to use to save output of simulation smoothing.
        Default is `SimulationSmoothResults`. If specified, class must extend
        from `SimulationSmoothResults`.
    simulation_smoother_classes : dict, optional
        Dictionary with BLAS prefixes as keys and classes as values.
    **kwargs
        Keyword arguments may be used to provide default values for state space
        matrices, for Kalman filtering options, for Kalman smoothing
        options, or for Simulation smoothing options.
        See `Representation`, `KalmanFilter`, and `KalmanSmoother` for more
        details.
    t   simulate_statet   simulate_disturbancet   simulate_allc         K` sn   t  t |  ƒ j | | | |  | d  k r4 t } n  | |  _ | d  k	 rO | n t j j ƒ  |  _ i  |  _	 d  S(   N(
   t   superR   t   __init__t   Nonet   SimulationSmoothResultst   simulation_smooth_results_classR   t   prefix_simulation_smoother_mapt   copyt   _simulators(   t   selft   k_endogt   k_statest   k_posdefR   t   simulation_smoother_classest   kwargs(    (    sM   lib/python2.7/site-packages/statsmodels/tsa/statespace/simulation_smoother.pyR
   6   s    		c         K` s§   | d k r£ d } | r% | t O} n  | r8 | t O} n  | rK | t O} n  | d k r£ t | d k | d k | d k g ƒ } | r” t d ƒ ‚ n  |  j } q£ n  | S(   sÛ  
        Get simulation output bitmask

        Helper method to get final simulation output bitmask from a set of
        optional arguments including the bitmask itself and possibly boolean
        flags.

        Parameters
        ----------
        simulation_output : integer, optional
            Simulation output bitmask. If this is specified, it is simply
            returned and the other arguments are ignored.
        simulate_state : boolean, optional
            Whether or not to include the state in the simulation output.
        simulate_disturbance : boolean, optional
            Whether or not to include the state and observation disturbances
            in the simulation output.
        simulate_all : boolean, optional
            Whether or not to include all simulation output.
        \*\*kwargs
            Additional keyword arguments. Present so that calls to this method
            can use \*\*kwargs without clearing out additional arguments.
        i    sK   Invalid simulation output options: given options would result in no output.N(   R   t   SIMULATION_STATEt   SIMULATION_DISTURBANCEt   SIMULATION_ALLt   allt
   ValueErrort   smoother_output(   R   t   simulation_outputR   R   R   R   t   argument_set(    (    sM   lib/python2.7/site-packages/statsmodels/tsa/statespace/simulation_smoother.pyt   get_simulation_outputJ   s     c         C` sñ  |  j  ƒ  \ } } } } }	 |  j d | ƒ | |  j k sQ | |  j | j k rä d }
 d } |  j } |  j } |  j } |  j } |  j } |  j	 } |  j
 } |  j | } | |  j | | | | | | | | | |
 | ƒ |  j | <n  |  j | } t j t j t j | j ƒ  | j ƒ  f d |  j ƒj ƒ  ƒ } | j | d t ƒt j t j | d |  j ƒj ƒ  ƒ } | j | ƒ | j d ƒ t j | j d t ƒ} t j | j d t ƒ} | d  d  … d  | … f j | d  d  … d  | … f j f S(   Nt   prefixi    iÿÿÿÿt   dtypet   pretransformedR   (   t   _initialize_smoothert   _initialize_stateR   t   nobst   filter_methodt   inversion_methodt   stability_methodt   conserve_memoryt   filter_timingt   loglikelihood_burnt	   toleranceR   t   _statespacest   npt
   atleast_1dt   arrayt   r_t   ravelR!   t   squeezet   set_disturbance_variatest   Truet   set_initial_statet   simulatet   generated_obst   generated_statet   T(   R   t   nsimulationst   measurement_shockst   state_shockst   initial_stateR    R!   t   create_smoothert   create_filtert   create_statespaceR   R   R&   R'   R(   R)   R*   R+   R,   t   clst	   simulatort   disturbance_variatest   simulated_obst   simulated_state(    (    sM   lib/python2.7/site-packages/statsmodels/tsa/statespace/simulation_smoother.pyt	   _simulate„   sB    							
	c         K` sV  | d
 k r |  j } n  t | t ƒ s6 t d ƒ ‚ n  |  j ƒ  \ } } } } } |  j | |  } | j d | ƒ }	 | j d |  j ƒ }
 | j d |  j	 ƒ } | j d |  j
 ƒ } | j d |  j ƒ } | j d |  j ƒ } | j d |  j ƒ } | j d	 |  j ƒ } |  j | } | |  j | |
 | | | | | | |	 | ƒ
 } | |  | ƒ } | S(   sa  
        Retrieve a simulation smoother for the statespace model.

        Parameters
        ----------
        simulation_output : int, optional
            Determines which simulation smoother output is calculated.
            Default is all (including state and disturbances).
        simulation_smooth_results_class : class, optional
            Default results class to use to save output of simulation
            smoothing. Default is `SimulationSmoothResults`. If specified,
            class must extend from `SimulationSmoothResults`.
        prefix : string
            The prefix of the datatype. Usually only used internally.
        **kwargs
            Additional keyword arguments, used to set the simulation output.
            See `set_simulation_output` for more details.

        Returns
        -------
        SimulationSmoothResults
        s   Invalid results class provided.R   R&   R'   R(   R)   R*   R+   R,   N(   R   R   t
   issubclassR   R   R#   R   t   getR&   R'   R(   R)   R*   R+   R,   R   R-   (   R   R   t   results_classR    R   R!   R?   R@   RA   R   R&   R'   R(   R)   R*   R+   R,   RB   t   simulation_smoothert   results(    (    sM   lib/python2.7/site-packages/statsmodels/tsa/statespace/simulation_smoother.pyRK   Â   s8    							
	N(	   t   __name__t
   __module__t   __doc__t   simulation_outputsR   R
   R   RG   RK   (    (    (    sM   lib/python2.7/site-packages/statsmodels/tsa/statespace/simulation_smoother.pyR      s   8	>R   c           B` s  e  Z d  Z d „  Z e d „  ƒ Z e j d „  ƒ Z e d „  ƒ Z e j d „  ƒ Z e d „  ƒ Z e j d „  ƒ Z e d „  ƒ Z	 e	 j d	 „  ƒ Z	 e d
 „  ƒ Z
 e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z d d d e d „ Z RS(   s…  
    Results from applying the Kalman smoother and/or filter to a state space
    model.

    Parameters
    ----------
    model : Representation
        A Statespace representation
    simulation_smoother : {{prefix}}SimulationSmoother object
        The Cython simulation smoother object with which to simulation smooth.

    Attributes
    ----------
    model : Representation
        A Statespace representation
    dtype : dtype
        Datatype of representation matrices
    prefix : str
        BLAS prefix of representation matrices
    simulation_output : integer
        Bitmask controlling simulation output.
    simulate_state : boolean
        Flag for if the state is included in simulation output.
    simulate_disturbance : boolean
        Flag for if the state and observation disturbances are included in
        simulation output.
    simulate_all : boolean
        Flag for if simulation output should include everything.
    generated_measurement_disturbance : array
        Measurement disturbance variates used to genereate the observation
        vector.
    generated_state_disturbance : array
        State disturbance variates used to genereate the state and
        observation vectors.
    generated_obs : array
        Generated observation vector produced as a byproduct of simulation
        smoothing.
    generated_state : array
        Generated state vector produced as a byproduct of simulation smoothing.
    simulated_state : array
        Simulated state.
    simulated_measurement_disturbance : array
        Simulated measurement disturbance.
    simulated_state_disturbance : array
        Simulated state disturbance.
    c         C` sm   | |  _  | j |  _ | j |  _ | |  _ d  |  _ d  |  _ d  |  _ d  |  _ d  |  _	 d  |  _
 d  |  _ d  S(   N(   t   modelR    R!   t   _simulation_smootherR   t"   _generated_measurement_disturbancet   _generated_state_disturbancet   _generated_obst   _generated_statet   _simulated_statet"   _simulated_measurement_disturbancet   _simulated_state_disturbance(   R   RQ   RK   (    (    sM   lib/python2.7/site-packages/statsmodels/tsa/statespace/simulation_smoother.pyR
   ;  s    								c         C` s
   |  j  j S(   N(   RR   R   (   R   (    (    sM   lib/python2.7/site-packages/statsmodels/tsa/statespace/simulation_smoother.pyR   J  s    c         C` s   | |  j  _ d  S(   N(   RR   R   (   R   t   value(    (    sM   lib/python2.7/site-packages/statsmodels/tsa/statespace/simulation_smoother.pyR   N  s    c         C` s   t  |  j t @ƒ S(   N(   t   boolR   R   (   R   (    (    sM   lib/python2.7/site-packages/statsmodels/tsa/statespace/simulation_smoother.pyR   R  s    c         C` s4   t  | ƒ r |  j t B|  _ n |  j t @|  _ d  S(   N(   R[   R   R   (   R   RZ   (    (    sM   lib/python2.7/site-packages/statsmodels/tsa/statespace/simulation_smoother.pyR   V  s    c         C` s   t  |  j t @ƒ S(   N(   R[   R   R   (   R   (    (    sM   lib/python2.7/site-packages/statsmodels/tsa/statespace/simulation_smoother.pyR   ]  s    c         C` s4   t  | ƒ r |  j t B|  _ n |  j t @|  _ d  S(   N(   R[   R   R   (   R   RZ   (    (    sM   lib/python2.7/site-packages/statsmodels/tsa/statespace/simulation_smoother.pyR   a  s    c         C` s   t  |  j t @ƒ S(   N(   R[   R   R   (   R   (    (    sM   lib/python2.7/site-packages/statsmodels/tsa/statespace/simulation_smoother.pyR   j  s    c         C` s4   t  | ƒ r |  j t B|  _ n |  j t @|  _ d  S(   N(   R[   R   R   (   R   RZ   (    (    sM   lib/python2.7/site-packages/statsmodels/tsa/statespace/simulation_smoother.pyR   n  s    c         C` si   |  j  d k rb |  j j |  j j } t j |  j j |  d t	 ƒj
 |  j j |  j j ƒ |  _  n  |  j  S(   s~  
        Randomly drawn measurement disturbance variates, used to construct
        `generated_obs`.

        Notes
        -----

        .. math::
            arepsilon_t^+ ~ N(0, H_t)

        If `disturbance_variates` were provided to the `simulate()` method,
        then this returns those variates (which were N(0,1)) transformed to the
        distribution above.

        R   N(   RS   R   RQ   R%   R   R.   R0   RR   RD   R5   t   reshape(   R   t   end(    (    sM   lib/python2.7/site-packages/statsmodels/tsa/statespace/simulation_smoother.pyt!   generated_measurement_disturbanceu  s    'c         C` si   |  j  d k rb |  j j |  j j } t j |  j j | d t	 ƒj
 |  j j |  j j ƒ |  _  n  |  j  S(   s‰  
        Randomly drawn state disturbance variates, used to construct
        `generated_state` and `generated_obs`.

        Notes
        -----

        .. math::

            \eta_t^+ ~ N(0, Q_t)

        If `disturbance_variates` were provided to the `simulate()` method,
        then this returns those variates (which were N(0,1)) transformed to the
        distribution above.

        R   N(   RT   R   RQ   R%   R   R.   R0   RR   RD   R5   R\   R   (   R   t   start(    (    sM   lib/python2.7/site-packages/statsmodels/tsa/statespace/simulation_smoother.pyt   generated_state_disturbance  s    'c         C` s7   |  j  d k r0 t j |  j j d t ƒ|  _  n  |  j  S(   s)  
        Generated vector of observations by iterating on the observation and
        transition equations, given a random initial state draw and random
        disturbance draws.

        Notes
        -----

        .. math::

            y_t^+ = d_t + Z_t \alpha_t^+ + \varepsilon_t^+

        R   N(   RU   R   R.   R0   RR   R8   R5   (   R   (    (    sM   lib/python2.7/site-packages/statsmodels/tsa/statespace/simulation_smoother.pyR8   ¦  s    c         C` s7   |  j  d k r0 t j |  j j d t ƒ|  _  n  |  j  S(   s  
        Generated vector of states by iterating on the transition equation,
        given a random initial state draw and random disturbance draws.

        Notes
        -----

        .. math::

            \alpha_{t+1}^+ = c_t + T_t \alpha_t^+ + \eta_t^+

        R   N(   RV   R   R.   R0   RR   R9   R5   (   R   (    (    sM   lib/python2.7/site-packages/statsmodels/tsa/statespace/simulation_smoother.pyR9   »  s    c         C` s7   |  j  d k r0 t j |  j j d t ƒ|  _  n  |  j  S(   s®   
        Random draw of the state vector from its conditional distribution.

        Notes
        -----

        .. math::

            \alpha ~ p(\alpha \mid Y_n)

        R   N(   RW   R   R.   R0   RR   RF   R5   (   R   (    (    sM   lib/python2.7/site-packages/statsmodels/tsa/statespace/simulation_smoother.pyRF   Ï  s    c         C` s7   |  j  d k r0 t j |  j j d t ƒ|  _  n  |  j  S(   sî   
        Random draw of the measurement disturbance vector from its conditional
        distribution.

        Notes
        -----

        .. math::

            \varepsilon ~ N(\hat \varepsilon, Var(\hat \varepsilon \mid Y_n))

        R   N(   RX   R   R.   R0   RR   t!   simulated_measurement_disturbanceR5   (   R   (    (    sM   lib/python2.7/site-packages/statsmodels/tsa/statespace/simulation_smoother.pyRa   â  s
    c         C` s7   |  j  d k r0 t j |  j j d t ƒ|  _  n  |  j  S(   sÓ   
        Random draw of the state disturbance vector from its conditional
        distribution.

        Notes
        -----

        .. math::

            \eta ~ N(\hat \eta, Var(\hat \eta \mid Y_n))

        R   N(   RY   R   R.   R0   RR   t   simulated_state_disturbanceR5   (   R   (    (    sM   lib/python2.7/site-packages/statsmodels/tsa/statespace/simulation_smoother.pyRb   ÷  s
    iÿÿÿÿc   
      C` s  d |  _ d |  _ d |  _ d |  _ d |  _ d |  _ d |  _ d |  _ |  j j	 ƒ  \ } } } } }	 |  j j
 d | ƒ | d k	 r° |  j j t j | d |  j ƒd | ƒn |  j j ƒ  | d k	 rô |  j j t j | d |  j ƒd | ƒn |  j j ƒ  |  j j | ƒ d S(   sä  
        Perform simulation smoothing

        Does not return anything, but populates the object's `simulated_*`
        attributes, as specified by simulation output.

        Parameters
        ----------
        simulation_output : integer, optional
            Bitmask controlling simulation output. Default is to use the
            simulation output defined in object initialization.
        disturbance_variates : array_likes, optional
            Random values to use as disturbance variates, distributed standard
            Normal. Usually only specified if results are to be replicated
            (e.g. to enforce a seed) or for testing. If not specified, random
            variates are drawn.
        initial_state_variates : array_likes, optional
            Random values to use as initial state variates. Usually only
            specified if results are to be replicated (e.g. to enforce a seed)
            or for testing. If not specified, random variates are drawn.
        R    R!   R"   N(   R   RS   RT   RV   RU   RW   RX   RY   RQ   R#   R$   RR   R4   R.   R0   R!   t   draw_disturbance_variatest   set_initial_state_variatest   draw_initial_state_variatesR7   (
   R   R   RD   t   initial_state_variatest   pretransformed_variatesR    R!   R?   R@   RA   (    (    sM   lib/python2.7/site-packages/statsmodels/tsa/statespace/simulation_smoother.pyR7     s*    									
	
N(   RM   RN   RO   R
   t   propertyR   t   setterR   R   R   R^   R`   R8   R9   RF   Ra   Rb   R   t   FalseR7   (    (    (    sM   lib/python2.7/site-packages/statsmodels/tsa/statespace/simulation_smoother.pyR     s&   .		(   RO   t
   __future__R    R   R   t   numpyR.   t   kalman_smootherR   t    R   R   R   R   R   t   objectR   (    (    (    sM   lib/python2.7/site-packages/statsmodels/tsa/statespace/simulation_smoother.pyt   <module>   s   
÷