Practical QMC: Scrambling, Error Bars, and High Dimensions

PyApprox Tutorial Library

Owen scrambling for error estimation, star discrepancy as a measure of uniformity, and how the QMC advantage depends on dimension.

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.

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()

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:

  1. Generate \(R\) independent scrambled replicates (different seed values).
  2. Compute the integral estimate from each replicate.
  3. 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.

Figure 1: Variability of mean estimates from 200 independent experiments, each using \(N = 256\) samples. Left: pseudorandom MC. Right: scrambled Sobol (RQMC). The RQMC histogram is much narrower, confirming that QMC’s faster convergence is preserved under scrambling. The spread of the RQMC estimates enables standard error computation.

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.

NoteTwo levers for integration accuracy

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.

Figure 2: Star discrepancy illustrated for \(N = 32\) points. The shaded box \([0, u_1] \times [0, u_2]\) is the axis-aligned region anchored at the origin where the fraction of points inside deviates most from the box’s area. Left: pseudorandom (\(D_N^*\) is large). Right: Sobol (\(D_N^*\) is small). Lower discrepancy corresponds to a tighter integration error bound.

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.0

Figure 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.

Figure 3: Integration error vs. \(N\) for a smooth product integrand in \(d = 2, 5, 10, 20\) dimensions. MC error (solid lines) is independent of \(d\) and follows \(O(N^{-1/2})\). Sobol error (dashed lines) increases with \(d\) but remains below MC for all dimensions shown. The QMC advantage is largest in low dimensions and erodes gradually.

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
TipRule of thumb

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

  1. 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?

  2. 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.)

  3. 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?

  4. (Challenge) The SobolSampler returns 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: