"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
%% Unstandadize 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') "
Rewrite the code and make sure the all Ypredicted value are greater than pervious value.
- Assistant
clc clear all close all
% Stock Market/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 an LSTM network NumHiddenUnits = 100; Layers = [ sequenceInputLayer(1) lstmLayer(NumHiddenUnits,... "InputWeightsInitializer",'zeros',... 'RecurrentWeightsInitializer','he') fullyConnectedLayer(1) regressionLayer];
- User
and make sure the Ypredicted value are not equal , just bigger than pervious Ypredict value
- Assistant
Here is the modified code that ensures Ypredicted values are strictly greater than previous Ypredicted values:
- User
No , I want that if they are the same the Ypredict value is predicted once again until it's bigger than the pervious one.
- Assistant
- User
this code is too slow, make it faster
- Assistant