Skip to content

2. Monte-Carlo Sampling and Importance Sampling

1. Basic Concepts

(1) Sampling Preliminaries

1) Distribution transformation

In the sampling functions, we call fX(x) as the probability density. In most of cases, we firstly sample from the range (0, 1) and then transform it by some distribution function.

To sample in numpy, we can use rng = np.random.default_rng and then use rng.uniform to sample a uniform distribution. We set

(1.1.1)UU(0,1)U(x)=y

Then we use an exponential distribution with parameter λ, the distribution function is :

(1.1.2)fX=λeλx

the cumulative distribution function would be :

(1.1.3)FX(x)=z=1eλxF1(x)=ln(1FX(x))λ

in order to transform a U into F, we need to find a transform K to map x1 is mapped to x2, i.e.,

(1.1.4)U(x1)=F(x2)

347

Since x1=U1(y) Then the transforming is :

(1.1.5)x2=F1(U(x1))

here U is cumulative distribution function of original variable, F1 is cumulative distribution of target variable.

As a general case, the following part gives the transform from a normal distribution to a exponential distribution :

python
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm

# Number of samples
n_samples = 10_000

# Create a random number generator
rng = np.random.default_rng(seed=42)

# Step 1: Sample U from the uniform distribution (0, 1)
u = rng.normal(0.0, 1.0, size=n_samples)

# Step 2: Transform U into an exponential random variable
lam = 1.0  # Exponential distribution rate parameter
x = -np.log(1.0 - norm.cdf(u)) / lam

# Step 3: Plot the samples as a bar-style histogram
x_max = np.percentile(x, 99)  # Avoid displaying extreme outliers
bins = np.linspace(0, x_max, 40)
# Theoretical exponential probability density function
x_theory = np.linspace(0, x_max, 500)
pdf = lam * np.exp(-lam * x_theory) 

plt.hist(
    x,
    bins=bins,
    density=True,
    alpha=0.75,
    color="steelblue",
    edgecolor="black",
    label="Generated samples"
)
plt.plot(
    x_theory,
    pdf,
    color="red",
    linewidth=2,
    label=r"Theoretical PDF: $\lambda e^{-\lambda x}$"
)

plt.xlabel("x")
plt.ylabel("Probability density")
plt.title("Sampling an Exponential Distribution by Inverse CDF")
plt.legend()
plt.tight_layout()
plt.show()

Which can yield the distribution graph as follows :

335

2) Reliability Analysis & Monte Carlo sampling

Firstly, to define whether a structure is safe or failure, we use a performance function :

(1.1.6)g(x)={0safe<0failure

And we define the failure region as F, The Monte Carlo sampling is sampling directly by fX(x), then the failure probability is :

(1.1.7)Pf=FfX(x)dx=RnIFf(x)dxIf={1failure0safe

where IF is the indicator function of failure.

We note since we already use f(x) as the sampling density, the failure probability is defined by :

(1.1.8)P^f=E(P^f)=1Nj=1nIF(xj)=NFNxjf(x)

The variance of the failure probability can be calculated as :

(1.1.9)V[P^f]=V[1Ni=1nIF(xi)](P^fP^f2)N1

The coefficient of variation (CoV) of the Monte Carlo estimator is :

(1.1.10)CoV[P^f]=V[P^f]P^f1P^f(N1)P^f.

(2) Importance sampling

1) Computation of Failure probability

We know in the Monte-Carlo sampling method, we define the performance function g(X) (denoting the structural fidelity),

(1.2.1)Pf=FIFfX(x)dxfX(x)=i=1nfXi(xi)for independent variables

where fX(x) is the unite sampling density distribution function. Which is the variable distribution in the sampling case (In engineering, all variables often satisfies the normal distribution). We also note i is for the variable, j is for the sampling identity :

(1.2.2)fX(xj)={1 failure 0 valid

However, we can still use another sampling function hX(x), then the failure probability is given by :

(1.2.3)P^f=RnIF(x)fX(x)hX(x)hX(x)dx=1Nj=1N[IF(xj)fX(xj)hX(xj)],xjhX.

This is called Importace sampling[1][2]

2) Mean, standard deviation, and coefficient of variation

Define the importance weight and the contribution of sample j as (weighted failure probability):

(1.2.4)w(x)=fX(x)hX(x),Yj=IF(xj)w(xj),xjhX.

The importance-sampling estimator is P^f=N1j=1NYj. Provided hX(x)>0 wherever IF(x)fX(x)>0, its mean is

(1.2.5)Eh[P^f]=Eh[Y]=RnIF(x)fX(x)hX(x)hX(x)dx=Pf.

Thus, the estimator is unbiased. Its variance (theoretical variance) is :

(1.2.6)Varh[P^f]=Varh[Y]N=1N[RnIF(x)fX2(x)hX(x)dxPf2].

Therefore, the standard deviation (standard error) is :

(1.2.7)Stdh[P^f]=Eh[Y2]Pf2N.

For a simulation estimate, let Y¯=P^f. An unbiased estimator of the variance and the corresponding estimated standard deviation (sample-based estimator) are defined as :

(1.2.8)Var^[P^f]=1N(N1)j=1N(YjY¯)2,Std^[P^f]=Var^[P^f].

We also note in (1.2.8), the term of Var^ can be computed by x as following equation :

(1.2.9)Var^[P^f]=1N(N1)j=1N(IffXhXP^f)2=1N(N1)[j=1NIffX2hX2P^fj=1NIffXhXNP^f+NP^f2]=1N1[1Nj=1NIffX2hX2P^f2]

Finally, for Pf>0, the coefficient of variation is :

(1.2.10)CoV[P^f]=Stdh[P^f]Pf=Eh[Y2]Pf2NPf2,

and its practical estimate is

(1.2.11)CoV^[P^f]=Std^[P^f]P^f.

A good importance density hX assigns substantial probability to the failure region and makes Y=IFfX/hX nearly constant, thereby reducing the standard deviation and CoV.

3) Optimal Importance sampling function

In the structural reliability analysis, we often sample near the failure point, we define as the "design point", which is x0 here. We want to sample more failure points near the failure surface, so the more it easy to fail, we are more willing to sample it. The optimal importance sampling function can be expressed by IF, as :

(1.2.12)hXopt(x)=IF(x)fX(x)P^f

In practical case, we often use the normal distribution near the design point, i.e.,

(1.2.13)hX(x)=12πσsexp(xx0σs)2

Reference


  1. https://en.wikipedia.org/wiki/Importance_sampling ↩︎

  2. 吕震宙, 宋述芳, 李璐伟等, 结构/机构可靠性设计基础 [M] 西北工业大学出版社, 2019.3 ↩︎