In [1]:
import numpy as np, pandas as pd, pandas_datareader.data as data
import matplotlib
from datetime import datetime as dt
%matplotlib inline
In [2]:
start = dt(2016,5,31)
start
Out[2]:
datetime.datetime(2016, 5, 31, 0, 0)
In [3]:
stocks = ['FB', 'AMZN', 'GOOG', 'NFLX', 'GLD']
In [4]:
stocks_data = data.DataReader(stocks, 'google', start)['Close']
stocks_data.describe()
Out[4]:
AMZN FB GLD GOOG NFLX
count 253.000000 253.000000 253.000000 253.000000 253.000000
mean 809.661383 129.027787 120.054545 796.559605 121.344783
std 69.926299 10.679318 5.647900 60.250534 22.788181
min 691.360000 108.970000 107.340000 668.260000 85.330000
25% 760.120000 120.310000 116.130000 768.270000 97.380000
50% 789.820000 127.880000 119.670000 786.140000 123.440000
75% 846.820000 136.760000 125.320000 824.730000 142.420000
max 996.700000 152.780000 130.520000 975.880000 163.220000
In [6]:
stocks_data.head()
Out[6]:
AMZN FB GLD GOOG NFLX
Date
2016-05-31 722.79 118.81 116.06 735.72 102.57
2016-06-01 719.44 118.78 115.94 734.15 101.51
2016-06-02 728.24 118.93 115.67 730.40 101.25
2016-06-03 725.54 118.47 118.88 722.34 99.59
2016-06-06 726.73 118.79 118.92 716.55 100.74
In [7]:
stocks_ln =pd.DataFrame()
for col in stocks_data:
    if col not in stocks_ln:
        stocks_ln[col] = np.log(stocks_data[col]).diff()
stocks_ln.head()        
Out[7]:
AMZN FB GLD GOOG NFLX
Date
2016-05-31 NaN NaN NaN NaN NaN
2016-06-01 -0.004646 -0.000253 -0.001034 -0.002136 -0.010388
2016-06-02 0.012158 0.001262 -0.002332 -0.005121 -0.002565
2016-06-03 -0.003714 -0.003875 0.027373 -0.011096 -0.016531
2016-06-06 0.001639 0.002697 0.000336 -0.008048 0.011481
In [8]:
corr_stocks = stocks_ln.corr()
In [9]:
corr_stocks
Out[9]:
AMZN FB GLD GOOG NFLX
AMZN 1.000000 0.584890 -0.079753 0.617099 0.294475
FB 0.584890 1.000000 -0.087232 0.644291 0.290458
GLD -0.079753 -0.087232 1.000000 -0.126469 -0.059774
GOOG 0.617099 0.644291 -0.126469 1.000000 0.318791
NFLX 0.294475 0.290458 -0.059774 0.318791 1.000000
In [ ]:
 
In [11]:
corr_stocks['AMZN'].sort_values(ascending=False)
Out[11]:
AMZN    1.000000
GOOG    0.617099
FB      0.584890
NFLX    0.294475
GLD    -0.079753
Name: AMZN, dtype: float64
In [12]:
from pandas.tools.plotting import scatter_matrix
scatter_matrix(stocks_ln, figsize=(16,12), alpha=0.3)
Out[12]:
array([[<matplotlib.axes._subplots.AxesSubplot object at 0x111a41550>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x114079e80>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x1140df4e0>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x1141449e8>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x11419f320>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x11419f358>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x11425f160>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x1142c5748>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x1142cee80>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x114387588>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x1143eea90>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x114448208>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x1144ae630>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x1144fef28>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x11456f0b8>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x1145c3d68>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x114629f98>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x114683ba8>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x1146f00f0>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x11469e1d0>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x1147b0208>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x11481a438>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x114873048>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x1148d9278>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x1149179b0>]], dtype=object)
In [ ]: