← Math
Math

Probability and expected value

Will tonight’s batch job fail?

Distinguish an average failure count from the chance of any failure in a run.

Outcome

Tell an expected count from a probability, compute the chance of at least one, rate retries by the right event, and see what a shared cause changes.

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.

Probability
A number from 0 to 1 for how often an event happens over many repetitions of the same situation.
Expected value
The long-run average of a quantity: each possible value weighted by its chance. An expected count can be larger than 1.
Complement
The event that something does not happen. P(at least one) = 1 − P(none).
Independence
Events whose chances do not affect each other, so the chance of all of them is the product of their chances.
Linearity of expectation
The expected value of a sum is the sum of the expected values, whether or not the parts are independent.
Correlated failures
Failures with a shared cause, such as an outage, that make many items fail together.

Why this matters

Alert thresholds, retry policies, capacity plans and on-call load all rest on small per-item chances multiplied across many items. Some decisions need the expected count (how many tickets tonight?) and others need the chance of any (will someone be paged?). Mixing them, or assuming independence when a shared cause exists, gives numbers that are wrong by orders of magnitude.

This lesson assumes percentages, powers and the geometric sums of the previous lesson.

Events, counts and expected work

Probability describes the chance of an event and lies between zero and one. Expected value averages a numerical quantity over repeated runs of the model; an expected count can exceed one and need not describe what happens in any single run.

Independence means knowing one outcome does not change the probability of another. It allows the multiplication used to find the chance that every task succeeds. Expected counts can be added without independence, but the probabilities assigned to retries must still reflect shared causes such as an outage lasting the whole run.

01Expected count

Expected count

A nightly job runs 500 tasks, and each fails 0.2% of the time. Someone reports “we expect one failure, so a failure is certain.” The team needs separate estimates for failed-task count, the chance of any failed task and the work retries create. A service outage that lasts the whole run may invalidate the independent-attempt estimate.

With n tasks that each fail with chance p, the expected number of failures is n × p, even without independence. The complement calculation in the next section does require independence. This count is an average over many nights: some nights none fail, some nights two do. Predict the three lines this program prints.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
const tasks = 500;
const p = 0.002; // chance one task fails

console.log((tasks * p).toFixed(4));
console.log((1 - (1 - p) ** tasks).toFixed(4));
console.log(((1 - p) ** tasks).toFixed(4));
Which line is the chance that tonight’s job sees at least one failed task?

02At least one

At least one

“At least one fails” has many ways to happen; “none fails” has one. With independence, P(none) = (1 − p)ⁿ, so P(at least one) = 1 − (1 − p)ⁿ. When n × p is small, the two numbers are close: 50 tasks at 0.002 give an expected 0.1 failures and a 0.0952 chance of any. When n × p reaches 1 or more they separate, and n × p can exceed 1 while a probability cannot.

For a tiny per-item chance, 1 − (1 − q) ** n loses digits because 1 − q rounds. -Math.expm1(n * Math.log1p(-q)) keeps them.

For q = 10⁻¹² and n = 10⁶ the plain form gives 9.99977 × 10⁻⁷ and the log1p form 9.999995 × 10⁻⁷, so the plain one is already wrong in the fifth digit.

03Name the per-task event

Name the per-task event

Each task stops on success, otherwise it uses up to two retries. With independent attempts, a task fails only when all three attempts fail, so its chance is 0.002³ = 8 × 10⁻⁹. The job fails when any of the 500 tasks fails: 1 − (1 − 8 × 10⁻⁹)⁵⁰⁰ ≈ 4.0 × 10⁻⁶.

Someone computes 1 − 0.998¹⁵⁰⁰. What would that measure if all 1,500 attempt slots ran even after a task had succeeded?

04Expected attempts

Expected attempts

The first attempt always runs, the second runs after one failure (chance p), the third after two (chance p²). By linearity of expectation the attempts per task average 1 + p + p² = 1.002004, and the job makes about 501 attempts, not 1,500. Linearity holds even when events are not independent, which makes expected counts easy to add up. The p² reach probability still uses the model’s independent failures while the service is healthy.

05A shared cause

A shared cause

Now suppose the service is down for the whole run with chance s = 0.001. Then every attempt fails, every task uses all three attempts and the job fails. Split the run into the two cases and weight each by its chance.

Job failure chance with 2 retries, 500 tasks, p = 0.002
ModelJob failure chance
Independent attempts only4.0 × 10⁻⁶
Outage s = 0.001, weighted correctly1.004 × 10⁻³
Outage treated as a per-task risk0.394
With the outage, what do more retries do to the job failure chance?

06Assess the batch job

Assess the batch job

The nightly-job review first needs a probability rather than an expected count. Retries change the event to a task exhausting every allowed attempt, while the work estimate counts only attempts that actually run. A persistent service outage then couples every task: weight the outage and healthy-run cases in both the failure estimate and the expected work.

In the frontend Labeling counts and chances in Next.js and Svelte Optional

A status widget shows both numbers with labels that say which is which: the expected failed checks, and the chance of at least one. It uses the log1p form so tiny failure rates survive the calculation. It handles zero checks and endpoint probabilities explicitly, and labels a small nonzero chance that rounds below the percentage display’s resolution instead of presenting it as zero risk.

Estimate.tsx
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
// Separate an expected count from a probability; use the independence model.
function anyFailureChance(checks: number, rate: number): number {
  if (checks === 0 || rate === 0) return 0;
  if (rate === 1) return 1;
  if (checks === 1) return rate;

  return -Math.expm1(checks * Math.log1p(-rate));
}

export function DailyRisk({
  checks,
  failureRate
}: {
  checks: number;
  failureRate: number;
}) {
  if (
    !Number.isInteger(checks) ||
    checks < 0 ||
    checks > 1_000_000 ||
    !Number.isFinite(failureRate) ||
    failureRate < 0 ||
    failureRate > 1
  ) {
    return <p>Risk estimate unavailable</p>;
  }

  const expected = checks * failureRate;
  const anyFailure = anyFailureChance(checks, failureRate);
  const chanceText =
    anyFailure > 0 && anyFailure < 0.001
      ? "Less than 0.1%"
      : `${(anyFailure * 100).toFixed(1)}%`;

  return (
    <dl>
      <dt>Expected failed checks today</dt>
      <dd>{expected.toFixed(2)}</dd>
      <dt>Chance of at least one failure</dt>
      <dd>{chanceText}</dd>
    </dl>
  );
}

Check this idea later
Three independent replicas are each down 1% of the time. How often are all three down?
Does linearity of expectation require independent events?
Sources

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