算法说明
Box-Muller变换是通过服从均匀分布的随机变量,来构建服从高斯分布的随机变量的一种方法。具体的描述为:选取两个服从
[
0
,
1
]
[0,1]
[0,1]上均匀分布的随机变
U
1
U_1
U1和
U
2
U_2
U2,使得
X
X
X,
Y
Y
Y满足高斯分布
X
=
σ
cos
(
2
π
U
1
)
−
2
ln
U
2
+
μ
.
,
Y
=
σ
sin
(
2
π
U
1
)
−
2
ln
U
2
+
μ
.
X = \sigma \cos (2\pi {U_1})\sqrt { - 2\ln {U_2}} + \mu., \quad Y = \sigma \sin (2\pi {U_1})\sqrt { - 2\ln {U_2}} + \mu.
X=σcos(2πU1)−2lnU2+μ.,Y=σsin(2πU1)−2lnU2+μ.
那么,
X
X
X,
Y
Y
Y~
N
(
μ
,
σ
2
)
N(\mu , \sigma^2)
N(μ,σ2)。
推导证明
P
r
o
o
f
:
Proof:
Proof: Suppose
X
X
X and
Y
Y
Y satisfy
N
(
μ
,
σ
2
)
N(\mu, \sigma^2)
N(μ,σ2), and they are
i
.
i
.
d
.
i.i.d.
i.i.d., the pdf is
p
(
X
)
=
1
2
π
σ
e
−
(
X
−
μ
)
2
2
σ
2
,
p
(
Y
)
=
1
2
π
σ
e
−
(
Y
−
μ
)
2
2
σ
2
.
p(X)=\frac{1}{\sqrt{2\pi} \sigma}e^{-\frac{(X-\mu)^2}{2\sigma^2}}, \quad p(Y)=\frac{1}{\sqrt{2\pi} \sigma}e^{-\frac{(Y-\mu)^2}{2\sigma^2}}.
p(X)=2πσ1e−2σ2(X−μ)2,p(Y)=2πσ1e−2σ2(Y−μ)2.
then we have
P
(
Z
)
=
P
(
X
Y
)
=
P
(
X
)
P
(
Y
)
=
1
2
π
σ
2
e
−
(
X
−
μ
)
2
+
(
Y
−
μ
)
2
2
σ
2
.
P(Z)=P(XY)=P(X)P(Y)=\frac{1}{2\pi \sigma^2}e^{-\frac{(X-\mu)^2+(Y-\mu)^2}{2\sigma^2}}.
P(Z)=P(XY)=P(X)P(Y)=2πσ21e−2σ2(X−μ)2+(Y−μ)2.
Do coordinate transformation, then
X
=
R
c
o
s
(
θ
)
+
μ
,
Y
=
R
s
i
n
(
θ
)
+
μ
.
X=Rcos(\theta)+\mu, \quad Y=Rsin(\theta)+\mu.
X=Rcos(θ)+μ,Y=Rsin(θ)+μ.
the sum of pdf of
Z
Z
Z can be written as
1
=
∫
−
∞
∞
∫
−
∞
∞
1
2
π
σ
2
e
−
(
X
−
μ
)
2
+
(
Y
−
μ
)
2
2
σ
2
d
X
d
Y
=
∫
0
2
π
∫
0
∞
1
2
π
σ
2
e
−
R
2
2
σ
2
R
d
R
d
θ
.
1=\int\limits_{ - \infty }^\infty {\int\limits_{ - \infty }^\infty {\frac{1}{{2\pi {\sigma ^2}}}{e^{ - \frac{{{{(X - \mu )}^2} + {{(Y - \mu )}^2}}}{{2{\sigma ^2}}}}}dXdY} } =\int\limits_0^{2\pi } {\int\limits_0^\infty {\frac{1}{{2\pi {\sigma ^2}}}{e^{ - \frac{{{R^2}}}{{2{\sigma ^2}}}}}RdRd\theta } } .
1=−∞∫∞−∞∫∞2πσ21e−2σ2(X−μ)2+(Y−μ)2dXdY=0∫2π0∫∞2πσ21e−2σ2R2RdRdθ.
This leads to the distribution functions of
R
R
R and
θ
θ
θ are
P
(
R
≤
r
)
=
∫
0
2
π
d
θ
∫
0
r
1
2
π
σ
2
e
−
R
2
2
σ
2
R
d
R
=
1
−
e
−
r
2
2
σ
2
,
P
(
θ
≤
ϕ
)
=
=
∫
0
ϕ
d
θ
∫
0
∞
1
2
π
σ
2
e
−
R
2
2
σ
2
R
d
R
=
ϕ
2
π
.
P(R\leq r)=\int\limits_0^{2\pi } d\theta {\int\limits_0^r {\frac{1}{{2\pi {\sigma ^2}}}{e^{ - \frac{{{R^2}}}{{2{\sigma ^2}}}}}RdR } } =1-e^{-\frac{r^2}{2\sigma^2}}, \quad P(\theta \leq \phi)==\int\limits_0^{\phi } d\theta{\int\limits_0^\infty {\frac{1}{{2\pi {\sigma ^2}}}{e^{ - \frac{{{R^2}}}{{2{\sigma ^2}}}}}RdR } }=\frac{\phi}{2\pi}.
P(R≤r)=0∫2πdθ0∫r2πσ21e−2σ2R2RdR=1−e−2σ2r2,P(θ≤ϕ)==0∫ϕdθ0∫∞2πσ21e−2σ2R2RdR=2πϕ.
It is obvious that
θ
\theta
θ satisfies a uniform distribution on
[
0
,
2
π
]
[0, 2\pi]
[0,2π]. Let
F
R
(
r
)
=
1
−
e
−
r
2
2
σ
2
,
F_R(r)=1-e^{-\frac{r^2}{2\sigma^2}},
FR(r)=1−e−2σ2r2,
then its inverse function is
R
=
F
R
−
1
(
z
)
=
σ
−
2
l
n
(
1
−
z
)
.
R=F_R^{-1}(z)=\sigma\sqrt{-2ln(1-z)}.
R=FR−1(z)=σ−2ln(1−z).
When
z
z
z satisfies the uniform distribution on
[
0
,
1
]
[0,1]
[0,1], the distribution function of R is
F
R
(
r
)
F_R(r)
FR(r). So we can choose
U
1
U_1
U1 and
U
2
U_2
U2 which satisfy the uniform distribution, let
θ
=
2
π
U
1
,
R
=
σ
−
2
l
n
U
2
.
\theta = 2\pi U_1,\quad R=\sigma\sqrt{-2lnU_2}.
θ=2πU1,R=σ−2lnU2.
Bringing them to
X
X
X and
Y
Y
Y, then we can get that the initial
X
X
X or
Y
Y
Y satisfies the Gaussian distribution.