<jupyter_start><jupyter_text>QMCPy for Lebesgue IntegrationThis notebook will give examples of how to use QMCPy for integration problems that not are defined in terms of a standard measure. i.e. Uniform or Gaussian.<jupyter_code>from qmcpy import *
from numpy import *<jupyter_output><empty_output><jupyter_text>Sample Problem 1$y  = \int_{[0,2]} x^2 dx, \:\: \mbox{Lebesgue Measure}$$\phantom{y} = 2\int_{[0,2]} \frac{x^2}{2} dx, \:\: \mbox{Uniform Measure}$<jupyter_code>abs_tol = .01
dim = 1
a = 0
b = 2
true_value = 8./3
# Lebesgue Measure
distribution = IIDStdUniform(dim, seed=7)
measure = Lebesgue(distribution, lower_bound=a, upper_bound=b)
integrand = CustomFun(measure, lambda x: x**2)
solution,data = CubMCCLT(integrand, abs_tol=abs_tol).integrate()
print('y = %.3f'%solution)
print('Within tolerance: %s'%(abs((solution-true_value))<abs_tol))
# Uniform Measure
distribution = IIDStdUniform(dim, seed=7)
measure = Uniform(distribution, lower_bound=a, upper_bound=b)
integrand = CustomFun(measure, lambda x: 2*(x**2))
solution,data = CubMCCLT(integrand, abs_tol=abs_tol).integrate()
print('y = %.3f'%solution)
print('Within tolerance: %s'%(abs((solution-true_value))<abs_tol))<jupyter_output>y = 2.669
Within tolerance: True<jupyter_text>Sample Problem 2$y = \int_{[a,b]^d} ||x||_2^2 dx, \:\: \mbox{Lebesgue Measure}$$\phantom{y} = \Pi_{i=1}^d (b_i-a_i)\int_{[a,b]^d} ||x||_2^2 \; [ \Pi_{i=1}^d (b_i-a_i)]^{-1} dx, \:\: \mbox{Uniform Measure}$<jupyter_code>abs_tol = .001
dim = 2
a = array([1.,2.])
b = array([2.,4.])
true_value = ((a[0]**3-b[0]**3)*(a[1]-b[1])+(a[0]-b[0])*(a[1]**3-b[1]**3))/3
print('Answer = %.5f'%true_value)
# Lebesgue Measure
distribution = Sobol(dim, randomize=True, seed=7)
measure = Lebesgue(distribution, lower_bound=a, upper_bound=b)
integrand = CustomFun(measure, lambda x: (x**2).sum(1))
solution,data = CubQMCCLT(integrand, abs_tol=abs_tol).integrate()
print('y = %.5f'%solution)
print('Within tolerance: %s'%(abs((solution-true_value))<abs_tol))
# Uniform Measure
distribution = Sobol(dim, randomize=True, seed=17)
measure = Uniform(distribution, lower_bound=a, upper_bound=b)
integrand = CustomFun(measure, lambda x: (b-a).prod()*(x**2).sum(1))
solution,data = CubQMCCLT(integrand, abs_tol=abs_tol).integrate()
print('y = %.5f'%solution)
print('Within tolerance: %s'%(abs((solution-true_value))<abs_tol))<jupyter_output>y = 23.33333
Within tolerance: True<jupyter_text>Sample Problem 3Integral that cannot be done in terms of any standard mathematical functions$$y = \int_{[a,b]} \frac{\sin{x}}{\log{x}} dx, \:\: \mbox{Lebesgue Measure}$$Mathematica Code: `Integrate[Sin[x]/Log[x], {x,a,b}]`<jupyter_code>abs_tol = .0001
dim = 1
a = 3
b = 5
true_value = -0.87961 
# Lebesgue Measure
distribution = Lattice(dim, randomize=True, seed=7)
measure = Lebesgue(distribution, lower_bound=a, upper_bound=b)
integrand = CustomFun(measure, lambda x: sin(x)/log(x))
solution,data = CubQMCLatticeG(integrand, abs_tol=abs_tol).integrate()
print('y = %.3f'%solution)
print('Within tolerance: %s'%(abs((solution-true_value))<abs_tol))<jupyter_output>y = -0.880
Within tolerance: True<jupyter_text>Sample Problem 4Integral over $\mathbb{R}^d$$$y = \int_{\mathbb{R}^2} e^{-||x||_2^2} dx$$<jupyter_code>abs_tol = .1
dim = 2
true_value = pi
distribution = Lattice(dim)
measure = Lebesgue(distribution, lower_bound=-inf, upper_bound=inf)
integrand = CustomFun(measure, lambda x: exp(-x**2).prod(1))
solution,data = CubQMCLatticeG(integrand,abs_tol=abs_tol).integrate()
print('y = %.3f'%solution)
print('Within tolerance: %s'%(abs((solution-true_value))<abs_tol))<jupyter_output>y = 3.142
Within tolerance: True
{'fraction_non_alphanumeric': 0.1864500137703112, 'fraction_numerical': 0.029193059763150647, 'mean_word_length': 7.330275229357798, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '381064', 'license': 'Apache-2.0', 'n_tokens_mistral': 1518, 'n_tokens_neox': 1398, 'n_words': 273, 'path': 'demos/lebesgue_integration.ipynb', 'repo_name': 'kachiann/QMCSoftware', 'size': '3631'}