Fit a distribution from an input sampleΒΆ

In this example we show how to use the BuildDistribution function to fit a distribution to an input sample. This function is used by the 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.

[1]:
from __future__ import print_function
import openturns as ot

We first create the function model.

[2]:
ot.RandomGenerator.SetSeed(0)
dimension = 2
input_names = ['x1', 'x2']
formulas = ['cos(x1 + x2)', '(x2 + 1) * exp(x1)']
model = ot.SymbolicFunction(input_names, formulas)

Then we create a sample x and compute the corresponding output sample y.

[3]:
distribution = ot.Normal(dimension)
samplesize = 1000
inputSample  = distribution.getSample(samplesize)
outputSample = model(inputSample)

Create a functional chaos model. We reduce the sampling size for the automatic selection of distribution based on Kolmogorov test.

[4]:
ot.ResourceMap.SetAsUnsignedInteger("FittingTest-KolmogorovSamplingSize", 100)
[5]:
distribution = ot.FunctionalChaosAlgorithm.BuildDistribution(inputSample)
[6]:
for i in range(dimension):
    marginal = distribution.getMarginal(i)
    marginalname = marginal.getImplementation().getClassName()
    print("Marginal #",i,":",marginalname)
distribution.getCopula()
Marginal # 0 : Histogram
Marginal # 1 : Histogram
[6]:

IndependentCopula(dimension = 2)

The previous call to BuildDistribution is what is done internally by the following constructor of FunctionalChaosAlgorithm.

[7]:
algo = ot.FunctionalChaosAlgorithm(inputSample, outputSample)