Search code examples
pythonnumpyweighted-average

Calculating a mean with more recent observations with greater importance


I am building an algorithm to predict the outcomes of sporting events using the performance of previous games. For example, I might have two lists that look like this:

# list of game numbers
game_number = [1, 2, 3, 4, 5, 6, 7] 

# list of points scored   
points_scored = [100, 106, 99, 106, 89, 94, 113]

I can easily calculate a mean using:

# calculate mean
mean_points_scored = np.mean(points_scored)

However, I want the more recent games to be weighted more heavily in the calculation of the mean. Does anyone have experience doing this?


Solution

  • You can do weighted averages with np.average

    mean_points_scored = np.average(points_scored, weights=game_number)