Search code examples
pandasdataframenumpygroup-bymultiplication

Multiplication of returns by company increasing in time (BHARs)


I have the following Dataframe, organized in panel data. It contains daily returns of many companies on different days following the IPO date. The day_diff represents the days that have passed since the IPO, and return_1 represents the daily individual returns for that specific day for that specific company, from which I have already added +1. Each company has its own company_tic and I have about 300 companies. My goal is to calculate the first component of the right-hand side of the equation below (so having results for each day_diff and company_tic, always starting at day 0, until the last day of data; e.g. = from day 0 to day 1, then from day 0 to day 2, from 0 to day 3, and so on until my last day, which is day 730). I have tried df.groupby(['company_tic', 'day_diff'])['return_1'].expanding().prod() but it doesn't work. Any alternatives?

Index     day_diff        company_tic   return_1
0           0               xyz          1.8914
1           1               xyz          1.0542
2           2               xyz          1.0016
3           0               abc          1.4398
4           1               abc          1.1023
5           2               abc          1.0233
...      ...                ...          ...
159236 x 3

enter image description here


Solution

  • Not sure to fully get what you want, but you might want to use cumprod instead of expanding().prod().

    Here's what I tried :

    df['return_1_prod'] = df.groupby('company_tic')['return_1'].cumprod()
    

    Output :

       day_diff company_tic  return_1  return_1_prod
    0         0         xyz    1.8914       1.891400
    1         1         xyz    1.0542       1.993914
    2         2         xyz    1.0016       1.997104
    3         0         abc    1.4398       1.439800
    4         1         abc    1.1023       1.587092
    5         2         abc    1.0233       1.624071