.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_data_analysis/sample_analysis/plot_src_confidence.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_data_analysis_sample_analysis_plot_src_confidence.py: Compute squared SRC indices confidence intervals ------------------------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 7-10 This example shows how to compute squared SRC indices confidence bounds with bootstrap. First, we compute squared SRC indices and draw them. Then we compute bootstrap confidence bounds using the :class:`~openturns.BootstrapExperiment` class and draw them. .. GENERATED FROM PYTHON SOURCE LINES 12-14 Define the model ~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 16-20 .. code-block:: Python import openturns as ot import openturns.viewer as otv from openturns.usecases import flood_model .. GENERATED FROM PYTHON SOURCE LINES 21-22 Load the flood model. .. GENERATED FROM PYTHON SOURCE LINES 22-27 .. code-block:: Python fm = flood_model.FloodModel() distribution = fm.distribution g = fm.model.getMarginal(1) dim = distribution.getDimension() .. GENERATED FROM PYTHON SOURCE LINES 28-29 See the distribution .. GENERATED FROM PYTHON SOURCE LINES 29-31 .. code-block:: Python distribution .. raw:: html
ComposedDistribution
Index Variable Distribution
0 Q (m3/s) TruncatedDistribution(Gumbel(beta = 558, gamma = 1013), bounds = [0, (19000.8) +inf[)
1 Ks TruncatedDistribution(Normal(mu = 30, sigma = 7.5), bounds = [0, (87.3797) +inf[)
2 Zv (m) Uniform(a = 49, b = 51)
3 Zm (m) Uniform(a = 54, b = 56)
4 B (m) Triangular(a = 295, m = 300, b = 305)
5 L (m) Triangular(a = 4990, m = 5000, b = 5010)
6 Zb (m) Triangular(a = 55, m = 55.5, b = 56)
7 Hd (m) Uniform(a = 2, b = 4)


.. GENERATED FROM PYTHON SOURCE LINES 32-33 See the model .. GENERATED FROM PYTHON SOURCE LINES 33-35 .. code-block:: Python g.getOutputDescription() .. raw:: html
[S]


.. GENERATED FROM PYTHON SOURCE LINES 36-38 Estimate the squared SRC indices ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 40-41 We produce a pair of input and output sample. .. GENERATED FROM PYTHON SOURCE LINES 41-46 .. code-block:: Python ot.RandomGenerator.SetSeed(0) N = 100 X = distribution.getSample(N) Y = g(X) .. GENERATED FROM PYTHON SOURCE LINES 47-48 Compute squared SRC indices from the generated design. .. GENERATED FROM PYTHON SOURCE LINES 48-51 .. code-block:: Python importance_factors = ot.CorrelationAnalysis(X, Y).computeSquaredSRC() print(importance_factors) .. rst-class:: sphx-glr-script-out .. code-block:: none [0.32175,0.137302,0.15122,0.005747,0.00141732,0.000104678,0.0189879,0.138303] .. GENERATED FROM PYTHON SOURCE LINES 52-53 Plot the squared SRC indices. .. GENERATED FROM PYTHON SOURCE LINES 53-60 .. code-block:: Python input_names = g.getInputDescription() graph = ot.SobolIndicesAlgorithm.DrawCorrelationCoefficients( importance_factors, input_names, "Importance factors" ) graph.setYTitle("Squared SRC") _ = otv.View(graph) .. image-sg:: /auto_data_analysis/sample_analysis/images/sphx_glr_plot_src_confidence_001.png :alt: Importance factors :srcset: /auto_data_analysis/sample_analysis/images/sphx_glr_plot_src_confidence_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 61-63 Compute confidence intervals ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 65-67 We now compute bootstrap confidence intervals for the importance factors. This is done with the :class:`~openturns.BootstrapExperiment` class. .. GENERATED FROM PYTHON SOURCE LINES 69-70 Create SRC bootstrap sample .. GENERATED FROM PYTHON SOURCE LINES 70-78 .. code-block:: Python bootstrap_size = 100 src_boot = ot.Sample(bootstrap_size, dim) for i in range(bootstrap_size): selection = ot.BootstrapExperiment.GenerateSelection(N, N) X_boot = X[selection] Y_boot = Y[selection] src_boot[i, :] = ot.CorrelationAnalysis(X_boot, Y_boot).computeSquaredSRC() .. GENERATED FROM PYTHON SOURCE LINES 79-80 Compute bootstrap quantiles .. GENERATED FROM PYTHON SOURCE LINES 80-87 .. code-block:: Python alpha = 0.05 src_lb = src_boot.computeQuantilePerComponent(alpha / 2.0) src_ub = src_boot.computeQuantilePerComponent(1.0 - alpha / 2.0) src_interval = ot.Interval(src_lb, src_ub) print(src_interval) .. rst-class:: sphx-glr-script-out .. code-block:: none [0.230359, 0.40736] [0.072598, 0.255676] [0.0938578, 0.221143] [0.00165483, 0.0153625] [5.263e-06, 0.00847213] [1.78931e-07, 0.00297661] [0.0095922, 0.0301949] [0.0873361, 0.225831] .. GENERATED FROM PYTHON SOURCE LINES 88-129 .. code-block:: Python def draw_importance_factors_with_bounds( importance_factors, input_names, alpha, importance_bounds ): """ Plot importance factors indices with confidence bounds of level 1 - alpha. Parameters ---------- importance_factors : Point(dimension) The importance factors. input_names : list(str) The names of the input variables. alpha : float, in [0, 1] The complementary confidence level. importance_bounds : Interval(dimension) The lower and upper bounds of the importance factors Returns ------- graph : Graph The importance factors indices with lower and upper 1-alpha confidence intervals. """ dim = importance_factors.getDimension() lb = importance_bounds.getLowerBound() ub = importance_bounds.getUpperBound() palette = ot.Drawable.BuildDefaultPalette(2) graph = ot.SobolIndicesAlgorithm.DrawCorrelationCoefficients( importance_factors, input_names, "Importance factors" ) graph.setColors([palette[0], "black"]) graph.setYTitle("Squared SRC") # Add confidence bounds for i in range(dim): curve = ot.Curve([1 + i, 1 + i], [lb[i], ub[i]]) curve.setLineWidth(2.0) curve.setColor(palette[1]) graph.add(curve) return graph .. GENERATED FROM PYTHON SOURCE LINES 130-131 sphinx_gallery_thumbnail_number = 2 .. GENERATED FROM PYTHON SOURCE LINES 133-134 Plot the SRC indices mean and confidence intervals. .. GENERATED FROM PYTHON SOURCE LINES 134-139 .. code-block:: Python src_mean = src_boot.computeMean() graph = draw_importance_factors_with_bounds(src_mean, input_names, alpha, src_interval) graph.setTitle(f"Importance factors - CI {(1.0 - alpha) * 100:.2f}%") _ = otv.View(graph) .. image-sg:: /auto_data_analysis/sample_analysis/images/sphx_glr_plot_src_confidence_002.png :alt: Importance factors - CI 95.00% :srcset: /auto_data_analysis/sample_analysis/images/sphx_glr_plot_src_confidence_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 140-161 We see that the variable :math:`Q` must be significant, because the lower bound of the confidence interval does not cross the X axis. Furthermore, its bounds are significantly greater than the bounds of the other variables (although perhaps less significantly for the :math:`K_s` variable). Hence, it must be recognized that :math:`Q` is the most important variable in this model, according to the linear regression model. We see that the variable :math:`Z_m` has an importance factor close to zero, taking into account the confidence bounds (which are very small in this case). Hence, the variable :math:`Z_m` could be replaced by a constant without reducing the variance of the output much. The variables :math:`K_s`, :math:`Z_v` and :math:`H_d` are somewhat in-between these two extreme situations. We cannot state that one of them is of greater importance than the other, because the confidence bounds are of comparable magnitude. Looking only at the importance factors, we may wrongly conclude that :math:`Z_v` has a greater impact than :math:`K_s` because the estimate of the importance factor for :math:`Z_v` is strictly greater than the estimate for :math:`K_s`. But taking into account for the variability of the estimators, this conclusion has no foundation, since confidence limits are comparable. In order to distinguish between the impact of these two variables, a larger sample size is needed. .. GENERATED FROM PYTHON SOURCE LINES 163-164 .. code-block:: Python otv.View.ShowAll() .. _sphx_glr_download_auto_data_analysis_sample_analysis_plot_src_confidence.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_src_confidence.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_src_confidence.py `