File: plot_draw_covariance_models.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 (460 lines) | stat: -rw-r--r-- 14,887 bytes parent folder | download
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
"""
Kriging : draw covariance models
================================
"""

import openturns as ot
import openturns.viewer as otv
from matplotlib import pylab as plt
import pylab as pl

# %%
# Abstract
# --------
#
# Gaussian processes are a common fixture in `UQ`.
# They are defined by their covariance function and the library implements several of them.
# In this example we plot covariance functions and modify their parameters
# for two families of models: the generalized exponential model and the Matérn models.
#
# For visualization sake, we limit ourselves to the dimension 1.
dimension = 1

# %%
# We set the lower bound to zero for stationary kernels
ot.ResourceMap.SetAsScalar("CovarianceModel-DefaultTMin", 0.0)


# %%
# The generalized exponential model
# ---------------------------------
#
# The :class:`~openturns.GeneralizedExponential` class implements a generalized exponential with a
# parameter :math:`p < 0 \leq 2` exponent. The case :math:`p=1` is the standard exponential model
# while :math:`p=2` is the squared exponential.
#

# %%
# Various parameters p and a fixed correlation length of 0.1
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#
# In this part we set the correlation length to :math:`\theta = 0.1` and study three different models
# with parameters :math:`p=0.25`, :math:`p=1` and :math:`p=2` and trajectories from Gaussian processes
# based on these models.

# %%
# We define the :math:`p = 0.25` generalized exponential model :
covarianceModel = ot.GeneralizedExponential([0.1], 0.25)

# %%
# We define the :math:`p = 1` generalized exponential model :
covarianceModel2 = ot.GeneralizedExponential([0.1], 1.0)

# %%
# We define the :math:`p = 2` generalized exponential model :
covarianceModel3 = ot.GeneralizedExponential([0.1], 2.0)

# %%
# We draw the covariance models :
graphModel = covarianceModel.draw()
graphModel.add(covarianceModel2.draw())
graphModel.add(covarianceModel3.draw())
graphModel.setColors(["green", "orange", "blue"])
graphModel.setXTitle(r"$\tau = \|s-t\|$")
graphModel.setYTitle(r"$C(\tau)$")
graphModel.setLegends([r"$p = 0.25$", r"$p = 1$", r"$p = 2$"])


# %%
# For each covariance model we build a Gaussian process and generate a random trajectory of
# on :math:`[-1,1]`.
# We first build a discretization of this interval with a regular grid with step 0.01.
xmin = -1.0
step = 0.01
n = 200
grid1D = ot.RegularGrid(xmin, step, n + 1)
nbTrajectories = 1

# %%
# We define the first Gaussian process and its trajectory :
process = ot.GaussianProcess(covarianceModel, grid1D)
sample = process.getSample(nbTrajectories)

# %%
# then the second one and its trajectory :
process2 = ot.GaussianProcess(covarianceModel2, grid1D)
sample2 = process2.getSample(nbTrajectories)

# %%
# and finally the third one and its trajectory :
process3 = ot.GaussianProcess(covarianceModel3, grid1D)
sample3 = process3.getSample(nbTrajectories)

# %%
# We draw the trajectories :
graphTraj = sample.drawMarginal(0)
graphTraj.add(sample2.drawMarginal(0))
graphTraj.add(sample3.drawMarginal(0))
graphTraj.setXTitle(r"$x$")
graphTraj.setYTitle(r"$GP_{\nu}(x)$")
graphTraj.setTitle("Random realization from the covariance model")
graphTraj.setColors(["green", "orange", "blue"])
graphTraj.setLegends([r"$p = 0.25$", r"$p = 1$", r"$p = 2$"])

# %%
# We present each covariance model and the corresponding trajectory side by side.
fig = pl.figure(figsize=(12, 4))
ax_pdf = fig.add_subplot(1, 2, 1)
_ = otv.View(graphModel, figure=fig, axes=[ax_pdf])
ax_cdf = fig.add_subplot(1, 2, 2)
_ = otv.View(graphTraj, figure=fig, axes=[ax_cdf])
_ = fig.suptitle(r"Generalized Exponential Model : influence of the p parameter")

# %%
# The blue trajectory corresponding to the parameter :math:`p=2` is smooth as expected as compared with
# the :math:`p=0.25` process which is less regular.


# %%
# The exponential model (:math:`p=1`)
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#
# In the case of the exponential model (:math:`p=1`) we show the influence of the correlation length on
# the trajectories.
#

# %%
# with correlation length :math:`\theta = 0.01`  :
covarianceModel = ot.GeneralizedExponential([0.01], 1.0)

# %%
# with correlation length :math:`\theta = 0.1` :
covarianceModel2 = ot.GeneralizedExponential([0.1], 1.0)

# %%
# with correlation length :math:`\theta = 1.0`
covarianceModel3 = ot.GeneralizedExponential([1.0], 1.0)

# %%
# We draw the covariance models :
graphModel = covarianceModel.draw()
graphModel.add(covarianceModel2.draw())
graphModel.add(covarianceModel3.draw())
graphModel.setColors(["green", "orange", "blue"])
graphModel.setXTitle(r"$\tau = \|s-t\|$")
graphModel.setYTitle(r"$C(\tau)$")
graphModel.setLegends([r"$\theta = 0.01$", r"$\theta = 0.1$", r"$\theta = 1$"])


# %%
# For each covariance model we build a Gaussian process and generate a random trajectory of
# on :math:`[-1,1]`.
# We first build a discretization of this interval with a regular grid with step 0.01.
xmin = -1.0
step = 0.01
n = 200
grid1D = ot.RegularGrid(xmin, step, n + 1)
nbTrajectories = 1

# %%
# We define the first Gaussian process and its trajectory :
process = ot.GaussianProcess(covarianceModel, grid1D)
sample = process.getSample(nbTrajectories)

# %%
# then the second one and its trajectory :
process2 = ot.GaussianProcess(covarianceModel2, grid1D)
sample2 = process2.getSample(nbTrajectories)

# %%
# and finally the third one and its trajectory :
process3 = ot.GaussianProcess(covarianceModel3, grid1D)
sample3 = process3.getSample(nbTrajectories)

# %%
# We draw the trajectories :
graphTraj = sample.drawMarginal(0)
graphTraj.add(sample2.drawMarginal(0))
graphTraj.add(sample3.drawMarginal(0))
graphTraj.setXTitle(r"$x$")
graphTraj.setYTitle(r"$GP_{\theta}(x)$")
graphTraj.setTitle("Random realization from the covariance model")
graphTraj.setColors(["green", "orange", "blue"])
graphTraj.setLegends([r"$\theta = 0.01$", r"$\theta = 0.1$", r"$\theta = 1$"])

# %%
# We present each covariance model and the corresponding tracjectory side by side.
fig = pl.figure(figsize=(12, 4))
ax_pdf = fig.add_subplot(1, 2, 1)
_ = otv.View(graphModel, figure=fig, axes=[ax_pdf])
ax_cdf = fig.add_subplot(1, 2, 2)
_ = otv.View(graphTraj, figure=fig, axes=[ax_cdf])
_ = fig.suptitle(r"Exponential Model : influence of correlation length $\theta$")

# %%
# We observe a smoother trajectory with a high correlation value.


# %%
# The squared exponential (:math:`p=2`)
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#
#
# In the case of the squared exponential model (:math:`p=2`) we show the influence of the correlation length on
# the trajectories.
#

# %%
# with correlation length :math:`\theta = 0.01`  :
covarianceModel = ot.GeneralizedExponential([0.01], 2.0)

# %%
# with correlation length :math:`\theta = 0.1` :
covarianceModel2 = ot.GeneralizedExponential([0.1], 2.0)

# %%
# with correlation length :math:`\theta = 1.0`
covarianceModel3 = ot.GeneralizedExponential([1.0], 2.0)

# %%
# We draw the covariance models :
graphModel = covarianceModel.draw()
graphModel.add(covarianceModel2.draw())
graphModel.add(covarianceModel3.draw())
graphModel.setColors(["green", "orange", "blue"])
graphModel.setXTitle(r"$\tau = \|s-t\|$")
graphModel.setYTitle(r"$C(\tau)$")
graphModel.setLegends([r"$\theta = 0.01$", r"$\theta = 0.1$", r"$\theta = 1$"])


# %%
# For each covariance model we build a Gaussian process and generate a random trajectory of
# on :math:`[-1,1]`.
# We first build a discretization of this interval with a regular grid with step 0.01.
xmin = -1.0
step = 0.01
n = 200
grid1D = ot.RegularGrid(xmin, step, n + 1)
nbTrajectories = 1

# %%
# We define the first Gaussian process and its trajectory :
process = ot.GaussianProcess(covarianceModel, grid1D)
sample = process.getSample(nbTrajectories)

# %%
# then the second one and its trajectory :
process2 = ot.GaussianProcess(covarianceModel2, grid1D)
sample2 = process2.getSample(nbTrajectories)

# %%
# and finally the third one and its trajectory :
process3 = ot.GaussianProcess(covarianceModel3, grid1D)
sample3 = process3.getSample(nbTrajectories)

# %%
# We draw the trajectories :
graphTraj = sample.drawMarginal(0)
graphTraj.add(sample2.drawMarginal(0))
graphTraj.add(sample3.drawMarginal(0))
graphTraj.setXTitle(r"$x$")
graphTraj.setYTitle(r"$GP_{\theta}(x)$")
graphTraj.setTitle("Random realization from the covariance model")
graphTraj.setColors(["green", "orange", "blue"])
graphTraj.setLegends([r"$\theta = 0.01$", r"$\theta = 0.1$", r"$\theta = 1$"])

# %%
# We present each covariance model and the corresponding tracjectory side by side.

fig = pl.figure(figsize=(12, 4))
ax_pdf = fig.add_subplot(1, 2, 1)
_ = otv.View(graphModel, figure=fig, axes=[ax_pdf])
ax_cdf = fig.add_subplot(1, 2, 2)
_ = otv.View(graphTraj, figure=fig, axes=[ax_cdf])
_ = fig.suptitle(
    r"Squared exponential model : influence of correlation length $\theta$"
)

# %%
# Except for very small values of the correlation length, trajectories are usually smooth. It is the
# main effect of the squared exponential model which leads to smooth processes.


# %%
# The Matérn covariance model
# ---------------------------
#
# The :class:`~openturns.MaternModel` class implements the Matern model of parameter :math:`\nu`.
# This parameter controls the smoothness of the process : for any :math:`\nu = n + \frac{1}{2}` the
# process is :math:`n` times continuously differentiable.

# %%
# Influence of the regularity
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^
#
# In this paragraph we represent three models with different regularity and generate the
# corresponding random trajectories. We shall use :math:`\nu = 0.5`, :math:`\nu = 1.5` and
# :math:`\nu = 2.5` and observe the regularity.
#

# %%
# We define the :math:`\nu = 0.5` Matern model :
covarianceModel = ot.MaternModel([1.0], 0.5)

# %%
# We define the :math:`\nu = 1.5` Matern model :
covarianceModel2 = ot.MaternModel([1.0], 1.5)

# %%
# We define the :math:`\nu = 2.5` Matern model :
covarianceModel3 = ot.MaternModel([1.0], 2.5)

# %%
# We draw the covariance models :
graphModel = covarianceModel.draw()
graphModel.add(covarianceModel2.draw())
graphModel.add(covarianceModel3.draw())
graphModel.setColors(["green", "orange", "blue"])
graphModel.setXTitle(r"$\tau = \|s-t\|$")
graphModel.setYTitle(r"$C(\tau)$")
graphModel.setLegends([r"$\nu = 1/2$", r"$\nu = 3/2$", r"$\nu = 5/2$"])


# %%
# For each covariance model we build a Gaussian process and generate a random trajectory of
# on :math:`[-1,1]`.
# We first build a discretization of this interval with a regular grid with step 0.001.
xmin = -5.0
step = 0.01
n = 1000
grid1D = ot.RegularGrid(xmin, step, n + 1)
nbTrajectories = 1

# %%
# We define the first Gaussian process and its trajectory :
process = ot.GaussianProcess(covarianceModel, grid1D)
sample = process.getSample(nbTrajectories)

# %%
# then the second one and its trajectory :
process2 = ot.GaussianProcess(covarianceModel2, grid1D)
sample2 = process2.getSample(nbTrajectories)

# %%
# and finally the third one and its trajectory :
process3 = ot.GaussianProcess(covarianceModel3, grid1D)
sample3 = process3.getSample(nbTrajectories)

# %%
# We draw the trajectories :
graphTraj = sample.drawMarginal(0)
graphTraj.add(sample2.drawMarginal(0))
graphTraj.add(sample3.drawMarginal(0))
graphTraj.setXTitle(r"$x$")
graphTraj.setYTitle(r"$GP_{\nu}(x)$")
graphTraj.setTitle("Random realization from the covariance model")
graphTraj.setColors(["green", "orange", "blue"])
graphTraj.setLegends([r"$\nu = 1/2$", r"$\nu = 3/2$", r"$\nu = 5/2$"])

# %%
# We present each covariance model and the corresponding tracjectory side by side.
fig = pl.figure(figsize=(12, 4))
ax_pdf = fig.add_subplot(1, 2, 1)
_ = otv.View(graphModel, figure=fig, axes=[ax_pdf])
ax_cdf = fig.add_subplot(1, 2, 2)
_ = otv.View(graphTraj, figure=fig, axes=[ax_cdf])
_ = fig.suptitle(r"Matern model : influence of the regularity $\nu$ parameter")

# %%
# The red trajectory is the least regular (:math:`nu = 0.5`) as it is only continuous. We see that the
# the blue trajectory is more smooth as expected.


# %%
# Variation of the correlation length
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#
# In this paragraph we fix the regularity by choosing :math:`\nu = 1.5` so we expect a continuously
# differentiable realization.
# We then use three different correlation lengths :math:`\theta = 0.01`, :math:`\theta = 0.1` and
# :math:`\theta = 1.0` and observe the impact on realizations of Gaussian processes based on these
# covariance models.

# %%
# We define the Matern model with :math:`\theta = 0.01` :
covarianceModel = ot.MaternModel([0.01], 1.5)

# %%
# We define the Matern model with :math:`\theta = 0.1` :
covarianceModel2 = ot.MaternModel([0.1], 1.5)

# %%
# We define the Matern model with :math:`\theta = 1.0` :
covarianceModel3 = ot.MaternModel([1.0], 1.5)

# %%
# We draw the covariance models :
graphModel = covarianceModel.draw()
graphModel.add(covarianceModel2.draw())
graphModel.add(covarianceModel3.draw())
graphModel.setColors(["green", "orange", "blue"])
graphModel.setXTitle(r"$\tau = \|s-t\|$")
graphModel.setYTitle(r"$C(\tau)$")
graphModel.setTitle("Matern covariance model with \nu = 3/2")
graphModel.setLegends([r"$\theta = 0.01$", r"$\theta = 0.1$", r"$\theta = 1.0$"])

# %%
# For each covariance model we build a Gaussian process and generate a random trajectory of
# on :math:`[-1,1]`.
# We build a discretization of this interval with a regular grid with step 0.01.
xmin = -1.0
step = 0.01
n = 200
grid1D = ot.RegularGrid(xmin, step, n + 1)
nbTrajectories = 1

# %%
# We define the first Gaussian process and its trajectory :
process = ot.GaussianProcess(covarianceModel, grid1D)
sample = process.getSample(nbTrajectories)

# %%
# then the second process :
process2 = ot.GaussianProcess(covarianceModel2, grid1D)
sample2 = process2.getSample(nbTrajectories)

# %%
# and the third one :
process3 = ot.GaussianProcess(covarianceModel3, grid1D)
sample3 = process3.getSample(nbTrajectories)

# %%
# We draw the trajectories :
graphTraj = sample.drawMarginal(0)
graphTraj.add(sample2.drawMarginal(0))
graphTraj.add(sample3.drawMarginal(0))
graphTraj.setXTitle(r"$x$")
graphTraj.setYTitle(r"$GP_{\theta}(x)$")
graphTraj.setColors(["green", "orange", "blue"])
graphTraj.setLegends([r"$\theta = 0.01$", r"$\theta = 0.1$", r"$\theta = 1.0$"])

# %%
# We present each covariance model and the corresponding tracjectory side by side.
fig = pl.figure(figsize=(12, 4))
ax_pdf = fig.add_subplot(1, 2, 1)
_ = otv.View(graphModel, figure=fig, axes=[ax_pdf])
ax_cdf = fig.add_subplot(1, 2, 2)
_ = otv.View(graphTraj, figure=fig, axes=[ax_cdf])
_ = fig.suptitle("The Matern model : variation of the correlation length")

# %%
# From the previous figure we see that the trajectory of the Gaussian process is smoother with large
# correlation length.

# %%
# Display figures
plt.show()

# %%
# Reset default settings
ot.ResourceMap.Reload()