Note
Go to the end to download the full example code or to run this example in your browser via Binder
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 KarhunenLoeveExpansion2D class for a 2 dimensional
random field and compare the statistics of the generated random field with the expected values.
Import the necessary libraries. Here we import standard libraries such as numpy and matplotlib, but also need to
import the KarhunenLoeveExpansionTwoDimension class from the stochastic_processes module of UQpy.
from matplotlib import pyplot as plt
from UQpy.stochastic_process import KarhunenLoeveExpansion2D
import numpy as np
The input parameters necessary for the generation of the stochastic processes are given below:
# Defining the Autocorrelation Function.
# Simulating the samples.
# Plotting a sample of the stochastic field.
fig = plt.figure()
plt.title('Realisation of the Karhunen Loeve Expansion for a 2D stochastic field')
plt.imshow(samples[0, 0])
plt.ylabel('t (Time)')
plt.xlabel('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')

The mean of the samples is 0.06238506034183266 whereas the expected mean is 0.000
The variance of the samples is 0.873477875144356 whereas the expected variance is 1.000
Total running time of the script: ( 0 minutes 0.050 seconds)