File: t_FunctionalChaos_mixed.py

package info (click to toggle)
openturns 1.24-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 66,204 kB
  • sloc: cpp: 256,662; python: 63,381; ansic: 4,414; javascript: 406; sh: 180; xml: 164; yacc: 123; makefile: 98; lex: 55
file content (52 lines) | stat: -rwxr-xr-x 1,585 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#! /usr/bin/env python

import openturns as ot

ot.TESTPREAMBLE()

# distributions
continuous_1D_dist = ot.Normal()
d = 3
finite_1D_dist = ot.Binomial(d - 1, 0.5)
marginals = [continuous_1D_dist, finite_1D_dist]
mixed_2D_dist = ot.JointDistribution(marginals)

# data
N = 1000
g = ot.SymbolicFunction(["X1", "X2"], ["sin(X1) + X2"])
x = mixed_2D_dist.getSample(N)
y = g(x)
y.setDescription(["G0"])

# polynomial chaos
q, totalDegree = 0.4, 5
P = x.getDimension()
enumerateFunction = ot.HyperbolicAnisotropicEnumerateFunction(P, q)
productBasis = ot.OrthogonalProductPolynomialFactory(marginals, enumerateFunction)
fittingAlgorithm = ot.CorrectedLeaveOneOut()
approximationAlgorithm = ot.LeastSquaresMetaModelSelectionFactory(
    ot.LARS(), fittingAlgorithm
)
adaptiveStrategy = ot.FixedStrategy(
    productBasis, enumerateFunction.getStrataCumulatedCardinal(totalDegree)
)
projectionStrategy = ot.LeastSquaresStrategy(approximationAlgorithm)
algo = ot.FunctionalChaosAlgorithm(
    x, y, mixed_2D_dist, adaptiveStrategy, projectionStrategy
)
algo.run()
result = algo.getResult()
print(result.getRelativeErrors())
assert result.getRelativeErrors()[0] < 1e-10, "relative error too high"
assert (
    algo.getResult().getMetaModel().getOutputDescription() == y.getDescription()
), "wrong output description"

# selection history
indices = result.getIndicesHistory()
coefs = result.getCoefficientsHistory()
assert indices.getSize() > 0, "no indices selection"
assert indices.getSize() == coefs.getSize(), "not same size"
print(indices)
print(coefs)
graph = result.drawSelectionHistory()