Spaces:
Running on Zero
Running on Zero
Download tests/test_baselines.py from Segizu/timestral-backtest-lab: direct link, hf CLI and curl.
- Browser
- Download file 1.43 kB
-
https://huggingface.co/spaces/Segizu/timestral-backtest-lab/resolve/main/tests/test_baselines.py
- Command line
-
hf download hf://spaces/Segizu/timestral-backtest-lab/tests/test_baselines.py
-
curl -L -o test_baselines.py https://huggingface.co/spaces/Segizu/timestral-backtest-lab/resolve/main/tests/test_baselines.py
1.43 kB
| """Offline tests for baseline forecasts and no-leakage holdout split.""" | |
| from __future__ import annotations | |
| import numpy as np | |
| import pytest | |
| from lab.baselines import holdout_split, last_value_forecast, seasonal_naive_forecast | |
| def test_holdout_split_no_leakage() -> None: | |
| values = np.arange(100, dtype=float) | |
| train, holdout = holdout_split(values, holdout=10) | |
| assert len(train) == 90 | |
| assert len(holdout) == 10 | |
| assert np.array_equal(train, values[:90]) | |
| assert np.array_equal(holdout, values[90:]) | |
| # Baselines must only see train. | |
| lv = last_value_forecast(train, horizon=10) | |
| assert lv[0] == train[-1] | |
| assert np.all(lv == train[-1]) | |
| def test_seasonal_naive_ordering() -> None: | |
| # Pattern repeats every 4: 1,2,3,4,1,2,3,4,... | |
| season = 4 | |
| train = np.tile(np.array([1.0, 2.0, 3.0, 4.0]), 5) # 20 points | |
| pred = seasonal_naive_forecast(train, horizon=6, seasonality=season) | |
| # Next values after ...1,2,3,4 should be 1,2,3,4,1,2 | |
| assert np.allclose(pred, [1.0, 2.0, 3.0, 4.0, 1.0, 2.0]) | |
| def test_seasonal_naive_requires_full_season() -> None: | |
| train = np.arange(5, dtype=float) | |
| with pytest.raises(ValueError): | |
| seasonal_naive_forecast(train, horizon=3, seasonality=7) | |
| def test_last_value_shape() -> None: | |
| train = np.array([3.0, 5.0, 8.0]) | |
| pred = last_value_forecast(train, horizon=4) | |
| assert pred.shape == (4,) | |
| assert np.all(pred == 8.0) | |