.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_sensitivity_analysis/plot_sensitivity_low_discrepancy.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_sensitivity_analysis_plot_sensitivity_low_discrepancy.py: Sobol' sensitivity indices using a low-discrepancy sequence =========================================================== .. GENERATED FROM PYTHON SOURCE LINES 7-13 In this example, we estimate Sobol' sensitivity indices for the :ref:`Ishigami function ` using quasi-Monte Carlo sampling with a low-discrepancy sequence (Sobol' sequence). We compare its estimation accuracy and convergence rate to those obtained with a standard Latin Hypercube Sampling (LHS) design of experiments. .. GENERATED FROM PYTHON SOURCE LINES 15-19 Model definition and analytical indices --------------------------------------- We load the Ishigami model from the usecases module. .. GENERATED FROM PYTHON SOURCE LINES 21-30 .. code-block:: Python import openturns as ot import openturns.viewer as otv from openturns.usecases import ishigami_function im = ishigami_function.IshigamiModel() distribution = im.distribution model = im.model dim = distribution.getDimension() .. GENERATED FROM PYTHON SOURCE LINES 31-33 The exact first-order (:math:`S_i`) and total-order (:math:`ST_i`) Sobol' indices are available from the use case: .. GENERATED FROM PYTHON SOURCE LINES 33-38 .. code-block:: Python print(f"Exact first-order indices: S1={im.S1:.4f}, S2={im.S2:.4f}, S3={im.S3:.4f}") print( f"Exact total-order indices: ST1={im.ST1:.4f}, ST2={im.ST2:.4f}, ST3={im.ST3:.4f}" ) .. rst-class:: sphx-glr-script-out .. code-block:: none Exact first-order indices: S1=0.3139, S2=0.4424, S3=0.0000 Exact total-order indices: ST1=0.5576, ST2=0.4424, ST3=0.2437 .. GENERATED FROM PYTHON SOURCE LINES 39-43 Standard Monte Carlo estimation ------------------------------- We first estimate the indices using standard Monte Carlo sampling. .. GENERATED FROM PYTHON SOURCE LINES 43-55 .. code-block:: Python ot.RandomGenerator.SetSeed(0) size = 1024 experiment_mc = ot.SobolIndicesExperiment(distribution, size, False) input_design_mc = experiment_mc.generate() output_design_mc = model(input_design_mc) algo_mc = ot.SaltelliSensitivityAlgorithm(input_design_mc, output_design_mc, size) print("+ Monte Carlo experiment:") print(" First-order indices:", algo_mc.getFirstOrderIndices()) print(" Total-order indices:", algo_mc.getTotalOrderIndices()) .. rst-class:: sphx-glr-script-out .. code-block:: none + Monte Carlo experiment: First-order indices: [0.338617,0.440851,0.0103021] Total-order indices: [0.564123,0.414875,0.250713] .. GENERATED FROM PYTHON SOURCE LINES 56-62 Low-discrepancy experiment (Sobol' sequence) -------------------------------------------- We replace the pseudo-random generator with a deterministic low-discrepancy sequence (:class:`~openturns.SobolSequence`). For Sobol' sequences, sample sizes equal to powers of two (:math:`2^m`) yield optimal uniformity. .. GENERATED FROM PYTHON SOURCE LINES 62-70 .. code-block:: Python sequence = ot.SobolSequence(dim) experiment_lds = ot.LowDiscrepancyExperiment(sequence, distribution, size) algo_lds = ot.SaltelliSensitivityAlgorithm(experiment_lds, model) print("+ Low-discrepancy (Sobol' sequence) experiment:") print(" First-order indices:", algo_lds.getFirstOrderIndices()) print(" Total-order indices:", algo_lds.getTotalOrderIndices()) .. rst-class:: sphx-glr-script-out .. code-block:: none + Low-discrepancy (Sobol' sequence) experiment: First-order indices: [0.318979,0.438101,-0.0294674] Total-order indices: [0.558409,0.439004,0.243178] .. GENERATED FROM PYTHON SOURCE LINES 71-75 Latin Hypercube Sampling (LHS) experiment ----------------------------------------- We now estimate the indices using Latin Hypercube Sampling (:class:`~openturns.LHSExperiment`). .. GENERATED FROM PYTHON SOURCE LINES 75-82 .. code-block:: Python experiment_lhs = ot.LHSExperiment(distribution, size) algo_lhs = ot.SaltelliSensitivityAlgorithm(experiment_lhs, model) print("+ LHS experiment:") print(" First-order indices:", algo_lhs.getFirstOrderIndices()) print(" Total-order indices:", algo_lhs.getTotalOrderIndices()) .. rst-class:: sphx-glr-script-out .. code-block:: none + LHS experiment: First-order indices: [0.322066,0.460579,-0.0253957] Total-order indices: [0.584051,0.448481,0.230591] .. GENERATED FROM PYTHON SOURCE LINES 83-93 Convergence comparison: Sobol' sequence vs LHS ---------------------------------------------- We evaluate the absolute error for each individual first-order (:math:`S_i`) and total-order (:math:`ST_i`) index as a function of sample size: .. math:: \epsilon(S_i) = |S_i - S_i^*|, \quad \epsilon(ST_i) = |ST_i - ST_i^*| We compare powers of two from :math:`2^4 = 16` up to :math:`2^{11} = 2048`. .. GENERATED FROM PYTHON SOURCE LINES 93-129 .. code-block:: Python num_iterations = 16 sample_sizes = ot.Sample(num_iterations, 1) err_fo_lds = ot.Sample(num_iterations, dim) err_to_lds = ot.Sample(num_iterations, dim) err_fo_lhs = ot.Sample(num_iterations, dim) err_to_lhs = ot.Sample(num_iterations, dim) exact_s = [im.S1, im.S2, im.S3] exact_st = [im.ST1, im.ST2, im.ST3] current_size = 16 for i in range(num_iterations): sample_sizes[i, 0] = current_size # Low-discrepancy estimation exp_sobol = ot.LowDiscrepancyExperiment(sequence, distribution, current_size) algo_sobol = ot.SaltelliSensitivityAlgorithm(exp_sobol, model) fo_lds = algo_sobol.getFirstOrderIndices() to_lds = algo_sobol.getTotalOrderIndices() for k in range(dim): err_fo_lds[i, k] = abs(fo_lds[k] - exact_s[k]) err_to_lds[i, k] = abs(to_lds[k] - exact_st[k]) # Latin Hypercube Sampling estimation exp_lhs = ot.LHSExperiment(distribution, current_size) algo_lhs_it = ot.SaltelliSensitivityAlgorithm(exp_lhs, model) fo_lhs = algo_lhs_it.getFirstOrderIndices() to_lhs = algo_lhs_it.getTotalOrderIndices() for k in range(dim): err_fo_lhs[i, k] = abs(fo_lhs[k] - exact_s[k]) err_to_lhs[i, k] = abs(to_lhs[k] - exact_st[k]) current_size *= 2 .. GENERATED FROM PYTHON SOURCE LINES 130-136 Display convergence graphs -------------------------- We plot the absolute errors for each variable, for both first- and total-order indices. Solid lines represent the Sobol' sequence and dashed lines represent LHS, with matching colors per sensitivity index. .. GENERATED FROM PYTHON SOURCE LINES 136-195 .. code-block:: Python graph_first = ot.Graph( "First-order indices", "Sample size", "Absolute error", True, ) graph_total = ot.Graph( "Total-order indices", "Sample size", "Absolute error", True, ) palette = ot.Drawable.BuildTableauPalette(2 * dim) # First order indices for k in range(dim): curve_lds = ot.Curve(sample_sizes, err_fo_lds[:, k]) curve_lds.setLegend(f"S{k + 1} (Sobol')") curve_lds.setColor(palette[k]) curve_lds.setLineWidth(2) graph_first.add(curve_lds) curve_lhs = ot.Curve(sample_sizes, err_fo_lhs[:, k]) curve_lhs.setLegend(f"S{k + 1} (LHS)") curve_lhs.setColor(palette[k]) curve_lhs.setLineWidth(2) curve_lhs.setLineStyle("dashed") graph_first.add(curve_lhs) # Total order indices for k in range(dim): curve_lds = ot.Curve(sample_sizes, err_to_lds[:, k]) curve_lds.setLegend(f"ST{k + 1} (Sobol')") curve_lds.setColor(palette[k]) curve_lds.setLineWidth(2) graph_total.add(curve_lds) curve_lhs = ot.Curve(sample_sizes, err_to_lhs[:, k]) curve_lhs.setLegend(f"ST{k + 1} (LHS)") curve_lhs.setColor(palette[k]) curve_lhs.setLineWidth(2) curve_lhs.setLineStyle("dashed") graph_total.add(curve_lhs) graph_first.setLogScale(ot.GraphImplementation.LOGXY) graph_first.setLegendPosition("bottomleft") graph_total.setLogScale(ot.GraphImplementation.LOGXY) graph_total.setLegendPosition("bottomleft") grid = ot.GridLayout(1, 2) grid.setTitle("Sensitivity convergence per variable: Sobol' sequence vs LHS") grid.setGraph(0, 0, graph_first) grid.setGraph(0, 1, graph_total) view = otv.View(grid) .. image-sg:: /auto_sensitivity_analysis/images/sphx_glr_plot_sensitivity_low_discrepancy_001.svg :alt: Sensitivity convergence per variable: Sobol' sequence vs LHS, First-order indices, Total-order indices :srcset: /auto_sensitivity_analysis/images/sphx_glr_plot_sensitivity_low_discrepancy_001.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 196-199 The quasi-Monte Carlo approach using the Sobol' low-discrepancy sequence converges more rapidly across individual indices than the standard Latin Hypercube Sampling strategy. .. GENERATED FROM PYTHON SOURCE LINES 201-202 Display all figures .. GENERATED FROM PYTHON SOURCE LINES 202-204 .. code-block:: Python otv.View.ShowAll() .. _sphx_glr_download_auto_sensitivity_analysis_plot_sensitivity_low_discrepancy.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_sensitivity_low_discrepancy.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_sensitivity_low_discrepancy.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_sensitivity_low_discrepancy.zip `