Skip to content

Commit

Permalink
first draft article
Browse files Browse the repository at this point in the history
  • Loading branch information
rageSpin committed Jun 17, 2024
1 parent 362d8f3 commit 481d17b
Show file tree
Hide file tree
Showing 73 changed files with 974 additions and 361 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
196 changes: 184 additions & 12 deletions content/posts/finance/stock_prediction/GRU/index.md
Original file line number Diff line number Diff line change
@@ -1,40 +1,212 @@
---
title: "Microsoft Stock Prediction using LSTM or GRU"
title: "MSFT Stock Prediction using LSTM or GRU"
date: 2024-06-16T00:00:00+01:00
description: "Short Stock price analysis on MSFT, then a prediction is tested using GRU"
menu:
sidebar:
name: GRU
identifier: GRU
parent: finance
parent: stock_prediction
weight: 9
hero: images/stock-market-prediction-using-data-mining-techniques.jpg
tags: ["Finance", "Deep Learning", "Forecasting"]
categories: ["Finance"]
---

## Pick a stock commodity
## Introduction

'
...
'
In this article, we will explore time series data extracted from the **stock market**, focusing on prominent technology companies such as Apple, Amazon, Google, and Microsoft. Our objective is to equip data analysts and scientists with the essential skills to effectively manipulate and interpret stock market data.

To achieve this, we will utilize the *yfinance* library to fetch stock information and leverage visualization tools such as Seaborn and Matplotlib to illustrate various facets of the data. Specifically, we will explore methods to analyze stock risk based on historical performance, and implement predictive modeling using **GRU/ LSTM** models.

Throughout this tutorial, we aim to address the following key questions:

1. How has the stock price evolved over time?
2. What is the average **daily return** of the stock?
3. How does the **moving average** of the stocks vary?
4. What is the **correlation** between different stocks?
5. How can we forecast future stock behavior, exemplified by predicting the closing price of Apple Inc. using LSTM or GRU?"

***

## Getting Data
The initial step involves **acquiring and loading** the data into memory. Our source of stock data is the **Yahoo Finance** website, renowned for its wealth of financial market data and investment tools. To access this data, we'll employ the **yfinance** library, known for its efficient and Pythonic approach to downloading market data from Yahoo. For further insights into yfinance, refer to the article titled [Reliably download historical market data from with Python](https://aroussi.com/post/python-yahoo-finance).

### Install Dependencies
```bash
pip install -qU yfinance seaborn
```
### Configuration Code
```python
import pandas as pd
import numpy as np

import matplotlib.pyplot as plt
import seaborn as sns
sns.set_style('whitegrid')
plt.style.use("fivethirtyeight")
%matplotlib inline #comment if you are not using a jupyter notebook

# For reading stock data from yahoo
from pandas_datareader.data import DataReader
import yfinance as yf
from pandas_datareader import data as pdr

yf.pdr_override()

# For time stamps
from datetime import datetime

# Get Microsoft data
data = yf.download("MSFT", start, end)
```

## Statistical Analysis on the price
### Summary
```python
# Summary Stats
data.describe()
```

### Closing Price
The closing price is the last price at which the stock is traded during the regular trading day. A stock’s closing price is the standard benchmark used by investors to track its performance over time.

```python
plt.figure(figsize=(14, 5))
plt.plot(data['Adj Close'], label='Close Price')
plt.xlabel('Date')
plt.ylabel('Close Price [$]')
plt.title('Stock Price History')
plt.legend()
plt.show()
```
### Volume of Sales
Volume is the amount of an asset or security that _changes hands over some period of time_, often over the course of a day. For instance, the stock trading volume would refer to the number of shares of security traded between its daily open and close. Trading volume, and changes to volume over the course of time, are important inputs for technical traders.
```python
plt.figure(figsize=(14, 5))
plt.plot(data['Volume'], label='Volume')
plt.xlabel('Date')
plt.ylabel('Volume')
plt.title('Stock Price History')
plt.show()
```

### Moving Average
The moving average (MA) is a simple **technical analysis** tool that smooths out price data by creating a constantly updated average price. The average is taken over a specific period of time, like 10 days, 20 minutes, 30 weeks, or any time period the trader chooses.


```python
ma_day = [10, 20, 50]

# compute moving average (can be also done in a vectorized way)
for ma in ma_day:
column_name = f"{ma} days MA"
data[column_name] = data['Adj Close'].rolling(ma).mean()

plt.figure(figsize=(14, 5))
data[['Adj Close', '10 days MA', '20 days MA', '50 days MA']].plot()
plt.xlabel('Date')
plt.ylabel('Volume')
plt.title('Stock Price History')
plt.show()
```

## Statistical Analysis on the returns
Now that we've done some baseline analysis, let's go ahead and dive a little deeper. We're now going to analyze the risk of the stock. In order to do so we'll need to take a closer look at the daily changes of the stock, and not just its absolute value. Let's go ahead and use pandas to retrieve teh daily returns for the **Microsoft** stock.
```python
# Compute daily return in percentage
data['Daily Return'] = data['Adj Close'].pct_change()

# simple plot
plt.figure(figsize=(14, 5))
data['Daily Return'].hist(bins=50)
plt.title('MSFT Daily Return Distribution')
plt.xlabel('Daily Return')
plt.show()

## GRU Model
# histogram
plt.figure(figsize=(8, 5))
data['Daily Return'].plot()
plt.title('MSFT Daily Return')
plt.show()

### Init
```
## Data Preparation
```python
# Create a new dataframe with only the 'Close column
X = data.filter(['Adj Close'])
# Convert the dataframe to a numpy array
X = X.values
# Get the number of rows to train the model on
training_data_len = int(np.ceil(len(X)*.95))

### Training
# Scale data
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler(feature_range=(0,1))
scaled_data = scaler.fit_transform(X)

### Testing Metrics
* mean squared error
scaled_data
```
Split training data into small chunks to ingest into LSTM and GRU
```python
# Create the training data set
# Create the scaled training data set
train_data = scaled_data[0:int(training_data_len), :]
# Split the data into x_train and y_train data sets
x_train = []
y_train = []
seq_length = 60
for i in range(seq_length, len(train_data)):
x_train.append(train_data[i-60:i, 0])
y_train.append(train_data[i, 0])
if i<= seq_length+1:
print(x_train)
print(y_train, end="\n\n")

# Convert the x_train and y_train to numpy arrays
x_train, y_train = np.array(x_train), np.array(y_train)

# Reshape the data
x_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1], 1))
```

## GRU
Gated-Recurrent Unit (GRU) is adopted in this part
```python
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import GRU, Dense, Dropout

lstm_model = Sequential()
lstm_model.add(GRU(units=128, return_sequences=True, input_shape=(seq_length, 1)))
lstm_model.add(Dropout(0.2))
lstm_model.add(GRU(units=64, return_sequences=False))
lstm_model.add(Dropout(0.2))
lstm_model.add(Dense(units=1))

## LSTM Comparison
lstm_model.compile(optimizer='adam', loss='mean_squared_error')
lstm_model.fit(x_train, y_train, epochs=10, batch_size=4)
```

## LSTM
Long Short-Term Memory (LSTM) is adopted in this part
```python
from tensorflow.keras.layers import LSTM

lstm_model = Sequential()
lstm_model.add(LSTM(units=128, return_sequences=True, input_shape=(seq_length, 1)))
lstm_model.add(Dropout(0.2))
lstm_model.add(LSTM(units=64, return_sequences=False))
lstm_model.add(Dropout(0.2))
lstm_model.add(Dense(units=1))

lstm_model.compile(optimizer='adam', loss='mean_squared_error')
lstm_model.fit(x_train, y_train, epochs=10, batch_size=4)
```


## Testing Metrics
* mean squared error

### Test Plot

## Possible trading performance
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions public/404.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<link rel="icon" type="image/png" href="/images/site/favicon_hub02d7508a1c89b2b7812eab204efeb9a_4223_42x0_resize_box_3.png" />

<meta property="og:url" content="https://localhost:1313/404.html">
<meta property="og:site_name" content="Stefano&#39;s Blog">
<meta property="og:site_name" content="Stefano Giannini">
<meta property="og:title" content="404 Page not found">
<meta property="og:locale" content="en">
<meta property="og:type" content="website">
Expand Down Expand Up @@ -125,7 +125,7 @@
<a class="navbar-brand" href="/">

<img src="/images/site/main-logo_hu9ad2f25a877e6fef77c7a3dbef5094ad_6881_42x0_resize_box_3.png" id="logo" alt="Logo">
Stefano&#39;s Blog</a>
Stefano Giannini</a>
<button
class="navbar-toggler navbar-light"
id="navbar-toggler"
Expand Down Expand Up @@ -479,7 +479,7 @@ <h5>Contact me:</h5>
Toha
</a>
</div>
<div class="col-md-4 text-center">© 2020 Copyright.</div>
<div class="col-md-4 text-center">© 2024 Copyright.</div>
<div class="col-md-4 text-end">
<a id="hugo" href="https://gohugo.io/" target="_blank" rel="noopener">Powered by
<img
Expand Down
11 changes: 8 additions & 3 deletions public/categories/basic/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<link rel="icon" type="image/png" href="/images/site/favicon_hub02d7508a1c89b2b7812eab204efeb9a_4223_42x0_resize_box_3.png" />

<meta property="og:url" content="https://localhost:1313/categories/basic/">
<meta property="og:site_name" content="Stefano&#39;s Blog">
<meta property="og:site_name" content="Stefano Giannini">
<meta property="og:title" content="Basic">
<meta property="og:locale" content="en">
<meta property="og:type" content="website">
Expand Down Expand Up @@ -125,7 +125,7 @@
<a class="navbar-brand" href="/">

<img src="/images/site/main-logo_hu9ad2f25a877e6fef77c7a3dbef5094ad_6881_42x0_resize_box_3.png" id="logo" alt="Logo">
Stefano&#39;s Blog</a>
Stefano Giannini</a>
<button
class="navbar-toggler navbar-light"
id="navbar-toggler"
Expand Down Expand Up @@ -259,6 +259,11 @@



<li><a class="taxonomy-term " href="https://localhost:1313/categories/finance/" data-taxonomy-term="finance"><span class="taxonomy-label">Finance</span></a></li>




<li><a class="taxonomy-term " href="https://localhost:1313/categories/physics/" data-taxonomy-term="physics"><span class="taxonomy-label">Physics</span></a></li>


Expand Down Expand Up @@ -531,7 +536,7 @@ <h5>Contact me:</h5>
Toha
</a>
</div>
<div class="col-md-4 text-center">© 2020 Copyright.</div>
<div class="col-md-4 text-center">© 2024 Copyright.</div>
<div class="col-md-4 text-end">
<a id="hugo" href="https://gohugo.io/" target="_blank" rel="noopener">Powered by
<img
Expand Down
4 changes: 2 additions & 2 deletions public/categories/basic/index.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="https://www.w3.org/2005/Atom">
<channel>
<title>Basic on Stefano&#39;s Blog</title>
<title>Basic on Stefano Giannini</title>
<link>https://localhost:1313/categories/basic/</link>
<description>Recent content in Basic on Stefano&#39;s Blog</description>
<description>Recent content in Basic on Stefano Giannini</description>
<generator>Hugo -- gohugo.io</generator>
<language>en</language>
<lastBuildDate>Sat, 08 Jun 2024 08:06:25 +0600</lastBuildDate><atom:link href="https://localhost:1313/categories/basic/index.xml" rel="self" type="application/rss+xml" /><item>
Expand Down
6 changes: 3 additions & 3 deletions public/categories/finance/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<link rel="icon" type="image/png" href="/images/site/favicon_hub02d7508a1c89b2b7812eab204efeb9a_4223_42x0_resize_box_3.png" />

<meta property="og:url" content="https://localhost:1313/categories/finance/">
<meta property="og:site_name" content="Stefano&#39;s Blog">
<meta property="og:site_name" content="Stefano Giannini">
<meta property="og:title" content="Finance">
<meta property="og:locale" content="en">
<meta property="og:type" content="website">
Expand Down Expand Up @@ -125,7 +125,7 @@
<a class="navbar-brand" href="/">

<img src="/images/site/main-logo_hu9ad2f25a877e6fef77c7a3dbef5094ad_6881_42x0_resize_box_3.png" id="logo" alt="Logo">
Stefano&#39;s Blog</a>
Stefano Giannini</a>
<button
class="navbar-toggler navbar-light"
id="navbar-toggler"
Expand Down Expand Up @@ -537,7 +537,7 @@ <h5>Contact me:</h5>
Toha
</a>
</div>
<div class="col-md-4 text-center">© 2020 Copyright.</div>
<div class="col-md-4 text-center">© 2024 Copyright.</div>
<div class="col-md-4 text-end">
<a id="hugo" href="https://gohugo.io/" target="_blank" rel="noopener">Powered by
<img
Expand Down
4 changes: 2 additions & 2 deletions public/categories/finance/index.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="https://www.w3.org/2005/Atom">
<channel>
<title>Finance on Stefano&#39;s Blog</title>
<title>Finance on Stefano Giannini</title>
<link>https://localhost:1313/categories/finance/</link>
<description>Recent content in Finance on Stefano&#39;s Blog</description>
<description>Recent content in Finance on Stefano Giannini</description>
<generator>Hugo -- gohugo.io</generator>
<language>en</language>
<lastBuildDate>Sun, 16 Jun 2024 00:00:00 +0100</lastBuildDate><atom:link href="https://localhost:1313/categories/finance/index.xml" rel="self" type="application/rss+xml" /><item>
Expand Down
11 changes: 8 additions & 3 deletions public/categories/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<link rel="icon" type="image/png" href="/images/site/favicon_hub02d7508a1c89b2b7812eab204efeb9a_4223_42x0_resize_box_3.png" />

<meta property="og:url" content="https://localhost:1313/categories/">
<meta property="og:site_name" content="Stefano&#39;s Blog">
<meta property="og:site_name" content="Stefano Giannini">
<meta property="og:title" content="Categories">
<meta property="og:locale" content="en">
<meta property="og:type" content="website">
Expand Down Expand Up @@ -125,7 +125,7 @@
<a class="navbar-brand" href="/">

<img src="/images/site/main-logo_hu9ad2f25a877e6fef77c7a3dbef5094ad_6881_42x0_resize_box_3.png" id="logo" alt="Logo">
Stefano&#39;s Blog</a>
Stefano Giannini</a>
<button
class="navbar-toggler navbar-light"
id="navbar-toggler"
Expand Down Expand Up @@ -293,6 +293,11 @@



<li><a class="taxonomy-term " href="https://localhost:1313/categories/finance/" data-taxonomy-term="finance"><span class="taxonomy-label">Finance</span></a></li>




<li><a class="taxonomy-term " href="https://localhost:1313/categories/physics/" data-taxonomy-term="physics"><span class="taxonomy-label">Physics</span></a></li>


Expand Down Expand Up @@ -521,7 +526,7 @@ <h5>Contact me:</h5>
Toha
</a>
</div>
<div class="col-md-4 text-center">© 2020 Copyright.</div>
<div class="col-md-4 text-center">© 2024 Copyright.</div>
<div class="col-md-4 text-end">
<a id="hugo" href="https://gohugo.io/" target="_blank" rel="noopener">Powered by
<img
Expand Down
6 changes: 3 additions & 3 deletions public/categories/index.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="https://www.w3.org/2005/Atom">
<channel>
<title>Categories on Stefano&#39;s Blog</title>
<title>Categories on Stefano Giannini</title>
<link>https://localhost:1313/categories/</link>
<description>Recent content in Categories on Stefano&#39;s Blog</description>
<description>Recent content in Categories on Stefano Giannini</description>
<generator>Hugo -- gohugo.io</generator>
<language>en</language>
<lastBuildDate>Wed, 12 Jun 2024 08:06:25 +0600</lastBuildDate><atom:link href="https://localhost:1313/categories/index.xml" rel="self" type="application/rss+xml" />
<lastBuildDate>Sun, 16 Jun 2024 00:00:00 +0100</lastBuildDate><atom:link href="https://localhost:1313/categories/index.xml" rel="self" type="application/rss+xml" />
</channel>
</rss>
Loading

0 comments on commit 481d17b

Please sign in to comment.