Sub topic 6
Weighted Averages and Why Ratios Act as Weights
This chapter assumes you can do the following:
- Compute a simple average as total divided by count.
- Simplify and scale ratios.
- Work with fractions and percentages.
- Solve a linear equation in one unknown.
The chapter shows that the average of several groups is a weighted average. It also shows that any ratio describing the group sizes can serve as the weights. This is the idea behind mixtures and alligation in the later chapters.
Why the plain average of averages fails
Suppose Section A has 30 students with an average mark of 60. Section B has 20 students with an average mark of 75. The tempting answer for the combined average is (60 + 75) / 2 = 67.5.
That answer treats both sections as equally important, but Section A has more students. The correct method goes back to the sum-of-values view from the previous chapter:
- Total marks in A = 30 × 60 = 1800.
- Total marks in B = 20 × 75 = 1500.
- Combined total = 3300 over 50 students.
- Combined average = 3300 / 50 = 66.
The result is 66, below 67.5, because the larger group has the lower average and so pulls the result toward itself.
The definition
A weighted average of values x₁, x₂, ..., xₙ with weights w₁, w₂, ..., wₙ is:
weighted average = (w₁x₁ + w₂x₂ + ... + wₙxₙ) / (w₁ + w₂ + ... + wₙ)
Each weight says how much its value counts. When the values are group averages, the weights are the group sizes, because the size tells you how many items sit behind each average. A simple average is the special case where all weights are equal.
The numerator w₁x₁ + w₂x₂ + ... is the total of all the underlying items. The denominator is the total count. So a weighted average is still "total divided by count", with the totals rebuilt from group averages.
Why ratios work as weights
Only the proportions among the weights matter. Multiply every weight by the same non-zero number and the numerator and denominator both scale by that number, so it cancels.
For the sections above, 30 : 20 simplifies to 3 : 2. Using 3 and 2 as weights:
(3 × 60 + 2 × 75) / (3 + 2) = (180 + 150) / 5 = 330 / 5 = 66
You get the same answer with weights 30 and 20, with 3 and 2, or with the fractions 3/5 and 2/5 (60 × 3/5 + 75 × 2/5 = 36 + 30 = 66).
This is why a problem can give you a ratio such as "boys to girls is 3 : 2" and no head count. You never need the actual numbers. The ratio is enough.
from fractions import Fraction
def weighted_average(values, weights):
"""Return sum(w * x) / sum(w) as an exact Fraction."""
if len(values) != len(weights):
raise ValueError("values and weights must have the same length")
total_weight = sum(weights)
if total_weight == 0:
raise ValueError("weights must not sum to zero")
return sum(Fraction(v) * w for v, w in zip(values, weights)) / total_weight
averages = [60, 75]
print(weighted_average(averages, [30, 20])) # 66 (actual head counts)
print(weighted_average(averages, [3, 2])) # 66 (the simplified ratio)
print(weighted_average(averages, [Fraction(3, 5), Fraction(2, 5)])) # 66 (fractions)All three calls print 66. The weights differ in scale but are proportional, so the answer is unchanged.
The shortcut: distance from the lower value
A weighted average always lies between the smallest and largest value. For two groups, it sits at a position fixed by the weights. With low value L, high value H, and weights wL and wH:
mean = L + (wH / (wL + wH)) × (H − L)
For the sections, L = 60, H = 75, and the weights are 3 and 2:
mean = 60 + (2/5) × 15 = 60 + 6 = 66
This form is quick because it needs one fraction and one multiplication. It also tells you that the mean sits closer to the group with the larger weight. Here the weights are 3 for A and 2 for B, so 66 is closer to 60 than to 75.
Three or more groups
The definition handles any number of groups. A course grade is a typical case.
A student scores 70 in a test worth 20% of the grade, 80 in a test worth 30%, and 90 in an exam worth 50%. The weights are 20, 30 and 50, which sum to 100:
(20 × 70 + 30 × 80 + 50 × 90) / 100 = (1400 + 2400 + 4500) / 100 = 83
The percentages sum to 100, so you can read them as fractions: 0.2 × 70 + 0.3 × 80 + 0.5 × 90 = 14 + 24 + 45 = 83. If the weights did not sum to 100, you would still divide by their sum.
Working backwards: finding the weights
Often the combined average is given and the question asks for the ratio of group sizes. This is the reverse direction, and it is the reason alligation exists.
Say the average marks are 68 for boys, 78 for girls, and 72 for the whole class. What is the ratio of boys to girls?
Each group's distance from the combined average acts as a lever. The combined average is 72:
- Boys are 72 − 68 = 4 below it.
- Girls are 78 − 72 = 6 above it.
For the mean to land at 72, the total shortfall from the boys must cancel the total surplus from the girls:
(number of boys) × 4 = (number of girls) × 6
So boys : girls = 6 : 4 = 3 : 2. The ratio of sizes is the inverse of the ratio of distances, which means the group closer to the mean has more weight.
Check with 24 boys and 16 girls (a class of 40): (24 × 68 + 16 × 78) / 40 = (1632 + 1248) / 40 = 2880 / 40 = 72.
def size_ratio(low_avg, high_avg, combined_avg):
"""Return (size of low group, size of high group) as the smallest whole-number ratio."""
from fractions import Fraction
if not low_avg < combined_avg < high_avg:
raise ValueError("combined average must lie strictly between the two group averages")
ratio = Fraction(high_avg - combined_avg, combined_avg - low_avg)
return ratio.numerator, ratio.denominator
print(size_ratio(68, 78, 72)) # (3, 2)The function prints (3, 2): three boys for every two girls. The next chapters generalize this to prices and concentrations.
Edge cases and common traps
Averaging rates
Average speed over two equal distances is not the simple mean of the speeds. Suppose a trip covers 120 km at 60 km/h and another 120 km at 40 km/h. The simple mean says 50 km/h, but the weights are the times, not the distances:
- Time at 60 km/h = 120 / 60 = 2 hours.
- Time at 40 km/h = 120 / 40 = 3 hours.
- Average speed = (60 × 2 + 40 × 3) / (2 + 3) = 240 / 5 = 48 km/h.
distance = 120
speeds = [60, 40]
times = [distance / s for s in speeds] # [2.0, 3.0]
print(2 * distance / sum(times)) # 48.0 (total distance / total time)
print(sum(s * t for s, t in zip(speeds, times)) / sum(times)) # 48.0 (time-weighted mean)Both lines print 48.0, because they are the same calculation. The rule: a speed is weighted by time. If the times are equal, the simple mean is right. If the distances are equal, the weights are the times, which are inversely proportional to the speeds, so the mean comes out lower than the simple mean.
Weights that do not match the values
Do not weight a percentage by a count that belongs to something else. If 40% of a company's employees are women and the company has two offices of different sizes, you cannot average the two office percentages unless you weight by each office's head count.
The same applies to percentage averages in general. An average of percentages is only meaningful when each percentage is weighted by the base it was taken from.
Empty or zero-weight groups
A group with weight zero contributes nothing, so you can drop it. If the weights sum to zero the average is undefined, and the function above raises an error for this case.
Result outside the range
A weighted average with positive weights can never fall below the smallest value or above the largest. If your answer does, you have made an arithmetic or setup error. The check takes a few seconds and catches most mistakes.
Summary table
| Situation | Values | Weights | Result |
|---|---|---|---|
| Two classes combined | Class averages | Number of students | Combined average |
| Course grade | Component scores | Percentage worth | Final score |
| Trip with two speeds | Speeds | Time at each speed | Average speed |
| Given combined mean, find sizes | Group averages | Unknown, found from distances | Ratio of sizes |
Exercises
Exercise
A batch of 12 items has an average weight of 40 g, and a batch of 18 items has an average weight of 30 g. What is the average weight when the two batches are combined?
Show answer
The size ratio is 12 : 18 = 2 : 3. Using the shortcut from the lower value 30: mean = 30 + (2/5) × (40 − 30) = 30 + 4 = 34 g. Check by totals: (12 × 40 + 18 × 30) / 30 = (480 + 540) / 30 = 34.
Exercise
A class has an average mark of 72. The boys average 68 and the girls average 78. If the class has 40 students, how many are boys and how many are girls?
Show answer
The distances from 72 are 4 for the boys and 6 for the girls, so boys : girls = 6 : 4 = 3 : 2. Out of 40 students, boys = 40 × 3/5 = 24 and girls = 40 × 2/5 = 16. Check: (24 × 68 + 16 × 78) / 40 = 2880 / 40 = 72.
Exercise
A car travels from town P to town Q at 30 km/h and returns along the same road at 60 km/h. What is its average speed for the round trip? Explain why the answer is not 45 km/h.
Show answer
Let the one-way distance be 60 km. The outward trip takes 60 / 30 = 2 hours and the return takes 60 / 60 = 1 hour. Total distance is 120 km and total time is 3 hours, so the average speed is 120 / 3 = 40 km/h. The simple mean 45 km/h is wrong because the car spends twice as long at 30 km/h as at 60 km/h. The weights are the times, 2 : 1, and (2 × 30 + 1 × 60) / 3 = 40.