#!/usr/bin/env python # coding: utf-8 # # Correlation Matrix in Python Using Downloaded Stock Data # #### Assumes you have python and a number of supporting libraries installed. The easiest way to get most everything you need is to install the anaconda scientific platform by by visiting http://anaconda.com # #### You will also need to install fix_yahoo_finance separately in order to download data from Yahoo Finance. From a command prompt enter pip install fix_yahoo_finance # # # ### 1. Set up environment # In[24]: import numpy as np import pandas as pd import fix_yahoo_finance as fyf # #### Note fix_yahoo_finance is billed a a temporary solution to recent API changes at Yahoo Finance. Pandas does have a complimentary library, pandas_datareader that allows that downloading of data from a number of sources. See http://pandas-datareader.readthedocs.io/en/latest/ for more information. # ### 2. Download data for FANG stocks # In[25]: stocks = 'FB AMZN NFLX GOOG GLD' stocks = stocks.split() data = fyf.download(stocks, '2017-06-01')['Close'] data.head() # In[26]: import matplotlib.pyplot as plt get_ipython().run_line_magic('matplotlib', 'inline') plt.plot(data) plt.legend(['AMZN', 'FB', 'GLD', 'GOOG', 'NFLX'], loc=2) # ### 3. Transform closing price into instantaneous rates of return # In[28]: returns = pd.DataFrame() for stock in data: if stock not in returns: returns[stock] = np.log(data[stock]).diff() returns = returns[1:] returns.head() # ### 4. Generate descriptives with pandas method [describe] # In[29]: returns.describe() # ### 5. Create correlation matrix # In[30]: returns.corr() # In[32]: returns.corr()['AMZN'].sort_values(ascending=False) # ### 6. Graphically display correlations # In[33]: from pandas.plotting import scatter_matrix scatter_matrix(returns, figsize=(16,12), alpha=0.3) # In[ ]: