I have a dataframe of economic series whose values can get revised every month, adding a new value for a given date and indexing it by realtime_start (see below dataframe). realtime_start indicates the date at which value for date becomes valid. This value expires as soon as another one takes its place.
| date | realtime_start | value |
|---|---|---|
| 2020-11-01 | 2020-12-04 | 142629.0 |
| 2020-11-01 | 2021-01-08 | 142764.0 |
| 2020-11-01 | 2021-02-05 | 142809.0 |
| 2020-12-01 | 2021-01-08 | 142624.0 |
| 2020-12-01 | 2021-02-05 | 142582.0 |
| 2020-12-01 | 2021-03-05 | 142503.0 |
| 2021-01-01 | 2021-02-05 | 142631.0 |
| 2021-01-01 | 2021-03-05 | 142669.0 |
| 2021-01-01 | 2021-04-02 | 142736.0 |
| 2021-02-01 | 2021-03-05 | 143048.0 |
| 2021-02-01 | 2021-04-02 | 143204.0 |
| 2021-03-01 | 2021-04-02 | 144120.0 |
I would like an easy way to calculate the month-over-month change in value based on the last known entry at date.
Calculation method: take the first release from month n (based on realtime_start) and subtract the relevant release from month n-1. Relevant release is the most recent release whose realtime_start date does not exceed that of month n.
See desired output below
| date | MoM change |
|---|---|
| 2020-11-01 | NaN |
| 2020-12-01 | -140 |
| 2021-01-01 | 49 |
| 2021-02-01 | 379 |
| 2021-03-01 | 916 |
For 2021-03-01, the MoM change value is 144120.0 - 143204.0 = 916.0
For 2021-02-01, the MoM change value is 143048.0 - 142669.0 = 379.0
For 2021-01-01, the MoM change value is 142631.0 - 142582.0 = 49.0
Similarly, I would like to calculate the year-over-year change based on the last known values at date (actual data frame extends further into the past). I would also like to calculate the 3-month (rolling) average of month-over-month change based on last known values at date.