How to update code so that future dates are used for forecsting?

2 ビュー (過去 30 日間)
Manny
Manny 2024 年 12 月 9 日
コメント済み: Manny 2024 年 12 月 10 日
Hi everyone
I am new to MATLAB. I have a code I got from a book I am reading. It separates the data into training and validation/testing sets. The code is forecasting based on the valdiation/testing set but that is still historical actual data. I want the code to give me predictions for the future and then I will compare the predictions against actual future values. How do I modify my code so I can get 10 future predictions? I am going to be importing the predictions into SQL Server so the format in MATLAB needs to allow me to import into a table in SQL Sever.
The desired layout is a 1 by 12 matrix:
[SYMBOL FORECAST_DATE_TIME DAY1 DAY2 DAY3 DAY4 DAY5 DAY6 DAY7 DAY7 DAY8 DAY9 DAY10]
I can add SYMBOL and FORECAST_DATE_TIME so I don't need help with that part.
Here is the code (just the parts used for the prediction):
n = length(CIV.COMPOSITE_IMPLIED_VOLATILITY);
nTrain = floor(0.8*n);
sTrain = CIV.COMPOSITE_IMPLIED_VOLATILITY(1:nTrain);
sTest = CIV.COMPOSITE_IMPLIED_VOLATILITY(nTrain+1:n);
sTestNorm = (sTest-mu)/sigma;
sTest = sTestNorm(1:end-1);
net = trainNetwork(xTrain.',yTrain.',layers,options);
yPred = predict(net,sTest.');
yPred(1) = yTrain(end-1);
yPred(2) = yTrain(end);
I want to keep the existing prediction but I want to add another one that is for future 10 days starting tomorrow. This is stock market data so there will be gaps (ie no trading happens on the weekends/holidays) so if actual dates are too trouble then having a generic "Day1", "Day2" etc label is enough. I can assign the actual dates in SQL Server when I process the data there.
Thank you
  1 件のコメント
Manny
Manny 2024 年 12 月 9 日
編集済み: Manny 2024 年 12 月 9 日
I have been working on this. I found this resource on matlab website:
I am following the closed-loop forecasting logic. I followed the steps they listed but I am getting an error. Is someone able to help me?
Code:
numPredictionTimeSteps = 10;
net = resetState(net);
offset = size(CIV.COMPOSITE_IMPLIED_VOLATILITY,1);
[Z,state] = predict(net,CIV.COMPOSITE_IMPLIED_VOLATILITY(1:offset,:));
net.State = state;
Y = zeroes(numPredictionTimeSteps);
Y(1,:) = Z(end,:);
for t = 2:numPredictionTimeSteps
[Y(t,:),state] = predict(net,Y(t-1,:));
net.State = state;
end
Error:
Error using SeriesNetwork/predict
Too many output arguments.
Error in ForecastVolatilityLSTMv1 (line 103)
[Z,state] = predict(net,CIV.COMPOSITE_IMPLIED_VOLATILITY(1:offset,:));
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Thank you

サインインしてコメントする。

回答 (1 件)

Pratik
Pratik 2024 年 12 月 10 日
Hi Manny,
The error of "Too many output arguments" occurs when a function you are trying to call expects fewer output arguments, than you have provided to it.
Please refer to this MATLAB Answer post for more information about the error:
From initial assessment it can be deduced that the 'predict' function needs to be checked. 'predict' function used here does not return state of the model. Please refer to the following documentation for more information:
  3 件のコメント
Pratik
Pratik 2024 年 12 月 10 日
Hi,
The predict function used in the code given in MATLAB example is of type 'dlnetwork' whereas from the error received I can see that your model is of type 'SeriesNetwork'. The predict function for 'dlnetwork' gives states as output however for 'SeriesNetwork' we just get the prediction.
To fix the issue, consider making the network using 'dlnetwork'.
Please refer to following documentation for more information:
Also consider refering to the documentation of 'SeriesNetwork' for more information about it's predict function:
Manny
Manny 2024 年 12 月 10 日
I fixed up the code but I am not getting the right result. The output is the wrong size. It is showing as 10 by 2654 which is wrong. It should be 1 by 10. Notice how yPred is a 1 by 530 result which makese sense given that it is the testing/valdiation set. The Y variable is the future 10 day forecast so it should be 1 by 10. How do fix my code?
Code:
yPred = predict(net,sTest.');
yPred(1) = yTrain(end-1);
yPred(2) = yTrain(end);
yPred = sigma*yPred+mu;
offset = size(CIV.COMPOSITE_IMPLIED_VOLATILITY,1);
Z = predict(net,CIV.COMPOSITE_IMPLIED_VOLATILITY(1:offset,:).');
numPredictionTimeSteps = 10;
%Y = zeros(numPredictionTimeSteps);
Y(1,:) = Z(end,:);
for t = 2:numPredictionTimeSteps
Y(t,:) = predict(net,Y(t-1,:));
end
Variables:
Thank you

サインインしてコメントする。

カテゴリ

Help Center および File ExchangePretrained Networks from External Platforms についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by