Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· OpenAIAi2 WildChat-4.8M (AI2)

"clc clear all close all % Stock Morket/Exchange Forecasing Using LSTM % A…

  1. 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);

    % Loop through each time step and update YPredict using the LSTM network for i = 2:nTimeStep % Find the indices where the predicted value is less than or equal to the previous value idx = find(YPredict(:, i) <= YPredict(:, i-1));

    % Check if idx is empty or has only one element if ~isempty(idx) && numel(idx) > 1 % Filter out the indices where the predicted value is greater than the previous value temp_idx = idx(yp(idx) <= YPredict(:, idx(1:end-1))); else temp_idx = []; end

    % Check if there are any filtered indices within range if ~isempty(temp_idx) && all(temp_idx >= 1) && all(temp_idx <= nTimeStep-1) % Update the LSTM network using the filtered indices [LstmNet, yp(temp_idx)] = predictAndUpdateState(LstmNet, YPredict(:, temp_idx)); YPredict(:, temp_idx+1) = yp(:, temp_idx); end 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 give me this fucking error, please solve it

    error : "Index in position 2 exceeds array bounds. Index must not exceed 1.

    Error in MainStockForecating2 (line 74) temp_idx = yp(:, i) > YPredict(:, i-1);"

  2. Assistant

    The error message indicates that you are trying to access an index that exceeds the bounds of the array. Specifically, in line 74 of your code, you are trying to access an element using an index greater than 1 in the second position of the index.

    To fix this error, you need to check the dimensions of yp and YPredict variables and make sure they match. It seems like yp might not have the same number of elements as YPredict in the second dimension.

    One possible solution is to replace yp(:, i) with yp in line 74 and update the conditional statement accordingly.

    Here's the updated code:

    if ~isempty(temp_idx) && all(temp_idx >= 1) && all(temp_idx <= nTimeStep-1) % Update the LSTM network using the filtered indices [LstmNet, yp(temp_idx)] = predictAndUpdateState(LstmNet, YPredict(:, temp_idx)); YPredict(:, temp_idx+1) = yp; end

    Try this and see if it resolves the error.