Efficient Python Pandas Stock Beta Calculation on Many Dataframes
<p>I have many (4000+) CSVs of stock data (Date, Open, High, Low, Close) which I import into individual Pandas dataframes to perform analysis.  I am new to python and want to calculate a rolling 12month beta for each stock, I found a post to calculate rolling beta (<a href="http://stackoverflow.com/questions/34802972/python-pandas-calculate-rolling-stock-beta-using-rolling-apply-to-groupby-object">Python pandas calculate rolling stock beta using rolling apply to groupby object in vectorized fashion</a>) however when used in my code below takes over 2.5 hours!  Considering I can run the exact same calculations in SQL tables in under 3 minutes this is too slow.</p>

<p>How can I improve the performance of my below code to match that of SQL?  I understand Pandas/python has that capability. My current method loops over each row which I know slows performance but I am unaware of any aggregate way to perform a rolling window beta calculation on a dataframe.</p>

<p>Note: the first 2 steps of loading the CSVs into individual dataframes and calculating daily returns only takes ~20seconds.  All my CSV dataframes are stored in the dictionary called 'FilesLoaded' with names such as 'XAO'.</p>

<p>Your help would be much appreciated!
Thank you :)</p>

<pre><code>import pandas as pd, numpy as np
import datetime
import ntpath
pd.set_option('precision',10)  #Set the Decimal Point precision to DISPLAY
start_time=datetime.datetime.now()

MarketIndex = 'XAO'
period = 250
MinBetaPeriod = period
# ***********************************************************************************************
# CALC RETURNS 
# ***********************************************************************************************
for File in FilesLoaded:
    FilesLoaded[File]['Return'] = FilesLoaded[File]['Close'].pct_change()
# ***********************************************************************************************
# CALC BETA
# ***********************************************************************************************
def calc_beta(df):
    np_array = df.values
    m = np_array[:,0] # market returns are column zero from numpy array
    s = np_array[:,1] # stock returns are column one from numpy array
    covariance = np.cov(s,m) # Calculate covariance between stock and market
    beta = covariance[0,1]/covariance[1,1]
    return beta

#Build Custom "Rolling_Apply" function
def rolling_apply(df, period, func, min_periods=None):
    if min_periods is None:
        min_periods = period
    result = pd.Series(np.nan, index=df.index)
    for i in range(1, len(df)+1):
        sub_df = df.iloc[max(i-period, 0):i,:]
        if len(sub_df) &gt;= min_periods:  
            idx = sub_df.index[-1]
            result[idx] = func(sub_df)
    return result

#Create empty BETA dataframe with same index as RETURNS dataframe
df_join = pd.DataFrame(index=FilesLoaded[MarketIndex].index)    
df_join['market'] = FilesLoaded[MarketIndex]['Return']
df_join['stock'] = np.nan

for File in FilesLoaded:
    df_join['stock'].update(FilesLoaded[File]['Return'])
    df_join  = df_join.replace(np.inf, np.nan) #get rid of infinite values "inf" (SQL won't take "Inf")
    df_join  = df_join.replace(-np.inf, np.nan)#get rid of infinite values "inf" (SQL won't take "Inf")
    df_join  = df_join.fillna(0) #get rid of the NaNs in the return data
    FilesLoaded[File]['Beta'] = rolling_apply(df_join[['market','stock']], period, calc_beta, min_periods = MinBetaPeriod)

# ***********************************************************************************************
# CLEAN-UP
# ***********************************************************************************************
print('Run-time: {0}'.format(datetime.datetime.now() - start_time))
</code></pre>

<p><strong><em>Generate Random Stock Data</em></strong><br>
20 Years of Monthly Data for 4,000 Stocks</p>

<pre><code>dates = pd.date_range('1995-12-31', periods=480, freq='M', name='Date')
stoks = pd.Index(['s{:04d}'.format(i) for i in range(4000)])
df = pd.DataFrame(np.random.rand(480, 4000), dates, stoks)
</code></pre>

<hr>

<pre><code>df.iloc[:5, :5]
</code></pre>

<p><a href="http://i.stack.imgur.com/DCpxf.png" rel="nofollow"><img src="http://i.stack.imgur.com/DCpxf.png" alt="enter image description here"></a></p>

<hr>

<p><strong><em>Roll Function</em></strong><br>
Returns groupby object ready to apply custom functions<br>
See <a href="http://stackoverflow.com/a/37491779/2336654">Source</a> </p>

<pre><code>def roll(df, w):
    # stack df.values w-times shifted once at each stack
    roll_array = np.dstack([df.values[i:i+w, :] for i in range(len(df.index) - w + 1)]).T
    # roll_array is now a 3-D array and can be read into
    # a pandas panel object
    panel = pd.Panel(roll_array, 
                     items=df.index[w-1:],
                     major_axis=df.columns,
                     minor_axis=pd.Index(range(w), name='roll'))
    # convert to dataframe and pivot + groupby
    # is now ready for any action normally performed
    # on a groupby object
    return panel.to_frame().unstack().T.groupby(level=0)
</code></pre>

<hr>

<p><strong><em>Beta Function</em></strong><br>
Use closed form solution of OLS regression<br>
Assume column 0 is market<br>
See <a href="http://stats.stackexchange.com/a/23132/114499">Source</a></p>

<pre><code>def beta(df):
    # first column is the market
    X = df.values[:, [0]]
    # prepend a column of ones for the intercept
    X = np.concatenate([np.ones_like(X), X], axis=1)
    # matrix algebra
    b = np.linalg.pinv(X.T.dot(X)).dot(X.T).dot(df.values[:, 1:])
    return pd.Series(b[1], df.columns[1:], name='Beta')
</code></pre>

<hr>

<p><strong><em>Demonstration</em></strong></p>

<pre><code>rdf = roll(df, 12)
betas = rdf.apply(beta)
</code></pre>

<hr>

<p><strong><em>Timing</em></strong></p>

<p><a href="http://i.stack.imgur.com/t6lvj.png" rel="nofollow"><img src="http://i.stack.imgur.com/t6lvj.png" alt="enter image description here"></a></p>

<hr>

<p><strong><em>Validation</em></strong><br>
Compare calculations with OP</p>

<pre><code>def calc_beta(df):
    np_array = df.values
    m = np_array[:,0] # market returns are column zero from numpy array
    s = np_array[:,1] # stock returns are column one from numpy array
    covariance = np.cov(s,m) # Calculate covariance between stock and market
    beta = covariance[0,1]/covariance[1,1]
    return beta
</code></pre>

<hr>

<pre><code>print(calc_beta(df.iloc[:12, :2]))

-0.311757542437
</code></pre>

<hr>

<pre><code>print(beta(df.iloc[:12, :2]))

s0001   -0.311758
Name: Beta, dtype: float64
</code></pre>

<hr>

<p><strong><em>Note the first cell</em></strong><br>
Is the same value as validated calculations above</p>

<pre><code>betas = rdf.apply(beta)
betas.iloc[:5, :5]
</code></pre>

<p><a href="http://i.stack.imgur.com/V9RGy.png" rel="nofollow"><img src="http://i.stack.imgur.com/V9RGy.png" alt="enter image description here"></a></p>

<hr>

<p><strong><em>Response to comment</em></strong><br>
Full working example with simulated multiple dataframes</p>

<pre><code>num_sec_dfs = 4000

cols = ['Open', 'High', 'Low', 'Close']
dfs = {'s{:04d}'.format(i): pd.DataFrame(np.random.rand(480, 4), dates, cols) for i in range(num_sec_dfs)}

market = pd.Series(np.random.rand(480), dates, name='Market')

df = pd.concat([market] + [dfs[k].Close.rename(k) for k in dfs.keys()], axis=1).sort_index(1)

betas = roll(df.pct_change().dropna(), 12).apply(beta)

for c, col in betas.iteritems():
    dfs[c]['Beta'] = col

dfs['s0001'].head(20)
</code></pre>

<p><a href="http://i.stack.imgur.com/5RuJ9.png" rel="nofollow"><img src="http://i.stack.imgur.com/5RuJ9.png" alt="enter image description here"></a></p>

python algorithm performance pandas