Chapter 2 Project Python

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

CHAPTER 2

CRUDE PRICE PREDICTION THROUGH LSTM


2.1 Introduction
The crude oil market is one of the most important and volatile global commodity markets.
Accurate prediction of crude oil prices is a significant concern for oil-producing and oil-
consuming countries, oil companies, traders, and investors. Forecasting crude oil prices has
always been a challenging task for analysts due to the complex nature of oil prices and the
involvement of numerous factors, such as economic, geopolitical, and environmental factors,
which influence the crude oil market.

In recent years, machine learning techniques, particularly deep learning algorithms such as
Long Short-Term Memory (LSTM), have shown promising results in predicting crude oil
prices. LSTMs are a type of recurrent neural network (RNN) that can capture long-term
dependencies in time series data. LSTM networks have been successfully used in various
fields, including finance, to predict stock prices, foreign exchange rates, and commodity
prices.

This report aims to apply LSTM networks to predict crude oil prices. We will explore the
crude oil price data and perform some pre-processing steps to prepare the data for LSTM.
Then, we will develop an LSTM model, train it on historical crude oil price data, and use it to
predict future crude oil prices. Finally, we will evaluate the performance of the LSTM model
and compare it with other traditional time-series forecasting models.

2.2 Library’s used


1. `numpy` (short for "Numerical Python"): a library for numerical computing in Python. It
provides support for arrays and matrices, as well as mathematical operations on them.

2. `tensorflow`: an open-source library for machine learning and deep learning. It allows
users to build and train various neural network architectures, including recurrent neural
networks like LSTM.
3. `pandas`: a library for data manipulation and analysis in Python. It provides data structures
for efficiently storing and manipulating large datasets, as well as functions for data cleaning
and transformation.

4. `matplotlib`: a library for creating visualizations and plots in Python. It provides a variety
of functions for creating line plots, scatter plots, histograms, and more.

5. `sklearn` (short for "Scikit-learn"): a library for machine learning and data analysis in
Python. It provides a variety of tools for data preprocessing, model selection, and evaluation.

6. `seaborn`: a library for creating visualizations and plots in Python. It provides a high-level
interface for creating aesthetically pleasing and informative visualizations.

2.3 Snaps of the codes


2.3 Conclusion
This is a Python script for implementing a time series forecasting model using LSTM neural
networks. The data used for training and forecasting is the daily opening price of the
Brazilian Real Futures (BZ=F) from Yahoo Finance. The script first imports necessary
libraries including numpy, pandas, matplotlib, seaborn, and TensorFlow.

The data is then loaded and pre-processed, where the date column is converted to datetime
format, a subset of the data is selected, and the data is standardized using the StandardScaler
function from sklearn. The training and testing data is then created by creating sequences of
past data for input and future data for output.

A sequential LSTM model is then built with two LSTM layers, a dropout layer, and a dense
layer. The model is then compiled with the Adam optimizer and mean squared error loss
function. The model is trained with the training data and validation split of 0.1 for 5 epochs
with a batch size of 16.

The predicted prices for the next 15 days are generated using the trained model and the
CustomBusinessDay and USFederalHolidayCalendar functions from pandas are used to
predict future business days. The predicted prices are then inverse transformed and plotted
against the original data using seaborn and matplotlib.

The script provides a useful example for implementing a time series forecasting model using
LSTM neural networks with a real-world financial dataset.

You might also like