forked from BobbyAxerol/quantbt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_optimization_integration.py
More file actions
370 lines (300 loc) · 13.8 KB
/
Copy pathtest_optimization_integration.py
File metadata and controls
370 lines (300 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
from __future__ import annotations
import json
import optuna
import pytest
import quantbt.walkforward as walkforward_module
from quantbt import (
CandidateSelector,
GenericEndpointEvaluator,
MissingOptimizationMetricError,
ObjectiveResult,
OptimizationConfig,
OptunaOptimizer,
ReportMetricObjective,
SamplerConfig,
SharpeObjective,
constraints_feasible,
max_turnover_constraint,
)
from quantbt.optimization.space import suggest_params
class Result:
def __init__(self, value, report=None, metadata=None):
self.value = float(value)
self._report = report
self.metadata = dict(metadata or {})
def full_report(self, trading_days=365, scope="auto"):
if self._report is not None:
return dict(self._report)
return {"sharpe": self.value, "max_drawdown_pct": abs(self.value), "num_trades": 1}
def test_optimizer_preserves_fixed_params_in_best_and_trial_records():
evaluator = GenericEndpointEvaluator(
build_run_inputs=lambda params: {"value": params["x"]},
run_func=lambda value: Result(value),
objective_builder=lambda result, params: ObjectiveResult.scalar(result.value, metrics={"x": result.value}),
)
optimizer = OptunaOptimizer(
evaluator=evaluator,
config=OptimizationConfig(study_name="fixed_params_integration", n_trials=4, seed=1, show_progress_bar=False),
sampler_config=SamplerConfig(name="random"),
)
result = optimizer.optimize(param_ranges={"x": [1, 2, 3]}, fixed_params={"issl": True}, candidate_selector=CandidateSelector())
assert result.best_params["issl"] is True
assert result.selected_params["issl"] is True
assert all(record.params.get("issl") is True for record in result.trials if record.state == "COMPLETE")
def test_walkforward_sampling_reuses_optimization_core_and_preserves_float_int_ranges():
trial = optuna.trial.FixedTrial(
{
"window": 3,
"threshold": 0.2,
"flag": True,
"mode": "fast",
}
)
ranges = {
"window": (1.0, 5.0, 1.0),
"threshold": (0.1, 0.5, 0.1),
"flag": [True, False],
"mode": ["fast", "slow"],
"constant": 7,
}
assert walkforward_module._sample_params(trial, ranges) == suggest_params(trial, ranges)
def test_constrained_optimization_and_feasible_candidate_selector():
evaluator = GenericEndpointEvaluator(
build_run_inputs=lambda params: {"value": float(params["x"])},
run_func=lambda value: Result(value),
objective_builder=lambda result, params: ObjectiveResult.scalar(
result.value,
metrics={"score": result.value},
constraints=(result.value - 1.0,),
),
)
optimizer = OptunaOptimizer(
evaluator=evaluator,
config=OptimizationConfig(study_name="constraints_integration", n_trials=4, seed=2, show_progress_bar=False),
sampler_config=SamplerConfig(name="grid", constraint_mode="post_filter"),
)
result = optimizer.optimize(param_ranges={"x": [0.0, 1.0, 2.0]}, candidate_selector=CandidateSelector("feasible_best"))
assert result.selected_params["x"] == 1.0
assert all(constraints_feasible(record.constraints) for record in result.trials if record.params.get("x") <= 1.0)
assert any(not constraints_feasible(record.constraints) for record in result.trials if record.params.get("x") > 1.0)
def test_missing_objective_metric_raises():
result = Result(1.0, report={"max_drawdown_pct": 1.0, "num_trades": 10})
with pytest.raises(MissingOptimizationMetricError, match="sharpe"):
SharpeObjective()(result, {})
def test_missing_constraint_metric_raises():
result = Result(1.0, report={"sharpe": 1.0, "max_drawdown_pct": 1.0, "num_trades": 10})
with pytest.raises(MissingOptimizationMetricError, match="turnover"):
ReportMetricObjective(constraints=(max_turnover_constraint(1.0),))(result, {})
def test_turnover_does_not_fallback_to_trade_count():
result = Result(1.0, report={"sharpe": 1.0, "max_drawdown_pct": 1.0, "num_trades": 99})
with pytest.raises(MissingOptimizationMetricError, match="turnover"):
ReportMetricObjective(value_metrics=("turnover",))(result, {})
def test_infeasible_highest_score_not_selected():
evaluator = GenericEndpointEvaluator(
build_run_inputs=lambda params: {"value": float(params["x"])},
run_func=lambda value: Result(value),
objective_builder=lambda result, params: ObjectiveResult.scalar(
result.value,
metrics={"score": result.value},
constraints=(result.value - 1.0,),
),
)
optimizer = OptunaOptimizer(
evaluator=evaluator,
config=OptimizationConfig(study_name="infeasible_best", n_trials=3, seed=1, show_progress_bar=False),
sampler_config=SamplerConfig(name="grid", constraint_mode="post_filter"),
)
raw = optimizer.optimize(param_ranges={"x": [0.0, 1.0, 2.0]})
filtered = optimizer.optimize(param_ranges={"x": [0.0, 1.0, 2.0]}, candidate_selector=CandidateSelector("feasible_best"))
assert raw.best_params["x"] == 2.0
assert raw.selected_params is None
assert filtered.selected_params["x"] == 1.0
def test_no_feasible_trial_returns_no_selected_params():
evaluator = GenericEndpointEvaluator(
build_run_inputs=lambda params: {"value": float(params["x"])},
run_func=lambda value: Result(value),
objective_builder=lambda result, params: ObjectiveResult.scalar(result.value, constraints=(1.0,)),
)
optimizer = OptunaOptimizer(
evaluator=evaluator,
config=OptimizationConfig(study_name="no_feasible", n_trials=2, seed=1, show_progress_bar=False),
sampler_config=SamplerConfig(name="grid", constraint_mode="post_filter"),
)
result = optimizer.optimize(param_ranges={"x": [1.0, 2.0]})
assert result.best_params["x"] == 2.0
assert result.selected_params is None
def test_multi_objective_pareto_smoke_and_selector_policy():
evaluator = GenericEndpointEvaluator(
build_run_inputs=lambda params: {"x": float(params["x"])},
run_func=lambda x: x,
objective_builder=lambda result, params: ObjectiveResult(
values=(float(result), abs(float(result) - 1.0)),
metrics={"score": float(result), "risk": abs(float(result) - 1.0)},
),
)
optimizer = OptunaOptimizer(
evaluator=evaluator,
config=OptimizationConfig(
study_name="pareto_integration",
n_trials=4,
directions=("maximize", "minimize"),
seed=3,
show_progress_bar=False,
duplicate_policy="allow",
),
sampler_config=SamplerConfig(name="nsgaii"),
)
result = optimizer.optimize(param_ranges={"x": [0.0, 1.0, 2.0]})
assert result.best_params is None
assert result.selected_params is None
assert result.pareto_trials
selected = CandidateSelector("pareto_first").select(result)
assert "x" in selected.params
def test_pareto_selector_filters_infeasible_trials():
evaluator = GenericEndpointEvaluator(
build_run_inputs=lambda params: {"x": float(params["x"])},
run_func=lambda x: x,
objective_builder=lambda result, params: ObjectiveResult(
values=(float(result), abs(float(result) - 1.0)),
constraints=(float(result) - 1.0,),
metrics={"score": float(result)},
),
)
optimizer = OptunaOptimizer(
evaluator=evaluator,
config=OptimizationConfig(
study_name="pareto_feasible_filter",
n_trials=3,
directions=("maximize", "minimize"),
show_progress_bar=False,
duplicate_policy="allow",
),
sampler_config=SamplerConfig(name="grid", constraint_mode="post_filter"),
)
result = optimizer.optimize(param_ranges={"x": [0.0, 1.0, 2.0]})
selected = CandidateSelector("pareto_first").select(result)
assert selected.params["x"] <= 1.0
assert constraints_feasible(selected.constraints)
def test_unsupported_constraint_sampler_requires_post_filter():
evaluator = GenericEndpointEvaluator(
build_run_inputs=lambda params: {"value": float(params["x"])},
run_func=lambda value: Result(value),
objective_builder=lambda result, params: ObjectiveResult.scalar(result.value, constraints=(0.0,)),
)
optimizer = OptunaOptimizer(
evaluator=evaluator,
config=OptimizationConfig(study_name="unsupported_constraints", n_trials=1, show_progress_bar=False),
sampler_config=SamplerConfig(name="random"),
)
with pytest.raises(ValueError, match="constraint_mode='post_filter'"):
optimizer.optimize(param_ranges={"x": [1.0]})
def test_parallel_mode_rejected_until_thread_safe():
optimizer = OptunaOptimizer(
evaluator=GenericEndpointEvaluator(
build_run_inputs=lambda params: {"value": params["x"]},
run_func=lambda value: Result(value),
objective_builder=lambda result, params: ObjectiveResult.scalar(result.value),
),
config=OptimizationConfig(study_name="parallel_reject", n_trials=1, n_jobs=2, show_progress_bar=False),
sampler_config=SamplerConfig(name="random"),
)
with pytest.raises(NotImplementedError, match="parallel optimization is not certified"):
optimizer.optimize(param_ranges={"x": [1.0]})
def test_duplicate_detection_after_sqlite_resume(tmp_path):
storage = f"sqlite:///{tmp_path / 'dup_resume.db'}"
def make_optimizer():
return OptunaOptimizer(
evaluator=GenericEndpointEvaluator(
build_run_inputs=lambda params: {"value": float(params["x"])},
run_func=lambda value: Result(value),
objective_builder=lambda result, params: ObjectiveResult.scalar(result.value),
),
config=OptimizationConfig(
study_name="dup_resume",
n_trials=1,
storage=storage,
load_if_exists=True,
show_progress_bar=False,
),
sampler_config=SamplerConfig(name="random"),
)
first = make_optimizer().optimize(param_ranges={"x": [1.0]})
second = make_optimizer().optimize(param_ranges={"x": [1.0]})
assert [record.state for record in first.trials] == ["COMPLETE"]
assert [record.state for record in second.trials][-1] == "PRUNED"
def test_repeated_optimize_does_not_reuse_stale_seen_set():
optimizer = OptunaOptimizer(
evaluator=GenericEndpointEvaluator(
build_run_inputs=lambda params: {"value": float(params["x"])},
run_func=lambda value: Result(value),
objective_builder=lambda result, params: ObjectiveResult.scalar(result.value),
),
config=OptimizationConfig(study_name="stale_seen", n_trials=1, show_progress_bar=False),
sampler_config=SamplerConfig(name="random"),
)
first = optimizer.optimize(param_ranges={"x": [1.0]})
second = optimizer.optimize(param_ranges={"x": [2.0]})
assert first.trials[-1].state == "COMPLETE"
assert second.trials[-1].state == "COMPLETE"
def test_jsonl_contains_fixed_and_search_params(tmp_path):
log_path = tmp_path / "study.jsonl"
optimizer = OptunaOptimizer(
evaluator=GenericEndpointEvaluator(
build_run_inputs=lambda params: {"value": float(params["x"])},
run_func=lambda value: Result(value),
objective_builder=lambda result, params: ObjectiveResult.scalar(result.value),
),
config=OptimizationConfig(study_name="jsonl_full_params", n_trials=1, show_progress_bar=False, log_path=log_path),
sampler_config=SamplerConfig(name="random"),
)
optimizer.optimize(param_ranges={"x": [1.0]}, fixed_params={"issl": True})
row = json.loads(log_path.read_text().splitlines()[0])
assert row["params"] == {"issl": True, "x": 1.0}
def test_custom_objective_can_raise_and_exception_policy_prunes():
class BrokenObjective:
def __call__(self, result, params):
raise ValueError("bad score")
evaluator = GenericEndpointEvaluator(
build_run_inputs=lambda params: {"value": params["x"]},
run_func=lambda value: Result(value),
objective_builder=BrokenObjective(),
)
optimizer = OptunaOptimizer(
evaluator=evaluator,
config=OptimizationConfig(
study_name="custom_objective_prune",
n_trials=2,
seed=1,
show_progress_bar=False,
exception_policy="prune",
),
sampler_config=SamplerConfig(name="random"),
)
result = optimizer.optimize(param_ranges={"x": [1, 2]})
assert all(record.state == "PRUNED" for record in result.trials)
def test_persistent_sqlite_resume_smoke(tmp_path):
storage = f"sqlite:///{tmp_path / 'resume.db'}"
def make_optimizer(n_trials):
evaluator = GenericEndpointEvaluator(
build_run_inputs=lambda params: {"value": float(params["x"])},
run_func=lambda value: Result(value),
objective_builder=lambda result, params: ObjectiveResult.scalar(result.value, metrics={"score": result.value}),
)
return OptunaOptimizer(
evaluator=evaluator,
config=OptimizationConfig(
study_name="sqlite_resume",
n_trials=n_trials,
seed=11,
show_progress_bar=False,
storage=storage,
load_if_exists=True,
duplicate_policy="allow",
),
sampler_config=SamplerConfig(name="random"),
)
first = make_optimizer(2).optimize(param_ranges={"x": [0.0, 1.0, 2.0]})
second = make_optimizer(3).optimize(param_ranges={"x": [0.0, 1.0, 2.0]})
assert len(first.trials) == 2
assert len(second.trials) == 5
assert second.best_params is not None