@@ -498,21 +498,47 @@ def test_alpha_beta_constraint(self):
498498 with pytest .raises (ValueError ):
499499 pl .DataFrame ({"r" : [0.01 ] * 10 }).select (garch_forecast ("r" , alpha = 0.5 , beta = 0.6 ))
500500
501- def test_forecast_ignores_future_returns (self ):
501+ @pytest .mark .parametrize ("horizon" , [1 , 2 , 5 ])
502+ @pytest .mark .parametrize ("lazy" , [False , True ])
503+ def test_forecast_ignores_future_returns (self , horizon , lazy ):
502504 """A later shock must not change forecasts made before it is observed."""
503505 from ml4t .engineer .features .volatility .garch_forecast import garch_forecast
504506
505507 calm = np .zeros (30 )
506508 shocked = calm .copy ()
507509 shocked [10 ] = 4.0
508- calm_forecast = pl .DataFrame ({"returns" : calm }).select (garch_forecast ("returns" ))["returns" ]
509- shocked_forecast = pl .DataFrame ({"returns" : shocked }).select (garch_forecast ("returns" ))[
510- "returns"
511- ]
510+ calm_frame = pl .DataFrame ({"returns" : calm })
511+ shocked_frame = pl .DataFrame ({"returns" : shocked })
512+ if lazy :
513+ calm_frame = calm_frame .lazy ()
514+ shocked_frame = shocked_frame .lazy ()
515+ calm_result = calm_frame .select (garch_forecast ("returns" , horizon = horizon ))
516+ shocked_result = shocked_frame .select (garch_forecast ("returns" , horizon = horizon ))
517+ if lazy :
518+ calm_result = calm_result .collect ()
519+ shocked_result = shocked_result .collect ()
520+ calm_forecast = calm_result ["returns" ]
521+ shocked_forecast = shocked_result ["returns" ]
512522
513523 np .testing .assert_allclose (shocked_forecast [:10 ], calm_forecast [:10 ])
514524 assert shocked_forecast [10 ] > shocked_forecast [9 ]
515525
526+ @pytest .mark .parametrize ("horizon" , [1 , 2 , 5 ])
527+ @pytest .mark .parametrize ("lazy" , [False , True ])
528+ def test_forecast_uses_current_return_and_correct_horizon (self , horizon , lazy ):
529+ from ml4t .engineer .features .volatility .garch_forecast import garch_forecast
530+
531+ frame = pl .DataFrame ({"returns" : [0.1 , - 0.2 , 0.4 ]})
532+ if lazy :
533+ frame = frame .lazy ()
534+ selected = frame .select (
535+ garch_forecast ("returns" , omega = 0.01 , alpha = 0.2 , beta = 0.3 , horizon = horizon )
536+ )
537+ result = selected .collect () if lazy else selected
538+ one_step_variances = np .array ([0.015 , 0.0225 , 0.04875 ])
539+ expected = np .sqrt (0.02 + 0.5 ** (horizon - 1 ) * (one_step_variances - 0.02 ))
540+ np .testing .assert_allclose (result ["returns" ].to_numpy (), expected , rtol = 1e-12 )
541+
516542 def test_responds_to_shocks (self , ohlcv_df ):
517543 """Conditional vol should increase after large returns."""
518544 from ml4t .engineer .features .volatility .garch_forecast import garch_forecast
0 commit comments