INPUT MODULE: SAMPLING STRATEGIES¶
This example showcases how to define a probabilistic input model and then use it to draw samples using various sampling strategies.
Package imports¶
In [1]:
from uqpylab import sessions
import matplotlib.pyplot as plt
from uqpylab.display_util import get_uq_color_order
Parameters¶
In [2]:
UQ_colors = get_uq_color_order(4)
Start a remote UQCloud session¶
In [3]:
# Start the session
mySession = sessions.cloud()
# (Optional) Get a convenient handle to the command line interface
uq = mySession.cli
# Reset the session
mySession.reset()
Processing .
.
done! uqpylab.sessions :: INFO :: This is UQ[py]Lab, version 1.00, running on https://uqcloud.ethz.ch. UQ[py]Lab is free software, published under the open source BSD 3-clause license. To request special permissions, please contact: - Stefano Marelli (marelli@ibk.baug.ethz.ch). A new session (c570d033a5c646458749a4e5d3c28d6f) started.
uqpylab.sessions :: INFO :: Reset successful.
Set the random seed for reproducibility¶
In [4]:
uq.rng(1, 'twister');
Probabilistic input model¶
The probabilistic input model consists of two uniform random variables $$X_i \sim \mathcal{U}(0, 1) \qquad i = 1,2 $$ Specify the marginals:
In [5]:
InputOpts = {
'Marginals': [
{
'Type': 'Uniform',
'Parameters': [0,1]
},
{
'Type': 'Uniform',
'Parameters': [0,1]
}
]
}
Create an INPUT object based on the specified marginals:
In [6]:
myInput = uq.createInput(InputOpts)
Print a report on the created INPUT object:
In [7]:
uq.print(myInput)
============================================================== Input object name: Input 1 Dimension(M): 2 Marginals: Index | Name | Type | Parameters | Moments ------------------------------------------------------------------------- 1 | X1 | Uniform | 0.000e+00, 1.000e+00 | 5.000e-01, 2.887e-01 2 | X2 | Uniform | 0.000e+00, 1.000e+00 | 5.000e-01, 2.887e-01 Copula: Type: Independent Dimension: 2 Variables coupled: [1 2] ==============================================================
Drawing samples¶
Different samples from the INPUT object are drawn using various sampling strategies.
1) Monte Carlo sampling¶
In [8]:
X_MC = uq.getSample(N=80, Method='MC')
In [9]:
fig_mc, ax_mc = plt.subplots()
ax_mc.scatter(X_MC[:,0], X_MC[:,1], color=UQ_colors[0])
ax_mc.set_xlabel("$X_1$")
ax_mc.set_ylabel("$X_2$")
ax_mc.set_title("MCS")
ax_mc.grid()
2) Latin hypercube sampling¶
In [10]:
X_LHS = uq.getSample(N=80, Method='LHS');
In [11]:
fig_lhs, ax_lhs = plt.subplots()
ax_lhs.scatter(X_LHS[:,0], X_LHS[:,1], color=UQ_colors[1])
ax_lhs.set_xlabel("$X_1$")
ax_lhs.set_ylabel("$X_2$")
ax_lhs.set_title("LHS")
ax_lhs.grid()
3) Sobol' sequence sampling¶
In [12]:
X_Sobol = uq.getSample(N=80, Method='Sobol');
In [13]:
fig_sobol, ax_sobol = plt.subplots()
ax_sobol.scatter(X_Sobol[:,0], X_Sobol[:,1], color=UQ_colors[2])
ax_sobol.set_xlabel("$X_1$")
ax_sobol.set_ylabel("$X_2$")
ax_sobol.set_title("Sobol")
ax_sobol.grid()
4) Halton sequence sampling¶
In [14]:
X_Halton = uq.getSample(N=80, Method='Halton');
In [15]:
fig_halton, ax_halton = plt.subplots()
ax_halton.scatter(X_Halton[:,0], X_Halton[:,1], color=UQ_colors[3])
ax_halton.set_xlabel("$X_1$")
ax_halton.set_ylabel("$X_2$")
ax_halton.set_title("Halton")
ax_halton.grid()
Comparison of sampling strategies¶
In [16]:
fig_all, axs_all = plt.subplots(2, 2, figsize=(10, 8), layout='constrained')
axs_all[0,0].scatter(X_MC[:,0], X_MC[:,1], color=UQ_colors[0])
axs_all[0,0].set_title("MCS")
axs_all[0,1].scatter(X_LHS[:,0], X_LHS[:,1], color=UQ_colors[1])
axs_all[0,1].set_title("LHS")
axs_all[1,0].scatter(X_Sobol[:,0], X_Sobol[:,1], color=UQ_colors[2])
axs_all[1,0].set_title("Sobol'")
axs_all[1,1].scatter(X_Halton[:,0], X_Halton[:,1], color=UQ_colors[3])
axs_all[1,1].set_title("Halton")
for ax in axs_all.flatten():
ax.grid()
plt.setp(axs_all[-1, :], xlabel="$X_1$")
plt.setp(axs_all[:, 0], ylabel="$X_2$")
Out[16]:
[Text(0, 0.5, '$X_2$'), Text(0, 0.5, '$X_2$')]
Terminate the remote UQCloud session¶
In [17]:
mySession.quit()
uqpylab.sessions :: INFO :: Session c570d033a5c646458749a4e5d3c28d6f terminated.
Out[17]:
True