知乎上已经有很多的学习笔记,但读完后总有一种这东西不是我的我理解不了的感觉,所以想试着写一篇文章来加深一下自己的理解,也记录下学习中的盲点。

非常推荐大家去Github看一个项目:

https://github.com/rlabbe/filterpy​github.com

#下面的代码也是完全基于上述作者的库函数完成的,所以需要先去Github下载库函数安装,或者直接使用
pip install filterpy
#但是作者忘记把离散的白噪声函数放进库里,那段函数可以在书里找到,或者改天我可以放在网盘里分享出来
#不影响下面代码展示

这里除了有卡尔曼滤波外还有各种贝叶斯滤波的python实现,作者也写了一本书开源在Github详细介绍了卡尔曼,本文就算是读书笔记吧。下面是作者发布的教材:

Jupyter Notebook Viewer​nbviewer.jupyter.org

太高估自己的效率,听了128次大田后生仔+n首其他终于赶在没有眼瞎前停工了。

下面代码基本是从原作者书里粘来的,我只改了某些错误和可视化以及一些小的细节(动图做的还不够好)作者在书中试图用追踪狗在走廊中移动的模型来解释离散贝叶斯滤波,由先验概率到后验概率再到引入反馈器这一思路详细的介绍了贝叶斯滤波器并且实现了python的编程。同时作者一直在向我们强调学会数学编程的重要性,本人理解的不够深刻,大家可以去翻阅原作者的书去看。

为什么我一直想实现贝叶斯滤波或者卡尔曼滤波,因为它是一种动态滤波器,它可以用来建立动态的反演模型,这和我以前接触的反演很不一样,我曾经做过利用光学遥感建立泥沙反演模型和利用两层BP神经网络对地震波建立反演模型找地下矿层的两个小东西,以上两个反演模型都是静态的过程,而卡尔曼滤波最反智的则是在滤波中不断更新模型参数(加入了后馈过程也就是K卡尔曼增益矩阵),这让我困惑了特别久。

如果大家也对这种滤波器感兴趣建议先学些统计知识和最小二乘法,卡尔曼滤波和最小二乘法之间有着很紧密地联系,并且最小二乘法方程可以很好的帮助你理解最小均方误差,这是实现卡尔曼滤波的重要假设。

下面也会放一段简单马尔可夫链的python小程序,如果你和我一样用Sublime ,多试几次Ctrl + B,这是基于随机数做的,但是你会发现在多次迭代后概率会收敛到一个定值,还记得统计课上讲的大数定律嘛,在样本空间足够大时,依概率收敛到某个数,大约就是这个意思。所以在随机天气生成器中需要用蒙托卡罗思想来添加扰动来破坏这种收敛性?,我上一篇文章中也写道过,但是我想错了,并不是对转移概率矩阵添加扰动,而是对初始状态添加扰动(最近听了几个MCMC方法的读书汇报,发现了这个秘密)

有关于今天统计课上讲到的气候序列均一化我也想说点儿给某位不来上课的同学,我认为卡尔曼滤波器可以很好的应用到均一化过程中,具体方法我不说,啦啦啦。

#离散贝叶斯滤波器的实现展示
#The Kalman filter belongs to a family of filters called Bayesian filters.
#NMEFC YuHao
import numpy as np
import matplotlib.pyplot as plt
from filterpy.discrete_bayes import normalize
from filterpy.discrete_bayes import predict
from filterpy.discrete_bayes import updatex = range(10)
hallway = np.array([1,1,0,0,0,0,0,0,1,0])
#Tracking a Dogdef update_belief(hall,belief,z,correct_scale):for i ,val in enumerate(hall):if val == z:belief[i] *= correct_scale
belief = np.array([0.1]*10)
reading = 1
update_belief(hallway,belief,z = reading,correct_scale = 3.)
print("belief:",belief)
print("sum = ",sum(belief))
plt.figure()
plt.bar(x,belief)
plt.title("belief")
plt.show()#归一化
homogeneity_array = belief/sum(belief)
print(homogeneity_array)
print(hallway == 1 )
#计算后验概率
def scale_update(hall ,belief, z, z_prob):scale = z_prob / (1. - z_prob)belief[hall == z] *= scalenormalize(belief)
belief = np.array([0.1]*10)
scale_update(hallway ,belief ,z=1,z_prob = .75)
print('sum =',sum(belief))
print("probability of door =",belief[0])
print("probability of wall =", belief[2])
plt.figure()
plt.bar(x,belief)
plt.title("belief")
plt.show()def scale_update(hall,belief,z,z_prob):scale = z_prob / (1. - z_prob)likelihood = np.ones(len(hall))likelihood[hall == z] *= scalereturn normalize(likelihood * belief)
def lh_hallway(hall, z, z_prob):#compute likelihood that a measurement matches#positions in the hallway.try:scale = z_prob / (1. - z_prob)except ZeroDivisionError:scale = 1e8likelihood = np.ones(len(hall))likelihood[hall==z] *= scalereturn likelihoodbelief = np.array([0.1] * 10)
likelihood = lh_hallway(hallway, z=1, z_prob=.75)
print(normalize(likelihood * belief))
def perfect_predict(belief,move):n = len(belief)result = np.zeros(n)for i in range(n):result[i] = belief[(i-move) % n]return result
belief = np.array([.35,.1,.2,.3,0,0,0,0,0,.05])belief = perfect_predict(belief,1)
plt.bar(x,belief)
plt.title("belief")
plt.show()
#添加噪声def predict_move(belief, move, p_under, p_correct, p_over):n = len(belief)prior = np.zeros(n)for i in range(n):prior[i] = (belief[(i-move) % n]   * p_correct +belief[(i-move-1) % n] * p_over +belief[(i-move+1) % n] * p_under)      return priorbelief = [0., 0., 0., 1., 0., 0., 0., 0., 0., 0.]
prior = predict_move(belief, 2, .1, .8, .1)
print(prior)
fig = plt.figure()
ax1 = fig.add_subplot(1,2,1)
ax2 = fig.add_subplot(1,2,2)
ax1.bar(x,belief)
ax2.bar(x,prior)
ax1.set_title("belief")
ax2.set_title("prior")
ax1.set_xticks(x)
ax2.set_xticks(x)
plt.show()
belief = [0, 0, .4, .6, 0, 0, 0, 0, 0, 0]
prior = predict_move(belief, 2, .1, .8, .1)
fig = plt.figure()
ax1 = fig.add_subplot(1,2,1)
ax2 = fig.add_subplot(1,2,2)
ax1.bar(x,belief)
ax2.bar(x,prior)
ax1.set_title("belief")
ax2.set_title("prior")
ax1.set_xticks(x)
ax2.set_xticks(x)
plt.show()belief = np.array([1.0,0,0,0,0,0,0,0,0,0])
beliefs = []
for i in range(100):belief = predict_move(belief,1,.1,.8,.1)beliefs.append(belief)
print("Final Belief:",belief)
print(len(beliefs))
print(beliefs[1])
for i in range(len(beliefs)):plt.cla()plt.bar(x,beliefs[i])plt.xticks(x)plt.pause(0.05)plt.show()
"""
#卷积泛化
#卷积算法的python描述,但是时间复杂度太高,pass
def predict_move_convolution(pdf ,offset,kernel):N = len(pdf)kN = len(kernel)width = int((kN - 1)/2)prior = np.zeros(N)for i in range(N):for k in range(kN):index = (i + (width - k)-offset)%Nprior[i] += pdf[index] * kernel[k]return prior
"""belief = [.05, .05, .05, .05, .55, .05, .05, .05, .05, .05]
prior = predict(belief,offset = 1,kernel = [.1,.8,.1])
fig = plt.figure()
ax1 = fig.add_subplot(1,2,1)
ax2 = fig.add_subplot(1,2,2)
ax1.bar(x,belief)
ax2.bar(x,prior)
ax1.set_title("belief")
ax2.set_title("prior")
ax1.set_xticks(x)
ax2.set_xticks(x)
plt.show()
prior = predict(belief, offset=3, kernel=[.05, .05, .6, .2, .1])
#make sure that it shifts the positions correctly for movements greater than one and for asymmetric kernels
#making a prediction of where the dog is moving, and convolving the probabilities to get the priorfig = plt.figure()
ax1 = fig.add_subplot(1,2,1)
ax2 = fig.add_subplot(1,2,2)
ax1.bar(x,belief)
ax2.bar(x,prior)
ax1.set_title("belief")
ax2.set_title("prior")
ax1.set_xticks(x)
ax2.set_xticks(x)
plt.show()
"""
#Integrating Measurements and Movement Updates
"""
"""
请仔细阅读下一段话
Let's think about this intuitively.Consider a simple case - you are tracking a dog while he sits still.During each prediction you predict he doesn't move.Your filter quickly converges on an accurate estimate of his position.Then the microwave in the kitchen turns on, and he goes streaking off.You don't know this, so at the next prediction you predict he is in the same spot.But the measurements tell a different story.As you incorporate the measurements your belief will be smeared along the hallway, leading towards the kitchen.On every epoch (cycle) your belief that he is sitting still will get smaller,and your belief that he is inbound towards the kitchen at a startling rate of speed increases.
最精彩的部分来了:反馈器"""
tank1 = []
tank2 = []hallway = np.array([1, 1, 0, 0, 0, 0, 0, 0, 1, 0])
prior = np.array([.1] * 10)
likelihood = lh_hallway(hallway, z=1, z_prob=.75)
posterior = normalize(likelihood * prior)
tank2.append(posterior)
tank1.append(prior)
fig = plt.figure()
ax1 = fig.add_subplot(1,2,1)
ax2 = fig.add_subplot(1,2,2)
ax1.bar(x,prior)
ax2.bar(x,posterior)
ax1.set_title("prior")
ax2.set_title("posterior")
ax1.set_xticks(x)
ax1.set_ylim(0,0.5)
ax2.set_ylim(0,0.5)
ax2.set_xticks(x)
plt.show()#After the first update we have assigned a high probability to each door position, and a low probability to each wall position.
prior = np.array([.1] * 10)
kernel = (.1, .8, .1)
prior = predict(posterior, 1, kernel)
tank1.append(posterior)
tank2.append(prior)
fig = plt.figure()
ax1 = fig.add_subplot(1,2,1)
ax2 = fig.add_subplot(1,2,2)
ax1.bar(x,prior)
ax2.bar(x,posterior)
ax1.set_title("posterior")
ax2.set_title("prior")
ax1.set_xticks(x)
ax1.set_ylim(0,0.5)
ax2.set_ylim(0,0.5)
ax2.set_xticks(x)
plt.show()
#The predict step shifted these probabilities to the right, smearing them about a bit. Now let's look at what happens at the next sense.likelihood = lh_hallway(hallway, z=1, z_prob=.75)
posterior = update(likelihood, prior)
tank1.append(prior)
tank2.append(posterior)
fig = plt.figure()
ax1 = fig.add_subplot(1,2,1)
ax2 = fig.add_subplot(1,2,2)
ax1.bar(x,prior)
ax2.bar(x,posterior)
ax1.set_title("prior")
ax2.set_title("posterior")
ax1.set_xticks(x)
ax1.set_ylim(0,0.5)
ax2.set_ylim(0,0.5)
ax2.set_xticks(x)
plt.show()
#Notice the tall bar at position 1. This corresponds with the (correct) case of starting at position 0, sensing a door, shifting 1 to the right, and sensing another door. No other positions make this set of observations as likely. Now we will add an update and then sense the wall.
prior = predict(posterior, 1, kernel)
likelihood = lh_hallway(hallway, z=0, z_prob=.75)
posterior = update(likelihood, prior)
tank1.append(prior)
tank2.append(posterior)
fig = plt.figure()
ax1 = fig.add_subplot(1,2,1)
ax2 = fig.add_subplot(1,2,2)
ax1.bar(x,prior)
ax2.bar(x,posterior)
ax1.set_title("prior")
ax2.set_title("posterior")
ax1.set_xticks(x)
ax1.set_ylim(0,0.5)
ax2.set_ylim(0,0.5)
ax2.set_xticks(x)
plt.show()
#This is exciting! We have a very prominent bar at position 2 with a value of around 35%. It is over twice the value of any other bar in the plot, and is about 4% larger than our last plot, where the tallest bar was around 31%. Let's see one more cycle.
prior = predict(posterior, 1, kernel)
likelihood = lh_hallway(hallway, z=0, z_prob=.75)
posterior = update(likelihood, prior)
prior = predict(posterior, 1, kernel)
likelihood = lh_hallway(hallway, z=0, z_prob=.75)
posterior = update(likelihood, prior)
tank1.append(prior)
tank2.append(posterior)
fig = plt.figure()
ax1 = fig.add_subplot(1,2,1)
ax2 = fig.add_subplot(1,2,2)
ax1.bar(x,prior)
ax2.bar(x,posterior)
ax1.set_title("prior")
ax2.set_title("posterior")
ax1.set_xticks(x)
ax1.set_ylim(0,0.5)
ax2.set_ylim(0,0.5)
ax2.set_xticks(x)
plt.show()
print(tank1)
print(tank2)
for i in range(len(tank1)):plt.cla()plt.subplot(1,2,1)plt.bar(x,tank1[i])plt.xticks(x)plt.ylim(0,0.5)plt.subplot(1,2,2)plt.bar(x,tank2[i])plt.xticks(x)plt.ylim(0,0.5)plt.pause(0.5)plt.show()
#离散贝叶斯算法

备注添加的可能不是很准确,有问题大家可以留言。

import numpy as np
import random as rmstate = ["sleep","Icecream","Run"]
transitionName = [["SS","SR","SI"],["RS","RR","RI"],["IS","IR","II"]]
transitionMatrix = [[0.2,0.6,0.2],[0.1,0.6,0.3],[0.2,0.7,0.1]]
if sum(transitionMatrix[0]) + sum(transitionMatrix[1]) + sum(transitionMatrix[2]) !=3:print("Wrong")
else:print("All is gonna be okay,you should move on!!;)")def activity_forecast(days):activityToday = "Sleep"#print("Start_state:" + activityToday)activityList = [activityToday]i = 0prob = 1while i != days:if activityToday == "Sleep":change = np.random.choice(transitionName[0],replace = True,p =transitionMatrix[0])if change == "SS":prob = prob * 0.2activityList.append("Sleep")passelif change == "SR":prob = prob * 0.6activityToday = "Sleep"activityList.append("Sleep")else:prob = prob *0.2activityToday = "Icecream"activityList.append("Icecream")elif activityToday == "Run":change = np.random.choice(transitionName[1],replace = True ,p = transitionMatrix[1])if change == "RR":prob = prob * 0.5activityList.append("Run")passelif change == "RS":prob = prob *0.2activityToday = "Sleep"activityList.append("Sleep")else:prob = prob * 0.3activityToday = "Icecream"activityList.append("Icecream")elif activityToday == "Icecream":change = np.random.choice(transitionName[2],replace = True,p = transitionMatrix[2])if change == "II":prob = prob *0.1activityList.append("Icecream")passelif change == "IS":prob = prob * 0.2activityToday = "Sleep"activityList.append("Sleep")else:prob = prob * 0.7activityToday = "Run"activityList.append("Run")i += 1return activityList
list_activity = []
count = 0
for iterations in range(1,10000):list_activity.append(activity_forecast(2))
for smaller_list in list_activity:if(smaller_list[2] == "Sleep"):count += 1
percentage = (count/10000) * 100print("the probability of starting at state:'Sleep' and ending at state:'Sleep' = " + str(percentage) + "%")#马尔可夫链展示

能看到这儿说明你对我的文章很有兴趣,那么有任何问题可以通过任何方式问我。

日常吐槽:

最近效率真是低的可怕,可我睡不着啊。。。

休息不好吃又吃不下要掉体重了,为了回家不让老妈发现(不然要被疯狂投食)牛奶得换全脂来保证热量摄入了。

来吧,都看到这儿了陪我听首歌啊:

キミがいれば(世纪末ヴァージョン)​music.163.com

柯姓男孩永不认输

python 排序统计滤波器_马尔可夫链+贝叶斯滤波器的Python展示相关推荐

  1. 朴素贝叶斯python代码_朴素贝叶斯模型及python实现

    1 朴素贝叶斯模型 朴素贝叶斯法是基于贝叶斯定理.特征条件独立假设的分类方法.在预测时,对输入x,找出对应后验概率最大的 y 作为预测. NB模型: 输入: 先验概率分布:P(Y=ck),k=1,2, ...

  2. python实现排列组合公式算法_朴素贝叶斯算法的Python实现

    朴素贝叶斯分类算法被广泛应用于文本分类场景中.包含垃圾邮件.互联网新闻等分类任务,属于有监督学习算法.它独立考量每一维度特征被分类的条件概率,然后综合这些概率对其所在的特征向量做出分类预测,即&quo ...

  3. python词频统计西游记_自学了一段时间Python,闲来无事爬了本《西游记》给大家分享下...

    [Python] 纯文本查看 复制代码import requests import os,time from lxml import etree from fake_useragent import ...

  4. 李航《统计学习方法》朴素贝叶斯的python实现

    前言 朴素贝叶斯算法是通过贝叶斯公式及条件的独立性建立的一个生成模型,具体内容及原理参见李航<统计学习方法>,下面对朴素贝叶斯算法通过python 实现.因为本人是数学专业非科班,所以很多 ...

  5. 【滤波】离散贝叶斯滤波器

    本文参考自rlabbe/Kalman-and-Bayesian-Filters-in-Python的第2章节02-Discrete-Bayes(离散贝叶斯). %matplotlib inline # ...

  6. [翻译][学习卡尔曼与贝叶斯滤波器][前言]

    [翻译][学习卡尔曼与贝叶斯滤波器--基于Python实践][前言] 开源飞控交流:562983648 项目链接:Github 注:这是一个互动式教程,博客只能显示静态页面.需要得到完整功能请下载项目 ...

  7. 统计学习方法03—朴素贝叶斯算法

    目录 1.朴素贝叶斯的基本原理 2. 贝叶斯算法实现 2.1 数据集的准备与处理 2.2 GaussianNB 高斯朴素贝叶斯 2.2.1 @staticmethod静态方法 2.2.2 几种概率统计 ...

  8. 朴素贝叶斯代码实现python

    P(B)称为"先验概率",即在A事件发生之前,对B事件概率的一个判断. P(B|A)称为"后验概率",即在A事件发生之后,对B事件概率的重新评估. P(A|B) ...

  9. 贝叶斯系列——贝叶斯定理、贝叶斯决策、朴素贝叶斯、贝叶斯网络、贝叶斯滤波器

    写一个贝叶斯系列,整理一下贝叶斯定理.贝叶斯决策.朴素贝叶斯.贝叶斯网络.贝叶斯滤波器 众所周知,概率分为两个学派,频率学派(大数定理)和贝叶斯学派(贝叶斯公式),我们不去判断哪个学派说的更对,但是在 ...

最新文章

  1. 双12众商超沦陷,你是否习惯了扮演观众?
  2. java类名变量_java类名操作变量方法
  3. java技术栈有哪些_Java程序员必备的21个核心技术,你都掌握了哪些?
  4. UOJ #586. 旅行问题
  5. tomcat源码阅读之StandardHost和StandardEngine
  6. Java DB中的Java存储过程
  7. Mysql中SQL语句不使用索引的情况
  8. 2.1 linux C 进程与多线程入门--(1)进程和程序的区别
  9. 三大开源生信基础教程和视频课程
  10. 【AD】Altium designer画pcb时出现Unknown Pin 和Failed to add class
  11. IT销售素质 --善于学习
  12. Android 通过开源框架AsyncHttpClient进行get和post请求
  13. java环境变量配置验证_怎么验证Java环境变量配置成功
  14. js html编码和解码,JavaScript字符集编码与解码
  15. 瞬时功率与有功功率计算公式
  16. 微信语音转文字的体验报告
  17. substance painter贴图导入UE4显示效果不一样的解决方法
  18. 开源h5游戏 宠物_释放:宠物和动物的开源技术
  19. DeltaOne介绍
  20. 全球医药研发支出及处方药市场发展前景分析:预计到2026年全球处方药销售额超过1.4万亿美元[图]

热门文章

  1. Android 第三课 构建简单的用户界面
  2. http超文本传输协议的http头部分析
  3. go导入私有仓库中的包配置方法
  4. Spark广播变量使用示例
  5. Go gin获取post请求数据
  6. 【收藏】CentOS 7 安装NFS
  7. 启动namenode报错:Journal Storage Directory /var/bigdata/hadoop/full/dfs/jn/dmgeo not formatted
  8. Python Django 全局上下文代码示例
  9. 【视频】vue表单提交
  10. Linux netstat查看网络连接状态