"clc clear all close all % Stock Morket/Exchange Forecasing Using LSTM % A…
- User
"clc clear all close all
% Stock Morket/Exchange Forecasing Using LSTM % A sequence-to-sequence regression LSTM Network. % This Example uses the data set of Tehran Stock in 2022.
%% Step1: load and Prepare Data
[Xtrain,YTrain,Params] = LoadPrepareData2();
%% Step2: Create a LSTM network NumHiddenUnits = 100; Layers = [ sequenceInputLayer(1)
lstmLayer(NumHiddenUnits,... "InputWeightsInitializer",'zeros',... 'RecurrentWeightsInitializer','he') fullyConnectedLayer(1) regressionLayer];%% Step3: Training Options maxEpochs = 1000; InitialLearnRate = 0.04; Options = trainingOptions("adam",... "Plots","training-progress",... 'MaxEpochs',maxEpochs,... 'InitialLearnRate',InitialLearnRate,... 'LearnRateSchedule','piecewise',... 'LearnRateDropPeriod',100,... 'LearnRateDropFactor',0.9,... 'ExecutionEnvironment','gpu',... 'Shuffle','never');
%% Step4: Train the Network
LstmNet = trainNetwork(Xtrain,YTrain,Layers,Options); %% Step5: Forecasting the Test Data % nTimeStep = 38; % YPredict = ForecastingWithPredictedValues(... % LstmNet,Xtrain,nTimeStep,Params);
%% Step5: Forecasting the Test Data nTimeStep = 38; YPredict = zeros(size(Xtrain,1), nTimeStep);
% Predict future values bigger than previous values LstmNet = resetState(LstmNet); LstmNet = predictAndUpdateState(LstmNet,Xtrain); [LstmNet,YPredict(:,1)] = predictAndUpdateState(LstmNet,Xtrain(end));
for i = 2:nTimeStep [LstmNet,YPredict(:,i)] = predictAndUpdateState(LstmNet,YPredict(:,i-1)); end
yp = YPredict(:,1);
% Find indices for values less than or equal to the previous values idx = find(YPredict(:,2:nTimeStep) <= YPredict(:,1:nTimeStep-1));
while ~isempty(idx) % Find the indices where predicted value is less than or equal to previous value temp_idx = idx(yp(idx) <= YPredict(:,idx-1));
if ~isempty(temp_idx) % Check that the indices are within the correct range temp_idx = temp_idx(temp_idx >= 1 & temp_idx <= nTimeStep); % Only update YPredict for valid indices if ~isempty(temp_idx) % Update the predicted values for the indices in temp_idx [LstmNet, yp(temp_idx)] = predictAndUpdateState(LstmNet, YPredict(:,temp_idx)); YPredict(:,temp_idx) = yp(:,temp_idx); end end
% Find new indices where predicted value is less than or equal to previous value idx = find(YPredict(:,2:nTimeStep) <= YPredict(:,1:nTimeStep-1)); end
%% Unstandardize the predictions Xtrain = XtrainParams.s + Params.m; YPredict = YPredictParams.s + Params.m;
%% Plot figure,plot(Xtrain,'LineWidth',2); hold on
idx = numel(Xtrain):(numel(Xtrain) + nTimeStep); plot(idx,[Xtrain(end) YPredict],"LineWidth",2,'LineStyle','-') xlabel('Time [Day]') ylabel('Close Values') title('LSTM Forecasting') legend('Observed','Forecasted')"
this code give me the following error "Index exceeds the number of array elements. Index must not exceed 1.
- Assistant
- User
this code give me this error "Index exceeds the number of array elements. Index must not exceed 1.
Error in MainStockForecating2 (line 69) temp_idx = idx(yp(idx) <= YPredict(:, idx(1:end-1)));"
- Assistant