.. 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 7-17 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 20-24 .. code-block:: Python import openturns as ot ot.Log.Show(ot.Log.NONE) .. GENERATED FROM PYTHON SOURCE LINES 25-26 We first create the function `model`. .. GENERATED FROM PYTHON SOURCE LINES 28-34 .. 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 35-36 Then we create a sample `x` and compute the corresponding output sample `y`. .. GENERATED FROM PYTHON SOURCE LINES 38-43 .. code-block:: Python distribution = ot.Normal(dimension) samplesize = 1000 inputSample = distribution.getSample(samplesize) outputSample = model(inputSample) .. GENERATED FROM PYTHON SOURCE LINES 44-49 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 51-58 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 60-62 .. code-block:: Python ot.ResourceMap.GetAsUnsignedInteger("FittingTest-LillieforsMaximumSamplingSize") .. rst-class:: sphx-glr-script-out .. code-block:: none 100000 .. GENERATED FROM PYTHON SOURCE LINES 63-64 In order to speed this example up, let us reduce this value. .. GENERATED FROM PYTHON SOURCE LINES 66-68 .. code-block:: Python ot.ResourceMap.SetAsUnsignedInteger("FittingTest-LillieforsMaximumSamplingSize", 100) .. GENERATED FROM PYTHON SOURCE LINES 69-70 Then we fit the distribution. .. GENERATED FROM PYTHON SOURCE LINES 70-72 .. code-block:: Python distribution = ot.FunctionalChaosAlgorithm.BuildDistribution(inputSample) .. GENERATED FROM PYTHON SOURCE LINES 73-74 Let us explore the distribution with its fitted parameters. .. GENERATED FROM PYTHON SOURCE LINES 76-78 .. code-block:: Python distribution .. raw:: html
JointDistribution
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 79-80 We can also analyze its properties in more details. .. GENERATED FROM PYTHON SOURCE LINES 82-88 .. 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 89-91 The previous call to :meth:`~openturns.MetaModelAlgorithm.BuildDistribution` is what is done internally by the following constructor of :class:`~openturns.FunctionalChaosAlgorithm`. .. GENERATED FROM PYTHON SOURCE LINES 93-95 .. code-block:: Python algo = ot.FunctionalChaosAlgorithm(inputSample, outputSample) .. GENERATED FROM PYTHON SOURCE LINES 96-98 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 100-101 .. code-block:: Python ot.ResourceMap.Reload() # reset default settings .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 2.367 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 ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_chaos_build_distribution.zip `