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
|
#! /usr/bin/env python
import openturns as ot
import math as m
# ot.Log.Show(ot.Log.ALL)
basisSize = 3
sampleSize = 5
X = ot.Sample(sampleSize, 1)
for i in range(sampleSize):
X[i, 0] = i + 1.0
phis = []
for j in range(basisSize):
phis.append(ot.SymbolicFunction(["x"], ["x^" + str(j + 1)]))
basis = ot.Basis(phis)
for i in range(basisSize):
print(ot.FunctionCollection(basis)[i](X))
proxy = ot.DesignProxy(X, phis)
full = range(basisSize)
design = ot.Matrix(proxy.computeDesign(full))
print("design=", design)
methods = [
ot.SVDMethod(proxy, full),
ot.CholeskyMethod(proxy, full),
ot.QRMethod(proxy, full),
ot.SparseMethod(ot.QRMethod(proxy, full)),
]
y = ot.Normal(
[1.0] * sampleSize, [0.1] * sampleSize, ot.CorrelationMatrix(sampleSize)
).getRealization()
yAt = design.transpose() * y
for method in methods:
name = method.__class__.__name__
print("-- method:", name)
x = method.solve(y)
print("solve:", x)
residual = m.sqrt((design * x - y).normSquare()) / sampleSize
print("residual: %.6g" % residual)
try:
x = method.solveNormal(yAt)
print("solveNormal:", x)
except RuntimeError:
print("-no solveNormal-")
d = method.getHDiag()
print("getHDiag:", d)
d = method.getGramInverse()
print("getGramInverse:", d)
d = method.getGramInverseDiag()
print("getGramInverseDiag:", d)
d = method.getGramInverseTrace()
print("getGramInverseTrace: %.5g" % d)
proxy = ot.DesignProxy(design)
for method in methods:
name = method.__class__.__name__
print("-- method:", name)
x = method.solve(y)
print("solve:", x)
residual = m.sqrt((design * x - y).normSquare()) / sampleSize
print("residual: %.6g" % residual)
try:
x = method.solveNormal(yAt)
print("solveNormal:", x)
except RuntimeError:
print("-no solveNormal-")
d = method.getHDiag()
print("getHDiag:", d)
d = method.getGramInverse()
print("getGramInverse:", d)
d = method.getGramInverseDiag()
print("getGramInverseDiag:", d)
d = method.getGramInverseTrace()
print("getGramInverseTrace: %.5g" % d)
|