Profile picture

Co-founder @ RMOTR

Sample Project Github

Last updated: December 24th, 20182018-12-24Project preview
In [1]:
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

from datetime import datetime

%matplotlib inline
In [2]:
df = pd.read_csv('data/stocks.csv', index_col=0, parse_dates=True)
In [3]:
df.head()
Out[3]:
AAPL AMZN FB GOOG MSFT
Date
2016-01-01 105.26 675.89 104.66 758.88 55.48
2016-01-04 105.35 636.99 102.22 741.84 54.80
2016-01-05 102.71 633.79 102.73 742.58 55.05
2016-01-06 100.70 632.65 102.97 743.62 54.05
2016-01-07 96.45 607.94 97.92 726.39 52.17

Google vs Amazon

In [4]:
df[['GOOG', 'AMZN']].plot(figsize=(14, 7))
Out[4]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f89684a7da0>

Apple vs Facebook

In [5]:
df[['AAPL', 'FB']].plot(figsize=(14, 7))
Out[5]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f89663698d0>

Correlation of stock prices

In [6]:
df.corr()
Out[6]:
AAPL AMZN FB GOOG MSFT
AAPL 1.000000 0.899789 0.958959 0.956381 0.953769
AMZN 0.899789 1.000000 0.926380 0.952595 0.958339
FB 0.958959 0.926380 1.000000 0.956119 0.951590
GOOG 0.956381 0.952595 0.956119 1.000000 0.982691
MSFT 0.953769 0.958339 0.951590 0.982691 1.000000
In [7]:
f, ax = plt.subplots(figsize=(10, 8))
sns.heatmap(df.corr(), square=True, ax=ax)
Out[7]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f896629dc88>

Correlation of returns

In [8]:
df.pct_change().dropna().corr()
Out[8]:
AAPL AMZN FB GOOG MSFT
AAPL 1.000000 0.408361 0.426262 0.517977 0.506307
AMZN 0.408361 1.000000 0.577092 0.602319 0.536374
FB 0.426262 0.577092 1.000000 0.660297 0.533240
GOOG 0.517977 0.602319 0.660297 1.000000 0.701324
MSFT 0.506307 0.536374 0.533240 0.701324 1.000000
In [9]:
f, ax = plt.subplots(figsize=(10, 8))
sns.heatmap(df.pct_change().dropna().corr(), square=True, ax=ax)
Out[9]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f89661d20f0>
In [ ]:
 
Notebooks AI
Notebooks AI Profile20060