U
    ÃmœdA*  ã                   @   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__ÚnpÚeyeZinitialize_approximate_diffuse©ÚselfÚendogÚkwargsr   r   ©Ú	__class__© úe/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/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rÂt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< qÈd S )Nz results/results_intercepts_R.csvz
1959-01-01z	2009-7-01ZQS)ÚstartÚendÚfreqZrealgdpZrealconsZrealinvÚallé2   é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ÚresultsZ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   ÚsumrG   Ú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 )NÚr1Úr2Zr3)r   rG   Ú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   rG   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   rG   Ú	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   rG   Ú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   rG   Ú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 )Nr8   Za1Za2Úa3)r   rG   Ú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   rG   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   rG   Ú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   rG   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   rG   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   rG   Ú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   rG   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   rG   Ú 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   rG   Ú$smoothed_measurement_disturbance_covrg   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_   rc   re   rh   rk   rl   rn   ro   rp   rr   rs   ru   rw   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 )Nr8   r;   r
   )r   r   r   )r   r   r   )r   r8   r8   )r   ry   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ç      à?r8   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   r8   )r   r   r   )r   ry   r"   r#   r   r   r   r"   Ý   s    zLargeStateCovAR1.updater%   r   r   r   r   ry   Ä   s   


ry   c                   C   s   t ttt d¡ƒ d S )Né
   )r   Ú
ValueErrorry   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 )
Nz TODO: 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Ó¿gš™™™™™¹¿g      ø?r{   gš™™™™™é?r8   )ÚpytestÚskipr   ZSARIMAXrF   Úres_desiredry   Ú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 )Nr8   r;   )r;   r|   )r8   r|   )r   rƒ   Zfilter_resultsr   r   rq   Ú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ƒ   r\   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ƒ   rb   r‚   r   r   r   r   rc     s    z#TestLargeStateCovAR1.test_forecastsc                 C   s   t | jj| jjƒ d S r†   )r   rƒ   rd   r‚   r   r   r   r   re     s    ÿz)TestLargeStateCovAR1.test_forecasts_errorc                 C   s   t | jj| jjƒ d S r†   )r   rƒ   rf   r‚   r   r   r   r   rh     s    ÿz-TestLargeStateCovAR1.test_forecasts_error_covc                 C   s   t | jjd | jjd ƒ d S r‡   )r   rƒ   rj   r‚   r   r   r   r   rk     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   rl   #  s    
ÿz.TestLargeStateCovAR1.test_predicted_states_covc                 C   s   t | jjd | jjd ƒ d S r‡   )r   rƒ   rm   r‚   r   r   r   r   rn   '  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   ro   +  s    
ÿz-TestLargeStateCovAR1.test_smoothed_states_covc                 C   s0   t | jjd | jjd ƒ t | jjd dƒ d S )Nr   r8   )r   rƒ   rq   r‚   r   r   r   r   rr   /  s    
ÿz4TestLargeStateCovAR1.test_smoothed_state_disturbancec                 C   s0   t | jjd | jjd ƒ t | jjd dƒ d S )Nr‰   )r8   r8   r   )r   rƒ   rP   r‚   rq   r   r   r   r   rs   4  s    
ÿz8TestLargeStateCovAR1.test_smoothed_state_disturbance_covc                 C   s   t | jj| jjƒ d S r†   )r   rƒ   rt   r‚   r   r   r   r   ru   9  s    ÿz:TestLargeStateCovAR1.test_smoothed_measurement_disturbancec                 C   s   t | jj| jjƒ d S r†   )r   rƒ   rv   r‚   r   r   r   r   rw   =  s    ÿz>TestLargeStateCovAR1.test_smoothed_measurement_disturbance_covN)r&   r'   r(   rx   rV   r…   rY   r^   r_   rc   re   rh   rk   rl   rn   ro   rr   rs   ru   rw   r   r   r   r   r   è   s"   

r   )r)   Ú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,   ry   r~   r   r   r   r   r   Ú<module>   s      