Create a polynomial chaos metamodelΒΆ

In this basic example we are going to create a global approximation of a model response using functional chaos.

Here

h(x) = [\cos(x_1 + x_2), (x2 + 1) e^{x_1 - 2 x_2}]

[1]:
from __future__ import print_function
import openturns as ot
[2]:
# prepare some X/Y data
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)
[3]:
# create a functional chaos model
# we reduce the sampling size for the automatic selection of distribution based on Kolmogorov test
ot.ResourceMap.SetAsUnsignedInteger("FittingTest-KolmogorovSamplingSize", 4)
algo = ot.FunctionalChaosAlgorithm(x, y)
algo.run()
result = algo.getResult()
responseSurface = result.getMetaModel()
[4]:
# plot 2nd output of our model with x1=0.5
graph = ot.ParametricFunction(responseSurface, [0], [0.5]).getMarginal(1).draw(-0.5, 0.5)
curve = ot.ParametricFunction(model, [0], [0.5]).getMarginal(1).draw(-0.5, 0.5).getDrawable(0)
curve.setColor('red')
graph.add(curve)
graph
[4]:
../../_images/examples_meta_modeling_functional_chaos_5_0.png
[12]:
# draw Sobol' indices
sensitivityAnalysis = ot.FunctionalChaosSobolIndices(result)
first_order = [sensitivityAnalysis.getSobolIndex(i) for i in range(dimension)]
total_order = [sensitivityAnalysis.getSobolTotalIndex(i) for i in range(dimension)]

graph = ot.Graph('Sobol indices', 'inputs', 'Value', True, '')
data = ot.Sample(dimension, 2)
for i in range(dimension):
    data[i, 0] = i+1
    data[i, 1] = first_order[i]
cloud = ot.Cloud(data, "red", "circle", "First order")
graph.add(cloud)
for i in range(dimension):
    data[i, 0] = i+1+0.01
    data[i, 1] = total_order[i]
cloud = ot.Cloud(data, "blue", "square", "Total order")
graph.add(cloud)
for i in range(dimension):
    data[i, 0] = i+1+0.02
    data[i, 1] = first_order[i]
text = ot.Text(data, input_names, 'right')
text.setColor('black')
graph.add(text)
graph.setLegendPosition("center")
graph
[12]:
../../_images/examples_meta_modeling_functional_chaos_6_0.png