.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/surrogates/pce/plot_pce_friedman.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 Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_surrogates_pce_plot_pce_friedman.py: Friedman function (5 random inputs, scalar output) ====================================================================== In this example, PCE is used to generate a surrogate model for a given set of 5D data. Friedman function ---------------------------------------- .. math:: f(x) = 10 sin(\pi x_1x_2) + 20(x_3 - 0.5)^2 + 10x_4 + 5x_5 **Description:** Dimensions: 5 **Input Domain:** This function is evaluated on the hypercube :math:`x_i \in [0,1]` for all i = 1, …, 5. **Reference:** Friedman, J. H. (1991). Multivariate adaptive regression splines. The annals of statistics, 19(1), 1-67. .. GENERATED FROM PYTHON SOURCE LINES 21-22 Import necessary libraries. .. GENERATED FROM PYTHON SOURCE LINES 25-31 .. code-block:: default import numpy as np import matplotlib.pyplot as plt from UQpy.distributions import Uniform, JointIndependent from UQpy.surrogates import * .. GENERATED FROM PYTHON SOURCE LINES 32-33 Define the function. .. GENERATED FROM PYTHON SOURCE LINES 36-40 .. code-block:: default def function(x): return 10*np.sin(np.pi*x[:,0]*x[:,1]) + 20*(x[:,2]-0.5)**2 + 10*x[:,3] + 5*x[:,4] .. GENERATED FROM PYTHON SOURCE LINES 41-42 Create a distribution object, generate samples and evaluate the function at the samples. .. GENERATED FROM PYTHON SOURCE LINES 45-57 .. code-block:: default np.random.seed(1) dist = Uniform(loc=0, scale=1) marg = [dist]*5 joint = JointIndependent(marginals=marg) n_samples = 200 x = joint.rvs(n_samples) y = function(x) .. GENERATED FROM PYTHON SOURCE LINES 58-59 Create an object from the PCE class. Then compute PCE coefficients using least squares regression. .. GENERATED FROM PYTHON SOURCE LINES 62-70 .. code-block:: default max_degree = 3 polynomial_basis = TotalDegreeBasis(joint, max_degree) least_squares = LeastSquareRegression() pce = PolynomialChaosExpansion(polynomial_basis=polynomial_basis, regression_method=least_squares) pce.fit(x,y) .. GENERATED FROM PYTHON SOURCE LINES 71-72 Compute PCE coefficients using Lasso regression. .. GENERATED FROM PYTHON SOURCE LINES 75-81 .. code-block:: default polynomial_basis = TotalDegreeBasis(joint, max_degree) lasso = LassoRegression() pce2 = PolynomialChaosExpansion(polynomial_basis=polynomial_basis, regression_method=lasso) pce2.fit(x,y) .. GENERATED FROM PYTHON SOURCE LINES 82-83 Compute PCE coefficients using Ridge regression. .. GENERATED FROM PYTHON SOURCE LINES 86-92 .. code-block:: default polynomial_basis = TotalDegreeBasis(joint, max_degree) ridge = RidgeRegression() pce3 = PolynomialChaosExpansion(polynomial_basis=polynomial_basis, regression_method=ridge) pce3.fit(x,y) .. GENERATED FROM PYTHON SOURCE LINES 93-96 Error Estimation ----------------- Validation error. .. GENERATED FROM PYTHON SOURCE LINES 98-115 .. code-block:: default n_samples = 100 x_val = joint.rvs(n_samples) y_val = function(x_val) y_pce = pce.predict(x_val).flatten() y_pce2 = pce2.predict(x_val).flatten() y_pce3 = pce3.predict(x_val).flatten() error = np.sum(np.abs(y_pce - y_val)/np.abs(y_val))/n_samples error2 = np.sum(np.abs(y_pce2 - y_val)/np.abs(y_val))/n_samples error3 = np.sum(np.abs(y_pce3 - y_val)/np.abs(y_val))/n_samples print('Validation error, LSTSQ-PCE:', error) print('Validation error, LASSO-PCE:', error2) print('Validation error, Ridge-PCE:', error3) .. rst-class:: sphx-glr-script-out .. code-block:: none Validation error, LSTSQ-PCE: 0.022952373223257647 Validation error, LASSO-PCE: 0.022197332426174762 Validation error, Ridge-PCE: 0.024639877369417387 .. GENERATED FROM PYTHON SOURCE LINES 116-119 Moment Estimation ----------------- Returns mean and variance of the PCE surrogate. .. GENERATED FROM PYTHON SOURCE LINES 121-131 .. code-block:: default n_mc = 1000000 x_mc = joint.rvs(n_mc) y_mc = function(x_mc) mean_mc = np.mean(y_mc) var_mc = np.var(y_mc) print('Moments from least squares regression :', pce.get_moments()) print('Moments from LASSO regression :', pce2.get_moments()) print('Moments from Ridge regression :', pce3.get_moments()) print('Moments from MC integration: ', mean_mc, var_mc) .. rst-class:: sphx-glr-script-out .. code-block:: none Moments from least squares regression : (14.386271292839744, 23.80629215684483) Moments from LASSO regression : (14.384776376629697, 23.63639382487195) Moments from Ridge regression : (14.365579523819942, 23.38819865240679) Moments from MC integration: 14.414000929081066 23.852139481345674 .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.455 seconds) .. _sphx_glr_download_auto_examples_surrogates_pce_plot_pce_friedman.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/SURGroup/UQpy/master?urlpath=lab/tree/notebooks/auto_examples/surrogates/pce/plot_pce_friedman.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_pce_friedman.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_pce_friedman.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_