Accompanying the article “Application of quasi-Monte Carlo methods to elliptic PDEs with random diffusion coefficients – a survey of analysis and implementation”, Foundations of Computational Mathematics, 2016, by Frances Y. Kuo & Dirk Nuyens.
This page provides code which accompanies the article “Application of quasi-Monte Carlo methods to elliptic PDEs with random diffusion coefficients — a survey of analysis and implementation”, Foundations of Computational Mathematics, 16(6):1631-1696, 2016, by Frances Y. Kuo & Dirk Nuyens.
The code accompanying this article consists of two parts:
The current version of the QMC4PDE package is dated 10 Dec 2024.
The Python scripts make use of the NumPy Python package (which is probably included in your Python distribution) as well as of the SciPy Python package in the case of lattice rules where the Riemann $\zeta$ function and erf functions are needed.
For integration over $\bbR^s$ (i.e., the lognormal setting for the PDE) we are currently investigating alternative choices to control the derivatives in the norm. A future update will likely include this alternative setting.
Change history:
The construction algorithms are all fast component-by-component constructions, using results from [NC06a,NC06b,CKN06,DKLNS14] and [Nuy14]. For lattice rules the construction algorithm will output the generating vector and for the interlaced polynomial lattice rules the algorithm will output the generating matrices. These generating vectors and matrices can then be used in the provided point generators.
For randomly shifted lattice rules the script constructs a good generating vector $\bsz$. The construction script expects the prime $p$ (which defaults to $2$) and the power $m$ to be given such that the (maximum) number of points is $n=p^m$. The generating vector can be used for any intermediate power of $p$ as an embedded sequence of lattice rules. A construction of such lattice sequences was proposed in [CKN06] and this is the approach followed in the lattice rule construction code.
For interlaced polynomial lattice rules the script constructs the associated generating matrices. We fix the base of the finite field to be $2$ for practicalities in the construction, and in the generation of the points. The number of points is $n = 2^m$, but, in contrast to the lattice rules, we currently do not provide these as embedded sequences. The construction script will automatically choose a default irreducible polynomial of degree $m$ as the modulus polynomial (which can be overridden by the user). The specific choice of modulus polynomial does not influence the theoretical error bound. The output of the script will be both the generating matrices $C_1,\ldots,C_{\alpha s} \in \bbZ_2^{m \times m}$ of the polynomial lattice rule as well as the generating matrices $B_1,\ldots,B_s \in \bbZ_2^{\alpha m \times m}$ of the interlaced polynomial lattice rule.
Once the generating vector or the generating matrices have been constructed, they are used as input to the corresponding point generator. These point generators are relatively straightforward to program, and their computational cost to generate a point is really minor and so can be neglected for practical purposes; in fact they are comparable to the fastest random number generators. To generate lattice points, one only requires an integer multiplication, a modulus operation, and a fixed float multiplication/division per dimension. This is comparable to the cost of a simple LCG (linear congruential generator) per dimension. To generate (interlaced) polynomial lattice points in Gray code ordering, one needs an xor instruction and a fixed float multiplication/division per dimension. Additionally, a CTZ (count trailing zeros) algorithm is used to determine the column number of the generating matrices to perform each xor instruction, which is available on most CPUs as a machine instruction or can be implemented with a simple algorithm having a fixed low arithmetic cost. This is comparable to the cost of a LFSR (linear feedback shift register) generator per dimension. In the case of randomly shifted lattice rules the points still have to be randomly shifted before being used as quadrature points. As QMC points are naturally enumerated, it is straightforward to parallelize the solving of the different PDE problems and we therefore equip the point generators with an option to start at any offset in the enumeration of the points.
The theory in the article [KN16] covers single-level and multi-level algorithms for both the uniform and lognormal cases, and in the uniform case it considers both first order (randomly shifted lattice rules) and higher order methods (interlaced polynomial lattice rules). As the theory is often quite involved, we extract the essential properties of the analysis in the next section and allow them to be applied to any general integrand which shares similar characteristics. The table in the next section clarifies all parameters.
We consider a linear functional $G$ applied to $u^s_h(\bsx,\bsy)$ — the approximate solution of the PDE, given a specific random instance. The randomness is parametrized in the form of an infinite vector $\bsy$ which is truncated to $s$ dimensions in the computational approach. Taking $F(\bsy) = G(u_h^s(\cdot,\bsy))$ we approximate the expected value of this function through a QMC rule. The simplified view is thus the integration of an infinite-dimensional function, over $\bsy$ against some distribution, truncated to $s$ dimensions. This $s$-dimensional function is denoted by $F(\bsy)$.
We distinguish two cases:
The integral is thus either against the uniform distribution on $[-1/2,1/2]^s$ or against the standard Gaussian distribution on $\bbR^s$. The latter situation arises when generating lognormal (and normal) random fields and is thus referred to as the lognormal case. For the lognormal case the only theoretical results are by means of randomly shifted lattice rules (RLR) which then give convergence close to $O(n^{-1})$ in approximating the integral.
For the uniform case we can apply randomly shifted lattice rules (RLR), obtaining a convergence close to $O(n^{-1})$, or interlaced polynomial lattice rules (IPLR), obtaining a convergence close to $O(n^{-\alpha})$ for an interlacing factor of $\alpha$. For interlaced polynomial lattice rules this interlacing factor must be an integer greater than 1. Theoretically $\alpha$ can be as big as one wants, but we are limited by the precision of the float representation as we need $m \times \alpha$ bits to represent the points if we want $n = 2^m$. Therefore the most realistic values of $\alpha$ are probably 2 and 3.
We provide two Python scripts, lat-cbc.py (see section RLR) and polylat-cbc.py (see section IPLR) (which are in fact symbolic links to
spod-cbc.py), to construct lattice rules and interlaced polynomial
lattice rules, respectively, in which the following generalized SPOD
(smoothness driven product and order dependent) bound on
the mixed derivatives of the integrand is assumed: for all $\bsnu \in \{0,
..., \alpha\}^s$,
\begin{align}\label{eq:general-bound}\tag{1}
\left| \partial^\bsnu F (\bsy) \right|
\,\lesssim\,
\Big( (|\bsnu|+a_1)! \Big)^{d_1}
\prod_{j=1}^s (a_2 \, \Bj)^{\nu_j}\,
\exp(a_3\,\Bj|y_j|)
\,,
\end{align}
for some integers $\alpha \ge 1$ and $a_1 \ge 0$, real numbers $a_2 \gt 0$,
$a_3 \ge 0$ and $d_1 \ge 0$, and a sequence of positive numbers $\Bj$,
corresponding to the values of $b_j$, $\overline{b}_j$, $\beta_j$ or
$\overline{\beta}_j$, appropriate for the setting, see the article for
full details. For single-level algorithms the integrand is $F (\bsy) =
G(u_h^s(\cdot,\bsy))$, while for multi-level algorithms the integrand at
level $\ell$ is $F(\bsy) =
G((u_{h_\ell}^s-u_{h_{\ell-1}}^s)(\cdot,\bsy))$.
These parameters will then result into the following weights $\gamma_\setu$:
An overview of all parameters and their description for the
lat-cbc.py and the
polylat-cbc.py scripts is given in
the following table and we give examples of usage in the next two sections.
s | number of dimensions | |
m | number of points given by $2^m$
(or $p^m$ in case of optional argument p for lattice rules) | |
p | optional |
|
alpha | optional |
|
a1 | optional, defaults to 0 | integer offset for factorial, e.g., in our analysis, $a_1=0$ for single-level and $a_1=5$ for multi-level |
a2 | optional, defaults to 1 | scaling in the product (can be Python expression) e.g., in our analysis, $a_2=1$ for uniform and $a_2=1/\ln 2$ for single-level lognormal and $a_2=2$ for multi-level lognormal |
a3 | optional, defaults to 0 | boundary behaviour for the lognormal case
($a_3=0$ means we are in the uniform case) e.g., in our analysis, $a_3=1$ for single-level lognormal and $a_3=9$ for multi-level lognormal |
d1 | optional, defaults to 1 | extra power on the factorial factor ($d_1=0$ implies product weights) |
d2 | optional, defaults to 2 | decay of the $\Bj$ sequence, $d_2 \gt 1$ |
b | optional, defaults to $cj^{-d_2}$ | the $\Bj$ sequence as a Python expression, see text
(alternatively as numerical values through a file with b_file) |
c | optional, defaults to 1 | in case no b and b_file are given, $\Bj$ is set to $cj^{-d_2}$ |
b_file | optional | file name containing numerical values for the sequence $\Bj$ |
out | optional | output directory to write results to |
The summary of bounds from the analysis of the article corresponds to taking $d_1 = 1$. For randomly shifted lattice rules the order of convergence is limited to 1 and therefore $\alpha = 1$. For interlaced polynomial lattice rules we need $\alpha \ge 2$. The uniform case corresponds to taking $a_3 = 0$. The lognormal case corresponds to taking $a_3 \gt 0$, with $a_3=1$ and $a_3=9$ for the single-level and multi-level algorithms, respectively. Our analysis lead to $a_1=0$ and $a_1=5$ for the single-level and multi-level algorithms, respectively. We have $a_2 = 1$ in the uniform case, while in the lognormal case we have $a_2=1/\ln 2$ and $a_2=2$ for the single-level and multi-level algorithms, respectively. To cater for other potential integrands which satisfy the generalized bound \eqref{eq:general-bound}, our scripts can take general values of $a_1$, $a_2$, $a_3$ and $d_1$ as input.
To specify the sequence $\Bj$ the user has two main options. Either the
user provides a Python expression (with access to variables j and
v, to stand for the values of $j$ and $\nu_j$) as the argument to
command line option b, or the user provides the name of an input
file containing numerical values for each of the $\Bj$ by means of the
b_file option. (Other possibilities are available, including
a configuration file, but are not discussed here for conciseness.)
We remark that the analysis in the article takes into account the truncation from infinite dimensions to $s$ dimensions. Therefore it is essential to have an idea of the summability of the infinite sequence $\Bj$. For this, take $p_* \in (0,1)$ for which $\sum_{j=1}^\infty \Bj^{\;\,{p_*}} \lt \infty$. We would like $p_*$ to be as small as possible to obtain the fastest convergence of the integral. Asit is more convenient to work with the reciprocal value, the script expects a value $d_2 \gt 1$. This is called the “decay” of the sequence $\Bj$.
The theoretical QMC convergence rate in the context of PDE problems, with the implied constant independent of $s$, is roughly of order $n^{-\min(1,d_2-1/2)}$ for randomly shifted lattice rules, and $n^{-\min(\alpha,d_2)}$ for interlaced polynomial lattice rules with interlacing factor $\alpha\ge 2$.
The analysis in the article can be extended to handle the bound \eqref{eq:general-bound} with a general exponent $d_1$ on the factorial factor, provided that $d_2 \gt d_1$ (to ensure that the implied constant in the error estimate is bounded independently of $s$). The case $d_1 = 0$ will lead to product weights $\gamma_\setu$, in which case the CBC construction could be performed at lower cost.
A synopsis of how to call the Python script lat-cbc.py to construct
a randomly shifted lattice rule with $n=2^m$ points is
./lat-cbc.py --s={s} --m={m} [--a1={a1}] [--a2={a2}] [--a3={a3}] --d2={d2}
[--b="{bound-function}" | --b_file={file_name} | --c={c}]
In particular, the default value of $a_3 = 0$ specifies the uniform case, while any value of $a_3 \gt 0$ specifies the lognormal case.
We give some examples on how to call the script:
## uniform case, 100-dimensional rule, 2^10 points and with specified bounds b:
./lat-cbc.py --s=100 --m=10 --d2=3 --b="0.1 * j**-3 / log(j+1)"
## as above, but multi-level and with bounds from file:
./lat-cbc.py --s=100 --m=10 --a1=5 --d2=3 --b_file=bounds.txt
## lognormal case, 100-dimensional rule, 2^10 points and with algebraic decay:
./lat-cbc.py --s=100 --m=10 --a2="1/log(2)" --a3=1 --d2=3 --c=0.1
## as above, but multi-level and with bounds from file:
./lat-cbc.py --s=100 --m=10 --a1=5 --a2=2 --a3=9 --d2=3 --b_file=bounds.txt
This will produce several files in the output directory. The most
important one is z.txt which contains the generating vector. These
points then need to be randomly shifted for the theory to apply. In the
lognormal case, the randomly shifted points should be mapped to
$\bbR^s$ by applying the inverse of the cumulative normal distribution
function component-wise.
Codes are available in Python
latticeseq_b2.py, Matlab/Octave
latticeseq_b2.m, and C++
latticeseq_b2.hpp and
latticeseq_b2.cpp
to generate lattice points. An example usage in Matlab is given below:
load z.txt % load generating vector
latticeseq_b2('init0', z) % initialize the procedural generator
Pa = latticeseq_b2(20, 512); % first 512 20-dimensional points
Pb = latticeseq_b2(20, 512); % next 512 20-dimensional points
With respect to the multi-level algorithm there are two important features of these lattice rules: they are lattice sequences in terms of the number of points, and they are constructed by a component-by-component algorithm which allows a rule constructed for $s$ dimensions to be used for a lower number of dimensions. This means the construction only has to be done once for the maximum number of points $\max_{0\le\ell\le L} n_\ell$ and the maximum number of dimensions $s_L$, since the parameters in \eqref{eq:general-bound} are the same for all levels.
A synopsis of how to call the Python script polylat-cbc.py to
construct an interlaced polynomial lattice rule with $n=2^m$ and
interlacing factor $\alpha\ge 2$ is
./polylat-cbc.py --s={s} --m={m} --alpha={alpha} [--a1={a1}] [--a2={a2}]
[--b="{bound-function}" | --b_file={file_name} | --d2={d2} --c={c}]
We expect the theoretical convergence rate to be of order close to $n^{-\min(\alpha,d_2)}$.
We give some examples on how to call the script:
## 100-dimensional rule, 2^10 points, interlacing 3 and with specified bounds b:
./polylat-cbc.py --s=100 --m=10 --alpha=3 --b="0.1 * j**-3 / log(j+1)"
## as above, but multi-level and with bounds from file:
./polylat-cbc.py --s=100 --m=10 --alpha=3 --a1=5 --b_file=bounds.txt
Several files will be saved into the output directory.
The most important one is Bs.col which contains the
generating matrices of the interlaced polynomial lattice rule.
(Also the generating matrices of the non-interlaced polynomial
lattice rule are available in the file Cs.col.)
Codes are available in Python
digitalseq_b2g.py, Matlab/Octave
digitalseq_b2g.m, and C++
digitalseq_b2g.hpp and
digitalseq_b2g.cpp
to generate these points.
For interlacing to work correctly, the product $\alpha m$ should
be no more than the number of available bits. We note that the IEEE double
precision type only has 53 bits available and Matlab uses this type to do
its calculations. As a compromise (which comes with no guarantee) we load
the generating matrices truncated to 53 bits precision, which are
available in the file Bs53.col. Similarly, to cater for the
extended long double precision in C++ we provide a file Bs64.col.
For instance, with interlacing factor $4$ we can have up to $2^{13}$
points in Matlab and up to $2^{16}$ points in C++ using long double. The
Python point generator is implemented such that it can use arbitrary
precision. One can also change the C++ generator to use arbitrary
precision.
Below we give an example to illustrate how to feed the output from the
construction script into the point generator. At the same time we
experiment on the effect of truncating the generating matrices. First
construct the generating matrices using the Python script and then save
these points (in long double precision) to the file points.txt
using the C++ example program:
./polylat-cbc.py --s=10 --m=15 --alpha=4 --b="0.1 * j**-4" --out=.
./digitalseq_b2g <Bs64.col >points.txt
In this example of $2^{15}$ points with interlacing factor $4$, we need $4\times 15 = 60$ bits of precision, which can be realized in full in C++ using long double. Now we use the generating matrices (truncated to 53 bits) in Matlab, make a plot, and compare these points to the full precision points we just created on the command line:
load Bs53.col % load generating matrices
s = 10; m = 15;
digitalseq_b2g('init0', Bs53) % initialize the procedural generator
Pa = digitalseq_b2g(s, pow2(m-1)); % first half of the points
Pb = digitalseq_b2g(s, pow2(m-1)); % second half of the points
s1 = 2; s2 = 10; % pick some dimensions to show
plot(Pa(s1,:), Pa(s2,:), 'b.', Pb(s1,:), Pb(s2,:), 'r.')
axis([0 1 0 1]); axis square
load points.txt % compare with the C++ generated points in long double
points = points';
Pc = points(:,1:pow2(m-1));
Pd = points(:,pow2(m-1)+(1:pow2(m-1)));
norm(Pc - Pa) % this should be in the order of 1e-14
norm(Pd - Pb) % and this as well...
Here the effect of truncating the generating matrices appears to be empirically insignificant, but the higher order QMC convergence theory no longer applies and there is no guarantee how well they would perform in practice.
We note that since the point generators operate using generating matrices,
they can be used to generate any other digital sequence, interlaced or
not. We provide the generating matrices for an
implementation of the Sobol' sequence from
[JK08] with 21201 dimensions (as the file
sobol_Cs.col), as well as the generating matrices for interlaced
Sobol' sequences with interlacing factor $\alpha = 2,3,4,5$ (e.g.,
sobol_alpha3_Bs53.col). An example usage in Matlab to generate the
points is given below:
load sobol_alpha3_Bs53.col % load generating matrices
digitalseq_b2g('init0', sobol_alpha3_Bs53) % initialize the procedural generator
Pa = digitalseq_b2g(10, 1024); % first 1024 10-dimensional points
Pb = digitalseq_b2g(10, 1024); % next 1024 10-dimensional points