Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
psimpy
Manage
Activity
Members
Labels
Plan
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
mbd
psimpy
Commits
f2f7d94a
Commit
f2f7d94a
authored
2 years ago
by
Hu Zhao
Browse files
Options
Downloads
Patches
Plain Diff
docs: add example of Latin hypercube sampling
parent
b672be1b
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
docs/examples/sampler/plot_latin.py
+91
-0
91 additions, 0 deletions
docs/examples/sampler/plot_latin.py
with
91 additions
and
0 deletions
docs/examples/sampler/plot_latin.py
0 → 100644
+
91
−
0
View file @
f2f7d94a
"""
Latin hypercube sampling
========================
"""
# %% md
#
# This example shows how to draw samples using Latin hypercube sampling.
#
#
# %% md
#
# For the illustration purpose, let's have a look at a two-dimensional example
# where we have two random variables X and Y. Each is uniformly distributed in its
# range.
import
numpy
as
np
ndim
=
2
# range of X is 10 to 20, range of Y is -10 to 0
bounds
=
np
.
array
([[
10
,
20
],
[
-
10
,
0
]])
# %% md
#
# Given this setting, we can import :class:`.Latin`, create an instance, and
# call the :py:meth:`.Latin.sample` method to draw required number of samples
from
psimpy.sampler
import
LHS
# setting seed leads to same samples every time when the codes are run
lhs_sampler
=
LHS
(
ndim
,
bounds
,
seed
=
10
)
lhs_samples
=
lhs_sampler
.
sample
(
nsamples
=
5
)
# %% md
#
# The samples are plotted in the following figure.
import
matplotlib.pyplot
as
plt
fig
,
ax
=
plt
.
subplots
(
figsize
=
(
6
,
4
))
ax
.
scatter
(
lhs_samples
[:,
0
],
lhs_samples
[:,
1
],
s
=
10
,
c
=
'
blue
'
,
marker
=
'
o
'
)
ax
.
set_xlabel
(
'
X
'
)
ax
.
set_ylabel
(
'
Y
'
)
ax
.
set_xlim
(
bounds
[
0
])
ax
.
set_ylim
(
bounds
[
1
])
ax
.
set_title
(
"
Latin hypercube samples (criterion=
'
random
'
)
"
)
_
=
ax
.
grid
(
visible
=
True
,
which
=
'
major
'
,
axis
=
'
both
'
)
# %% md
#
# There are different criterions to pick samples in each hypercube. The default
# is `random`, as used above. Other options are `center` and `maximin`. For instance,
# we can use the `center` criterion to draw :math:`5` samples as follows:
lhs_sampler
=
LHS
(
ndim
,
bounds
,
criterion
=
'
center
'
,
seed
=
10
)
lhs_samples
=
lhs_sampler
.
sample
(
nsamples
=
5
)
fig
,
ax
=
plt
.
subplots
(
figsize
=
(
6
,
4
))
ax
.
scatter
(
lhs_samples
[:,
0
],
lhs_samples
[:,
1
],
s
=
10
,
c
=
'
blue
'
,
marker
=
'
o
'
)
ax
.
set_xlabel
(
'
X
'
)
ax
.
set_ylabel
(
'
Y
'
)
ax
.
set_xlim
(
bounds
[
0
])
ax
.
set_ylim
(
bounds
[
1
])
ax
.
set_title
(
"
Latin hypercube samples (criterion=
'
center
'
)
"
)
_
=
ax
.
grid
(
visible
=
True
,
which
=
'
major
'
,
axis
=
'
both
'
)
# %% md
#
# And we can use the `maximin` criterion as follows:
lhs_sampler
=
LHS
(
ndim
,
bounds
,
criterion
=
'
maximin
'
,
seed
=
10
,
iteration
=
500
)
lhs_samples
=
lhs_sampler
.
sample
(
nsamples
=
5
)
fig
,
ax
=
plt
.
subplots
(
figsize
=
(
6
,
4
))
ax
.
scatter
(
lhs_samples
[:,
0
],
lhs_samples
[:,
1
],
s
=
10
,
c
=
'
blue
'
,
marker
=
'
o
'
)
ax
.
set_xlabel
(
'
X
'
)
ax
.
set_ylabel
(
'
Y
'
)
ax
.
set_xlim
(
bounds
[
0
])
ax
.
set_ylim
(
bounds
[
1
])
ax
.
set_title
(
"
Latin hypercube samples (criterion=
'
maximin
'
)
"
)
_
=
ax
.
grid
(
visible
=
True
,
which
=
'
major
'
,
axis
=
'
both
'
)
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment