Computing integral with quasi-Monte Carlo methods
Contents
Computing integral with quasi-Monte Carlo methods¶
Randall Romero Aguilar, PhD
This demo is based on the original Matlab demo accompanying the Computational Economics and Finance 2001 textbook by Mario Miranda and Paul Fackler.
Original (Matlab) CompEcon file: demqua01bis.m
Running this file requires the Python version of CompEcon. This can be installed with pip by running
!pip install compecon --upgrade
Last updated: 2021-Oct-01
About¶
To seven significant digits,
\[\begin{align*}
A &=\int_{-1}^1\int_{-1}^1 e^{-x_1}\cos^2(x_2)dx _1dx_2\\
&=\int_{-1}^1 e^{-x_1} dx _1 \times \int_{-1}^1 \cos^2(x_2) dx_2\\
&=\left(e - \tfrac{1}{e}\right) \times \left(1+\tfrac{1}{2}\sin(2)\right)\\
&\approx 3.4190098
\end{align*}\]
Initial tasks¶
import numpy as np
from compecon import qnwequi
import pandas as pd
Make support function¶
f1 = lambda x1: np.exp(-x1)
f2 = lambda x2: np.cos(x2)**2
f = lambda x1, x2: f1(x1) * f2(x2)
def quad(method, n):
(x1, x2), w = qnwequi(n,[-1, -1], [1, 1],method)
return w.dot(f(x1, x2))
Compute the approximation errors¶
nlist = range(3,7)
quadmethods = ['Random', 'Neiderreiter','Weyl']
f_quad = np.array([[quad(qnw[0], 10**ni) for qnw in quadmethods] for ni in nlist])
f_true = (np.exp(1) - np.exp(-1)) * (1+0.5*np.sin(2))
f_error = np.log10(np.abs(f_quad/f_true - 1))
Make table with results¶
results = pd.DataFrame(f_error, columns=quadmethods)
results['Nodes'] = ['$10^%d$' % n for n in nlist]
results.set_index('Nodes', inplace=True)
results
Random | Neiderreiter | Weyl | |
---|---|---|---|
Nodes | |||
$10^3$ | -1.633600 | -2.879952 | -3.518213 |
$10^4$ | -3.260970 | -3.148008 | -3.929531 |
$10^5$ | -3.063893 | -3.982252 | -4.377373 |
$10^6$ | -3.128315 | -5.449389 | -5.740493 |
#results.to_latex('demqua01bis.tex', escape=False, float_format='%.1f')