Note

Go to the end to download the full example code.

# Polynomial chaos exploitation¶

The goal of this example is to create a polynomial chaos expansion using the
`FunctionalChaosAlgorithm`

class and see the different methods
of the `FunctionalChaosResult`

class.
In order to understand the different results, let us review some notations associated to the
polynomial chaos expansion.

We first introduce the physical input, output and model:

the random vector is the physical input random vector,

the output random vector is the output of the physical model,

the physical model is a function of the physical input:

Then we introduce the iso-probabilistic transformation:

the random vector is the standardized input random vector,

the transformation is the iso-probabilistic transformation mapping from the physical input to the standardized input:

the standard distribution of the standardized random vector .

We expand the model on the multivariate basis:

the full (i.e. unselected) multivariate basis is ,

the composition of each polynomial of the truncated multivariate basis ,

the full set of coefficients of the polynomial expansion is the set ,

the composed model is a function of the standardized random vector :

Then we use a model selection method (e.g. from the `LARS`

class):

the set is the set of indices corresponding to the result of the selection method,

the truncated (i.e. selected) multivariate basis is ,

the selected set of coefficients of the polynomial expansion is the set ,

the composed meta model is the function of the standardized variables based on the truncated multivariate basis .

the meta model is a function of the physical input:

Based on the previous definitions, the composed model is:

the composed metamodel is:

and the metamodel is:

The three previous mathematical functions are implemented as instances of the
`Function`

class.

## Create the polynomial chaos expansion¶

```
import openturns as ot
ot.Log.Show(ot.Log.NONE)
```

Prepare some input and output samples. We define a model which has two inputs and two outputs. Then we define a normal input random vector with independent marginals, and we generate a sample from the input random vector. Finally, we evaluate the output sample from the model.

```
ot.RandomGenerator.SetSeed(0)
dimension = 2
input_names = ["x1", "x2"]
formulas = ["cos(x1 + x2)", "(x2 + 1) * exp(x1 - 2 * x2)"]
model = ot.SymbolicFunction(input_names, formulas)
distribution = ot.Normal(dimension)
x = distribution.getSample(30)
y = model(x)
```

Create a functional chaos algorithm.

```
algo = ot.FunctionalChaosAlgorithm(x, y)
```

The previous constructor is the simplest since it has only two inputs arguments. In this case, the algorithm has to retrieve the distribution from the x sample, which can be difficult, especially if the sample size is small. Notice, however, that the input distribution is known in our simple case. This is why we update the previous script and give the input distribution as a third input argument of the constructor.

```
algo = ot.FunctionalChaosAlgorithm(x, y, distribution)
algo.run()
```

Get the result.

```
result = algo.getResult()
result
```

## Get the coefficients¶

In the next cells, we observe several methods of the result object. First, we get the polynomial chaos coefficients.

```
result.getCoefficients()
```

The coefficients of the i-th output marginal.

```
i = 1
result.getCoefficients()[i]
```

Get the indices of the selected polynomials.

```
subsetK = result.getIndices()
subsetK
```

Get the composition of the polynomials of the truncated multivariate basis.

```
for i in range(subsetK.getSize()):
print(
"Polynomial number ",
i,
" in truncated basis <-> polynomial number ",
subsetK[i],
" = ",
ot.LinearEnumerateFunction(dimension)(subsetK[i]),
" in complete basis",
)
```

```
Polynomial number 0 in truncated basis <-> polynomial number 0 = [0,0] in complete basis
Polynomial number 1 in truncated basis <-> polynomial number 1 = [1,0] in complete basis
Polynomial number 2 in truncated basis <-> polynomial number 2 = [0,1] in complete basis
Polynomial number 3 in truncated basis <-> polynomial number 3 = [2,0] in complete basis
Polynomial number 4 in truncated basis <-> polynomial number 4 = [1,1] in complete basis
Polynomial number 5 in truncated basis <-> polynomial number 5 = [0,2] in complete basis
Polynomial number 6 in truncated basis <-> polynomial number 6 = [3,0] in complete basis
Polynomial number 7 in truncated basis <-> polynomial number 7 = [2,1] in complete basis
Polynomial number 8 in truncated basis <-> polynomial number 8 = [1,2] in complete basis
Polynomial number 9 in truncated basis <-> polynomial number 9 = [0,3] in complete basis
Polynomial number 10 in truncated basis <-> polynomial number 10 = [4,0] in complete basis
Polynomial number 11 in truncated basis <-> polynomial number 11 = [3,1] in complete basis
Polynomial number 12 in truncated basis <-> polynomial number 12 = [2,2] in complete basis
Polynomial number 13 in truncated basis <-> polynomial number 13 = [1,3] in complete basis
Polynomial number 14 in truncated basis <-> polynomial number 14 = [0,4] in complete basis
Polynomial number 15 in truncated basis <-> polynomial number 15 = [5,0] in complete basis
Polynomial number 16 in truncated basis <-> polynomial number 16 = [4,1] in complete basis
Polynomial number 17 in truncated basis <-> polynomial number 17 = [3,2] in complete basis
Polynomial number 18 in truncated basis <-> polynomial number 18 = [2,3] in complete basis
Polynomial number 19 in truncated basis <-> polynomial number 19 = [1,4] in complete basis
Polynomial number 20 in truncated basis <-> polynomial number 20 = [0,5] in complete basis
Polynomial number 21 in truncated basis <-> polynomial number 21 = [6,0] in complete basis
Polynomial number 22 in truncated basis <-> polynomial number 22 = [5,1] in complete basis
Polynomial number 23 in truncated basis <-> polynomial number 23 = [4,2] in complete basis
Polynomial number 24 in truncated basis <-> polynomial number 24 = [3,3] in complete basis
Polynomial number 25 in truncated basis <-> polynomial number 25 = [2,4] in complete basis
Polynomial number 26 in truncated basis <-> polynomial number 26 = [1,5] in complete basis
Polynomial number 27 in truncated basis <-> polynomial number 27 = [0,6] in complete basis
Polynomial number 28 in truncated basis <-> polynomial number 28 = [7,0] in complete basis
Polynomial number 29 in truncated basis <-> polynomial number 29 = [6,1] in complete basis
```

## Get the basis¶

Get the multivariate basis
as a collection of `Function`

objects.

```
reduced = result.getReducedBasis()
```

Get the orthogonal basis.

```
orthgBasis = result.getOrthogonalBasis()
```

Get the standard distribution of the standardized random vector .

```
orthgBasis.getMeasure()
```

## Get the metamodel¶

Get the composed meta model which is the model of the standardized random vector within the reduced polynomials basis.

```
result.getComposedMetaModel()
```

Get the meta model which is the composed meta model composed with the iso probabilistic transformation .

```
result.getMetaModel()
```

## Get the projection method¶

Get the projection strategy: this is the method to estimate the coefficients, i.e. regression or integration.

```
algo.getProjectionStrategy()
```