.. 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_exponential.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_exponential.py: Exponential function (3 random inputs, scalar output) ====================================================================== In this example, PCE is used to generate a surrogate model for a given set of 3D data. Dette & Pepelyshev exponential function ---------------------------------------- .. math:: f(x) = 100(\exp{(-2/x_1^{1.75})} + \exp{(-2/x_2^{1.5})} + \exp{(-2/x_3^{1.25})}) **Description:** Dimensions: 3 **Input Domain:** This function is evaluated on the hypercube :math:`x_i \in [0,1]` for all :math:`i = 1,2,3`. **Reference:** Dette, H., & Pepelyshev, A. (2010). Generalized Latin hypercube design for computer experiments. Technometrics, 52(4). .. GENERATED FROM PYTHON SOURCE LINES 21-22 Import necessary libraries. .. GENERATED FROM PYTHON SOURCE LINES 25-32 .. code-block:: default import math import numpy as np import matplotlib.pyplot as plt from UQpy.distributions import Uniform, JointIndependent from UQpy.surrogates import * .. GENERATED FROM PYTHON SOURCE LINES 33-34 Define the function. .. GENERATED FROM PYTHON SOURCE LINES 37-41 .. code-block:: default def function(x): return 100*(np.exp(-2/(x[:,0]**1.75)) + np.exp(-2/(x[:,1]**1.5)) + np.exp(-2/(x[:,2]**1.25))) .. GENERATED FROM PYTHON SOURCE LINES 42-43 Define the input probability distributions. .. GENERATED FROM PYTHON SOURCE LINES 46-52 .. code-block:: default # input distributions dist = Uniform(loc=0, scale=1) marg = [dist]*3 joint = JointIndependent(marginals=marg) .. GENERATED FROM PYTHON SOURCE LINES 53-54 Compute reference mean and variance values using Monte Carlo sampling. .. GENERATED FROM PYTHON SOURCE LINES 57-65 .. code-block:: default # reference moments via Monte Carlo Sampling n_samples_mc = 1000000 xx = joint.rvs(n_samples_mc) yy = function(xx) mean_ref = yy.mean() var_ref = yy.var() .. GENERATED FROM PYTHON SOURCE LINES 66-67 Create validation data sets, to be used later to estimate the accuracy of the PCE. .. GENERATED FROM PYTHON SOURCE LINES 70-77 .. code-block:: default # validation data sets n_samples_val = 1000 xx_val = joint.rvs(n_samples_val) yy_val = function(xx_val) .. GENERATED FROM PYTHON SOURCE LINES 78-79 Assess the PCE in terms of approximation and moment estimation accuracy, for increasing maximum polynomial degree. .. GENERATED FROM PYTHON SOURCE LINES 82-124 .. code-block:: default # construct PCE surrogate models l2_err = [] mean_err = [] var_err = [] for max_degree in range(1, 10): print(' ') # PCE basis print('Total degree: ', max_degree) polynomial_basis = TotalDegreeBasis(joint, max_degree) print('Size of basis:', polynomial_basis.polynomials_number) # generate training data sampling_coeff = 5 print('Sampling coefficient: ', sampling_coeff) np.random.seed(42) n_samples = math.ceil(sampling_coeff * polynomial_basis.polynomials_number) print('Training data: ', n_samples) xx_train = joint.rvs(n_samples) yy_train = function(xx_train) # fit model least_squares = LeastSquareRegression() pce_metamodel = PolynomialChaosExpansion(polynomial_basis=polynomial_basis, regression_method=least_squares) pce_metamodel.fit(xx_train, yy_train) # validation errors yy_val_pce = pce_metamodel.predict(xx_val) errors = np.abs(yy_val - yy_val_pce.flatten()) error_l2 = np.linalg.norm(errors) l2_err.append(error_l2) print('Validation error in the l2 norm:', error_l2) # moment errors pce_moments = pce_metamodel.get_moments() mean_pce = pce_moments[0] var_pce = pce_moments[1] mean_err.append(np.abs((mean_pce - mean_ref) / mean_ref)) var_err.append(np.abs((var_pce - var_ref) / var_ref)) print('Relative error in the mean:', mean_err[-1]) print('Relative error in the variance:', var_err[-1]) .. rst-class:: sphx-glr-script-out .. code-block:: none Total degree: 1 Size of basis: 4 Sampling coefficient: 5 Training data: 20 Validation error in the l2 norm: 129.20598047202918 Relative error in the mean: 0.06718582089180117 Relative error in the variance: 0.19464294065482993 Total degree: 2 Size of basis: 10 Sampling coefficient: 5 Training data: 50 Validation error in the l2 norm: 32.491868013742526 Relative error in the mean: 0.005657152666309865 Relative error in the variance: 0.021441707594204832 Total degree: 3 Size of basis: 20 Sampling coefficient: 5 Training data: 100 Validation error in the l2 norm: 11.203232601483554 Relative error in the mean: 0.0037013939722399386 Relative error in the variance: 0.028353176125452285 Total degree: 4 Size of basis: 35 Sampling coefficient: 5 Training data: 175 Validation error in the l2 norm: 6.491941276358539 Relative error in the mean: 0.001343264325296654 Relative error in the variance: 0.0028847488197525036 Total degree: 5 Size of basis: 56 Sampling coefficient: 5 Training data: 280 Validation error in the l2 norm: 2.109018991714873 Relative error in the mean: 0.0020302227928515534 Relative error in the variance: 0.0030831698249971394 Total degree: 6 Size of basis: 84 Sampling coefficient: 5 Training data: 420 Validation error in the l2 norm: 1.51701246194853 Relative error in the mean: 0.0014108145347999925 Relative error in the variance: 0.004548486290468092 Total degree: 7 Size of basis: 120 Sampling coefficient: 5 Training data: 600 Validation error in the l2 norm: 0.6558976196693772 Relative error in the mean: 0.0011112582388267986 Relative error in the variance: 0.0015059072771149253 Total degree: 8 Size of basis: 165 Sampling coefficient: 5 Training data: 825 Validation error in the l2 norm: 0.3622570432474069 Relative error in the mean: 0.0012297780921141103 Relative error in the variance: 0.0021545930796069593 Total degree: 9 Size of basis: 220 Sampling coefficient: 5 Training data: 1100 Validation error in the l2 norm: 0.14772577969491651 Relative error in the mean: 0.0011826721179544438 Relative error in the variance: 0.0022894928914782933 .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.184 seconds) .. _sphx_glr_download_auto_examples_surrogates_pce_plot_pce_exponential.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_exponential.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_pce_exponential.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_pce_exponential.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_