apaTrendModels.NeuralNetwork(series, nLags, nAhead, hidden1, hidden2, learningRate, epochs, batchSize, l2Alpha, validationFraction, patience, oneBased)
=apaTrendModels.NeuralNetwork(series, nLags, nAhead, hidden1, hidden2, learningRate, epochs, batchSize, l2Alpha, validationFraction, patience, oneBased)Result
Fits a neural network forecast model with lagged inputs, trained with the Adam optimizer and early stopping, and calculates a recursive multi-step forecast.
Syntax
series: r x 1 or 1 x r data vector containing a time series
nLags: number of lagged observations used as inputs
nAhead: number of recursive forecast steps
hidden1: size of the first hidden layer. Default value: 16
hidden2: size of the second hidden layer, 0 to omit the layer. Default value: 8
learningRate: learning rate of the Adam optimizer. Default value: 0.001
epochs: maximum number of training epochs. Default value: 1000
batchSize: mini-batch size. Default value: 32
l2Alpha: L2 regularization strength. Default value: 0.0001
validationFraction: fraction of the observations reserved for validation, between 0 and 0.5. Default value: 0.1
patience: number of epochs without improvement of the validation loss before training is stopped. Default value: 30
oneBased: 1 for 1-based row numbering, 0 for 0-based row numbering
Algorithm
The input matrix is set up from the nLags lagged observations, the target is the current observation. The last validationFraction of the rows is held out for validation. Inputs and target are standardized with the means and standard deviations of the training rows. The network has ReLU hidden layers and a linear output and is trained on the mean squared error plus the L2 penalty with the Adam optimizer in mini-batches of size batchSize (fixed random seed 42). After each epoch the mean squared error on the validation rows is computed; training stops after patience epochs without improvement or after epochs epochs, and the weights with the lowest validation loss are kept. The forecast is calculated recursively: each forecast is appended to the series and used as a lagged input for the next step.
Remarks
The result contains a header row followed by the columns Position, FittedValue, ForecastValue, BestValidationLoss, EpochsCompleted, LagCount and HiddenLayers. The first nLags fitted values are not available and are shown as #N/A, as are the ForecastValue cells beyond row nAhead. The ForecastValue in row j is the j-step-ahead forecast. The loss is the mean squared error of the standardized target. If validationFraction is 0, no rows are held out, the weights with the lowest training loss are kept, and the fourth column is labelled TrainingLoss.
Leading and trailing non-numeric or empty cells of series (for example a header) are ignored. A non-numeric or empty cell within the series returns an error message, since removing it would shift the lags.
