gpmp 0.9.15__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- examples/__init__.py +4 -0
- examples/gpmp_example01_materncov.py +27 -0
- examples/gpmp_example02_1d_interpolation.py +99 -0
- examples/gpmp_example02_1d_interpolation_peaks.py +145 -0
- examples/gpmp_example03_2d.py +143 -0
- examples/gpmp_example04_nd.py +114 -0
- examples/gpmp_example05_1d_custom_kernel.py +133 -0
- examples/gpmp_example06_1d_regression.py +120 -0
- examples/gpmp_example10_sample_paths.py +71 -0
- examples/gpmp_example11_sample_paths_noisy_obs.py +167 -0
- examples/gpmp_example20_1d_interpolation_variation.py +112 -0
- examples/gpmp_example21_1d_interpolation_variation.py +125 -0
- gpmp/__init__.py +4 -0
- gpmp/core.py +974 -0
- gpmp/kernel.py +743 -0
- gpmp/misc/__init__.py +6 -0
- gpmp/misc/dataframe.py +114 -0
- gpmp/misc/designs.py +300 -0
- gpmp/misc/modeldiagnosis.py +399 -0
- gpmp/misc/plotutils.py +298 -0
- gpmp/misc/scoringrules.py +145 -0
- gpmp/misc/testfunctions.py +365 -0
- gpmp/num.py +809 -0
- gpmp-0.9.15.dist-info/AUTHORS.md +9 -0
- gpmp-0.9.15.dist-info/LICENSE.txt +674 -0
- gpmp-0.9.15.dist-info/METADATA +823 -0
- gpmp-0.9.15.dist-info/RECORD +33 -0
- gpmp-0.9.15.dist-info/WHEEL +5 -0
- gpmp-0.9.15.dist-info/top_level.txt +3 -0
- tests/__init__.py +2 -0
- tests/test_examples.py +49 -0
- tests/test_jax.py +72 -0
- tests/test_scoringrules.py +45 -0
examples/__init__.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
""" Plot the Matern nu = p + 1/2 kernel/covariance functions
|
|
2
|
+
|
|
3
|
+
Author: Emmanuel Vazquez <emmanuel.vazquez@centralesupelec.fr>
|
|
4
|
+
Copyright (c) 2022, CentraleSupelec
|
|
5
|
+
License: GPLv3 (see LICENSE)
|
|
6
|
+
"""
|
|
7
|
+
import gpmp.num as gnp
|
|
8
|
+
import gpmp as gp
|
|
9
|
+
|
|
10
|
+
def main():
|
|
11
|
+
h = gnp.linspace(-2.0, 2.0, 500)
|
|
12
|
+
|
|
13
|
+
fig = gp.misc.plotutils.Figure()
|
|
14
|
+
|
|
15
|
+
for p in [0, 1, 4]:
|
|
16
|
+
r = gp.kernel.maternp_kernel(p, gnp.abs(h))
|
|
17
|
+
fig.plot(h, r, label='p={} / nu={}/2'.format(p, 2*p+1))
|
|
18
|
+
|
|
19
|
+
fig.title('Matern covariances')
|
|
20
|
+
fig.xlabel('h')
|
|
21
|
+
fig.ylabel('$k_{p+1/2}(h)$')
|
|
22
|
+
fig.legend()
|
|
23
|
+
fig.show(grid=True)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
if __name__ == '__main__':
|
|
27
|
+
main()
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Plot and optimize the restricted negative log-likelihood
|
|
3
|
+
|
|
4
|
+
Author: Emmanuel Vazquez <emmanuel.vazquez@centralesupelec.fr>
|
|
5
|
+
Copyright (c) 2022-2023, CentraleSupelec
|
|
6
|
+
License: GPLv3 (see LICENSE)
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import gpmp.num as gnp
|
|
10
|
+
import gpmp as gp
|
|
11
|
+
import matplotlib.pyplot as plt
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def generate_data():
|
|
15
|
+
"""
|
|
16
|
+
Data generation.
|
|
17
|
+
|
|
18
|
+
Returns
|
|
19
|
+
-------
|
|
20
|
+
tuple
|
|
21
|
+
(xt, zt): target data
|
|
22
|
+
(xi, zi): input dataset
|
|
23
|
+
"""
|
|
24
|
+
dim = 1
|
|
25
|
+
nt = 200
|
|
26
|
+
box = [[-1], [1]]
|
|
27
|
+
xt = gp.misc.designs.regulargrid(dim, nt, box)
|
|
28
|
+
zt = gp.misc.testfunctions.twobumps(xt)
|
|
29
|
+
|
|
30
|
+
ni = 6
|
|
31
|
+
xi = gp.misc.designs.ldrandunif(dim, ni, box)
|
|
32
|
+
zi = gp.misc.testfunctions.twobumps(xi)
|
|
33
|
+
|
|
34
|
+
return xt, zt, xi, zi
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def constant_mean(x, param):
|
|
38
|
+
return gnp.ones((x.shape[0], 1))
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def kernel(x, y, covparam, pairwise=False):
|
|
42
|
+
p = 3
|
|
43
|
+
return gp.kernel.maternp_covariance(x, y, p, covparam, pairwise)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def visualize_results(xt, zt, xi, zi, zpm, zpv):
|
|
47
|
+
"""
|
|
48
|
+
Visualize the results using gp.misc.plotutils (a matplotlib wrapper).
|
|
49
|
+
|
|
50
|
+
Parameters
|
|
51
|
+
----------
|
|
52
|
+
xt : numpy.ndarray
|
|
53
|
+
Target x values
|
|
54
|
+
zt : numpy.ndarray
|
|
55
|
+
Target z values
|
|
56
|
+
xi : numpy.ndarray
|
|
57
|
+
Input x values
|
|
58
|
+
zi : numpy.ndarray
|
|
59
|
+
Input z values
|
|
60
|
+
zpm : numpy.ndarray
|
|
61
|
+
Posterior mean
|
|
62
|
+
zpv : numpy.ndarray
|
|
63
|
+
Posterior variance
|
|
64
|
+
"""
|
|
65
|
+
fig = gp.misc.plotutils.Figure(isinteractive=True)
|
|
66
|
+
fig.plot(xt, zt, 'k', linewidth=1, linestyle=(0, (5, 5)))
|
|
67
|
+
fig.plotdata(xi, zi)
|
|
68
|
+
fig.plotgp(xt, zpm, zpv, colorscheme='simple')
|
|
69
|
+
fig.xylabels('$x$', '$z$')
|
|
70
|
+
fig.title('Posterior GP with parameters selected by ReML')
|
|
71
|
+
fig.show(grid=True, xlim=[-1.0, 1.0], legend=True, legend_fontsize=9)
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def main():
|
|
75
|
+
xt, zt, xi, zi = generate_data()
|
|
76
|
+
|
|
77
|
+
meanparam = None
|
|
78
|
+
covparam0 = None
|
|
79
|
+
model = gp.core.Model(constant_mean, kernel, meanparam, covparam0)
|
|
80
|
+
|
|
81
|
+
# Automatic selection of parameters using REML
|
|
82
|
+
model, info = gp.kernel.select_parameters_with_reml(model, xi, zi, info=True)
|
|
83
|
+
gp.misc.modeldiagnosis.diag(model, info, xi, zi)
|
|
84
|
+
|
|
85
|
+
# Prediction
|
|
86
|
+
zpm, zpv = model.predict(xi, zi, xt)
|
|
87
|
+
|
|
88
|
+
# Visualization
|
|
89
|
+
print('\nVisualization')
|
|
90
|
+
print('-------------')
|
|
91
|
+
plot_likelihood = True
|
|
92
|
+
if plot_likelihood:
|
|
93
|
+
gp.misc.modeldiagnosis.plot_likelihood_sigma_rho(model, info)
|
|
94
|
+
|
|
95
|
+
visualize_results(xt, zt, xi, zi, zpm, zpv)
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
if __name__ == '__main__':
|
|
99
|
+
main()
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Plot and optimize the restricted negative log-likelihood
|
|
3
|
+
|
|
4
|
+
Author: Emmanuel Vazquez <emmanuel.vazquez@centralesupelec.fr>
|
|
5
|
+
Copyright (c) 2022-2023, CentraleSupelec
|
|
6
|
+
License: GPLv3 (see LICENSE)
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import gpmp.num as gnp
|
|
10
|
+
import gpmp as gp
|
|
11
|
+
import matplotlib.pyplot as plt
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def f_matern(x):
|
|
15
|
+
l = 0.1 # length scale
|
|
16
|
+
x1, x2, x3, x4 = -0.5, -0.2, 0.3, 0.5 # translation points
|
|
17
|
+
x = gnp.asarray(x)
|
|
18
|
+
return gnp.exp(-1.0 + 0.2 * x**2 -
|
|
19
|
+
0.1 * gp.kernel.matern32_kernel(gnp.abs(x - x1) / 0.01)
|
|
20
|
+
- 0.2 * gp.kernel.matern32_kernel(gnp.abs(x - x2) / 0.02)
|
|
21
|
+
- 0.1 * gp.kernel.matern32_kernel(gnp.abs(x - x3) / 0.04)
|
|
22
|
+
- 0.2 * gp.kernel.matern32_kernel(gnp.abs(x - x4) / 0.04)
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def generate_data():
|
|
27
|
+
"""
|
|
28
|
+
Data generation.
|
|
29
|
+
|
|
30
|
+
Returns
|
|
31
|
+
-------
|
|
32
|
+
tuple
|
|
33
|
+
(xt, zt): target data
|
|
34
|
+
(xi, zi): input dataset
|
|
35
|
+
"""
|
|
36
|
+
dim = 1
|
|
37
|
+
nt = 1000
|
|
38
|
+
box = [[-1], [1]]
|
|
39
|
+
xt = gp.misc.designs.regulargrid(dim, nt, box)
|
|
40
|
+
zt = f_matern(xt)
|
|
41
|
+
|
|
42
|
+
ni = 40
|
|
43
|
+
xi = gp.misc.designs.ldrandunif(dim, ni, box)
|
|
44
|
+
zi = f_matern(xi)
|
|
45
|
+
|
|
46
|
+
return xt, zt, xi, zi
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def constant_mean(x, param):
|
|
50
|
+
return gnp.ones((x.shape[0], 1))
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def kernel(x, y, covparam, pairwise=False):
|
|
54
|
+
p = 1
|
|
55
|
+
return gp.kernel.maternp_covariance(x, y, p, covparam, pairwise)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def visualize_results(xt, zt, xi, zi, zpm, zpv):
|
|
59
|
+
"""
|
|
60
|
+
Visualize the results using gp.misc.plotutils (a matplotlib wrapper).
|
|
61
|
+
|
|
62
|
+
Parameters
|
|
63
|
+
----------
|
|
64
|
+
xt : numpy.ndarray
|
|
65
|
+
Target x values
|
|
66
|
+
zt : numpy.ndarray
|
|
67
|
+
Target z values
|
|
68
|
+
xi : numpy.ndarray
|
|
69
|
+
Input x values
|
|
70
|
+
zi : numpy.ndarray
|
|
71
|
+
Input z values
|
|
72
|
+
zpm : numpy.ndarray
|
|
73
|
+
Posterior mean
|
|
74
|
+
zpv : numpy.ndarray
|
|
75
|
+
Posterior variance
|
|
76
|
+
"""
|
|
77
|
+
fig = gp.misc.plotutils.Figure(isinteractive=True)
|
|
78
|
+
fig.plot(xt, zt, "k", linewidth=1, linestyle=(0, (5, 5)))
|
|
79
|
+
fig.plotdata(xi, zi)
|
|
80
|
+
fig.plotgp(xt, zpm, zpv, colorscheme="simple")
|
|
81
|
+
fig.xylabels("$x$", "$z$")
|
|
82
|
+
fig.title("Posterior GP with parameters selected by ReML")
|
|
83
|
+
fig.show(grid=True, xlim=[-1.0, 1.0], legend=True, legend_fontsize=9)
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def main():
|
|
87
|
+
xt, zt, xi, zi = generate_data()
|
|
88
|
+
|
|
89
|
+
meanparam = None
|
|
90
|
+
covparam0 = None
|
|
91
|
+
model = gp.core.Model(constant_mean, kernel, meanparam, covparam0)
|
|
92
|
+
|
|
93
|
+
# Automatic selection of parameters using REML
|
|
94
|
+
model, info = gp.kernel.select_parameters_with_reml(model, xi, zi, info=True)
|
|
95
|
+
gp.misc.modeldiagnosis.diag(model, info, xi, zi)
|
|
96
|
+
|
|
97
|
+
# Prediction
|
|
98
|
+
zpm, zpv = model.predict(xi, zi, xt)
|
|
99
|
+
|
|
100
|
+
# Visualization
|
|
101
|
+
print("\nVisualization")
|
|
102
|
+
print("-------------")
|
|
103
|
+
plot_likelihood = False
|
|
104
|
+
if plot_likelihood:
|
|
105
|
+
gp.misc.modeldiagnosis.plot_likelihood_sigma_rho(model, info)
|
|
106
|
+
|
|
107
|
+
visualize_results(xt, zt, xi, zi, zpm, zpv)
|
|
108
|
+
|
|
109
|
+
zloo, sigma2loo, eloo = model.loo(xi, zi)
|
|
110
|
+
|
|
111
|
+
plt.figure()
|
|
112
|
+
plt.plot(zi, eloo / sigma2loo, 'o')
|
|
113
|
+
plt.show()
|
|
114
|
+
|
|
115
|
+
r = gnp.to_np(eloo / sigma2loo)
|
|
116
|
+
|
|
117
|
+
from scipy.stats import gennorm
|
|
118
|
+
import numpy as np
|
|
119
|
+
|
|
120
|
+
shape, loc, scale = gennorm.fit(r)
|
|
121
|
+
print(f"Shape (beta): {shape}")
|
|
122
|
+
print(f"Location (mu): {loc}")
|
|
123
|
+
print(f"Scale (alpha): {scale}")
|
|
124
|
+
|
|
125
|
+
# Plot the histogram of the data
|
|
126
|
+
plt.figure()
|
|
127
|
+
plt.hist(r, bins=30, density=True, alpha=0.6, color='g')
|
|
128
|
+
|
|
129
|
+
# Create a range of values for the PDF
|
|
130
|
+
xmin, xmax = plt.xlim()
|
|
131
|
+
x_range = np.linspace(xmin, xmax, 100)
|
|
132
|
+
|
|
133
|
+
# Plot the fitted PDF
|
|
134
|
+
pdf_fitted = gennorm.pdf(x_range, shape, loc, scale)
|
|
135
|
+
plt.plot(x_range, pdf_fitted, 'k', linewidth=2)
|
|
136
|
+
|
|
137
|
+
# Display the plot
|
|
138
|
+
plt.title('Fit of Generalized Gaussian Distribution')
|
|
139
|
+
plt.xlabel('Data')
|
|
140
|
+
plt.ylabel('Density')
|
|
141
|
+
plt.show()
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
if __name__ == "__main__":
|
|
145
|
+
main()
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Gaussian processes in 2D
|
|
3
|
+
|
|
4
|
+
An anisotropic Matern covariance function is used for the Gaussian
|
|
5
|
+
Process (GP) prior. The parameters of this covariance function
|
|
6
|
+
(variance and ranges) are estimated using the Restricted Maximum
|
|
7
|
+
Likelihood (ReML) method.
|
|
8
|
+
|
|
9
|
+
The mean function of the GP prior is assumed to be constant and
|
|
10
|
+
unknown.
|
|
11
|
+
|
|
12
|
+
The function is sampled on a space-filling Latin Hypercube design, and
|
|
13
|
+
the data is assumed to be noiseless.
|
|
14
|
+
|
|
15
|
+
----
|
|
16
|
+
Author: Emmanuel Vazquez <emmanuel.vazquez@centralesupelec.fr>
|
|
17
|
+
Copyright (c) 2022-2023, CentraleSupelec
|
|
18
|
+
License: GPLv3 (see LICENSE)
|
|
19
|
+
----
|
|
20
|
+
This example is based on the file stk_example_kb03.m from the STK at
|
|
21
|
+
https://github.com/stk-kriging/stk/
|
|
22
|
+
by Julien Bect and Emmanuel Vazquez, released under the GPLv3 license.
|
|
23
|
+
|
|
24
|
+
Original copyright notice:
|
|
25
|
+
|
|
26
|
+
Copyright (c) 2015, 2016, 2018 CentraleSupelec
|
|
27
|
+
Copyright (c) 2011-2014 SUPELEC
|
|
28
|
+
----
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
import numpy as np
|
|
32
|
+
import gpmp.num as gnp
|
|
33
|
+
import gpmp as gp
|
|
34
|
+
import matplotlib.pyplot as plt
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
# Test function selection
|
|
38
|
+
def select_test_function(case_num):
|
|
39
|
+
if case_num == 1:
|
|
40
|
+
f = gp.misc.testfunctions.braninhoo
|
|
41
|
+
dim = 2
|
|
42
|
+
box = [[-5, 0], [10, 15]]
|
|
43
|
+
ni = 20
|
|
44
|
+
elif case_num == 2:
|
|
45
|
+
f = gp.misc.testfunctions.wave
|
|
46
|
+
dim = 2
|
|
47
|
+
box = [[-1, -1], [1, 1]]
|
|
48
|
+
ni = 40
|
|
49
|
+
return f, dim, box, ni
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def create_model():
|
|
53
|
+
def constant_mean(x, param):
|
|
54
|
+
return gnp.ones((x.shape[0], 1))
|
|
55
|
+
|
|
56
|
+
def kernel(x, y, covparam, pairwise=False):
|
|
57
|
+
p = 6
|
|
58
|
+
return gp.kernel.maternp_covariance(x, y, p, covparam, pairwise)
|
|
59
|
+
|
|
60
|
+
meanparam = None
|
|
61
|
+
covparam = None
|
|
62
|
+
|
|
63
|
+
return gp.core.Model(constant_mean, kernel, meanparam, covparam)
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def main():
|
|
67
|
+
case_num = 2
|
|
68
|
+
f, dim, box, ni = select_test_function(case_num)
|
|
69
|
+
|
|
70
|
+
# Compute the function on a 80 x 80 regular grid
|
|
71
|
+
nt = [80, 80]
|
|
72
|
+
xt = gp.misc.designs.regulargrid(dim, nt, box)
|
|
73
|
+
zt = f(xt)
|
|
74
|
+
|
|
75
|
+
design_type = 'ld'
|
|
76
|
+
if design_type == 'lhs':
|
|
77
|
+
xi = gp.misc.designs.maximinlhs(dim, ni, box)
|
|
78
|
+
elif design_type == 'ld':
|
|
79
|
+
xi = gp.misc.designs.ldrandunif(dim, ni, box)
|
|
80
|
+
zi = f(xi)
|
|
81
|
+
|
|
82
|
+
model = create_model()
|
|
83
|
+
|
|
84
|
+
# Parameter selection
|
|
85
|
+
covparam0 = gp.kernel.anisotropic_parameters_initial_guess(model, xi, zi)
|
|
86
|
+
nlrl, dnlrl = gp.kernel.make_selection_criterion_with_gradient(
|
|
87
|
+
model.negative_log_restricted_likelihood,
|
|
88
|
+
xi,
|
|
89
|
+
zi)
|
|
90
|
+
covparam_reml, info = gp.kernel.autoselect_parameters(covparam0, nlrl, dnlrl, info=True)
|
|
91
|
+
model.covparam = gnp.asarray(covparam_reml)
|
|
92
|
+
gp.misc.modeldiagnosis.diag(model, info, xi, zi)
|
|
93
|
+
|
|
94
|
+
# Prediction
|
|
95
|
+
(zpm, zpv) = model.predict(xi, zi, xt)
|
|
96
|
+
|
|
97
|
+
# Visualization
|
|
98
|
+
cmap = plt.get_cmap('PiYG')
|
|
99
|
+
contour_lines = 30
|
|
100
|
+
|
|
101
|
+
fig, axes = plt.subplots(nrows=2, ncols=2)
|
|
102
|
+
data = [zt, zpm, np.abs(zpm - zt), np.sqrt(zpv)]
|
|
103
|
+
titles = [
|
|
104
|
+
'function to be approximated',
|
|
105
|
+
f'approximation from {ni} points',
|
|
106
|
+
'true approx error',
|
|
107
|
+
'posterior std'
|
|
108
|
+
]
|
|
109
|
+
|
|
110
|
+
for ax, z, title in zip(axes.flat, data, titles):
|
|
111
|
+
cs = ax.contourf(xt[:, 0].reshape(nt),
|
|
112
|
+
xt[:, 1].reshape(nt),
|
|
113
|
+
z.reshape(nt),
|
|
114
|
+
levels=contour_lines,
|
|
115
|
+
cmap=cmap)
|
|
116
|
+
ax.plot(xi[:, 0], xi[:, 1], 'ro', label='data')
|
|
117
|
+
ax.set_title(title)
|
|
118
|
+
ax.set_xlabel('$x_1$')
|
|
119
|
+
ax.set_ylabel('$x_2$')
|
|
120
|
+
ax.legend()
|
|
121
|
+
fig.colorbar(cs, ax=ax, shrink=0.9)
|
|
122
|
+
|
|
123
|
+
plt.show()
|
|
124
|
+
|
|
125
|
+
# Predictions vs truth
|
|
126
|
+
plt.figure()
|
|
127
|
+
plt.plot(zt, zpm, 'ko')
|
|
128
|
+
(xmin, xmax), (ymin, ymax) = plt.xlim(), plt.ylim()
|
|
129
|
+
xmin = min(xmin, ymin)
|
|
130
|
+
xmax = max(xmax, ymax)
|
|
131
|
+
plt.plot([xmin, xmax], [xmin, xmax], '--')
|
|
132
|
+
plt.xlabel('true values')
|
|
133
|
+
plt.ylabel('predictions')
|
|
134
|
+
plt.show()
|
|
135
|
+
|
|
136
|
+
# LOO predictions
|
|
137
|
+
zloom, zloov, eloo = model.loo(xi, zi)
|
|
138
|
+
gp.misc.plotutils.plot_loo(zi, zloom, zloov)
|
|
139
|
+
|
|
140
|
+
gp.misc.plotutils.crosssections(model, xi, zi, box, [0, 20], [0, 1])
|
|
141
|
+
|
|
142
|
+
if __name__ == '__main__':
|
|
143
|
+
main()
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
"""Prediction of some classical test functions in dimension > 2
|
|
2
|
+
|
|
3
|
+
An anisotropic Matern covariance function is used for the Gaussian
|
|
4
|
+
Process (GP) prior. The parameters of this covariance function
|
|
5
|
+
(variance and ranges) are estimated using the Restricted Maximum
|
|
6
|
+
Likelihood (ReML).
|
|
7
|
+
|
|
8
|
+
The mean function of the GP prior is assumed to be constant and
|
|
9
|
+
unknown.
|
|
10
|
+
|
|
11
|
+
The function is sampled on a space-filling Latin Hypercube design, and
|
|
12
|
+
the data is assumed to be noiseless.
|
|
13
|
+
|
|
14
|
+
----
|
|
15
|
+
Author: Emmanuel Vazquez <emmanuel.vazquez@centralesupelec.fr>
|
|
16
|
+
Copyright (c) 2022-2023, CentraleSupelec
|
|
17
|
+
License: GPLv3 (see LICENSE)
|
|
18
|
+
"""
|
|
19
|
+
import gpmp.num as gnp
|
|
20
|
+
import gpmp as gp
|
|
21
|
+
import matplotlib.pyplot as plt
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def choose_test_case(problem):
|
|
25
|
+
if problem == 1:
|
|
26
|
+
problem_name = "Hartmann4"
|
|
27
|
+
f = gp.misc.testfunctions.hartmann4
|
|
28
|
+
dim = 4
|
|
29
|
+
box = [[0.0] * 4, [1.0] * 4]
|
|
30
|
+
ni = 40
|
|
31
|
+
xi = gp.misc.designs.ldrandunif(dim, ni, box)
|
|
32
|
+
nt = 1000
|
|
33
|
+
xt = gp.misc.designs.ldrandunif(dim, nt, box)
|
|
34
|
+
|
|
35
|
+
elif problem == 2:
|
|
36
|
+
problem_name = "Hartmann6"
|
|
37
|
+
f = gp.misc.testfunctions.hartmann6
|
|
38
|
+
dim = 6
|
|
39
|
+
box = [[0.0] * 6, [1.0] * 6]
|
|
40
|
+
ni = 500
|
|
41
|
+
xi = gp.misc.designs.ldrandunif(dim, ni, box)
|
|
42
|
+
nt = 1000
|
|
43
|
+
xt = gp.misc.designs.ldrandunif(dim, nt, box)
|
|
44
|
+
|
|
45
|
+
elif problem == 3:
|
|
46
|
+
problem_name = "Borehole"
|
|
47
|
+
f = gp.misc.testfunctions.borehole
|
|
48
|
+
dim = 8
|
|
49
|
+
box = [
|
|
50
|
+
[0.05, 100., 63070., 990., 63.1, 700., 1120., 9855.],
|
|
51
|
+
[0.15, 50000., 115600., 1110., 116., 820., 1680., 12045.],
|
|
52
|
+
]
|
|
53
|
+
ni = 30
|
|
54
|
+
xi = gp.misc.designs.maximinldlhs(dim, ni, box)
|
|
55
|
+
nt = 1000
|
|
56
|
+
xt = gp.misc.designs.ldrandunif(dim, nt, box)
|
|
57
|
+
|
|
58
|
+
elif problem == 4:
|
|
59
|
+
problem_name = "detpep8d"
|
|
60
|
+
f = gp.misc.testfunctions.detpep8d
|
|
61
|
+
dim = 8
|
|
62
|
+
box = [[0.0] * 8, [1.0] * 8]
|
|
63
|
+
ni = 60
|
|
64
|
+
xi = gp.misc.designs.maximinldlhs(dim, ni, box)
|
|
65
|
+
nt = 1000
|
|
66
|
+
xt = gp.misc.designs.ldrandunif(dim, nt, box)
|
|
67
|
+
|
|
68
|
+
return problem_name, f, dim, box, ni, xi, nt, xt
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def constant_mean(x, param):
|
|
72
|
+
return gnp.ones((x.shape[0], 1))
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def kernel(x, y, covparam, pairwise=False):
|
|
76
|
+
p = 10
|
|
77
|
+
return gp.kernel.maternp_covariance(x, y, p, covparam, pairwise)
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def visualize_predictions(problem_name, zt, zpm):
|
|
81
|
+
plt.figure()
|
|
82
|
+
plt.plot(zt, zpm, "ko")
|
|
83
|
+
(xmin, xmax), (ymin, ymax) = plt.xlim(), plt.ylim()
|
|
84
|
+
xmin = min(xmin, ymin)
|
|
85
|
+
xmax = max(xmax, ymax)
|
|
86
|
+
plt.plot([xmin, xmax], [xmin, xmax], "--")
|
|
87
|
+
plt.title(problem_name)
|
|
88
|
+
plt.show()
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def main():
|
|
92
|
+
problem = 1
|
|
93
|
+
problem_name, f, dim, box, ni, xi, nt, xt = choose_test_case(problem)
|
|
94
|
+
|
|
95
|
+
zi = f(xi)
|
|
96
|
+
zt = f(xt)
|
|
97
|
+
|
|
98
|
+
model = gp.core.Model(constant_mean, kernel)
|
|
99
|
+
|
|
100
|
+
model, info = gp.kernel.select_parameters_with_reml(model, xi, zi, info=True)
|
|
101
|
+
gp.misc.modeldiagnosis.diag(model, info, xi, zi)
|
|
102
|
+
|
|
103
|
+
(zpm, zpv) = model.predict(xi, zi, xt)
|
|
104
|
+
|
|
105
|
+
visualize_predictions(problem_name, zt, zpm)
|
|
106
|
+
|
|
107
|
+
zloom, zloov, eloo = model.loo(xi, zi)
|
|
108
|
+
gp.misc.plotutils.plot_loo(zi, zloom, zloov)
|
|
109
|
+
|
|
110
|
+
gp.misc.plotutils.crosssections(model, xi, zi, box, [0, 1], list(range(dim)))
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
if __name__ == "__main__":
|
|
114
|
+
main()
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
"""
|
|
2
|
+
GP interpolation in 1D, with noiseless data
|
|
3
|
+
|
|
4
|
+
This example demonstrates how to compute GP interpolation with unknown mean
|
|
5
|
+
(aka ordinary / intrinsic kriging) on a one-dimensional noiseless dataset.
|
|
6
|
+
|
|
7
|
+
A Mat'ern covariance function is used for the Gaussian Process (GP) prior.
|
|
8
|
+
The parameters of this covariance function are assumed to be known
|
|
9
|
+
(i.e., no parameter estimation is performed here).
|
|
10
|
+
|
|
11
|
+
The kriging predictor / posterior mean of the GP, interpolates the data.
|
|
12
|
+
|
|
13
|
+
----
|
|
14
|
+
Author: Emmanuel Vazquez <emmanuel.vazquez@centralesupelec.fr>
|
|
15
|
+
Copyright (c) 2022-2023, CentraleSupelec
|
|
16
|
+
License: GPLv3 (see LICENSE)
|
|
17
|
+
----
|
|
18
|
+
This example is based on the file stk_example_kb01.m from the STK at
|
|
19
|
+
https://github.com/stk-kriging/stk/
|
|
20
|
+
by Julien Bect and Emmanuel Vazquez, released under the GPLv3 license.
|
|
21
|
+
|
|
22
|
+
Original copyright notice:
|
|
23
|
+
|
|
24
|
+
Copyright (c) 2015, 2016, 2018 CentraleSupelec
|
|
25
|
+
Copyright (c) 2011-2014 SUPELEC
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
import math
|
|
29
|
+
import numpy as np
|
|
30
|
+
import gpmp.num as gnp
|
|
31
|
+
import gpmp as gp
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def generate_data():
|
|
35
|
+
"""
|
|
36
|
+
Data generation
|
|
37
|
+
(xt, zt): target
|
|
38
|
+
(xi, zi): input dataset
|
|
39
|
+
"""
|
|
40
|
+
# Build (xt, zt)
|
|
41
|
+
dim = 1
|
|
42
|
+
nt = 200
|
|
43
|
+
box = [[-1], [1]]
|
|
44
|
+
xt = gp.misc.designs.regulargrid(dim, nt, box)
|
|
45
|
+
zt = gp.misc.testfunctions.twobumps(xt)
|
|
46
|
+
|
|
47
|
+
shuffle = True
|
|
48
|
+
if shuffle:
|
|
49
|
+
ni = 5
|
|
50
|
+
ind = np.random.choice(nt, ni, replace=False)
|
|
51
|
+
else:
|
|
52
|
+
ind = [10, 45, 100, 130, 160]
|
|
53
|
+
xi = xt[ind]
|
|
54
|
+
zi = zt[ind]
|
|
55
|
+
|
|
56
|
+
return xt, zt, xi, zi
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def zero_mean(x, param):
|
|
60
|
+
return None
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def constant_mean(x, param):
|
|
64
|
+
return gnp.ones((x.shape[0], 1))
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def kernel_ii_or_tt(x, param, pairwise=False):
|
|
68
|
+
"""Covariance between observations or predictands at x."""
|
|
69
|
+
p = 2
|
|
70
|
+
sigma2 = gnp.exp(param[0])
|
|
71
|
+
loginvrho = param[1]
|
|
72
|
+
nugget = 100 * gnp.eps
|
|
73
|
+
|
|
74
|
+
if pairwise:
|
|
75
|
+
# return a vector of covariances
|
|
76
|
+
K = sigma2 * gnp.ones((x.shape[0], )) # nx x 0
|
|
77
|
+
else:
|
|
78
|
+
# return a covariance matrix
|
|
79
|
+
K = gnp.scaled_distance(loginvrho, x, x) # nx x nx
|
|
80
|
+
K = sigma2 * gp.kernel.maternp_kernel(p, K) + nugget * gnp.eye(K.shape[0])
|
|
81
|
+
|
|
82
|
+
return K
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def kernel_it(x, y, param, pairwise=False):
|
|
86
|
+
"""Covariance between observations and prediction points."""
|
|
87
|
+
p = 2
|
|
88
|
+
sigma2 = gnp.exp(param[0])
|
|
89
|
+
loginvrho = param[1]
|
|
90
|
+
|
|
91
|
+
if pairwise:
|
|
92
|
+
# return a vector of covariances
|
|
93
|
+
K = gnp.scaled_distance_elementwise(loginvrho, x, y) # nx x 0
|
|
94
|
+
else:
|
|
95
|
+
# return a covariance matrix
|
|
96
|
+
K = gnp.scaled_distance(loginvrho, x, y) # nx x ny
|
|
97
|
+
|
|
98
|
+
K = sigma2 * gp.kernel.maternp_kernel(p, K)
|
|
99
|
+
return K
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def kernel(x, y, param, pairwise=False):
|
|
103
|
+
if y is x or y is None:
|
|
104
|
+
return kernel_ii_or_tt(x, param, pairwise)
|
|
105
|
+
else:
|
|
106
|
+
return kernel_it(x, y, param, pairwise)
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def visualize(xt, zt, xi, zi, zpm, zpv):
|
|
110
|
+
fig = gp.misc.plotutils.Figure(isinteractive=True)
|
|
111
|
+
fig.plot(xt, zt, 'C0', linestyle=(0, (5, 5)), linewidth=1.0)
|
|
112
|
+
fig.plotdata(xi, zi)
|
|
113
|
+
fig.plotgp(xt, zpm, zpv)
|
|
114
|
+
fig.xylabels('x', 'z')
|
|
115
|
+
fig.show(grid=True, legend=True, legend_fontsize=9)
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def main():
|
|
119
|
+
xt, zt, xi, zi = generate_data()
|
|
120
|
+
mean = constant_mean
|
|
121
|
+
meanparam = None
|
|
122
|
+
|
|
123
|
+
covparam = gnp.array([math.log(0.5**2), # log(sigma2)
|
|
124
|
+
math.log(1 / .7)]) # log(1/rho)
|
|
125
|
+
|
|
126
|
+
model = gp.core.Model(mean, kernel, meanparam, covparam)
|
|
127
|
+
|
|
128
|
+
zpm, zpv = model.predict(xi, zi, xt)
|
|
129
|
+
visualize(xt, zt, xi, zi, zpm, zpv)
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
if __name__ == "__main__":
|
|
133
|
+
main()
|