Project

Download as pdf or txt
Download as pdf or txt
You are on page 1of 39

COMPARATIVE ANALYSIS OF

TIME SERIES MODELS FOR


PRICE PREDICTION
OBJECTIVE
This project aims to compare and evaluate the performance of four
popular machine learning models:

• Recurrent Neural Network (RNN)


• Long Short-Term Memory (LSTM)
• Bi-Directional LSTM
• Convolutional Neural Network - Long Short-Term Memory(CNN-
LSTM)
FINANCIAL MARKET
FORECASTING

• Stock trading and investing are crucial for individuals and institutions as they provide
opportunities to grow wealth, achieve financial goals, and contribute to economic
development.
• Stock price forecasting helps investors make informed decisions, manage risk and
optimize their investment portfolios based on predicted future price movements
WHY TIME SERIES ANALYSIS ?

• Time series analysis is a statistical method used to analyze and interpret patterns, trends,
and relationships within a sequence of data points collected over time.

• It can be used to identify complex patterns and make unbiased decision free from
emotional and external influences.
MACHINE LEARNING
Machine learning is a subset of artificial intelligence that
enables computers to learn from data using statistical and
computational techniques, without being explicitly
programmed. The aim of machine learning is to create
algorithms that can learn from data patterns and make
predictions or choices.
Neural Network
• Neural networks are a type of machine learning model inspired by the human brain's structure.
• They consist of interconnected nodes called neurons organized into layers:
⚬ an input layer,
⚬ one or more hidden layers, and
⚬ an output layer.
• Each neuron receives input, performs a computation (often involving weights and biases), and
passes the output to the next layer through activation functions.
• Neural networks learn from data by adjusting the weights and biases associated with
connections between neurons, enabling them to make predictions or classifications.
IMPLEMENTATIONS
Data
df=yf.download(”TSLA”,period=”60mo”,interval=”1d”)
Open Price
Preprocessing
b_size = 32 # Batch Size
epoch = 200 # Number of epochs
train_per = 0.8 # 80% of data taken for training
w_size = 30 # Data for consecutive 30 days are used to predict the next day's data
f_size = 10 # Open price for 10 future days

#test-train split
t_size=int(len(df)*train_per)
data_train = df.iloc[:t_size, 1:2].values
data_test = df.iloc[t_size:, 1:2].values
data_total = df.iloc[:,1:2]#open price

#Normalisation
sc = MinMaxScaler(feature_range = (0, 1))
training_scaled = sc.fit_transform(data_train)
total_input = sc.transform(total_input)
Train

#Train Data
x_train = []
y_train = []
for iter in range(w_size, t_size):
x_train.append(training_scaled[iter-w_size:iter, 0])
y_train.append(training_scaled[iter, 0])
x_train, y_train=np.array(x_train), np.array(y_train)
x_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1], 1))
Test
#Test data
total_input = data_total[len(data_total) - len(data_test) - w_size:].values
total_input = total_input.reshape(-1, 1)
x_test = []
y_test=[]

# Calculate the maximum number of blocks of length w_size that can be created

blocks = len(total_input) - w_size

for iter in range(blocks):


x_test.append(total_input[iter:iter+w_size, 0])
y_test.append(total_input[iter+w_size, 0])

#new addition
x_test = np.array(x_test)
x_test = np.reshape(x_test, (x_test.shape[0], x_test.shape[1], 1))
Graph
RNN - Recurrent Neural Network
• Recurrent Neural Networks (RNNs) are a class of artificial neural networks designed to effectively
process sequential data by preserving information through time.
• RNNs consist of interconnected nodes (neurons) organized in layers, much like other neural
networks, but with a crucial addition of loops that allow them to retain information across time
steps.
• The fundamental unit of an RNN is the recurrent neuron, which processes an input and maintains a
hidden state that serves as its memory.
RNN - Recurrent Neural Network
RN
N
initializer = RandomNormal(mean=0.0, stddev=0.05,seed=5)
algo = Sequential()
algo.add(SimpleRNN(units=60, return_sequences=True, input_shape=(x_train.shape[1], 1),
kernel_initializer=initializer))
algo.add(Dropout(0.2))
algo.add(SimpleRNN(units=60))

algo.add(Dense(units=1))
algo.compile(optimizer = 'adam', loss = 'mean_squared_error')
RNN : Evaluation

Mean Absolute Error (MAE): 6.52


Mean Squared Error (MSE): 66.89
Root Mean Squared Error (RMSE): 8.18
RNN
LSTM-Long Short Term Memory
• The Long Short-Term Memory (LSTM) model is a type of recurrent neural network that
can capture long-term dependencies in time series data.
• In stock market forecasting, the LSTM model can be used to analyze historical price and
volume data to make predictions about future trends.
• LSTM model is able to handle noisy and complex data, which is often the case in stock
market data.
• Model can learn from past mistakes and adjust its predictions accordingly, making it well-
suited for dynamic and unpredictable markets.
• However, one of the weaknesses of the LSTM model is its computational complexity, which
can make it difficult to train and optimize for large datasets.
LSTM
initializer = RandomNormal(mean=0.0, stddev=0.05, seed = 5)
algo = Sequential()
algo.add(LSTM(units = 60, return_sequences = True, input_shape = (x_train.shape[1], 1),
kernel_initializer=initializer))
algo.add(Dropout(0.2))
algo.add(LSTM(units = 60))
#algo.add(Dropout(0.2))
algo.add(Dense(units = 1))
algo.compile(optimizer = 'adam', loss = 'mean_squared_error')
LSTM : Evaluation

Mean Absolute Error (MAE): 5.26


Mean Squared Error (MSE): 45.55
Root Mean Squared Error (RMSE): 6.75
LSTM :
Bi-Directional LSTM
• Bi-LSTM processes sequential data in both forward and backward directions,
leveraging two LSTM layers to capture both past and future context of the input
sequence.
• This dual processing helps the network capture long-term dependencies and
improve its ability to model complex patterns in sequential data.
• By combining outputs from both directions, Bi-LSTM creates enriched context
representations, vital for nuanced predictions in applications like natural
language processing and time series analysis.
Bi-Directional LSTM
Bi-Directional LSTM

initializer = RandomNormal(mean=0.0, stddev=0.05, seed=5)


algo = Sequential()
algo.add(Bidirectional(LSTM(60,return_sequences=True,activation='relu',input_shape =
(x_train.shape[1], 1),kernel_initializer=initializer)))
algo.add(Dropout(0.2))
algo.add(Bidirectional(LSTM(60)))
algo.add(Dense(1))
algo.compile(optimizer = 'adam', loss = 'mean_squared_error')
Bi-LSTM : Evaluation

Mean Absolute Error (MAE): 3.83


Mean Squared Error (MSE): 22.39
Root Mean Squared Error (RMSE): 4.73
Bi-LSTM :
CNN-
LSTM
• The CNN-LSTM model is a powerful tool for time series analysis that combines the strengths
of both convolutional neural networks and long short-term memory networks.

• The model works by first using a CNN to extract important features from the input data, such
as trends and patterns, which are then fed into an LSTM network for further analysis and
prediction.

• This approach allows the model to capture both short-term and long-term dependencies in
the data, making it well-suited for predicting stock prices over extended periods of time.
• One of the key strengths of the CNN-LSTM model is its ability to handle large volumes of data.
Because the model can process data in parallel, it is able to analyze vast amounts of historical
stock market data quickly and efficiently.

• This model is able to adapt to changing market conditions, making it more robust than some
other models that may struggle with sudden shifts in market trends.

• One potential weakness of the CNN-LSTM model is that it can be computationally expensive to
train, requiring significant processing power and time.
CNN-
LSTM
CNN-LSTM
initializer = RandomNormal(mean = 0.0, stddev = 0.05,seed =5 )
model = Sequential()
model.add(TimeDistributed(Conv1D(128, kernel_size=1, activation='relu', input_shape=(None,w_size,1),
kernel_initializer=initializer)))
model.add(TimeDistributed(MaxPooling1D(2)))
model.add(TimeDistributed(Conv1D(256, kernel_size=1, activation='relu')))
model.add(TimeDistributed(MaxPooling1D(2)))
model.add(TimeDistributed(Conv1D(512, kernel_size=1, activation='relu')))
model.add(TimeDistributed(MaxPooling1D(2)))
model.add(TimeDistributed(Flatten()))
model.add(Bidirectional(LSTM(60,return_sequences=True)))
model.add(Dropout(0.2))
model.add(Bidirectional(LSTM(60)))
model.add(Dense(1, activation='linear'))
model.compile(optimizer='adam', loss='mse')
model.fit(train_X, train_label, validation_data=(test_X,test_label), epochs=epoch,batch_size=b_size,shuffle =False)
print(model.evaluate(test_X,test_label))
CNN-LSTM : Evaluation

MAE: 15.867282467503701
MSE: 391.43820034302917
RMSE: 19.784797202474156
CNN-LSTM :
Comparison
Conclusion
• Bi-LSTM model has been found to have less error than other models for given dataset.
• Due to the lack of Spatial-Temporal patterns CNN-LSTM is found have comparetively large
error
• For CNN-LSTM model loss is found to be converging for number of epochs near 300
• For other models loss is converging between 120 to 150 epochs
Future Scope
• Optimization and hyperparameter tuning
• Real-time prediction and deployment
• Feature engineering and external data incorporating
• Develop more interpretable models, leveraging techniques like attention mechanisms or
SHAP values
THANK
YOU

You might also like