-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #282 from damar-wicaksono/dev-261
Add an implementation of the 2D function from Webster et al. (1996).
- Loading branch information
Showing
10 changed files
with
278 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
--- | ||
jupytext: | ||
formats: ipynb,md:myst | ||
text_representation: | ||
extension: .md | ||
format_name: myst | ||
format_version: 0.13 | ||
jupytext_version: 1.14.1 | ||
kernelspec: | ||
display_name: Python 3 (ipykernel) | ||
language: python | ||
name: python3 | ||
--- | ||
|
||
(test-functions:webster-2d)= | ||
# Two-Dimensional Function from Webster et al. (1996) | ||
|
||
```{code-cell} ipython3 | ||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
import uqtestfuns as uqtf | ||
``` | ||
|
||
The 2D function introduced in {cite}`Webster1996` is a polynomial function. | ||
It was used to illustrate the construction of a polynomial chaos expansion | ||
metamodel (via stochastic collocation) | ||
having uncertain (random) input variables. | ||
|
||
```{code-cell} ipython3 | ||
:tags: [remove-input] | ||
from mpl_toolkits.axes_grid1 import make_axes_locatable | ||
my_fun = uqtf.Webster2D() | ||
# --- Create 2D data | ||
lb_1 = my_fun.prob_input.marginals[0].lower | ||
ub_1 = my_fun.prob_input.marginals[0].upper | ||
lb_2 = ( | ||
my_fun.prob_input.marginals[1].parameters[0] | ||
- 3 * my_fun.prob_input.marginals[1].parameters[1] | ||
) | ||
ub_2 = ( | ||
my_fun.prob_input.marginals[1].parameters[0] | ||
+ 3 * my_fun.prob_input.marginals[1].parameters[1] | ||
) | ||
xx_1 = np.linspace(lb_1, ub_1, 1000)[:, np.newaxis] | ||
xx_2 = np.linspace(lb_2, ub_2, 1000)[:, np.newaxis] | ||
mesh_2d = np.meshgrid(xx_1, xx_2) | ||
xx_2d = np.array(mesh_2d).T.reshape(-1, 2) | ||
yy_2d = my_fun(xx_2d) | ||
# --- Create two-dimensional plots | ||
fig = plt.figure(figsize=(10, 5)) | ||
# Surface | ||
axs_1 = plt.subplot(121, projection='3d') | ||
axs_1.plot_surface( | ||
mesh_2d[0], | ||
mesh_2d[1], | ||
yy_2d.reshape(1000,1000).T, | ||
linewidth=0, | ||
cmap="plasma", | ||
antialiased=False, | ||
alpha=0.5 | ||
) | ||
axs_1.set_xlabel("$x_1$", fontsize=14) | ||
axs_1.set_ylabel("$x_2$", fontsize=14) | ||
axs_1.set_zlabel("$\mathcal{M}(x_1, x_2)$", fontsize=14) | ||
axs_1.set_title("Surface plot of Webster 2D", fontsize=14) | ||
# Contour | ||
axs_2 = plt.subplot(122) | ||
cf = axs_2.contourf( | ||
mesh_2d[0], mesh_2d[1], yy_2d.reshape(1000, 1000).T, cmap="plasma" | ||
) | ||
axs_2.set_xlim([lb_1, ub_1]) | ||
axs_2.set_ylim([lb_2, ub_2]) | ||
axs_2.set_xlabel("$x_1$", fontsize=14) | ||
axs_2.set_ylabel("$x_2$", fontsize=14) | ||
axs_2.set_title("Contour plot of Webster 2D", fontsize=14) | ||
divider = make_axes_locatable(axs_2) | ||
cax = divider.append_axes('right', size='5%', pad=0.05) | ||
fig.colorbar(cf, cax=cax, orientation='vertical') | ||
fig.tight_layout(pad=4.0) | ||
plt.gcf().set_dpi(75); | ||
``` | ||
|
||
## Test function instance | ||
|
||
To create a default instance of the test function: | ||
|
||
```{code-cell} ipython3 | ||
my_testfun = uqtf.Webster2D() | ||
``` | ||
|
||
Check if it has been correctly instantiated: | ||
|
||
```{code-cell} ipython3 | ||
print(my_testfun) | ||
``` | ||
|
||
## Description | ||
|
||
The Webster 2D function is defined as follows[^location]: | ||
|
||
$$ | ||
\mathcal{M}(\boldsymbol{x}) = A^2 + B^3, | ||
$$ | ||
where $\boldsymbol{x} = \{ A, B \}$ | ||
is the two-dimensional vector of input variables further defined below. | ||
|
||
## Probabilistic input | ||
|
||
Based on {cite}`Webster1996`, the probabilistic input model | ||
for the function consists of two independent random variables as shown below. | ||
|
||
```{code-cell} ipython3 | ||
my_testfun.prob_input | ||
``` | ||
|
||
## Reference results | ||
|
||
This section provides several reference results of typical UQ analyses involving | ||
the test function. | ||
|
||
### Sample histogram | ||
|
||
Shown below is the histogram of the output based on $100'000$ random points: | ||
|
||
```{code-cell} ipython3 | ||
:tags: [hide-input] | ||
xx_test = my_testfun.prob_input.get_sample(100000) | ||
yy_test = my_testfun(xx_test) | ||
plt.hist(yy_test, bins="auto", color="#8da0cb"); | ||
plt.grid(); | ||
plt.ylabel("Counts [-]"); | ||
plt.xlabel("$\mathcal{M}(\mathbf{X})$"); | ||
plt.gcf().set_dpi(150); | ||
``` | ||
|
||
## References | ||
|
||
```{bibliography} | ||
:filter: docname in docnames | ||
``` | ||
|
||
[^location]: see Eq. (8), Section 2.2, p. 4 in {cite}`Webster1996`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
""" | ||
Module with an implementation of the 2D function from Webster et al. (1996). | ||
The two-dimensional function is polynomial with random input variables. | ||
It was introduced in [1] and used to illustrate the construction of polynomial | ||
chaos expansion metamodel. | ||
References | ||
---------- | ||
1. M. Webster, M. A. Tatang, and G. J. McRae, “Application of the probabilistic | ||
collocation method for an uncertainty analysis of a simple ocean model,” | ||
Massachusetts Institute of Technology, Cambridge, MA, | ||
Joint Program Report Series 4, 1996. | ||
[Online]. Available: http://globalchange.mit.edu/publication/15670 | ||
""" | ||
import numpy as np | ||
|
||
from ..core.prob_input.input_spec import UnivDistSpec, ProbInputSpecFixDim | ||
from ..core.uqtestfun_abc import UQTestFunABC | ||
|
||
__all__ = ["Webster2D"] | ||
|
||
|
||
AVAILABLE_INPUT_SPECS = { | ||
"Webster1996": ProbInputSpecFixDim( | ||
name="Webster1996", | ||
description=( | ||
"Input specification for the 2D function " | ||
"from Webster et al. (1996)" | ||
), | ||
marginals=[ | ||
UnivDistSpec( | ||
name="A", | ||
distribution="uniform", | ||
parameters=[1.0, 10.0], | ||
description="None", | ||
), | ||
UnivDistSpec( | ||
name="B", | ||
distribution="normal", | ||
parameters=[2.0, 1.0], | ||
description="None", | ||
), | ||
], | ||
copulas=None, | ||
), | ||
} | ||
|
||
|
||
def evaluate(xx: np.ndarray): | ||
"""Evaluate the 2D Webster function on a set of input values. | ||
Parameters | ||
---------- | ||
xx : np.ndarray | ||
Two-Dimensional input values given by N-by-2 arrays where | ||
N is the number of input values. | ||
Returns | ||
------- | ||
np.ndarray | ||
The output of the 2D Webster function evaluated on the input values. | ||
The output is a 1-dimensional array of length N. | ||
""" | ||
yy = xx[:, 0] ** 2 + xx[:, 1] ** 3 | ||
|
||
return yy | ||
|
||
|
||
class Webster2D(UQTestFunABC): | ||
"""A concrete implementation of the function from Webster et al. (1996).""" | ||
|
||
_tags = ["metamodeling"] | ||
_description = "2D polynomial function from Webster et al. (1996)." | ||
_available_inputs = AVAILABLE_INPUT_SPECS | ||
_available_parameters = None | ||
_default_spatial_dimension = 2 | ||
|
||
eval_ = staticmethod(evaluate) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters