.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_gaussian_quadrature.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_gaussian_quadrature.py: Univariate Gaussian Quadrature ------------------------------ Univariate Gaussian quadrature can be used to efficiently integrate smooth one-dimensional functions. While numpy supports Hermite and Legendre Gaussian qurature, Pyapprox can generate Gaussian quadrature rules for any continouous random variable implemented in scipy.stats. To generate a univariate quadrature rule for uniform random variables .. GENERATED FROM PYTHON SOURCE LINES 11-20 .. code-block:: default from pyapprox import surrogates from scipy import stats degree = 10 scipy_var = stats.uniform(-1, 2) quad_rule = surrogates.get_gauss_quadrature_rule_from_marginal( scipy_var, degree+1) x_quad, w_quad = quad_rule(5) .. GENERATED FROM PYTHON SOURCE LINES 21-23 As an example, we can use this quadrature rule to integrate :math:`\rv^2` with repsect to the uniform PDF on [-1, 1], i.e. 1/2 .. GENERATED FROM PYTHON SOURCE LINES 23-27 .. code-block:: default values = x_quad**2 integral = values.dot(w_quad) print(integral) .. rst-class:: sphx-glr-script-out .. code-block:: none 0.3333333333333332 .. GENERATED FROM PYTHON SOURCE LINES 28-31 The quadrature recovers the integral value of 1/3 to machine precision. Note unlike :class:`~numpy.polynomial.legendre.leggauss` we are integrating with resepect to the uniform PDF. .. GENERATED FROM PYTHON SOURCE LINES 33-35 The function is also capable of generating rules on different intervals For example .. GENERATED FROM PYTHON SOURCE LINES 35-42 .. code-block:: default scipy_var = stats.uniform(0, 2) x_quad, w_quad = surrogates.get_gauss_quadrature_rule_from_marginal( scipy_var, degree+1)(degree) values = x_quad**2 integral = values.dot(w_quad) print(integral) .. rst-class:: sphx-glr-script-out .. code-block:: none 1.3333333333333346 .. GENERATED FROM PYTHON SOURCE LINES 43-45 Quadrature rules can be created for almost any random variable. Here we will generate a quadrature rule for an exponential random variable .. GENERATED FROM PYTHON SOURCE LINES 45-52 .. code-block:: default scipy_var = stats.expon() x_quad, w_quad = surrogates.get_gauss_quadrature_rule_from_marginal( scipy_var, degree+1)(degree) values = x_quad**2 integral = values.dot(w_quad) print(integral) .. rst-class:: sphx-glr-script-out .. code-block:: none 2.000000000000001 .. GENERATED FROM PYTHON SOURCE LINES 53-55 For interest, we plot the quadrature rule against the PDF of the exponential variable .. GENERATED FROM PYTHON SOURCE LINES 55-64 .. code-block:: default import numpy as np import matplotlib.pyplot as plt from pyapprox.analysis import visualize from pyapprox.variables import marginals visualize.plot_discrete_measure_1d(x_quad, w_quad) vrange = marginals.get_truncated_range(scipy_var, 1-1e-6) xx = np.linspace(vrange[0], vrange[1], 101) plt.fill_between(xx, 0*xx, scipy_var.pdf(xx), alpha=0.3) plt.show() .. image-sg:: /auto_examples/images/sphx_glr_plot_gaussian_quadrature_001.png :alt: plot gaussian quadrature :srcset: /auto_examples/images/sphx_glr_plot_gaussian_quadrature_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.098 seconds) .. _sphx_glr_download_auto_examples_plot_gaussian_quadrature.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_gaussian_quadrature.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_gaussian_quadrature.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_