-
-
Save analyticd/13bba7b951c3f35373b6b4ba21bf209f to your computer and use it in GitHub Desktop.
simple factor analysis using python/pandas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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