.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_meta_modeling/polynomial_chaos_metamodel/plot_chaos_build_distribution.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_meta_modeling_polynomial_chaos_metamodel_plot_chaos_build_distribution.py: Fit a distribution from an input sample ======================================= .. GENERATED FROM PYTHON SOURCE LINES 6-16 In this example we show how to use the :meth:`~openturns.MetaModelAlgorithm.BuildDistribution` function to fit a distribution to an input sample. This function is used by the :class:`~openturns.FunctionalChaosAlgorithm` class when we want to create a polynomial chaos metamodel and we have a design of experiments which have been computed beforehand. In this case, we have to identify the distributions which best fit to the input sample in order to define the input probabilistic model. This is, in turn, used by in the polynomial chaos to create the orthogonal basis. .. GENERATED FROM PYTHON SOURCE LINES 19-23 .. code-block:: Python import openturns as ot ot.Log.Show(ot.Log.NONE) .. GENERATED FROM PYTHON SOURCE LINES 24-25 We first create the function `model`. .. GENERATED FROM PYTHON SOURCE LINES 27-33 .. code-block:: Python ot.RandomGenerator.SetSeed(0) dimension = 2 input_names = ["x1", "x2"] formulas = ["cos(x1 + x2)", "(x2 + 1) * exp(x1)"] model = ot.SymbolicFunction(input_names, formulas) .. GENERATED FROM PYTHON SOURCE LINES 34-35 Then we create a sample `x` and compute the corresponding output sample `y`. .. GENERATED FROM PYTHON SOURCE LINES 37-42 .. code-block:: Python distribution = ot.Normal(dimension) samplesize = 1000 inputSample = distribution.getSample(samplesize) outputSample = model(inputSample) .. GENERATED FROM PYTHON SOURCE LINES 43-48 Create a functional chaos model. The algorithm used by :meth:`~openturns.MetaModelAlgorithm.BuildDistribution` fits a distribution on the input sample. This is done with the Lilliefors test. Please read :ref:`kolmogorov_smirnov_test` for more details on this topic. .. GENERATED FROM PYTHON SOURCE LINES 50-57 The Lilliefors test is based on sampling the distribution of the Kolmogorov-Smirnov statistics. The sample size corresponding to this algorithm is configured by the `"FittingTest-LillieforsMaximumSamplingSize"` :class:`~openturns.ResourceMap` key. In order to get satisfactory results, the default value of this key is relatively large. .. GENERATED FROM PYTHON SOURCE LINES 59-61 .. code-block:: Python ot.ResourceMap.GetAsUnsignedInteger("FittingTest-LillieforsMaximumSamplingSize") .. rst-class:: sphx-glr-script-out .. code-block:: none 100000 .. GENERATED FROM PYTHON SOURCE LINES 62-63 In order to speed this example up, let us reduce this value. .. GENERATED FROM PYTHON SOURCE LINES 65-67 .. code-block:: Python ot.ResourceMap.SetAsUnsignedInteger("FittingTest-LillieforsMaximumSamplingSize", 100) .. GENERATED FROM PYTHON SOURCE LINES 68-69 Then we fit the distribution. .. GENERATED FROM PYTHON SOURCE LINES 69-71 .. code-block:: Python distribution = ot.FunctionalChaosAlgorithm.BuildDistribution(inputSample) .. GENERATED FROM PYTHON SOURCE LINES 72-73 Let us explore the distribution with its fitted parameters. .. GENERATED FROM PYTHON SOURCE LINES 75-77 .. code-block:: Python distribution .. raw:: html
ComposedDistribution
Index Variable Distribution
0 X0 Normal(mu = -0.0268588, sigma = 0.985838)
1 X1 Normal(mu = 0.00246667, sigma = 1.00015)


.. GENERATED FROM PYTHON SOURCE LINES 78-79 We can also analyse its properties in more detail. .. GENERATED FROM PYTHON SOURCE LINES 81-87 .. code-block:: Python for i in range(dimension): marginal = distribution.getMarginal(i) marginalname = marginal.getImplementation().getClassName() print("Marginal #", i, ":", marginalname) distribution.getCopula() .. rst-class:: sphx-glr-script-out .. code-block:: none Marginal # 0 : Normal Marginal # 1 : Normal .. raw:: html
IndependentCopula


.. GENERATED FROM PYTHON SOURCE LINES 88-90 The previous call to :meth:`~openturns.MetaModelAlgorithm.BuildDistribution` is what is done internally by the following constructor of `FunctionalChaosAlgorithm`. .. GENERATED FROM PYTHON SOURCE LINES 92-94 .. code-block:: Python algo = ot.FunctionalChaosAlgorithm(inputSample, outputSample) .. GENERATED FROM PYTHON SOURCE LINES 95-97 The previous constructor is the main topic of the example :doc:`Create a polynomial chaos metamodel from a data set `. .. GENERATED FROM PYTHON SOURCE LINES 99-100 .. code-block:: Python ot.ResourceMap.Reload() # reset default settings .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 2.122 seconds) .. _sphx_glr_download_auto_meta_modeling_polynomial_chaos_metamodel_plot_chaos_build_distribution.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_chaos_build_distribution.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_chaos_build_distribution.py `