.. 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_sphere.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_sphere.py: Sinusoidal Function Sphere function (2 random inputs, scalar output) ====================================================================== In this example, PCE is used to generate a surrogate model for a given set of 2D data. .. math:: f(x) = x_1^2 + x_2^2 **Description:** Dimensions: 2 **Input Domain:** This function is evaluated on the hypercube :math:`x_i \in [-5.12, 5.12]` for all :math:`i = 1,2`. **Global minimum:** :math:`f(x^*)=0,` at :math:`x^* = (0,0)`. **Reference:** Dixon, L. C. W., & Szego, G. P. (1978). The global optimization problem: an introduction. Towards global optimization, 2, 1-15. .. GENERATED FROM PYTHON SOURCE LINES 20-21 Import necessary libraries. .. GENERATED FROM PYTHON SOURCE LINES 24-33 .. code-block:: default import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D from matplotlib import cm from matplotlib.ticker import LinearLocator, FormatStrFormatter from UQpy.surrogates import * from UQpy.distributions import Uniform, JointIndependent .. GENERATED FROM PYTHON SOURCE LINES 34-35 Define the function. .. GENERATED FROM PYTHON SOURCE LINES 38-42 .. code-block:: default def function(x,y): return x**2 + y**2 .. GENERATED FROM PYTHON SOURCE LINES 43-44 Create a distribution object, generate samples and evaluate the function at the samples. .. GENERATED FROM PYTHON SOURCE LINES 47-60 .. code-block:: default np.random.seed(1) dist_1 = Uniform(loc=-5.12, scale=10.24) dist_2 = Uniform(loc=-5.12, scale=10.24) marg = [dist_1, dist_2] joint = JointIndependent(marginals=marg) n_samples = 100 x = joint.rvs(n_samples) y = function(x[:,0], x[:,1]) .. GENERATED FROM PYTHON SOURCE LINES 61-62 Visualize the 2D function. .. GENERATED FROM PYTHON SOURCE LINES 65-86 .. code-block:: default xmin, xmax = -6,6 ymin, ymax = -6,6 X1 = np.linspace(xmin, xmax, 50) X2 = np.linspace(ymin, ymax, 50) X1_, X2_ = np.meshgrid(X1, X2) # grid of points f = function(X1_, X2_) fig = plt.figure(figsize=(10,6)) ax = fig.add_subplot(projection='3d') surf = ax.plot_surface(X1_, X2_, f, rstride=1, cstride=1, cmap='gnuplot2', linewidth=0, antialiased=False) ax.set_title('True function') ax.set_xlabel('$x_1$', fontsize=15) ax.set_ylabel('$x_2$', fontsize=15) ax.zaxis.set_major_locator(LinearLocator(10)) ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f')) ax.view_init(20, 140) fig.colorbar(surf, shrink=0.5, aspect=7) plt.show() .. image-sg:: /auto_examples/surrogates/pce/images/sphx_glr_plot_pce_sphere_001.png :alt: True function :srcset: /auto_examples/surrogates/pce/images/sphx_glr_plot_pce_sphere_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 87-88 Visualize training data. .. GENERATED FROM PYTHON SOURCE LINES 91-104 .. code-block:: default fig = plt.figure(figsize=(10,6)) ax = fig.add_subplot(projection='3d') ax.scatter(x[:,0], x[:,1], y, s=20, c='r') ax.set_title('Training data') ax.zaxis.set_major_locator(LinearLocator(10)) ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f')) ax.view_init(20,140) ax.set_xlabel('$x_1$', fontsize=15) ax.set_ylabel('$x_2$', fontsize=15) plt.show() .. image-sg:: /auto_examples/surrogates/pce/images/sphx_glr_plot_pce_sphere_002.png :alt: Training data :srcset: /auto_examples/surrogates/pce/images/sphx_glr_plot_pce_sphere_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 105-106 Create an object from the PCE class. Compute PCE coefficients using least squares regression. .. GENERATED FROM PYTHON SOURCE LINES 109-117 .. 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 118-119 Compute PCE coefficients using LASSO. .. GENERATED FROM PYTHON SOURCE LINES 122-129 .. 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 130-131 Compute PCE coefficients with Ridge regression. .. GENERATED FROM PYTHON SOURCE LINES 134-141 .. 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 142-143 PCE surrogate is used to predict the behavior of the function at new samples. .. GENERATED FROM PYTHON SOURCE LINES 146-152 .. code-block:: default n_test_samples = 10000 x_test = joint.rvs(n_test_samples) y_test = pce.predict(x_test) .. GENERATED FROM PYTHON SOURCE LINES 153-154 Plot PCE prediction. .. GENERATED FROM PYTHON SOURCE LINES 157-172 .. code-block:: default fig = plt.figure(figsize=(10,6)) ax = fig.add_subplot(projection='3d') ax.scatter(x_test[:,0], x_test[:,1], y_test, s=1) ax.set_title('PCE predictor') ax.zaxis.set_major_locator(LinearLocator(10)) ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f')) ax.view_init(20,140) ax.set_xlim(-6,6) ax.set_ylim(-6,6) ax.set_xlabel('$x_1$', fontsize=15) ax.set_ylabel('$x_2$', fontsize=15) plt.show() .. image-sg:: /auto_examples/surrogates/pce/images/sphx_glr_plot_pce_sphere_003.png :alt: PCE predictor :srcset: /auto_examples/surrogates/pce/images/sphx_glr_plot_pce_sphere_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 173-176 Error Estimation ----------------- Construct a validation dataset and get the validation error. .. GENERATED FROM PYTHON SOURCE LINES 178-198 .. code-block:: default # validation sample n_samples = 150 x_val = joint.rvs(n_samples) y_val = function(x_val[:,0], x_val[:,1]) # PCE predictions y_pce = pce.predict(x_val).flatten() y_pce2 = pce2.predict(x_val).flatten() y_pce3 = pce3.predict(x_val).flatten() # mean relative validation errors error = np.sum(np.abs((y_val - y_pce)/y_val))/n_samples error2 = np.sum(np.abs((y_val - y_pce2)/y_val))/n_samples error3 = np.sum(np.abs((y_val - y_pce3)/y_val))/n_samples print('Mean rel. error, LSTSQ:', error) print('Mean rel. error, LASSO:', error2) print('Mean rel. error, Ridge:', error3) .. rst-class:: sphx-glr-script-out .. code-block:: none Mean rel. error, LSTSQ: 3.418671019158622e-15 Mean rel. error, LASSO: 0.0010814080466504871 Mean rel. error, Ridge: 0.022999347585135183 .. GENERATED FROM PYTHON SOURCE LINES 199-202 Moment Estimation ----------------- Returns mean and variance of the PCE surrogate. .. GENERATED FROM PYTHON SOURCE LINES 204-214 .. code-block:: default n_mc = 1000000 x_mc = joint.rvs(n_mc) y_mc = function(x_mc[:,0], x_mc[:,1]) 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 Monte Carlo integration: ', mean_mc, var_mc) .. rst-class:: sphx-glr-script-out .. code-block:: none Moments from least squares regression : (17.47626666666666, 122.16795864177791) Moments from LASSO regression : (17.475033992047372, 122.01572481219833) Moments from Ridge regression : (17.44278392807492, 118.87517009983924) Moments from Monte Carlo integration: 17.479457628244443 122.28294295715122 .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.463 seconds) .. _sphx_glr_download_auto_examples_surrogates_pce_plot_pce_sphere.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_sphere.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_pce_sphere.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_pce_sphere.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_