Python Forum
Pandas dataframe: sum of exponentially weighted correlation matrices per row
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pandas dataframe: sum of exponentially weighted correlation matrices per row
#1
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.
Reply
#2
1
2
3
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question [Solved] Formatting cells of a pandas dataframe into an OpenDocument ods spreadsheet Calab 1 729 Mar-01-2025, 04:51 AM
Last Post: Calab
  Find duplicates in a pandas dataframe list column on other rows Calab 2 2,260 Sep-18-2024, 07:38 PM
Last Post: Calab
  Find strings by index from a list of indexes in a different Pandas dataframe column Calab 3 1,661 Aug-26-2024, 04:52 PM
Last Post: Calab
  Add NER output to pandas dataframe dg3000 0 1,174 Apr-22-2024, 08:14 PM
Last Post: dg3000
  HTML Decoder pandas dataframe column mbrown009 3 2,727 Sep-29-2023, 05:56 PM
Last Post: deanhystad
  Use pandas to obtain cartesian product between a dataframe of int and equations? haihal 0 2,040 Jan-06-2023, 10:53 PM
Last Post: haihal
  Different Correlation Coefficents with different Time Ranges giaco__mar 0 1,393 Sep-28-2022, 02:03 PM
Last Post: giaco__mar
  Pandas Dataframe Filtering based on rows mvdlm 0 2,094 Apr-02-2022, 06:39 PM
Last Post: mvdlm
  Pandas dataframe: calculate metrics by year mcva 1 3,456 Mar-02-2022, 08:22 AM
Last Post: mcva
  Pandas dataframe comparing anto5 0 1,936 Jan-30-2022, 10:21 AM
Last Post: anto5

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020