Documentation

forecast

Forecast identified model output

Syntax

  • yf = forecast(sys,PastData,K)
    example
  • yf = forecast(sys,PastData,K,FutureInputs)
    example
  • yf = forecast(___,opts)
    example
  • [yf,x0,sysf] = forecast(___)
    example
  • [yf,x0,sysf,yf_sd,x,x_sd] = forecast(___)
    example

Description

example

yf = forecast(sys,PastData,K) forecasts the output of an identified time series model, sys, K steps into the future using past measured data, PastData.

forecast performs prediction into the future, in a time range beyond the last instant of measured data. In contrast, the predict command predicts the response of an identified model over the time span of measured data. Use predict to determine if the predicted result matches the observed response of an estimated model. If sys is a good prediction model, consider using it with forecast.

example

yf = forecast(sys,PastData,K,FutureInputs) uses the future values of the inputs, FutureInputs, to forecast the response of an identified model with input channels.

example

yf = forecast(___,opts) uses the option set, opts, to specify additional forecast options. Use opts with any of the previous input argument combinations.

example

[yf,x0,sysf] = forecast(___) also returns the estimated values for initial states, x0, and a forecasting model, sysf, and can include any of the previous input argument combinations.

example

[yf,x0,sysf,yf_sd,x,x_sd] = forecast(___) also returns estimated standard deviation of the output, yf_sd, state trajectory, x, and standard deviation of the trajectory, x_sd. Use with any of the previous input argument combinations.

Examples

collapse all

Forecast Future Values of a Sinusoidal Signal

Forecast the values of a sinusoidal signal using an AR model.

Generate and plot data.

data = iddata(sin(0.1*[1:100])',[]);
plot(data)

Fit an AR model to the sine wave.

sys = ar(data,2);

Forecast the values into the future for a given time horizon.

K = 100;
p = forecast(sys,data,K);

K specifies the forecasting time horizon as 100 samples. p is the forecasted model response.

Analyze the forecasted data.

plot(data,'b',p,'r'), legend('measured','forecasted')

Forecast Response of Time-Series Model

Obtain past data and identify a time-series model.

load iddata9 z9
past_data = z9.OutputData(1:50);

model = ar(z9,4);

z9 is an iddata object that contains measured output only.

model is an idpoly time-series model.

Specify initial conditions for forecasting.

opt = forecastOptions('InitialCondition','e');

Forecast the system response into the future for a given time horizon.

K = 100;
yf = forecast(model,past_data,K,opt);

yf is the forecasted model response.

Plot the forecasted data.

t = z9.SamplingInstants;
t1 = t(1:50);
t2 = t(51:150)';
plot(t(1:150),z9.y(1:150),t2,yf,'r')
legend('Measured','Forecasted')

Forecast Model Response for Known Future Inputs

Obtain past data, future inputs, and an identified linear model.

load iddata1 z1
z1 = iddata(cumsum(z1.y),cumsum(z1.u),z1.Ts,'InterSample','foh');
past_data = z1(1:100);
future_inputs = z1.u(101:end);
sys = polyest(z1,[2 2 2 0 0 1],'IntegrateNoise',true);

z1 is an iddata object that contains integrated data. sys is an idpoly model. past_data contains the first 100 data points of z1.

future_inputs contains the last 200 data points of z1.

Forecast the system response into the future for a given time horizon and future inputs.

K = 200;
[yf,x0,sysf,yf_sd,x,x_sd] = forecast(sys,past_data,K,future_inputs);

yf is the forecasted model response, and yf_sd is the standard deviation of the output. x0 is the estimated value for initial states, and sysf is the forecasting state-space model. Also returned are the state trajectory, x, and standard deviation of the trajectory, x_sd.

Plot the forecasted response.

UpperBound = iddata(yf.OutputData+3*yf_sd,[],yf.Ts,'Tstart',yf.Tstart);
LowerBound = iddata(yf.OutputData-3*yf_sd,[],yf.Ts,'Tstart',yf.Tstart);
plot(past_data(:,:,[]),yf(:,:,[]),UpperBound,'k--',LowerBound,'k--')
legend({'Measured','Forecasted','3 sd uncertainty'},'Location','best')

Plot the state trajectory.

t = z1.SamplingInstants(101:end);
subplot(3,1,1)
plot(t,x(:,1),t,x(:,1)+3*x_sd(:,1),'k--',t,x(:,1)-3*x_sd(:,1),'k--')
title('X_1')

subplot(3,1,2)
plot(t, x(:,2),t,x(:,2)+3*x_sd(:,2),'k--',t, x(:,2)-3*x_sd(:,2),'k--')
title('X_2')

subplot(3,1,3)
plot(t,x(:,3),t,x(:,3)+3*x_sd(:,3),'k--',t, x(:,3)-3*x_sd(:,3),'k--')
title('X_3')

The response uncertainty does not grow over the forecasting time span because of the specification of future inputs.

Forecast Response of Multi-Output Nonlinear Time-Series Model

Load data.

load(fullfile(matlabroot,'toolbox','ident','iddemos','data','predprey2data'));
z = iddata(y,[],0.1);
set(z,'Tstart',0,'OutputUnit',{'Population (in thousands)','Population (in thousands)'},'TimeUnit','Years');

z is a two output time-series data set (no inputs) from a 1-predator 1-prey population. The population exhibits a decline in predator population due to crowding. The data set contains 201 data samples covering 20 years of evolution.

The changes in the predator (y1) and prey (y2) population can be represented as:

$$y_1(t) = p_1*y_1(t-1)+p_2*y_1(t-1)*y_2(t-1)$$

$$y_2(t) = p_3*y_2(t-1)-p_4*y_1(t-1)*y_2(t-1)-p_5*y_2(t-1)^2$$

The nonlinearity in the predator and prey populations can be fit using a nonlinear ARX model with custom regressors.

Use part of the data as past data.

past_data = z(1:100);

Specify the standard regressors.

na = [1 0; 0 1];
nb = [];
nk = [];

Specify the custom regressors.

C = {{'y1(t-1)*y2(t-1)'};{'y1(t-1)*y2(t-1)','y2(t-1)^2'}};

Estimate a nonlinear ARX model using past_data as estimation data.

sys = nlarx(past_data,[na nb nk],'wavenet','CustomRegressors',C);

Compare the simulated output of sys with measured data to ensure it is a good fit.

compare(past_data,sys);

Forecast the output of sys.

yf = forecast(sys,past_data,101);

forecast command returns the forecasted predator and prey populations, yf.

Plot the measured and forecasted populations.

t = z.SamplingInstants;
t1 = past_data.SamplingInstants;
t2 = t(101:end);
figure;
subplot(2,1,1);plot(t,z.OutputData(:,1),t2,yf.OutputData(:,1),'r')
legend('Measured','Forecasted');
ylabel('Predator Population (thousands)');
subplot(2,1,2);plot(t,z.OutputData(:,2),t2,yf.OutputData(:,2),'r')
ylabel('Prey Population (thousands)')
xlabel('Time (years)')

Reproduce Forecasting Results by Simulation

Obtain past data, future inputs, and identified linear model.

load iddata3 z3
past_data = z3(1:100);
future_inputs = z3.u(101:end);
sys = polyest(z3,[2 2 2 0 0 1]);

Forecast the system response into the future for a given time horizon and future inputs.

K = size(future_inputs,1);
[yf,x0,sysf] = forecast(sys,past_data,K,future_inputs);

yf is the forecasted model response, x0 is the estimated value for initial states, and sysf is the forecasting state-space model.

Simulate the forecasting state-space model with inputs, future_inputs, and initial conditions, x0.

opt = simOptions;
opt.InitialCondition = x0;
ys = sim(sysf,future_inputs(1:K),opt);

Plot the forecasted and simulated outputs.

t = yf.SamplingInstants;
plot(t,yf.OutputData,'b',t,ys,'.r');
legend('Forecasted Output','Simulated Output')

Simulation of forecasting model, sysf, with inputs, future_inputs, and initial conditions, x0, yields the forecasted output, yf.

Related Examples

Input Arguments

collapse all

sys — Identified modellinear model | nonlinear model

Identified model whose output is to be forecasted, specified as one of the following:

If a model is unavailable, estimate sys from PastData using commands such as ar, arx, armax, nlarx, and ssest.

PastData — Past input-output time-domain dataiddata object | matrix of doubles

Past input-output time-domain data, specified as one of the following:

  • iddata object — Specify as an iddata object when you have past input data in addition to past output data. For time-series data, specify as an iddata object with no inputs if you want to attach metadata like units and output name to your data, or if you have data from multiple experiments.

  • Matrix of doubles — For discrete-time models only. Specify as an N-by-Ny matrix for time-series data (no inputs). Here, N is the number of observations and Ny is the number of outputs.

    For models withNu inputs, specify PastData as an N-by-(Ny+Nu) matrix.

Example: iddata(output,[])

K — Time horizon of forecastingpositive integer

Time horizon of forecasting, specified as a positive integer. The output, yf, is calculated K steps into the future, where K represents a multiple of PastData sample time.

FutureInputs — Future input values[] | matrix of doubles | iddata object | cell array of matrices

Future input values, specified as one of the following:

  • [] — Future input values are assumed to be zero, or equal to input offset levels (if they are specified in opts). FutureInputs is only relevant if sys is not a time-series model. For time-series models, specify as [].

  • iddata object — Specify as an iddata object with no outputs.

  • K-by-Nu matrix of doubles — K is the forecast horizon, and Nu is the number of inputs.

    If you have data from multiple experiments, you can specify a cell array of matrices, one for each experiment in PastData.

opts — Forecast optionsforecastOptions option set

Forecast options, specified as a forecastOptions option set.

Output Arguments

collapse all

yf — Forecasted responseiddata object

Forecasted response, returned as an iddata object. yf is the forecasted response at times after the last sample time in PastData. yf contains data for the time interval PastData.Tstart+(N+1:N+K)*PastData.Ts. N is the number of samples in PastData.

x0 — Estimated initial statescolumn vector | cell array

Estimated initial states at the start of forecasting, returned as a column vector of size equal to number of states.

If PastData is multiexperiment, x0 is a cell array of size Ne, where Ne is the number of experiments.

When sys is not a state-space model (idss, idgrey, or idnlgrey), the definition of states depends on if sys is linear or nonlinear:

  • Linear model (idpoly, idproc, idtf) – sys is converted to a discrete-time state-space model, and x0 is returned as the states of the converted model at a time-point beyond the last data in PastData.

    If conversion of sys to idss is not possible, x0 is returned empty. For example, if sys is a MIMO continuous-time model with irreducible internal delays.

  • Nonlinear model (idnlhw or idnlarx) — For a definition of the states of idnlarx and idnlhw models, see Definition of idnlarx States, and Definition of idnlhw States.

sysf — Forecasting modeldiscrete-time idss | idnlarx | idnlhw | cell array of models

Forecasting model, returned as one of the following:

  • Discrete-time idss — If sys is a discrete-time idss model, sysf is the same as sys. If sys is a linear model that is not a state-space model (idpoly, idproc, idtf), or is a continuous-time state-space model (idss, idgrey), sys is converted to a discrete-time idss model. The converted model is returned in sysf.

  • idnlarx — If sys is a nonlinear ARX model, sysf is the same as sys.

  • idnlhw — If sys is a Hammerstein-Wiener model, sysf is the same as sys.

  • idnlgrey — If sys is a nonlinear grey-box model, sysf is the same as sys.

  • Cell array of models — If PastData is multiexperiment, sysf is an array of Ne models, where Ne is the number of experiments.

Simulation of sysf using sim, with inputs, FutureInputs, and initial conditions, x0, yields yf as the output. For time-series models, FutureInputs is empty.

yf_sd — Estimated standard deviations of forecasted responsematrix | cell array

Estimated standard deviations of forecasted response, returned as a K-by-Ny matrix, where K is the forecast horizon, and Ny is the number of outputs.

If PastData is multiexperiment, yf_sd is a cell array of size Ne, where Ne is the number of experiments.

yf_sd is empty if sys is a nonlinear ARX (idnlarx) or Hammerstein-Wiener model (idnlhw).

x — Forecasted state trajectorymatrix | cell array

Forecasted state trajectory, returned as a K-by-Nx matrix, where K, the forecast horizon and Nx is the number of states. x are the states of the forecasting model.

If PastData is multiexperiment, x is a cell array of size Ne, where Ne is the number of experiments.

If sys is linear model other than a state-space model (not idss or idgrey), then it is converted to a discrete-time state-space model, and the states of the converted model are calculated. If conversion of sys to idss is not possible, x is returned empty. For example, if sys is a MIMO continuous-time model with irreducible internal delays.

x is empty if sys is a nonlinear ARX (idnlarx) or Hammerstein-Wiener model (idnlhw).

x_sd — Estimated standard deviations of forecasted statesmatrix | cell array

Estimated standard deviations of forecasted states, returned as a K-by-Ns matrix, where K, the forecast horizon and Ns is the number of states.

If PastData is multiexperiment, x_sd is a cell array of size Ne, where Ne is the number of experiments.

If sys is linear model other than a state-space model (not idss or idgrey), then it is converted to a discrete-time state-space model, and the states and standard deviations of the converted model are calculated. If conversion of sys to idss is not possible, x_sd is returned empty. For example, if sys is a MIMO continuous-time model with irreducible internal delays.

x_sd is empty if sys is a nonlinear ARX (idnlarx) or Hammerstein-Wiener model (idnlhw).

Introduced in R2012a

Was this topic helpful?