U
    KvfA*                     @   s   d Z ddlZddlZddlZddlZddlmZm	Z	 ddl
mZ ddlmZmZmZ ejejeZG dd dejZG dd	 d	ZG d
d dejZdd ZG dd dZdS )zM
Tests for miscellaneous models

Author: Chad Fulton
License: Simplified-BSD
    N)mlemodelsarimax)datasets)assert_equalassert_allcloseassert_raisesc                       sD   e Zd ZdZ fddZedd Zedd Z fdd	Z  Z	S )

Interceptszp
    Test class for observation and state intercepts (which usually do not
    get tested in other models).
    c                    sx   d}d}t t| j|f||d| td| d< td| d< td| d< td| d< td| d< |   d S )N   k_statesk_posdefdesignZobs_cov
transition	selection	state_cov)superr   __init__npZeyeZinitialize_approximate_diffuseselfendogkwargsr   r   	__class__ P/tmp/pip-unpacked-wheel-2v6byqio/statsmodels/tsa/statespace/tests/test_models.pyr      s     
 zIntercepts.__init__c                 C   s   ddddddgS )Nzd.1zd.2zd.3zc.1zc.2zc.3r   r   r   r   r   param_names&   s    zIntercepts.param_namesc                 C   s
   t dS )N   )r   aranger   r   r   r   start_params*   s    zIntercepts.start_paramsc                    s8   t t| j|f|}|d d | d< |dd  | d< d S )Nr	   Zobs_interceptZstate_intercept)r   r   updater   paramsr   r   r   r   r!   .   s    zIntercepts.update
__name__
__module____qualname____doc__r   propertyr   r    r!   __classcell__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dd Zdd Zdd Zdd Zd d! Zd"S )$TestInterceptsmixedc                 K   s  t tj d }t|| _tj j	}tj
dddd|_|dddg  }||  }|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|}tdd | _|| _|j| jdd| _td| jjf| j_td| jjf| j_td| jjf| j_td| jjf| j_t| jjD ]}tj| jj d d d d |f | jjd|f< tj| jj!d d d d |d f | jjd|f< tj| jj"d d d d |f | jjd|f< tj| jj#d d d d |f | jjd|f< qd S )Nz results/results_intercepts_R.csvz
1959-01-01z	2009-7-01ZQS)startendfreqZrealgdpZrealconsZrealinvall2   w      partialr   r,      F      '   Z      r   T)Z
return_ssm)$current_pathosseppdZread_csvdesiredr   Z	macrodataZload_pandasdataZ
date_rangeindexcopyZstdr   nanZilocr   r   r#   modelsmoothresultszerosZnobs!det_scaled_smoothed_estimator_covdet_predicted_state_covdet_smoothed_state_cov"det_smoothed_state_disturbance_covrangeZlinalgZdetscaled_smoothed_estimator_covpredicted_state_covsmoothed_state_covsmoothed_state_disturbance_cov)clswhichr   pathZdtaZobsmodir   r   r   setup_class6   sZ    
zTestIntercepts.setup_classc                 C   s   t t| jjd d S )NgY	)r   r   sumrF   llf_obsr   r   r   r   test_loglikel   s    zTestIntercepts.test_loglikec                 C   s    t | jjj| jdddg  d S )NZr1Zr2Zr3)r   rF   scaled_smoothed_estimatorTr?   r   r   r   r   test_scaled_smoothed_estimatoro   s    z-TestIntercepts.test_scaled_smoothed_estimatorc                 C   s   t | jjj| jdg  d S )NZdetN)r   rF   rH   r[   r?   r   r   r   r   "test_scaled_smoothed_estimator_covu   s    
z1TestIntercepts.test_scaled_smoothed_estimator_covc                 C   s    t | jjj| jdddg  d S )Nm1m2Zm3)r   rF   	forecastsr[   r?   r   r   r   r   test_forecasts{   s    zTestIntercepts.test_forecastsc                 C   s    t | jjj| jdddg  d S )NZv1Zv2Zv3)r   rF   forecasts_errorr[   r?   r   r   r   r   test_forecasts_error   s    z#TestIntercepts.test_forecasts_errorc                 C   s"   t | jj | jdddg  d S )NZF1ZF2ZF3)r   rF   forecasts_error_covdiagonalr?   r   r   r   r   test_forecasts_error_cov   s    
z'TestIntercepts.test_forecasts_error_covc                 C   s0   t | jjd d dd f j| jdddg  d S )Nr7   Za1Za2a3)r   rF   predicted_stater[   r?   r   r   r   r   test_predicted_states   s    z$TestIntercepts.test_predicted_statesc                 C   s   t | jjj| jdg  d S )NZdetP)r   rF   rI   r[   r?   r   r   r   r   test_predicted_states_cov   s    
z(TestIntercepts.test_predicted_states_covc                 C   s    t | jjj| jdddg  d S )NZ	alphahat1Z	alphahat2Z	alphahat3)r   rF   smoothed_stater[   r?   r   r   r   r   test_smoothed_states   s    z#TestIntercepts.test_smoothed_statesc                 C   s   t | jjj| jdg  d S )NZdetV)r   rF   rJ   r[   r?   r   r   r   r   test_smoothed_states_cov   s    
z'TestIntercepts.test_smoothed_states_covc                 C   s    t | jjj| jdddg  d S )NZmuhat1Zmuhat2Zmuhat3)r   rF   Zsmoothed_forecastsr[   r?   r   r   r   r   test_smoothed_forecasts   s    z&TestIntercepts.test_smoothed_forecastsc                 C   s    t | jjj| jdddg  d S )NZetahat1Zetahat2Zetahat3)r   rF   smoothed_state_disturbancer[   r?   r   r   r   r   test_smoothed_state_disturbance   s    z.TestIntercepts.test_smoothed_state_disturbancec                 C   s   t | jjj| jdg  d S )NZdetVeta)r   rF   rK   r[   r?   r   r   r   r   #test_smoothed_state_disturbance_cov   s    
z2TestIntercepts.test_smoothed_state_disturbance_covc                 C   s$   t | jjj| jdddg dd d S )NZepshat1Zepshat2Zepshat3g&.>)Zatol)r   rF    smoothed_measurement_disturbancer[   r?   r   r   r   r   %test_smoothed_measurement_disturbance   s
     z4TestIntercepts.test_smoothed_measurement_disturbancec                 C   s"   t | jj | jdddg  d S )NZVeps1ZVeps2ZVeps3)r   rF   $smoothed_measurement_disturbance_covre   r?   r   r   r   r   )test_smoothed_measurement_disturbance_cov   s    
z8TestIntercepts.test_smoothed_measurement_disturbance_covN)r,   )r%   r&   r'   classmethodrV   rY   r\   r]   ra   rc   rf   ri   rj   rl   rm   rn   rp   rq   rs   ru   r   r   r   r   r+   5   s"   5r+   c                       sD   e Zd ZdZ fddZedd Zedd Z fdd	Z  Z	S )
LargeStateCovAR1z
    Test class for k_posdef > k_states (which usually do not get tested in
    other models).

    This is just an AR(1) model with an extra unused state innovation
    c                    sJ   d}d}t t| j|f||d| d| d< d| d< d| d< |   d S )Nr7   r:   r
   )r   r   r   )r   r   r   )r   r7   r7   )r   rw   r   Zinitialize_stationaryr   r   r   r   r      s    
 zLargeStateCovAR1.__init__c                 C   s   ddgS )NphiZsigma2r   r   r   r   r   r      s    zLargeStateCovAR1.param_namesc                 C   s   ddgS )N      ?r7   r   r   r   r   r   r       s    zLargeStateCovAR1.start_paramsc                    s0   t t| j|f|}|d | d< |d | d< d S )Nr   )r   r   r   r7   )r   r   r   )r   rw   r!   r"   r   r   r   r!      s    zLargeStateCovAR1.updater$   r   r   r   r   rw      s   


rw   c                   C   s   t tttd d S )N
   )r   
ValueErrorrw   r   r   r   r   r   r   test_large_kposdef   s    r|   c                   @   s   e Zd Ze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 )"TestLargeStateCovAR1c              
   C   sX   t d ddddddddddg
}dd	g}t|}||| _t|}||| _d S )
NzTODO: This test is skipped since an exception is currently raised if k_posdef > k_states. However, this test could be used if models of those types were allowedg?g      g333333ӿgg      ?ry   g?r7   )pytestskipr   ZSARIMAXrE   res_desiredrw   res)rQ   r   r#   Zmod_desiredrT   r   r   r   rV      s    
z TestLargeStateCovAR1.setup_classc                 C   sd   t | jjjd t | jjjd t | jjjd t | jjjd t | jjjd t | jjjd d S )Nr7   r:   )r:   rz   )r7   rz   )r   r   Zfilter_resultsr   r   ro   shaper   r   r   r   r   test_dimensions   s    
z$TestLargeStateCovAR1.test_dimensionsc                 C   s   t | jj| jj d S N)r   r   rX   r   r   r   r   r   rY   	  s    z!TestLargeStateCovAR1.test_loglikec                 C   s   t | jjd | jjd  d S Nr   )r   r   rZ   r   r   r   r   r   r\     s    
z3TestLargeStateCovAR1.test_scaled_smoothed_estimatorc                 C   s   t | jjd | jjd  d S r   )r   r   rM   r   r   r   r   r   r]     s    
z7TestLargeStateCovAR1.test_scaled_smoothed_estimator_covc                 C   s   t | jj| jj d S r   )r   r   r`   r   r   r   r   r   ra     s    z#TestLargeStateCovAR1.test_forecastsc                 C   s   t | jj| jj d S r   )r   r   rb   r   r   r   r   r   rc     s    z)TestLargeStateCovAR1.test_forecasts_errorc                 C   s   t | jj| jj d S r   )r   r   rd   r   r   r   r   r   rf     s    z-TestLargeStateCovAR1.test_forecasts_error_covc                 C   s   t | jjd | jjd  d S r   )r   r   rh   r   r   r   r   r   ri     s    
z*TestLargeStateCovAR1.test_predicted_statesc                 C   s   t | jjd | jjd  d S Nr   r   )r   r   rN   r   r   r   r   r   rj   #  s    
z.TestLargeStateCovAR1.test_predicted_states_covc                 C   s   t | jjd | jjd  d S r   )r   r   rk   r   r   r   r   r   rl   '  s    
z)TestLargeStateCovAR1.test_smoothed_statesc                 C   s   t | jjd | jjd  d S r   )r   r   rO   r   r   r   r   r   rm   +  s    
z-TestLargeStateCovAR1.test_smoothed_states_covc                 C   s0   t | jjd | jjd  t | jjd d d S )Nr   r7   )r   r   ro   r   r   r   r   r   rp   /  s    
z4TestLargeStateCovAR1.test_smoothed_state_disturbancec                 C   s0   t | jjd | jjd  t | jjd d d S )Nr   )r7   r7   r   )r   r   rP   r   ro   r   r   r   r   rq   4  s    
z8TestLargeStateCovAR1.test_smoothed_state_disturbance_covc                 C   s   t | jj| jj d S r   )r   r   rr   r   r   r   r   r   rs   9  s    z:TestLargeStateCovAR1.test_smoothed_measurement_disturbancec                 C   s   t | jj| jj d S r   )r   r   rt   r   r   r   r   r   ru   =  s    z>TestLargeStateCovAR1.test_smoothed_measurement_disturbance_covN)r%   r&   r'   rv   rV   r   rY   r\   r]   ra   rc   rf   ri   rj   rl   rm   rp   rq   rs   ru   r   r   r   r   r}      s"   

r}   )r(   Znumpyr   Zpandasr>   r<   r~   Zstatsmodels.tsa.statespacer   r   Zstatsmodelsr   Znumpy.testingr   r   r   rS   dirnameabspath__file__r;   ZMLEModelr   r+   rw   r|   r}   r   r   r   r   <module>   s      