File: t_QuadraticLeastSquares_std.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 (45 lines) | stat: -rwxr-xr-x 1,348 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
#! /usr/bin/env python

import openturns as ot
import openturns.testing as ott

# 1-d
X = [[1.0], [2.0], [3.0], [4.0]]
Y = [[5.0], [3.0], [4.0], [7.0]]
algo = ot.QuadraticLeastSquares(X, Y)
algo.run()
mm = algo.getMetaModel()
x = [0.0]
y2 = mm(x)
print(f"mm(x)={y2}")
print(f"constant={algo.getConstant()}")
print(f"linear={algo.getLinear()}")
print(f"quadratic={algo.getQuadratic()}")
ott.assert_almost_equal(mm.gradient([0.0])[0, 0], -5.55)
ott.assert_almost_equal(mm.hessian([0.0])[0, 0, 0], 2.5)

# 2-d
f = ot.SymbolicFunction(
    ["x1", "x2"], ["9*x1^2 + 6*x2^2 + 0.1*x1 * x2 + 4*x1 + 7*x2 + 5.0"]
)
X = ot.Normal([2.0, 3.0], [1.0, 1.0]).getSample(1000)
Y = f(X)
algo = ot.QuadraticLeastSquares(X, Y)
algo.run()
mm = algo.getMetaModel()
x = [2.0, 3.0]
y = f(x)
y2 = mm(x)
print(f"f(x)={y}")
print(f"mm(x)={y2}")
print(f"constant={algo.getConstant()}")
print(f"linear={algo.getLinear()}")
print(f"quadratic={algo.getQuadratic()}")
ott.assert_almost_equal(y, y2, 1e-2, 1e-2)
linear = algo.getLinear()
ott.assert_almost_equal(linear[0, 0], 4.0, 1e-2, 1e-2)
ott.assert_almost_equal(linear[1, 0], 7.0, 1e-2, 1e-2)
quadratic = algo.getQuadratic()
ott.assert_almost_equal(0.5 * quadratic[0, 0, 0], 9.0, 1e-2, 1e-2)
ott.assert_almost_equal(0.5 * quadratic[0, 1, 0], 0.1, 1e-2, 1e-2)
ott.assert_almost_equal(0.5 * quadratic[1, 1, 0], 6.0, 1e-2, 1e-2)