.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/monte_carlo/quasi_monte_carlo/plot_04_owen_scrambling.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code or to run this example in your browser via JupyterLite. .. rst-class:: sphx-glr-example-title .. _sphx_glr_api_gallery_monte_carlo_quasi_monte_carlo_plot_04_owen_scrambling.py: Owen scrambling: randomized QMC =============================== A deterministic point set gives one number and no error bar. Art Owen (1995) randomly permuted the digits of a net, nested so that the result is still a net: every scrambled point is uniform on the cube, so the estimate is unbiased, independent scramblings give an honest standard error, and for smooth integrands the root-mean-square error falls like :math:`N^{-3/2}`, faster than the unscrambled sequence's :math:`N^{-1}`. .. GENERATED FROM PYTHON SOURCE LINES 14-20 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from financialkit.monte_carlo import monte_carlo_integrate, randomized_qmc_estimate, sobol from financialkit.monte_carlo.visualizers import plot_error_convergence, plot_point_set .. GENERATED FROM PYTHON SOURCE LINES 21-23 Scrambling keeps the net structure ---------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 23-28 .. code-block:: Python fig, axes = plt.subplots(1, 3, figsize=(14, 4)) plot_point_set(sobol(64, 2, scramble=False), ax=axes[0], grid=8, title="unscrambled Sobol") plot_point_set(sobol(64, 2, rng=1), ax=axes[1], grid=8, title="Owen-scrambled Sobol") .. image-sg:: /api/gallery/monte_carlo/quasi_monte_carlo/images/sphx_glr_plot_04_owen_scrambling_001.png :alt: unscrambled Sobol, Owen-scrambled Sobol :srcset: /api/gallery/monte_carlo/quasi_monte_carlo/images/sphx_glr_plot_04_owen_scrambling_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 29-31 Error bars and convergence -------------------------- .. GENERATED FROM PYTHON SOURCE LINES 31-44 .. code-block:: Python d = 3 f = lambda u: np.prod(np.exp(u) / (np.e - 1), axis=1) # noqa: E731 integral 1 ns = 2 ** np.arange(5, 14) rqmc = [randomized_qmc_estimate(f, n, d, n_replications=16, rng=n) for n in ns] mc = [monte_carlo_integrate(f, n, d, rng=n) for n in ns] # Per-replication RMSE = standard error times sqrt(replications). plot_error_convergence(ns, [r.std_error * 4 for r in rqmc], ax=axes[2], label="scrambled Sobol (RMSE)", rates=(1.5,)) plot_error_convergence(ns, [m.std_error for m in mc], ax=axes[2], label="Monte Carlo (SE)", rates=(0.5,)) res = rqmc[-1] print(f"N = {ns[-1]} x 16 scramblings: {res.estimate:.8f} +/- {res.std_error:.1e}, exact 1") fig.tight_layout() plt.show() .. rst-class:: sphx-glr-script-out .. code-block:: none N = 8192 x 16 scramblings: 0.99999989 +/- 1.5e-07, exact 1 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.106 seconds) .. _sphx_glr_download_api_gallery_monte_carlo_quasi_monte_carlo_plot_04_owen_scrambling.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: lite-badge .. image:: images/jupyterlite_badge_logo.svg :target: ../../../../lite/lab/index.html?path=api/gallery/monte_carlo/quasi_monte_carlo/plot_04_owen_scrambling.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_04_owen_scrambling.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_04_owen_scrambling.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_04_owen_scrambling.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_