← Math
Math

Weighted averages and aggregation

Pages got lighter. Why did the average get heavier?

Separate changes within groups from changes in their share of traffic.

Outcome

Weight cohort means by the right unit, hold a reference mix fixed, and split a change into what happened inside cohorts and what the mix did.

Terminology (6)Terms used in this lesson

Term on the front, definition on the back. Paste with tabs between fields and a new line between cards, or import the download in Anki as Basic.

Weighted mean
A mean where each part counts in proportion to its size: Σ count × mean ÷ Σ count.
Weighting unit
What the average is per: page view, user, request or device class. It decides the weights.
Mix
The share of the total each cohort contributes, such as 70% desktop and 30% mobile views.
Standardized mean
Cohort means combined with a fixed reference mix, so mix changes cannot move it.
Mix effect
The part of a change in an average caused by shifting shares between cohorts.
Within-cohort effect
The part of a change caused by the cohorts’ own means changing, measured on the reference mix.

Why this matters

Dashboards aggregate: average latency over regions, conversion over campaigns, page weight over devices. Every aggregate has a weighting unit, and when the mix of cohorts shifts, the aggregate can move against every cohort. A team that reads the aggregate alone may roll back a change that worked or ship one that did not.

This lesson assumes the reweighting from Sampling and selection bias.

What gives an average its weight

A weighted mean gives each group influence in proportion to a chosen unit. For a mean per page view, multiply each device group’s mean kilobytes by its view count, add those totals and divide by all views. Giving every group equal weight answers a different question unless the counts are equal.

The reweighting from Sampling and selection bias also helps compare periods. Hold a reference mix fixed while changing the group means to isolate the within-group part of an aggregate change; the remaining part reflects the changed mix. State which period supplies the reference. This arithmetic separates contributions under that choice without proving what caused the changes inside groups.

01Name the weighting unit

Name the weighting unit

After a redesign, desktop pages dropped from 900 KB to 850 KB and mobile pages from 1,500 KB to 1,400 KB. The dashboard’s average page weight rose from 1,140 KB to 1,235 KB. The pages did their part; the traffic moved to mobile.

“Average page weight” here means per page view, so each device class counts in proportion to its views. Predict the three lines this program prints.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
// [page views, mean KB] for desktop and mobile, before and after a redesign
const before: [number, number][] = [
  [6000, 900],
  [4000, 1500]
];
const after: [number, number][] = [
  [3000, 850],
  [7000, 1400]
];

const mean = (cohorts: [number, number][]) =>
  cohorts.reduce((sum, [n, m]) => sum + n * m, 0) /
  cohorts.reduce((sum, [n]) => sum + n, 0);

console.log(mean(before).toFixed(2));
console.log(mean(after).toFixed(2));
console.log(
  mean([
    [6000, 850],
    [4000, 1400]
  ]).toFixed(2)
); // after means, before mix
Why is the first line 1,140 and not 1,200?

reduce over [views, mean] tuples gives both sums. Integer products and their sum are exact only while each intermediate integer is at most 2⁵³; fractional means generally use approximate binary64 arithmetic.

02What the equal average answers

What the equal average answers

The plain average of the two device means, (900 + 1,500) / 2 = 1,200 KB, gives each device class one vote. It answers “how heavy is a typical device class’s page?”, which is a legitimate question for a design review and the wrong one for a bandwidth bill. Name the unit next to any average.

03Hold a reference mix

Hold a reference mix

Average page weight, KB
Means fromMix fromAverage
BeforeBefore (60/40)1,140
AfterAfter (30/70)1,235
AfterBefore (60/40)1,070

The third row keeps the before mix and swaps in the after means. Only changes in device-class means can move it. This removes the device-mix effect, but other changes inside each class could still affect page weight. Attributing the whole within part to the redesign requires a controlled comparison.

04Mix versus within

Mix versus within

The total change is 1,235 − 1,140 = +95 KB. On the fixed mix the pages changed by 1,070 − 1,140 = −70 KB. The rest, 1,235 − 1,070 = +165 KB, comes from traffic moving to mobile. Algebraically, (standardized − before) + (after − standardized) = after − before: the intermediate mean cancels. Separately rounded displayed parts can differ from the displayed total by 0.01 KB.

Should the team roll back the redesign?

05When the mean has no value

When the mean has no value

06Explain the page weight change

Explain the page weight change

The report must answer three different requests. A combines loaded kilobytes per view; S asks whether the observed change survives a fixed device mix; D must explain the difference without inventing an after mean for a vanished cohort. A repair that makes the parts add up can still assign the traffic shift to the wrong part.

In the frontend A change tile with its parts in Next.js and Svelte Optional

A dashboard tile shows the change, its within and mix parts and, in the caption, the reference mix, and says when a device class has no views to compare.

Estimate.tsx
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
type Parts = { total: number; within: number; mix: number } | null;

// Show the change and where it came from, and name the reference mix in the caption.
export function PageWeightChange({
  parts,
  reference
}: {
  parts: Parts;
  reference: string;
}) {
  if (parts === null)
    return (
      <p>
        Comparison unavailable: no reference views, no after views, or a reference
        cohort has no after mean.
      </p>
    );

  const kb = (x: number) => `${x >= 0 ? "+" : ""}${x.toFixed(0)} KB`;

  return (
    <figure>
      <p>Average page weight {kb(parts.total)}</p>
      <ul>
        <li>Within device classes: {kb(parts.within)}</li>
        <li>Shift in device mix: {kb(parts.mix)}</li>
      </ul>
      <figcaption>
        Within-device change measured on the {reference} device mix.
      </figcaption>
    </figure>
  );
}

Check this idea later
Each region reports its average request latency. What does the plain average of the regions’ averages answer?
Every cohort’s conversion rate rose, yet the overall rate fell. What happened?
Sources

Values above were computed in Node 22 and run in Go 1.27, Rust 1.99 and Apple clang 17. The page weights are a worked model.

Report a problem in this lesson

For typos, mistakes and things that don’t work. We read every report and fix what’s wrong, but we don’t reply here, so a question won’t get an answer.