.. 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_advanced_ctors.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_advanced_ctors.py: Advanced polynomial chaos construction ====================================== .. GENERATED FROM PYTHON SOURCE LINES 6-11 In this example we are going to expose advanced elements in the construction of a polynomial chaos algorithm: - construction of the multivariate orthonormal basis, - truncature strategy of the multivariate orthonormal basis, - evaluation strategy of the approximation coefficients. .. GENERATED FROM PYTHON SOURCE LINES 13-28 In this example, we consider the following function :math:`\mathbb{R}^4 \rightarrow \mathbb{R}`: .. math:: g(\mathbf{x}) = 1+x_1 x_2 + 2 x_3^2+x_4^4 for any :math:`x_1,x_2,x_3,x_4\in\mathbb{R}`. We assume that the inputs have normal, uniform, gamma and beta distributions : .. math:: X_1 \sim \mathcal{N}(0,1), \qquad X_2 \sim \mathcal{U}(-1,1), \qquad X_3 \sim \mathcal{G}(2.75,1), \qquad X_4 \sim \mathcal{B}(2.5,1,-1,2), and :math:`X_1`, :math:`X_2`, :math:`X_3` and :math:`X_4` are independent. .. GENERATED FROM PYTHON SOURCE LINES 30-32 Define the model and the input distribution ------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 34-38 .. code-block:: Python import openturns as ot ot.Log.Show(ot.Log.NONE) .. GENERATED FROM PYTHON SOURCE LINES 39-41 .. code-block:: Python model = ot.SymbolicFunction(["x1", "x2", "x3", "x4"], ["1+x1*x2 + 2*x3^2+x4^4"]) .. GENERATED FROM PYTHON SOURCE LINES 42-43 Create a distribution of dimension 4. .. GENERATED FROM PYTHON SOURCE LINES 45-49 .. code-block:: Python distribution = ot.ComposedDistribution( [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 50-53 .. code-block:: Python inputDimension = distribution.getDimension() inputDimension .. rst-class:: sphx-glr-script-out .. code-block:: none 4 .. GENERATED FROM PYTHON SOURCE LINES 54-56 STEP 1: Construction of the multivariate orthonormal basis ---------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 58-59 Create the univariate polynomial family collection which regroups the polynomial families for each direction. .. GENERATED FROM PYTHON SOURCE LINES 61-63 .. code-block:: Python polyColl = ot.PolynomialFamilyCollection(inputDimension) .. GENERATED FROM PYTHON SOURCE LINES 64-65 We could use the Krawtchouk and Charlier families (for discrete distributions). .. GENERATED FROM PYTHON SOURCE LINES 67-70 .. code-block:: Python polyColl[0] = ot.KrawtchoukFactory() polyColl[1] = ot.CharlierFactory() .. GENERATED FROM PYTHON SOURCE LINES 71-72 We could also use the automatic selection of the polynomial which corresponds to the distribution: this is done with the `StandardDistributionPolynomialFactory` class. .. GENERATED FROM PYTHON SOURCE LINES 74-78 .. code-block:: Python for i in range(inputDimension): marginal = distribution.getMarginal(i) polyColl[i] = ot.StandardDistributionPolynomialFactory(marginal) .. GENERATED FROM PYTHON SOURCE LINES 79-80 In our specific case, we use specific polynomial factories. .. GENERATED FROM PYTHON SOURCE LINES 82-88 .. code-block:: Python polyColl[0] = ot.HermiteFactory() polyColl[1] = ot.LegendreFactory() polyColl[2] = ot.LaguerreFactory(2.75) # Parameter for the Jacobi factory : 'Probabilty' encoded with 1 polyColl[3] = ot.JacobiFactory(2.5, 3.5, 1) .. GENERATED FROM PYTHON SOURCE LINES 89-90 Create the enumeration function. .. GENERATED FROM PYTHON SOURCE LINES 92-93 The first possibility is to use the `LinearEnumerateFunction`. .. GENERATED FROM PYTHON SOURCE LINES 95-99 .. code-block:: Python enumerateFunction = ot.LinearEnumerateFunction(inputDimension) multivariateBasis = ot.OrthogonalProductPolynomialFactory(polyColl, enumerateFunction) 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 100-101 Another possibility is to use the `HyperbolicAnisotropicEnumerateFunction`, which gives less weight to interactions. .. GENERATED FROM PYTHON SOURCE LINES 103-106 .. code-block:: Python q = 0.4 enumerateFunction = ot.HyperbolicAnisotropicEnumerateFunction(inputDimension, q) .. GENERATED FROM PYTHON SOURCE LINES 107-108 Create the multivariate orthonormal basis which is the cartesian product of the univariate basis. .. GENERATED FROM PYTHON SOURCE LINES 110-113 .. code-block:: Python multivariateBasis = ot.OrthogonalProductPolynomialFactory(polyColl, enumerateFunction) 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 114-116 Ask how many basis terms there are in the 6-th strata. In the special case of the Linear enumerate function this is also the strata with all the multi-indices of total degree 5. .. GENERATED FROM PYTHON SOURCE LINES 116-119 .. code-block:: Python k = 5 enumerateFunction.getStrataCardinal(k) .. rst-class:: sphx-glr-script-out .. code-block:: none 4 .. GENERATED FROM PYTHON SOURCE LINES 120-121 Ask how many basis multi-indices have total degrees lower or equal to k=5. .. GENERATED FROM PYTHON SOURCE LINES 121-123 .. code-block:: Python enumerateFunction.getBasisSizeFromTotalDegree(k) .. rst-class:: sphx-glr-script-out .. code-block:: none 27 .. GENERATED FROM PYTHON SOURCE LINES 124-125 Give the k-th term of the multivariate basis. To calculate its degree, add the integers. .. GENERATED FROM PYTHON SOURCE LINES 125-128 .. code-block:: Python k = 5 enumerateFunction(k) .. raw:: html
[2,0,0,0]


.. GENERATED FROM PYTHON SOURCE LINES 129-130 Build a term of the basis as a Function. Generally, we do not need to construct manually any term, all terms are constructed automatically by a strategy of construction of the basis. .. GENERATED FROM PYTHON SOURCE LINES 132-136 .. code-block:: Python i = 5 Psi_i = multivariateBasis.build(i) Psi_i .. raw:: html
-0.707107 + 0.707107 × x02


.. GENERATED FROM PYTHON SOURCE LINES 137-138 Get the measure mu associated to the multivariate basis. .. GENERATED FROM PYTHON SOURCE LINES 140-143 .. code-block:: Python distributionStandard = multivariateBasis.getMeasure() distributionStandard .. raw:: html
ComposedDistribution
Index Variable Distribution
0 X0 Normal(mu = 0, sigma = 1)
1 X1 Uniform(a = -1, b = 1)
2 X2 Gamma(k = 3.75, lambda = 1, gamma = 0)
3 X3 Beta(alpha = 2.5, beta = 1, a = -1, b = 1)


.. GENERATED FROM PYTHON SOURCE LINES 144-146 STEP 2: Truncature strategy of the multivariate orthonormal basis ----------------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 148-149 FixedStrategy : all the polynomials af degree lower or equal to 2 which corresponds to the 15 first ones. .. GENERATED FROM PYTHON SOURCE LINES 151-154 .. code-block:: Python p = 15 truncatureBasisStrategy = ot.FixedStrategy(multivariateBasis, p) .. GENERATED FROM PYTHON SOURCE LINES 155-158 CleaningStrategy : among the maximumConsideredTerms = 500 first polynomials, those which have the mostSignificant = 50 most significant contributions with significance criterion significanceFactor equal to :math:`10^{-4}` The `True` boolean indicates if we are interested in the online monitoring of the current basis update (removed or added coefficients). .. GENERATED FROM PYTHON SOURCE LINES 160-167 .. code-block:: Python maximumConsideredTerms = 500 mostSignificant = 50 significanceFactor = 1.0e-4 truncatureBasisStrategy_2 = ot.CleaningStrategy( multivariateBasis, maximumConsideredTerms, mostSignificant, significanceFactor ) .. GENERATED FROM PYTHON SOURCE LINES 168-170 STEP 3: Evaluation strategy of the approximation coefficients ------------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 172-173 The technique illustrated is the Least Squares technique where the points come from an design of experiments. Here : the Monte Carlo sampling technique. .. GENERATED FROM PYTHON SOURCE LINES 175-179 .. code-block:: Python sampleSize = 100 evaluationCoeffStrategy = ot.LeastSquaresStrategy() experiment = ot.MonteCarloExperiment(distribution, sampleSize) .. GENERATED FROM PYTHON SOURCE LINES 180-181 You can specify the approximation algorithm. This is the algorithm that generates a sequence of basis using Least Angle Regression. .. GENERATED FROM PYTHON SOURCE LINES 183-185 .. code-block:: Python basisSequenceFactory = ot.LARS() .. GENERATED FROM PYTHON SOURCE LINES 186-187 This algorithm estimates the empirical error on each sub-basis using Leave One Out strategy. .. GENERATED FROM PYTHON SOURCE LINES 189-197 .. code-block:: Python fittingAlgorithm = ot.CorrectedLeaveOneOut() # Finally the metamodel selection algorithm embbeded in LeastSquaresStrategy approximationAlgorithm = ot.LeastSquaresMetaModelSelectionFactory( basisSequenceFactory, fittingAlgorithm ) evaluationCoeffStrategy_2 = ot.LeastSquaresStrategy(approximationAlgorithm) experiment_2 = experiment .. GENERATED FROM PYTHON SOURCE LINES 198-199 Try integration. .. GENERATED FROM PYTHON SOURCE LINES 199-203 .. code-block:: Python marginalSizes = [2] * inputDimension evaluationCoeffStrategy_3 = ot.IntegrationStrategy() experiment_3 = ot.GaussProductExperiment(distribution, marginalSizes) .. GENERATED FROM PYTHON SOURCE LINES 204-206 Evaluate design of experiments. For the Gauss product we need to specify the non-uniform weights. .. GENERATED FROM PYTHON SOURCE LINES 206-209 .. code-block:: Python X, W = experiment.generateWithWeights() Y = model(X) .. GENERATED FROM PYTHON SOURCE LINES 210-219 STEP 4: Creation of the Functional Chaos Algorithm -------------------------------------------------- The `FunctionalChaosAlgorithm` class combines * the model : `model` * the distribution of the input random vector : `distribution` * the truncature strategy of the multivariate basis * and the evaluation strategy of the coefficients .. GENERATED FROM PYTHON SOURCE LINES 221-224 .. code-block:: Python polynomialChaosAlgorithm = ot.FunctionalChaosAlgorithm( X, W, Y, distribution, truncatureBasisStrategy, evaluationCoeffStrategy ) .. _sphx_glr_download_auto_meta_modeling_polynomial_chaos_metamodel_plot_functional_chaos_advanced_ctors.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_advanced_ctors.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_functional_chaos_advanced_ctors.py `