Before talking about Quasi-Monte Carlo (QMC), a quick introduction to Monte
Carlo (MC). MC methods, or MC experiments, are a broad class of
computational algorithms that rely on repeated random sampling to obtain
numerical results. The underlying concept is to use randomness to solve
problems that might be deterministic in principle. They are often used in
physical and mathematical problems and are most useful when it is difficult or
impossible to use other approaches. MC methods are mainly used in
three problem classes: optimization, numerical integration, and generating
draws from a probability distribution.
Generating random numbers with specific properties is a more complex problem
than it sounds. Simple MC methods are designed to sample points that are
independent and identically distributed (IID). But generating multiple sets
of random points can produce radically different results.
In both cases in the plot above, points are generated randomly without any
knowledge about previously drawn points. It is clear that some regions of
the space are left unexplored - which can cause problems in simulations as a
particular set of points might trigger a totally different behavior.
A great benefit of MC is that it has known convergence properties.
Let’s look at the mean of the squared sum in 5 dimensions:
f(x)=(j=1∑5xj)2,
with xj∼U(0,1). It has a known mean value,
μ=5/3+5(5−1)/4. Using MC sampling, we
can compute that mean numerically, and the approximation error follows a
theoretical rate of O(n−1/2).
Although the convergence is ensured, practitioners tend to want to have an
exploration process that is more deterministic. With normal MC, a seed can be
used to have a repeatable process. But fixing the seed would break the
convergence property: a given seed could work for a given class of problem
and break for another one.
What is commonly done to walk through the space in a deterministic manner is
to use a regular grid spanning all parameter dimensions, also called a
saturated design. Let’s consider the unit-hypercube, with all bounds ranging
from 0 to 1. Now, having a distance of 0.1 between points, the number of points
required to fill the unit interval would be 10. In a 2-dimensional hypercube
the same spacing would require 100, and in 3 dimensions 1,000 points. As the
number of dimensions grows, the number of experiments required to fill
the space rises exponentially as the dimensionality of the space increases.
This exponential growth is called “the curse of dimensionality”.
To mitigate this issue, QMC methods have been designed. They are
deterministic, have a good coverage of the space and some of them can be
continued and retain good properties.
The main difference with MC methods is that the points are not IID but they
depend on previous points. Hence, some methods are also referred to as
sequences.
This figure presents 2 sets of 256 points. The design of the left is a plain
MC whereas the design of the right is a QMC design using the Sobol’ method.
We clearly see that the QMC version is more uniform. The points sample better
near the boundaries and there are fewer clusters or gaps.
One way to assess the uniformity is to use a measure called discrepancy.
Here the discrepancy of Sobol’ points is better than crude MC.
Coming back to the computation of the mean, QMC methods also have better rates
of convergence for the error. They can achieve O(n−1) for this
function, and even better rates on very smooth functions. This figure shows
that the Sobol’ method has a rate of O(n−1):
We refer to the documentation of scipy.stats.qmc for
more mathematical details.
Calculate the discrepancy
Let’s consider two sets of points. From the figure below, it is clear that
the design on the left covers more of the space than the design on the right.
This can be quantified using a scipy.stats.qmc.discrepancy measure.
The lower the discrepancy, the more uniform a sample is.
"""Sobol' and Halton sequences."""fromscipy.statsimportqmcimportnumpyasnpimportmatplotlib.pyplotaspltrng=np.random.default_rng()n_sample=256dim=2sample={}# Sobol'engine=qmc.Sobol(d=dim,seed=rng)sample["Sobol'"]=engine.random(n_sample)# Haltonengine=qmc.Halton(d=dim,seed=rng)sample["Halton"]=engine.random(n_sample)fig,axs=plt.subplots(1,2,figsize=(8,4))fori,kindinenumerate(sample):axs[i].scatter(sample[kind][:,0],sample[kind][:,1])axs[i].set_aspect('equal')axs[i].set_xlabel(r'$x_1$')axs[i].set_ylabel(r'$x_2$')axs[i].set_title(f'{kind}—$C^2 = ${qmc.discrepancy(sample[kind]):.2}')plt.tight_layout()plt.show()
QMC engines are state-aware. Meaning that you can continue the sequence,
skip some points, or reset it. Let’s take 5 points from
scipy.stats.qmc.Halton. And then ask for a second set of 5 points: