.. 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-23 .. code-block:: Python import openturns as ot .. GENERATED FROM PYTHON SOURCE LINES 24-25 We first create the function `model`. .. GENERATED FROM PYTHON SOURCE LINES 27-32 .. code-block:: Python 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 33-34 Then we create a sample `x` and compute the corresponding output sample `y`. .. GENERATED FROM PYTHON SOURCE LINES 36-41 .. code-block:: Python distribution = ot.Normal(dimension) samplesize = 1000 inputSample = distribution.getSample(samplesize) outputSample = model(inputSample) .. GENERATED FROM PYTHON SOURCE LINES 42-47 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 49-56 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 58-60 .. code-block:: Python ot.ResourceMap.GetAsUnsignedInteger("FittingTest-LillieforsMaximumSamplingSize") .. rst-class:: sphx-glr-script-out .. code-block:: none 100000 .. GENERATED FROM PYTHON SOURCE LINES 61-62 In order to speed this example up, let us reduce this value. .. GENERATED FROM PYTHON SOURCE LINES 64-66 .. code-block:: Python ot.ResourceMap.SetAsUnsignedInteger("FittingTest-LillieforsMaximumSamplingSize", 100) .. GENERATED FROM PYTHON SOURCE LINES 67-68 Then we fit the distribution. .. GENERATED FROM PYTHON SOURCE LINES 68-70 .. code-block:: Python distribution = ot.FunctionalChaosAlgorithm.BuildDistribution(inputSample) .. GENERATED FROM PYTHON SOURCE LINES 71-72 Let us explore the distribution with its fitted parameters. .. GENERATED FROM PYTHON SOURCE LINES 74-76 .. 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 77-78 We can also analyze its properties in more details. .. GENERATED FROM PYTHON SOURCE LINES 80-86 .. 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 87-89 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 91-93 .. code-block:: Python algo = ot.FunctionalChaosAlgorithm(inputSample, outputSample) .. GENERATED FROM PYTHON SOURCE LINES 94-96 The previous constructor is the main topic of the example :doc:`Create a polynomial chaos metamodel from a data set `. .. _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 `