File: t_IsoProbabilisticTransformation_EllipticalDistribution.py

package info (click to toggle)
openturns 1.26-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 67,708 kB
  • sloc: cpp: 261,605; python: 67,030; ansic: 4,378; javascript: 406; sh: 185; xml: 164; makefile: 101
file content (127 lines) | stat: -rwxr-xr-x 3,835 bytes parent folder | download | duplicates (2)
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#! /usr/bin/env python

import openturns as ot

ot.TESTPREAMBLE()
ot.PlatformInfo.SetNumericalPrecision(5)


def cleanSymmetricTensor(inSymmetricTensor):
    rowDim = inSymmetricTensor.getNbRows()
    colDim = inSymmetricTensor.getNbColumns()
    sheetDim = inSymmetricTensor.getNbSheets()
    for i in range(rowDim):
        for j in range(colDim):
            for k in range(sheetDim):
                if abs(inSymmetricTensor[i, j, k]) < 1.0e-6:
                    inSymmetricTensor[i, j, k] = 0.0
    return inSymmetricTensor


# Instantiate one distribution object
dim = 3
meanPoint = ot.Point(dim, 1.0)
meanPoint[0] = 0.5
meanPoint[1] = -0.5
sigma = ot.Point(dim, 1.0)
sigma[0] = 2.0
sigma[1] = 3.0
R = ot.CorrelationMatrix(dim)
for i in range(1, dim):
    R[i, i - 1] = 0.5
distribution = ot.Normal(meanPoint, sigma, R)

# Test for sampling
size = 10000
sample = distribution.getSample(size)
print("sample first=", repr(sample[0]), " last=", repr(sample[size - 1]))
print("sample mean=", repr(sample.computeMean()))
print("sample covariance=", repr(sample.computeCovariance()))

transform = distribution.getIsoProbabilisticTransformation()
print("isoprobabilistic transformation=", repr(transform))
transformedSample = transform(sample)
print(
    "transformed sample first=",
    repr(transformedSample[0]),
    " last=",
    repr(transformedSample[size - 1]),
)
print("transformed sample mean=", repr(transformedSample.computeMean()))
print("transformed sample covariance=", repr(transformedSample.computeCovariance()))

# Test for evaluation
inverseTransform = distribution.getInverseIsoProbabilisticTransformation()
print("inverse isoprobabilistic transformation=", repr(inverseTransform))
transformedBackSample = inverseTransform(transformedSample)
print(
    "transformed back sample first=",
    repr(transformedBackSample[0]),
    " last=",
    repr(transformedBackSample[size - 1]),
)
print("transformed back sample mean=", repr(transformedBackSample.computeMean()))
print(
    "transformed back sample covariance=",
    repr(transformedBackSample.computeCovariance()),
)
point = ot.Point(dim, 1.0)
print("point=", repr(point))
transformedPoint = transform(point)
print("transform value at point        =", repr(transformedPoint))
print("transform gradient at point     =", repr(transform.gradient(point)))
print(
    "transform gradient at point (FD)=",
    repr(
        ot.CenteredFiniteDifferenceGradient(1.0e-5, transform.getEvaluation()).gradient(
            point
        )
    ),
)
print(
    "transform hessian at point      =",
    repr(cleanSymmetricTensor(transform.hessian(point))),
)
print(
    "transform hessian at point (FD) =",
    repr(
        cleanSymmetricTensor(
            ot.CenteredFiniteDifferenceHessian(
                1.0e-4, transform.getEvaluation()
            ).hessian(point)
        )
    ),
)
print(
    "inverse transform value at transformed point        =",
    repr(inverseTransform(transformedPoint)),
)
print(
    "inverse transform gradient at transformed point (FD)=",
    repr(inverseTransform.gradient(transformedPoint)),
)
print(
    "inverse transform gradient at transformed point     =",
    repr(
        ot.CenteredFiniteDifferenceGradient(
            1.0e-5, inverseTransform.getEvaluation()
        ).gradient(transformedPoint)
    ),
)
print(
    "inverse transform hessian at transformed point      =",
    repr(cleanSymmetricTensor(inverseTransform.hessian(transformedPoint))),
)
print(
    "inverse transform hessian at transformed point (FD) =",
    repr(
        cleanSymmetricTensor(
            ot.CenteredFiniteDifferenceHessian(
                1.0e-4, inverseTransform.getEvaluation()
            ).hessian(transformedPoint)
        )
    ),
)

# Test for parameters
print("parameters gradient at point=", repr(transform.parameterGradient(point)))