Particle filters——粒子滤波

Markov Chain Monte Carlo 马尔科夫链蒙特卡罗

其发展于Bayesian Statistics 贝叶斯统计学

image.png

Monte Carlo

image.png

蒙特卡罗方法用的是多次采样,然后取均值。随机一点落在圆内 则标记为红色,若在圆外 则标记为黑色。最后红色点数量/全部点数量 得到圆的相对面积

Markov Chain

image.png

在Markov Chain中 每个节点都有不同的概率前往下一个节点,比如A点,有10%概率前往B,有90%概率前往C。但需要注意的是,不允许产生死循环。

经典的MC算法实现链的转换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import tensorflow
from keras.activations import softmax

n=4
P_Start=0
iters,samples,bp = 2000,200,[1,2,3,4,5,10,50,100,150,200]

m = tensorflow.constant([[(i+k)/n for i in range(n)] for k in range (n)], dtype =tensorflow.float32)
P = softmax(tensorflow.random.normal((n,n),dtype = tensorflow.float32)+m , axis = -1)
P = tensorflow.cumsum(P,axis=-1)

summary = [{(i ,P_Start): 0 for i in range(n)} for _ in bp]

for i in range(iters):
S = P_Start

PS = tensorflow.random.uniform((samples,))
PS_exp =tensorflow.reshape(PS,shape=(-1,1,1))
P_exp = tensorflow.expand_dims(P,axis=0)
res = P_exp-PS_exp
state_table = tensorflow.argmax(res>0,axis=-1)

count,idx=0,0
for t in range(samples):
count += 1
S= state_table[t,S]
if count in bp:
summary[idx][(int(S),int(P_Start))] +=1
idx +=1

print("\n".join([str(p)+ " "+ str(s) for p,s in zip(bp,summary)]))

--------------------------------------------------------------------------------
1 {(0, 0): 424, (1, 0): 349, (2, 0): 762, (3, 0): 465}
2 {(0, 0): 615, (1, 0): 451, (2, 0): 317, (3, 0): 617}
3 {(0, 0): 637, (1, 0): 464, (2, 0): 390, (3, 0): 509}
4 {(0, 0): 625, (1, 0): 441, (2, 0): 384, (3, 0): 550}
5 {(0, 0): 609, (1, 0): 456, (2, 0): 411, (3, 0): 524}
10 {(0, 0): 641, (1, 0): 467, (2, 0): 386, (3, 0): 506}
50 {(0, 0): 589, (1, 0): 478, (2, 0): 382, (3, 0): 551}
100 {(0, 0): 610, (1, 0): 465, (2, 0): 396, (3, 0): 529}
150 {(0, 0): 572, (1, 0): 468, (2, 0): 416, (3, 0): 544}
200 {(0, 0): 639, (1, 0): 437, (2, 0): 383, (3, 0): 541}
进程已结束,退出代码0
(x,y):n 表示为起点为y,终点为x的步调为n次

最终的结果说明从0->3在步数越来越多的情况下区域稳定,抖动的趋势变缓

image.png

为了得到我们想要的Markov chain ,可以用Metropolis-Hastings的算法

image.png

image.png