-
Notifications
You must be signed in to change notification settings - Fork 0
/
s.py
72 lines (58 loc) · 2.1 KB
/
s.py
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
# lsqfitgp/examples/s.py
#
# Copyright (c) 2020, 2022, Giacomo Petrillo
#
# This file is part of lsqfitgp.
#
# lsqfitgp is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# lsqfitgp is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with lsqfitgp. If not, see <http://www.gnu.org/licenses/>.
"""
EXAMPLE S.
Where different coordinates unite together under a single
field name.
"""
import lsqfitgp as lgp
from lsqfitgp import _linalg
from matplotlib import pyplot as plt
from mpl_toolkits import mplot3d
import numpy as np
import gvar
xdata1d = np.linspace(-4, 4, 10)
xpred1d = np.linspace(-10, 10, 50)
def makexy(x1d, y1d):
xy = np.empty((len(x1d), len(y1d)), dtype=[
('xy', float, 2)
])
x, y = np.meshgrid(x1d, y1d)
xy['xy'][..., 0] = x
xy['xy'][..., 1] = y
return xy
xdata = makexy(xdata1d, xdata1d)
xpred = makexy(xpred1d, xpred1d)
y = np.cos(xdata['xy'][..., 0]) * np.cos(xdata['xy'][..., 1])
gp = (lgp
.GP(lgp.ExpQuad(scale=3))
.addx(xdata.reshape(-1), 'pere')
.addx(xpred.reshape(-1), 'banane')
)
print('fit...')
m, cov = gp.predfromdata({'pere': y.reshape(-1)}, 'banane', raw=True)
print('samples...')
sample = m + gp.decompose(cov, solver='chol', epsrel=5e-5).correlate(np.random.randn(len(m)))
sample = sample.reshape(xpred.shape)
print('plot...')
fig, ax = plt.subplots(num='s', clear=True, subplot_kw=dict(projection='3d', computed_zorder=False))
ax.scatter(xdata['xy'][..., 0].reshape(-1), xdata['xy'][..., 1].reshape(-1), y.reshape(-1), color='black', zorder=10)
ax.plot_surface(xpred['xy'][..., 0], xpred['xy'][..., 1], sample, alpha=0.9, cmap='viridis')
ax.view_init(elev=70, azim=110)
fig.show()