
    !g)                     B   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                   j#                  ej                   j%                  e            Z G d dej*                        Z G d d	      Z G d
 dej*                        Zd Z G d d      Zy)zM
Tests for miscellaneous models

Author: Chad Fulton
License: Simplified-BSD
    N)mlemodelsarimax)datasets)assert_equalassert_allcloseassert_raisesc                   L     e Zd ZdZ fdZed        Zed        Z fdZ xZ	S )
Interceptszp
    Test class for observation and state intercepts (which usually do not
    get tested in other models).
    c                 D   d}d}t        |   |f||d| t        j                  d      | d<   t        j                  d      | d<   t        j                  d      | d<   t        j                  d      | d<   t        j                  d      | d<   | j	                          y )N   k_statesk_posdefdesignobs_cov
transition	selection	state_cov)super__init__npeyeinitialize_approximate_diffuseselfendogkwargsr   r   	__class__s        i/var/www/dash_apps/app1/venv/lib/python3.12/site-packages/statsmodels/tsa/statespace/tests/test_models.pyr   zIntercepts.__init__   s    	C$x	C;A	CX&&)YVVAY\FF1I[FF1I[++-    c                 
    g dS )N)zd.1zd.2zd.3zc.1zc.2zc.3 r   s    r   param_nameszIntercepts.param_names&   s    99r    c                 ,    t        j                  d      S )N   )r   aranger#   s    r   start_paramszIntercepts.start_params*   s    yy|r    c                 F    t        |   |fi |}|d d | d<   |dd  | d<   y )Nr   obs_interceptstate_interceptr   updater   paramsr   r   s      r   r-   zIntercepts.update.   s7    1&1 &r
_"(*r    
__name__
__module____qualname____doc__r   propertyr$   r(   r-   __classcell__r   s   @r   r
   r
      s?    
. : :  - -r    r
   c                   x    e Zd Zedd       Zd Zd Zd Zd Zd Z	d Z
d Zd	 Zd
 Zd Zd Zd Zd Zd Zd Zy)TestInterceptsc                 8	   t         t        j                  z   dz   }t        j                  |      | _        t        j                  j                         j                  }t        j                  ddd      |_        |g d   j                         }||j                         z  }|dk(  rGt        j                  |j                   d dd d f<   t        j                  |j                   d	d
d d f<   n|dk(  rCt        j                  |j                   dddf<   t        j                  |j                   d	d
df<   n|dk(  rt        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i |}t        j$                  d      dz   | _        || _        |j+                  | j&                  d      | _        t        j.                  d| j(                  j0                  f      | j,                  _        t        j.                  d| j(                  j0                  f      | j,                  _        t        j.                  d| j(                  j0                  f      | j,                  _        t        j.                  d| j(                  j0                  f      | j,                  _        t;        | j(                  j0                        D ]^  }t        j<                  j?                  | j,                  j@                  d d d d |f         | j,                  j2                  d|f<   t        j<                  j?                  | j,                  jB                  d d d d |dz   f         | j,                  j4                  d|f<   t        j<                  j?                  | j,                  jD                  d d d d |f         | j,                  j6                  d|f<   t        j<                  j?                  | j,                  jF                  d d d d |f         | j,                  j8                  d|f<   a y )Nz results/results_intercepts_R.csvz
1959-01-01z	2009-7-01QS)startendfreq)realgdprealconsrealinvall2   w      partialr   mixed   F      '   Z      r&   T)
return_ssm)$current_pathosseppdread_csvdesiredr   	macrodataload_pandasdata
date_rangeindexcopystdr   nanilocr
   r'   r/   modelsmoothresultszerosnobs!det_scaled_smoothed_estimator_covdet_predicted_state_covdet_smoothed_state_cov"det_smoothed_state_disturbance_covrangelinalgdetscaled_smoothed_estimator_covpredicted_state_covsmoothed_state_covsmoothed_state_disturbance_cov)clswhichr   pathdtaobsmodis           r   setup_classzTestIntercepts.setup_class6   su    bff$'IIkk$'   ,,.33MM+'+-	45::<CGGIoE>!vvCHHSbS!V#%66CHHSWaZ i "CHHQrT1W#%66CHHSWaZ g "CHHQrT1W!#CHHRUAX!#CHHRUAX#%66CHHSWaZ #%66CHHSWaZ ''YYq\A%
	jjj=
 HHa() 	5.0hh399>>7J.K+-/XXq#))..6I-J*HHa() 	6 syy~~& 
	JA		KK==aAgFH KK99!Q$? 9;		//1ac	:9<CKK//1579yy}}..q!Qw789CKK..q!t4 		KK>>q!QwGI KK::1a4@
	Jr    c                 j    t        t        j                  | j                  j                        d       y )NgY	)r   r   sumr`   llf_obsr#   s    r   test_loglikezTestIntercepts.test_loglikel   s    t||334nEr    c                 v    t        | j                  j                  j                  | j                  g d          y )N)r1r2r3)r   r`   scaled_smoothed_estimatorTrT   r#   s    r   test_scaled_smoothed_estimatorz-TestIntercepts.test_scaled_smoothed_estimatoro   s*    LL2244LL+,	
r    c                 t    t        | j                  j                  j                  | j                  dg          y )NdetN)r   r`   rc   r   rT   r#   s    r   "test_scaled_smoothed_estimator_covz1TestIntercepts.test_scaled_smoothed_estimator_covu   s+    LL::<<LL&"	
r    c                 v    t        | j                  j                  j                  | j                  g d          y )N)m1m2m3)r   r`   	forecastsr   rT   r#   s    r   test_forecastszTestIntercepts.test_forecasts{   s*    LL""$$LL+,	
r    c                 v    t        | j                  j                  j                  | j                  g d          y )N)v1v2v3)r   r`   forecasts_errorr   rT   r#   s    r   test_forecasts_errorz#TestIntercepts.test_forecasts_error   s*    LL((**LL+,	
r    c                 ~    t        | j                  j                  j                         | j                  g d          y )N)F1F2F3)r   r`   forecasts_error_covdiagonalrT   r#   s    r   test_forecasts_error_covz'TestIntercepts.test_forecasts_error_cov   s-    LL,,557LL+,	
r    c                     t        | j                  j                  d d dd f   j                  | j                  g d          y )NrJ   )a1a2a3)r   r`   predicted_stater   rT   r#   s    r   test_predicted_statesz$TestIntercepts.test_predicted_states   s5    LL((AB/11LL+,	
r    c                 t    t        | j                  j                  j                  | j                  dg          y )NdetP)r   r`   rd   r   rT   r#   s    r   test_predicted_states_covz(TestIntercepts.test_predicted_states_cov   s+    LL0022LL&"	
r    c                 v    t        | j                  j                  j                  | j                  g d          y )N)	alphahat1	alphahat2	alphahat3)r   r`   smoothed_stater   rT   r#   s    r   test_smoothed_statesz#TestIntercepts.test_smoothed_states   s*    LL''))LL@A	
r    c                 t    t        | j                  j                  j                  | j                  dg          y )NdetV)r   r`   re   r   rT   r#   s    r   test_smoothed_states_covz'TestIntercepts.test_smoothed_states_cov   s+    LL//11LL&"	
r    c                 v    t        | j                  j                  j                  | j                  g d          y )N)muhat1muhat2muhat3)r   r`   smoothed_forecastsr   rT   r#   s    r   test_smoothed_forecastsz&TestIntercepts.test_smoothed_forecasts   s*    LL++--LL78	
r    c                 v    t        | j                  j                  j                  | j                  g d          y )N)etahat1etahat2etahat3)r   r`   smoothed_state_disturbancer   rT   r#   s    r   test_smoothed_state_disturbancez.TestIntercepts.test_smoothed_state_disturbance   s*    LL3355LL:;	
r    c                 t    t        | j                  j                  j                  | j                  dg          y )NdetVeta)r   r`   rf   r   rT   r#   s    r   #test_smoothed_state_disturbance_covz2TestIntercepts.test_smoothed_state_disturbance_cov   s+    LL;;==LL)%	
r    c                 z    t        | j                  j                  j                  | j                  g d   d       y )N)epshat1epshat2epshat3g&.>)atol)r   r`    smoothed_measurement_disturbancer   rT   r#   s    r   %test_smoothed_measurement_disturbancez4TestIntercepts.test_smoothed_measurement_disturbance   s,    LL99;;LL:;$	
r    c                 ~    t        | j                  j                  j                         | j                  g d          y )N)Veps1Veps2Veps3)r   r`   $smoothed_measurement_disturbance_covr   rT   r#   s    r   )test_smoothed_measurement_disturbance_covz8TestIntercepts.test_smoothed_measurement_disturbance_cov   s-    LL==FFHLL45	
r    N)rG   )r1   r2   r3   classmethodru   ry   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r"   r    r   r9   r9   5   sc    3J 3JjF













r    r9   c                   L     e Zd ZdZ fdZed        Zed        Z fdZ x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                 r    d}d}t        |   |f||d| d| d<   d| d<   d| d<   | j                          y )NrJ   rM   r   )r   r   r   )r   r   r   )r   rJ   rJ   )r   r   initialize_stationaryr   s        r   r   zLargeStateCovAR1.__init__   s^    	C$x	C;A	C ^"#"#""$r    c                 
    ddgS )Nphisigma2r"   r#   s    r   r$   zLargeStateCovAR1.param_names   s    x  r    c                 
    ddgS )N      ?rJ   r"   r#   s    r   r(   zLargeStateCovAR1.start_params   s    Qxr    c                 F    t        |   |fi |}|d   | d<   |d   | d<   y )Nr   )r   r   r   rJ   )r   r   r   r,   r.   s      r   r-   zLargeStateCovAR1.update   s4    1&1#)!9 "()r    r0   r7   s   @r   r   r      s?    % ! !  , ,r    r   c                  T    t        t        t        t        j                  d             y )N
   )r   
ValueErrorr   r   r'   r"   r    r   test_large_kposdefr      s    *.		">r    c                   v    e Zd Zed        Zd Zd Zd Zd Zd Z	d Z
d Zd	 Zd
 Zd Zd Zd Zd Zd Zd Zy)TestLargeStateCovAR1c                     t        j                  d       g d}ddg}t        j                  |      }|j	                  |      | _        t        |      }|j	                  |      | _        y )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 allowed)
皙?g      333333ӿgg      ?r   r   r   r   g?r   rJ   )pytestskipr   SARIMAXr_   res_desiredr   res)rn   r   r/   mod_desiredrs   s        r   ru   z TestLargeStateCovAR1.setup_class   sd    9	
 E q ooe,%,,V4 u%**V$r    c                    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       y )NrJ   rM   )rM   r   )rJ   r   )r   r   filter_resultsr   r   r   shaper   r#   s    r   test_dimensionsz$TestLargeStateCovAR1.test_dimensions   s    TXX,,55q9TXX,,55q9TXX88>>HT%%44==qAT%%44==qAT%%@@FF	r    c                 l    t        | j                  j                  | j                  j                         y N)r   r   rx   r   r#   s    r   ry   z!TestLargeStateCovAR1.test_loglike	  s"    (($*:*:*B*BCr    c                 x    t        | j                  j                  d   | j                  j                  d          y Nr   )r   r   r~   r   r#   s    r   r   z3TestLargeStateCovAR1.test_scaled_smoothed_estimator  s0    ::1=((BB1E	Gr    c                 x    t        | j                  j                  d   | j                  j                  d          y r   )r   r   rj   r   r#   s    r   r   z7TestLargeStateCovAR1.test_scaled_smoothed_estimator_cov  s0    >>qA((FFqI	Kr    c                 l    t        | j                  j                  | j                  j                         y r   )r   r   r   r   r#   s    r   r   z#TestLargeStateCovAR1.test_forecasts  s"    **D,<,<,F,FGr    c                 l    t        | j                  j                  | j                  j                         y r   )r   r   r   r   r#   s    r   r   z)TestLargeStateCovAR1.test_forecasts_error  s%    00((88	:r    c                 l    t        | j                  j                  | j                  j                         y r   )r   r   r   r   r#   s    r   r   z-TestLargeStateCovAR1.test_forecasts_error_cov  s%    44((<<	>r    c                 x    t        | j                  j                  d   | j                  j                  d          y r   )r   r   r   r   r#   s    r   r   z*TestLargeStateCovAR1.test_predicted_states  s/    003((88;	=r    c                 x    t        | j                  j                  d   | j                  j                  d          y Nr   r   )r   r   rk   r   r#   s    r   r   z.TestLargeStateCovAR1.test_predicted_states_cov#  s0    44T:((<<TB	Dr    c                 x    t        | j                  j                  d   | j                  j                  d          y r   )r   r   r   r   r#   s    r   r   z)TestLargeStateCovAR1.test_smoothed_states'  s/    //2((77:	<r    c                 x    t        | j                  j                  d   | j                  j                  d          y r   )r   r   rl   r   r#   s    r   r   z-TestLargeStateCovAR1.test_smoothed_states_cov+  s0    33D9((;;DA	Cr    c                     t        | j                  j                  d   | j                  j                  d          t        | j                  j                  d   d       y )Nr   rJ   )r   r   r   r   r#   s    r   r   z4TestLargeStateCovAR1.test_smoothed_state_disturbance/  sI    ;;A>((CCAF	H;;A>Br    c                     t        | j                  j                  d   | j                  j                  d          t        | j                  j                  d   d       y )Nr   )rJ   rJ   r   )r   r   rm   r   r   r#   s    r   r   z8TestLargeStateCovAR1.test_smoothed_state_disturbance_cov4  sI    ??E((GGM	O;;DA1Er    c                 l    t        | j                  j                  | j                  j                         y r   )r   r   r   r   r#   s    r   r   z:TestLargeStateCovAR1.test_smoothed_measurement_disturbance9  s&    AA((II	Kr    c                 l    t        | j                  j                  | j                  j                         y r   )r   r   r   r   r#   s    r   r   z>TestLargeStateCovAR1.test_smoothed_measurement_disturbance_cov=  s&    EE((MM	Or    N)r1   r2   r3   r   ru   r   ry   r   r   r   r   r   r   r   r   r   r   r   r   r   r"   r    r   r   r      si    % %*DGKH:>=D<CC
F
KOr    r   )r4   numpyr   pandasrR   rP   r   statsmodels.tsa.statespacer   r   statsmodelsr   numpy.testingr   r   r   rp   dirnameabspath__file__rO   MLEModelr
   r9   r   r   r   r"   r    r   <module>r      s      	  8   F Fwwrwwx89-"" -@L
 L
^,x(( ,@?WO WOr    