TExcelTAD
Home › Basic Math & Pre-Algebra › Weighted Averages

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:

weighted average=w1x1+w2x2+⋯+wnxnw1+w2+⋯+wn=∑wx∑w\text{weighted average} = \frac{w_1 x_1 + w_2 x_2 + \cdots + w_n x_n}{w_1 + w_2 + \cdots + w_n} = \frac{\sum wx}{\sum w}

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%
0.20×92+0.30×74+0.50×85=18.4+22.2+42.5=83.10.20 \times 92 + 0.30 \times 74 + 0.50 \times 85 = 18.4 + 22.2 + 42.5 = 83.1

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 2513≈83.67\frac{251}{3} \approx 83.67, which overstates the grade because it gives the homework the same say as the final.

python · try it yourself
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 40+120+3003≈153.3 ms\frac{40 + 120 + 300}{3} \approx 153.3\ \text{ms}, which is badly wrong: eu-1 served 90% of the traffic. The correct overall mean weights each server by its request count:

python · try it yourself
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:

score=C⋅m+∑ratingsC+nm=site-wide mean rating,C=how many imaginary votes,n=real votes\begin{aligned} &\text{score} = \frac{C \cdot m + \sum \text{ratings}}{C + n} \\ &m = \text{site-wide mean rating},\quad C = \text{how many imaginary votes},\quad n = \text{real votes} \end{aligned}
python · try it yourself
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: new=α⋅latest+(1−α)⋅old\text{new} = \alpha \cdot \text{latest} + (1 - \alpha) \cdot \text{old}.
  • Weighted random choice in games (loot tables) uses weights as relative chances; see weighted random.
  • Neural networks compute weighted sums w1x1+w2x2+⋯+bw_1 x_1 + w_2 x_2 + \cdots + b in every neuron; training is the search for good weights (see the perceptron).
  • Blending: linear interpolation (1−t)⋅a+t⋅b(1 - t) \cdot a + t \cdot b 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
0.25×88+0.35×70+0.40×79=22+24.5+31.6=78.10.25 \times 88 + 0.35 \times 70 + 0.40 \times 79 = 22 + 24.5 + 31.6 = 78.1

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
total revenue=30⋅3+50⋅4+20⋅6=90+200+120=410coffees=30+50+20=100average=410100=4.10\begin{aligned} \text{total revenue} &= 30 \cdot 3 + 50 \cdot 4 + 20 \cdot 6 = 90 + 200 + 120 = 410 \\ \text{coffees} &= 30 + 50 + 20 = 100 \\ \text{average} &= \frac{410}{100} = 4.10 \end{aligned}

The plain mean of the three prices, 3+4+63≈4.33\frac{3 + 4 + 6}{3} \approx 4.33, 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 961240≈7.74%\frac{96}{1240} \approx 7.74\%; 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.

python · try it yourself
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, α=0.25\alpha = 0.25, starting from the first value. What is the final smoothed value?

Answer

About 19.70 ms.

Solution

Each step: ema=0.25⋅latest+0.75⋅ema\text{ema} = 0.25 \cdot \text{latest} + 0.75 \cdot \text{ema}. The 40 ms spike is softened instead of jumping the display.

python · try it yourself
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?

  1. 65
  2. 60
  3. 70
  4. 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?

  1. 25 min
  2. about 10.3 min
  3. 40 min
  4. about 20 min

Which NumPy call computes a weighted average?

  1. np.mean(x, weights=w)
  2. np.median(x, w)
  3. np.average(x, weights=w)
  4. np.sum(x * w)