.. 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_camel.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_camel.py: Camel function (3 random inputs, scalar output) ====================================================================== In this example, PCE is used to generate a surrogate model for a given set of 2D data. Six-hump camel function .. math:: f(x) = \Big(4-2.1x_1^2 + rac{x_1^4}{3} \Big)x_1^2 + x_1x_2 + (-4 + 4x_2^2)x_2^2 **Description:** Dimensions: 2 **Input Domain:** This function is evaluated on the hypercube :math:`x_1 \in [-3, 3], x_2 \in [-2, 2]`. **Global minimum:** :math:`f(x^*)=-1.0316,` at :math:`x^* = (0.0898, -0.7126)` and :math:`(-0.0898, 0.7126)`. **Reference:** Molga, M., & Smutnicki, C. Test functions for optimization needs (2005). Retrieved June 2013, from http://www.zsd.ict.pwr.wroc.pl/files/docs/functions.pdf. .. GENERATED FROM PYTHON SOURCE LINES 24-25 Import necessary libraries. .. GENERATED FROM PYTHON SOURCE LINES 28-38 .. code-block:: default import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D from matplotlib import cm from UQpy.distributions import Uniform, JointIndependent from matplotlib.ticker import LinearLocator, FormatStrFormatter from UQpy.surrogates import * .. GENERATED FROM PYTHON SOURCE LINES 39-40 Define the function. .. GENERATED FROM PYTHON SOURCE LINES 43-48 .. code-block:: default def function(x, y): return (4 - 2.1 * x ** 2 + x ** 4 / 3) * x ** 2 + x * y + (-4 + 4 * y ** 2) * y ** 2 .. GENERATED FROM PYTHON SOURCE LINES 49-50 Create a distribution object, generate samples and evaluate the function at the samples. .. GENERATED FROM PYTHON SOURCE LINES 53-66 .. code-block:: default np.random.seed(1) dist_1 = Uniform(loc=-2, scale=4) dist_2 = Uniform(loc=-1, scale=2) marg = [dist_1, dist_2] joint = JointIndependent(marginals=marg) n_samples = 250 x = joint.rvs(n_samples) y = function(x[:, 0], x[:, 1]) .. GENERATED FROM PYTHON SOURCE LINES 67-68 Visualize the 2D function. .. GENERATED FROM PYTHON SOURCE LINES 71-92 .. code-block:: default xmin, xmax = -2, 2 ymin, ymax = -1, 1 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, 50) fig.colorbar(surf, shrink=0.5, aspect=7) plt.show() .. image-sg:: /auto_examples/surrogates/pce/images/sphx_glr_plot_pce_camel_001.png :alt: True function :srcset: /auto_examples/surrogates/pce/images/sphx_glr_plot_pce_camel_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 93-94 Visualize training data. .. GENERATED FROM PYTHON SOURCE LINES 97-110 .. 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, 50) 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_camel_002.png :alt: Training data :srcset: /auto_examples/surrogates/pce/images/sphx_glr_plot_pce_camel_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 111-112 Visualize training data. .. GENERATED FROM PYTHON SOURCE LINES 115-118 .. code-block:: default max_degree = 6 .. GENERATED FROM PYTHON SOURCE LINES 119-120 Compute a PCE using least squares regression. .. GENERATED FROM PYTHON SOURCE LINES 123-131 .. code-block:: default 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 132-133 Compute PCE using LASSO regression. .. GENERATED FROM PYTHON SOURCE LINES 136-143 .. 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 144-145 Compute PCE using Ridge regression. .. GENERATED FROM PYTHON SOURCE LINES 148-155 .. 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 156-157 PCE surrogate is used to predict the behavior of the function at new samples. .. GENERATED FROM PYTHON SOURCE LINES 160-165 .. code-block:: default n_test_samples = 20000 x_test = joint.rvs(n_test_samples) y_test = pce.predict(x_test) .. GENERATED FROM PYTHON SOURCE LINES 166-167 Plot PCE prediction. .. GENERATED FROM PYTHON SOURCE LINES 170-185 .. 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,50) ax.set_xlim(-2,2) ax.set_ylim(-1,1) 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_camel_003.png :alt: PCE predictor :srcset: /auto_examples/surrogates/pce/images/sphx_glr_plot_pce_camel_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 186-189 Error Estimation ----------------- Validation error. .. GENERATED FROM PYTHON SOURCE LINES 192-212 .. 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: 6.6383407624691066e-15 Mean rel. error, LASSO: 0.013733318222447327 Mean rel. error, Ridge: 0.01399966972099561 .. GENERATED FROM PYTHON SOURCE LINES 213-216 Moment Estimation ----------------- Returns mean and variance of the PCE surrogate. .. GENERATED FROM PYTHON SOURCE LINES 219-229 .. 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 : (1.1276190476190477, 1.3807125276839571) Moments from LASSO regression : (1.1266773278385855, 1.3702252349746957) Moments from Ridge regression : (1.1265829663089746, 1.368059711959485) Moments from Monte Carlo integration: 1.127527316332168 1.3805224979467277 .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.766 seconds) .. _sphx_glr_download_auto_examples_surrogates_pce_plot_pce_camel.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_camel.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_pce_camel.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_pce_camel.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_