Skip to content

Instantly share code, notes, and snippets.

@schalekamp
Created June 2, 2015 10:20
Show Gist options
  • Save schalekamp/1439b03c1a4957aee557 to your computer and use it in GitHub Desktop.
Save schalekamp/1439b03c1a4957aee557 to your computer and use it in GitHub Desktop.
simple factor analysis using python/pandas
# basic factor analysis
# http://blog.alphaarchitect.com/2015/05/28/basic-factor-analysis-simple-tools-to-understand-what-drives-performance/
import pandas as pd
import pandas.io.data as web
import datetime, re, copy
import numpy as np
import statsmodels.formula.api as sm
start = datetime.date(2000,1,1)
# get fund/etf data from yahoo (, compress to monthly or use daily factor data)
df = web.get_data_yahoo("FVDFX",start=start)
adj_close = df['Adj Close']
rets = adj_close.pct_change()*100
rets = pd.DataFrame(rets)
rets.rename( columns={"Adj Close":"fund"}, inplace=True)
# get fama/french factor data
# http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/data_library.html
three_fac = web.DataReader("F-F_Research_Data_Factors_daily", "famafrench")
f = copy.copy(three_fac[0])
f.rename( columns={c:re.sub(r'[0-9\-\s]','',c) for c in f.columns}, inplace=True)
#five_fac = web.DataReader("F-F_Research_Data_5_Factors_2x3", "famafrench")
f.index=[pd.datetime(i/10000,(i % 10000 - (i % 100))/100,i % 100) for i in f.index]
# merge dataframes
m = pd.merge(rets,f,left_index=True,right_index=True)
# excess returns
m['fund_e'] = m.fund-m.RF
# ols regression
result = sm.ols( formula = "fund_e ~ MktRF + SMB + HML", data=m).fit()
# intercept = alpha
print result.params
print result.summary()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment