Note
Go to the end to download the full example code or to run this example in your browser via Binder
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
Description: Dimensions: 3
Input Domain: This function is evaluated on the hypercube \(x_i \in [0,1]\) for all \(i = 1,2,3\).
Reference: Dette, H., & Pepelyshev, A. (2010). Generalized Latin hypercube design for computer experiments. Technometrics, 52(4).
Import necessary libraries.
import math
import numpy as np
import matplotlib.pyplot as plt
from UQpy.distributions import Uniform, JointIndependent
from UQpy.surrogates import *
Define the function.
Define the input probability distributions.
# input distributions
dist = Uniform(loc=0, scale=1)
marg = [dist]*3
joint = JointIndependent(marginals=marg)
Compute reference mean and variance values using Monte Carlo sampling.
# 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()
Create validation data sets, to be used later to estimate the accuracy of the PCE.
# validation data sets
n_samples_val = 1000
xx_val = joint.rvs(n_samples_val)
yy_val = function(xx_val)
Assess the PCE in terms of approximation and moment estimation accuracy, for increasing maximum polynomial degree.
# 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])
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
Total running time of the script: ( 0 minutes 0.183 seconds)