8

This code predicts the values of a specified stock up to the current date but not a date beyond the training dataset. This code is from an earlier question I had asked and so my understanding of it is rather low. I assume the solution would be a simple variable change to add the extra time but I am unaware as to which value needs to be manipulated.

import pandas as pdimport numpy as npimport yfinance as yfimport osimport matplotlib.pyplot as pltfrom IPython.display import displayfrom keras.models import Sequentialfrom keras.layers import LSTM, Densefrom sklearn.preprocessing import MinMaxScaleros.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'pd.options.mode.chained_assignment = None# download the datadf = yf.download(tickers=['AAPL'], period='2y')# split the datatrain_data = df[['Close']].iloc[: - 200, :]valid_data = df[['Close']].iloc[- 200:, :]# scale the datascaler = MinMaxScaler(feature_range=(0, 1))scaler.fit(train_data)train_data = scaler.transform(train_data)valid_data = scaler.transform(valid_data)# extract the training sequencesx_train, y_train = [], []for i in range(60, train_data.shape[0]):    x_train.append(train_data[i - 60: i, 0])    y_train.append(train_data[i, 0])x_train = np.array(x_train)y_train = np.array(y_train)# extract the validation sequencesx_valid = []for i in range(60, valid_data.shape[0]):    x_valid.append(valid_data[i - 60: i, 0])x_valid = np.array(x_valid)# reshape the sequencesx_train = x_train.reshape(x_train.shape[0], x_train.shape[1], 1)x_valid = x_valid.reshape(x_valid.shape[0], x_valid.shape[1], 1)# train the modelmodel = Sequential()model.add(LSTM(units=50, return_sequences=True, input_shape=x_train.shape[1:]))model.add(LSTM(units=50))model.add(Dense(1))model.compile(loss='mean_squared_error', optimizer='adam')model.fit(x_train, y_train, epochs=50, batch_size=128, verbose=1)# generate the model predictionsy_pred = model.predict(x_valid)y_pred = scaler.inverse_transform(y_pred)y_pred = y_pred.flatten()# plot the model predictionsdf.rename(columns={'Close': 'Actual'}, inplace=True)df['Predicted'] = np.nandf['Predicted'].iloc[- y_pred.shape[0]:] = y_preddf[['Actual', 'Predicted']].plot(title='AAPL')display(df)plt.show()
user11989081's user avatar
user11989081
8,75211 gold badges34 silver badges44 bronze badges
askedNov 9, 2021 at 23:47
WillTheWA's user avatar
0

1 Answer1

23

You could train your model to predict a future sequence (e.g. the next 30 days) instead of predicting the next value (the next day) as it is currently the case.

In order to do that, you need to define the outputs asy[t: t + H] (instead ofy[t] as in the current code) wherey is the time series andH is the length of the forecast period (i.e. the number of days ahead that you want to forecast). You also need to set the number of outputs of the last layer equal toH (instead of equal to1 as in the current code).

You can still define the inputs asy[t - T: t] whereT is the length of the lookback period (or number of timesteps), and therefore the model's input shape is still(T, 1). The lookback periodT is usually longer than the forecast periodH (i.e.T > H) and it's often set equal to a multiple ofH (i.e.T = m * H wherem > 1 is an integer.).

enter image description here

import numpy as npimport pandas as pdimport yfinance as yfimport tensorflow as tffrom tensorflow.keras.layers import Dense, LSTMfrom tensorflow.keras.models import Sequentialfrom sklearn.preprocessing import MinMaxScalerpd.options.mode.chained_assignment = Nonetf.random.set_seed(0)# download the datadf = yf.download(tickers=['AAPL'], period='1y')y = df['Close'].fillna(method='ffill')y = y.values.reshape(-1, 1)# scale the datascaler = MinMaxScaler(feature_range=(0, 1))scaler = scaler.fit(y)y = scaler.transform(y)# generate the input and output sequencesn_lookback = 60  # length of input sequences (lookback period)n_forecast = 30  # length of output sequences (forecast period)X = []Y = []for i in range(n_lookback, len(y) - n_forecast + 1):    X.append(y[i - n_lookback: i])    Y.append(y[i: i + n_forecast])X = np.array(X)Y = np.array(Y)# fit the modelmodel = Sequential()model.add(LSTM(units=50, return_sequences=True, input_shape=(n_lookback, 1)))model.add(LSTM(units=50))model.add(Dense(n_forecast))model.compile(loss='mean_squared_error', optimizer='adam')model.fit(X, Y, epochs=100, batch_size=32, verbose=0)# generate the forecastsX_ = y[- n_lookback:]  # last available input sequenceX_ = X_.reshape(1, n_lookback, 1)Y_ = model.predict(X_).reshape(-1, 1)Y_ = scaler.inverse_transform(Y_)# organize the results in a data framedf_past = df[['Close']].reset_index()df_past.rename(columns={'index': 'Date', 'Close': 'Actual'}, inplace=True)df_past['Date'] = pd.to_datetime(df_past['Date'])df_past['Forecast'] = np.nandf_past['Forecast'].iloc[-1] = df_past['Actual'].iloc[-1]df_future = pd.DataFrame(columns=['Date', 'Actual', 'Forecast'])df_future['Date'] = pd.date_range(start=df_past['Date'].iloc[-1] + pd.Timedelta(days=1), periods=n_forecast)df_future['Forecast'] = Y_.flatten()df_future['Actual'] = np.nanresults = df_past.append(df_future).set_index('Date')# plot the resultsresults.plot(title='AAPL')

enter image description here

Seethis answer for a different approach.

answeredNov 10, 2021 at 11:16
user11989081's user avatar
Sign up to request clarification or add additional context in comments.

2 Comments

How can this be done for multivariate time series forecasting when we have other independent variables such high, low , volume etc. Is it possible to provide the update to incorporate additional variables
How can this be done for multivariate time series forecasting when we have other independent variables such high, low , volume etc and use those to predict close and do the forecast for future time. Is it possible to provide the update to incorporate additional variables
Protected question. To answer this question, you need to have at least 10 reputation on this site (not counting theassociation bonus). The reputation requirement helps protect this question from spam and non-answer activity.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.