.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_surrogate_modeling/gaussian_process_regression/plot_gpr_noise.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. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_surrogate_modeling_gaussian_process_regression_plot_gpr_noise.py: Gaussian Process Regression: nugget effect and noise ==================================================== .. GENERATED FROM PYTHON SOURCE LINES 7-34 In this example, we show how to estimate a Gaussian Process Regression surrogate model (refer to :ref:`gaussian_process_regression`) from a noisy data set of the model. We consider the following cases: - Case 1: No noise is considered, assuming that the output values are exact, - Case 2: We consider a homoscedastic noise on the output values, - Case 3: We consider a heteroscedastic noise on the output values, - Case 4: We consider a homoscedastic noise modeled as a nugget factor. Both Case 2 and Case 4 model a homoscedastic noise on the output values of the model. But the two models are quite different: - the :meth:`~openturns.GaussianProcessFitter.setNoise` method of the :class:`~openturns.GaussianProcessFitter` takes noise into account only in the expression of the model likelihood, in the evaluation of the covariance parameters, - the :meth:`~openturns.CovarianceModel.setNuggetFactor` method of the :class:`~openturns.CovarianceModel` modifies the covariance model of the Gaussian process and keeps it modified once the parameters have been estimated. As a result, the trajectories of the Gaussian process become less smooth than with the first model. Modeling a noise on the output values as a nugget effect is interesting to simulate some sensors which have a finite precision and generates some output values perturbated by a White Noise process. On the contrary, taking noise into account with the :meth:`~openturns.GaussianProcessFitter.setNoise` method impacts the estimation of the covariance parameters but not the regularity of the process. .. GENERATED FROM PYTHON SOURCE LINES 36-52 Introduction ------------ We consider the model :math:`\model: [0,12] \rightarrow \Rset` defined by: .. math:: y = \model (x) = x \sin(x) We want to create a surrogate of this model using Gaussian Process Regression, from the data set defined by: .. math:: y_k = x_k \sin(x_k), \quad 1 \leq k \leq \sampleSize .. GENERATED FROM PYTHON SOURCE LINES 54-59 .. code-block:: Python import openturns as ot import openturns.experimental as otexp import openturns.viewer as otv import math as m .. GENERATED FROM PYTHON SOURCE LINES 60-62 First let us introduce some useful function. In order to observe the function and the location of the points in the input design of experiments, we define `plot_1d_data`. .. GENERATED FROM PYTHON SOURCE LINES 62-66 .. code-block:: Python # sphinx_gallery_thumbnail_number = 10 .. GENERATED FROM PYTHON SOURCE LINES 67-82 .. code-block:: Python def plot_1d_data(x_data, y_data, type="Curve", legend=None, color=None, linestyle=None): """Plot the data (x_data,y_data) as a Cloud/Curve""" if type == "Curve": graphF = ot.Curve(x_data, y_data) else: graphF = ot.Cloud(x_data, y_data) if legend is not None: graphF.setLegend(legend) if color is not None: graphF.setColor(color) if linestyle is not None: graphF.setLineStyle(linestyle) return graphF .. GENERATED FROM PYTHON SOURCE LINES 83-84 We define the model to be approximated. .. GENERATED FROM PYTHON SOURCE LINES 84-86 .. code-block:: Python g = ot.SymbolicFunction(["x"], ["x * sin(x)"]) .. GENERATED FROM PYTHON SOURCE LINES 87-88 We define a train data set. .. GENERATED FROM PYTHON SOURCE LINES 88-96 .. code-block:: Python xmin = 0.0 xmax = 12.0 n_train = 10 step = (xmax - 1 - xmin) / (n_train - 1.0) x_train = ot.RegularGrid(xmin + 0.2, step, n_train).getVertices() y_train = g(x_train) n_train = x_train.getSize() .. GENERATED FROM PYTHON SOURCE LINES 97-101 In order to compare the function and its metamodel, we use a test (i.e. validation) design of experiments made of a regular grid of 100 points from 0 to 12. Then we convert this grid into a :class:`~openturns.Sample` and we compute the outputs of the function on this sample. .. GENERATED FROM PYTHON SOURCE LINES 103-109 .. code-block:: Python n_test = 500 step = (xmax - xmin) / (n_test - 1) myRegularGrid = ot.RegularGrid(xmin, step, n_test) x_test = myRegularGrid.getVertices() y_test = g(x_test) .. GENERATED FROM PYTHON SOURCE LINES 110-111 We plot the model. .. GENERATED FROM PYTHON SOURCE LINES 111-121 .. code-block:: Python graph = ot.Graph(r"Model $x \rightarrow x\sin(x)$", "", "") graph.add( plot_1d_data(x_test, y_test, legend="Model", color="black", linestyle="dashed") ) graph.setAxes(True) graph.setXTitle("X") graph.setYTitle("Y") graph.setLegendPosition("lower left") view = otv.View(graph) .. image-sg:: /auto_surrogate_modeling/gaussian_process_regression/images/sphx_glr_plot_gpr_noise_001.svg :alt: Model $x \rightarrow x\sin(x)$ :srcset: /auto_surrogate_modeling/gaussian_process_regression/images/sphx_glr_plot_gpr_noise_001.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 122-124 We use the :class:`~openturns.ConstantBasisFactory` class to define the trend and the :class:`~openturns.MaternModel` class to define the covariance model. This Matérn model is based on the regularity parameter :math:`\nu=3/2`. .. GENERATED FROM PYTHON SOURCE LINES 126-130 .. code-block:: Python ot.ResourceMap.SetAsScalar("CovarianceModel-DefaultNuggetFactor", 0.0) dimension = 1 basis = ot.ConstantBasisFactory(dimension).build() .. GENERATED FROM PYTHON SOURCE LINES 131-135 Case 1: Ignore any noise ~~~~~~~~~~~~~~~~~~~~~~~~ We introduce no noise here: it means that we assume that the output values are exact. We plot the surrogate model: we see that it interpolates the data set, as expected. .. GENERATED FROM PYTHON SOURCE LINES 135-140 .. code-block:: Python covarianceModel = ot.MaternModel([1.0] * dimension, 1.5) fitter_algo = ot.GaussianProcessFitter(x_train, y_train, covarianceModel, basis) fitter_algo.run() fitter_result_noNoise = fitter_algo.getResult() .. GENERATED FROM PYTHON SOURCE LINES 141-142 We get the estimated parameters of the covariance model. .. GENERATED FROM PYTHON SOURCE LINES 142-148 .. code-block:: Python estimated_cov_noNoise = fitter_result_noNoise.getCovarianceModel() print("Case 1: ignore any noise") print( "Estimated covariance model with the homoscedastic noise = ", estimated_cov_noNoise ) .. rst-class:: sphx-glr-script-out .. code-block:: none Case 1: ignore any noise Estimated covariance model with the homoscedastic noise = MaternModel(scale=[2.45291], amplitude=[6.7043], nu=1.5) .. GENERATED FROM PYTHON SOURCE LINES 149-150 We create the GPR metamodel. .. GENERATED FROM PYTHON SOURCE LINES 150-166 .. code-block:: Python gpr_algo_noNoise = ot.GaussianProcessRegression(fitter_result_noNoise) gpr_algo_noNoise.run() gpr_result_noNoise = gpr_algo_noNoise.getResult() gprMM_noNoise = gpr_result_noNoise.getMetaModel() graph = ot.Graph("Model with exact data", "x", "y") graph.setLegendPosition("lower left") graph.add( plot_1d_data(x_test, y_test, legend="Model", color="black", linestyle="dashed") ) graph.add( plot_1d_data(x_train, y_train, type="Cloud", legend="Exact Data", color="red") ) graph.add(plot_1d_data(x_test, gprMM_noNoise(x_test), legend="GPR", color="green")) view = otv.View(graph) .. image-sg:: /auto_surrogate_modeling/gaussian_process_regression/images/sphx_glr_plot_gpr_noise_002.svg :alt: Model with exact data :srcset: /auto_surrogate_modeling/gaussian_process_regression/images/sphx_glr_plot_gpr_noise_002.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 167-170 We can draw some trajectories of the resulting conditioned Gaussian process using the class :class:`~openturns.experimental.ConditionedGaussianProcess` built from a :class:`~openturns.GaussianProcessRegressionResult`. We create the conditioned Gaussian process. Then we generate some trajectories. .. GENERATED FROM PYTHON SOURCE LINES 170-187 .. code-block:: Python process_noNoise = otexp.ConditionedGaussianProcess(gpr_result_noNoise, myRegularGrid) traj_noNoise = process_noNoise.getSample(10) graph_traj_noNoise = ot.Graph(r"Model with exact data", "x", "y", True, "lower left") graph_traj_noNoise.add(traj_noNoise.drawMarginal()) graph_traj_noNoise.setLegends(["conditioned trajectories"] + [""] * 9) graph_traj_noNoise.setColors(["grey"] * 10) graph_traj_noNoise.add( plot_1d_data(x_test, y_test, legend="Model", color="black", linestyle="dashed") ) graph_traj_noNoise.add( plot_1d_data(x_test, gprMM_noNoise(x_test), legend="GPR", color="green") ) graph_traj_noNoise.add( plot_1d_data(x_train, y_train, type="Cloud", legend="Exact data", color="red") ) view = otv.View(graph_traj_noNoise) .. image-sg:: /auto_surrogate_modeling/gaussian_process_regression/images/sphx_glr_plot_gpr_noise_003.svg :alt: Model with exact data :srcset: /auto_surrogate_modeling/gaussian_process_regression/images/sphx_glr_plot_gpr_noise_003.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 188-193 Case 2: Introduce a homoscedastic noise ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ We want to model a noise on each output value of the model. This noise is the same for output :math:`y_i` value and is modeled as a :class:`~openturns.Normal` distribution which is centered with a variance denoted by :math:`\sigma^2 = 0.5`. .. GENERATED FROM PYTHON SOURCE LINES 193-195 .. code-block:: Python variance_point_homosc = [0.25] * n_train .. GENERATED FROM PYTHON SOURCE LINES 196-198 In order to illustrate some noisy output values, we add a random noise to each output, following the noise distribution. .. GENERATED FROM PYTHON SOURCE LINES 198-201 .. code-block:: Python noise_homosk = ot.Normal(0, m.sqrt(variance_point_homosc[0])).getSample(n_train) y_train_homosc = y_train + noise_homosk .. GENERATED FROM PYTHON SOURCE LINES 202-204 We plot the model and the noisy train data which is not on the graph of the model. .. GENERATED FROM PYTHON SOURCE LINES 204-215 .. code-block:: Python graph = ot.Graph(r"Model with homoscedastic noisy data", "x", "y", True, "lower left") graph.add( plot_1d_data(x_test, y_test, legend="Model", color="black", linestyle="dashed") ) graph.add( plot_1d_data( x_train, y_train_homosc, type="Cloud", legend="Noisy data", color="red" ) ) view = otv.View(graph) .. image-sg:: /auto_surrogate_modeling/gaussian_process_regression/images/sphx_glr_plot_gpr_noise_004.svg :alt: Model with homoscedastic noisy data :srcset: /auto_surrogate_modeling/gaussian_process_regression/images/sphx_glr_plot_gpr_noise_004.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 216-217 We define the Gaussian Process Fitter on the noisy data. .. GENERATED FROM PYTHON SOURCE LINES 217-220 .. code-block:: Python covarianceModel = ot.MaternModel([1.0] * dimension, 1.5) fitter_algo = ot.GaussianProcessFitter(x_train, y_train_homosc, covarianceModel, basis) .. GENERATED FROM PYTHON SOURCE LINES 221-222 If we ignore the noise, we get the following surrogate model. .. GENERATED FROM PYTHON SOURCE LINES 222-225 .. code-block:: Python fitter_algo.run() fitter_result = fitter_algo.getResult() .. GENERATED FROM PYTHON SOURCE LINES 226-227 We get the estimated parameters of the covariance model. .. GENERATED FROM PYTHON SOURCE LINES 227-231 .. code-block:: Python estimated_cov = fitter_result.getCovarianceModel() print("Case 2: homoscedastic noise") print("Estimated covariance model with the homoscedastic noise = ", estimated_cov) .. rst-class:: sphx-glr-script-out .. code-block:: none Case 2: homoscedastic noise Estimated covariance model with the homoscedastic noise = MaternModel(scale=[2.1802], amplitude=[6.29094], nu=1.5) .. GENERATED FROM PYTHON SOURCE LINES 232-233 We create the GPR metamodel. .. GENERATED FROM PYTHON SOURCE LINES 233-239 .. code-block:: Python gpr_algo_1 = ot.GaussianProcessRegression(fitter_result) gpr_algo_1.run() gpr_result_1 = gpr_algo_1.getResult() gprMM_1 = gpr_result_1.getMetaModel() .. GENERATED FROM PYTHON SOURCE LINES 240-241 Now we do not ignore the noise on the output values any more and we add it to the algorithm. .. GENERATED FROM PYTHON SOURCE LINES 241-251 .. code-block:: Python covarianceModel = ot.MaternModel([1.0] * dimension, 1.5) fitter_algo = ot.GaussianProcessFitter(x_train, y_train_homosc, covarianceModel, basis) fitter_algo.setNoise(variance_point_homosc) fitter_algo.run() fitter_result = fitter_algo.getResult() gpr_algo_homosc = ot.GaussianProcessRegression(fitter_result) gpr_algo_homosc.run() gpr_result_homosc = gpr_algo_homosc.getResult() gprMM_homosc = gpr_result_homosc.getMetaModel() .. GENERATED FROM PYTHON SOURCE LINES 252-255 We plot now the resulting surrogate model. We can see that the GPR metamodel does not interpolate the train set any more due to the introduction of the noise. .. GENERATED FROM PYTHON SOURCE LINES 255-274 .. code-block:: Python graph = ot.Graph(r"Model with homoscedastic noisy data", "x", "y", True, "lower left") graph.add( plot_1d_data(x_test, y_test, legend="Model", color="black", linestyle="dashed") ) graph.add( plot_1d_data( x_train, y_train_homosc, type="Cloud", legend="Noisy data", color="red" ) ) graph.add( plot_1d_data( x_test, gprMM_homosc(x_test), legend="GPR modeling noise", color="green" ) ) graph.add( plot_1d_data(x_test, gprMM_1(x_test), legend="GPR ignoring noise", color="blue") ) view = otv.View(graph) .. image-sg:: /auto_surrogate_modeling/gaussian_process_regression/images/sphx_glr_plot_gpr_noise_005.svg :alt: Model with homoscedastic noisy data :srcset: /auto_surrogate_modeling/gaussian_process_regression/images/sphx_glr_plot_gpr_noise_005.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 275-277 We can draw some trajectories of the resulting conditioned Gaussian process still using the class :class:`~openturns.experimental.ConditionedGaussianProcess` as previoulsy. .. GENERATED FROM PYTHON SOURCE LINES 277-300 .. code-block:: Python process_homosc = otexp.ConditionedGaussianProcess(gpr_result_homosc, myRegularGrid) traj_homosc = process_homosc.getSample(10) graph_traj_homosc = ot.Graph( r"Model with homoscedastic noisy data", "x", "y", True, "lower left" ) graph_traj_homosc.add(traj_homosc.drawMarginal()) graph_traj_homosc.setLegends(["conditioned trajectories"] + [""] * 9) graph_traj_homosc.setColors(["grey"] * 10) graph_traj_homosc.add( plot_1d_data(x_test, y_test, legend="Model", color="black", linestyle="dashed") ) graph_traj_homosc.add( plot_1d_data( x_test, gprMM_homosc(x_test), legend="GPR modeling noise", color="green" ) ) graph_traj_homosc.add( plot_1d_data( x_train, y_train_homosc, type="Cloud", legend="Noisy data", color="red" ) ) view = otv.View(graph_traj_homosc) .. image-sg:: /auto_surrogate_modeling/gaussian_process_regression/images/sphx_glr_plot_gpr_noise_006.svg :alt: Model with homoscedastic noisy data :srcset: /auto_surrogate_modeling/gaussian_process_regression/images/sphx_glr_plot_gpr_noise_006.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 301-307 Case 3: Introduce a heteroscedastic noise ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ We want to model a noise on each output value of the model. This noise depends on the output value :math:`y_i` and is modeled as a :class:`~openturns.Normal` distribution which is centered with a variance denoted by :math:`\sigma_i^2`. We generate a list of :math:`\sampleSize` variances. .. GENERATED FROM PYTHON SOURCE LINES 307-309 .. code-block:: Python variance_point_heterosc = ot.Uniform(0.0, 1.5).getSample(n_train).asPoint() .. GENERATED FROM PYTHON SOURCE LINES 310-312 Once more, in order to illustrate some noisy output values, we add a random noise to each output, following the noise distribution. .. GENERATED FROM PYTHON SOURCE LINES 312-318 .. code-block:: Python noise_heterosk = ot.Sample(0, 1) for i in range(n_train): noise_i = ot.Normal(0, m.sqrt(variance_point_heterosc[i])).getRealization() noise_heterosk.add(noise_i) y_train_heterosc = y_train + noise_heterosk .. GENERATED FROM PYTHON SOURCE LINES 319-321 We plot the model and the noisy train data which is not on the graph of the model. .. GENERATED FROM PYTHON SOURCE LINES 321-332 .. code-block:: Python graph = ot.Graph(r"Model with heteroscedastic noisy data", "x", "y", True, "lower left") graph.add( plot_1d_data(x_test, y_test, legend="Model", color="black", linestyle="dashed") ) graph.add( plot_1d_data( x_train, y_train_heterosc, type="Cloud", legend="Noisy data", color="red" ) ) view = otv.View(graph) .. image-sg:: /auto_surrogate_modeling/gaussian_process_regression/images/sphx_glr_plot_gpr_noise_007.svg :alt: Model with heteroscedastic noisy data :srcset: /auto_surrogate_modeling/gaussian_process_regression/images/sphx_glr_plot_gpr_noise_007.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 333-334 We define the Gaussian Process Fitter on the noisy data. .. GENERATED FROM PYTHON SOURCE LINES 334-339 .. code-block:: Python covarianceModel = ot.MaternModel([1.0] * dimension, 1.5) fitter_algo = ot.GaussianProcessFitter( x_train, y_train_heterosc, covarianceModel, basis ) .. GENERATED FROM PYTHON SOURCE LINES 340-341 If we ignore the noise, we get the following surrogate model. .. GENERATED FROM PYTHON SOURCE LINES 341-344 .. code-block:: Python fitter_algo.run() fitter_result = fitter_algo.getResult() .. GENERATED FROM PYTHON SOURCE LINES 345-346 We get the estimated parameters of the covariance model. .. GENERATED FROM PYTHON SOURCE LINES 346-350 .. code-block:: Python estimated_cov = fitter_result.getCovarianceModel() print("Case 3: heteroscedastic noise") print("Estimated covariance model with the heteroscedastic noise = ", estimated_cov) .. rst-class:: sphx-glr-script-out .. code-block:: none Case 3: heteroscedastic noise Estimated covariance model with the heteroscedastic noise = MaternModel(scale=[2.1177], amplitude=[5.73932], nu=1.5) .. GENERATED FROM PYTHON SOURCE LINES 351-352 We create the GPR metamodel. .. GENERATED FROM PYTHON SOURCE LINES 352-357 .. code-block:: Python gpr_algo_2 = ot.GaussianProcessRegression(fitter_result) gpr_algo_2.run() gpr_result_2 = gpr_algo_2.getResult() gprMM_2 = gpr_result_2.getMetaModel() .. GENERATED FROM PYTHON SOURCE LINES 358-359 Now we do not ignore the noise on output values and we add it to the algorithm. .. GENERATED FROM PYTHON SOURCE LINES 359-370 .. code-block:: Python fitter_algo = ot.GaussianProcessFitter( x_train, y_train_heterosc, covarianceModel, basis ) fitter_algo.setNoise(variance_point_heterosc) fitter_algo.run() fitter_result = fitter_algo.getResult() gpr_algo_heterosc = ot.GaussianProcessRegression(fitter_result) gpr_algo_heterosc.run() gpr_result_heterosc = gpr_algo_heterosc.getResult() gprMM_heterosc = gpr_result_heterosc.getMetaModel() .. GENERATED FROM PYTHON SOURCE LINES 371-376 We plot now the resulting surrogate model. We can see that the Gaussian process regression does not interpolate the train set any more due to the introduction of the noise. We could compute the mean square error betwwen the model and the metamodels and show that the distribution of the errors is more tight when we take into account the noise modeling. .. GENERATED FROM PYTHON SOURCE LINES 376-396 .. code-block:: Python graph = ot.Graph(r"Model with heteroscedastic noisy data", "x", "y", True, "lower left") graph.add( plot_1d_data(x_test, y_test, legend="Model", color="black", linestyle="dashed") ) graph.add( plot_1d_data( x_train, y_train_heterosc, type="Cloud", legend="Noisy data", color="red" ) ) graph.add( plot_1d_data( x_test, gprMM_heterosc(x_test), legend="GPR modeling noise", color="green" ) ) graph.add( plot_1d_data(x_test, gprMM_2(x_test), legend="GPR ignoring noise", color="blue") ) view = otv.View(graph) .. image-sg:: /auto_surrogate_modeling/gaussian_process_regression/images/sphx_glr_plot_gpr_noise_008.svg :alt: Model with heteroscedastic noisy data :srcset: /auto_surrogate_modeling/gaussian_process_regression/images/sphx_glr_plot_gpr_noise_008.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 397-399 We can draw some trajectories of the resulting conditioned Gaussian process as previoulsy. We can see that the trajectories have kept the regularity of the initial Matern model. .. GENERATED FROM PYTHON SOURCE LINES 399-422 .. code-block:: Python process_heterosc = otexp.ConditionedGaussianProcess(gpr_result_heterosc, myRegularGrid) traj_heterosc = process_heterosc.getSample(10) graph_traj_heterosc = ot.Graph( r"Model with heteroscedastic noisy data", "x", "y", True, "lower left" ) graph_traj_heterosc.add(traj_heterosc.drawMarginal()) graph_traj_heterosc.setLegends(["conditioned trajectories"] + [""] * 9) graph_traj_heterosc.setColors(["grey"] * 10) graph_traj_heterosc.add( plot_1d_data(x_test, y_test, legend="Model", color="black", linestyle="dashed") ) graph_traj_heterosc.add( plot_1d_data( x_test, gprMM_heterosc(x_test), legend="GPR modeling noise", color="green" ) ) graph_traj_heterosc.add( plot_1d_data( x_train, y_train_heterosc, type="Cloud", legend="Noisy data", color="red" ) ) view = otv.View(graph_traj_heterosc) .. image-sg:: /auto_surrogate_modeling/gaussian_process_regression/images/sphx_glr_plot_gpr_noise_009.svg :alt: Model with heteroscedastic noisy data :srcset: /auto_surrogate_modeling/gaussian_process_regression/images/sphx_glr_plot_gpr_noise_009.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 423-431 Case 4: Introduce a nugget effect ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ We can introduce the homoscedastic noise on the output values of the model as a nugget effect, using the :meth:`~openturns.CovarianceModel.setNuggetFactor` method of the :class:`~openturns.CovarianceModel`. We consider the Matern covariance model defined previously and we activate the estimation of the nugget factor. .. GENERATED FROM PYTHON SOURCE LINES 431-439 .. code-block:: Python covarianceModel = ot.MaternModel([1.0] * dimension, 1.5) covarianceModel.activateNuggetFactor(True) fitter_algo_nugget = ot.GaussianProcessFitter( x_train, y_train_homosc, covarianceModel, basis ) fitter_algo_nugget.run() fitter_result_nugget = fitter_algo_nugget.getResult() .. GENERATED FROM PYTHON SOURCE LINES 440-442 We get the estimated parameters of the covariance model and the nugget factor :math:`\varepsilon_{nugget}`. .. GENERATED FROM PYTHON SOURCE LINES 442-447 .. code-block:: Python estimated_cov_nugget = fitter_result_nugget.getCovarianceModel() print("Case 4: nugget effect") print("Estimated covariance model with the nugget factor = ", estimated_cov_nugget) print("Estimated nugget factor = ", estimated_cov_nugget.getNuggetFactor()) .. rst-class:: sphx-glr-script-out .. code-block:: none Case 4: nugget effect Estimated covariance model with the nugget factor = MaternModel(scale=[1.13448], amplitude=[2.77258], nu=1.5) Estimated nugget factor = 2.4417556250992196 .. GENERATED FROM PYTHON SOURCE LINES 448-453 .. code-block:: Python gpr_algo_nuggetF = ot.GaussianProcessRegression(fitter_result_nugget) gpr_algo_nuggetF.run() gpr_result_nuggetF = gpr_algo_nuggetF.getResult() gprMM_nuggetF = gpr_result_nuggetF.getMetaModel() .. GENERATED FROM PYTHON SOURCE LINES 454-455 We draw the graph to compare the nugget effect and the noise modeling. .. GENERATED FROM PYTHON SOURCE LINES 455-480 .. code-block:: Python graph = ot.Graph(r"Model with homoscedastic noisy data", "x", "y", True, "lower left") graph.add( plot_1d_data(x_test, y_test, legend="Model", color="black", linestyle="dashed") ) graph.add( plot_1d_data( x_train, y_train_homosc, type="Cloud", legend="Noisy data", color="red" ) ) graph.add( plot_1d_data( x_test, gprMM_nuggetF(x_test), legend="GPR modeling nugget factor", color="green", ) ) graph.add( plot_1d_data( x_test, gprMM_homosc(x_test), legend="GPR modeling noise", color="blue" ) ) view = otv.View(graph) .. image-sg:: /auto_surrogate_modeling/gaussian_process_regression/images/sphx_glr_plot_gpr_noise_010.svg :alt: Model with homoscedastic noisy data :srcset: /auto_surrogate_modeling/gaussian_process_regression/images/sphx_glr_plot_gpr_noise_010.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 481-483 We can draw some trajectories of the resulting conditioned Gaussian process as previoulsy. We can see that the trajectories are not as smooth as the initial Matern model any more. .. GENERATED FROM PYTHON SOURCE LINES 483-509 .. code-block:: Python process_nuggetF = otexp.ConditionedGaussianProcess(gpr_result_nuggetF, myRegularGrid) traj_nuggetF = process_nuggetF.getSample(10) graph_traj_nuggetF = ot.Graph( r"Model with homoscedastic noisy data", "x", "y", True, "lower left" ) graph_traj_nuggetF.add(traj_nuggetF.drawMarginal()) graph_traj_nuggetF.setLegends(["conditioned trajectories"] + [""] * 9) graph_traj_nuggetF.setColors(["grey"] * 10) graph_traj_nuggetF.add( plot_1d_data(x_test, y_test, legend="Model", color="black", linestyle="dashed") ) graph_traj_nuggetF.add( plot_1d_data( x_test, gprMM_nuggetF(x_test), legend="GPR modeling nugget factor", color="green", ) ) graph_traj_nuggetF.add( plot_1d_data( x_train, y_train_homosc, type="Cloud", legend="Noisy data", color="red" ) ) view = otv.View(graph_traj_nuggetF) .. image-sg:: /auto_surrogate_modeling/gaussian_process_regression/images/sphx_glr_plot_gpr_noise_011.svg :alt: Model with homoscedastic noisy data :srcset: /auto_surrogate_modeling/gaussian_process_regression/images/sphx_glr_plot_gpr_noise_011.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 510-512 To facilitate comparison, we draw the trajectories generated by the Gaussian process built with nugget factor or with noise. .. GENERATED FROM PYTHON SOURCE LINES 512-517 .. code-block:: Python grid = ot.GridLayout(1, 2) grid.setGraph(0, 0, graph_traj_nuggetF) grid.setGraph(0, 1, graph_traj_homosc) view = otv.View(grid) .. image-sg:: /auto_surrogate_modeling/gaussian_process_regression/images/sphx_glr_plot_gpr_noise_012.svg :alt: , Model with homoscedastic noisy data, Model with homoscedastic noisy data :srcset: /auto_surrogate_modeling/gaussian_process_regression/images/sphx_glr_plot_gpr_noise_012.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 518-519 Display all figures .. GENERATED FROM PYTHON SOURCE LINES 519-520 .. code-block:: Python otv.View.ShowAll() .. _sphx_glr_download_auto_surrogate_modeling_gaussian_process_regression_plot_gpr_noise.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_gpr_noise.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_gpr_noise.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_gpr_noise.zip `