Weighted Averages
An ordinary mean treats every value as equally important. A weighted average lets some values count more than others: a final exam that is worth more than a quiz, a product rating backed by 2,000 reviews versus one backed by 3, or a server that handles ten times the traffic of another. Getting weights right (or forgetting them) is behind many real-world reporting bugs.
The formula
Multiply each value by its weight, add those products, and divide by the total of the weights:
An ordinary mean is the special case where every weight is 1.
Example: a course grade
| Component | Score | Weight |
|---|---|---|
| Homework | 92 | 20% |
| Midterm | 74 | 30% |
| Final exam | 85 | 50% |
The weights already add up to 100% (1.0), so there is nothing to divide by. The plain mean of the three scores would be , which overstates the grade because it gives the homework the same say as the final.
scores = [92, 74, 85]
weights = [0.20, 0.30, 0.50]
weighted = sum(w * x for w, x in zip(weights, scores)) / sum(weights)
plain = sum(scores) / len(scores)
print(f"weighted: {weighted:.2f} plain mean: {plain:.2f}")
import numpy as np
print("np.average:", round(float(np.average(scores, weights=weights)), 2))weighted: 83.10 plain mean: 83.67
np.average: 83.1
np.average takes a weights argument; np.mean does not.
Weights that are counts
Often the weights are counts. Three web servers report their average response time:
| Server | Requests | Mean latency |
|---|---|---|
| eu-1 | 9,000 | 40 ms |
| us-1 | 900 | 120 ms |
| ap-1 | 100 | 300 ms |
What is the mean latency over all requests? Averaging the three averages gives , which is badly wrong: eu-1 served 90% of the traffic. The correct overall mean weights each server by its request count:
requests = [9_000, 900, 100]
latency = [40, 120, 300]
avg_of_avgs = sum(latency) / len(latency)
overall = sum(n * m for n, m in zip(requests, latency)) / sum(requests)
print(f"average of averages: {avg_of_avgs:.1f} ms (wrong)")
print(f"weighted by count : {overall:.1f} ms")average of averages: 153.3 ms (wrong)
weighted by count : 49.8 ms
Note: Averaging averages is only correct when every group has the same size. When combining means from groups of different sizes, always weight by the group sizes. The same applies to percentages: a 50% conversion rate on 4 visitors and 10% on 1,000 visitors do not average to 30%.
Medians and percentiles cannot be combined like this at all: the median of several medians is not the overall median. Monitoring systems keep histograms instead so they can merge data correctly.
Ratings with few votes
A store sorts products by average star rating. Product A has one 5-star review; product B has 4.7 stars from 2,000 reviews. Should A rank first? A common fix is a weighted (Bayesian) average that blends each product's ratings with the site-wide average, as if every product started with a few "imaginary" average votes:
def bayes_score(ratings_sum, n, site_mean=3.8, c=20):
return (c * site_mean + ratings_sum) / (c + n)
products = {"A (one 5-star)": (5, 1), "B (2000 votes, 4.7)": (4.7 * 2000, 2000),
"C (40 votes, 4.9)": (4.9 * 40, 40)}
for name, (total, n) in products.items():
print(f"{name:22} plain {total / n:.2f} weighted {bayes_score(total, n):.2f}")A (one 5-star) plain 5.00 weighted 3.86
B (2000 votes, 4.7) plain 4.70 weighted 4.69
C (40 votes, 4.9) plain 4.90 weighted 4.53
With few votes the score stays near the site average; with many votes it approaches the product's own average. Variants of this idea rank movies, apps and forum posts.
In software
- Exponential moving averages smooth a metric (FPS counters, CPU load, loss curves in ML training) by giving recent values more weight: .
- Weighted random choice in games (loot tables) uses weights as relative chances; see weighted random.
- Neural networks compute weighted sums in every neuron; training is the search for good weights (see the perceptron).
- Blending: linear interpolation is a weighted average of two values with weights that sum to 1 (lerp).
Exercises
Exercise 1: Grade
Labs count 25%, the project 35% and the exam 40%. A student scores 88, 70 and 79. What is the final grade?
Answer
78.1.
Solution
Exercise 2: Average price
A shop sells 30 coffees at $3, 50 at $4 and 20 at $6. What is the average price per coffee sold?
Answer
$4.10.
Solution
The plain mean of the three prices, , ignores how many of each were sold.
Exercise 3: Conversion rate
Page A converted 6 of 40 visitors, page B 90 of 1,200. What is the overall conversion rate? Compare with the average of the two rates.
Answer
Overall ; the average of the rates (15% and 7.5%) would wrongly give 11.25%.
Solution
Add the successes and the visitors, then divide. That is the same as weighting each rate by its visitor count.
conv, visits = [6, 90], [40, 1200]
rates = [c / v for c, v in zip(conv, visits)]
print([f"{r:.2%}" for r in rates])
print(f"overall {sum(conv) / sum(visits):.2%} avg of rates {sum(rates) / 2:.2%}")['15.00%', '7.50%']
overall 7.74% avg of rates 11.25%
Exercise 4: Moving average
Smooth the frame times [16, 17, 16, 40, 16, 17] (ms) with an exponential moving average, , starting from the first value. What is the final smoothed value?
Answer
About 19.70 ms.
Solution
Each step: . The 40 ms spike is softened instead of jumping the display.
times = [16, 17, 16, 40, 16, 17]
ema = times[0]
history = []
for t in times[1:]:
ema = 0.25 * t + 0.75 * ema
history.append(round(ema, 2))
print(history)[16.25, 16.19, 22.14, 20.61, 19.7]
Test yourself
Scores 80 (weight 1) and 50 (weight 2). What is the weighted average?
- 65
- 60
- 70
- 55
Region X: 1,000 users with mean session 10 min. Region Y: 10 users with mean 40 min. What is the overall mean session length?
- 25 min
- about 10.3 min
- 40 min
- about 20 min
Which NumPy call computes a weighted average?
- np.mean(x, weights=w)
- np.median(x, w)
- np.average(x, weights=w)
- np.sum(x * w)