-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added example file and documentation for the method along with anothe…
…r test for KLE 2d code
- Loading branch information
Showing
6 changed files
with
112 additions
and
2 deletions.
There are no files selected for viewing
File renamed without changes.
72 changes: 72 additions & 0 deletions
72
docs/code/stochastic_processes/karhunen_loeve/plot_karhunen_loeve_2d.py
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,72 @@ | ||
""" | ||
Karhunen Loeve Expansion 2 Dimesional | ||
================================================================= | ||
In this example, the KL Expansion is used to generate 2 dimensional stochastic fields from a prescribed Autocorrelation | ||
Function. This example illustrates how to use the :class:`.KarhunenLoeveExpansion2D` class for a 2 dimensional | ||
random field and compare the statistics of the generated random field with the expected values. | ||
""" | ||
|
||
# %% md | ||
# | ||
# Import the necessary libraries. Here we import standard libraries such as numpy and matplotlib, but also need to | ||
# import the :class:`.KarhunenLoeveExpansionTwoDimension` class from the :py:mod:`stochastic_processes` module of UQpy. | ||
|
||
# %% | ||
from matplotlib import pyplot as plt | ||
from UQpy.stochastic_process import KarhunenLoeveExpansion2D | ||
import numpy as np | ||
|
||
plt.style.use('seaborn') | ||
|
||
# %% md | ||
# | ||
# The input parameters necessary for the generation of the stochastic processes are given below: | ||
|
||
# %% | ||
|
||
n_samples = 100 # Num of samples | ||
nx, nt = 20, 10 | ||
dx, dt = 0.05, 0.1 | ||
|
||
x = np.linspace(0, (nx - 1) * dx, nx) | ||
t = np.linspace(0, (nt - 1) * dt, nt) | ||
xt_list = np.meshgrid(x, x, t, t, indexing='ij') # R(t_1, t_2, x_1, x_2) | ||
|
||
# %% md | ||
|
||
# Defining the Autocorrelation Function. | ||
|
||
# %% | ||
|
||
R = np.exp(-(xt_list[0] - xt_list[1]) ** 2 - (xt_list[2] - xt_list[3]) ** 2) | ||
# R(x_1, x_2, t_1, t_2) = exp(-(x_1 - x_2) ** 2 -(t_1 - t_2) ** 2) | ||
|
||
KLE_Object = KarhunenLoeveExpansion2D(n_samples=n_samples, correlation_function=R, time_intervals=np.array([dt, dx]), | ||
thresholds=[4, 5], random_state=128) | ||
|
||
# %% md | ||
|
||
# Simulating the samples. | ||
|
||
# %% | ||
|
||
samples = KLE_Object.samples | ||
|
||
# %% md | ||
|
||
# Plotting a sample of the stochastic field. | ||
|
||
# %% | ||
|
||
fig = plt.figure() | ||
plt.title('Realisation of the Karhunen Loeve Expansion for a 2D stochastic field') | ||
plt.plot(samples[0, 0]) | ||
plt.xlabel('t (Time)') | ||
plt.ylabel('x (Space)') | ||
plt.show() | ||
|
||
print('The mean of the samples is ', np.mean(samples), 'whereas the expected mean is 0.000') | ||
print('The variance of the samples is ', np.var(samples), 'whereas the expected variance is 1.000') |
File renamed without changes.
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,32 @@ | ||
Karhunen Loève Expansion for Multi-Dimensional Fields | ||
---------------------------- | ||
|
||
The Karhunen Loève Expansion expands the stochastic field as follows: | ||
|
||
.. math:: A(x, t) = \sum_{n=1}^{\infty} \sum_{k=1}^{\infty}\eta_{nk}(\theta) \sqrt{\lambda_n(x)} f_n(t, x) \sqrt{\mu_{nk}} g_{nk}(x) | ||
|
||
where :math:`\eta_{nk}(\theta)` are uncorrelated standardized normal random variables and :math:`\lambda_n(x)` and :math:`f_n(x, t)` are the eigenvalues and eigenvectors repsectively of the "quasi" one dimensional covariance function :math:`C(x, t_1, t_2)`. :math:`\mu_{nk}` and :math:`g_{nk}(x)` are the eigenvalues and eigenvectors of the derived "one" dimensional covariance function :math:`H(x_1, x_2)`. Additional details regarding the simulation formula can be found at https://www.sciencedirect.com/science/article/abs/pii/S0045782516318692. | ||
|
||
KarhunenLoeve2D Class | ||
^^^^^^^^^^^^^^^^^^^^ | ||
|
||
The :class:`.KarhunenLoeve2D` class is imported using the following command: | ||
|
||
>>> from UQpy.stochastic_process.KarhunenLoeveExpansionTwoDimension2D import KarhunenLoeveExpansion | ||
|
||
Methods | ||
""""""" | ||
.. autoclass:: UQpy.stochastic_process.KarhunenLoeveExpansion2D | ||
:members: run | ||
|
||
Attributes | ||
"""""""""" | ||
.. autoattribute:: UQpy.stochastic_process.KarhunenLoeveExpansion2D.samples | ||
.. autoattribute:: UQpy.stochastic_process.KarhunenLoeveExpansion2D.xi | ||
|
||
Examples | ||
"""""""""" | ||
|
||
.. toctree:: | ||
|
||
Karhunen Loeve Examples <../auto_examples/stochastic_processes/karhunen_loeve_2d/index> |
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