In [1]:
%pylab inline
Numpy is automatically imported to the top level namespace when you use %pylab
Numpy's main workhorse are is the "array" data structure. You can think of arrays as vectors or matrices.
In [2]:
test = array(range(10))
print(test)
Don't be confused by the fact that they output as python lists! It's more like Matlab. The main differences we care about are:
In [4]:
# example of elementwise operations
print(test + 1)
print(test * 2)
print(sqrt(test))
In [5]:
"""Load all the data!"""
import pandas as pd
import pandas.io.data as web
from datetime import datetime
tickers = ["AAPL", "GOOG", "MSFT"]
stocks = pd.DataFrame({'AAPL': web.DataReader('AAPL', "yahoo", datetime(1970,1,1),
datetime(2013,10,1))['Adj Close'].resample('M', how='last')})
print(stocks)
# load everything into one nice dataframe
for stock in tickers:
stocks[stock] = np.NaN
temp = web.DataReader(stock, "yahoo", datetime(1970,1,1),
datetime(2013,10,1))['Adj Close'].resample('M', how='last')
stocks[stock] = temp
# put things into percent return
for stock in tickers:
stocks[stock] = stocks[stock]/stocks[stock].shift(1) - 1.
# visualize!
stocks.cumsum().plot()
show()
In [6]:
# load things into numpy data types
V = matrix(stocks.cov().values) # covariance matrix
R = matrix(stocks.mean().values).T # expected returns
ones_col = np.ones((3,1))
ones_row = ones_col.T
print(V)
print()
print(R)
In [8]:
# And now let's calculte the optimum portfolio!
# The optimum portfolio is just the inversion of the covariance matrix times the expected returns
# and then "normalized" so that the weights sum to 1
optimum_weights = multiply(((ones_col.T * V.I * R).I), (V.I * R) )
print(optimum_weights)
print(tickers)
In [12]:
from scipy.stats import norm # import a normal distribution
from scipy.stats import chi2 # import a chi square distribution
normal_rvs = norm(0, 1).rvs(1000)
chi_rvs = chi2(10).rvs(1000)
hist(chi_rvs, bins=20)
show()
Out[12]: