## Using Python (and R) for Finance -- (Sanjiv Das)

In this notebook we will get started with using Python for financial computation. You first need to download and install IPython which is an entire system for interactive use of Python. Luckily, this is easily done by visiting the following URL and downloading the version applicable to your machine (Python runs on all platforms). 

Visit Continuum Analytics and download Python: http://continuum.io/downloads and install it. Test that it works by using the Launcher (which will be on your desktop) to start up the IPython Notebook. 

Also install the R programming language, which is a very useful tool for Machine Learning. 
See: http://en.wikipedia.org/wiki/Machine_learning
Get R from: http://www.r-project.org/ (download and install it). 

In order to allow Python to interface with R, you also need to install the "rpy2" package from: http://rpy.sourceforge.net/
While you do not need to use R, and can get by mostly with Python, it is also a useful language as there are heaps of finance packages in R. As you will see, we can run both within the IPython Notebook. (Indeed, these lecture notes have been prepared in the Notebook.)

If you want to use R in IDE mode, download RStudio: http://www.rstudio.com/

Particularly useful are linear algebra capabilities in Python and R. In Python the package numPy makes these facile. Also useful are graphical libraries, and in Python, these comes in the matplotlib package. 


```python
#Invoke numPy and matplotlib in one command
%pylab inline
```

    Populating the interactive namespace from numpy and matplotlib



```python
#IMPORTING STOCK DATA USING PANDAS
from pandas.io.data import DataReader
from datetime import datetime
goog = DataReader("GOOG",  "yahoo", datetime(2014,4,1), datetime(2015,3,31))
stkp = goog["Adj Close"]
print stkp
```

    Date
    2014-04-01    567.15997
    2014-04-02    567.00000
    2014-04-03    569.73999
    2014-04-04    543.14001
    2014-04-07    538.15002
    2014-04-08    554.90002
    2014-04-09    564.14001
    2014-04-10    540.95001
    2014-04-11    530.59998
    2014-04-14    532.52002
    2014-04-15    536.44000
    2014-04-16    556.53998
    2014-04-17    536.09998
    2014-04-21    528.62000
    2014-04-22    534.81000
    ...
    2015-03-11    551.17999
    2015-03-12    555.51001
    2015-03-13    547.32001
    2015-03-16    554.51001
    2015-03-17    550.84003
    2015-03-18    559.50000
    2015-03-19    557.98999
    2015-03-20    560.35999
    2015-03-23    558.81000
    2015-03-24    570.19000
    2015-03-25    558.78497
    2015-03-26    555.16998
    2015-03-27    548.34003
    2015-03-30    552.03003
    2015-03-31    548.00000
    Name: Adj Close, Length: 252



```python
goog.head()
```




<div style="max-height:1000px;max-width:1500px;overflow:auto;">
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>Open</th>
      <th>High</th>
      <th>Low</th>
      <th>Close</th>
      <th>Volume</th>
      <th>Adj Close</th>
    </tr>
    <tr>
      <th>Date</th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>2014-04-01</th>
      <td> 558.71002</td>
      <td> 568.45001</td>
      <td> 558.71002</td>
      <td> 567.15997</td>
      <td>    7900</td>
      <td> 567.15997</td>
    </tr>
    <tr>
      <th>2014-04-02</th>
      <td> 599.98999</td>
      <td> 604.83002</td>
      <td> 562.19000</td>
      <td> 567.00000</td>
      <td>  146700</td>
      <td> 567.00000</td>
    </tr>
    <tr>
      <th>2014-04-03</th>
      <td> 569.84998</td>
      <td> 587.28003</td>
      <td> 564.13000</td>
      <td> 569.73999</td>
      <td> 5085200</td>
      <td> 569.73999</td>
    </tr>
    <tr>
      <th>2014-04-04</th>
      <td> 574.65002</td>
      <td> 577.77002</td>
      <td> 543.00000</td>
      <td> 543.14001</td>
      <td> 6351900</td>
      <td> 543.14001</td>
    </tr>
    <tr>
      <th>2014-04-07</th>
      <td> 540.73999</td>
      <td> 548.47998</td>
      <td> 527.15002</td>
      <td> 538.15002</td>
      <td> 4389600</td>
      <td> 538.15002</td>
    </tr>
  </tbody>
</table>
</div>




```python
goog.index
```




    <class 'pandas.tseries.index.DatetimeIndex'>
    [2014-04-01, ..., 2015-03-31]
    Length: 252, Freq: None, Timezone: None




```python
t = goog.index
plot(t,stkp)
xlabel("Date")
ylabel("Stock Price")
```


```python
n = len(t)-1
rets = zeros(n)
for j in range(n):
    rets[j] = log(stkp[j+1]/stkp[j])
plot(rets)
ylabel("Returns")
```


```python
hist(rets,25)
```


```python
goog.describe()
```




<div style="max-height:1000px;max-width:1500px;overflow:auto;">
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>Open</th>
      <th>High</th>
      <th>Low</th>
      <th>Close</th>
      <th>Volume</th>
      <th>Adj Close</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>count</th>
      <td> 252.000000</td>
      <td> 252.000000</td>
      <td> 252.000000</td>
      <td> 252.000000</td>
      <td>     252.000000</td>
      <td> 252.000000</td>
    </tr>
    <tr>
      <th>mean</th>
      <td> 550.523850</td>
      <td> 554.668048</td>
      <td> 545.193662</td>
      <td> 550.081029</td>
      <td> 1922350.000000</td>
      <td> 550.081029</td>
    </tr>
    <tr>
      <th>std</th>
      <td>  24.599106</td>
      <td>  24.145277</td>
      <td>  24.955973</td>
      <td>  24.524011</td>
      <td>  941218.139443</td>
      <td>  24.524011</td>
    </tr>
    <tr>
      <th>min</th>
      <td> 494.649990</td>
      <td> 495.976010</td>
      <td> 487.560000</td>
      <td> 492.549990</td>
      <td>    7900.000000</td>
      <td> 492.549990</td>
    </tr>
    <tr>
      <th>25%</th>
      <td> 531.007507</td>
      <td> 535.871978</td>
      <td> 526.500015</td>
      <td> 531.240007</td>
      <td> 1371625.000000</td>
      <td> 531.240007</td>
    </tr>
    <tr>
      <th>50%</th>
      <td> 550.000000</td>
      <td> 553.994995</td>
      <td> 544.024995</td>
      <td> 549.170015</td>
      <td> 1691350.000000</td>
      <td> 549.170015</td>
    </tr>
    <tr>
      <th>75%</th>
      <td> 571.622515</td>
      <td> 576.689985</td>
      <td> 566.231262</td>
      <td> 571.405017</td>
      <td> 2193975.000000</td>
      <td> 571.405017</td>
    </tr>
    <tr>
      <th>max</th>
      <td> 599.989990</td>
      <td> 604.830020</td>
      <td> 592.500000</td>
      <td> 596.080020</td>
      <td> 6790900.000000</td>
      <td> 596.080020</td>
    </tr>
  </tbody>
</table>
</div>




```python
import scipy.stats as ss
print("Skewness = ",ss.skew(rets))
print("Kurtosis = ",ss.kurtosis(rets))

#CHECK IF THIS IS EXCESS KURTOSIS or PLAIN KURTOSIS
x = randn(1000000)
print(ss.kurtosis(x))
```

    ('Skewness = ', -0.173028777259206)
    ('Kurtosis = ', 0.9993234508893414)
    0.00302654770593


### SHARING VARIABLES FROM PYTHON TO R


```python
#SENDING DATA VARIABLES TO R
%load_ext rpy2.ipython
#THIS ALLOWS US TO USE R INSIDE THE NOTEBOOK
```


```python
#SENDS DATA FROM PYTHON TO R
%Rpush stkp       
```


```python
#PREFIX NEEDED TO CALL R INSTEAD OF PYTHON
%R plot(stkp,type="l",col="red",lwd=2)   
```


```python
#GETTING DATA BACK FROM R TO PYTHON
%R ret = diff(log(stkp))
#GET DATA BACK FROM R TO PYTHON
ret = %Rget ret   
plot(ret)
%R print(summary(ret))
```

### GETTING STOCK DATA FROM R INTO PYTHON


```r
%%R
library(quantmod)
getSymbols(c("C","AAPL","CSCO","YHOO","IBM"))
```


    Loading required package: xts
    Loading required package: zoo
    
    Attaching package: ‘zoo’
    
    The following objects are masked from ‘package:base’:
    
        as.Date, as.Date.numeric
    
    Loading required package: TTR
    Version 0.4-0 included new data defaults. See ?getSymbols.
        As of 0.4-0, ‘getSymbols’ uses env=parent.frame() and
     auto.assign=TRUE by default.
    
     This  behavior  will be  phased out in 0.5-0  when the call  will
     default to use auto.assign=FALSE. getOption("getSymbols.env") and 
     getOptions("getSymbols.auto.assign") are now checked for alternate defaults
    
     This message is shown once per session and may be disabled by setting 
     options("getSymbols.warning4.0"=FALSE). See ?getSymbol for more details
    [1] "C"    "AAPL" "CSCO" "YHOO" "IBM" 




```r
%%R 
citi = as.matrix(C$C.Adjusted)
aapl = as.matrix(AAPL$AAPL.Adjusted)
csco = as.matrix(CSCO$CSCO.Adjusted)
yhoo = as.matrix(YHOO$YHOO.Adjusted)
ibm = as.matrix(IBM$IBM.Adjusted)
```


```r
%%R 
stkdata = data.frame(cbind(citi,aapl,csco,yhoo,ibm))
rn = rownames(stkdata)
head(stkdata)
```


               C.Adjusted AAPL.Adjusted CSCO.Adjusted YHOO.Adjusted IBM.Adjusted
    2007-01-03   497.5920      11.29213      24.94332         25.61     83.41148
    2007-01-04   495.8808      11.54276      25.59996         26.85     84.30331
    2007-01-05   493.2690      11.46057      25.60896         27.74     83.54011
    2007-01-08   495.7907      11.51716      25.75288         27.92     84.80925
    2007-01-09   491.4678      12.47390      25.60896         27.58     85.81255
    2007-01-10   487.5050      13.07084      25.79785         28.70     84.80067




```python
stkdata = %Rget stkdata
rn = %Rget rn
```


```python
stkdata
```




    array([[ 497.59198,  495.8808 ,  493.26904, ...,   53.76   ,   53.32   ,
              53.76   ],
           [  11.29213,   11.54276,   11.46057, ...,  128.64   ,  125.15   ,
             128.95   ],
           [  24.94332,   25.59996,   25.60896, ...,   29.05   ,   28.83   ,
              29.13   ],
           [  25.61   ,   26.85   ,   27.74   , ...,   43.28   ,   42.57   ,
              42.51   ],
           [  83.41148,   84.30331,   83.54011, ...,  174.39999,  171.28999,
             173.67   ]])




```python
rn
```




    array(['2007-01-03', '2007-01-04', '2007-01-05', ..., '2015-04-29',
           '2015-04-30', '2015-05-01'], 
          dtype='|S10')



### PANDAS IS AWESOME


```python
import pandas as pd
stk = pd.DataFrame(stkdata)
stk = stk.T
stk.head()
```




<div style="max-height:1000px;max-width:1500px;overflow:auto;">
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>0</th>
      <th>1</th>
      <th>2</th>
      <th>3</th>
      <th>4</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td> 497.59198</td>
      <td> 11.29213</td>
      <td> 24.94332</td>
      <td> 25.61</td>
      <td> 83.41148</td>
    </tr>
    <tr>
      <th>1</th>
      <td> 495.88080</td>
      <td> 11.54276</td>
      <td> 25.59996</td>
      <td> 26.85</td>
      <td> 84.30331</td>
    </tr>
    <tr>
      <th>2</th>
      <td> 493.26904</td>
      <td> 11.46057</td>
      <td> 25.60896</td>
      <td> 27.74</td>
      <td> 83.54011</td>
    </tr>
    <tr>
      <th>3</th>
      <td> 495.79074</td>
      <td> 11.51716</td>
      <td> 25.75288</td>
      <td> 27.92</td>
      <td> 84.80925</td>
    </tr>
    <tr>
      <th>4</th>
      <td> 491.46780</td>
      <td> 12.47390</td>
      <td> 25.60896</td>
      <td> 27.58</td>
      <td> 85.81255</td>
    </tr>
  </tbody>
</table>
</div>




```python
stk.columns=["C","AAPL","CSCO","YHOO","IBM"]
stk.index = rn
stk.head()
```




<div style="max-height:1000px;max-width:1500px;overflow:auto;">
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>C</th>
      <th>AAPL</th>
      <th>CSCO</th>
      <th>YHOO</th>
      <th>IBM</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>2007-01-03</th>
      <td> 497.59198</td>
      <td> 11.29213</td>
      <td> 24.94332</td>
      <td> 25.61</td>
      <td> 83.41148</td>
    </tr>
    <tr>
      <th>2007-01-04</th>
      <td> 495.88080</td>
      <td> 11.54276</td>
      <td> 25.59996</td>
      <td> 26.85</td>
      <td> 84.30331</td>
    </tr>
    <tr>
      <th>2007-01-05</th>
      <td> 493.26904</td>
      <td> 11.46057</td>
      <td> 25.60896</td>
      <td> 27.74</td>
      <td> 83.54011</td>
    </tr>
    <tr>
      <th>2007-01-08</th>
      <td> 495.79074</td>
      <td> 11.51716</td>
      <td> 25.75288</td>
      <td> 27.92</td>
      <td> 84.80925</td>
    </tr>
    <tr>
      <th>2007-01-09</th>
      <td> 491.46780</td>
      <td> 12.47390</td>
      <td> 25.60896</td>
      <td> 27.58</td>
      <td> 85.81255</td>
    </tr>
  </tbody>
</table>
</div>




```python
plot(stk["AAPL"])
```


```python
stk.ix['2007-01-03']
```




    C       497.59198
    AAPL     11.29213
    CSCO     24.94332
    YHOO     25.61000
    IBM      83.41148
    Name: 2007-01-03, dtype: float64




```python
stk.ix['2007-01-03']["AAPL"]
```




    11.29213




```python
stk["extra"] = 1.0
stk.head()
```




<div style="max-height:1000px;max-width:1500px;overflow:auto;">
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>C</th>
      <th>AAPL</th>
      <th>CSCO</th>
      <th>YHOO</th>
      <th>IBM</th>
      <th>extra</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>2007-01-03</th>
      <td> 497.59198</td>
      <td> 11.29213</td>
      <td> 24.94332</td>
      <td> 25.61</td>
      <td> 83.41148</td>
      <td> 1</td>
    </tr>
    <tr>
      <th>2007-01-04</th>
      <td> 495.88080</td>
      <td> 11.54276</td>
      <td> 25.59996</td>
      <td> 26.85</td>
      <td> 84.30331</td>
      <td> 1</td>
    </tr>
    <tr>
      <th>2007-01-05</th>
      <td> 493.26904</td>
      <td> 11.46057</td>
      <td> 25.60896</td>
      <td> 27.74</td>
      <td> 83.54011</td>
      <td> 1</td>
    </tr>
    <tr>
      <th>2007-01-08</th>
      <td> 495.79074</td>
      <td> 11.51716</td>
      <td> 25.75288</td>
      <td> 27.92</td>
      <td> 84.80925</td>
      <td> 1</td>
    </tr>
    <tr>
      <th>2007-01-09</th>
      <td> 491.46780</td>
      <td> 12.47390</td>
      <td> 25.60896</td>
      <td> 27.58</td>
      <td> 85.81255</td>
      <td> 1</td>
    </tr>
  </tbody>
</table>
</div>




```python
sort(stk["AAPL"])
```




    array([  10.53752,   10.8461 ,   11.09404, ...,  132.17   ,  132.64999,
            133.     ])




```python
stk.head()
```




<div style="max-height:1000px;max-width:1500px;overflow:auto;">
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>C</th>
      <th>AAPL</th>
      <th>CSCO</th>
      <th>YHOO</th>
      <th>IBM</th>
      <th>extra</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>2007-01-03</th>
      <td> 497.59198</td>
      <td> 11.29213</td>
      <td> 24.94332</td>
      <td> 25.61</td>
      <td> 83.41148</td>
      <td> 1</td>
    </tr>
    <tr>
      <th>2007-01-04</th>
      <td> 495.88080</td>
      <td> 11.54276</td>
      <td> 25.59996</td>
      <td> 26.85</td>
      <td> 84.30331</td>
      <td> 1</td>
    </tr>
    <tr>
      <th>2007-01-05</th>
      <td> 493.26904</td>
      <td> 11.46057</td>
      <td> 25.60896</td>
      <td> 27.74</td>
      <td> 83.54011</td>
      <td> 1</td>
    </tr>
    <tr>
      <th>2007-01-08</th>
      <td> 495.79074</td>
      <td> 11.51716</td>
      <td> 25.75288</td>
      <td> 27.92</td>
      <td> 84.80925</td>
      <td> 1</td>
    </tr>
    <tr>
      <th>2007-01-09</th>
      <td> 491.46780</td>
      <td> 12.47390</td>
      <td> 25.60896</td>
      <td> 27.58</td>
      <td> 85.81255</td>
      <td> 1</td>
    </tr>
  </tbody>
</table>
</div>




```python
stk = stk.drop("extra",axis=1)  #IF AXIS=0 (default), THEN ROW IS DROPPED
stk.head()
```




<div style="max-height:1000px;max-width:1500px;overflow:auto;">
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>C</th>
      <th>AAPL</th>
      <th>CSCO</th>
      <th>YHOO</th>
      <th>IBM</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>2007-01-03</th>
      <td> 497.59198</td>
      <td> 11.29213</td>
      <td> 24.94332</td>
      <td> 25.61</td>
      <td> 83.41148</td>
    </tr>
    <tr>
      <th>2007-01-04</th>
      <td> 495.88080</td>
      <td> 11.54276</td>
      <td> 25.59996</td>
      <td> 26.85</td>
      <td> 84.30331</td>
    </tr>
    <tr>
      <th>2007-01-05</th>
      <td> 493.26904</td>
      <td> 11.46057</td>
      <td> 25.60896</td>
      <td> 27.74</td>
      <td> 83.54011</td>
    </tr>
    <tr>
      <th>2007-01-08</th>
      <td> 495.79074</td>
      <td> 11.51716</td>
      <td> 25.75288</td>
      <td> 27.92</td>
      <td> 84.80925</td>
    </tr>
    <tr>
      <th>2007-01-09</th>
      <td> 491.46780</td>
      <td> 12.47390</td>
      <td> 25.60896</td>
      <td> 27.58</td>
      <td> 85.81255</td>
    </tr>
  </tbody>
</table>
</div>




```python
stk[["AAPL","IBM"]].head()
```




<div style="max-height:1000px;max-width:1500px;overflow:auto;">
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>AAPL</th>
      <th>IBM</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>2007-01-03</th>
      <td> 11.29213</td>
      <td> 83.41148</td>
    </tr>
    <tr>
      <th>2007-01-04</th>
      <td> 11.54276</td>
      <td> 84.30331</td>
    </tr>
    <tr>
      <th>2007-01-05</th>
      <td> 11.46057</td>
      <td> 83.54011</td>
    </tr>
    <tr>
      <th>2007-01-08</th>
      <td> 11.51716</td>
      <td> 84.80925</td>
    </tr>
    <tr>
      <th>2007-01-09</th>
      <td> 12.47390</td>
      <td> 85.81255</td>
    </tr>
  </tbody>
</table>
</div>




```python
stk[stk["AAPL"]<11]
```




<div style="max-height:1000px;max-width:1500px;overflow:auto;">
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>C</th>
      <th>AAPL</th>
      <th>CSCO</th>
      <th>YHOO</th>
      <th>IBM</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>2008-11-20</th>
      <td> 46.80173</td>
      <td> 10.84610</td>
      <td> 13.01586</td>
      <td>  8.95</td>
      <td> 63.47624</td>
    </tr>
    <tr>
      <th>2009-01-20</th>
      <td> 27.82269</td>
      <td> 10.53752</td>
      <td> 13.50160</td>
      <td> 11.01</td>
      <td> 72.53670</td>
    </tr>
  </tbody>
</table>
</div>




```python
stk[stk["AAPL"]<11]["IBM"]
```




    2008-11-20    63.48
    2009-01-20    72.54
    Name: IBM, dtype: float64




```python
(stk < 50).head()
```




<div style="max-height:1000px;max-width:1500px;overflow:auto;">
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>C</th>
      <th>AAPL</th>
      <th>CSCO</th>
      <th>YHOO</th>
      <th>IBM</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>2007-01-03</th>
      <td> False</td>
      <td> True</td>
      <td> True</td>
      <td> True</td>
      <td> False</td>
    </tr>
    <tr>
      <th>2007-01-04</th>
      <td> False</td>
      <td> True</td>
      <td> True</td>
      <td> True</td>
      <td> False</td>
    </tr>
    <tr>
      <th>2007-01-05</th>
      <td> False</td>
      <td> True</td>
      <td> True</td>
      <td> True</td>
      <td> False</td>
    </tr>
    <tr>
      <th>2007-01-08</th>
      <td> False</td>
      <td> True</td>
      <td> True</td>
      <td> True</td>
      <td> False</td>
    </tr>
    <tr>
      <th>2007-01-09</th>
      <td> False</td>
      <td> True</td>
      <td> True</td>
      <td> True</td>
      <td> False</td>
    </tr>
  </tbody>
</table>
</div>




```python
sum(stk)
```




    C       221567.15
    AAPL    102168.36
    CSCO     42781.91
    YHOO     47775.87
    IBM     291151.26
    dtype: float64




```python
#USING FUNCTIONS ON DATA FRAMES
f = lambda x: x.max() - x.min()
stk.apply(f)
```




    C       496.94
    AAPL    122.46
    CSCO     18.41
    YHOO     43.42
    IBM     142.62
    dtype: float64




```python
def g(x):
    return pd.Series([x.mean(),x.std(),x.min(),x.max()], index=['mean','stdev','min','max'])
stk.apply(g)
```




<div style="max-height:1000px;max-width:1500px;overflow:auto;">
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>C</th>
      <th>AAPL</th>
      <th>CSCO</th>
      <th>YHOO</th>
      <th>IBM</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>mean</th>
      <td> 106.522668</td>
      <td>  49.119404</td>
      <td> 20.568226</td>
      <td> 22.969168</td>
      <td> 139.976567</td>
    </tr>
    <tr>
      <th>stdev</th>
      <td> 135.337468</td>
      <td>  30.355509</td>
      <td>  3.962029</td>
      <td>  9.759319</td>
      <td>  40.315380</td>
    </tr>
    <tr>
      <th>min</th>
      <td>  10.160000</td>
      <td>  10.540000</td>
      <td> 12.250000</td>
      <td>  8.950000</td>
      <td>  63.480000</td>
    </tr>
    <tr>
      <th>max</th>
      <td> 507.100000</td>
      <td> 133.000000</td>
      <td> 30.660000</td>
      <td> 52.370000</td>
      <td> 206.100000</td>
    </tr>
  </tbody>
</table>
</div>




```python
stk.sort_index(axis=1,ascending=False).head()
```




<div style="max-height:1000px;max-width:1500px;overflow:auto;">
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>YHOO</th>
      <th>IBM</th>
      <th>CSCO</th>
      <th>C</th>
      <th>AAPL</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>2007-01-03</th>
      <td> 25.61</td>
      <td> 83.41</td>
      <td> 24.94</td>
      <td> 497.59</td>
      <td> 11.29</td>
    </tr>
    <tr>
      <th>2007-01-04</th>
      <td> 26.85</td>
      <td> 84.30</td>
      <td> 25.60</td>
      <td> 495.88</td>
      <td> 11.54</td>
    </tr>
    <tr>
      <th>2007-01-05</th>
      <td> 27.74</td>
      <td> 83.54</td>
      <td> 25.61</td>
      <td> 493.27</td>
      <td> 11.46</td>
    </tr>
    <tr>
      <th>2007-01-08</th>
      <td> 27.92</td>
      <td> 84.81</td>
      <td> 25.75</td>
      <td> 495.79</td>
      <td> 11.52</td>
    </tr>
    <tr>
      <th>2007-01-09</th>
      <td> 27.58</td>
      <td> 85.81</td>
      <td> 25.61</td>
      <td> 491.47</td>
      <td> 12.47</td>
    </tr>
  </tbody>
</table>
</div>




```python
stk.sum()
```




    C       221567.15
    AAPL    102168.36
    CSCO     42781.91
    YHOO     47775.87
    IBM     291151.26
    dtype: float64




```python
stk.mean()
```




    C       106.522668
    AAPL     49.119404
    CSCO     20.568226
    YHOO     22.969168
    IBM     139.976567
    dtype: float64




```python
stk.describe()
```




<div style="max-height:1000px;max-width:1500px;overflow:auto;">
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>C</th>
      <th>AAPL</th>
      <th>CSCO</th>
      <th>YHOO</th>
      <th>IBM</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>count</th>
      <td> 2080.000000</td>
      <td> 2080.000000</td>
      <td> 2080.000000</td>
      <td> 2080.000000</td>
      <td> 2080.000000</td>
    </tr>
    <tr>
      <th>mean</th>
      <td>  106.522668</td>
      <td>   49.119404</td>
      <td>   20.568226</td>
      <td>   22.969168</td>
      <td>  139.976567</td>
    </tr>
    <tr>
      <th>std</th>
      <td>  135.337468</td>
      <td>   30.355509</td>
      <td>    3.962029</td>
      <td>    9.759319</td>
      <td>   40.315380</td>
    </tr>
    <tr>
      <th>min</th>
      <td>   10.160000</td>
      <td>   10.540000</td>
      <td>   12.250000</td>
      <td>    8.950000</td>
      <td>   63.480000</td>
    </tr>
    <tr>
      <th>25%</th>
      <td>   37.145000</td>
      <td>   22.397500</td>
      <td>   17.407500</td>
      <td>   15.450000</td>
      <td>  102.350000</td>
    </tr>
    <tr>
      <th>50%</th>
      <td>   47.025000</td>
      <td>   45.075000</td>
      <td>   20.800000</td>
      <td>   18.250000</td>
      <td>  148.295000</td>
    </tr>
    <tr>
      <th>75%</th>
      <td>   54.675000</td>
      <td>   73.145000</td>
      <td>   23.500000</td>
      <td>   28.435000</td>
      <td>  180.472500</td>
    </tr>
    <tr>
      <th>max</th>
      <td>  507.100000</td>
      <td>  133.000000</td>
      <td>   30.660000</td>
      <td>   52.370000</td>
      <td>  206.100000</td>
    </tr>
  </tbody>
</table>
</div>




```python
stk.diff().head()
```




<div style="max-height:1000px;max-width:1500px;overflow:auto;">
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>C</th>
      <th>AAPL</th>
      <th>CSCO</th>
      <th>YHOO</th>
      <th>IBM</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>2007-01-03</th>
      <td>  NaN</td>
      <td>  NaN</td>
      <td>  NaN</td>
      <td>  NaN</td>
      <td>  NaN</td>
    </tr>
    <tr>
      <th>2007-01-04</th>
      <td>-1.71</td>
      <td> 0.25</td>
      <td> 0.66</td>
      <td> 1.24</td>
      <td> 0.89</td>
    </tr>
    <tr>
      <th>2007-01-05</th>
      <td>-2.61</td>
      <td>-0.08</td>
      <td> 0.01</td>
      <td> 0.89</td>
      <td>-0.76</td>
    </tr>
    <tr>
      <th>2007-01-08</th>
      <td> 2.52</td>
      <td> 0.06</td>
      <td> 0.14</td>
      <td> 0.18</td>
      <td> 1.27</td>
    </tr>
    <tr>
      <th>2007-01-09</th>
      <td>-4.32</td>
      <td> 0.95</td>
      <td>-0.14</td>
      <td>-0.34</td>
      <td> 1.00</td>
    </tr>
  </tbody>
</table>
</div>




```python
stk.pct_change().head()
```




<div style="max-height:1000px;max-width:1500px;overflow:auto;">
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>C</th>
      <th>AAPL</th>
      <th>CSCO</th>
      <th>YHOO</th>
      <th>IBM</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>2007-01-03</th>
      <td>      NaN</td>
      <td>      NaN</td>
      <td>      NaN</td>
      <td>      NaN</td>
      <td>      NaN</td>
    </tr>
    <tr>
      <th>2007-01-04</th>
      <td>-0.003437</td>
      <td> 0.022143</td>
      <td> 0.026464</td>
      <td> 0.048419</td>
      <td> 0.010670</td>
    </tr>
    <tr>
      <th>2007-01-05</th>
      <td>-0.005263</td>
      <td>-0.006932</td>
      <td> 0.000391</td>
      <td> 0.033147</td>
      <td>-0.009015</td>
    </tr>
    <tr>
      <th>2007-01-08</th>
      <td> 0.005109</td>
      <td> 0.005236</td>
      <td> 0.005467</td>
      <td> 0.006489</td>
      <td> 0.015202</td>
    </tr>
    <tr>
      <th>2007-01-09</th>
      <td>-0.008713</td>
      <td> 0.082465</td>
      <td>-0.005437</td>
      <td>-0.012178</td>
      <td> 0.011791</td>
    </tr>
  </tbody>
</table>
</div>




```python
stk.pct_change().mean()*252.0
```




    C      -0.052665
    AAPL    0.351436
    CSCO    0.062700
    YHOO    0.152730
    IBM     0.106559
    dtype: float64




```python
stk.pct_change().std()*sqrt(252.0)
```




    C       0.669590
    AAPL    0.343694
    CSCO    0.317718
    YHOO    0.416351
    IBM     0.228973
    dtype: float64




```python
rets = stk.pct_change()
rets.corr()
```




<div style="max-height:1000px;max-width:1500px;overflow:auto;">
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>C</th>
      <th>AAPL</th>
      <th>CSCO</th>
      <th>YHOO</th>
      <th>IBM</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>C</th>
      <td> 1.000000</td>
      <td> 0.378732</td>
      <td> 0.465752</td>
      <td> 0.330307</td>
      <td> 0.442698</td>
    </tr>
    <tr>
      <th>AAPL</th>
      <td> 0.378732</td>
      <td> 1.000000</td>
      <td> 0.487141</td>
      <td> 0.342638</td>
      <td> 0.485708</td>
    </tr>
    <tr>
      <th>CSCO</th>
      <td> 0.465752</td>
      <td> 0.487141</td>
      <td> 1.000000</td>
      <td> 0.406206</td>
      <td> 0.582500</td>
    </tr>
    <tr>
      <th>YHOO</th>
      <td> 0.330307</td>
      <td> 0.342638</td>
      <td> 0.406206</td>
      <td> 1.000000</td>
      <td> 0.377730</td>
    </tr>
    <tr>
      <th>IBM</th>
      <td> 0.442698</td>
      <td> 0.485708</td>
      <td> 0.582500</td>
      <td> 0.377730</td>
      <td> 1.000000</td>
    </tr>
  </tbody>
</table>
</div>




```python
rets.cov()
```




<div style="max-height:1000px;max-width:1500px;overflow:auto;">
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>C</th>
      <th>AAPL</th>
      <th>CSCO</th>
      <th>YHOO</th>
      <th>IBM</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>C</th>
      <td> 0.001779</td>
      <td> 0.000346</td>
      <td> 0.000393</td>
      <td> 0.000365</td>
      <td> 0.000269</td>
    </tr>
    <tr>
      <th>AAPL</th>
      <td> 0.000346</td>
      <td> 0.000469</td>
      <td> 0.000211</td>
      <td> 0.000195</td>
      <td> 0.000152</td>
    </tr>
    <tr>
      <th>CSCO</th>
      <td> 0.000393</td>
      <td> 0.000211</td>
      <td> 0.000401</td>
      <td> 0.000213</td>
      <td> 0.000168</td>
    </tr>
    <tr>
      <th>YHOO</th>
      <td> 0.000365</td>
      <td> 0.000195</td>
      <td> 0.000213</td>
      <td> 0.000688</td>
      <td> 0.000143</td>
    </tr>
    <tr>
      <th>IBM</th>
      <td> 0.000269</td>
      <td> 0.000152</td>
      <td> 0.000168</td>
      <td> 0.000143</td>
      <td> 0.000208</td>
    </tr>
  </tbody>
</table>
</div>




```python
sqrt(diag(rets.cov())*252.0)
```




    array([ 0.66958975,  0.3436938 ,  0.31771753,  0.41635095,  0.22897315])




```python
rets.corrwith(rets.AAPL)
```




    C       0.378732
    AAPL    1.000000
    CSCO    0.487141
    YHOO    0.342638
    IBM     0.485708
    dtype: float64



### STACKING UP DATA IN PANELS


```python
import pandas.io.data as pid
panel = pd.Panel(dict((stock, pid.get_data_yahoo(stock,'1/1/2014','2/28/2015')) for stock in ['C','AAPL','CSCO','YHOO','IBM']))
panel
```




    <class 'pandas.core.panel.Panel'>
    Dimensions: 5 (items) x 291 (major_axis) x 6 (minor_axis)
    Items axis: AAPL to YHOO
    Major_axis axis: 2014-01-02 00:00:00 to 2015-02-27 00:00:00
    Minor_axis axis: Open to Adj Close




```python
panel = panel.swapaxes('items','minor')
panel
```




    <class 'pandas.core.panel.Panel'>
    Dimensions: 6 (items) x 291 (major_axis) x 5 (minor_axis)
    Items axis: Open to Adj Close
    Major_axis axis: 2014-01-02 00:00:00 to 2015-02-27 00:00:00
    Minor_axis axis: AAPL to YHOO




```python
panel['Adj Close'].head()
```




<div style="max-height:1000px;max-width:1500px;overflow:auto;">
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>AAPL</th>
      <th>C</th>
      <th>CSCO</th>
      <th>IBM</th>
      <th>YHOO</th>
    </tr>
    <tr>
      <th>Date</th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>2014-01-02</th>
      <td> 77.09</td>
      <td> 52.22</td>
      <td> 21.18</td>
      <td> 179.87</td>
      <td> 39.59</td>
    </tr>
    <tr>
      <th>2014-01-03</th>
      <td> 75.39</td>
      <td> 53.35</td>
      <td> 21.16</td>
      <td> 180.95</td>
      <td> 40.12</td>
    </tr>
    <tr>
      <th>2014-01-06</th>
      <td> 75.80</td>
      <td> 53.76</td>
      <td> 21.19</td>
      <td> 180.33</td>
      <td> 39.93</td>
    </tr>
    <tr>
      <th>2014-01-07</th>
      <td> 75.26</td>
      <td> 54.12</td>
      <td> 21.47</td>
      <td> 183.92</td>
      <td> 40.92</td>
    </tr>
    <tr>
      <th>2014-01-08</th>
      <td> 75.74</td>
      <td> 54.75</td>
      <td> 21.45</td>
      <td> 182.24</td>
      <td> 41.02</td>
    </tr>
  </tbody>
</table>
</div>




```python
panel.ix[:,'1/3/2014',:]
```




<div style="max-height:1000px;max-width:1500px;overflow:auto;">
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>Open</th>
      <th>High</th>
      <th>Low</th>
      <th>Close</th>
      <th>Volume</th>
      <th>Adj Close</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>AAPL</th>
      <td> 552.86</td>
      <td> 553.70</td>
      <td> 540.43</td>
      <td> 540.98</td>
      <td> 98116900</td>
      <td>  75.39</td>
    </tr>
    <tr>
      <th>C</th>
      <td>  52.39</td>
      <td>  53.47</td>
      <td>  52.31</td>
      <td>  53.40</td>
      <td> 26884900</td>
      <td>  53.35</td>
    </tr>
    <tr>
      <th>CSCO</th>
      <td>  22.09</td>
      <td>  22.12</td>
      <td>  21.83</td>
      <td>  21.98</td>
      <td> 36328200</td>
      <td>  21.16</td>
    </tr>
    <tr>
      <th>IBM</th>
      <td> 185.83</td>
      <td> 187.35</td>
      <td> 185.30</td>
      <td> 186.64</td>
      <td>  4063200</td>
      <td> 180.95</td>
    </tr>
    <tr>
      <th>YHOO</th>
      <td>  40.16</td>
      <td>  40.44</td>
      <td>  39.82</td>
      <td>  40.12</td>
      <td> 15755200</td>
      <td>  40.12</td>
    </tr>
  </tbody>
</table>
</div>



### READING DATA FROM FILES


```python
import pandas as pd
data = pd.read_table("markowitzdata.txt")
```


```python
data.head()
```




<div style="max-height:1000px;max-width:1500px;overflow:auto;">
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>DATE</th>
      <th>SUNW</th>
      <th>MSFT</th>
      <th>IBM</th>
      <th>CSCO</th>
      <th>AMZN</th>
      <th>mktrf</th>
      <th>smb</th>
      <th>hml</th>
      <th>rf</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td> 20010102</td>
      <td>-0.087444</td>
      <td> 0.000000</td>
      <td>-0.002206</td>
      <td>-0.129085</td>
      <td>-0.108434</td>
      <td>-0.0345</td>
      <td>-0.0037</td>
      <td> 0.0209</td>
      <td> 0.00026</td>
    </tr>
    <tr>
      <th>1</th>
      <td> 20010103</td>
      <td> 0.297297</td>
      <td> 0.105187</td>
      <td> 0.115696</td>
      <td> 0.240150</td>
      <td> 0.265766</td>
      <td> 0.0527</td>
      <td> 0.0097</td>
      <td>-0.0493</td>
      <td> 0.00026</td>
    </tr>
    <tr>
      <th>2</th>
      <td> 20010104</td>
      <td>-0.060606</td>
      <td> 0.010430</td>
      <td>-0.015192</td>
      <td> 0.013616</td>
      <td>-0.117438</td>
      <td>-0.0121</td>
      <td> 0.0083</td>
      <td>-0.0015</td>
      <td> 0.00026</td>
    </tr>
    <tr>
      <th>3</th>
      <td> 20010105</td>
      <td>-0.096774</td>
      <td> 0.014194</td>
      <td> 0.008719</td>
      <td>-0.125373</td>
      <td>-0.060484</td>
      <td>-0.0291</td>
      <td> 0.0027</td>
      <td> 0.0242</td>
      <td> 0.00026</td>
    </tr>
    <tr>
      <th>4</th>
      <td> 20010108</td>
      <td> 0.006696</td>
      <td>-0.003817</td>
      <td>-0.004654</td>
      <td>-0.002133</td>
      <td> 0.025751</td>
      <td>-0.0037</td>
      <td>-0.0053</td>
      <td> 0.0129</td>
      <td> 0.00026</td>
    </tr>
  </tbody>
</table>
</div>




```python
gdata = pd.read_csv("goog.csv")
```


```python
gdata.head()
```




<div style="max-height:1000px;max-width:1500px;overflow:auto;">
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>Date</th>
      <th>Open</th>
      <th>High</th>
      <th>Low</th>
      <th>Close</th>
      <th>Volume</th>
      <th>Adj Close</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td> 2011-04-06</td>
      <td> 572.18</td>
      <td> 575.16</td>
      <td> 568.00</td>
      <td> 574.18</td>
      <td> 2668300</td>
      <td> 574.18</td>
    </tr>
    <tr>
      <th>1</th>
      <td> 2011-04-05</td>
      <td> 581.08</td>
      <td> 581.49</td>
      <td> 565.68</td>
      <td> 569.09</td>
      <td> 6047500</td>
      <td> 569.09</td>
    </tr>
    <tr>
      <th>2</th>
      <td> 2011-04-04</td>
      <td> 593.00</td>
      <td> 594.74</td>
      <td> 583.10</td>
      <td> 587.68</td>
      <td> 2054500</td>
      <td> 587.68</td>
    </tr>
    <tr>
      <th>3</th>
      <td> 2011-04-01</td>
      <td> 588.76</td>
      <td> 595.19</td>
      <td> 588.76</td>
      <td> 591.80</td>
      <td> 2613200</td>
      <td> 591.80</td>
    </tr>
    <tr>
      <th>4</th>
      <td> 2011-03-31</td>
      <td> 583.00</td>
      <td> 588.16</td>
      <td> 581.74</td>
      <td> 586.76</td>
      <td> 2029400</td>
      <td> 586.76</td>
    </tr>
  </tbody>
</table>
</div>




```python
scatter(data["mktrf"],data["IBM"])
xlabel("Market return")
ylabel("IBM return")
grid(True)
```


```python
from scipy import stats
y = data["IBM"]
x = data["mktrf"]
b, a, r_value, p_value, std_err = stats.linregress(x,y)
print "Intercept = ",a
print "slope (beta) = ",b
```

    Intercept =  0.00011890050765
    slope (beta) =  1.10915240511


### BRINGING IT ALL TOGETHER


```python
import pandas as pd
import pandas.io.data as web
aapl = web.DataReader('AAPL',data_source='google',start='1/1/2104',end='4/1/2015')
aapl.head()
```




<div style="max-height:1000px;max-width:1500px;overflow:auto;">
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>Open</th>
      <th>High</th>
      <th>Low</th>
      <th>Close</th>
      <th>Volume</th>
    </tr>
    <tr>
      <th>﻿Date</th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>2014-04-02</th>
      <td> 77.48</td>
      <td> 77.64</td>
      <td> 77.18</td>
      <td> 77.51</td>
      <td> 45104871</td>
    </tr>
    <tr>
      <th>2014-04-03</th>
      <td> 77.34</td>
      <td> 77.50</td>
      <td> 76.81</td>
      <td> 76.97</td>
      <td> 40648111</td>
    </tr>
    <tr>
      <th>2014-04-04</th>
      <td> 77.12</td>
      <td> 77.14</td>
      <td> 75.80</td>
      <td> 75.97</td>
      <td> 68812485</td>
    </tr>
    <tr>
      <th>2014-04-07</th>
      <td> 75.43</td>
      <td> 75.84</td>
      <td> 74.56</td>
      <td> 74.78</td>
      <td> 72462530</td>
    </tr>
    <tr>
      <th>2014-04-08</th>
      <td> 75.03</td>
      <td> 75.16</td>
      <td> 74.10</td>
      <td> 74.78</td>
      <td> 60971883</td>
    </tr>
  </tbody>
</table>
</div>




```python
aapl.tail()
```




<div style="max-height:1000px;max-width:1500px;overflow:auto;">
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>Open</th>
      <th>High</th>
      <th>Low</th>
      <th>Close</th>
      <th>Volume</th>
    </tr>
    <tr>
      <th>﻿Date</th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>2015-03-26</th>
      <td> 122.76</td>
      <td> 124.88</td>
      <td> 122.60</td>
      <td> 124.24</td>
      <td> 47572869</td>
    </tr>
    <tr>
      <th>2015-03-27</th>
      <td> 124.57</td>
      <td> 124.70</td>
      <td> 122.91</td>
      <td> 123.25</td>
      <td> 39546151</td>
    </tr>
    <tr>
      <th>2015-03-30</th>
      <td> 124.05</td>
      <td> 126.40</td>
      <td> 124.00</td>
      <td> 126.37</td>
      <td> 47099670</td>
    </tr>
    <tr>
      <th>2015-03-31</th>
      <td> 126.09</td>
      <td> 126.49</td>
      <td> 124.36</td>
      <td> 124.43</td>
      <td> 42090553</td>
    </tr>
    <tr>
      <th>2015-04-01</th>
      <td> 124.82</td>
      <td> 125.12</td>
      <td> 123.10</td>
      <td> 124.25</td>
      <td> 40621437</td>
    </tr>
  </tbody>
</table>
</div>




```python
aapl['cont_ret'] = log(aapl['Close']/aapl['Close'].shift(1))
aapl.head()
```




<div style="max-height:1000px;max-width:1500px;overflow:auto;">
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>Open</th>
      <th>High</th>
      <th>Low</th>
      <th>Close</th>
      <th>Volume</th>
      <th>cont_ret</th>
    </tr>
    <tr>
      <th>﻿Date</th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>2014-04-02</th>
      <td> 77.48</td>
      <td> 77.64</td>
      <td> 77.18</td>
      <td> 77.51</td>
      <td> 45104871</td>
      <td>      NaN</td>
    </tr>
    <tr>
      <th>2014-04-03</th>
      <td> 77.34</td>
      <td> 77.50</td>
      <td> 76.81</td>
      <td> 76.97</td>
      <td> 40648111</td>
      <td>-0.006991</td>
    </tr>
    <tr>
      <th>2014-04-04</th>
      <td> 77.12</td>
      <td> 77.14</td>
      <td> 75.80</td>
      <td> 75.97</td>
      <td> 68812485</td>
      <td>-0.013077</td>
    </tr>
    <tr>
      <th>2014-04-07</th>
      <td> 75.43</td>
      <td> 75.84</td>
      <td> 74.56</td>
      <td> 74.78</td>
      <td> 72462530</td>
      <td>-0.015788</td>
    </tr>
    <tr>
      <th>2014-04-08</th>
      <td> 75.03</td>
      <td> 75.16</td>
      <td> 74.10</td>
      <td> 74.78</td>
      <td> 60971883</td>
      <td> 0.000000</td>
    </tr>
  </tbody>
</table>
</div>




```python
aapl['Vols'] = pd.rolling_std(aapl['cont_ret'],window=5)*sqrt(252.0)
aapl.tail()
```




<div style="max-height:1000px;max-width:1500px;overflow:auto;">
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>Open</th>
      <th>High</th>
      <th>Low</th>
      <th>Close</th>
      <th>Volume</th>
      <th>cont_ret</th>
      <th>Vols</th>
    </tr>
    <tr>
      <th>﻿Date</th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>2015-03-26</th>
      <td> 122.76</td>
      <td> 124.88</td>
      <td> 122.60</td>
      <td> 124.24</td>
      <td> 47572869</td>
      <td> 0.006946</td>
      <td> 0.237901</td>
    </tr>
    <tr>
      <th>2015-03-27</th>
      <td> 124.57</td>
      <td> 124.70</td>
      <td> 122.91</td>
      <td> 123.25</td>
      <td> 39546151</td>
      <td>-0.008000</td>
      <td> 0.230939</td>
    </tr>
    <tr>
      <th>2015-03-30</th>
      <td> 124.05</td>
      <td> 126.40</td>
      <td> 124.00</td>
      <td> 126.37</td>
      <td> 47099670</td>
      <td> 0.024999</td>
      <td> 0.301835</td>
    </tr>
    <tr>
      <th>2015-03-31</th>
      <td> 126.09</td>
      <td> 126.49</td>
      <td> 124.36</td>
      <td> 124.43</td>
      <td> 42090553</td>
      <td>-0.015471</td>
      <td> 0.318742</td>
    </tr>
    <tr>
      <th>2015-04-01</th>
      <td> 124.82</td>
      <td> 125.12</td>
      <td> 123.10</td>
      <td> 124.25</td>
      <td> 40621437</td>
      <td>-0.001448</td>
      <td> 0.247050</td>
    </tr>
  </tbody>
</table>
</div>




```python
aapl.head(10)
```




<div style="max-height:1000px;max-width:1500px;overflow:auto;">
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>Open</th>
      <th>High</th>
      <th>Low</th>
      <th>Close</th>
      <th>Volume</th>
      <th>cont_ret</th>
      <th>Vols</th>
    </tr>
    <tr>
      <th>﻿Date</th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>2014-04-02</th>
      <td> 77.48</td>
      <td> 77.64</td>
      <td> 77.18</td>
      <td> 77.51</td>
      <td> 45104871</td>
      <td>      NaN</td>
      <td>      NaN</td>
    </tr>
    <tr>
      <th>2014-04-03</th>
      <td> 77.34</td>
      <td> 77.50</td>
      <td> 76.81</td>
      <td> 76.97</td>
      <td> 40648111</td>
      <td>-0.006991</td>
      <td>      NaN</td>
    </tr>
    <tr>
      <th>2014-04-04</th>
      <td> 77.12</td>
      <td> 77.14</td>
      <td> 75.80</td>
      <td> 75.97</td>
      <td> 68812485</td>
      <td>-0.013077</td>
      <td>      NaN</td>
    </tr>
    <tr>
      <th>2014-04-07</th>
      <td> 75.43</td>
      <td> 75.84</td>
      <td> 74.56</td>
      <td> 74.78</td>
      <td> 72462530</td>
      <td>-0.015788</td>
      <td>      NaN</td>
    </tr>
    <tr>
      <th>2014-04-08</th>
      <td> 75.03</td>
      <td> 75.16</td>
      <td> 74.10</td>
      <td> 74.78</td>
      <td> 60971883</td>
      <td> 0.000000</td>
      <td>      NaN</td>
    </tr>
    <tr>
      <th>2014-04-09</th>
      <td> 74.66</td>
      <td> 75.78</td>
      <td> 74.57</td>
      <td> 75.76</td>
      <td> 51542722</td>
      <td> 0.013020</td>
      <td> 0.183481</td>
    </tr>
    <tr>
      <th>2014-04-10</th>
      <td> 75.81</td>
      <td> 76.03</td>
      <td> 74.74</td>
      <td> 74.78</td>
      <td> 59912818</td>
      <td>-0.013020</td>
      <td> 0.193232</td>
    </tr>
    <tr>
      <th>2014-04-11</th>
      <td> 74.14</td>
      <td> 74.69</td>
      <td> 73.88</td>
      <td> 74.23</td>
      <td> 67975012</td>
      <td>-0.007382</td>
      <td> 0.183662</td>
    </tr>
    <tr>
      <th>2014-04-14</th>
      <td> 74.56</td>
      <td> 74.59</td>
      <td> 73.89</td>
      <td> 74.53</td>
      <td> 51445177</td>
      <td> 0.004033</td>
      <td> 0.160238</td>
    </tr>
    <tr>
      <th>2014-04-15</th>
      <td> 74.32</td>
      <td> 74.52</td>
      <td> 73.05</td>
      <td> 73.99</td>
      <td> 66622577</td>
      <td>-0.007272</td>
      <td> 0.166516</td>
    </tr>
  </tbody>
</table>
</div>




```python
aapl[['Close','Vols']].plot(subplots=True,color='blue',figsize=(8,6))
```

### DIVERSIFICATION

DIVERSIFICATION OF A PORTFOLIO

It is useful to examine the power of using vector algebra with an application. Here we use vector and summation math to understand how diversification in stock portfolios works. Diversification occurs when we increase the number of non-perfectly correlated stocks in a portfolio, thereby reducing portfolio variance. In order to compute the variance of the portfolio we need to use the portfolio weights ${\bf w}$ and the covariance matrix of stock returns ${\bf R}$, denoted ${\bf \Sigma}$. We first write down the formula for a portfolio's return variance: 

\begin{equation}
Var(\boldsymbol{w'R}) = \boldsymbol{w'\Sigma w} = \sum_{i=1}^n \boldsymbol{w_i^2 \sigma_i^2}
+ \sum_{i=1}^n \sum_{j=1,i \neq j}^n \boldsymbol{w_i w_j \sigma_{ij}}
\end{equation}

Readers are strongly encouraged to implement this by hand for $n=2$ to convince themselves that the vector form of the expression for variance $\boldsymbol{w'\Sigma w}$ is the same thing as the long form on the right-hand side of the equation above. 
If returns are independent, then the formula collapses to: 

\begin{equation}
Var(\bf{w'R}) = \bf{w'\Sigma w} = \sum_{i=1}^n \boldsymbol{w_i^2 \sigma_i^2}
\end{equation}

If returns are dependent, and equal amounts are invested in
each asset ($w_i=1/n,\;\;\forall i$):

\begin{eqnarray*}
Var(\bf{w'R}) &=&  \frac{1}{n}\sum_{i=1}^n \frac{\sigma_i^2}{n}
+ \frac{n-1}{n}\sum_{i=1}^n \sum_{j=1,i \neq j}^n  \frac{\sigma_{ij}}{n(n-1)}\\
&=& \frac{1}{n} \bar{\sigma_i}^2 + \frac{n-1}{n} \bar{\sigma_{ij}}\\
&=& \frac{1}{n} \bar{\sigma_i}^2 + \left(1 - \frac{1}{n} \right) \bar{\sigma_{ij}}
\end{eqnarray*}

The first term is the average variance, denoted $\bar{\sigma_1}^2$ divided by $n$, and the second is the average covariance, denoted $\bar{\sigma_{ij}}$ multiplied by factor $(n-1)/n$. As $n \rightarrow \infty$,

\begin{equation}
Var({\bf w'R}) = \bar{\sigma_{ij}}
\end{equation}

This produces the remarkable result that in a well diversified portfolio, the variances of each stock's return does not matter at all for portfolio risk! Further the risk of the portfolio, i.e., its variance, is nothing but the average of off-diagonal terms in the covariance matrix. 


```python
sd=0.20; cv=0.01; m=100
n = range(1,m+1) 
sd_p = zeros(m)
for j in n:
    cv_mat = matrix(ones((j,j))*cv)
    fill_diagonal(cv_mat,sd**2)
    w = matrix(ones(j)*(1.0/j)).T
    sd_p[j-1] = sqrt((w.T).dot(cv_mat).dot(w))
```


```python
plot(n,sd_p)
xlabel('#stocks')
ylabel('stddev of portfolio')
grid()
```

### Arithmetic vs Geometric mean returns

The geometric mean is a good indicator of past performance, especially when we are interested in holding period returns. But if we are interested in expected future returns, the arithmetic mean is the relevant statistic.

Suppose a stock will rise by 30% or fall by 20% with equal probability. If it did one each in consecutive years then the geometric mean return is:


```python
g_ret = ((1+0.30)*(1-0.20))**0.5-1
print "Geometric mean return = ", g_ret
```


```python
a_ret = 0.5*(0.30+(-0.20))
print "Arithmetic mean return per year = ",a_ret
```

Which one is more realistic in predicting future expected returns over the next two years? Note that there are 4 cases to consider for outcomes, all with equal probability $1/4$. 


```python
ret = zeros(4)
ret[0] = (1+0.3)*(1+0.3)
ret[1] = (1+0.3)*(1-0.2)
ret[2] = (1-0.2)*(1+0.3)
ret[3] = (1-0.2)*(1-0.2)
two_year_return = 0.25*sum(ret)
print "Expected two year return = ", two_year_return
print "Expected two year return (annualized) = ", two_year_return**0.5
```


```python

```

fce2f0b464dbe4986c10110a555ead03348cf2f0
ipynb
Jupyter Notebook
Code/Lab/GetStarted_Python_in_Finance.ipynb
beeway/Data_Bootcamp
abc2486060672e19f5e4a71342cb8ca05155db83
MIT
2015-01-14T22:51:39.000Z
2021-01-31T17:23:58.000Z
Code/Lab/GetStarted_Python_in_Finance.ipynb
beeway/Data_Bootcamp
abc2486060672e19f5e4a71342cb8ca05155db83
MIT
2015-03-18T20:24:40.000Z
2016-05-06T13:44:33.000Z
Code/Lab/GetStarted_Python_in_Finance.ipynb
beeway/Data_Bootcamp
abc2486060672e19f5e4a71342cb8ca05155db83
MIT
2015-03-24T00:05:50.000Z
2021-05-12T15:15:32.000Z
Qwen/Qwen-72B
1. YES
2. YES