Effacer les filtres
Effacer les filtres

NARX: Outputs are known today, target is not, I want to get estimated target value put in today position. Do I use the one step ahead method? Or the "default" method?

3 vues (au cours des 30 derniers jours)
% This is a basic question,but I have managed to confuse myself. I have already searched for prior answers unsuccessfully, and while there is likely one one among the many hundreds of submissions from over the past decade, I have not been to locate it.
% I have a target vector and several input vectors aligned with the target
% in time. That is, on any given day the input values are known, but the
% actual target value is not. The idea is to use the the input values to
% try to estimate the target value.
% first I will show the "default" method as returned as a simple script by
% MATLAB
% then I will show the one-step ahead prediction variant
% Which is correct for my goal? That is, whcih gives the correct alignment
% of the inputs, targets and estimated targets?
% For the "default" method I do the following:
% gather the n inputs for days=1,2,3,... into n columns, one for each input
% put the target for days=1,2,3,... into 1 column
% prepare inputs and targets
X = tonndata(inputs,false,false);
T = tonndata(target,false,false);
% choose the training function
trainFcn = 'trainlm';
% create a nonlinear autoregressive network
inputDelays = 1:4;
feedbackDelays = 1:4;
hiddenLayerSize = 10;
net = narxnet(inputDelays,feedbackDelays,hiddenLayerSize,'open',trainFcn);
% prepare for training and simulation
[x,xi,ai,t] = preparets(net,X,{},T);
net.divideParam.trainRatio = 70/100;
net.divideParam.valRatio = 15/100;
net.divideParam.testRatio = 15/100;
% train the network
[net,tr] = train(net,x,t,xi,ai);
% test the network
y = net(x,xi,ai);
% Next, I implement the alternative method, the Step-Ahead Prediction Network
nets = removedelay(net);
[xs,xis,ais,ts] = preparets(nets,X,{},T);
ys = nets(xs,xis,ais);
% The MATLAB comments in the script state that "The new network returns
% the same outputs as the original network, but outputs are shifted left
% one timestep." Well, that's true, except that there is also an additional new value
% at the end of ys not present in y!
% My question is: Which method puts the estimated target value into the
% same day as the outputs used to estimate it?

Réponse acceptée

Himanshu
Himanshu le 31 Mar 2023
Hello Kevin,
I understand that you have a target vector and several input vectors aligned with the target in time. You want to know which method, the "default" method or the one-step-ahead prediction (Step-Ahead Prediction Network) variant, aligns the estimated target value with the inputs used to estimate it.
The one-step-ahead prediction method, which uses the Step-Ahead Prediction Network, is the correct choice for your goal. This method is designed to predict the target value at time t using only the input values up to time t-1. This means that the estimated target value is aligned with the input values used for its estimation.
The "default" method does not align the estimated target value with the input values used for the estimation, as it considers the feedback from the target itself.
To implement the one-step-ahead prediction method, you can use the following code:
% remove the delay from the original network
nets = removedelay(net);
[xs,xis,ais,ts] = preparets(nets,X,{},T);
ys = nets(xs,xis,ais);
The output ys will be shifted left one timestep compared to y, and it will also have an additional value at the end. Based on the available input data, the step-ahead prediction network estimates each timestep, including the last one.
You can refer to the below documentation to understand more about the “removedelay” and "preparets" functions.
  2 commentaires
Kevin Johnson
Kevin Johnson le 31 Mar 2023
Thank you. I plan to reply to you in more detail later today. Kevin
Kevin Johnson
Kevin Johnson le 1 Avr 2023
OK, in the following code, signal and inputs are all known up until day t.
The output, signal_pred, is the same length as signal. Does it contain the predicted signal for times 2:t+1?
% Predict signal one day ahead
signal_pred=NN_predict_signal(signal,input1,input2,input3);
%% NN
function signal_pred = NN_predict_signal(signal, input1, input2, input3)
% Prepare target data
target = [NaN; diff(signal)];
target(isnan(target)) = 0;
r = length(target);
% Neural network inputs
input.p1 = input1;
input.p2 = input2;
input.p3 = input3;
inputs = [input.p1 input.p2 input.p3];
inputs = [zeros(1, size(inputs, 2)); diff(inputs)];
inputs(isnan(inputs(:))) = 0;
inputs = (inputs - nanmedian(inputs)) ./ nanstd(inputs);
% Training function
trainFcn = 'trainlm';
% Create a NARX network
inputDelays = 1:4;
feedbackDelays = 1:4;
hiddenLayerSize = 10;
net = narxnet(inputDelays, feedbackDelays, hiddenLayerSize, 'open', trainFcn);
net.trainParam.showWindow = false;
% Designate division of data
net.divideParam.trainRatio = 70 / 100;
net.divideParam.valRatio = 15 / 100;
net.divideParam.testRatio = 15 / 100;
% Prepare inputs and targets
X = tonndata(inputs, false, false);
T = tonndata(target, false, false);
[x, xi, ai, t] = preparets(net, X, {}, T);
% Train the network
net = train(net, x, t, xi, ai);
% Step-ahead prediction
nets = removedelay(net);
[xs, xis, ais] = preparets(nets, X, {}, T);
ys = nets(xs, xis, ais);
num = r - length(ys);
yfit = [NaN(num, 1); cell2mat(ys)'];
% Construct predicted signal
yfit(isnan(yfit)) = 0;
signal_pred = NaN(r, 1);
for g = 2:r
signal_pred(g) = signal(g - 1) + yfit(g);
end
end % Function end

Connectez-vous pour commenter.

Plus de réponses (0)

Produits


Version

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by