import numpy as np
import matplotlib.pyplot as plt
from pyapprox.util.backends.numpy import NumpyBkd
from pyapprox.benchmarks.instances.analytic.cantilever_beam import (
cantilever_beam_1d_analytical,
)
from pyapprox.expdesign.quadrature.halton import HaltonSampler
from pyapprox.expdesign.quadrature.sobol import SobolSampler
bkd = NumpyBkd()
benchmark = cantilever_beam_1d_analytical(bkd)
model = benchmark.function()
variable = benchmark.prior()Practical QMC: Scrambling, Error Bars, and High Dimensions
PyApprox Tutorial Library
Learning Objectives
After completing this tutorial, you will be able to:
- Explain why deterministic QMC provides no built-in error estimate
- Use Owen scrambling to create randomized QMC replicates for error bars
- Define star discrepancy and state the Koksma–Hlawka inequality as the theoretical basis for QMC
- Quantify empirically how the QMC advantage depends on dimension
- Make practical decisions about when to prefer QMC vs. MC
Prerequisites
Complete Quasi–Monte Carlo Sampling before this tutorial.
Setup
We continue with the beam model and QMC samplers from the previous tutorial.
The Error Bar Problem
The previous tutorial showed that Halton and Sobol sequences converge faster than MC. But there is a catch: deterministic QMC gives the same answer every time. Run it again with the same parameters, and you get the identical estimate — there is no built-in notion of variability and therefore no standard error.
In contrast, MC’s randomness is a feature for error estimation: repeat the experiment with a different random seed and you get a different estimate; the spread of estimates gives a variance.
We need a way to introduce just enough randomness into QMC to enable error estimation, without destroying the low-discrepancy structure that makes it converge fast.
Owen Scrambling: Randomized QMC
Owen scrambling solves this problem. It randomly permutes the digits in the radical-inverse representation of each QMC point. Crucially, the permutations preserve the equidistribution properties of the sequence — each scrambled replicate is itself a valid low-discrepancy point set — while introducing enough randomness that independent replicates give different estimates.
The resulting method is called randomized quasi–Monte Carlo (RQMC). The workflow is:
- Generate \(R\) independent scrambled replicates (different
seedvalues). - Compute the integral estimate from each replicate.
- Average the estimates and use their sample standard deviation to form a standard error.
R = 30 # number of scrambled replicates
N = 256 # samples per replicate
replicate_means = []
for seed in range(R):
sampler = SobolSampler(
model.nvars(), bkd, distribution=variable,
scramble=True, seed=seed,
)
samples, weights = sampler.sample(N)
qoi = model(samples)[0]
replicate_means.append(bkd.to_float(bkd.dot(weights, qoi)))
replicate_means = np.array(replicate_means)
rqmc_mean = np.mean(replicate_means)
rqmc_se = np.std(replicate_means, ddof=1) / np.sqrt(R)
print(f"RQMC estimate: {rqmc_mean:.6f}")
print(f"RQMC standard error: {rqmc_se:.6f}")
print(f"True mean: {true_mean:.6f}")RQMC estimate: 4.259103
RQMC standard error: 0.000023
True mean: 4.259102
The standard error is a practical error bar: a 95% confidence interval is approximately \(\hat{\mu}_{\text{RQMC}} \pm 1.96 \cdot \text{SE}\).
RQMC vs. MC Variability
Figure 1 compares the variability of MC and RQMC estimates side-by-side, using the same format as the histogram experiments in the MC tutorial. Each method runs 200 independent experiments with \(N = 256\) samples per experiment.
Why QMC Works: Star Discrepancy and Koksma–Hlawka
The convergence advantage of QMC has a precise theoretical basis. To state it, we need one definition.
Star discrepancy \(D_N^*\) measures how uniformly a point set \(\{x_1, \ldots, x_N\} \subset [0,1]^d\) fills the unit hypercube. For every axis-aligned box \([0, u_1] \times \cdots \times [0, u_d]\) anchored at the origin, we compare the fraction of points inside the box to the box’s volume:
\[ D_N^* = \sup_{\mathbf{u} \in [0,1]^d} \left| \frac{\#\{x_i \in [\mathbf{0}, \mathbf{u}]\}}{N} - \prod_{j=1}^d u_j \right| \]
A small \(D_N^*\) means the empirical distribution of the points closely matches the uniform distribution. For pseudorandom samples, \(D_N^* = O(N^{-1/2})\) (by the DKW inequality). Halton and Sobol sequences achieve:
\[ D_N^* = O\!\left(\frac{(\log N)^d}{N}\right) \]
which is asymptotically much faster for moderate \(d\).
The Koksma–Hlawka inequality connects discrepancy to integration error:
\[ \left|\frac{1}{N}\sum_{i=1}^N f(\mathbf{x}_i) - \int_{[0,1]^d} f(\mathbf{x})\,d\mathbf{x}\right| \le V_{\mathrm{HK}}(f) \cdot D_N^* \]
where \(V_{\mathrm{HK}}(f)\) is the variation of \(f\) in the sense of Hardy–Krause — a measure of how “wiggly” the integrand is. The bound says: integration error is at most the integrand’s roughness times the point set’s non-uniformity. QMC reduces the second factor.
The Koksma–Hlawka bound identifies two independent levers:
- Reduce \(D_N^*\) by using a better point set (QMC instead of MC) — this is what this tutorial series is about.
- Reduce \(V_{\mathrm{HK}}(f)\) by smoothing or transforming the integrand — this is the idea behind importance sampling and control variates, covered in later tutorials.
Visualizing Discrepancy
Figure 2 makes star discrepancy concrete. For a small point set (\(N = 32\)), we find the axis-aligned box \([\mathbf{0}, \mathbf{u}^*]\) where the discrepancy is largest and overlay it on the point set. The random sample has a much larger worst-case deviation than the Sobol sequence.
Effect of Dimension
The Koksma–Hlawka bound includes the factor \((\log N)^d\), which grows rapidly with dimension \(d\). Does this mean QMC is useless in high dimensions?
Not necessarily. The bound is often pessimistic because many real-world functions have low effective dimension — they depend primarily on a few coordinates, even if the nominal dimension is large. Still, the QMC advantage does erode as \(d\) grows, and it is important to quantify this empirically.
We test with a smooth, separable integrand whose true integral is known analytically:
\[ f(\mathbf{x}) = \prod_{j=1}^d \frac{3x_j^2 + 1}{2}, \qquad \mathbf{x} \in [0,1]^d \]
Each factor integrates to 1, so \(\int f\,d\mathbf{x} = 1\) for all \(d\).
def product_integrand(x):
"""
Separable product integrand with known integral = 1.
Parameters
----------
x : ndarray, shape (d, N)
Points in [0, 1]^d.
Returns
-------
ndarray, shape (N,)
Function values.
"""
return np.prod((3 * x**2 + 1) / 2, axis=0)
true_integral = 1.0Figure 3 shows convergence for \(d = 2, 5, 10, 20\). The MC error is dimension-independent (all solid curves overlap), while the QMC error increases with \(d\) but remains below MC for all dimensions tested.
Practical Decision Guide
The following table summarizes when to use QMC vs. MC:
| Situation | Recommendation |
|---|---|
| \(d \le 10\), smooth integrand | QMC (Sobol preferred) |
| \(d \le 10\), discontinuous integrand | MC or RQMC with many replicates |
| \(10 < d \le 50\), smooth | Try RQMC; compare to MC empirically |
| \(d > 50\) or rough integrand | MC (dimension-free convergence rate) |
| Need error bars | Always use scrambled RQMC |
| Need reproducibility | Deterministic QMC with fixed seed |
When in doubt, try scrambled Sobol first. It is never worse than MC (both are unbiased with the same weights), and it is often dramatically better. If your problem is high-dimensional or has discontinuities, you can always fall back to MC.
Key Takeaways
- Scrambling (Owen) turns deterministic QMC into randomized QMC (RQMC): it preserves the low-discrepancy structure and fast convergence while enabling standard error estimation via independent replicates
- Star discrepancy \(D_N^*\) measures how uniformly a point set fills the domain; QMC sequences achieve \(D_N^* = O((\log N)^d / N)\) vs. MC’s \(O(N^{-1/2})\)
- The Koksma–Hlawka inequality bounds integration error as \(V_{\mathrm{HK}}(f) \cdot D_N^*\) — the product of integrand roughness and point set non-uniformity
- Dimension matters: the QMC advantage is largest for smooth, low-dimensional problems and erodes with \(d\), though effective dimension often keeps QMC competitive even in moderately high dimensions
- In practice, always try RQMC and compare empirically — it is never worse than MC and is often orders of magnitude better
Exercises
Compute the star discrepancy (brute-force grid search as in Figure 2) for Halton and Sobol at \(N = 64, 256, 1024\). Plot \(D_N^*\) vs. \(N\) on a log-log axis. What convergence rate do you observe?
In Figure 3, replace the smooth product integrand with a “corner peak”: \(f(\mathbf{x}) = (1 + \sum_j x_j)^{-(d+1)}\). Does the QMC advantage change? (This function is smooth but has most of its mass in one corner of the hypercube.)
Using scrambled Sobol, construct a 95% confidence interval for the beam model’s mean tip deflection at \(N = 512\) with \(R = 30\) replicates. Compare the interval width to a MC confidence interval based on the same total budget of \(30 \times 512 = 15360\) samples. How many MC samples would be needed to match the RQMC interval width?
(Challenge) The
SobolSamplerreturns equal weights \(w_i = 1/N\). Investigate whether using the QMC samples as nodes in a weighted least-squares regression (polynomial chaos) further improves convergence compared to using random samples as regression nodes.
Next Steps
Continue with:
- Monte Carlo Budget Estimation — Predicting the sample budget for a target accuracy
- Multi-Fidelity Monte Carlo — Using cheap surrogate models to reduce the constant in the \(1/M\) convergence rate