.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_sparse_grid_uq.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 .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_plot_sparse_grid_uq.py: Sparse Grid Interpolation ========================= We will use the Genz benchmark to illustrate how to use a sparse grid as a surrogate for uncertainty quantification and sensitivity analysis First we must load the benchmark .. GENERATED FROM PYTHON SOURCE LINES 10-24 .. code-block:: default import numpy as np from pyapprox.benchmarks import setup_benchmark from pyapprox import surrogates from pyapprox.analysis import visualize from pyapprox import util import matplotlib.pyplot as plt benchmark = setup_benchmark('genz', nvars=2, test_name="oscillatory") np.random.seed(1) #We can set a maximum number of samples using the options dictionary options = {"max_nsamples": 100, "verbose": 0} approx = surrogates.adaptive_approximate( benchmark.fun, benchmark.variable, "sparse_grid", options).approx .. GENERATED FROM PYTHON SOURCE LINES 25-26 Plot the sparse grid samples with .. GENERATED FROM PYTHON SOURCE LINES 26-30 .. code-block:: default sparse_grid_samples = approx.get_samples() util.plot_2d_samples(sparse_grid_samples, marker='o') plt.show() .. image-sg:: /auto_examples/images/sphx_glr_plot_sparse_grid_uq_001.png :alt: plot sparse grid uq :srcset: /auto_examples/images/sphx_glr_plot_sparse_grid_uq_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 31-32 We can estimate the error in the surrogate using some validation samples. .. GENERATED FROM PYTHON SOURCE LINES 32-39 .. code-block:: default validation_samples = benchmark.variable.rvs(100) validation_values = benchmark.fun(validation_samples) approx_values = approx(validation_samples) error = np.linalg.norm(validation_values-approx_values, axis=0)/np.sqrt( validation_values.shape[0]) print(f"The RMSE error is {error}") .. rst-class:: sphx-glr-script-out .. code-block:: none The RMSE error is [4.6830029e-16] .. GENERATED FROM PYTHON SOURCE LINES 40-42 and building a kernel density estimator. Lets first just plot the marginal PDFs of the output .. GENERATED FROM PYTHON SOURCE LINES 42-46 .. code-block:: default surrogate_samples = benchmark.variable.rvs(10000) approx_values = approx(surrogate_samples) visualize.plot_qoi_marginals(approx_values) plt.show() .. image-sg:: /auto_examples/images/sphx_glr_plot_sparse_grid_uq_002.png :alt: plot sparse grid uq :srcset: /auto_examples/images/sphx_glr_plot_sparse_grid_uq_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.129 seconds) .. _sphx_glr_download_auto_examples_plot_sparse_grid_uq.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_sparse_grid_uq.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_sparse_grid_uq.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_