RollMultiLayerPerceptron¶
Defined in fynance.models.rolling
- class RollMultiLayerPerceptron(X, y, layers=[], activation=None, drop=None, bias=True, x_type=None, y_type=None, activation_kwargs={}, **kwargs)[source]
Bases:
MultiLayerPerceptron,_RollingBasisRolling version of the multi-layer perceptron model.
End-to-end walk-forward training pipeline for an MLP: at each step the model is fitted on a sliding window of length
n, evaluated on the previous out-of-sample slice, then used to predict the next slice. Losses (train, eval, test) and out-of-sample predictions are accumulated step by step inself.log,self.y_evalandself.y_testfor downstream analysis.Use
set_roll_periodto configure window sizes and batch options, thenrunto execute the loop.runcan also drive a livefynance.backtest.dynamic_plot_backtest.BacktestNeuralNetfigure to monitor convergence.Combines
MultiLayerPerceptronwith the walk-forward iterator from_RollingBasis. Useset_roll_periodinstead of calling the object directly (__call__is captured bytorch.nn.Module).Methods
set_roll_period(train_period, test_period[, ...])Configure rolling window parameters.
sub_predict(X)Return predictions as a numpy array.
- cross_validate(model_factory, X, y, metric_fn=None, epochs=1)
Walk-forward cross-validation with out-of-fold predictions.
At each fold a fresh model is created via
model_factory(), trained on the rolling training window, and used to predict the next out-of-sample window. Results are accumulated across all folds without any state leaking between them.Call
__call__(orset_roll_periodforRollMultiLayerPerceptron) to configuretrain_period,test_period, androll_periodbefore calling this method.- Parameters:
- model_factorycallable
Called with no arguments before every fold. Must return an object that exposes
train_on(X, y)andpredict(X) -> NDArray | Tensor(theBaseNeuralNetinterface).- X, yarray_like
Input and target arrays shaped
(T, N)and(T, M).- metric_fncallable, optional
metric_fn(y_true, y_pred) -> floatevaluated on the test window of each fold. If None,CVResult.fold_metricsis an empty list and the mean/std fields are None.- epochsint, optional
Number of full training passes per fold, default 1.
- Returns:
- CVResult
Examples
>>> import numpy as np >>> import torch, torch.nn as nn >>> from fynance.models.mlp import MultiLayerPerceptron >>> from fynance.models.rolling import _RollingBasis >>> rng = np.random.default_rng(0) >>> X = rng.standard_normal((80, 4)).astype(np.float32) >>> y = rng.standard_normal((80, 1)).astype(np.float32) >>> Xt, yt = torch.from_numpy(X), torch.from_numpy(y) >>> def factory(): ... m = MultiLayerPerceptron(4, 1, layers=[8]) ... m.set_optimizer(nn.MSELoss, torch.optim.Adam, lr=1e-3) ... return m >>> rb = _RollingBasis(Xt, yt) >>> _ = rb(train_period=40, test_period=10, roll_period=10) >>> result = rb.cross_validate(factory, Xt, yt) >>> result.oof_predictions.shape (80, 1)
- forward(x)
Forward computation.
- get_stats()
Return per-step loss history as a DataFrame.
- Returns:
- pd.DataFrame
Columns:
step,train_loss,eval_loss,test_loss.
- load_model(path, load_optimizer=False)
Save the model with this weights and parameters.
- Parameters:
- pathstr or os.PathLike object
Path to load the model.
- load_optimizerbool, optional
If True, then load also the optimizer.
- plot_loss(figsize=(9, 4))
Plot train / eval / test loss curves.
- Parameters:
- figsizetuple of int, optional
- Returns:
- matplotlib.figure.Figure
- predict(X)
Predicts outputs of neural network model.
Runs
self.forward(X)undertorch.no_grad, so no autograd graph is built. The returned tensor is detached and lives on the same device as the model parameters.- Parameters:
- Xtorch.Tensor
Inputs to compute prediction. Same shape and dtype contract as
train_on.
- Returns:
- torch.Tensor
Outputs prediction (detached, gradient-free).
- run(backtest_plot=True, backtest_kpi=True, figsize=(9, 6), func=np.sign)
Run the rolling model and collect backtest predictions.
- Parameters:
- backtest_plotbool, optional
If True, display a live backtest performance plot.
- backtest_kpibool, optional
If True, print KPIs to stdout at each step.
- figsizetuple of int, optional
Figure size.
- funccallable, optional
Function applied to predictions before computing returns.
- save(path)[source]
Save the model weights.
- Parameters:
- pathstr
Destination path.
- save_model(path, save_optimizer=False)
Save the model with this weights and parameters.
- Parameters:
- pathstr or os.PathLike object
Path to save the model.
- save_optimizerbool, optional
If True, then save also the optimizer.
- set_data(X, y, x_type=None, y_type=None)
Set data inputs and outputs.
Coerces
Xandytotorch.Tensorand caches them asself.X/self.y. After the call the attributesself.T(number of observations),self.N(input columns) andself.M(output columns) are set.- Parameters:
- X, yarray-like
Respectively input and output data. Accepted types:
numpy.ndarray,torch.Tensor,pandas.DataFrame. Shapes must be(T, N)and(T, M)respectively.- x_type, y_typetorch.dtype, optional
Target dtypes for the resulting tensors. Default is None, which preserves the input dtype.
- Returns:
- BaseNeuralNet
self, to allow chaining.
- Raises:
- ValueError
If
self.N/self.Mwere already set andX/ydo not match, or ifXandyhave different lengths.
- set_lr_scheduler(lr_scheduler, **kwargs)
Set dynamic learning rate.
- Parameters:
- lr_schedulertorch.optim.lr_scheduler._LRScheduler
Method from
torch.optim.lr_schedulerto wrapself.optimizer, cf moduletorch.optim.lr_schedulerin PyTorch documentation [2].- **kwargs
Keyword arguments to pass to the learning rate scheduler.
References
- set_optimizer(criterion, optimizer, params=None, **kwargs)
Set the optimizer object.
Set optimizer object with specified criterion as loss function and any kwargs as optional parameters.
- Parameters:
- criterionCallabletorch.nn.modules.loss
A loss function.
- optimizertorch.optim.Optimizer
An optimizer algorithm.
- paramsobject or iterable object
Layer of parameters to optimize or dicts defining parameter groups. If set to None then all parameters of model will be optimized. Default is None.
- **kwargs
Keyword arguments of
optimizer, cf PyTorch documentation [1].
- Returns:
- BaseNeuralNet
Self object model.
References
- set_roll_period(train_period, test_period, start=0, end=None, roll_period=None, eval_period=None, batch_size=64, epochs=1)[source]
Configure rolling window parameters.
This is the preferred entry-point for
RollMultiLayerPerceptronbecause__call__is captured bytorch.nn.Module.- Parameters:
- train_period, test_periodint
Size of respectively training and testing sub-periods.
- startint, optional
- endint, optional
- roll_periodint, optional
- eval_periodint, optional
- batch_sizeint, optional
- epochsint, optional
- Returns:
- _RollingBasis
- set_seed(seed_torch=None, seed_numpy=None)
Set seed for PyTorch and NumPy random number generator.
- Parameters:
- seed_torch, seed_numpybool or int, optional
If seed is an int \(0 < seed < 2^32\) set respectively PyTorch and NumPy seed with the number. Otherwise if is True then choose a random number, else doesn’t set seed.
- sub_predict(X)[source]
Return predictions as a numpy array.
- train_on(X, y)
Trains the neural network model on a single batch.
Runs one forward / backward / optimizer-step cycle on the batch
(X, y). As a side effect, gradients of all parameters are zeroed before the forward pass and the optimizer state is advanced afterwards. If a learning-rate scheduler has been registered viaset_lr_scheduler, itsstepis also called.- Parameters:
- X, ytorch.Tensor
Respectively inputs and outputs to train model. Shapes must match what
self.forwardexpects (see the class-level “Public API contract” section).
- Returns:
- torch.Tensor
The loss tensor produced by
self.criterion(self(X), y), with gradient already consumed byloss.backward().
- Raises:
- AttributeError
If
set_optimizerhas not been called yet.