Skip to content

Instantly share code, notes, and snippets.

@vishalbelsare
Forked from analyticd/factor_analysis.py
Created September 22, 2019 13:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vishalbelsare/d243b886e89c80c0aa0e586323370774 to your computer and use it in GitHub Desktop.
Save vishalbelsare/d243b886e89c80c0aa0e586323370774 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