Add probabilistic multi-horizon Torch forecaster
This commit is contained in:
@@ -124,6 +124,73 @@ def _write_multifeature_torch_gru_artifact(path, *, head_bias: float) -> None:
|
||||
)
|
||||
|
||||
|
||||
def _write_probabilistic_torch_gru_artifact(path) -> None:
|
||||
hidden_size = 2
|
||||
input_size = 2
|
||||
output_size = 10
|
||||
path.write_text(
|
||||
json.dumps(
|
||||
{
|
||||
"version": 4,
|
||||
"type": "pytorch_recurrent_forecaster",
|
||||
"target_horizon": 3,
|
||||
"target_horizons": [1, 3],
|
||||
"direct_horizon": True,
|
||||
"target_transform": "net_return_over_volatility",
|
||||
"round_trip_cost": 0.0026,
|
||||
"output_layout": ["mean", "q10", "q50", "q90", "logit_up"],
|
||||
"feature_count": input_size,
|
||||
"feature_names": ["return_1", "range_percent"],
|
||||
"symbols": {
|
||||
"BTCUSDT": {
|
||||
"model": "torch_gru",
|
||||
"architecture": "gru",
|
||||
"lookback": 8,
|
||||
"target_horizon": 3,
|
||||
"target_horizons": [1, 3],
|
||||
"direct_horizon": True,
|
||||
"target_transform": "net_return_over_volatility",
|
||||
"round_trip_cost": 0.0026,
|
||||
"output_layout": ["mean", "q10", "q50", "q90", "logit_up"],
|
||||
"input_size": input_size,
|
||||
"output_size": output_size,
|
||||
"feature_names": ["return_1", "range_percent"],
|
||||
"feature_means": [0.0, 0.0],
|
||||
"feature_scales": [0.001, 0.001],
|
||||
"target_means": [0.0, 0.0],
|
||||
"target_scales": [1.0, 1.0],
|
||||
"target_mean": 0.0,
|
||||
"target_scale": 1.0,
|
||||
"hidden_size": hidden_size,
|
||||
"num_layers": 1,
|
||||
"clip": 8.0,
|
||||
"validation_mae_percent": 0.01,
|
||||
"baseline_mae_percent": 0.08,
|
||||
"validation_mae_by_horizon": {"1": 0.001, "3": 0.0015},
|
||||
"baseline_mae_by_horizon": {"1": 0.002, "3": 0.003},
|
||||
"skill": 0.2,
|
||||
"attention_pooling": True,
|
||||
"attention_weight": [0.0, 0.0],
|
||||
"attention_bias": 0.0,
|
||||
"context_norm": True,
|
||||
"context_norm_weight": [1.0, 1.0],
|
||||
"context_norm_bias": [0.0, 0.0],
|
||||
"state_dict": {
|
||||
"weight_ih_l0": [[0.0, 0.0] for _ in range(3 * hidden_size)],
|
||||
"weight_hh_l0": [[0.0, 0.0] for _ in range(3 * hidden_size)],
|
||||
"bias_ih_l0": [0.0 for _ in range(3 * hidden_size)],
|
||||
"bias_hh_l0": [0.0 for _ in range(3 * hidden_size)],
|
||||
},
|
||||
"head_weight": [[0.0, 0.0] for _ in range(output_size)],
|
||||
"head_bias": [0.2, 0.05, 0.15, 0.35, 1.0, 0.35, 0.10, 0.30, 0.55, 2.0],
|
||||
},
|
||||
},
|
||||
}
|
||||
),
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
|
||||
def test_time_series_forecaster_requires_torch_artifact(make_settings, tmp_path) -> None:
|
||||
settings = make_settings(
|
||||
tmp_path,
|
||||
@@ -233,3 +300,25 @@ def test_time_series_forecaster_reads_multifeature_direct_horizon_artifact(make_
|
||||
assert forecast.horizon == 3
|
||||
assert 0.015 <= forecast.expected_return_percent <= 0.025
|
||||
assert forecast.volatility_model == "direct horizon validation MAE"
|
||||
|
||||
|
||||
def test_time_series_forecaster_reads_probabilistic_multi_horizon_artifact(make_settings, tmp_path) -> None:
|
||||
artifact_path = tmp_path / "lstm_forecaster.json"
|
||||
_write_probabilistic_torch_gru_artifact(artifact_path)
|
||||
settings = make_settings(
|
||||
tmp_path,
|
||||
time_series_min_candles=80,
|
||||
time_series_forecast_horizon=3,
|
||||
time_series_lstm_model_path=artifact_path,
|
||||
)
|
||||
returns = [0.0002 if index % 5 else -0.00007 for index in range(160)]
|
||||
|
||||
forecast = TimeSeriesForecaster(settings).forecast(_candles_from_returns(returns), symbol="BTCUSDT")
|
||||
|
||||
assert forecast.usable is True
|
||||
assert forecast.model == "torch_gru"
|
||||
assert forecast.horizon == 3
|
||||
assert forecast.target_transform == "net_return_over_volatility"
|
||||
assert forecast.probability_up > 0.85
|
||||
assert forecast.quantile_10_percent <= forecast.quantile_50_percent <= forecast.quantile_90_percent
|
||||
assert sorted(forecast.horizon_forecasts) == ["1", "3"]
|
||||
|
||||
Reference in New Issue
Block a user