ObjectiveModel¶
Defined in fynance.models.objective
- class ObjectiveModel(net=None, *, layers=(16, 8), loss=None, optimizer=torch.optim.Adam, lr=1e-3, epochs=80, position_fn=torch.tanh, cost=0.0, seed=0)[source]
Bases:
objectTrain a net to maximize a differentiable financial objective.
- Parameters:
- nettorch.nn.Module, optional
Architecture mapping a feature matrix
(T, F)to(T, 1). Defaults to an MLP built lazily on the firstfit(so it learnsF). Pass anynn.Module(e.g. a TCN/LSTM) to use a custom architecture.- layerstuple of int
Hidden sizes of the default MLP (ignored when
netis given).- lossBaseLoss, optional
Differentiable financial loss applied to the strategy returns
positions * returns. Defaults toSharpeLoss.- optimizertype[torch.optim.Optimizer]
Optimizer class (default
Adam).- lrfloat
Learning rate.
- epochsint
Full-batch training steps per
fit.- position_fncallable
Maps the net output to a position; default
tanh(positions in[-1, 1]).- costfloat
Per-bar proportional turnover cost penalized during training (e.g.
0.0026for 26 bps). When non-zero the objective is computed on the net-of-cost returnpositions * returns - cost * |Δpositions|, so the net learns to hold positions instead of churning — the anti-churn brick for high-cost / high-frequency settings. Use the same value as the backtest’sProportionalCost. Default0(no penalty, original behaviour).- seedint
Seed for reproducible initialization/training.
Notes
The net is warm-started across successive
fitcalls (so a walk-forward refit adapts online). Build a fresh model for an independent run.- fit(X, y)[source]
Train the net to maximize the objective of
positions * y.- Parameters:
- Xarray-like, shape (T, F)
Feature matrix.
- yarray-like, shape (T,)
Realized per-bar returns aligned with
X(not a supervised label).
- Returns:
- ObjectiveModel
self.
- predict(X)[source]
Return positions in
[-1, 1]for each row ofX.