Neural network models¶
BaseNeuralNet is the root of all PyTorch models in
this package. It wraps torch.nn.Module with higher-level training, prediction
and serialization helpers — set_optimizer,
train_on,
predict,
set_data,
save_model /
load_model — so that subclasses only
need to implement forward.
MultiLayerPerceptron is the feed-forward specialization:
a configurable stack of Linear → Dropout → Activation blocks, best suited for
tabular or sliding-window features (technical indicators, volatility signals). For
time-ordered sequences, prefer the recurrent architectures described in
Recurrent neural networks.
Objective-aligned training
ObjectiveModel trains a network directly on a
differentiable financial objective — e.g. SharpeLoss
— rather than MSE against a target. The net outputs positions and the loss is
computed on positions * returns: fit(X, y) reads y as the realized
returns, and predict(X) returns positions in [-1, 1]. It is a
SignalModel, so it drops straight into a Strategy
with an identity signal:
from fynance.models import ObjectiveModel, SharpeLoss
from fynance.strategy import Strategy
model = ObjectiveModel(layers=(16, 8), loss=SharpeLoss(), epochs=60, seed=0)
strat = Strategy(model=model, signal=lambda positions: positions)
Set cost (a per-bar proportional fee, e.g. 0.0026) to train on the
net-of-cost return positions * returns - cost * |Δpositions|: the net then
learns to hold rather than churn — the anti-churn lever for high-cost or
high-frequency settings (use the same value as the backtest’s
ProportionalCost).
Feed it through the research harness via the X path with y = returns; see
Research workflow.
Cross-asset pretraining & persistence.
pretrain_pooled trains one net on a pool of aligned
(X_i, y_i) assets to learn a shared signal — each asset stays a contiguous
segment and mini-batches never cross an asset join, so the turnover carry and
temporal order stay intact per asset. The usual workflow then adapts per asset:
clone a copy with the pretrained
weights and finetune it on that
asset’s own data (freeze_trunk=True trains only the head). A trained model
round-trips to disk with
save /
load.
Distributional (quantile) regression
QuantileModel trains a feed-forward trunk with
one output per target quantile (default taus=(0.1, 0.5, 0.9)) on
PinballLoss, giving a distributional forecast
instead of a single point estimate. Unlike ObjectiveModel, fit(X, y) reads
y as an ordinary supervised target (e.g. the next-bar return), not a returns
series to combine with positions. It is a SignalModel: predict(X) returns
the median (or nearest-to-0.5 tau) column, shape (T,); the full band is
available through
predict_quantiles, shape
(T, n_taus). Quantile columns are trained independently (no crossing penalty),
so non-crossing is enforced at predict time by sorting along the quantile
axis:
from fynance.models import QuantileModel
model = QuantileModel(taus=(0.1, 0.5, 0.9), layers=(16, 8), epochs=200, seed=0)
model.fit(X, y)
point = model.predict(X) # (T,) median column
q10, q50, q90 = model.predict_quantiles(X).T
Regime-conditioned architecture
RegimeMoE conditions an objective-aligned
network on the causal market regime (RegimeDetector):
the prediction depends on which volatility regime the market is in. routing="soft"
(default) concatenates a learned regime embedding to the features through a
shared trunk; routing="hard" uses one expert per regime. The regime label
is produced by a detector fit on the training slice only and assigned online,
from a designated positive price/level column of X (regime_col). It reuses
ObjectiveModel for training, so it is a SignalModel like the above.
Inheritance
BaseNeuralNet → MultiLayerPerceptron
BaseNeuralNet → _RecurrentBase → RecurrentNeuralNetwork / GRUCell
/ LSTMCell (see Recurrent neural networks)
Classes
|
Base object for neural network model with PyTorch. |
|
Neural network with MultiLayer Perceptron architecture. |
|
Train a net to maximize a differentiable financial objective. |
|
Pretrain one |
|
Multi-quantile regression |
|
Regime-conditioned mixture-of-experts |