← Math
Math

Sampling and selection bias

Did the crash rate really go up?

Check whose observations a measured rate can represent.

Outcome

Name the population and the sample, estimate a population session rate from stratum rates, and compare two windows on one stated mix.

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.

Population
Everyone a figure is claimed for, such as all active users.
Sample
The part that was actually measured, such as sessions from users who opted into telemetry.
Selection bias
A difference between sample and population caused by how the sample was collected.
Stratum
A group measured separately, such as a platform, region or plan.
Reweighting Also post-stratification
Combining stratum rates with population shares instead of sample shares.
Simpson’s paradox
A pooled rate moving the opposite way from every stratum’s rate because the mix changed.

Why this matters

Telemetry comes from users who opt in, logs from requests that reached a server, benchmarks from machines someone chose, and surveys from people who answered. Each collects some groups more than others. A rate computed on such a sample describes the sample; claiming it for everyone needs the population’s mix, and comparing two periods needs the same mix in both.

This lesson assumes fractions and weighted sums from earlier lessons.

From a sample to a population

The population is the set of cases you want to describe; the sample is the subset you observed. Selection bias arises when inclusion favors cases whose outcomes differ from those left out. A larger sample can still have the wrong composition.

A stratum is a group, such as a platform, whose rate you estimate separately. Using the weighted sums from earlier lessons, combine stratum rates with the target population’s shares. For a per-session rate those must be session shares; user shares suffice only under equal session frequency. This requires representative observations within each stratum and evidence for every stratum with positive weight. A fixed mix also makes comparisons across windows answer the same population question.

01Rate of the sample

Rate of the sample

The crash dashboard says 4.0% last month and 6.1% this month. Every platform crashed less this month. The dashboard divides crashes by opt-in sessions, and an opt-in campaign on mobile, the platform that crashes most, changed who sends telemetry.

The app team needs to decide whether to investigate a crash-rate increase. Last month, opt-in telemetry held 8,000 desktop sessions with 240 crashes and 2,000 mobile sessions with 160. Desktop users are 40% of all users and mobile users 60%. Count a session as crashed at most once. Assume equal average sessions per user across platforms, so population user shares also describe population session shares. Predict the three lines this program prints.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
// Opt-in crash telemetry: desktop is 40% of users, mobile 60%.
const desktop = { share: 0.4, sessions: 8000, crashes: 240 };
const mobile = { share: 0.6, sessions: 2000, crashes: 160 };

const rate = (p: { sessions: number; crashes: number }) => p.crashes / p.sessions;

console.log(
  ((desktop.crashes + mobile.crashes) / (desktop.sessions + mobile.sessions)).toFixed(4)
);
console.log(((rate(desktop) + rate(mobile)) / 2).toFixed(4));
console.log((desktop.share * rate(desktop) + mobile.share * rate(mobile)).toFixed(4));
Which line estimates the population’s per-session crash rate under these assumptions?

Counts arrive as numbers, so crashes / sessions is already a fraction. Check for zero sessions yourself: 0 / 0 is NaN.

02Reweight to the population

Reweight to the population

For each platform, assume sampled sessions have the same crash probability as unsampled sessions. Its measured rate then estimates that platform’s rate: 240 / 8,000 = 3% and 160 / 2,000 = 8%. Combining them with population shares gives 0.4 × 3% + 0.6 × 8% = 6.0%. The units are crashed sessions per session. Sample shares instead give 0.8 × 3% + 0.2 × 8% = 4.0%. Reweighting removes this platform-mix difference; it cannot repair selection within a platform. If mobile users start twice as many sessions per user, population session shares must replace user shares for an actual session-rate estimate.

Last month
PlatformShare of usersShare of sampleCrash rate
Desktop40%80%3.0%
Mobile60%20%8.0%

03When the sample supports no figure

When the sample supports no figure

A benchmark suite runs only on idle CI machines. What does its p95 latency describe?

04Mix shifts between windows

Mix shifts between windows

This month, after a mobile opt-in campaign, the telemetry has 2,000 desktop sessions with 50 crashes (2.5%) and 8,000 mobile sessions with 560 (7.0%). Both platforms improved, yet the pooled rate is 610 / 10,000 = 6.1%, up from 4.0%, because the sample moved toward the platform that crashes more.

05Compare on one mix

Compare on one mix

Keep the 40/60 population mix and put both months on it: 6.0% last month, 0.4 × 2.5% + 0.6 × 7.0% = 5.2% this month. The standardized rate improved by 0.8 percentage points, so the pooled increase alone does not justify blaming the release. This calculation isolates the platform mix; it does not establish that the release caused the improvement. Other changes within platforms remain possible. A different fixed mix answers a different population question and can change the conclusion when some platforms improve and others worsen.

Why not compare each month on its own sample mix?

06Report the crash rate

Report the crash rate

Repair the dashboard’s aggregation before using it to compare release windows. First count sampled sessions correctly, then account for the population mix, then hold that mix fixed across both windows. The lab calls a lower standardized rate better; it does not assess a causal claim. Compare computed binary64 rates before display rounding: an absolute represented difference below the binary64 literal 1e-9 is same. Compute each weight share and platform session rate first, multiply them, then add products in input order, rounding each operation to binary64. Mathematical rates 0.1 and 0.100000001 differ by exactly 10⁻⁹, while their represented difference is about 9.9999999947 × 10⁻¹⁰, so this numerical policy calls them same. The cutoff measures neither statistical uncertainty nor release impact; four-decimal display rounding is another separate step.

Opt-in crash telemetry

Repair the sample rate, estimate the population rate, then compare release windows.

  1. easy · M-SAMPLING-V4-1 Rate of the sample
  2. medium · M-SAMPLING-V4-2 Estimate the population rate
  3. hard · M-SAMPLING-V4-3 Compare release windows
In the frontend A reweighted crash tile in Next.js and Svelte Optional

The tile labels its estimate as standardized to user shares, assuming equal session frequency and representative sampling within platforms. Beside each platform it shows the user share and sample session share. The supplied user shares sum to one. A platform with users but no sessions turns the figure into “not enough data”.

Estimate.tsx
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
type Platform = { name: string; userShare: number; sessions: number; crashes: number };

// User shares sum to 1; assume equal session frequency and representative within-platform samples.
export function CrashRateTile({ platforms }: { platforms: Platform[] }) {
  const missing = platforms.filter((p) => p.userShare > 0 && p.sessions === 0);
  const sampled = platforms.reduce((sum, p) => sum + p.sessions, 0);
  const rate = platforms.reduce(
    (sum, p) => sum + (p.sessions ? p.userShare * (p.crashes / p.sessions) : 0),
    0
  );

  return (
    <section>
      <h2>Session crash rate, standardized to users</h2>
      <p>
        {missing.length
          ? `Not enough data: no sessions from ${missing.map((p) => p.name).join(", ")}`
          : `${(rate * 100).toFixed(2)}%`}
      </p>
      <ul>
        {platforms.map((p) => (
          <li key={p.name}>
            {p.name}: {(p.userShare * 100).toFixed(0)}% of users,{" "}
            {sampled ? ((100 * p.sessions) / sampled).toFixed(0) : 0}% of sample
          </li>
        ))}
      </ul>
    </section>
  );
}

Check this idea later
An app store rating averages 4.6 stars from people who chose to review. Does it describe all users?
Crash reports upload after restart, and some crashes corrupt the upload. Can reweighting by platform fix that?
Sources

Values above were computed in Node 22 and run in Go 1.27, Rust 1.99 and Apple clang 17. The telemetry is 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.