Python Forum

Full Version: Pandas dataframe: sum of exponentially weighted correlation matrices per row
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Consider the following dataframe:

df = pd.DataFrame(np.random.random((200,3)))
df['date'] = pd.date_range('2000-1-1', periods=200, freq='D')
df = df.set_index(['date'])


date 0 1 2 3 4 5
2000-01-01 0.101782 0.111237 0.177719 0.229994 0.298786 0.747169
2000-01-02 0.348568 0.916997 0.527036 0.998144 0.544261 0.824907
2000-01-03 0.095015 0.480519 0.493345 0.632072 0.965326 0.244732
2000-01-04 0.502706 0.014287 0.045354 0.461621 0.359125 0.489150
2000-01-05 0.559364 0.337121 0.763715 0.460163 0.515309 0.732979
2000-01-06 0.488153 0.149655 0.015616 0.658693 0.864032 0.425497
2000-01-07 0.266161 0.392923 0.606358 0.286874 0.160191 0.573436
2000-01-08 0.786785 0.770826 0.202838 0.259263 0.732071 0.546918
2000-01-09 0.739847 0.886894 0.094900 0.257210 0.264688 0.005631
2000-01-10 0.615846 0.347249 0.516575 0.886096 0.347741 0.259998

Now, I want to treat each row as a vector and perform a multiplication like this:

[[0.101782]] [[0.101782 0.111237 0.177719 0.229994 0.298786 0.747169]]
[[0.111237]]
[[0.177719]]
[[0.229994]]
[[0.298786]]
[[0.747169]]

For the i-th row, let's call this X_i. Now I have a parameter alpha and I want to multiply X_i with alpha^i and sum across all the i's. In the real world, I can have thousands of rows so I need to do this with reasonably good performance.

Can someone please help me out with this? Many thanks in advance for all the help.
alpha = 1.05
alphas = np.power(alpha, np.arange(1, df.shape[0] + 1)).reshape(df.shape[0], 1) # array of alpha^i [alpha^1, ...., alpha^(size+1)]
what_you_want = np.sum(df.values * alphas, axis=0) # sum of all rows