# Welcome to QMCPy¶

## Importing QMCPy¶

Here we show three different ways to import QMCPy in a Python environment. First, we can import the package qmcpy under the alias qp.

import qmcpy as qp
print(qp.name, qp.__version__)

qmcpy 1.0


Alternatively, we can import individual objects from ‘qmcpy’ as shown below.

from qmcpy.integrand import *
from qmcpy.true_measure import *
from qmcpy.discrete_distribution import *
from qmcpy.stopping_criterion import *


Lastly, we can import all objects from the package using an asterisk.

from qmcpy import *


## Important Notes¶

### IID vs LDS¶

Low discrepancy (LD) sequences such as lattice and Sobol’ are not independent like IID (independent identically distributed) points.

The code below generates 4 Sobol samples of 2 dimensions.

distribution = Lattice(dimension=2, randomize=True, seed=7)
distribution.gen_samples(n_min=0,n_max=4)

array([[0.076, 0.78 ],
[0.576, 0.28 ],
[0.326, 0.53 ],
[0.826, 0.03 ]])


### Multi-Dimensional Inputs¶

Suppose we want to create an integrand in QMCPy for evaluating the following integral:

where is the unit hypercube in . The integrand is defined everywhere except at and hence the definite integral is also defined.

The key in defining a Python function of an integrand in the QMCPy framework is that not only it should be able to take one point and return a real value, but also that it would be able to take a set of sampling points as rows in a Numpy array of size and return an array with values evaluated at each sampling point. The following examples illustrate this point.

from numpy.linalg import norm as norm
from numpy import sqrt, array


Our first attempt maybe to create the integrand as a Python function as follows:

def f(x): return norm(x) ** sqrt(norm(x))


It looks reasonable except that maybe the Numpy function norm is executed twice. It’s okay for now. Let us quickly test if the function behaves as expected at a point value:

x = 0.01
f(x)

0.6309573444801932


What about an array that represents sampling points in a two-dimensional domain, i.e., ?

x = array([[1., 0.],
[0., 0.01],
[0.04, 0.04]])
f(x)

1.001650000560437


Now, the function should have returned real values that corresponding to each of the sampling points. Let’s debug our Python function.

norm(x)

1.0016486409914407


Numpy’s norm(x) is obviously a matrix norm, but we want it to be vector 2-norm that acts on each row of x. To that end, let’s add an axis argument to the function:

norm(x, axis = 1)

array([1.   , 0.01 , 0.057])


Now it’s working! Let’s make sure that the sqrt function is acting on each element of the vector norm results:

sqrt(norm(x, axis = 1))

array([1.   , 0.1  , 0.238])


It is. Putting everything together, we have:

norm(x, axis = 1) ** sqrt(norm(x, axis = 1))

array([1.   , 0.631, 0.505])


We have got our proper function definition now.

def myfunc(x):
x_norms = norm(x, axis = 1)
return x_norms ** sqrt(x_norms)


We can now create an integrand instance with our QuickConstruct class in QMCPy and then invoke QMCPy’s integrate function:

dim = 1
abs_tol = .01
integrand = CustomFun(
true_measure = Uniform(IIDStdUniform(dimension=dim, seed=7)),
g=myfunc)
solution,data = CubMCCLT(integrand,abs_tol=abs_tol,rel_tol=0).integrate()
print(data)

Solution: 0.6571
CustomFun (Integrand Object)
IIDStdUniform (DiscreteDistribution Object)
d               1
seed            7
mimics          StdUniform
Uniform (TrueMeasure Object)
lower_bound     0
upper_bound     1
CubMCCLT (StoppingCriterion Object)
inflate         1.200
alpha           0.010
abs_tol         0.010
rel_tol         0
n_init          2^(10)
n_max           10000000000
MeanVarData (AccumulateData Object)
levels          1
solution        0.657
n               3359
n_total         4383
error_bound     0.010
confid_int      [0.647 0.667]
time_integrate  0.002


For our integral, we know the true value. Let’s check if QMCPy’s solution is accurate enough:

true_sol = 0.658582  # In WolframAlpha: Integral[x**Sqrt[x], {x,0,1}]
abs_tol = data.stopping_crit.abs_tol
qmcpy_error = abs(true_sol - solution)
if qmcpy_error > abs_tol: raise Exception("Error not within bounds")


It’s good. Shall we test the function with by simply changing the input parameter value of dimension for QuickConstruct?

dim = 2
integrand = CustomFun(
true_measure = Uniform(IIDStdUniform(dimension=dim, seed=7)),
g = myfunc)
solution2,data2 = CubMCCLT(integrand,abs_tol=abs_tol,rel_tol=0).integrate()
print(data2)

Solution: 0.8264
CustomFun (Integrand Object)
IIDStdUniform (DiscreteDistribution Object)
d               2^(1)
seed            7
mimics          StdUniform
Uniform (TrueMeasure Object)
lower_bound     0
upper_bound     1
CubMCCLT (StoppingCriterion Object)
inflate         1.200
alpha           0.010
abs_tol         0.010
rel_tol         0
n_init          2^(10)
n_max           10000000000
MeanVarData (AccumulateData Object)
levels          1
solution        0.826
n               5455
n_total         6479
error_bound     0.010
confid_int      [0.816 0.836]
time_integrate  0.003


Once again, we could test for accuracy of QMCPy with respect to the true value:

true_sol2 = 0.827606  # In WolframAlpha: Integral[Sqrt[x**2+y**2])**Sqrt[Sqrt[x**2+y**2]], {x,0,1}, {y,0,1}]
abs_tol2 = data2.stopping_crit.abs_tol
qmcpy_error2 = abs(true_sol2 - solution2)
if qmcpy_error2 > abs_tol2: raise Exception("Error not within bounds")