# How to create alpha values (linear regression intercepts) matrix for multiple instruments

I am trying to get matrix with alpha (linear regression intercepts) values for all pair of instruments, which will give result in the same manner as df.corr() function returns but with alphas not correlations. I was trying with below code but it is super slow, with 100 columns it takes at least 30 seconds, 150 over a minute to calculate, whereas df.corr needs 2 seconds. Is there any efficient way to do this?

``````data  ={'A' : [0.00631691870684443,  np.nan,  np.nan,  0.0006932409289767081,  0.015301596582667625,   0.003746598388026663,  0.0023769111360482232,  0.008611281579541384,
0.028180217130803077,  0.007977250280644188],
'B' : [0.007, 0.023, 0.002, 0.013, 0.019, 0.02, -0.013, 0.024, -0.018, 0.019],
'C': [0.014, 0.034, -0.009, 0.022, -0.0, -0.002, -0.003, -0.006, -0.023, 0.016],
'D': [-0.009, 0.043, 0.001, -0.03, 0.019, -0.01, 0.036, 0.082, -0.04, 0.009]
}

df = pd.DataFrame(data)

def calculate_alpha(df):
#Y = Alpha + Beta*X
tickers = df.columns.to_list()
create_empty_data_frame = lambda: pd.DataFrame(index=tickers, columns=tickers, dtype=float)
intercepts = create_empty_data_frame() # Alpha
for column in tickers:
for row in tickers:
x = df.dropna(subset=[column,row])[column] #drops NA only for processed pair of instruments
y = df.dropna(subset=[column,row])[row]
result = stats.linregress(x,y)

intercepts.at[row, column] = result.intercept
return intercepts

calculate_alpha(df)
``````

Alpha matrix

Thanks Kasia