I've been racking my brain for hours trying to get this, but just can't seem to.
Example data set:
To clarify the variables: ID (obvious), date is mofd(date variable), month is calendar month number (e.g., Jan = 1, Feb = 2 etc)
In my original data, it is xtset by id and date: (since I have multiple IDs)
I have coded my calculation 'manually' so that it is working, and wanted to know how to do it using a for-loop
The manual calculation:
So here, ret_6month is the cumulative product of the current return multiplied by the 5 before it, minus 1, when we are at June (month_num == 6) each year.
I know in this case it is not so tedious to write manually do it, but I also wanted to calculate the cumulative product for 12, 36, and 72 months, also at month_num==6
How would I go about doing this? I just can't seem to figure it out, every attempt gives me different numbers, yet I feel like it is simple.
Any help is appreciated.
Thanks,
Dave
Edit: also, the resulting values for the above data set (should be, if I haven't made any typos):
For month 329: ret_6month = -.1316437
For month 341: ret_6month = .1019949
Example data set:
Code:
clear input byte id float date byte(month) float(return) 1 324 1 .9642857 1 325 2 .9259259 1 326 3 1.0368 1 327 4 .9607843 1 328 5 .9285714 1 329 6 1.051429 1 330 7 1.021277 1 331 8 1.083333 1 332 9 .9776923 1 333 10 1.02 1 334 11 .9705882 1 335 12 .9664646 1 336 1 1.06383 1 337 2 1.08 1 338 3 .9237037 1 339 4 1.030612 1 340 5 1.019802 1 341 6 .9879612 end
In my original data, it is xtset by id and date: (since I have multiple IDs)
Code:
xtset id date
The manual calculation:
Code:
gen ret_6month = return*L.return*L2.return*L3.return*L4.return*L5.return-1 if month_num == 6
I know in this case it is not so tedious to write manually do it, but I also wanted to calculate the cumulative product for 12, 36, and 72 months, also at month_num==6
How would I go about doing this? I just can't seem to figure it out, every attempt gives me different numbers, yet I feel like it is simple.
Any help is appreciated.
Thanks,
Dave
Edit: also, the resulting values for the above data set (should be, if I haven't made any typos):
For month 329: ret_6month = -.1316437
For month 341: ret_6month = .1019949
Comment