First, let's look at the OLS regression by using the pandas.ols
function as follows:
from datetime import datetime import numpy as np import pandas as pd n = 252 np.random.seed(12345) begdate=datetime(2013, 1, 2) dateRange = pd.date_range(begdate, periods=n) x0= pd.DataFrame(np.random.randn(n, 1),columns=['ret'],index=dateRange) y0=pd.Series(np.random.randn(n), index=dateRange) print pd.ols(y=y0, x=x0)
For the Fama-MacBeth regression, we have the following code:
import numpy as np import pandas as pd import statsmodels.api as sm from datetime import datetime # n = 252 np.random.seed(12345) begdate=datetime(2013, 1, 2) dateRange = pd.date_range(begdate, periods=n) def makeDataFrame(): data=pd.DataFrame(np.random.randn(n,7),columns=['A','B','C','D','E',' F','G'], index=dateRange) return data # data = { 'A': makeDataFrame(), 'B': makeDataFrame(), 'C': makeDataFrame() } Y = makeDataFrame() print(pd.fama_macbeth(y=Y,x=data))