# Multiple independent variables (returns) on common dependent variables (Fama-French risk factors): Efficiency and data structure in Python

As a common topic in factor investing, I wish to implement the well-known Fama-French regressions on several stocks (+2000 IDs).

In a deep sense, factor regressions tell how the right hand (returns) variable is formed, from expectations of the left hand variable (factors). Since these factors are common across all the securities, it is inefficient to duplicate the factors for each stock at t.

My question is about efficiency and data structure. I have seen many tutorials explaining Fama-French regressions for a given stock return, but so far the regressions on several stocks (or portfolios) remain unclear for me. Maybe I am just missing something.

Let me illustrate my question. I created the following DataFrame.

time id returns Mkt-RF SMB HML
1 1 0.012 0.029 -0.023 -0.035
1 2 -0.020 0.029 -0.023 -0.035
1 2000 -0.039 0.029 -0.023 -0.035
... ...
12 2 -0.007 0.015 -0.056 0.004
12 3 0.027 0.015 -0.056 0.004
12 2000 -0.019 0.015 -0.056 0.004

In this table you can see, that the three factors (Mkt-Rf, SMB, and HML) are constant at given time across all ids. As opposed to the stock-level returns, which depend on the id and t. Since I have a broad universe, I believe there is a faster way to implement the regressions instead of copying the factors each time, creating lots of duplicates.

I am using Python or Stata to run the regressions. Doing simple OLS regressions take a lot of time. Maybe should I use two different datasets? But how.

Any "best" practice or advice would be great.