Previous topic

Neural Networks package

Next topic

BackTest package

This Page

Rolling Neural Networks module

The main idea behind Rolling Neural Network is to train the model by followig the temporal structure of the data. By example we start to train over 1 year (from 01/01/2000 to 31/12/2000), predict 3 months ahead (from 01/01/2001 to 31/03/2001), and move 3 months ahead to retrain the model (from 01/04/2000 to 31/03/2001) and predict again 3 months ahead (from 01/04/2001 to 30/06/2001), and so on until present.

_images/rolling_mechanism.png
fynance.neural_networks.RollNeuralNet([…]) Object to train/test a neural network along time axis.
fynance.neural_networks.RollMultiNeuralNet([…]) Rolling Multi Neural Networks object allow you to train several neural networks along training periods (from t - n to t) and predict along testing periods (from t to t + s) and roll along this time axis.
fynance.neural_networks.RollAggrMultiNeuralNet(*args) Rolling Aggregated Multi Neural Networks object allow you to train several neural networks along training periods (from t - n to t), predict along testing periods (from t to t + s) and aggregate prediction following a specified rule and roll along this time axis.
fynance.neural_networks.RollMultiRollNeuralNet([…]) Rolling Multi Rolling Neural Networks object allow you to train several rolling neural networks along training periods (from t - n to t) and predict along testing periods (from t to t + s) and roll along this time axis.
class fynance.neural_networks.RollNeuralNet(train_period=252, estim_period=63, value_init=100, target_filter='sign', params=None)

Bases: object

Object to train/test a neural network along time axis.

Rolling Neural Network object allow you to train neural networks along training periods (from t - n to t) and predict along testing periods (from t to t + s) and roll along this time axis.

Attributes:
y : np.ndarray[np.float32, ndim=2] with shape=(T, 1)

Target to predict, a good practice is to use log-returns.

X : np.ndarray[np.float32, ndim=2] with shape=(T, N)

Features (inputs).

NN : keras.Model

Neural network to train and predict.

y_train : np.ndarray[np.float64, ndim=1]

Prediction on training set.

y_estim : np.ndarray[np.float64, ndim=1]

Prediction on estimating set.

Methods

run(y, X, NN, plot_loss=True, plot_perf=True, x_axis=None) Train rolling neural networks along pre-specified training period and predict along test period. Display loss and performance if specified.
__call__(y, X, NN, start=0, end=1e8, x_axis=None) Callable method to set target and features data, neural network object (Keras object is prefered).
__iter__() Train and predict along time axis from day number n to last day number T and by step of size s period.
plot_loss(self, f, ax) Plot loss function
plot_perf(self, f, ax) Plot perfomances.
__call__(self, y, X, NN, start=0, end=100000000.0, x_axis=None)

Callable method.

Set terget and features data, neural network object (Currently Keras object is prefered).

Parameters:
y : np.ndarray[ndim=1, dtype=np.float32]

Target to predict.

X : np.ndarray[ndim=2, dtype=np.float32]

Features data.

NN : keras.engine.training.Model

Neural network model.

start : int, optional

Starting observation, default is first observation.

end : int, optional

Ending observation, default is last observation.

x_axis : np.ndarray[ndim=1], optional

X-Axis to use for the backtest (int, date, str, etc.).

Returns:
rnn : RollNeuralNet
__init__(self, train_period=252, estim_period=63, value_init=100, target_filter='sign', params=None)

Initialize method.

Set size of training and predicting period, inital value to backtest, a target filter and training parameters.

Parameters:
train_period : int, optional

Size of the training period. Default is 252 corresponding at one year i.e 252 trading days.

estim_period : int, optional

Size of the period to predict (is also the default rolling period). Default is 63 corresponding at three months i.e 63 trading days.

value_init : int, optional

Initial value to backtest strategy. Default is 100.

target_filter : function, str or bool

Function to filtering target. If True or ‘sign’ use np.sign() function, if False doesn’t filtering target. Default is ‘sign’.

params : dict, optional

Parameters for training periods

__iter__(self)

Set iterative method.

__next__(self)

Increment method.

__weakref__

list of weak references to the object (if defined)

plot_loss(self, f, ax)

Plot loss function of train/estim periods of neural network.

Parameters:
fig : matplotlib.figure.Figure

Figure to display backtest.

ax : matplotlib.axes

Axe(s) to display a part of backtest.

Returns:
rnn : RollNeuralNet
plot_perf(self, f, ax)

Plot performances of train/estim periods of neural network.

Parameters:
fig : matplotlib.figure.Figure

Figure to display backtest.

ax : matplotlib.axes

Axe(s) to display a part of backtest.

Returns:
rnn : RollNeuralNet
run(self, y, X, NN, plot_loss=True, plot_perf=True, x_axis=None)

Train rolling neural networks along time axis.

Display loss and performance if specified.

Parameters:
y : np.ndarray[np.float32, ndim=2], with shape (T, 1)

Time series of target to estimate or predict.

X : np.ndarray[np.float32, ndim=2], with shape (T, N)

Several time series of features.

NN : keras.Model or list of keras.Model

Neural networks to train and predict.

plot_loss : bool, optional

If true dynamic plot of loss function.

plot_perf : bool, optional

If true dynamic plot of strategy performance.

x_axis : list or array, optional

x-axis to plot (e.g. list of dates).

Returns:
rnn : RollNeuralNet
class fynance.neural_networks.RollMultiNeuralNet(train_period=252, estim_period=63, value_init=100, target_filter='sign', params=None)

Bases: fynance.neural_networks.roll_neural_network.RollNeuralNet

Rolling Multi Neural Networks object allow you to train several neural networks along training periods (from t - n to t) and predict along testing periods (from t to t + s) and roll along this time axis.

Attributes:
y : np.ndarray[np.float32, ndim=2] with shape=(T, 1)

Target to estimate or predict.

X : np.ndarray[np.float32, ndim=2] with shape=(T, N)

Features (inputs).

NN : list of keras.Model

Neural network models to train and predict.

y_train : np.ndarray[np.float64, ndim=1]

Prediction on training set.

y_estim : np.ndarray[np.float64, ndim=1]

Prediction on estimating set.

Methods

run(y, X, NN, plot_loss=True, plot_perf=True, x_axis=None) Train several rolling neural networks along pre-specified training period and predict along test period. Display loss and performance if specified.
__call__(y, X, NN, start=0, end=1e8, x_axis=None) Callable method to set target and features data, neural network object (Keras object is prefered).
__iter__() Train and predict along time axis from day number n to last day number T and by step of size s period.
plot_loss(self, f, ax) Plot loss function
plot_perf(self, f, ax) Plot perfomances.
__call__(self, y, X, NN, start=0, end=100000000.0, x_axis=None)

Callable method to set terget and features data, neural network object (Keras object is prefered).

Parameters:
y : np.ndarray[ndim=1, dtype=np.float32]

Target to predict.

X : np.ndarray[ndim=2, dtype=np.float32]

Features data.

NN : list of keras.engine.training.Model

Neural network models.

start : int, optional

Starting observation, default is first one.

end : int, optional

Ending observation, default is last one.

x_axis : np.ndarray[ndim=1], optional

X-Axis to use for the backtest.

Returns:
rmnn : RollMultiNeuralNet
__iter__(self)

Set iterative method

__next__(self)

Incrementing method

run(self, y, X, NN, plot_loss=True, plot_perf=True, x_axis=None)

Train several rolling neural networks along pre-specified train period and predict along test period. Display loss and performance if specified.

Parameters:
y : np.ndarray[np.float32, ndim=2] with shape=(T, 1)

Time series of target to estimate or predict.

X : np.ndarray[np.float32, ndim=2] with shape=(T, N)

Several time series of features.

NN : keras.Model or list of keras.Model

Neural networks to train and predict.

plot_loss : bool, optional

If true dynamic plot of loss function, default is True.

plot_perf : bool, optional

If true dynamic plot of strategy performance, default is True.

x_axis : list or array, optional

x-axis to plot (e.g. list of dates).

Returns:
rmnn : RollMultiNeuralNet
class fynance.neural_networks.RollAggrMultiNeuralNet(*args, agg_fun='mean', **kwargs)

Bases: fynance.neural_networks.roll_multi_neural_networks.RollMultiNeuralNet

Rolling Aggregated Multi Neural Networks object allow you to train several neural networks along training periods (from t - n to t), predict along testing periods (from t to t + s) and aggregate prediction following a specified rule and roll along this time axis.

Attributes:
y : np.ndarray[np.float32, ndim=2] with shape=(T, 1)

Target to estimate or predict.

X : np.ndarray[np.float32, ndim=2] with shape=(T, N)

Features (inputs).

NN : list of keras.Model

Neural network to train and predict.

y_train : np.ndarray[np.float64, ndim=1]

Prediction on training set.

y_estim : np.ndarray[np.float64, ndim=1]

Prediction on estimating set.

Methods

run(y, X, NN, plot_loss=True, plot_perf=True, x_axis=None) Train several rolling neural networks along pre-specified training period and predict along test period. Display loss and performance if specified.
__call__(y, X, NN, start=0, end=1e8, x_axis=None) Callable method to set target and features data, neural network object (Keras object is prefered).
__iter__() Train and predict along time axis from day number n to last day number T and by step of size s period.
aggregate(mat_pred, y, t=0, t_s=-1) Method to aggregate predictions from several neural networks.
set_aggregate(*args) Set your own aggregation method.
plot_loss(self, f, ax) Plot loss function
plot_perf(self, f, ax) Plot perfomances.
__call__(self, y, X, NN, start=0, end=100000000.0, x_axis=None)

Callable method to set terget and features data, neural network object (Keras object is prefered).

Parameters:
y : np.ndarray[ndim=1, dtype=np.float32]

Target to predict.

X : np.ndarray[ndim=2, dtype=np.float32]

Features data.

NN : list of keras.engine.training.Model

Neural network model.

start : int, optional

Starting observation, default is 0.

end : int, optional

Ending observation, default is end.

x_axis : np.ndarray[ndim=1], optional

X-Axis to use for the backtest.

Returns:
ramnn : RollAggrMultiNeuralNet
__init__(self, *args, agg_fun='mean', **kwargs)

Initialize method.

Set size of training and predicting period, inital value to backtest, a target filter and training parameters.

Parameters:
train_period : int, optional

Size of the training period. Default is 252 corresponding at one year i.e 252 trading days.

estim_period : int, optional

Size of the period to predict (is also the default rolling period). Default is 63 corresponding at three months i.e 63 trading days.

value_init : int, optional

Initial value to backtest strategy. Default is 100.

target_filter : function, str or bool

Function to filtering target. If True or ‘sign’ use np.sign() function, if False doesn’t filtering target. Default is ‘sign’.

params : dict, optional

Parameters for training periods

aggregate(self, mat_pred, y, t=0, t_s=-1)

Method to aggregate predictions from several neural networks.

Parameters:
mat_pred : np.ndarray[np.float32, ndim=2] with shape=(T, n_NN)

Several time series of neural networks predictions.

y : np.ndarray[np.float32, ndim=2] with shape=(T, 1)

Time series of target to estimate or predict.

t : int, optional

First observation, default is first one.

t_s : int, optional

Last observation, default is last one.

Returns:
ramnn : RollAggrMultiNeuralNet
plot_perf(self, f, ax)

Plot performances method

Parameters:
fig : matplotlib.figure.Figure

Figure to display backtest.

ax : matplotlib.axes

Axe(s) to display a part of backtest.

Returns:
ramnn : RollAggrMultiNeuralNet
run(self, y, X, NN, plot_loss=True, plot_perf=True, x_axis=None)

Train several rolling neural networks along pre-specified train period and predict along test period. Display loss and performance if specified.

Parameters:
y : np.ndarray[np.float32, ndim=2] with shape=(T, 1)

Time series of target to estimate or predict.

X : np.ndarray[np.float32, ndim=2] with shape=(T, N)

Several time series of features.

NN : keras.Model or list of keras.Model

Neural networks to train and predict.

plot_loss : bool, optional

If true dynamic plot of loss function, default is True.

plot_perf : bool, optional

If true dynamic plot of strategy performance, default is True.

x_axis : list or array, optional

x-axis to plot (e.g. list of dates).

Returns:
ramnn : RollAggrMultiNeuralNet
set_aggregate(self, *args)

Set your own aggregation method.

Parameters:
args : tuple of function

Any function such that the final value is a numpy array.

Returns:
ramnn : RollAggrMultiNeuralNet
class fynance.neural_networks.RollMultiRollNeuralNet(train_period=252, estim_period=63, value_init=100, target_filter='sign', params=None)

Bases: fynance.neural_networks.roll_multi_neural_networks.RollMultiNeuralNet

Rolling Multi Rolling Neural Networks object allow you to train several rolling neural networks along training periods (from t - n to t) and predict along testing periods (from t to t + s) and roll along this time axis. And “sometime” it reseting parameters of one rolling neural network.

Attributes:
y : np.ndarray[np.float32, ndim=2] with shape=(T, 1)

Target to estimate or predict.

X : np.ndarray[np.float32, ndim=2] with shape=(T, N)

Features (inputs).

NN : list of keras.Model

Neural network models to train and predict.

y_train : np.ndarray[np.float64, ndim=1]

Prediction on training set.

y_estim : np.ndarray[np.float64, ndim=1]

Prediction on estimating set.

Methods

run(y, X, NN, plot_loss=True, plot_perf=True, x_axis=None) Train several rolling neural networks along pre-specified training period and predict along test period. Display loss and performance if specified.
__call__(y, X, NN, start=0, end=1e8, x_axis=None) Callable method to set target and features data, neural network object (Keras object is prefered).
__iter__() Train and predict along time axis from day number n to last day number T and by step of size s period.
plot_loss(self, f, ax) Plot loss function
plot_perf(self, f, ax) Plot perfomances.
__call__(self, y, X, NN, weights=[], start=0, end=100000000.0, x_axis=None, reset_nn=True)

Callable method to set terget and features data, neural network object (Keras object is prefered).

Parameters:
y : np.ndarray[ndim=1, dtype=np.float32]

Target to predict.

X : np.ndarray[ndim=2, dtype=np.float32]

Features data.

NN : list of keras.engine.training.Model

Neural network models.

start : int

Starting observation, default is first one.

end : int

Ending observation, default is last one.

x_axis : np.ndarray[ndim=1], optional

X-Axis to use for the backtest.

reset_nn : bool or int, optional

If int reset one neural network each reset_nn periods. Default is True.

Returns:
rmrnn : RollMultiRollNeuralNet
__next__(self)

Incrementing method

run(self, y, X, NN, weights=[], plot_loss=True, plot_perf=True, x_axis=None, reset_nn=True)

Train several rolling neural networks along pre-specified train period and predict along test period. Display loss and performance if specified.

Parameters:
y : np.ndarray[np.float32, ndim=2] with shape=(T, 1)

Time series of target to estimate or predict.

X : np.ndarray[np.float32, ndim=2] with shape=(T, N)

Several time series of features.

NN : keras.Model or list of keras.Model

Neural networks to train and predict.

plot_loss : bool, optional

If true dynamic plot of loss function, default is True.

plot_perf : bool, optional

If true dynamic plot of strategy performance, default is True.

x_axis : list or array, optional

x-axis to plot (e.g. list of dates).

Returns:
rmrnn : RollMultiRollNeuralNet (object)