{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "recurrent-neural-networks.ipynb",
"provenance": [],
"collapsed_sections": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "oA6GZzYgRBsu"
},
"source": [
"
\n",
"\n",
"------"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "6QBqbb8BRFzu"
},
"source": [
"# Recurrent Neural Networks\n",
"\n",
"A case study of univariate time series analysis.\n",
"\n",
"### Practical Session\n",
"\n",
"
Prof. Dr. Georgios K. Ouzounis\n",
"
email: georgios.ouzounis@go.kauko.lt\n",
"
last update: June 25, 2021\n",
"\n",
"\n",
"--------"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "TdhSL4xTROUn"
},
"source": [
"## Contents\n",
"\n",
"1. [Challenge](#challenge)\n",
"2. [Download the data](#download-the-data)\n",
"3. [Visualize the stock price history](#visualize-the-stock-price-history)\n",
"4. [Data transforms and preprocessing](#data-transforms-and-preprocessing)\n",
"5. [Build the RNN](#build-the-rnn)\n",
"6. [Train the RNN](#train-the-rnn)\n",
"7. [Improve the RNN](#improve-the-rnn)\n",
"8. [Fine tune the RNN](#fine-tune-the-rnn)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "CU-lVwMmRTNK"
},
"source": [
"## Challenge \n",
"\n",
"
\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "WehN_1mKRhu4"
},
"source": [
"Given a 5-year history of any stock traded in NASDAQ predict the stock prices for the period of the recent-most month that are not included in the historical data.\n",
"\n",
"To address this challenge we will employ [**univariate time series analysis**](www.homepages.ucl.ac.uk/~uctpsc0/Teaching/GR03/TS1.pdf) with [**recurrent neural networks**](https://stanford.edu/~shervine/teaching/cs-230/cheatsheet-recurrent-neural-networks)."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "mReQATWhIv6d"
},
"source": [
"## Download the data "
]
},
{
"cell_type": "code",
"metadata": {
"id": "TGl4xmaGRYCc"
},
"source": [
"# importing the libraries\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"import pandas as pd"
],
"execution_count": 1,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "hvLG67JMRlJf",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "3d4af5bf-8824-49d3-d6ed-921fb271dc54"
},
"source": [
"# install yahoo-finance\n",
"!pip install yfinance"
],
"execution_count": 2,
"outputs": [
{
"output_type": "stream",
"text": [
"Collecting yfinance\n",
" Downloading https://files.pythonhosted.org/packages/5e/4e/88d31f5509edcbc51bcbb7eeae72516b17ada1bc2ad5b496e2d05d62c696/yfinance-0.1.60.tar.gz\n",
"Requirement already satisfied: pandas>=0.24 in /usr/local/lib/python3.7/dist-packages (from yfinance) (1.1.5)\n",
"Requirement already satisfied: numpy>=1.15 in /usr/local/lib/python3.7/dist-packages (from yfinance) (1.19.5)\n",
"Requirement already satisfied: requests>=2.20 in /usr/local/lib/python3.7/dist-packages (from yfinance) (2.23.0)\n",
"Requirement already satisfied: multitasking>=0.0.7 in /usr/local/lib/python3.7/dist-packages (from yfinance) (0.0.9)\n",
"Collecting lxml>=4.5.1\n",
"\u001b[?25l Downloading https://files.pythonhosted.org/packages/30/c0/d0526314971fc661b083ab135747dc68446a3022686da8c16d25fcf6ef07/lxml-4.6.3-cp37-cp37m-manylinux2014_x86_64.whl (6.3MB)\n",
"\u001b[K |████████████████████████████████| 6.3MB 27.5MB/s \n",
"\u001b[?25hRequirement already satisfied: pytz>=2017.2 in /usr/local/lib/python3.7/dist-packages (from pandas>=0.24->yfinance) (2018.9)\n",
"Requirement already satisfied: python-dateutil>=2.7.3 in /usr/local/lib/python3.7/dist-packages (from pandas>=0.24->yfinance) (2.8.1)\n",
"Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.7/dist-packages (from requests>=2.20->yfinance) (1.24.3)\n",
"Requirement already satisfied: chardet<4,>=3.0.2 in /usr/local/lib/python3.7/dist-packages (from requests>=2.20->yfinance) (3.0.4)\n",
"Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.7/dist-packages (from requests>=2.20->yfinance) (2021.5.30)\n",
"Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.7/dist-packages (from requests>=2.20->yfinance) (2.10)\n",
"Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.7/dist-packages (from python-dateutil>=2.7.3->pandas>=0.24->yfinance) (1.15.0)\n",
"Building wheels for collected packages: yfinance\n",
" Building wheel for yfinance (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
" Created wheel for yfinance: filename=yfinance-0.1.60-py2.py3-none-any.whl size=23819 sha256=de567c9e1d56b3f51d8234a5b5b230d25a14fd924b18fc926824a0c6d6bea0c6\n",
" Stored in directory: /root/.cache/pip/wheels/f0/be/a4/846f02c5985562250917b0ab7b33fff737c8e6e8cd5209aa3b\n",
"Successfully built yfinance\n",
"Installing collected packages: lxml, yfinance\n",
" Found existing installation: lxml 4.2.6\n",
" Uninstalling lxml-4.2.6:\n",
" Successfully uninstalled lxml-4.2.6\n",
"Successfully installed lxml-4.6.3 yfinance-0.1.60\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "gBW1a1O3RnKx",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "740f576b-9995-4335-e767-e20f47acc255"
},
"source": [
"# copy some custom code files \n",
"!wget https://raw.githubusercontent.com/georgiosouzounis/deep-learning-lectures/main/code/NASDAQ_io.py"
],
"execution_count": 3,
"outputs": [
{
"output_type": "stream",
"text": [
"--2021-07-04 05:02:15-- https://raw.githubusercontent.com/georgiosouzounis/deep-learning-lectures/main/code/NASDAQ_io.py\n",
"Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 185.199.110.133, 185.199.111.133, 185.199.108.133, ...\n",
"Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|185.199.110.133|:443... connected.\n",
"HTTP request sent, awaiting response... 200 OK\n",
"Length: 1716 (1.7K) [text/plain]\n",
"Saving to: ‘NASDAQ_io.py’\n",
"\n",
"\rNASDAQ_io.py 0%[ ] 0 --.-KB/s \rNASDAQ_io.py 100%[===================>] 1.68K --.-KB/s in 0s \n",
"\n",
"2021-07-04 05:02:15 (37.2 MB/s) - ‘NASDAQ_io.py’ saved [1716/1716]\n",
"\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "CB8DpuO6_sp1"
},
"source": [
"# import necessary functions\n",
"from NASDAQ_io import * #getStockTickerSymbols, searchBySymbol, getStockPriceHistory, getDateTime"
],
"execution_count": 4,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "jalyBe1iRs2G"
},
"source": [
"# get the companies listed in NASDAQ\n",
"companies = getStockTickerSymbols()"
],
"execution_count": 5,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "Nn2ULPRPRvGq",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 511
},
"outputId": "16d9f5c5-0608-47b7-bb3b-3be0ca56cd78"
},
"source": [
"# view the companies\n",
"companies"
],
"execution_count": 6,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/html": [
"
| \n", " | Symbol | \n", "Company Name | \n", "Security Name | \n", "Market Category | \n", "Test Issue | \n", "Financial Status | \n", "Round Lot Size | \n", "
|---|---|---|---|---|---|---|---|
| 0 | \n", "AAIT | \n", "iShares MSCI All Country Asia Information Tech... | \n", "iShares MSCI All Country Asia Information Tech... | \n", "G | \n", "N | \n", "N | \n", "100.0 | \n", "
| 1 | \n", "AAL | \n", "American Airlines Group, Inc. | \n", "American Airlines Group, Inc. - Common Stock | \n", "Q | \n", "N | \n", "N | \n", "100.0 | \n", "
| 2 | \n", "AAME | \n", "Atlantic American Corporation | \n", "Atlantic American Corporation - Common Stock | \n", "G | \n", "N | \n", "N | \n", "100.0 | \n", "
| 3 | \n", "AAOI | \n", "Applied Optoelectronics, Inc. | \n", "Applied Optoelectronics, Inc. - Common Stock | \n", "G | \n", "N | \n", "N | \n", "100.0 | \n", "
| 4 | \n", "AAON | \n", "AAON, Inc. | \n", "AAON, Inc. - Common Stock | \n", "Q | \n", "N | \n", "N | \n", "100.0 | \n", "
| ... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "
| 2962 | \n", "ZN | \n", "Zion Oil & Gas Inc | \n", "Zion Oil & Gas Inc - Common Stock | \n", "G | \n", "N | \n", "N | \n", "100.0 | \n", "
| 2963 | \n", "ZNGA | \n", "Zynga Inc. | \n", "Zynga Inc. - Class A Common Stock | \n", "Q | \n", "N | \n", "N | \n", "100.0 | \n", "
| 2964 | \n", "ZSPH | \n", "ZS Pharma, Inc. | \n", "ZS Pharma, Inc. - Common Stock | \n", "G | \n", "N | \n", "N | \n", "100.0 | \n", "
| 2965 | \n", "ZU | \n", "zulily, inc. | \n", "zulily, inc. - Class A Common Stock | \n", "Q | \n", "N | \n", "N | \n", "100.0 | \n", "
| 2966 | \n", "ZUMZ | \n", "Zumiez Inc. | \n", "Zumiez Inc. - Common Stock | \n", "Q | \n", "N | \n", "N | \n", "100.0 | \n", "
2967 rows × 7 columns
\n", "| \n", " | Open | \n", "High | \n", "Low | \n", "Close | \n", "Adj Close | \n", "Volume | \n", "Name | \n", "
|---|---|---|---|---|---|---|---|
| Date | \n", "\n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " |
| 2016-01-04 | \n", "46.144001 | \n", "46.276001 | \n", "43.799999 | \n", "44.681999 | \n", "44.681999 | \n", "34135500 | \n", "TSLA | \n", "
| 2016-01-05 | \n", "45.271999 | \n", "45.377998 | \n", "44.000000 | \n", "44.686001 | \n", "44.686001 | \n", "15934000 | \n", "TSLA | \n", "
| 2016-01-06 | \n", "44.000000 | \n", "44.009998 | \n", "43.195999 | \n", "43.807999 | \n", "43.807999 | \n", "18895500 | \n", "TSLA | \n", "
| 2016-01-07 | \n", "42.838001 | \n", "43.688000 | \n", "42.734001 | \n", "43.130001 | \n", "43.130001 | \n", "17771500 | \n", "TSLA | \n", "
| 2016-01-08 | \n", "43.571999 | \n", "44.088001 | \n", "42.153999 | \n", "42.200001 | \n", "42.200001 | \n", "18140500 | \n", "TSLA | \n", "
| ... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "
| 2021-05-24 | \n", "581.599976 | \n", "614.479980 | \n", "573.650024 | \n", "606.440002 | \n", "606.440002 | \n", "34558100 | \n", "TSLA | \n", "
| 2021-05-25 | \n", "607.309998 | \n", "613.989990 | \n", "595.710022 | \n", "604.690002 | \n", "604.690002 | \n", "28005900 | \n", "TSLA | \n", "
| 2021-05-26 | \n", "607.559998 | \n", "626.169983 | \n", "601.500000 | \n", "619.130005 | \n", "619.130005 | \n", "28639300 | \n", "TSLA | \n", "
| 2021-05-27 | \n", "620.239990 | \n", "631.130005 | \n", "616.210022 | \n", "630.849976 | \n", "630.849976 | \n", "26370600 | \n", "TSLA | \n", "
| 2021-05-28 | \n", "628.500000 | \n", "635.590027 | \n", "622.380005 | \n", "625.219971 | \n", "625.219971 | \n", "22737000 | \n", "TSLA | \n", "
1361 rows × 7 columns
\n", "| \n", " | Date | \n", "Open | \n", "High | \n", "Low | \n", "Close | \n", "Adj Close | \n", "Volume | \n", "Name | \n", "
|---|---|---|---|---|---|---|---|---|
| 0 | \n", "2016-01-04 | \n", "46.144001 | \n", "46.276001 | \n", "43.799999 | \n", "44.681999 | \n", "44.681999 | \n", "34135500 | \n", "TSLA | \n", "
| 1 | \n", "2016-01-05 | \n", "45.271999 | \n", "45.377998 | \n", "44.000000 | \n", "44.686001 | \n", "44.686001 | \n", "15934000 | \n", "TSLA | \n", "
| 2 | \n", "2016-01-06 | \n", "44.000000 | \n", "44.009998 | \n", "43.195999 | \n", "43.807999 | \n", "43.807999 | \n", "18895500 | \n", "TSLA | \n", "
| 3 | \n", "2016-01-07 | \n", "42.838001 | \n", "43.688000 | \n", "42.734001 | \n", "43.130001 | \n", "43.130001 | \n", "17771500 | \n", "TSLA | \n", "
| 4 | \n", "2016-01-08 | \n", "43.571999 | \n", "44.088001 | \n", "42.153999 | \n", "42.200001 | \n", "42.200001 | \n", "18140500 | \n", "TSLA | \n", "
| ... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "
| 1356 | \n", "2021-05-24 | \n", "581.599976 | \n", "614.479980 | \n", "573.650024 | \n", "606.440002 | \n", "606.440002 | \n", "34558100 | \n", "TSLA | \n", "
| 1357 | \n", "2021-05-25 | \n", "607.309998 | \n", "613.989990 | \n", "595.710022 | \n", "604.690002 | \n", "604.690002 | \n", "28005900 | \n", "TSLA | \n", "
| 1358 | \n", "2021-05-26 | \n", "607.559998 | \n", "626.169983 | \n", "601.500000 | \n", "619.130005 | \n", "619.130005 | \n", "28639300 | \n", "TSLA | \n", "
| 1359 | \n", "2021-05-27 | \n", "620.239990 | \n", "631.130005 | \n", "616.210022 | \n", "630.849976 | \n", "630.849976 | \n", "26370600 | \n", "TSLA | \n", "
| 1360 | \n", "2021-05-28 | \n", "628.500000 | \n", "635.590027 | \n", "622.380005 | \n", "625.219971 | \n", "625.219971 | \n", "22737000 | \n", "TSLA | \n", "
1361 rows × 8 columns
\n", "