How to get a weighted average with different sample sizes

155 Views Asked by At

I have some data on a cyclists distance traveled and hours it took.

I want to get a weighted average of MPH, where more recent samples have a higher weight. The weights in the table use an exponential time decay to get the appropriate weight 0.99 ^ DAYS_AGO.

For example row 2 happened 2 days ago so I would like the MPH from row 2 to have a higher weight than the MPH from row 0. The issue I am stuck on is this becomes a weighted average of a weighted average since the simple weighted average would be sum(MILES) / sum(HOURS) but then I also want to add my time decay weighting to this.

    MILES  HOURS MPH    DAYS_AGO WEIGHT
0   2      5     0.4    10        0.317
1   5      10    0.5    4         0.337
2   14     20    0.7    2         0.344