In [1]:
%matplotlib inline
from pandas import Series, DataFrame
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

In [2]:
# Correlation and covariance
import pandas.io.data as web
import os.path
cachePath = 'stocks.pkl'

In [3]:
if os.path.isfile(cachePath):
    print 'Loading from cache'
    all_data = pd.read_pickle(cachePath)
else:
    print 'Retrieving from web'
    all_data = {}
    for ticker in ['AAPL', 'IBM', 'MSFT', 'GOOG']:
        all_data[ticker]=web.get_data_yahoo(ticker)
    pd.to_pickle(all_data,cachePath)


Retrieving from web

In [4]:
all_data


Out[4]:
{'AAPL':                   Open        High         Low       Close     Volume  \
 Date                                                                    
 2010-01-04  213.429998  214.500023  212.380001  214.010025  123432400   
 2010-01-05  214.599985  215.589994  213.250034  214.379967  150476200   
 2010-01-06  214.379967  215.229986  210.749977  210.969995  138040000   
 2010-01-07  211.750000  211.999966  209.050032  210.580027  119282800   
 2010-01-08  210.300020  211.999966  209.059965  211.980019  111902700   
 2010-01-11  212.799997  212.999989  208.449991  210.109976  115557400   
 2010-01-12  209.190035  209.769981  206.419985  207.720028  148614900   
 2010-01-13  207.869965  210.930021  204.099985  210.650015  151473000   
 2010-01-14  210.109976  210.459970  209.020004  209.429987  108223500   
 2010-01-15  210.930021  211.599997  205.869999  205.929987  148516900   
 2010-01-19  208.330015  215.190012  207.239964  215.039995  182501900   
 2010-01-20  214.910006  215.550020  209.499989  211.729986  153038200   
 2010-01-21  212.079981  213.310022  207.210016  208.070036  152038600   
 2010-01-22  206.780006  207.500023  197.159973  197.750000  220441900   
 2010-01-25  202.510002  204.700026  200.189989  203.070002  266424900   
 2010-01-26  205.950014  213.710005  202.580004  205.940001  466777500   
 2010-01-27  206.849995  210.580027  199.530027  207.879978  430642100   
 2010-01-28  204.929964  205.499977  198.699968  199.289995  293375600   
 2010-01-29  201.079969  202.199968  190.249989  192.059977  311488100   
 2010-02-01  192.370010  196.000000  191.299986  194.729984  187469100   
 2010-02-02  195.909985  196.319967  193.379967  195.859997  174585600   
 2010-02-03  195.170008  200.200003  194.420031  199.230007  153832000   
 2010-02-04  196.730030  198.369987  191.569979  192.049976  189413000   
 2010-02-05  192.629990  196.000000  190.850029  195.460014  212576700   
 2010-02-08  195.689966  197.879990  194.000034  194.120010  119567700   
 2010-02-09  196.419996  197.500034  194.750011  196.189978  158221700   
 2010-02-10  195.890024  196.599974  194.260014  195.120033   92590400   
 2010-02-11  194.880001  199.749966  194.060022  198.670008  137586400   
 2010-02-12  198.110008  201.639969  195.499989  200.379967  163867200   
 2010-02-16  201.939989  203.689989  201.519993  203.399982  135934400   
 ...                ...         ...         ...         ...        ...   
 2015-05-13  126.150002  127.190002  125.870003  126.010002   34694200   
 2015-05-14  127.410004  128.949997  127.160004  128.949997   45203500   
 2015-05-15  129.070007  129.490005  128.210007  128.770004   38208000   
 2015-05-18  128.380005  130.720001  128.360001  130.190002   50882900   
 2015-05-19  130.690002  130.880005  129.639999  130.070007   44633200   
 2015-05-20  130.000000  130.979996  129.339996  130.059998   36454900   
 2015-05-21  130.070007  131.630005  129.830002  131.389999   39730400   
 2015-05-22  131.600006  132.970001  131.399994  132.539993   45123800   
 2015-05-26  132.600006  132.910004  129.119995  129.619995   70193700   
 2015-05-27  130.339996  132.259995  130.050003  132.039993   45662800   
 2015-05-28  131.860001  131.949997  131.100006  131.779999   30647300   
 2015-05-29  131.229996  131.449997  129.899994  130.279999   50338200   
 2015-06-01  130.279999  131.389999  130.050003  130.539993   31956000   
 2015-06-02  129.860001  130.660004  129.320007  129.960007   33556900   
 2015-06-03  130.660004  130.940002  129.899994  130.119995   30889400   
 2015-06-04  129.580002  130.580002  128.910004  129.360001   38229300   
 2015-06-05  129.500000  129.690002  128.360001  128.649994   35314200   
 2015-06-08  128.899994  129.210007  126.830002  127.800003   52538200   
 2015-06-09  126.699997  128.080002  125.620003  127.419998   55771500   
 2015-06-10  127.919998  129.339996  127.849998  128.880005   38915900   
 2015-06-11  129.179993  130.179993  128.479996  128.589996   35260400   
 2015-06-12  128.190002  128.330002  127.110001  127.169998   36754200   
 2015-06-15  126.099998  127.239998  125.709999  126.919998   39842600   
 2015-06-16  127.029999  127.849998  126.370003  127.599998   31404000   
 2015-06-17  127.720001  127.879997  126.739998  127.300003   32768500   
 2015-06-18  127.230003  128.309998  127.220001  127.879997   35241100   
 2015-06-19  127.709999  127.820000  126.400002  126.599998   54181300   
 2015-06-22  127.489998  128.059998  127.080002  127.610001   33833500   
 2015-06-23  127.480003  127.610001  126.879997  127.029999   30137100   
 2015-06-24  127.209999  129.800003  127.120003  128.110001   54964900   
 
              Adj Close  
 Date                    
 2010-01-04   28.718094  
 2010-01-05   28.767737  
 2010-01-06   28.310151  
 2010-01-07   28.257821  
 2010-01-08   28.445687  
 2010-01-11   28.194745  
 2010-01-12   27.874037  
 2010-01-13   28.267213  
 2010-01-14   28.103497  
 2010-01-15   27.633830  
 2010-01-19   28.856306  
 2010-01-20   28.412135  
 2010-01-21   27.921004  
 2010-01-22   26.536154  
 2010-01-25   27.250048  
 2010-01-26   27.635174  
 2010-01-27   27.895500  
 2010-01-28   26.742807  
 2010-01-29   25.772608  
 2010-02-01   26.130897  
 2010-02-02   26.282534  
 2010-02-03   26.734757  
 2010-02-04   25.771266  
 2010-02-05   26.228860  
 2010-02-08   26.049045  
 2010-02-09   26.326814  
 2010-02-10   26.183238  
 2010-02-11   26.659611  
 2010-02-12   26.889071  
 2010-02-16   27.294328  
 ...                ...  
 2015-05-13  126.010002  
 2015-05-14  128.949997  
 2015-05-15  128.770004  
 2015-05-18  130.190002  
 2015-05-19  130.070007  
 2015-05-20  130.059998  
 2015-05-21  131.389999  
 2015-05-22  132.539993  
 2015-05-26  129.619995  
 2015-05-27  132.039993  
 2015-05-28  131.779999  
 2015-05-29  130.279999  
 2015-06-01  130.539993  
 2015-06-02  129.960007  
 2015-06-03  130.119995  
 2015-06-04  129.360001  
 2015-06-05  128.649994  
 2015-06-08  127.800003  
 2015-06-09  127.419998  
 2015-06-10  128.880005  
 2015-06-11  128.589996  
 2015-06-12  127.169998  
 2015-06-15  126.919998  
 2015-06-16  127.599998  
 2015-06-17  127.300003  
 2015-06-18  127.879997  
 2015-06-19  126.599998  
 2015-06-22  127.610001  
 2015-06-23  127.029999  
 2015-06-24  128.110001  
 
 [1378 rows x 6 columns],
 'GOOG':                   Open        High         Low       Close   Volume  \
 Date                                                                  
 2014-03-27  568.002570  568.002570  552.922516  558.462551    13100   
 2014-03-28  561.202549  566.432590  558.672477  559.992504    41200   
 2014-03-31  566.892592  567.002574  556.932537  556.972503    10800   
 2014-04-01  558.712565  568.452595  558.712565  567.162558     7900   
 2014-04-02  599.992707  604.832763  562.192568  567.002574   147100   
 2014-04-03  569.852553  587.282679  564.132581  569.742571  5099200   
 2014-04-04  574.652643  577.772650  543.002488  543.142460  6369300   
 2014-04-07  540.742445  548.482483  527.152440  538.152456  4401700   
 2014-04-08  542.602466  555.002500  541.612446  554.902556  3151200   
 2014-04-09  559.622532  565.372554  552.952506  564.142557  3330800   
 2014-04-10  565.002582  565.002582  539.902495  540.952433  4036900   
 2014-04-11  532.552381  540.002440  526.532392  530.602392  3924800   
 2014-04-14  538.252462  544.102429  529.562370  532.522453  2575100   
 2014-04-15  536.822454  538.452473  518.462348  536.442444  3855100   
 2014-04-16  543.002488  557.002492  540.002440  556.542490  4893300   
 2014-04-17  548.812490  549.502492  531.152424  536.102400  6809500   
 2014-04-21  536.102400  536.702435  525.602352  528.622414  2566700   
 2014-04-22  528.642427  537.232391  527.512375  534.812425  2365400   
 2014-04-23  533.792415  533.872408  526.252389  526.942391  2052300   
 2014-04-24  530.072374  531.652452  522.122349  525.162363  1883200   
 2014-04-25  522.512395  524.702361  515.422333  516.182352  2100400   
 2014-04-28  517.182348  518.602319  502.802274  517.152359  3335500   
 2014-04-29  516.902344  529.462425  516.322324  527.702410  2699100   
 2014-04-30  527.602343  528.002366  522.522372  526.662388  1751200   
 2014-05-01  527.112352  532.932391  523.882364  531.352374  1905500   
 2014-05-02  533.762426  534.002403  525.612389  527.932411  1688500   
 2014-05-05  524.822381  528.902418  521.322364  527.812392  1024100   
 2014-05-06  525.232379  526.812396  515.062337  515.142330  1689000   
 2014-05-07  515.792305  516.682320  503.302272  509.962291  3224300   
 2014-05-08  508.462297  517.232290  506.452298  511.002313  2021300   
 ...                ...         ...         ...         ...      ...   
 2015-05-13  530.559998  534.322021  528.655029  529.619995  1252300   
 2015-05-14  533.770020  539.000000  532.409973  538.400024  1399100   
 2015-05-15  539.179993  539.273987  530.380005  533.849976  1962700   
 2015-05-18  532.010010  534.820007  528.849976  532.299988  1998600   
 2015-05-19  533.979980  540.659973  533.039978  537.359985  1963300   
 2015-05-20  538.489990  542.919983  532.971985  539.270020  1429100   
 2015-05-21  537.950012  543.840027  535.979980  542.510010  1461400   
 2015-05-22  540.150024  544.190002  539.510010  540.109985  1173300   
 2015-05-26  538.119995  539.000000  529.880005  532.320007  2403400   
 2015-05-27  532.799988  540.549988  531.710022  539.789978  1520400   
 2015-05-28  538.010010  540.609985  536.250000  539.780029  1027900   
 2015-05-29  537.369995  538.630005  531.450012  532.109985  2584900   
 2015-06-01  536.789978  536.789978  529.760010  533.989990  1899600   
 2015-06-02  532.929993  543.000000  531.330017  539.179993  1934700   
 2015-06-03  539.909973  543.500000  537.109985  540.309998  1714500   
 2015-06-04  537.760010  540.590027  534.320007  536.700012  1335600   
 2015-06-05  536.349976  537.200012  532.520020  533.330017  1375000   
 2015-06-08  533.309998  534.119995  526.239990  526.830017  1520600   
 2015-06-09  527.559998  529.200012  523.010010  526.690002  1441600   
 2015-06-10  529.359985  538.359985  529.349976  536.690002  1811400   
 2015-06-11  538.424988  538.979980  533.020020  534.609985  1205000   
 2015-06-12  531.599976  533.119995  530.159973  532.330017   952400   
 2015-06-15  528.000000  528.299988  524.000000  527.200012  1630700   
 2015-06-16  528.400024  529.640015  525.559998  528.150024  1069300   
 2015-06-17  529.369995  530.979980  525.099976  529.260010  1268600   
 2015-06-18  531.000000  538.150024  530.789978  536.729980  1828100   
 2015-06-19  537.210022  538.250000  533.010010  536.690002  1885700   
 2015-06-22  539.590027  543.739990  537.530029  538.190002  1242500   
 2015-06-23  539.640015  541.499023  535.250000  540.479980  1196000   
 2015-06-24  540.000000  540.000000  535.659973  537.840027  1283400   
 
              Adj Close  
 Date                    
 2014-03-27  558.462551  
 2014-03-28  559.992504  
 2014-03-31  556.972503  
 2014-04-01  567.162558  
 2014-04-02  567.002574  
 2014-04-03  569.742571  
 2014-04-04  543.142460  
 2014-04-07  538.152456  
 2014-04-08  554.902556  
 2014-04-09  564.142557  
 2014-04-10  540.952433  
 2014-04-11  530.602392  
 2014-04-14  532.522453  
 2014-04-15  536.442444  
 2014-04-16  556.542490  
 2014-04-17  536.102400  
 2014-04-21  528.622414  
 2014-04-22  534.812425  
 2014-04-23  526.942391  
 2014-04-24  525.162363  
 2014-04-25  516.182352  
 2014-04-28  517.152359  
 2014-04-29  527.702410  
 2014-04-30  526.662388  
 2014-05-01  531.352374  
 2014-05-02  527.932411  
 2014-05-05  527.812392  
 2014-05-06  515.142330  
 2014-05-07  509.962291  
 2014-05-08  511.002313  
 ...                ...  
 2015-05-13  529.619995  
 2015-05-14  538.400024  
 2015-05-15  533.849976  
 2015-05-18  532.299988  
 2015-05-19  537.359985  
 2015-05-20  539.270020  
 2015-05-21  542.510010  
 2015-05-22  540.109985  
 2015-05-26  532.320007  
 2015-05-27  539.789978  
 2015-05-28  539.780029  
 2015-05-29  532.109985  
 2015-06-01  533.989990  
 2015-06-02  539.179993  
 2015-06-03  540.309998  
 2015-06-04  536.700012  
 2015-06-05  533.330017  
 2015-06-08  526.830017  
 2015-06-09  526.690002  
 2015-06-10  536.690002  
 2015-06-11  534.609985  
 2015-06-12  532.330017  
 2015-06-15  527.200012  
 2015-06-16  528.150024  
 2015-06-17  529.260010  
 2015-06-18  536.729980  
 2015-06-19  536.690002  
 2015-06-22  538.190002  
 2015-06-23  540.479980  
 2015-06-24  537.840027  
 
 [314 rows x 6 columns],
 'IBM':                   Open        High         Low       Close    Volume  \
 Date                                                                   
 2010-01-04  131.179993  132.970001  130.850006  132.449997   6155300   
 2010-01-05  131.679993  131.850006  130.100006  130.850006   6841400   
 2010-01-06  130.679993  131.490005  129.809998  130.000000   5605300   
 2010-01-07  129.869995  130.250000  128.910004  129.550003   5840600   
 2010-01-08  129.070007  130.919998  129.050003  130.850006   4197200   
 2010-01-11  131.059998  131.059998  128.669998  129.479996   5730400   
 2010-01-12  129.029999  131.330002  129.000000  130.509995   8081500   
 2010-01-13  130.389999  131.119995  129.160004  130.229996   6455400   
 2010-01-14  130.550003  132.710007  129.910004  132.309998   7111800   
 2010-01-15  132.029999  132.889999  131.089996  131.779999   8494400   
 2010-01-19  131.630005  134.250000  131.559998  134.139999  13916200   
 2010-01-20  130.460007  131.149994  128.949997  130.250000  15197500   
 2010-01-21  130.470001  130.690002  128.059998  129.000000   9608600   
 2010-01-22  128.669998  128.889999  125.370003  125.500000  10088600   
 2010-01-25  126.330002  126.889999  125.709999  126.120003   5738500   
 2010-01-26  125.919998  127.750000  125.410004  125.750000   7135300   
 2010-01-27  125.820000  126.959999  125.040001  126.330002   8719200   
 2010-01-28  127.029999  127.040001  123.050003  123.750000   9622200   
 2010-01-29  124.320000  125.000000  121.900002  122.389999  11571200   
 2010-02-01  123.230003  124.949997  122.779999  124.669998   7242900   
 2010-02-02  124.790001  125.809998  123.949997  125.529999   5899900   
 2010-02-03  125.160004  126.070000  125.070000  125.660004   4177100   
 2010-02-04  125.190002  125.440002  122.900002  123.000000   9126900   
 2010-02-05  123.040001  123.720001  121.830002  123.519997   8617000   
 2010-02-08  123.150002  123.220001  121.739998  121.879997   5718500   
 2010-02-09  122.650002  124.199997  122.459999  123.209999   6044500   
 2010-02-10  122.940002  123.650002  122.209999  122.809998   5219100   
 2010-02-11  122.580002  124.199997  122.059998  123.730003   5089000   
 2010-02-12  123.010002  124.050003  121.610001  124.000000   8017700   
 2010-02-16  124.910004  125.230003  124.110001  125.230003   6777300   
 ...                ...         ...         ...         ...       ...   
 2015-05-13  171.240005  172.740005  170.750000  172.279999   2411500   
 2015-05-14  173.500000  174.399994  173.220001  174.050003   2435700   
 2015-05-15  173.910004  174.410004  172.600006  173.259995   2913700   
 2015-05-18  173.440002  173.490005  172.300003  173.059998   1923600   
 2015-05-19  172.970001  173.750000  171.929993  173.479996   2519600   
 2015-05-20  173.330002  174.440002  172.460007  173.759995   2300300   
 2015-05-21  173.320007  174.139999  173.039993  173.339996   2293600   
 2015-05-22  173.039993  173.389999  172.190002  172.220001   2704700   
 2015-05-26  172.110001  172.119995  169.130005  170.130005   3840000   
 2015-05-27  171.160004  172.479996  170.490005  172.000000   2749100   
 2015-05-28  171.449997  171.839996  170.660004  171.710007   1727000   
 2015-05-29  171.350006  171.350006  169.649994  169.649994   3996000   
 2015-06-01  170.210007  171.039993  169.029999  170.179993   2978500   
 2015-06-02  169.660004  170.449997  168.429993  169.649994   2571400   
 2015-06-03  170.500000  171.559998  169.630005  169.919998   2128200   
 2015-06-04  169.529999  170.600006  167.929993  168.380005   3053600   
 2015-06-05  168.250000  168.910004  167.199997  167.399994   3093300   
 2015-06-08  167.169998  167.279999  165.020004  165.339996   3757400   
 2015-06-09  165.339996  166.020004  163.369995  165.679993   3334800   
 2015-06-10  166.490005  169.389999  166.059998  168.919998   4674800   
 2015-06-11  169.259995  170.440002  168.539993  168.779999   3432500   
 2015-06-12  168.229996  168.300003  166.690002  166.990005   3053700   
 2015-06-15  166.990005  166.990005  164.250000  166.259995   4209500   
 2015-06-16  166.330002  167.399994  165.910004  166.839996   3246900   
 2015-06-17  167.000000  167.850006  166.100006  167.169998   2861100   
 2015-06-18  167.050003  168.720001  167.050003  168.250000   3329100   
 2015-06-19  167.619995  168.419998  166.770004  166.990005   6971200   
 2015-06-22  167.649994  168.339996  167.199997  167.729996   2210700   
 2015-06-23  168.380005  169.970001  167.570007  168.619995   3536800   
 2015-06-24  168.710007  168.839996  166.830002  166.970001   3526500   
 
              Adj Close  
 Date                    
 2010-01-04  118.634733  
 2010-01-05  117.201630  
 2010-01-06  116.440284  
 2010-01-07  116.037224  
 2010-01-08  117.201630  
 2010-01-11  115.974519  
 2010-01-12  116.897084  
 2010-01-13  116.646290  
 2010-01-14  118.509336  
 2010-01-15  118.034619  
 2010-01-19  120.148459  
 2010-01-20  116.664208  
 2010-01-21  115.544590  
 2010-01-22  112.409659  
 2010-01-25  112.964992  
 2010-01-26  112.633583  
 2010-01-27  113.153087  
 2010-01-28  110.842194  
 2010-01-29  109.624049  
 2010-02-01  111.666231  
 2010-02-02  112.436529  
 2010-02-03  112.552973  
 2010-02-04  110.170423  
 2010-02-05  110.636181  
 2010-02-08  109.655504  
 2010-02-09  110.852108  
 2010-02-10  110.492226  
 2010-02-11  111.319956  
 2010-02-12  111.562872  
 2010-02-16  112.669507  
 ...                ...  
 2015-05-13  172.279999  
 2015-05-14  174.050003  
 2015-05-15  173.259995  
 2015-05-18  173.059998  
 2015-05-19  173.479996  
 2015-05-20  173.759995  
 2015-05-21  173.339996  
 2015-05-22  172.220001  
 2015-05-26  170.130005  
 2015-05-27  172.000000  
 2015-05-28  171.710007  
 2015-05-29  169.649994  
 2015-06-01  170.179993  
 2015-06-02  169.649994  
 2015-06-03  169.919998  
 2015-06-04  168.380005  
 2015-06-05  167.399994  
 2015-06-08  165.339996  
 2015-06-09  165.679993  
 2015-06-10  168.919998  
 2015-06-11  168.779999  
 2015-06-12  166.990005  
 2015-06-15  166.259995  
 2015-06-16  166.839996  
 2015-06-17  167.169998  
 2015-06-18  168.250000  
 2015-06-19  166.990005  
 2015-06-22  167.729996  
 2015-06-23  168.619995  
 2015-06-24  166.970001  
 
 [1378 rows x 6 columns],
 'MSFT':                  Open       High        Low      Close     Volume  Adj Close
 Date                                                                        
 2010-01-04  30.620001  31.100000  30.590000  30.950001   38409100  26.769336
 2010-01-05  30.850000  31.100000  30.639999  30.959999   49749600  26.777984
 2010-01-06  30.879999  31.080000  30.520000  30.770000   58182400  26.613650
 2010-01-07  30.629999  30.700001  30.190001  30.450001   50559700  26.336875
 2010-01-08  30.280001  30.879999  30.240000  30.660000   51197400  26.518508
 2010-01-11  30.709999  30.760000  30.120001  30.270000   68754700  26.181189
 2010-01-12  30.150000  30.400000  29.910000  30.070000   65912100  26.008204
 2010-01-13  30.260000  30.520000  30.010000  30.350000   51863500  26.250383
 2010-01-14  30.309999  31.100000  30.260000  30.959999   63228100  26.777984
 2010-01-15  31.080000  31.240000  30.709999  30.860001   79913200  26.691493
 2010-01-19  30.750000  31.240000  30.680000  31.100000   46575700  26.899074
 2010-01-20  30.809999  30.940001  30.309999  30.590000   54849500  26.457964
 2010-01-21  30.610001  30.719999  30.000000  30.010000   73086700  25.956309
 2010-01-22  30.000000  30.200001  28.840000  28.959999  102004600  25.048140
 2010-01-25  29.240000  29.660000  29.100000  29.320000   63373000  25.359512
 2010-01-26  29.200001  29.850000  29.090000  29.500000   66639900  25.515199
 2010-01-27  29.350000  29.820000  29.020000  29.670000   63949500  25.662235
 2010-01-28  29.840000  29.870001  28.889999  29.160000  117513700  25.221125
 2010-01-29  29.900000  29.920000  27.660000  28.180000  193888500  24.373502
 2010-02-01  28.389999  28.480000  27.920000  28.410000   85931100  24.572434
 2010-02-02  28.370001  28.500000  28.139999  28.459999   54413700  24.615679
 2010-02-03  28.260000  28.790001  28.120001  28.629999   61397900  24.762716
 2010-02-04  28.379999  28.500000  27.809999  27.840000   77850000  24.079428
 2010-02-05  28.000000  28.280001  27.570000  28.020000   80960100  24.235114
 2010-02-08  28.010000  28.080000  27.570000  27.719999   52820600  23.975637
 2010-02-09  27.969999  28.340000  27.750000  28.010000   59195800  24.226465
 2010-02-10  28.030001  28.240000  27.840000  27.990000   48591300  24.209166
 2010-02-11  27.930000  28.400000  27.700001  28.120001   65993700  24.321607
 2010-02-12  27.809999  28.059999  27.580000  27.930000   81117200  24.157271
 2010-02-16  28.129999  28.370001  28.020000  28.350000   51935600  24.635203
 ...               ...        ...        ...        ...        ...        ...
 2015-05-13  48.189999  48.320000  47.570000  47.630001   34184600  47.322456
 2015-05-14  48.029999  48.820000  48.029999  48.720001   32980900  48.405418
 2015-05-15  48.869999  48.910000  48.049999  48.299999   28642700  47.988128
 2015-05-18  47.980000  48.220001  47.610001  48.009998   24136500  47.699999
 2015-05-19  47.560001  47.810001  47.180000  47.580002   28574800  47.580002
 2015-05-20  47.389999  47.930000  47.270000  47.580002   25047900  47.580002
 2015-05-21  47.279999  47.599998  47.009998  47.419998   22410700  47.419998
 2015-05-22  47.299999  47.349998  46.820000  46.900002   25720600  46.900002
 2015-05-26  46.830002  46.880001  46.189999  46.590000   29581900  46.590000
 2015-05-27  46.820000  47.770000  46.619999  47.610001   27335600  47.610001
 2015-05-28  47.500000  48.020000  47.389999  47.450001   19283700  47.450001
 2015-05-29  47.430000  47.570000  46.590000  46.860001   35428100  46.860001
 2015-06-01  47.060001  47.770000  46.619999  47.230000   28592900  47.230000
 2015-06-02  46.930000  47.349998  46.619999  46.919998   21283400  46.919998
 2015-06-03  47.369999  47.740002  46.820000  46.849998   27955200  46.849998
 2015-06-04  46.790001  47.160000  46.200001  46.360001   26868000  46.360001
 2015-06-05  46.310001  46.520000  45.840000  46.139999   25258900  46.139999
 2015-06-08  46.299999  46.430000  45.669998  45.730000   21822300  45.730000
 2015-06-09  45.759998  45.939999  45.459999  45.650002   24124700  45.650002
 2015-06-10  45.790001  46.830002  45.689999  46.610001   27952300  46.610001
 2015-06-11  46.660000  46.919998  46.130001  46.439999   27186700  46.439999
 2015-06-12  46.220001  46.470001  45.900002  45.970001   23416200  45.970001
 2015-06-15  45.450001  45.650002  45.020000  45.480000   30080100  45.480000
 2015-06-16  45.349998  46.240002  45.299999  45.830002   26822500  45.830002
 2015-06-17  45.730000  46.070000  45.360001  45.970001   28566300  45.970001
 2015-06-18  46.220001  46.799999  46.169998  46.720001   32184500  46.720001
 2015-06-19  46.790001  46.830002  45.990002  46.099998   57500600  46.099998
 2015-06-22  46.330002  46.720001  46.160000  46.230000   20278600  46.230000
 2015-06-23  46.130001  46.279999  45.619999  45.910000   25787000  45.910000
 2015-06-24  45.669998  46.250000  45.549999  45.639999   34784600  45.639999
 
 [1378 rows x 6 columns]}

In [5]:
price = DataFrame({tic: data['Adj Close'] for tic, data in all_data.iteritems()})

In [6]:
volume = DataFrame({tic: data['Volume'] for tic, data in all_data.iteritems()})

In [7]:
returns = price.pct_change()
returns.tail()


Out[7]:
AAPL GOOG IBM MSFT
Date
2015-06-18 0.004556 0.014114 0.006461 0.016315
2015-06-19 -0.010009 -0.000074 -0.007489 -0.013271
2015-06-22 0.007978 0.002795 0.004431 0.002820
2015-06-23 -0.004545 0.004255 0.005306 -0.006922
2015-06-24 0.008502 -0.004884 -0.009785 -0.005881

In [8]:
returns.MSFT.corr(returns.IBM)


Out[8]:
0.49876886430781581

In [9]:
returns.corr()


Out[9]:
AAPL GOOG IBM MSFT
AAPL 1.000000 0.311405 0.377656 0.347533
GOOG 0.311405 1.000000 0.355794 0.464120
IBM 0.377656 0.355794 1.000000 0.498769
MSFT 0.347533 0.464120 0.498769 1.000000

In [10]:
returns.corrwith(returns.IBM)


Out[10]:
AAPL    0.377656
GOOG    0.355794
IBM     1.000000
MSFT    0.498769
dtype: float64

In [11]:
help(DataFrame.corr)


Help on method corr in module pandas.core.frame:

corr(self, method='pearson', min_periods=1) unbound pandas.core.frame.DataFrame method
    Compute pairwise correlation of columns, excluding NA/null values
    
    Parameters
    ----------
    method : {'pearson', 'kendall', 'spearman'}
        * pearson : standard correlation coefficient
        * kendall : Kendall Tau correlation coefficient
        * spearman : Spearman rank correlation
    min_periods : int, optional
        Minimum number of observations required per pair of columns
        to have a valid result. Currently only available for pearson
        and spearman correlation
    
    Returns
    -------
    y : DataFrame


In [12]:
returns.corrwith(volume)


Out[12]:
AAPL   -0.094103
GOOG   -0.049483
IBM    -0.173894
MSFT   -0.096123
dtype: float64

In [13]:
# Panel data
pdata = pd.Panel(all_data)

In [14]:
pdata


Out[14]:
<class 'pandas.core.panel.Panel'>
Dimensions: 4 (items) x 1378 (major_axis) x 6 (minor_axis)
Items axis: AAPL to MSFT
Major_axis axis: 2010-01-04 00:00:00 to 2015-06-24 00:00:00
Minor_axis axis: Open to Adj Close

In [15]:
pdata = pdata.swapaxes('items','minor')

In [16]:
pdata['Adj Close'].tail()


Out[16]:
AAPL GOOG IBM MSFT
Date
2015-06-18 127.879997 536.729980 168.250000 46.720001
2015-06-19 126.599998 536.690002 166.990005 46.099998
2015-06-22 127.610001 538.190002 167.729996 46.230000
2015-06-23 127.029999 540.479980 168.619995 45.910000
2015-06-24 128.110001 537.840027 166.970001 45.639999

In [17]:
pdata.ix[:,'2015-06-22',:]


Out[17]:
Open High Low Close Volume Adj Close
AAPL 127.489998 128.059998 127.080002 127.610001 33833500 127.610001
GOOG 539.590027 543.739990 537.530029 538.190002 1242500 538.190002
IBM 167.649994 168.339996 167.199997 167.729996 2210700 167.729996
MSFT 46.330002 46.720001 46.160000 46.230000 20278600 46.230000

In [18]:
pdata.ix[:,'2015-06-22':,:].swapaxes('items','minor').to_frame()


Out[18]:
AAPL GOOG IBM MSFT
Date minor
2015-06-22 Open 127.489998 539.590027 167.649994 46.330002
High 128.059998 543.739990 168.339996 46.720001
Low 127.080002 537.530029 167.199997 46.160000
Close 127.610001 538.190002 167.729996 46.230000
Volume 33833500.000000 1242500.000000 2210700.000000 20278600.000000
Adj Close 127.610001 538.190002 167.729996 46.230000
2015-06-23 Open 127.480003 539.640015 168.380005 46.130001
High 127.610001 541.499023 169.970001 46.279999
Low 126.879997 535.250000 167.570007 45.619999
Close 127.029999 540.479980 168.619995 45.910000
Volume 30137100.000000 1196000.000000 3536800.000000 25787000.000000
Adj Close 127.029999 540.479980 168.619995 45.910000
2015-06-24 Open 127.209999 540.000000 168.710007 45.669998
High 129.800003 540.000000 168.839996 46.250000
Low 127.120003 535.659973 166.830002 45.549999
Close 128.110001 537.840027 166.970001 45.639999
Volume 54964900.000000 1283400.000000 3526500.000000 34784600.000000
Adj Close 128.110001 537.840027 166.970001 45.639999

In [25]:
pdata.ix[:,'2015-06-22':,:].to_frame().reset_index().set_index(['Date','minor'])


Out[25]:
Open High Low Close Volume Adj Close
Date minor
2015-06-22 AAPL 127.489998 128.059998 127.080002 127.610001 33833500 127.610001
GOOG 539.590027 543.739990 537.530029 538.190002 1242500 538.190002
IBM 167.649994 168.339996 167.199997 167.729996 2210700 167.729996
MSFT 46.330002 46.720001 46.160000 46.230000 20278600 46.230000
2015-06-23 AAPL 127.480003 127.610001 126.879997 127.029999 30137100 127.029999
GOOG 539.640015 541.499023 535.250000 540.479980 1196000 540.479980
IBM 168.380005 169.970001 167.570007 168.619995 3536800 168.619995
MSFT 46.130001 46.279999 45.619999 45.910000 25787000 45.910000
2015-06-24 AAPL 127.209999 129.800003 127.120003 128.110001 54964900 128.110001
GOOG 540.000000 540.000000 535.659973 537.840027 1283400 537.840027
IBM 168.710007 168.839996 166.830002 166.970001 3526500 166.970001
MSFT 45.669998 46.250000 45.549999 45.639999 34784600 45.639999

In [ ]: