U
    Kvfj                     @   s   d Z ddlZddlZddlmZmZ ddlZddl	Z	ddl
mZ ddlmZ ddlmZ ddlmZ ejejeZG dd	 d	ZG d
d deZG dd dZG dd deZG dd deZG dd deZG dd deZG dd dZdd ZdS )ai  
Tests for univariate treatment of multivariate models

TODO skips the tests for measurement disturbance and measurement disturbance
covariance, which do not pass. The univariate smoother *appears* to be
correctly implemented against Durbin and Koopman (2012) chapter 6, yet still
gives a different answer from the conventional smoother. It's not clear if
this is intended (i.e. it has to be at least slightly different, since the
conventional smoother can return a non-diagonal covariance matrix whereas the
univariate smoother must return a diagonal covariance matrix).

Author: Chad Fulton
License: Simplified-BSD
    N)assert_almost_equalassert_allclose)datasets)MLEModel)results_kalman_filter)SARIMAXc                   @   s   e Zd ZdZeedfddZdd Zdd Zd	d
 Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)S )*TestClark1989aj  
    Clark's (1989) bivariate unobserved components model of real GDP (as
    presented in Kim and Nelson, 1999)

    Tests two-dimensional observation data.

    Test data produced using GAUSS code described in Kim and Nelson (1999) and
    found at http://econ.korea.ac.kr/~cjkim/SSMARKOV.htm

    See `results.results_kalman_filter` for more information.
    Fc                 K   s  t j| _t| jd | _tj| jd tjddddddgd	d
d  }t|d |d< |d d |d< d}t	|fd|i|| _
| j
j| _ddddddgddddddgg| jjd d d d df< ddddddddg| jjddddddd
dgdd
ddddd
dgddddddddgf< t| jj| j_t| jd \
}}}}	}
}}}}}|||g| jjdddgdddgdddgf< ||g| jjddgddgddgf< |
d | jjd< |d |d dd|d |	d g| jjt|tj|tdf < t|f}t|d }|sJtt| jjd d d d df || jjd d d d df j}nd| j_| j|| d| j_| j | _| jj| jj  | jj! }| jj"t|t| jjd| _#d| j_$| j | _%| jj"t|t| jjd| _&d S )NZstatesdataz
1947-01-01z
1995-07-01QS)freqZGDPZUNEMP)indexcolumns   d      k_states   r            
parametersr   r   r   )dtypeTZdisturbance_variatesZinitial_state_variates)'r   Zuc_bitruepdZ	DataFrameZtrue_states
date_rangenplogr   Zmlemodelssmmodeldesign
transitioneyer   	selectionarrayobs_cov	state_covZdiag_indiceszerosintdotTZtiming_init_filteredZinitialize_knownfilter_conventionalsmoothconventional_resultsk_endogk_posdefnobssimulation_smootherconventional_simfilter_univariateunivariate_resultsunivariate_sim)clsr   alternate_timingkwargsr	   r   Zsigma_vZsigma_eZsigma_wZsigma_vlZsigma_ecZphi_1Zphi_2Zalpha_1Zalpha_2Zalpha_3Zinitial_stateZinitial_state_covn_disturbance_variates r;   T/tmp/pip-unpacked-wheel-2v6byqio/statsmodels/tsa/statespace/tests/test_univariate.pysetup_class*   s    
6      $"      zTestClark1989.setup_classc                 C   s@   | j jrt| jjstt| j jd d t| jjd d d S )Nr   g+1a@gEKК_*^@r.   r4   AssertionErrorr5   r   forecasts_error_covselfr;   r;   r<   test_using_univariatev   s    

z#TestClark1989.test_using_univariatec                 C   s0   t | jjdd d f | jjdd d f d d S Nr   	   r   r.   	forecastsr5   rA   r;   r;   r<   test_forecasts   s
     zTestClark1989.test_forecastsc                 C   s0   t | jjdd d f | jjdd d f d d S rD   r   r.   forecasts_errorr5   rA   r;   r;   r<   test_forecasts_error   s
     z"TestClark1989.test_forecasts_errorc                 C   s4   t | jjddd d f | jjddd d f d d S rD   r   r.   r@   r5   rA   r;   r;   r<   test_forecasts_error_cov   s
     z&TestClark1989.test_forecasts_error_covc                 C   s   t | jj| jjd d S N   r   r.   filtered_stater5   rA   r;   r;   r<   test_filtered_state   s
     z!TestClark1989.test_filtered_statec                 C   s   t | jj| jjd d S NrE   r   r.   filtered_state_covr5   rA   r;   r;   r<   test_filtered_state_cov   s
     z%TestClark1989.test_filtered_state_covc                 C   s   t | jj| jjd d S rN   r   r.   predicted_stater5   rA   r;   r;   r<   test_predicted_state   s
     z"TestClark1989.test_predicted_statec                 C   s   t | jj| jjd d S rS   r   r.   predicted_state_covr5   rA   r;   r;   r<   test_predicted_state_cov   s
     z&TestClark1989.test_predicted_state_covc                 C   s   t | jj| jj d S Nr   r.   llf_obsr5   rA   r;   r;   r<   test_loglike   s    zTestClark1989.test_loglikec                 C   s   t | jj| jjd d S N   r   r.   smoothed_stater5   rA   r;   r;   r<   test_smoothed_states   s
     z"TestClark1989.test_smoothed_statesc                 C   s   t | jj| jjd d S Nr   r   r.   smoothed_state_covr5   rA   r;   r;   r<   test_smoothed_states_cov   s
     z&TestClark1989.test_smoothed_states_covc                 C   s   t | jj| jjd d S rS   r   r.    smoothed_measurement_disturbancer5   rA   r;   r;   r<   %test_smoothed_measurement_disturbance   s
     z3TestClark1989.test_smoothed_measurement_disturbancec                 C   s(   | j }| j}t|j |j d d S rS   r.   r5   r   $smoothed_measurement_disturbance_covdiagonalrB   convunivr;   r;   r<   )test_smoothed_measurement_disturbance_cov   s     z7TestClark1989.test_smoothed_measurement_disturbance_covc                 C   s   t | jj| jjdd d S NgHz>Zatolr   r.   smoothed_state_disturbancer5   rA   r;   r;   r<   test_smoothed_state_disturbance   s
    z-TestClark1989.test_smoothed_state_disturbancec                 C   s   t | jj| jjd d S rS   r   r.   smoothed_state_disturbance_covr5   rA   r;   r;   r<   #test_smoothed_state_disturbance_cov   s
     z1TestClark1989.test_smoothed_state_disturbance_covc                 C   s   t | jj| jjd d S rS   r   r3   simulated_stater6   rA   r;   r;   r<   test_simulation_smoothed_state   s
     z,TestClark1989.test_simulation_smoothed_statec                 C   s   t | jj| jjd d S rS   r   r3   !simulated_measurement_disturbancer6   rA   r;   r;   r<   0test_simulation_smoothed_measurement_disturbance   s
     z>TestClark1989.test_simulation_smoothed_measurement_disturbancec                 C   s   t | jj| jjd d S rS   r   r3   simulated_state_disturbancer6   rA   r;   r;   r<   *test_simulation_smoothed_state_disturbance   s
     z8TestClark1989.test_simulation_smoothed_state_disturbanceN)__name__
__module____qualname____doc__classmethodfloatr=   rC   rH   rK   rM   rR   rV   rY   r\   r`   re   ri   rl   rs   rx   r{   r~   r   r   r;   r;   r;   r<   r      s*   Kr   c                       s(   e Zd Ze fddZdd Z  ZS )TestClark1989Alternatec                    s   t t| j|ddi| d S )Nr8   T)superr   r=   r7   argsr9   	__class__r;   r<   r=      s
    
z"TestClark1989Alternate.setup_classc                 C   s   | j jjdkstd S )Nr   )r    Z_kalman_filterZfilter_timingr?   rA   r;   r;   r<   test_using_alterate   s    z*TestClark1989Alternate.test_using_alterate)r   r   r   r   r=   r   __classcell__r;   r;   r   r<   r      s   r   c                   @   s   e Zd ZeedfddZdd Zdd Zdd	 Zd
d Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zejjdd Zejjdd Zdd Zd d! Zd"d# Zejjd$d% Zd&d' Zd(S )) MultivariateMissingGeneralObsCovFc                 K   sB  t jtdd}t|| _tj	 j
}tjdddd|_|ddd	g  jd
d  }|dkrtj|jd dd d f< tj|jddd d f< n|dkrtj|jdddf< tj|jdddf< nn|dkr0tj|jdddf< tj|jddd
f< tj|jdddf< tj|jdddf< tj|jdddf< t|fddd|}td|d< tdd
 dd }	t|	|	j|d< td|d< td|d< td|d < |d! |j| _d"| j_| j | _| jj| jj | jj }
| jj t!|
t!| jj"d#| _#d"| j_$| j | _%| jj t!|
t!| jj"d#| _&d S )$Nresults%results_smoothing_generalobscov_R.csv
1959-01-01	2009-7-01r
   startendr   realgdprealconsrealinvr   all2   w      partialr   mixed   F   '   Z   r   r   r   r0   r!   rE   )r   r   g      $@r&   r"   r$   r'       .ATr   )'ospathjoincurrent_pathr   read_csvdesiredr   	macrodataload_pandasr	   r   r   diffilocr   nanr   r#   ZarangeZreshaper*   r+   initialize_approximate_diffuser   r    r,   r-   r.   r/   r0   r1   r2   r(   r   r3   r4   r5   r6   )r7   whichr   r8   r9   r   dtaobsmodXr:   r;   r;   r<   r=      sZ    
 

z,MultivariateMissingGeneralObsCov.setup_classc                 C   s@   | j jrt| jjstt| j jd d t| jjd d d S )Nr   gp=.Ar>   rA   r;   r;   r<   rC   4  s    

z6MultivariateMissingGeneralObsCov.test_using_univariatec                 C   s0   t | jjdd d f | jjdd d f d d S rD   rF   rA   r;   r;   r<   rH   E  s
     z/MultivariateMissingGeneralObsCov.test_forecastsc                 C   s0   t | jjdd d f | jjdd d f d d S rD   rI   rA   r;   r;   r<   rK   K  s
     z5MultivariateMissingGeneralObsCov.test_forecasts_errorc                 C   s4   t | jjddd d f | jjddd d f d d S rD   rL   rA   r;   r;   r<   rM   Q  s
     z9MultivariateMissingGeneralObsCov.test_forecasts_error_covc                 C   s   t | jj| jjd d S rN   rP   rA   r;   r;   r<   rR   W  s
     z4MultivariateMissingGeneralObsCov.test_filtered_statec                 C   s   t | jj| jjd d S rS   rT   rA   r;   r;   r<   rV   ]  s
     z8MultivariateMissingGeneralObsCov.test_filtered_state_covc                 C   s   t | jj| jjd d S rN   rW   rA   r;   r;   r<   rY   c  s
     z5MultivariateMissingGeneralObsCov.test_predicted_statec                 C   s   t | jj| jjd d S rS   rZ   rA   r;   r;   r<   r\   i  s
     z9MultivariateMissingGeneralObsCov.test_predicted_state_covc                 C   s   t | jj| jj d S r]   r^   rA   r;   r;   r<   r`   o  s    z-MultivariateMissingGeneralObsCov.test_loglikec                 C   s   t | jj| jjd d S ra   rc   rA   r;   r;   r<   re   u  s
     z5MultivariateMissingGeneralObsCov.test_smoothed_statesc                 C   s   t | jj| jjd d S rf   rg   rA   r;   r;   r<   ri   {  s
     z9MultivariateMissingGeneralObsCov.test_smoothed_states_covc                 C   s   t | jj| jjd d S rS   rj   rA   r;   r;   r<   rl     s
     zFMultivariateMissingGeneralObsCov.test_smoothed_measurement_disturbancec                 C   s(   | j }| j}t|j |j d d S rS   rm   rp   r;   r;   r<   rs     s     zJMultivariateMissingGeneralObsCov.test_smoothed_measurement_disturbance_covc                 C   s   t | jj| jjdd d S rt   rv   rA   r;   r;   r<   rx     s
    z@MultivariateMissingGeneralObsCov.test_smoothed_state_disturbancec                 C   s   t | jj| jjd d S rS   ry   rA   r;   r;   r<   r{     s
     zDMultivariateMissingGeneralObsCov.test_smoothed_state_disturbance_covc                 C   s   t | jj| jjd d S rS   r|   rA   r;   r;   r<   r~     s
     z?MultivariateMissingGeneralObsCov.test_simulation_smoothed_statec                 C   s   t | jj| jjd d S rS   r   rA   r;   r;   r<   r     s
     zQMultivariateMissingGeneralObsCov.test_simulation_smoothed_measurement_disturbancec                 C   s   t | jj| jjd d S rS   r   rA   r;   r;   r<   r     s
     zKMultivariateMissingGeneralObsCov.test_simulation_smoothed_state_disturbanceN)r   r   r   r   r   r=   rC   rH   rK   rM   rR   rV   rY   r\   r`   re   ri   pytestmarkskiprl   rs   rx   r{   r~   r   r   r;   r;   r;   r<   r      s.   7


r   c                       s$   e Zd ZdZe fddZ  ZS )TestMultivariateGeneralObsCovz
    This class tests the univariate method when the observation covariance
    matrix is not diagonal and all data is available.

    Tests are against the conventional smoother.
    c                    s   t t| d d S )Nnone)r   r   r=   r   r   r;   r<   r=     s    z)TestMultivariateGeneralObsCov.setup_classr   r   r   r   r   r=   r   r;   r;   r   r<   r     s   r   c                       s$   e Zd ZdZe fddZ  ZS )'TestMultivariateAllMissingGeneralObsCovz
    This class tests the univariate method when the observation covariance
    matrix is not diagonal and there are cases of fully missing data only.

    Tests are against the conventional smoother.
    c                    s   t t| d d S )Nr   )r   r   r=   r   r   r;   r<   r=     s    z3TestMultivariateAllMissingGeneralObsCov.setup_classr   r;   r;   r   r<   r     s   r   c                       s4   e Zd ZdZe fddZdd Zdd Z  ZS )+TestMultivariatePartialMissingGeneralObsCovz
    This class tests the univariate method when the observation covariance
    matrix is not diagonal and there are cases of partially missing data only.

    Tests are against the conventional smoother.
    c                    s   t t| d d S )Nr   )r   r   r=   r   r   r;   r<   r=     s
    z7TestMultivariatePartialMissingGeneralObsCov.setup_classc                 C   s0   t | jjdd d f | jjdd d f d d S Nr   rO   rF   rA   r;   r;   r<   rH     s
     z:TestMultivariatePartialMissingGeneralObsCov.test_forecastsc                 C   s0   t | jjdd d f | jjdd d f d d S r   rI   rA   r;   r;   r<   rK     s
     z@TestMultivariatePartialMissingGeneralObsCov.test_forecasts_error	r   r   r   r   r   r=   rH   rK   r   r;   r;   r   r<   r     s
   r   c                       s4   e Zd ZdZe fddZdd Zdd Z  ZS ))TestMultivariateMixedMissingGeneralObsCovz
    This class tests the univariate method when the observation covariance
    matrix is not diagonal and there are cases of both partially missing and
    fully missing data.

    Tests are against the conventional smoother.
    c                    s   t t| d d S )Nr   )r   r   r=   r   r   r;   r<   r=     s
    z5TestMultivariateMixedMissingGeneralObsCov.setup_classc                 C   s0   t | jjdd d f | jjdd d f d d S r   rF   rA   r;   r;   r<   rH     s
     z8TestMultivariateMixedMissingGeneralObsCov.test_forecastsc                 C   s0   t | jjdd d f | jjdd d f d d S r   rI   rA   r;   r;   r<   rK     s
     z>TestMultivariateMixedMissingGeneralObsCov.test_forecasts_errorr   r;   r;   r   r<   r     s
   r   c                   @   s   e Zd Zed'ddZdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zdd Zdd Zejjdd Zejjdd Zdd Zdd Zd d! Zejjd"d# Zd$d% Zd&S )(TestMultivariateVARr   c                 K   sn  t jtdd}t|| _tj	 j
}tjdddd|_|ddd	g  jd
d  }|dkrtj|jd dd d f< tj|jddd d f< n|dkrtj|jdddf< tj|jdddf< nn|dkr0tj|jdddf< tj|jddd
f< tj|jdddf< tj|jdddf< tj|jdddf< t|fddd|}td|d< tdddgdddgdddgg|d< tdd d!gd"d#d$gd%d&d'gg|d(< td|d)< td*d+d,gd+d-d.gd,d.d/gg|d0< |d1 |j| _d2| j_| j | _| jj| jj | jj }| jjt|t| jjd3| _ d2| j_!| j | _"| jjt|t| jjd3| _#d S )4Nr   r   r   r   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r!   g@g        g=z4
?g%'Q+_@r&   g^g
G?g
{vX?gίE.gXOE@gƹ?gja?g4CϿg8ur"   r$   g1=NC@g̿Q%@gT|l@g+=}@gpqQ@g0Z$@r'   r   Tr   )$r   r   r   r   r   r   r   r   r   r   r	   r   r   r   r   r   r   r   r#   r%   r   r   r    r,   r-   r.   r/   r0   r1   r2   r(   r   r3   r4   r5   r6   )r7   r   r9   r   r   r   r   r:   r;   r;   r<   r=     sp    
 




zTestMultivariateVAR.setup_classc                 C   s0   t | jjdd d f | jjdd d f d d S rD   rF   rA   r;   r;   r<   rH   A  s
     z"TestMultivariateVAR.test_forecastsc                 C   s0   t | jjdd d f | jjdd d f d d S rD   rI   rA   r;   r;   r<   rK   G  s
     z(TestMultivariateVAR.test_forecasts_errorc                 C   s4   t | jjddd d f | jjddd d f d d S rD   rL   rA   r;   r;   r<   rM   M  s
     z,TestMultivariateVAR.test_forecasts_error_covc                 C   s   t | jj| jjd d S rN   rP   rA   r;   r;   r<   rR   S  s
     z'TestMultivariateVAR.test_filtered_statec                 C   s   t | jj| jjd d S rS   rT   rA   r;   r;   r<   rV   Y  s
     z+TestMultivariateVAR.test_filtered_state_covc                 C   s   t | jj| jjd d S rN   rW   rA   r;   r;   r<   rY   _  s
     z(TestMultivariateVAR.test_predicted_statec                 C   s   t | jj| jjd d S rS   rZ   rA   r;   r;   r<   r\   e  s
     z,TestMultivariateVAR.test_predicted_state_covc                 C   s   t | jj| jj d S r]   r^   rA   r;   r;   r<   r`   k  s    z TestMultivariateVAR.test_loglikec                 C   s   t | jj| jj d S r]   )r   r.   rd   r5   rA   r;   r;   r<   re   q  s    z(TestMultivariateVAR.test_smoothed_statesc                 C   s   t | jj| jjdd d S )Ng&.>ru   )r   r.   rh   r5   rA   r;   r;   r<   ri   w  s
     z,TestMultivariateVAR.test_smoothed_states_covc                 C   s   t | jj| jjd d S rS   rj   rA   r;   r;   r<   rl   }  s
     z9TestMultivariateVAR.test_smoothed_measurement_disturbancec                 C   s*   | j j}| j}t|j |j d d S rS   )rB   r.   r5   r   rn   ro   rp   r;   r;   r<   rs     s    z=TestMultivariateVAR.test_smoothed_measurement_disturbance_covc                 C   s   t | jj| jjdd d S rt   rv   rA   r;   r;   r<   rx     s
    z3TestMultivariateVAR.test_smoothed_state_disturbancec                 C   s   t | jj| jjd d S rS   ry   rA   r;   r;   r<   r{     s
     z7TestMultivariateVAR.test_smoothed_state_disturbance_covc                 C   s   t | jj| jjd d S rS   r|   rA   r;   r;   r<   r~     s
     z2TestMultivariateVAR.test_simulation_smoothed_statec                 C   s   t | jj| jjd d S rS   r   rA   r;   r;   r<   r     s
     zDTestMultivariateVAR.test_simulation_smoothed_measurement_disturbancec                 C   s   t | jj| jjd d S rS   r   rA   r;   r;   r<   r     s
     z>TestMultivariateVAR.test_simulation_smoothed_state_disturbanceN)r   )r   r   r   r   r=   rH   rK   rM   rR   rV   rY   r\   r`   re   ri   r   r   r   rl   rs   rx   r{   r~   r   r   r;   r;   r;   r<   r      s,   ?

	
r   c            	   	   C   s>  t dddddddg} t d}d|dd df< d|ddd f< t| d	d
d}|dddg ||jd< |j }t| d	d
d}d
|j_|dddg ||jd< |j }|j|j	 |j
 }|jt |t |jd}|jt |t |jd}t|jdd d f |jdd d f  t|jdd d f |jdd d f  t|jddd d f |jddd d f  t|j|j t|j|j t|j|j t|j|j t|j|j t|j|j t|j|j t|j|j t|j |j  t|j|j t|j|j t|j|j t|j|j t|j |j  d S )N
   r   g      @g      ?)r   r   rb   g      ?.r   )r   r   r   T)orderZmeasurement_errorg       @g      ?r"   r   r   )!r   r%   Zonesr   updater   r-   r4   r/   r0   r1   r2   r(   r   r   rG   rJ   r@   rQ   rU   rX   r[   r_   rd   rh   rk   rn   ro   rw   rz   r}   r   r   )	Zendogr"   Zmod1Zres1Zmod2Zres2r:   Zsim1Zsim2r;   r;   r<   test_time_varying_transition  sj    






&&
r   ) r   r   Znumpyr   Znumpy.testingr   r   Zpandasr   r   Zstatsmodelsr   Z#statsmodels.tsa.statespace.mlemodelr   Z(statsmodels.tsa.statespace.tests.resultsr   Z"statsmodels.tsa.statespace.sarimaxr   r   dirnameabspath__file__r   r   r   r   r   r   r   r   r   r   r;   r;   r;   r<   <module>   s8    T
 8


 0