Apr 28, 2017 Working with Time Series Data in MATLAB MATLAB. This presentation considers the collection of time-series data sets from multiple sources including Excel files, SQL databases, and data.
Time Series Forecasting Using Deep Learning
This example shows how to forecast time series data using a long short-term memory (LSTM) network.
To forecast the values of future time steps of a sequence, you can train a sequence-to-sequence regression LSTM network, where the responses are the training sequences with values shifted by one time step. That is, at each time step of the input sequence, the LSTM network learns to predict the value of the next time step.
To forecast the values of multiple time steps in the future, use the
predictAndUpdateState
function to predict time steps one at a time and update the network state at each prediction.This example uses the data set
chickenpox_dataset
. The example trains an LSTM network to forecast the number of chickenpox cases given the number of cases in previous months.Load Sequence Data
Load the example data.
chickenpox_dataset
contains a single time series, with time steps corresponding to months and values corresponding to the number of cases. The output is a cell array, where each element is a single time step. Reshape the data to be a row vector.Partition the training and test data. Train on the first 90% of the sequence and test on the last 10%.
Standardize Data
For a better fit and to prevent the training from diverging, standardize the training data to have zero mean and unit variance. At prediction time, you must standardize the test data using the same parameters as the training data.
Prepare Predictors and Responses
To forecast the values of future time steps of a sequence, specify the responses to be the training sequences with values shifted by one time step. That is, at each time step of the input sequence, the LSTM network learns to predict the value of the next time step. The predictors are the training sequences without the final time step.
Define LSTM Network Architecture
Create an LSTM regression network. Specify the LSTM layer to have 200 hidden units.
Specify the training options. Set the solver to
'adam'
and train for 250 epochs. To prevent the gradients from exploding, set the gradient threshold to 1. Specify the initial learn rate 0.005, and drop the learn rate after 125 epochs by multiplying by a factor of 0.2.Train LSTM Network
Train the LSTM network with the specified training options by using
trainNetwork
.Forecast Future Time Steps
To forecast the values of multiple time steps in the future, use the
predictAndUpdateState
function to predict time steps one at a time and update the network state at each prediction. For each prediction, use the previous prediction as input to the function.Standardize the test data using the same parameters as the training data.
To initialize the network state, first predict on the training data
XTrain
. Next, make the first prediction using the last time step of the training response YTrain(end)
. Loop over the remaining predictions and input the previous prediction to predictAndUpdateState
.For large collections of data, long sequences, or large networks, predictions on the GPU are usually faster to compute than predictions on the CPU. Otherwise, predictions on the CPU are usually faster to compute. For single time step predictions, use the CPU. To use the CPU for prediction, set the
'ExecutionEnvironment'
option of predictAndUpdateState
to 'cpu'
.Unstandardize the predictions using the parameters calculated earlier.
The training progress plot reports the root-mean-square error (RMSE) calculated from the standardized data. Calculate the RMSE from the unstandardized predictions.
Plot the training time series with the forecasted values.
Compare the forecasted values with the test data.
Update Network State with Observed Values
If you have access to the actual values of time steps between predictions, then you can update the network state with the observed values instead of the predicted values.
First, initialize the network state. To make predictions on a new sequence, reset the network state using
resetState
. Resetting the network state prevents previous predictions from affecting the predictions on the new data. Reset the network state, and then initialize the network state by predicting on the training data.Predict on each time step. For each prediction, predict the next time step using the observed value of the previous time step. Set the
'ExecutionEnvironment'
option of predictAndUpdateState
to 'cpu'
.Unstandardize the predictions using the parameters calculated earlier.
Calculate the root-mean-square error (RMSE).
Compare the forecasted values with the test data.
Here, the predictions are more accurate when updating the network state with the observed values instead of the predicted values.
See Also
lstmLayer
| sequenceInputLayer
| trainNetwork
| trainingOptions
Related Topics
Class: arima
Forecast ARIMA or ARIMAX model responses or conditional variances
Syntax
[Y,YMSE]= forecast(Mdl,numperiods,Y0)
[Y,YMSE] = forecast(Mdl,numperiods,Y0,Name,Value)
[Y,YMSE,V]= forecast(___)
Description
[Y
,YMSE
]= forecast(Mdl
,numperiods
,Y0
)
returns numperiods
consecutive forecasted responses Y
and corresponding mean square errors YMSE
of the fully specified, univariate ARIMA or ARIMAX model Mdl
. The presample response data Y0
initializes the model to generate forecasts.[Y
,YMSE
] = forecast(Mdl
,numperiods
,Y0
,Name,Value
)
uses additional options specified by one or more name-value pair arguments. For example, for a model with a regression component, 'X0',X0,'XF',XF
specifies the presample and forecasted predictor data X0
and XF
, respectively.[Y
,YMSE
,V
]= forecast(___)
also forecasts numperiods
conditional variances V
of a composite conditional mean and variance model (for example, an ARIMA and GARCH composite model) using any of the input argument combinations in the previous syntaxes.Input Arguments
Mdl
— Fully specified ARIMA or ARIMAX model
arima
model
Fully specified ARIMA or ARIMAX model, specified as an
arima
model returned by arima
or estimate
.The properties of
Mdl
cannot contain NaN
s.Forecast horizon, or the number of time points in the forecast period, specified as a positive integer.
Data Types:
double
Y0
— Presample response data
numeric column vector | numeric matrix
Presample response data used to initialize the model for forecasting, specified as a numeric column vector with length
numpreobs
or a numpreobs
-by-numpaths
numeric matrix.Rows of
Y0
correspond to periods in the presample, and the last row contains the latest presample response. numpreobs
is the number of specified presample responses and it must be at least Mdl.P
. If numpreobs
exceeds Mdl.P
, forecast
uses only the latest Mdl.P
rows. For more details, see Time Base Partitions for Forecasting.Columns of
Y0
correspond to separate, independent paths.- If
Y0
is a column vector,forecast
applies it to each forecasted path. In this case, all forecast pathsY
derive from the same initial conditions. - If
Y0
is a matrix, it must havenumpaths
columns, wherenumpaths
is the maximum among the second dimensions of the specified presample observation arraysY0
,E0
, andV0
.
Data Types:
double
Name-Value Pair Arguments
Specify optional comma-separated pairs of
Name,Value
arguments. Name
is the argument name and Value
is the corresponding value. Name
must appear inside quotes. You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN
.'E0'
— Presample innovations
numeric column vector | numeric matrix
Presample innovations used to initialize the moving average (MA) component of the model or conditional variance model, specified as the comma-separated pair consisting of
'E0'
and a numeric column vector or a numeric matrix with numpaths
columns. forecast
assumes that the presample innovations have a mean of 0.Rows of
E0
correspond to periods in the presample, and the last row contains the latest presample innovation. E0
must have at least Mdl.Q
rows to initialize the MA component. If Mdl.Variance
is a conditional variance model (for example, garch
), E0
might require more than Mdl.Q
rows. If the number of rows exceeds the minimum number required to forecast Mdl
, forecast
uses only the latest required rows.Columns of
E0
correspond to separate, independent paths.- If
E0
is a column vector,forecast
applies it to each forecasted path. In this case, the MA component and conditional variance model of all forecast pathsY
derive from the same initial innovations. - If
E0
is a matrix, it must havenumpaths
columns. - By default, if
numpreobs
≥Mdl.P
+Mdl.Q
,forecast
infers any necessary presample innovations by passing the modelMdl
and presample data toinfer
. For more details on this default for models containing a regression component, seeX0
andXF
. - By default, if
numpreobs
<Mdl.P
+Mdl.Q
,forecast
sets all necessary presample innovations to0
.
Data Types:
double
'V0'
— Presample conditional variances
positive numeric column vector | positive numeric matrix
Presample conditional variances used to initialize the conditional variance model, specified as the comma-separated pair consisting of
'V0'
and a positive numeric column vector or a positive numeric matrix with numpaths
columns. If the model variance Mdl.Variance
is constant, forecast
ignores V0
.Rows of
V0
correspond to periods in the presample, and the last row contains the latest presample conditional variance. If Mdl.Variance
is a conditional variance model (for example, a garch
model object), E0
might require more than Mdl.Q
rows to initialize Mdl
for forecasting. If the number of rows exceeds the minimum number required to forecast, the forecast
function uses only the latest required presample conditional variances.Columns of
V0
correspond to separate, independent paths.- If
V0
is a column vector,forecast
applies it to each forecasted path. In this case, the conditional variance model of all forecast pathsY
derive from the same initial conditional variances. - If
V0
is a matrix, it must havenumpaths
columns. - By default, if you specify enough presample innovations
E0
to initialize the conditional variance model,forecast
infers any necessary presample conditional variances by passing the conditional variance modelMdl.Variance
andE0
toinfer
. - By default, if you do not specify
E0
, but you specify enough presample responsesY0
to infer enough presample innovations, thenforecast
infers any necessary presample conditional variances from the inferred presample innovations. - By default, if you do not specify enough presample data,
forecast
sets all necessary presample conditional variances to the unconditional variance of the variance process.
Data Types:
double
Presample predictor data used to infer presample innovations
E0
, specified as the comma-separated pair consisting of 'X0'
and a numeric matrix with numpreds
columns.Rows of
X0
correspond to periods in the presample, and the last row contains the latest set of presample predictor observations.- If you do not specify
E0
,X0
must have at leastnumpreobs
–Mdl.P
rows so thatforecast
can infer presample innovations. If the number of rows exceeds the minimum number required to infer presample innovations,forecast
uses only the latest required presample predictor observations. A best practice is to setX0
to the same predictor data matrix used in the estimation, simulation, or inference ofMdl
. This setting ensures the correct estimation of the presample innovationsE0
. - If you specify
E0
, thenforecast
ignoresX0
.
Columns of
X0
represent separate time series variables, and they correspond to the columns of XF
.If you specify
X0
but you do not specify forecasted predictor data XF
, then forecast
issues an error.By default,
forecast
drops the regression component from the model when it infers presample innovations, regardless of the value of the regression coefficient Mdl.Beta
.Data Types:
double
'XF'
— Forecasted or future predictor data
numeric matrix
Forecasted or future predictor data, specified as the comma-separated pair consisting of
'XF'
and a numeric matrix with numpreds
columns. XF
represents the evolution of specified presample predictor data X0
forecasted into the future (the forecast period).Rows of
XF
correspond to time points in the future; XF(t
,:)
contains the t
-period-ahead predictor forecasts. XF
must have at least numperiods
rows. If the number of rows exceeds numperiods
, forecast
uses only the first numperiods
forecasts. For more details, see Time Base Partitions for Forecasting.Columns of
XF
are separate time series variables, and they correspond to the columns of X0
.By default,
forecast
generates forecasts from Mdl
without a regression component, regardless of the value of the regression coefficient Mdl.Beta
.Notes
forecast
assumes that you synchronize all specified presample data sets such that the latest observation of each presample series occurs simultaneously. Similarly, forecast
assumes that the first observation in the forecasted predictor data XF
occurs in the next time point after the last observation in the presample predictor data X0
.Output Arguments
Y
— Minimum mean square error forecasts of response data
numeric matrix
Minimum mean square error (MMSE) forecasts of the conditional mean of the response data, returned as a
numperiods
-by-numpaths
numeric matrix. Y
represents a continuation of Y0
(Y(1,:)
occurs in the next time point after Y0(end,:)
).Y(t
,:)
contains the conditional mean forecast of all paths for time point t
in the forecast period (the t
-period-ahead forecasts). forecast
determines numpaths
from the number of columns in the presample data sets Y0
, E0
, and V0
. For details, see Algorithms. If each presample data set has one column, then Y
is a column vector.Data Types:
double
YMSE
— Mean square errors of forecasted responses
numeric matrix
Mean square errors (MSEs) of the forecasted responses
Y
(or forecast error variances), returned as a numperiods
-by-numpaths
numeric matrix.YMSE(t
,:)
contains the forecast error variances of all paths for time point t
in the forecast period. forecast
determines numpaths
from the number of columns in the presample data sets Y0
, E0
, and V0
. For details, see Algorithms. If you do not specify any presample data sets or each data set is a column vector, then YMSE
is a column vector.The square roots of
YMSE
are the standard errors of the forecasts Y
.Data Types:
double
V
— Minimum MSE forecasts of conditional variances of future model innovations
numeric matrix
Minimum mean square error (MMSE) forecasts of the conditional variances of future model innovations, returned as a
numperiods
-by-numpaths
numeric matrix. V
has numPeriods
rows and numPaths
columns.forecast
sets the number of columns of V
(numPaths
) to the largest number of columns in the presample arrays Y0
, E0
, and V0
. If you do not specify Y0
, E0
, and V0
, then V
is a numPeriods
column vector.In all cases, row i contains the conditional variance forecasts for the ith period.
Data Types:
double
Examples
Forecast the conditional mean response of simulated data over a 30-period horizon.
Simulate 130 observations from a multiplicative seasonal MA model with known parameter values.
Fit a seasonal MA model to the first 100 observations, and reserve the remaining 30 observations to evaluate forecast performance.
EstMdl
is a new arima
model that contains estimated parameters (that is, a fully specified model). Forecast the fitted model into a 30-period horizon. Specify the estimation period data as a presample.
YF
and YMSE
are 30-by-1 vectors of forecasted responses and corresponding MSEs, respectively. The 15-period-ahead forecast is 0.2040 and its MSE is 0.2592.Visually compare the forecasts to the holdout data.
Forecast the daily NASDAQ Composite Index over a 500-day horizon.
Load the NASDAQ data included with the toolbox, and extract the first 1500 observations.
Fit an ARIMA(1,1,1) model to the data.
Forecast the Composite Index for 500 days using the fitted model. Use the observed data as presample data.
Plot the forecasts and 95% forecast intervals.
The process is nonstationary, so the widths of the forecast intervals grow with time.
Forecast the following known ARX(1) model into a 10-period forecast horizon:
where is a standard Gaussian random variable, and is an exogenous Gaussian random variable with a mean of 1 and a standard deviation of 0.5.
Create an
arima
model object that represents the ARX(1) model.To forecast responses from the ARX(1) model,
forecast
requires:- One presample response to initialize the autoregressive term
- Future exogenous data to include the effects of the exogenous variable on the forecasted responses
Set the presample response to the unconditional mean of the stationary process:
For the future exogenous data, draw 10 values from the distribution of the exogenous variable.
Forecast the ARX(1) model into a 10-period forecast horizon. Specify the presample response and future exogenous data.
yf(3)
= 3.8232
is the 3-period-ahead forecast of the ARX(1) model.Forecast multiple response paths from a known SAR model by specifying multiple presample response paths.
Create an
arima
model object that represents this quarterly SAR model:where is a standard Gaussian random variable.
Because
Mdl
contains autoregressive dynamic terms, forecast
requires the previous Mdl.P
responses to generate a -period-ahead forecast from the model. Therefore, the presample should contain nine values.Generate a random 9-by-10 matrix representing 10 presample paths of length 9.
Forecast 10 paths from the SAR model into a 12-quarter forecast horizon. Specify the presample observation paths
Y0
.YF
is a 12-by-10 matrix of independent forecasted paths. YF(j,k)
is the j
-period-ahead forecast of path k
. Path YF(:,k)
represents the continuation of the presample path Y0(:,k)
.Plot the presample and forecasts.
More About
Time Base Partitions for Forecasting
Time base partitions for forecasting are two disjoint, contiguous intervals of the time base; each interval contains time series data for forecasting a dynamic model. The forecast period (forecast horizon) is a
numperiods
length partition at the end of the time base during which forecast
generates forecasts Y
from the dynamic model Mdl
. The presample period is the entire partition occurring before the forecast period. forecast
can require observed responses Y0
, innovations E0
, or conditional variances V0
in the presample period to initialize the dynamic model for forecasting. The model structure determines the types and amounts of required presample observations.A common practice is to fit a dynamic model to a portion of the data set, then validate the predictability of the model by comparing its forecasts to observed responses. During forecasting, the presample period contains the data to which the model is fit, and the forecast period contains the holdout sample for validation. Suppose that yt is an observed response series; x1,t, x2,t, and x3,t are observed exogenous series; and time t = 1,…,T. Consider forecasting responses from a dynamic model of yt containing a regression component
numperiods
= K periods. Suppose that the dynamic model is fit to the data in the interval [1,T – K] (for more details, see estimate
). This figure shows the time base partitions for forecasting.For example, to generate forecasts
Y
from an ARX(2) model, forecast
requires:- Presample responses
Y0
= to initialize the model. The 1-period-ahead forecast requires both observations, whereas the 2-periods-ahead forecast requires yT – K and the 1-period-ahead forecastY(1)
.forecast
generates all other forecasts by substituting previous forecasts for lagged responses in the model. - Future exogenous data
XF
= for the model regression component. Without specified future exogenous data,forecast
ignores the model regression component, which can yield unrealistic forecasts.
Dynamic models containing a moving average component or a conditional variance model can require presample innovations or conditional variances. Given enough presample responses,
forecast
infers the required presample innovations and conditional variances. If such a model also contains a regression component, then forecast
must have enough presample responses and exogenous data to infer the required presample innovations and conditional variances. This figure shows the arrays of required observations for this case, with corresponding input and output arguments.Algorithms
forecast
sets the number of sample paths to forecastnumpaths
to the maximum number of columns among the presample data setsE0
,V0
, andY0
. All presample data sets must have eithernumpaths
> 1 columns or one column. Otherwise,forecast
issues an error. For example, ifY0
has five columns, representing five paths, thenE0
andV0
can either have five columns or one column. IfE0
has one column, thenforecast
appliesE0
to each path.NaN
values in presample and future data sets indicate missing data.forecast
removes missing data from the presample data sets following this procedure:forecast
horizontally concatenates the specified presample data setsY0
,E0
,V0
, andX0
such that the latest observations occur simultaneously. The result can be a jagged array because the presample data sets can have a different number of rows. In this case,forecast
prepads variables with an appropriate amount of zeros to form a matrix.forecast
applies list-wise deletion to the combined presample matrix by removing all rows containing at least oneNaN
.forecast
extracts the processed presample data sets from the result of step 2, and removes all prepadded zeros.
forecast
applies a similar procedure to the forecasted predictor dataXF
. Afterforecast
applies list-wise deletion toXF
, the result must have at leastnumperiods
rows. Otherwise,forecast
issues an error.List-wise deletion reduces the sample size and can create irregular time series.- When
forecast
estimates MSEsYMSE
of the conditional mean forecastsY
, the function treats the specified predictor data setsX0
andXF
as exogenous, nonstochastic, and statistically independent of the model innovations. Therefore,YMSE
reflects the variance associated with the ARIMA component of the input modelMdl
alone.
Compatibility Considerations
Univariate time series models require specification of presample response data to forecast responses
forecast
now has a third input argument for you to supply presample response data. Before R2019a, the syntaxes were:
You could optionally supply presample responses using the 'Y0'
name-value pair argument.There are no plans to remove the previous syntaxes or the
'Y0'
name-value pair argument at this time. However, you are encouraged to supply presample responses because, to forecast responses from a dynamic model, forecast
must initialize models containing lagged responses. Without specified presample responses, forecast
initializes models by using reasonable default values, but the default might not support all workflows.- For stationary models without a regression component, all presample responses are the unconditional mean of the process, by default.
- For nonstationary models or models containing a regression component, all presample responses are
0
, by default.
Update Code
Update your code by specifying presample responses in the third input argument.
If you do not supply presample responses, then
forecast
provides default presample values that might not support all workflows.References
[1] Baillie, R., and T. Bollerslev. “Prediction in Dynamic Models with Time-Dependent Conditional Variances.” Journal of Econometrics. Vol. 52, 1992, pp. 91–113.
[2] Bollerslev, T. “Generalized Autoregressive Conditional Heteroskedasticity.” Journal of Econometrics. Vol. 31, 1996, pp. 307–327.
[3] Bollerslev, T. “A Conditionally Heteroskedastic Time Series Model for Speculative Prices and Rates of Return.” The Review Economics and Statistics. Vol. 69, 1987, pp. 542–547.
[4] Box, G. E. P., G. M. Jenkins, and G. C. Reinsel. Time Series Analysis: Forecasting and Control 3rd ed. Englewood Cliffs, NJ: Prentice Hall, 1994.
[5] Enders, W. Applied Econometric Time Series. Hoboken, NJ: John Wiley & Sons, 1995.
[6] Engle, R. F. “Autoregressive Conditional Heteroskedasticity with Estimates of the Variance of United Kingdom Inflation.” Econometrica. Vol. 50, 1982, pp. 987–1007.
[7] Hamilton, J. D. Time Series Analysis. Princeton, NJ: Princeton University Press, 1994.
See Also
arima
| estimate
| filter
| impulse
| infer
| print
| simulate