.. 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_functional_chaos_database.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_functional_chaos_database.py: Create a full or sparse polynomial chaos expansion ================================================== .. GENERATED FROM PYTHON SOURCE LINES 7-17 In this example we create a global approximation of a model using polynomial chaos expansion based on a design of experiments. The goal of this example is to show how we can create a full or sparse polynomial chaos expansion depending on our needs and depending on the number of observations we have. In general, we should have more observations than parameters to estimate. This is why a sparse polynomial chaos may be interesting: by carefully selecting the coefficients we estimate, we may reduce overfitting and increase the predictions of the metamodel. .. 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-26 Define the model ~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 28-29 Create the function. .. GENERATED FROM PYTHON SOURCE LINES 29-33 .. code-block:: Python myModel = ot.SymbolicFunction( ["x1", "x2", "x3", "x4"], ["1 + x1 * x2 + 2 * x3^2 + x4^4"] ) .. GENERATED FROM PYTHON SOURCE LINES 34-35 Create a multivariate distribution. .. GENERATED FROM PYTHON SOURCE LINES 35-39 .. code-block:: Python distribution = ot.JointDistribution( [ot.Normal(), ot.Uniform(), ot.Gamma(2.75, 1.0), ot.Beta(2.5, 1.0, -1.0, 2.0)] ) .. GENERATED FROM PYTHON SOURCE LINES 40-46 In order to create the PCE, we can specify the distribution of the input parameters. If not known, statistical inference can be used to select a possible candidate, and fitting tests can validate such an hypothesis. Please read :doc:`Fit a distribution from an input sample ` for an example of this method. .. GENERATED FROM PYTHON SOURCE LINES 48-50 Create a training sample ~~~~~~~~~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 52-53 Create a pair of input and output samples. .. GENERATED FROM PYTHON SOURCE LINES 53-57 .. code-block:: Python sampleSize = 250 inputSample = distribution.getSample(sampleSize) outputSample = myModel(inputSample) .. GENERATED FROM PYTHON SOURCE LINES 58-60 Build the orthogonal basis ~~~~~~~~~~~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 62-64 In the next cell, we create the univariate orthogonal polynomial basis for each marginal. .. GENERATED FROM PYTHON SOURCE LINES 64-72 .. code-block:: Python inputDimension = inputSample.getDimension() coll = [ ot.StandardDistributionPolynomialFactory(distribution.getMarginal(i)) for i in range(inputDimension) ] enumerateFunction = ot.LinearEnumerateFunction(inputDimension) productBasis = ot.OrthogonalProductPolynomialFactory(coll, enumerateFunction) .. GENERATED FROM PYTHON SOURCE LINES 73-74 We can achieve the same result using :class:`~openturns.OrthogonalProductPolynomialFactory`. .. GENERATED FROM PYTHON SOURCE LINES 74-82 .. code-block:: Python marginalDistributionCollection = [ distribution.getMarginal(i) for i in range(inputDimension) ] multivariateBasis = ot.OrthogonalProductPolynomialFactory( marginalDistributionCollection ) multivariateBasis .. raw:: html
Index Name Distribution Univariate polynomial
0 X0 Normal HermiteFactory
1 X1 Uniform LegendreFactory
2 X2 Gamma LaguerreFactory
3 X3 Beta JacobiFactory


.. GENERATED FROM PYTHON SOURCE LINES 83-85 Create a full PCE ~~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 87-99 Create the algorithm. We compute the basis size from the total degree. The next lines use the :class:`~openturns.LeastSquaresStrategy` class with default parameters (the default is the :class:`~openturns.PenalizedLeastSquaresAlgorithmFactory` class). This creates a full polynomial chaos expansion, i.e. we keep all the candidate coefficients produced by the enumeration rule. In order to create a sparse polynomial chaos expansion, we must use the :class:`~openturns.LeastSquaresMetaModelSelectionFactory` class instead. .. GENERATED FROM PYTHON SOURCE LINES 99-111 .. code-block:: Python totalDegree = 3 candidateBasisSize = enumerateFunction.getBasisSizeFromTotalDegree(totalDegree) print("Candidate basis size = ", candidateBasisSize) adaptiveStrategy = ot.FixedStrategy(productBasis, candidateBasisSize) projectionStrategy = ot.LeastSquaresStrategy() algo = ot.FunctionalChaosAlgorithm( inputSample, outputSample, distribution, adaptiveStrategy, projectionStrategy ) algo.run() result = algo.getResult() result .. rst-class:: sphx-glr-script-out .. code-block:: none Candidate basis size = 35 .. raw:: html
FunctionalChaosResult
Index Multi-index Coeff.
0 [0,0,0,0] 26.12489
1 [1,0,0,0] -0.008183713
2 [0,1,0,0] 0.03242933
3 [0,0,1,0] 24.89812
4 [0,0,0,1] 3.977448
5 [2,0,0,0] -0.007980056
6 [1,1,0,0] 0.566605
7 [1,0,1,0] -0.01328741
8 [1,0,0,1] -0.01765562
9 [0,2,0,0] 0.003943191
10 [0,1,1,0] 0.02739853
11 [0,1,0,1] -0.0491181
12 [0,0,2,0] 9.098184
13 [0,0,1,1] -0.01695863
14 [0,0,0,2] 2.463476
15 [3,0,0,0] -0.01985544
16 [2,1,0,0] -0.007527326
17 [2,0,1,0] 0.02265436
18 [2,0,0,1] 0.02087004
19 [1,2,0,0] 0.02155156
20 [1,1,1,0] -0.01319882
21 [1,1,0,1] -0.01864534
22 [1,0,2,0] 0.0136746
23 [1,0,1,1] 0.02377915
24 [1,0,0,2] 0.05699986
25 [0,3,0,0] -0.006699248
26 [0,2,1,0] -0.02076579
27 [0,2,0,1] -0.0100742
28 [0,1,2,0] -0.02103417
29 [0,1,1,1] -0.008148804
30 [0,1,0,2] 0.0206051
31 [0,0,3,0] -0.01129148
32 [0,0,2,1] -0.0109149
33 [0,0,1,2] -0.003731515
34 [0,0,0,3] 0.946916


.. GENERATED FROM PYTHON SOURCE LINES 112-113 Get the number of coefficients in the PCE. .. GENERATED FROM PYTHON SOURCE LINES 113-116 .. code-block:: Python selectedBasisSizeFull = result.getIndices().getSize() print("Selected basis size = ", selectedBasisSizeFull) .. rst-class:: sphx-glr-script-out .. code-block:: none Selected basis size = 35 .. GENERATED FROM PYTHON SOURCE LINES 117-120 We see that the number of coefficients in the selected basis is equal to the number of coefficients in the candidate basis. This is, indeed, a *full* PCE. .. GENERATED FROM PYTHON SOURCE LINES 122-124 Use the PCE ~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 126-127 Get the metamodel function. .. GENERATED FROM PYTHON SOURCE LINES 127-129 .. code-block:: Python metamodel = result.getMetaModel() .. GENERATED FROM PYTHON SOURCE LINES 130-132 In order to evaluate the metamodel on a single point, we just use it as any other :class:`openturns.Function`. .. GENERATED FROM PYTHON SOURCE LINES 132-136 .. code-block:: Python xPoint = distribution.getMean() yPoint = metamodel(xPoint) print("Value at ", xPoint, " is ", yPoint) .. rst-class:: sphx-glr-script-out .. code-block:: none Value at [0,0,2.75,1.14286] is [17.7186] .. GENERATED FROM PYTHON SOURCE LINES 137-138 Print residuals. .. GENERATED FROM PYTHON SOURCE LINES 138-140 .. code-block:: Python result.getResiduals() .. raw:: html
class=Point name=Unnamed dimension=1 values=[0.0142158]


.. GENERATED FROM PYTHON SOURCE LINES 141-144 Based on these results, we may want to validate our metamodel. More details on this topic are presented in :doc:`Validate a polynomial chaos `. .. GENERATED FROM PYTHON SOURCE LINES 146-148 Create a sparse PCE ~~~~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 150-154 In order to create a sparse polynomial chaos expansion, we use the :class:`~openturns.LeastSquaresMetaModelSelectionFactory` class instead. .. GENERATED FROM PYTHON SOURCE LINES 154-167 .. code-block:: Python totalDegree = 6 candidateBasisSize = enumerateFunction.getBasisSizeFromTotalDegree(totalDegree) print("Candidate basis size = ", candidateBasisSize) adaptiveStrategy = ot.FixedStrategy(productBasis, candidateBasisSize) selectionAlgorithm = ot.LeastSquaresMetaModelSelectionFactory() projectionStrategy = ot.LeastSquaresStrategy(selectionAlgorithm) algo = ot.FunctionalChaosAlgorithm( inputSample, outputSample, distribution, adaptiveStrategy, projectionStrategy ) algo.run() result = algo.getResult() result .. rst-class:: sphx-glr-script-out .. code-block:: none Candidate basis size = 210 .. raw:: html
FunctionalChaosResult
Index Multi-index Coeff.
0 [0,0,0,0] 26.11651
1 [0,0,1,0] 24.87469
2 [0,0,0,1] 3.974438
3 [1,1,0,0] 0.5773503
4 [0,0,2,0] 9.082951
5 [0,0,0,2] 2.419672
6 [0,0,0,3] 0.9657677
7 [0,0,0,4] 0.2409653
8 [0,0,2,4] -1.030422e-14


.. GENERATED FROM PYTHON SOURCE LINES 168-169 Get the number of coefficients in the PCE. .. GENERATED FROM PYTHON SOURCE LINES 169-172 .. code-block:: Python selectedBasisSizeSparse = result.getIndices().getSize() print("Selected basis size = ", selectedBasisSizeSparse) .. rst-class:: sphx-glr-script-out .. code-block:: none Selected basis size = 9 .. GENERATED FROM PYTHON SOURCE LINES 173-177 We see that the number of selected coefficients is lower than the number of candidate coefficients. This may reduce overfitting and can produce a PCE with more accurate predictions. .. _sphx_glr_download_auto_meta_modeling_polynomial_chaos_metamodel_plot_functional_chaos_database.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_functional_chaos_database.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_functional_chaos_database.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_functional_chaos_database.zip `