.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/uncertainty/plot_u_parameter_space.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_examples_uncertainty_plot_u_parameter_space.py: Parameter space =============== In this example, we will see the basics of :class:`.ParameterSpace`. .. GENERATED FROM PYTHON SOURCE LINES 28-39 .. code-block:: default from __future__ import annotations from gemseo import configure_logger from gemseo import create_discipline from gemseo import create_scenario from gemseo.algos.parameter_space import ParameterSpace from gemseo.post.dataset.scatter_plot_matrix import ScatterMatrix configure_logger() .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 40-44 Create a parameter space ------------------------ Firstly, the creation of a :class:`.ParameterSpace` does not require any mandatory argument: .. GENERATED FROM PYTHON SOURCE LINES 44-46 .. code-block:: default parameter_space = ParameterSpace() .. GENERATED FROM PYTHON SOURCE LINES 47-52 Then, we can add either deterministic variables from their lower and upper bounds (use :meth:`.ParameterSpace.add_variable`) or uncertain variables from their distribution names and parameters (use :meth:`.ParameterSpace.add_random_variable`) .. GENERATED FROM PYTHON SOURCE LINES 52-56 .. code-block:: default parameter_space.add_variable("x", l_b=-2.0, u_b=2.0) parameter_space.add_random_variable("y", "SPNormalDistribution", mu=0.0, sigma=1.0) parameter_space .. raw:: html
Parameter space:
name lower_bound value upper_bound type Initial distribution
x -2 None 2 float
y -inf 0 inf float norm(mu=0.0, sigma=1.0)


.. GENERATED FROM PYTHON SOURCE LINES 57-59 We can check that the variables *x* and *y* are implemented as deterministic and deterministic variables respectively: .. GENERATED FROM PYTHON SOURCE LINES 59-61 .. code-block:: default parameter_space.is_deterministic("x"), parameter_space.is_uncertain("y") .. rst-class:: sphx-glr-script-out .. code-block:: none (True, True) .. GENERATED FROM PYTHON SOURCE LINES 62-66 Sample from the parameter space ------------------------------- We can sample the uncertain variables from the :class:`.ParameterSpace` and get values either as a NumPy array (by default) .. GENERATED FROM PYTHON SOURCE LINES 66-69 .. code-block:: default sample = parameter_space.compute_samples(n_samples=2, as_dict=True) sample .. rst-class:: sphx-glr-script-out .. code-block:: none [{'y': array([0.80969516])}, {'y': array([2.46015528])}] .. GENERATED FROM PYTHON SOURCE LINES 70-71 or as a dictionary of NumPy arrays indexed by the names of the variables: .. GENERATED FROM PYTHON SOURCE LINES 71-74 .. code-block:: default sample = parameter_space.compute_samples(n_samples=4) sample .. rst-class:: sphx-glr-script-out .. code-block:: none array([[0.36250283], [0.70154774], [0.34327059], [0.39628863]]) .. GENERATED FROM PYTHON SOURCE LINES 75-80 Sample a discipline over the parameter space -------------------------------------------- We can also sample a discipline over the parameter space. For simplicity, we instantiate an :class:`.AnalyticDiscipline` from a dictionary of expressions: .. GENERATED FROM PYTHON SOURCE LINES 80-82 .. code-block:: default discipline = create_discipline("AnalyticDiscipline", expressions={"z": "x+y"}) .. GENERATED FROM PYTHON SOURCE LINES 83-100 From these parameter space and discipline, we build a :class:`.DOEScenario` and execute it with a Latin Hypercube Sampling algorithm and 100 samples. .. warning:: A :class:`.DOEScenario` considers all the variables available in its :class:`.DesignSpace`. By inheritance, in the special case of a :class:`.ParameterSpace`, a :class:`.DOEScenario` considers all the variables available in this :class:`.ParameterSpace`. Thus, if we do not filter the uncertain variables, the :class:`.DOEScenario` will consider both the deterministic variables as uniformly distributed variables and the uncertain variables with their specified probability distributions. .. GENERATED FROM PYTHON SOURCE LINES 100-106 .. code-block:: default scenario = create_scenario( [discipline], "DisciplinaryOpt", "z", parameter_space, scenario_type="DOE" ) scenario.execute({"algo": "lhs", "n_samples": 100}) .. rst-class:: sphx-glr-script-out .. code-block:: none INFO - 08:22:41: INFO - 08:22:41: *** Start DOEScenario execution *** INFO - 08:22:41: DOEScenario INFO - 08:22:41: Disciplines: AnalyticDiscipline INFO - 08:22:41: MDO formulation: DisciplinaryOpt INFO - 08:22:41: Optimization problem: INFO - 08:22:41: minimize z(x, y) INFO - 08:22:41: with respect to x, y INFO - 08:22:41: over the design space: INFO - 08:22:41: +------+-------------+-------+-------------+-------+-------------------------+ INFO - 08:22:41: | name | lower_bound | value | upper_bound | type | Initial distribution | INFO - 08:22:41: +------+-------------+-------+-------------+-------+-------------------------+ INFO - 08:22:41: | x | -2 | None | 2 | float | | INFO - 08:22:41: | y | -inf | 0 | inf | float | norm(mu=0.0, sigma=1.0) | INFO - 08:22:41: +------+-------------+-------+-------------+-------+-------------------------+ INFO - 08:22:41: Solving optimization problem with algorithm lhs: INFO - 08:22:41: ... 0%| | 0/100 [00:00
GROUP designs functions
VARIABLE x y z
COMPONENT 0 0 0
1 1.869403 1.246453 3.115855
2 -1.567970 3.285041 1.717071
3 0.282640 -0.101706 0.180934
4 1.916313 1.848317 3.764630
5 1.562653 0.586038 2.148691
... ... ... ...
6 0.120633 -0.327477 -0.206844
7 -0.999225 1.461403 0.462178
8 -1.396066 -0.972779 -2.368845
9 1.090093 0.225565 1.315658
10 -1.433207 -0.779330 -2.212536

100 rows × 3 columns



.. GENERATED FROM PYTHON SOURCE LINES 115-117 or with a graphical post-processing, e.g. a scatter plot matrix: .. GENERATED FROM PYTHON SOURCE LINES 117-119 .. code-block:: default ScatterMatrix(dataset).execute(save=False, show=True) .. image-sg:: /examples/uncertainty/images/sphx_glr_plot_u_parameter_space_001.png :alt: plot u parameter space :srcset: /examples/uncertainty/images/sphx_glr_plot_u_parameter_space_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none [
] .. GENERATED FROM PYTHON SOURCE LINES 120-124 Sample a discipline over the uncertain space -------------------------------------------- If we want to sample a discipline over the uncertain space, we need to extract it: .. GENERATED FROM PYTHON SOURCE LINES 124-126 .. code-block:: default uncertain_space = parameter_space.extract_uncertain_space() .. GENERATED FROM PYTHON SOURCE LINES 127-129 Then, we clear the cache, create a new scenario from this parameter space containing only the uncertain variables and execute it. .. GENERATED FROM PYTHON SOURCE LINES 129-134 .. code-block:: default scenario = create_scenario( [discipline], "DisciplinaryOpt", "z", uncertain_space, scenario_type="DOE" ) scenario.execute({"algo": "lhs", "n_samples": 100}) .. rst-class:: sphx-glr-script-out .. code-block:: none INFO - 08:22:41: INFO - 08:22:41: *** Start DOEScenario execution *** INFO - 08:22:41: DOEScenario INFO - 08:22:41: Disciplines: AnalyticDiscipline INFO - 08:22:41: MDO formulation: DisciplinaryOpt INFO - 08:22:41: Optimization problem: INFO - 08:22:41: minimize z(y) INFO - 08:22:41: with respect to y INFO - 08:22:41: over the design space: INFO - 08:22:41: +------+-------------+--------------------+-------------+-------+-------------------------+ INFO - 08:22:41: | name | lower_bound | value | upper_bound | type | Initial distribution | INFO - 08:22:41: +------+-------------+--------------------+-------------+-------+-------------------------+ INFO - 08:22:41: | y | -inf | -1.368441899688857 | inf | float | norm(mu=0.0, sigma=1.0) | INFO - 08:22:41: +------+-------------+--------------------+-------------+-------+-------------------------+ INFO - 08:22:41: Solving optimization problem with algorithm lhs: INFO - 08:22:41: ... 0%| | 0/100 [00:00
GROUP designs functions
VARIABLE y z
COMPONENT 0 0
1 -0.640726 -0.640726
2 -0.393653 -0.393653
3 0.550565 0.550565
4 0.944369 0.944369
5 -2.115275 -2.115275
... ... ...
6 0.081947 0.081947
7 -1.085812 -1.085812
8 -0.761651 -0.761651
9 -0.042932 -0.042932
10 -0.813354 -0.813354

100 rows × 2 columns



.. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.640 seconds) .. _sphx_glr_download_examples_uncertainty_plot_u_parameter_space.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_u_parameter_space.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_u_parameter_space.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_