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
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
  Add NER output to pandas dataframe dg3000 0 80 Apr-22-2024, 08:14 PM
Last Post: dg3000
  HTML Decoder pandas dataframe column mbrown009 3 1,053 Sep-29-2023, 05:56 PM
Last Post: deanhystad
  Use pandas to obtain cartesian product between a dataframe of int and equations? haihal 0 1,127 Jan-06-2023, 10:53 PM
Last Post: haihal
  Different Correlation Coefficents with different Time Ranges giaco__mar 0 855 Sep-28-2022, 02:03 PM
Last Post: giaco__mar
  Pandas Dataframe Filtering based on rows mvdlm 0 1,445 Apr-02-2022, 06:39 PM
Last Post: mvdlm
  Pandas dataframe: calculate metrics by year mcva 1 2,326 Mar-02-2022, 08:22 AM
Last Post: mcva
  Pandas dataframe comparing anto5 0 1,271 Jan-30-2022, 10:21 AM
Last Post: anto5
  PANDAS: DataFrame | Replace and others questions moduki1 2 1,808 Jan-10-2022, 07:19 PM
Last Post: moduki1
  PANDAS: DataFrame | Saving the wrong value moduki1 0 1,558 Jan-10-2022, 04:42 PM
Last Post: moduki1
  update values in one dataframe based on another dataframe - Pandas iliasb 2 9,308 Aug-14-2021, 12:38 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

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