Regime Surrogate¶
Regime-conditional surrogate that interpolates factor recommendations
across regime descriptors (e.g. dataset size, noise level) instead of
relying on hard regime buckets. Builds on
fit_surrogate.
Install via the optional extra (same as the base surrogate):
trade_study.fit_regime_surrogate(results, regime_factors, factors, *, method='gp', seed=0, n_estimators=200, cv_folds=5, warn_below_r2=0.0, cv_group_by=None)
¶
Fit a surrogate that conditions on regime features.
Internally fits a single :class:SurrogateModel over the joint
regime_factors + factors space, so observables can be
interpolated across continuous regime axes.
Every config in results.configs must contain values for both the
regime features and the design factors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
results
|
ResultsTable
|
A :class: |
required |
regime_factors
|
list[Factor]
|
Factors describing the regime (additional input dimensions of the surrogate; typically continuous). |
required |
factors
|
list[Factor]
|
Tunable design factors. Together with
|
required |
method
|
str
|
Surrogate backend, |
'gp'
|
seed
|
int
|
Random seed forwarded to the backend estimators. |
0
|
n_estimators
|
int
|
Number of trees for the |
200
|
cv_folds
|
int
|
Cross-validation folds for the held-out accuracy check. See
:func: |
5
|
warn_below_r2
|
float | None
|
Warn if any observable's cross-validated R^2 falls
below this threshold. See :func: |
0.0
|
cv_group_by
|
str | Sequence[str] | None
|
|
None
|
Returns:
| Type | Description |
|---|---|
RegimeSurrogate
|
A fitted :class: |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/trade_study/regime.py
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 371 372 373 374 375 376 377 | |
fit_regime_surrogate(..., cv_group_by="regime") holds out entire tuples of
regime descriptors. The underlying model exposes cv_group_by and group ids;
cv_r2/cv_rmse describe held-out regimes, while row_cv_r2/row_cv_rmse
describe shuffled rows. Use "design" for joint design/regime configurations
or an explicit sequence of factor names for another grouping.
Inspect model.support(regime, configs) before trusting a recommendation.
Prediction and recommendation warn when candidates or the query regime fall
outside observed marginal support. warn_support=False disables that warning;
warn_below_r2=None independently disables accuracy warnings.
trade_study.RegimeSurrogate(inner, regime_factors, factors)
dataclass
¶
Surrogate that conditions on regime features.
Wraps a single :class:SurrogateModel fit over the union of regime
descriptors and design factors. Use :func:fit_regime_surrogate to
construct one.
Attributes:
| Name | Type | Description |
|---|---|---|
inner |
SurrogateModel
|
The underlying :class: |
regime_factors |
list[Factor]
|
Factors describing the regime (additional input dimensions of the surrogate). |
factors |
list[Factor]
|
Tunable design factors that are optimized at a given
regime by :meth: |
cv_r2
property
¶
Per-observable held-out cross-validated R^2.
Returns:
| Name | Type | Description |
|---|---|---|
dict[str, float]
|
Mapping from observable name to cross-validated R^2, from the |
|
underlying |
dict[str, float]
|
attr: |
cv_rmse
property
¶
Per-observable held-out cross-validated RMSE.
Returns:
| Type | Description |
|---|---|
dict[str, float]
|
Mapping from observable name to cross-validated RMSE, from |
dict[str, float]
|
the underlying :attr: |
row_cv_r2
property
¶
Shuffled row-validation R^2, for comparison with grouped validation.
Returns:
| Type | Description |
|---|---|
dict[str, float]
|
Per-observable row-validation R^2 from the underlying surrogate. |
row_cv_rmse
property
¶
Shuffled row-validation RMSE in observable units.
Returns:
| Type | Description |
|---|---|
dict[str, float]
|
Per-observable row-validation RMSE from the underlying surrogate. |
support(regime, configs)
¶
Inspect observed support at a query regime without warnings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
regime
|
dict[str, Any]
|
Regime descriptor values. |
required |
configs
|
Sequence[dict[str, Any]]
|
Design-factor configurations to inspect. |
required |
Returns:
| Type | Description |
|---|---|
list[PredictionSupport]
|
Marginal support diagnostics for each config and observable. |
Source code in src/trade_study/regime.py
predict(regime, config, *, warn_support=True)
¶
Predict observables at a regime + config pair.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
regime
|
dict[str, Any]
|
Mapping of regime-feature names to values. |
required |
config
|
dict[str, Any]
|
Mapping of design-factor names to values. |
required |
warn_support
|
bool
|
Warn on values outside observed training support. |
True
|
Returns:
| Type | Description |
|---|---|
dict[str, float]
|
Mapping from observable name to predicted scalar. |
Source code in src/trade_study/regime.py
predict_batch(regime, configs, *, warn_support=True)
¶
Predict observables for a batch of configs at one regime.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
regime
|
dict[str, Any]
|
Mapping of regime-feature names to values. |
required |
configs
|
Sequence[dict[str, Any]]
|
Sequence of design-factor configs to score at
|
required |
warn_support
|
bool
|
Warn on values outside observed training support. |
True
|
Returns:
| Type | Description |
|---|---|
dict[str, NDArray[float64]]
|
Mapping from observable name to a length- |
dict[str, NDArray[float64]]
|
array of predictions. |
Source code in src/trade_study/regime.py
uncertainty(regime, config, *, warn_support=True)
¶
Predictive standard deviation per observable (GP only).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
regime
|
dict[str, Any]
|
Mapping of regime-feature names to values. |
required |
config
|
dict[str, Any]
|
Mapping of design-factor names to values. |
required |
warn_support
|
bool
|
Warn on values outside observed training support. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
dict[str, float]
|
Mapping from observable name to predictive standard deviation. |
|
Propagates |
dict[str, float]
|
class: |
dict[str, float]
|
surrogate when the backend does not expose calibrated |
|
dict[str, float]
|
uncertainties (non-GP backends). |
Source code in src/trade_study/regime.py
recommend(regime, *, objective, mode='min', n_candidates=512, seed=0, candidates=None, warn_below_r2=0.0, risk=0.0, warn_support=True)
¶
Recommend a design-factor config at a query regime.
Samples n_candidates configs from the design-factor space via
a scrambled Sobol' sequence and returns the one whose surrogate
prediction for objective is best under mode. With
risk > 0 candidates are ranked pessimistically, by
prediction + risk * spread when minimizing and
prediction - risk * spread when maximizing, where spread is
:meth:SurrogateModel.spread_batch; this avoids picking the
candidate the surrogate is most optimistically wrong about.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
regime
|
dict[str, Any]
|
Mapping of regime-feature names to values. |
required |
objective
|
str
|
Name of the observable to optimize. Must be one
of |
required |
mode
|
str
|
|
'min'
|
n_candidates
|
int
|
Number of design-space samples to evaluate.
Ignored when |
512
|
seed
|
int
|
Seed for the Sobol' sampler. |
0
|
candidates
|
Sequence[dict[str, Any]] | None
|
Optional explicit list of design-factor configs
to score; if given, overrides |
None
|
warn_below_r2
|
float | None
|
Warn if |
0.0
|
risk
|
float
|
Non-negative multiple of the predictive spread added to
the ranking criterion; |
0.0
|
warn_support
|
bool
|
Warn if candidates or the query regime lie outside
observed training support. Inspect :meth: |
True
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The candidate config (a copy) achieving the best predicted |
dict[str, Any]
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/trade_study/regime.py
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 | |