# Approaches for Time Series Forecasting with Different Input Types

As we know, one of the most effective algorithms to predict Time Series data is the LSTM(Long Short Term Memory) .In this article, I am going to show you how to build and deploy an LSTM Model for stock price forecasting in different forms of input data.

A common LSTM unit is composed of a **cell**, an **input gate**, an **output gate** and a **forget gate**. The cell remembers values over arbitrary time intervals and the three *gates* regulate the flow of information into and out of the cell. LSTM works well on time series prediction because of its capability to ‘remember’ vital information while ‘forgetting’ unimportant information.The advent of LSTM has solved 2 major problems Recurrent Neural Networks face: vanishing gradient and exploding gradient.You can check out further explanations about LSTM networks here.

For univariate input data, we need to get,rearrange and scale our data.We are using Goldman Sachs' share price downloaded from Yahoo Finance as a csv file.

We then arrange the data to fit perfectly into the LSTM network with the code below:

Always remember that the input to every LSTM layer must be three-dimensional with shape**(Samples, Timesteps, Features)**.However,for the **input_shape** argument, you only have to enter a 2D array of shape **(Timesteps, Features).**For univariate LSTM prediction, the number of features is 1(close price of the stock).Let the timesteps be 60(which means we are predicting the value of the future stock price using past 60 days of stock price as an input).The number of samples， in this case, is 947 for x_train and 192 for x_test.Therefore, the shape of x_train and x_test after arrangement of dimensions are (947, 60, 1) and (192, 60, 1) respectively.

The results are pretty decent.With two layers of 100 unit-LSTM networks, a Dropout layer of 0.4 and a learning rate of 0.0005, the model was able to achieve a validation loss of 0.0016 after 50 epochs.

As for Multi-variate input data, we would use a few main indicators( MACD, Bollinger Bands and RSI).The total number of features is 8 now.The data would be retrieved using a predefined **TechnicalIndicators() **class.Notice how the last row of X (features)is deleted(because it has no corresponding price to predict).The first row of Y(labels) is also deleted because it has no corresponding features.It is now safe to say that the MACD,Bollinger Bands,RSI and close price in the past 60 days are used to predict the close price of the following day.

The values are then reshaped to fulfill the correct input shape of the LSTM network:

Predictably, the accuracy of the multivariate model is much better than the univariate model.By using the same hyperparameters, the model achieved a validation loss of 7.5137e-04 after 50 epochs.

For Multi step forecast, we just need to rearrange the data to use past 60 days of data to predict **N** steps into the future and change the units of the **Dense** layer to** N. **The mean squared error showed linear growth against the value of **N**.

For example,we split the data into 4 parts, namely **a_train, b_train,c_test,d_test. a_train** and **c_test** contains data from the past 60 days while** b_train** and **d_tes**t contains data of the following 5 days. As you can guess,**c_test** and **d_test **will be used as validation data.

All the code for this article can be found here.

Questions?Thoughts?Ideas?Feel free to leave your comments below!

*References*: