.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/sampling/refined_stratified_sampling/refined_stratified_rectangular_gradient.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_sampling_refined_stratified_sampling_refined_stratified_rectangular_gradient.py: Rectangular Refined Stratified Sampling - Gradient Enhanced Refinement ======================================================================= In this example, Stratified sampling is used to generate samples from Uniform distribution and sample expansion is done adaptively using Refined Stratified Sampling. .. GENERATED FROM PYTHON SOURCE LINES 11-14 Import the necessary libraries. Here we import standard libraries such as numpy, matplotlib and other necessary library for plots, but also need to import the :class:`.TrueStratifiedSampling`, :class:`.RefinedStratifiedSampling` and :class:`.Kriging` class from :py:mod:`UQpy`. .. GENERATED FROM PYTHON SOURCE LINES 17-30 .. code-block:: default import shutil from UQpy.sampling import TrueStratifiedSampling, RefinedStratifiedSampling from UQpy.surrogates import GaussianProcessRegression from UQpy.run_model.RunModel import RunModel from UQpy.distributions import Uniform import matplotlib.pyplot as plt from matplotlib import cm from matplotlib.ticker import LinearLocator, FormatStrFormatter import numpy as np from UQpy.sampling.stratified_sampling.strata import RectangularStrata from UQpy.utilities.MinimizeOptimizer import MinimizeOptimizer .. GENERATED FROM PYTHON SOURCE LINES 31-32 Create a distribution object. .. GENERATED FROM PYTHON SOURCE LINES 35-38 .. code-block:: default marginals = [Uniform(loc=0., scale=1.), Uniform(loc=0., scale=1.)] .. GENERATED FROM PYTHON SOURCE LINES 39-40 Create a strata object. .. GENERATED FROM PYTHON SOURCE LINES 43-46 .. code-block:: default strata = RectangularStrata(strata_number=[4, 4]) .. GENERATED FROM PYTHON SOURCE LINES 47-48 Run stratified sampling. .. GENERATED FROM PYTHON SOURCE LINES 51-55 .. code-block:: default x = TrueStratifiedSampling(distributions=marginals, strata_object=strata, nsamples_per_stratum=1, random_state=1) initial_samples=x.samples.copy() .. GENERATED FROM PYTHON SOURCE LINES 56-57 This plot shows the samples and strata generated by the :class:`.TrueStratifiedSampling` class. .. GENERATED FROM PYTHON SOURCE LINES 60-68 .. code-block:: default fig1 = x.strata_object.plot_2d() plt.title("STS samples U(0,1) and space stratification") plt.plot(x.samples[:16, 0], x.samples[:16, 1], 'ro') plt.xlim([0, 1]) plt.ylim([0, 1]) plt.show() .. GENERATED FROM PYTHON SOURCE LINES 69-71 RunModel class is used to estimate the function value at sample points generated using :class:`.TrueStratifiedSampling` class. .. GENERATED FROM PYTHON SOURCE LINES 74-77 .. code-block:: default rmodel = RunModel(model_script='local_python_model_function.py', vec=False) .. GENERATED FROM PYTHON SOURCE LINES 78-79 This figure shows the actual function defined in python model script. .. GENERATED FROM PYTHON SOURCE LINES 82-108 .. code-block:: default rmodel1 = RunModel(model_script='local_python_model_function.py', vec=False) rmodel1.run(samples=x.samples) num = 100 x1 = np.linspace(0, 1, num) x2 = np.linspace(0, 1, num) x1v, x2v = np.meshgrid(x1, x2) y_act = np.zeros([num, num]) r1model = RunModel(model_script='local_python_model_function.py') for i in range(num): for j in range(num): r1model.run(samples=np.array([[x1v[i, j], x2v[i, j]]]), append_samples=False) y_act[i, j] = r1model.qoi_list[0] fig1 = plt.figure() ax1 = fig1.gca(projection='3d') # Plot for estimated values surf = ax1.plot_surface(x1v, x2v, y_act, cmap=cm.coolwarm, linewidth=0, antialiased=False) # Customize the z axis. ax1.set_zlim(-1, 15) ax1.zaxis.set_major_locator(LinearLocator(10)) ax1.zaxis.set_major_formatter(FormatStrFormatter('%.02f')) # Add a color bar which maps values to colors. fig1.colorbar(surf, shrink=0.5, aspect=5) plt.show() .. GENERATED FROM PYTHON SOURCE LINES 109-111 :class:`.Kriging` class generated a surrogate model using :class:`.TrueStratifiedSampling` samples and function value at those points. .. GENERATED FROM PYTHON SOURCE LINES 114-125 .. code-block:: default from UQpy.surrogates.gaussian_process.regression_models import LinearRegression from UQpy.surrogates.gaussian_process.kernels import RBF bounds = [[10**(-3), 10**3], [10**(-3), 10**2], [10**(-3), 10**2]] K = GaussianProcessRegression(regression_model=LinearRegression(), kernel=RBF(), optimizer=MinimizeOptimizer(method="L-BFGS-B", bounds=bounds), hyperparameters=[1, 1, 0.1], optimizations_number=20) K.fit(samples=x.samples, values=rmodel1.qoi_list) print(K.hyperparameters) .. GENERATED FROM PYTHON SOURCE LINES 126-127 This figure shows the surrogate model generated using :class:`.Kriging` class from initial samples. .. GENERATED FROM PYTHON SOURCE LINES 130-150 .. code-block:: default num = 25 x1 = np.linspace(0, 1, num) x2 = np.linspace(0, 1, num) x1v, x2v = np.meshgrid(x1, x2) y = np.zeros([num, num]) for i in range(num): for j in range(num): y[i, j] = K.predict(np.array([x1v[i, j], x2v[i, j]])) fig2 = plt.figure() ax2 = fig2.gca(projection='3d') # Plot for estimated values kr = ax2.plot_wireframe(x1v, x2v, y, color='Green', label='Kriging interpolate') # Plot for scattered data ID = ax2.scatter3D(x.samples[:, 0], x.samples[:, 1], rmodel1.qoi_list, color='Red', label='Input data') plt.legend(handles=[kr, ID]) plt.show() .. GENERATED FROM PYTHON SOURCE LINES 151-153 A :class:`.RefinedStratifiedSampling` class object is initiated by using the :class:`.TrueStratifiedSampling`, :class:`.RunModel` and :class:`.Kriging` object. .. GENERATED FROM PYTHON SOURCE LINES 156-161 .. code-block:: default from UQpy.sampling import GradientEnhancedRefinement refinement = GradientEnhancedRefinement(strata=strata, runmodel_object=rmodel, surrogate=K) z = RefinedStratifiedSampling(stratified_sampling=x, refinement_algorithm=refinement, random_state=2) .. GENERATED FROM PYTHON SOURCE LINES 162-164 After initiating the :class:`.RefinedStratifiedSampling` class object, new samples are generated using the :code:`refinedStratifiedSampling.sample` method. .. GENERATED FROM PYTHON SOURCE LINES 167-170 .. code-block:: default z.run(nsamples=50) .. GENERATED FROM PYTHON SOURCE LINES 171-173 This figure shows the final samples generated using :class:`.RefinedStratifiedSampling` class, where red dots shows the initial samples. .. GENERATED FROM PYTHON SOURCE LINES 176-184 .. code-block:: default fig3 = strata.plot_2d() plt.xlim([0, 1]) plt.ylim([0, 1]) plt.plot(initial_samples[:, 0], initial_samples[:, 1], 'ro') plt.plot(z.samplesU01[:, 0], z.samplesU01[:, 1], 'gx') plt.show() .. GENERATED FROM PYTHON SOURCE LINES 185-187 :class:`.Kriging` class is used to generate a surrogate model using final samples from :class:`.RefinedStratifiedSampling` class. .. GENERATED FROM PYTHON SOURCE LINES 190-198 .. code-block:: default hyperparameters=K.hyperparameters.tolist() K2 = GaussianProcessRegression(kernel=RBF(), optimizer=MinimizeOptimizer(method="L-BFGS-B"), hyperparameters=hyperparameters, noise=False) K2.fit(samples=z.samples, values=rmodel.qoi_list) .. GENERATED FROM PYTHON SOURCE LINES 199-200 This figure shows the final surrogate model, generated using 200 samples. .. GENERATED FROM PYTHON SOURCE LINES 203-224 .. code-block:: default y = np.zeros([num, num]) for i in range(num): for j in range(num): y[i, j] = K2.predict(np.array([x1v[i, j], x2v[i, j]])) plt.clf() fig4 = plt.figure() a4 = fig4.gca(projection='3d') # Plot for estimated values kr = a4.plot_wireframe(x1v, x2v, y, color='Green', label='Kriging interpolate') # Plot for scattered data ID = a4.scatter3D(z.samples[:, 0], z.samples[:, 1], rmodel.qoi_list, color='Red', label='Input data') plt.legend(handles=[kr, ID]) plt.show() shutil.rmtree(rmodel.model_dir) shutil.rmtree(r1model.model_dir) shutil.rmtree(rmodel1.model_dir) .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.000 seconds) .. _sphx_glr_download_auto_examples_sampling_refined_stratified_sampling_refined_stratified_rectangular_gradient.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/sampling/refined_stratified_sampling/refined_stratified_rectangular_gradient.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: refined_stratified_rectangular_gradient.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: refined_stratified_rectangular_gradient.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_