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
55652b10
Commit
55652b10
authored
2 years ago
by
Hu Zhao
Browse files
Options
Downloads
Patches
Plain Diff
docs: add example of Saltelli sampling
parent
5eee2981
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_saltelli.py
+82
-0
82 additions, 0 deletions
docs/examples/sampler/plot_saltelli.py
with
82 additions
and
0 deletions
docs/examples/sampler/plot_saltelli.py
0 → 100644
+
82
−
0
View file @
55652b10
"""
Saltelli sampling
=================
"""
# %% md
#
# This example shows how to draw samples using Saltelli sampling.
# Assume that there is a three-dimensional problem where X, Y, and Z are the
# three random variables.
import
numpy
as
np
ndim
=
3
# range of X is 10 to 20, range of Y is 100 to 200, range of Z is 1000 to 2000
bounds
=
np
.
array
([[
10
,
20
],
[
100
,
200
],
[
1000
,
2000
]])
# %% md
#
# Given this setting, we can import :class:`.Saltelli`, create an instance, and
# call the :py:meth:`.Saltelli.sample` method to draw required number of samples.
from
psimpy.sampler
import
Saltelli
saltelli_sampler
=
Saltelli
(
ndim
,
bounds
,
calc_second_order
=
False
)
saltelli_samples
=
saltelli_sampler
.
sample
(
nbase
=
128
)
# %% md
#
# In above codes, we set ``calc_second_order`` to `False`. It means that picked
# samples can be used in following Sobol' analysis to compute first-order and
# total-effect Sobol' indices but not second-order Sobol' indices. It leads to
# :math:`nbase*(ndim+2)` samples as shown below
import
matplotlib.pyplot
as
plt
fig
=
plt
.
figure
()
ax
=
fig
.
add_subplot
(
projection
=
'
3d
'
)
ax
.
scatter
(
saltelli_samples
[:,
0
],
saltelli_samples
[:,
1
],
saltelli_samples
[:,
2
],
marker
=
'
o
'
)
ax
.
set_xlabel
(
'
X
'
)
ax
.
set_ylabel
(
'
Y
'
)
ax
.
set_zlabel
(
'
Z
'
)
plt
.
tight_layout
()
print
(
'
Number of samples:
'
,
f
'
{
len
(
saltelli_samples
)
}
'
)
# %% md
#
# If we want to draw samples which can also be used to compute second-order
# Sobol' indices, we need to set ``calc_second_order`` to `True`.
# It leads to :math:`nbase*(2*ndim+2)` samples.
saltelli_sampler
=
Saltelli
(
ndim
,
bounds
,
calc_second_order
=
True
)
saltelli_samples
=
saltelli_sampler
.
sample
(
nbase
=
128
)
fig
=
plt
.
figure
()
ax
=
fig
.
add_subplot
(
projection
=
'
3d
'
)
ax
.
scatter
(
saltelli_samples
[:,
0
],
saltelli_samples
[:,
1
],
saltelli_samples
[:,
2
],
marker
=
'
o
'
)
ax
.
set_xlabel
(
'
X
'
)
ax
.
set_ylabel
(
'
Y
'
)
ax
.
set_zlabel
(
'
Z
'
)
plt
.
tight_layout
()
print
(
'
Number of samples:
'
,
f
'
{
len
(
saltelli_samples
)
}
'
)
# %% md
# .. note:: If one has a two-dimensional problem, there is no need to set
# ``calc_second_order`` to `True`. The reason is that the second-order Sobol'
# index can be directly computed based on the first-order and total-effect
# Sobol' index in that case.
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