1

I have a dataframe with cumulative stock returns from 1 to 5 days:

         1dReturn  2dReturn  3dReturn  4dReturn  5dReturn
 Ticker                                                  
 LUNA      -3.077    -3.077    -6.923    -6.915    -6.615
 YTEN      -2.139    -2.139   -18.182   -16.043   -16.578

I would like to compute the daily returns. Is there a function for that?

Code below creates the table above:

  df = pd.DataFrame({'1dReturn': [-3.077, -2.139],
                '2dReturn': [-3.077, -2.139],
                '3dReturn': [-6.923, -18.182],
                '4dReturn': [-6.915, -16.043],
                '5dReturn': [-6.615, -16.578],},
                index=['LUNA', 'YTEN'])

The formula to arrive at the daily returns works as follows:

daily returns day 2: cD2/d1
daily returns day 3: cD4/(d1*d2)
daily returns day 4: cD5/(d1*d2*d3)
daily returns day 5: cD5/(d1*d2*d3*d4)

where cD1 is the cum return of day 1 and d1 is the daily return for d1 etc.

Niccola Tartaglia
  • 1,537
  • 2
  • 26
  • 40

1 Answers1

0
np.exp(np.log(cumReturn + 1.0).diff()) - 1

cumReturn is the cumulative return series in Pandas.

R_cum_i = (1 + R_daily_i) * (1 + R_daily_i-1) ... - 1

R_cum_i-1 = (1 + R_daily_i-1) * (1 + R_daily_i-2) ... - 1

so

R_cum_i =(R_cum_i-1 + 1) * (1 + R_daily_i-1) - 1

1 + R_daily_i-1 = (R_cum_i + 1) / (R_cum_i-1 + 1)

1 + R_daily_i-1 = exp(log((R_cum_i + 1) / (R_cum_i-1 + 1)))

1 + R_daily_i-1 = exp(log(R_cum_i + 1) - log(R_cum_i-1 + 1))

1 + R_daily_i-1 = exp(log(R_cum_i + 1).diff())

then

R_daily_i-1 = exp(log(R_cum_i + 1).diff()) - 1

J.D
  • 1,885
  • 4
  • 11
  • 19