Average a function over 1,024 points. Scatter them at random and the answer misses by about 0.038. Place them by Sobol's rule and it misses by 0.0029: 13 times closer, for the same cost.
Pick a point set and slide N. Watch where the points land and how far each average misses. Then we build, piece by piece, the measure of evenness, the digit trick, Halton and Sobol points, the error bound, the rate, and the error bars.
You need Monte Carlo averaging (the error falls like 1/√N) and a little Python. We build the rest from zero.
Part of the Monte Carlo family. Start with Monte Carlo if the 1/√N rule is new to you.
The function is f(x, y) = ex+y; its exact average is (e − 1)² = 2.9525. Halton and Sobol errors are exact for these points. The random bar is the typical (root mean square) miss, σ/√N with σ = 1.22; the dot is this draw. Error axis is logarithmic.
Chapter 0
Drop 16 random points in 16 cells and count the cells they miss
You want the average of a function over the unit square: the mean brightness of one pixel, or the expected payoff of a trade. Monte Carlo drops N random points, evaluates the function at each, and averages. The error falls like 1/√N.
Where does that error come from? Look at where the points land. Random points do not know about each other, so some crowd together and some regions get none.
Each cell should get one point. Red cells got none. Switch the point set, then press New random draw a few times.
Each random draw uses a fixed seed, so it repeats. Halton uses bases 2 and 3 and starts at n = 1; Sobol is the unscrambled scipy sequence and starts at 0.
Work the number for 16 points in 16 cells. One random point misses a given cell with chance 15/16. All 16 points miss it with chance (15/16)16 = 0.356. So about 0.356 × 16 = 5.7 cells stay empty: a third of the square gets no sample at all.
Quasi-Monte Carlo (QMC) keeps the average and changes the points. It places them by a fixed rule so that every region gets its fair share. A point set built this way is a low-discrepancy set. Sobol's first 16 points fill all 16 cells. Halton's first 16 leave 2 empty, and at 64 points Halton leaves 10 of 64 empty, against 23 for random.
The symptom is an estimate that changes too much from run to run. To measure it, run 10 seeds and look at the spread, or count empty cells as above. The fix is not only more points. Better placed points remove the holes that cause the spread.
Chapter 1
Drag a box from the corner and find where the points betray their unevenness
Two people each hand you 64 points and say theirs are well spread. You need one number that decides. Here is a test. Take a box that starts at the corner (0, 0). If the points are even, the share of points inside the box equals the box's area.
Drag on the square to move the box's far corner. The read-out compares the share of points inside with the area. Then press Find the worst box.
The page finds the worst box exactly. It tries corners at the points' own coordinates and at 1, with the edge counted in and out.
Name the parts. Call the box B. It runs from (0, 0) to a corner (t1, t2), so its area vol(B) is t1 × t2. Call the number of points inside it A(B), out of N points in all. The star discrepancy D*N is the worst gap between share and area, over every such box.
D-star-N equals the max over boxes B of the size of: A of B over N, minus vol of B.
For each corner box B, count the points inside, divide by N, and subtract the area vol(B). Keep the largest gap. That gap is D*N.
On a line a box is an interval [0, t]. Clumped set {0.10, 0.15, 0.20, 0.70}: at t = 0.20, A = 3, so 3/4 − 0.20 = 0.55. Even set {0.125, 0.375, 0.625, 0.875}: the worst gap is half a spacing, 0.125. The clumped set is 4.4 times less even.
In two dimensions, 64 Halton points score D* = 0.052. Two hundred random sets of 64 points average 0.148, nearly three times worse. A random set's discrepancy shrinks only like 1/√N. That is the 1/√N of Monte Carlo, seen from the points' side.
The symptom is a discrepancy that looks small but is wrong. A scan over a coarse grid of corners misses the worst box, and its cost grows like (grid size)d. To measure it, compare with the exact scan on a small set. The fix in practice is a closed-form cousin: scipy's qmc.discrepancy computes an L2 discrepancy (the centred one by default) with no scan.
Chapter 2
Write n in base 2, read it backwards behind the point, and watch each new point split the largest gap
Equal spacing (1/N, 2/N, …) is perfectly even, but you must fix N first. To add one point you start over. You want a sequence: points you add one at a time that stay even after every step.
The trick: write the integer n in a base b, then read its digits backwards behind a point.
Press Next point. The digits of n flip behind the point, and the new point drops into the line. The blue band is the largest gap left.
Exact arithmetic, n from 1 to 64. The sequence in base 2 is the van der Corput sequence.
Name the parts. Write n in base b. Its digits are a0 (the last digit), a1, a2 and so on: n = a0 + a1b + a2b² + …. The radical inverse φb(n) puts those digits after the point in reverse order.
phi-b of n equals the sum over k of a-k times b to the minus k plus one.
Write n in base b. Take the digits a-k from the last to the first. Put digit a-k at place b−(k+1) after the point and add them up.
n = 6 in base 2 is 110, so a0 = 0, a1 = 1, a2 = 1. φ2(6) = 0/2 + 1/4 + 1/8 = 0.375. n = 5 is 101: φ2(5) = 1/2 + 0 + 1/8 = 0.625. Neighbours 5 and 6 land in opposite halves. In base 3, 7 is 21: φ3(7) = 1/3 + 2/9 = 0.556.
In base 2 the sequence runs 0.5, 0.25, 0.75, 0.125, 0.625, 0.375, 0.875. The last digit of n changes fastest, and it now decides the first half. So consecutive points jump to opposite sides, and each new point lands in one of the largest gaps. This is the van der Corput sequence. Its first 16 points have D* = 0.0625 = 1/N, within a factor of 2 of the best any 16 points can do, 1/(2N).
The symptom is a running average that drifts in one direction before it settles. Points taken in forward order (1/8, 2/8, 3/8, …) sweep left to right. The first half of the run sees only the left half of the line. To measure it, plot the running mean. The fix is the mirror: it makes every prefix of the sequence even.
Chapter 3
Give each coordinate its own base, and watch large bases line the points up
The van der Corput sequence fills a line. A pixel or a trade needs two or more coordinates. What goes in the second one?
Base 2 again fails at once: point n becomes (φ2(n), φ2(n)), and every point sits on the diagonal x = y. You cover a line, not a square.
Choose the bases, then slide N. Watch bases 17 and 19 at 16 points, then add points.
Points n = 1 to N. The correlation is the plain Pearson correlation of the x and y columns.
The Halton sequence gives each coordinate its own prime base. Call the dimension d and the d-th prime pd. Point number n is xn.
x-n equals the list: phi-2 of n, phi-3 of n, and so on up to phi-p-d of n.
To make point n, mirror n in base 2 for the first coordinate, in base 3 for the second, and in the d-th prime p-d for the last. Different primes keep the coordinates from moving in step.
n = 4. In base 2, 4 is 100, so φ2(4) = 0.001 in binary = 0.125. In base 3, 4 is 11, so φ3(4) = 1/3 + 1/9 = 0.444. Point 4 is (0.125, 0.444).
Large primes bring a known weakness. For n below 17, φ17(n) = n/17 and φ19(n) = n/19. Both grow in step, so the first 16 points lie on one straight line: correlation 1.00. With bases 2 and 3 the first 16 points have correlation −0.19. A 30-dimensional Halton set uses primes up to 113 and shows this Halton degradation in its last axes.
The symptom is stripes when you plot two high coordinates of a Halton set against each other. To measure it, compute the correlation of coordinate pairs over the first few hundred points. The fix is scrambled Halton, which permutes each base's digits at random, or a switch to Sobol, next chapter.
Chapter 4
Cut the square into boxes of equal area in every allowed shape and check that Sobol puts one point in each
Halton hopes that different primes keep coordinates apart. Sobol works only in base 2 and guarantees a fair count in a whole family of boxes.
An elementary box in base 2 is a grid cell with sides that are powers of 1/2: 1/4 by 1/4, or 1 by 1/16. Take 2m points. If every elementary box of area 2t−m holds exactly 2t points, the set is a (t, m, d)-net. Smaller t is better; t = 0 means one point per box.
Pick the number of points, then slide the box shape. Green boxes hold exactly one point; red boxes hold none or more than one.
Sobol is the unscrambled two-dimensional scipy sequence, starting at point 0. Its first 4, 16 and 64 points are (0, m, 2)-nets; we checked every shape.
Name the parts. The set has N = 2m points. Each elementary box has area vol = 2t−m. A fair share is N times the area.
The count in each box equals N times vol, which is two to the m times two to the t minus m, which is two to the t.
Take 2-to-the-m points. Cut the square into equal elementary boxes of area 2-to-the-(t minus m), in any allowed shape. Count the points in each box. A net puts exactly 2-to-the-t points in every one.
Sobol's first 4 points: (0, 0), (0.5, 0.5), (0.75, 0.25), (0.25, 0.75). Here m = 2 and t = 0, so each box of area 1/4 holds 4 × 1/4 = 1 point. That holds for the 2 by 2 cells, the 4 tall strips and the 4 flat strips. Four random points do this for the 2 by 2 cells only with chance 4!/44 = 0.094.
How does Sobol build such points? Each coordinate has a list of direction numbers, bit patterns chosen from a polynomial in arithmetic on 0 and 1. Point n + 1 is point n with one direction number XOR-ed in. The first coordinate is the van der Corput set in a reordered sequence: 0, 0.5, 0.75, 0.25, 0.375, 0.875, 0.625, 0.125. Halton is not a base-2 net: its first 16 points leave 2 of the 4 by 4 boxes empty.
The symptom is a Sobol run with N = 1,000 that does a little worse than expected. The balance holds only at powers of 2. To measure it, count boxes as above. The fix is to ask for 2m points: random_base2(m) in scipy.
Chapter 5
Make the function wiggle and the points uneven, and watch the real error stay under the bound
Even points should give a better average. The Koksma-Hlawka inequality says how much better, with no probability in it. It splits the error into two factors: one belongs to the function, one belongs to the points.
The function's factor is its total variation V(f): on a line, the sum of all its rises and falls from 0 to 1. A flat function has V = 0. A function that climbs from 0 to 1 once has V = 1. In more dimensions the matching quantity is the Hardy-Krause variation.
The grey curve is f(x) = x + a sin(6πx). Raise the wiggle a to raise V(f). Switch the points and change N. The yellow bar is the bound; the other bar is the real error.
The exact average of f is 0.5 for every a. The page measures V(f) on a fine grid; D* is exact. Each random set uses a seed fixed by N.
Name the parts. The QMC estimate QN is the plain average of f over the N points. The true average is I, the integral of f over the cube. V(f) is the variation and D*N the star discrepancy from chapter 1.
The size of Q-N minus I is at most V of f times D-star-N.
Measure how much f wiggles, V(f). Measure how uneven the points are, D*N. Multiply them. The error of the average Q-N is never larger than that product.
f(x) = x² on the points {0.125, 0.375, 0.625, 0.875}. Q4 = (1 + 9 + 25 + 49)/256 = 0.3281 and I = 1/3, so the error is 0.0052. f rises once from 0 to 1, so V(f) = 1. D* = 0.125. The bound is 1 × 0.125 = 0.125, 24 times the real error.
The split explains the whole field. Once you fix the function, V(f) is fixed. The only factor left is D*N, so QMC is the craft of making D*N small. The bound is loose: in the device, the real error sits well under the yellow bar.
The symptom is a bound of infinity. An indicator with a slanted edge, such as f = 1 when x + y > 1, has infinite Hardy-Krause variation, so the bound says nothing. QMC still helps such functions, but less. To measure the real gain, use the error bars of chapter 8. The fix is to smooth the integrand where you can.
Chapter 6
Race random points against Sobol on a log-log plot and raise the dimension
Monte Carlo pays a steep price for accuracy. Its error falls like 1/√N, so halving the error costs 4 times the points, and one more decimal digit costs 100 times.
For Halton and Sobol, D*N falls almost like 1/N. Name the parts: the dimension d, the number of points N, and a constant cd that depends on the construction and on d.
D-star-N is at most c-d times log N to the d, over N.
Take the log of N, raise it to the power d, divide by N, and scale by c-d. Times V(f), by chapter 5, that ceilings the QMC error.
With cd set to 1 for comparison: d = 1 and N = 64 give ln 64 / 64 = 4.16 / 64 = 0.065, below Monte Carlo's 1/√64 = 0.125. But d = 10 and N = 1,000,000 give 13.810 / 106 = 2.5 × 105, against 0.001. At d = 10 this ceiling drops below 1/√N only after about 1039 points.
The ceiling is a worst case. Real errors are much smaller, so measure them. The device averages a product of d bumps, each with average 1, with random points and with scrambled Sobol points, 24 runs at each N.
On log-log axes, a rate N−p is a straight line of slope −p. Pick the dimension, then slide N to read the gap.
Computed offline with numpy and scipy.stats.qmc: f(x) = product over i of (π/2) sin(πxi), exact average 1. Each point is the root mean square error over 24 seeded runs; the fitted slopes wobble by about 0.1.
At d = 4 and N = 4,096, random points miss by 0.0157 and Sobol by 0.00036: 44 times smaller. At d = 16 the gap shrinks to 2 times, and at N = 16 Sobol is worse. Here every coordinate matters equally, the hardest case for QMC.
The symptom is QMC no better than Monte Carlo on a high-dimensional problem with a small budget. To measure it, compare error bars (chapter 8) at your real N. The fix is to move the important variation into the first few coordinates, next chapter.
Chapter 7
Build a random path two ways and see where its variance lives
In 1995 Paskov and Traub priced a mortgage-backed security with 360 monthly cash flows: an average over 360 dimensions. The ceiling of chapter 6 is useless there. Yet Sobol points beat Monte Carlo. Why?
Most of the integral's variation came from a few directions. A function can have nominal dimension 360 and still act like a function of 2 or 3 coordinates. The count that matters is its effective dimension.
Name the parts. Split the variance of f by coordinate, in the order the points feed them. Call Sk the share of the variance carried by the first k coordinates.
d-eff is the smallest k for which S-k is at least 0.95.
Add up the variance share of coordinate 1, then 1 and 2, then 1 to 3. Stop when the running total S-k reaches 95 percent. The count k is d-eff.
Take g = ex1 + 0.05 × (sin x2 + … + sin x10). The first term has variance 0.2420. Each sine term has 0.0025 × 0.0614 = 0.00015, nine of them 0.0014. S1 = 0.2420 / 0.2434 = 0.994, so deff = 1 of 10.
Sobol balances its first coordinates best. So the trick is to feed the important directions into them. A random price path needs one normal draw per time step. The plain sequential build uses draw j for step j, which spreads the variance over all draws. The Brownian bridge uses draw 1 for the end point, draw 2 for the midpoint, draws 3 and 4 for the quarter points, and so on.
Bars are each coordinate's share of the variance of the path's average. The grey line is the running total Sk; the yellow line marks 95 percent. Switch the build and the step count.
Exact, not sampled: the path average is linear in the normal draws, so each draw's share is its squared weight over the sum of squared weights. Both builds give the same path distribution.
With 16 steps the sequential build needs 11 coordinates to reach 95 percent. The bridge needs 3; its first coordinate alone carries 77 percent. Same paths, same distribution, a different order of draws.
The symptom is a QMC gain that vanishes when the draws feed time steps in order. To measure it, compute shares as above, or compare error bars with and without reordering. The fix is a Brownian bridge or a principal-component ordering of the draws.
Chapter 8
Shift the whole point set at random, repeat, and read the error bar from the spread
Your manager asks for a confidence interval. Plain Sobol gives one fixed number. There is no spread to measure, so there is no error bar, and the Koksma-Hlawka ceiling is too loose to quote.
Randomized QMC (RQMC) moves the whole point set at random in a way that keeps it even. Each moved set gives an unbiased estimate. Repeat with independent moves and the spread of the estimates is your error bar.
The simplest move is the random shift (Cranley-Patterson rotation). Name the parts: a point xi and one random offset U, uniform on [0, 1)d. frac keeps only the part after the point.
x-i-prime equals frac of x-i plus U.
Draw one offset U. Add it to every point x-i and wrap back into [0, 1). Average f over the shifted points x-i-prime. Repeat R times with new offsets; the R averages give a mean and a spread.
Offset U = 0.8 on the points 0.375 and 0.625: frac(1.175) = 0.175 and frac(1.425) = 0.425. Both moved by the same amount, so their gap stays 0.25 and the set stays even.
The error bar is the standard error: the spread s of the R estimates divided by √R. With 20 shifts of 256 Sobol points on f = 2x × 2y (true average 1), s = 0.0083 and the standard error is 0.0083 / √20 = 0.0019. Twenty random sets of 256 give s = 0.061 and a standard error of 0.014, seven times wider.
Each run draws a fresh random set (warm, top) and a fresh shift of the Sobol set (pink, bottom), both 256 points. Press Run and watch the two histograms grow.
Live in your browser: f(x, y) = 4xy on 256 points; the dashed line is the true value 1. Draws use fixed seeds, so Reset repeats them.
Owen scrambling permutes the digits of each coordinate at random instead. It is scipy's default (scramble=True). On the same test it gave s = 0.00045. Averaged over 500 scrambles, the estimate was 0.99998: no bias.
The symptom is an error bar that is far too small or far too big. It comes from treating the N points of one QMC set as independent and using s/√N. They are not independent. The fix is R independent randomizations, R of 10 to 30, and the spread across them.
Chapter 9
Race Monte Carlo, Sobol and Sobol with a bridge to the same accuracy on a 64-dimensional price
An Asian call option pays the amount by which a stock's average price over d dates beats a fixed price K, the strike. If it does not, it pays nothing. Each simulated price path needs d normal draws, so each path is one point in d dimensions. QMC turns a uniform point into normal draws with the inverse normal CDF.
Name the parts. Ā is one path's average price over the d dates. K is the strike. r is the interest rate and T the time to expiry in years. The price C is the discounted average payoff.
C equals e to the minus r T, times the average over paths of the max of A-bar minus K and zero.
Simulate a price path and average its prices to get A-bar. Pay A-bar minus K if that is positive, else 0. Average the payoffs over all paths and discount by e to the minus r T.
With made-up paths: one path has Ā = $108, so it pays $8; another has Ā = $95 and pays $0. Their average payoff is $4, and C = 4 × e−0.05 = $3.80. Over many paths, our toy model with 64 dates gives C = $5.84.
Top: sample price paths and the strike. Bottom: error against paths. Teal is Monte Carlo, dashed pink is plain Sobol, solid pink adds the bridge. The yellow line is Monte Carlo's error at 16,384 paths. Pick the dates, then press Run.
Toy model, computed offline with numpy and scipy.stats.qmc: stock at $100, strike $100, volatility 0.2, r = 0.05, one year. Errors are root mean square over 20 seeded runs against a 218-path reference. The paths drawn on top are illustrative random paths.
With 64 dates and 16,384 paths, Monte Carlo misses by $0.062. Plain Sobol misses by $0.0089, seven times less. Sobol with the bridge misses by $0.0017. It matches Monte Carlo's 16,384-path accuracy with about 100 paths: some 160 times fewer.
The symptom is plain Sobol gaining less as the dates grow. To match Monte Carlo's 16,384-path error, it needs about 650 paths at 16 and 64 dates, against 125 at 4. To measure it, plot error against paths for each build. The fix is the bridge of chapter 7, which puts the path's overall level in coordinate 1.
Chapter 10
Keep the terms, the numbers, the checklist and the code in one place
QMC lowers the error of an average by choosing better points, not more points. It needs a known domain it can fill, usually the unit cube. Discrepancy measures the points, variation measures the function, and their product caps the error.
| Term | What it is | Number on this page |
|---|---|---|
| Star discrepancy D*N | worst gap between share of points and area, over corner boxes | 64 Halton points: 0.052; 64 random: 0.148 on average |
| Radical inverse φb(n) | digits of n in base b, mirrored behind the point | φ2(6) = 0.375 |
| Halton point | one prime base per coordinate | point 4 = (0.125, 0.444) |
| (t, m, d)-net | every elementary box of area 2t−m holds 2t points | Sobol in 2-D: t = 0 |
| Koksma-Hlawka | error ≤ V(f) × D*N | x² on 4 points: error 0.0052, bound 0.125 |
| Rate | D*N up to cd(log N)d/N | d = 4, N = 4,096: 44 times less error than random |
| Effective dimension | leading coordinates that carry 95% of the variance | 16-step path: 11 sequential, 3 with a bridge |
| Randomized QMC | random shift or scramble, R replicates, standard error s/√R | 256 points: 0.0019 against 0.014 for random |
| Asian option, 64 dates | C = e−rT × average of max(Ā − K, 0) | about 100 bridge paths match 16,384 random ones |
random_base2(m).import numpy as np from scipy.stats import qmc def rqmc_mean(f, d, m, R=20): """Average f over the unit cube with R scrambled Sobol sets of 2**m points.""" ests = [f(qmc.Sobol(d, scramble=True, seed=r).random_base2(m)).mean() for r in range(R)] ests = np.array(ests) return ests.mean(), ests.std(ddof=1) / np.sqrt(R) # estimate, standard error f = lambda P: np.exp(P.sum(axis=1)) # exact average (e-1)**2 in 2-D print(rqmc_mean(f, d=2, m=10)) # PyTorch has the same tool: torch.quasirandom.SobolEngine(dimension=d, scramble=True).draw(N)
| Method | Its lever | Error falls like | It needs |
|---|---|---|---|
| Monte Carlo | random points | 1/√N | samples from the distribution |
| Quasi-Monte Carlo | better points | near 1/N in low effective dimension | a domain it can fill, usually the cube |
| Importance sampling | better weights | 1/√N, smaller constant | a good proposal distribution |
| MCMC | a chain that walks to the target | 1/√(effective sample size) | the target up to a constant |
| Hamiltonian MC | a chain moved by gradients | 1/√(effective sample size) | the target's gradient |
| Sequential MC | weighted particles, resampled over time | 1/√N per step | a model that moves step by step |
QMC combines with the others. Importance sampling can draw its proposal through Sobol points. Path tracers in computer graphics sample pixels with Halton and Sobol points, and scipy (scipy.stats.qmc) and PyTorch (torch.quasirandom.SobolEngine) ship scrambled Sobol. Where you cannot sample the target at all, you need weights or a chain: importance sampling or MCMC. For search over a tree of choices, see Monte Carlo tree search.
Now press Present or Teach and explain from memory: the empty cells, the mirrored digits, the fair share in every box, and the bound. Then go back to the hero, set N to 1,024, and say why Sobol's bar sits so far below the random one.