Skip to content

Commit a186613

Browse files
committed
exa1901 exa1902
1 parent 9f79752 commit a186613

File tree

7 files changed

+78
-3
lines changed

7 files changed

+78
-3
lines changed

CH19/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,27 @@ $p(x)=\frac{1}{\sqrt{2\pi\sigma^2}}e^{-\frac{(x-\mu)^2}{2\sigma^2}}$
7373
#### 数学期望估计
7474

7575
#### 积分计算
76+
**例19.1**
77+
$$
78+
\int_0^1e^{-{x^2}/{2}}\mathrm{d}x
79+
$$
80+
![fig_e1901](assets/fig_e1901.png)
81+
实际上这个例子,数据点比较少的时候积分效果不是特别好。十个点,均匀分布出来的也不是特别均匀。
82+
注意,按照**均匀分布**抽取,和**整个区间均分10等分**是不一样的。
83+
84+
**例19.2**
85+
10个样本
86+
![fig_e1902_1](assets/fig_e1902_1.png)
87+
50个样本
88+
![fig_e1902](assets/fig_e1902.png)
89+
90+
这两个例子,**注意**一下$f(x)$和$p(x)$的定义。
91+
7692

7793
### 马尔可夫链
94+
7895
### 马尔可夫链蒙特卡罗法
96+
7997
### Metropolis-Hastings算法
8098
### Gibbs Sampling
8199

CH19/assets/fig_e1901.png

14.9 KB
Loading

CH19/assets/fig_e1902.png

15.4 KB
Loading

CH19/assets/fig_e1902_1.png

14.6 KB
Loading

CH19/fig_1901.png

-43.8 KB
Binary file not shown.

CH19/mcmc.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#! /usr/bin/env python
2-
#!-*- coding=utf-8 -*-
2+
3+
# -*- coding=utf-8 -*-
34
# Project: Lihang
45
# Filename: mcmc
56
# Date: 6/10/19

CH19/unit_test.py

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,66 @@
1616

1717
class TestMCMCMethod(unittest.TestCase):
1818
def test_exa_1901(self):
19-
pass
19+
def f(x):
20+
return np.exp(-x**2/2)
21+
22+
def p(x):
23+
return 1
24+
25+
x = np.random.uniform(0, 1, 10)
26+
y = f(x)
27+
x_raw = np.arange(0, 1, 0.01)
28+
y_raw = f(x_raw)
29+
print(x.shape, y.shape)
30+
data = np.concatenate((x_raw[:, np.newaxis], y_raw[:, np.newaxis]),
31+
axis=1)
32+
print(data)
33+
# data = data[data[:, 0].argsort()]
34+
rst = np.mean(y)
35+
36+
plt.rc('text', usetex=True)
37+
plt.rc('font', family='serif')
38+
39+
plt.figure(figsize=(5, 5))
40+
plt.scatter(x, y, marker='*')
41+
plt.plot(data[:, 0], data[:, 1], alpha=0.3)
42+
bbox = dict(boxstyle="round", fc="0.8")
43+
comment = r"$\int_0^1e^{-{x^2}/{2}}\mathrm{d}x=$"+str(round(rst, 2))
44+
plt.annotate(comment, (0.2, 0.8), bbox=bbox)
45+
plt.ylabel(r'$\exp\left(-\frac{1}{2}x^2\right)$')
46+
plt.ylim(-0.02, 1.02)
47+
plt.xlim(-0.02, 1.02)
48+
plt.show()
2049

2150
def test_exa_1902(self):
22-
pass
51+
def p(x):
52+
return np.sqrt(2*np.pi)*np.exp(-x**2/2)
53+
54+
def f(x):
55+
return x
56+
57+
x = np.random.normal(0, 1, 10)
58+
y = f(x)
59+
x_raw = np.arange(-2, 2, 0.1)
60+
y_raw = f(x_raw)
61+
print(x.shape, y.shape)
62+
data = np.concatenate((x_raw[:, np.newaxis], y_raw[:, np.newaxis]),
63+
axis=1)
64+
print(data)
65+
# data = data[data[:, 0].argsort()]
66+
rst = np.mean(y)
67+
68+
plt.rc('text', usetex=True)
69+
plt.rc('font', family='serif')
70+
71+
plt.figure(figsize=(5, 5))
72+
plt.scatter(x, y, marker='*', alpha=0.3)
73+
plt.plot(data[:, 0], data[:, 1], alpha=0.3)
74+
bbox = dict(boxstyle="round", fc="0.8")
75+
comment = r"$\int_{-\infty}^{\infty}x\frac{1}{\sqrt{2\pi}}e^{-{x^2}/{2}}\mathrm{d}x=$"+str(round(rst, 2))
76+
plt.annotate(comment, (-2, 2), bbox=bbox)
77+
78+
plt.show()
2379

2480
def test_exa_1903(self):
2581
pass

0 commit comments

Comments
 (0)