C11 · Lesson 7 · Uncertainty lab
Budgets, Monte Carlo propagation and pass-fail decisions in Python
Lessons 1 to 3 built uncertainty by hand: standard uncertainties, propagation by derivatives, budgets and guard bands. Code makes all of it routine, and adds a method that needs no derivatives at all: simulate the measurement thousands of times with random inputs and look at the spread of the results. In this lab you'll convert specifications to standard uncertainties, combine a budget, propagate by Monte Carlo, and write the decision rule that turns a reading and its uncertainty into pass, fail or indeterminate.
- The curve is a budget's combined uncertainty as one contribution x grows while the other, , stays fixed: .
- While x is less than a third of , it barely matters: the total rises less than 6%. Once x passes , it takes over.
- This is why budgets are read from the top: improving anything but the largest term changes almost nothing.
Predict
A Monte Carlo propagation of uses x drawn from a Gaussian with mean 0 and σ = 1. What spread does y have, and what does first-order propagation by derivatives predict?
Monte Carlo gives (y is chi-square distributed with mean 1). The derivative of at x = 0 is zero, so first-order propagation predicts no uncertainty at all. Linearization fails when a function is curved over the range of its inputs; simulation doesn't care.
Name it
The pieces
- Standard uncertainties: rectangular ±a gives , triangular ±a gives , and an expanded uncertainty U quoted at k = 2 gives U/2.
- A budget: and .
- Monte Carlo propagation: draw each input from its distribution, compute the result, repeat N times, and take the mean and standard deviation of the results. Seed the random generator so the answer is reproducible. The precision of the estimated spread improves as , so tens of thousands of trials give two or three good digits.
- Decision rules: with expanded uncertainty U and limits L and H, a reading x is a clear pass inside , a clear fail outside , and indeterminate in between.
Worked example
Propagate R = V/I by Monte Carlo with V = 5.00 ± 0.02 V and I = 2.000 ± 0.010 mA (standard uncertainties), and compare with the derivative formula.
Show the solution
- Monte Carlo. Twenty thousand trials give R = 2500 Ω with a spread of 15.9 Ω.
- Derivative formula.
- Why they agree. V/I is nearly linear over such small uncertainties; the simulation would reveal the difference if I's uncertainty were a large fraction of I.
Answer both give R = 2500 Ω, with an uncertainty of 15.9 Ω by Monte Carlo and 16.0 Ω by the formula.
Worked example
A 10 V reference must read between 9 V and 10 V, and U = 0.02 V. Classify readings of 9.97, 9.99 and 10.05 V.
Show the solution
- Acceptance zone. Pulling each limit in by U = 0.02 V leaves [9.02, 9.98] V.
- 9.97 V. It is inside [9.02, 9.98]: pass.
- 9.99 V. It is between 9.98 and 10.02: indeterminate, because the true value could be on either side of the limit.
- 10.05 V. It is above 10.02: fail.
- An indeterminate reading. It calls for a better measurement or a decision rule agreed in advance.
Answer 9.97 V passes, 9.99 V is indeterminate, and 10.05 V fails.
Check
A budget has a term of 1 and another 0.2 times as large. What is the combined uncertainty?
Check
Write standard_u(a, dist): the standard uncertainty for a half-width a when dist is 'rectangular' or 'triangular', or for an expanded uncertainty a at k = 2 when dist is 'normal'. Then write budget(rows): given a list of (u, c) pairs, return (uc, U) with k = 2.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write monte_carlo(f, means, sds, n, seed): draw each input from a Gaussian with the given mean and standard deviation using random.Random(seed), evaluate f n times, and return (mean, std) of the results (std with n − 1).
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write decide(x, lo, hi, U): return 'pass' if x is within [lo + U, hi − U], 'fail' if it's below lo − U or above hi + U, and 'indeterminate' otherwise.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
When does Monte Carlo propagation give a different answer from propagation with derivatives, and which should be trusted?
Derivative propagation replaces the function with a straight line through the measured point and assumes the inputs' spreads are small enough that the curvature doesn't matter. When an uncertainty is a sizable fraction of its input, or the function bends sharply (a square near zero, a division by a quantity that could approach zero, a threshold), the straight-line approximation misjudges both the spread and the mean of the result. Monte Carlo makes no such approximation: it pushes real samples through the real function, so it captures curvature, skewed distributions and correlations when they're modeled. When the two methods disagree, the simulation is the one to trust, provided enough trials were run.
Practice
Keep practicing
One kind of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.