def capm(symbol, index, start_date=None, end_date=None): stock = data.get_hist(symbol, start_date, end_date) bench = data.get_hist(index, start_date, end_date) y = stock.close y = ind.returnize(y) x = stock.ix[bench.index]['close'] x = ind.returnize(x) regr = OLS(y, x) return regr
def capm(symbol, index, start_date=None, end_date=None, visual=False): stock = Market.get_stock(symbol, start_date, end_date) bench = Market.get_stock(index, start_date, end_date) s = stock.prices i = bench.prices y = s['close'] y = ind.returnize(y) x = i.ix[s.index]['close'] x = ind.returnize(x) X = [[a] for a in x.values] regr = linear_model.LinearRegression(fit_intercept=True) regr.fit(X[1:], y[1:]) beta = regr.coef_[0] alpha = regr.intercept_ # if visual: # plt.scatter(x.values, y.values) # plt.plot(X, regr.predict(X), 'r') # plt.show() return x.values, y.values, beta, alpha