# Kalman filtering

卡尔曼滤波是以**最小均方误差**为估计的最佳准则，来寻求一套递推估计的算法，其基本思想是：采用信号与噪声的状态空间模型，利用前一时刻地估计值和现时刻的观测值来更新对状态变量的估计，求出现时刻的估计值。

> what? 最小均方误差不就是最小二乘吗？

![](https://2270971654-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M7DcNFhVrwIk3Tks_pB%2Fsync%2Fb9509b60ad8483b8958215ab88ec022631a99e40.png?generation=1589383944111896\&alt=media)

模型的预测+测量的反馈（权重由kalman gain决定） 形成新的高斯分布，所以可以作为下次迭代的起始点。

PRML Chapter13 [线性动态系统](https://mqshen.gitbooks.io/prml/content/Chapter13/linear_dynamical.html) 从概率的角度讲叙了kalman Filter 。或者参考 [卡尔曼滤波器学习笔记（一）](http://blog.csdn.net/lizilpl/article/details/45268471) 。

> 附截图\
> ![](https://2270971654-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M7DcNFhVrwIk3Tks_pB%2Fsync%2F067705019eac05b0561c7d271cb628105b57a4f2.png?generation=1589383943504947\&alt=media)

## 5条黄金公式的推导：####（参考[卡尔曼滤波 -- 从推导到应用(一)](http://blog.csdn.net/heyijia0327/article/details/17487467)，自己再推导了一遍）

最后盗一张算法流程图：\
![](https://2270971654-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M7DcNFhVrwIk3Tks_pB%2Fsync%2Fa7b643321dbb5809fbd1d0341f3af5d6c102f35c.png?generation=1589383943893949\&alt=media)

![](https://2270971654-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M7DcNFhVrwIk3Tks_pB%2Fsync%2F7648004b8e62596e62f9755d8008365c02060eec.png?generation=1589383943673864\&alt=media)

[概率机器人——贝叶斯滤波](https://zhuanlan.zhihu.com/p/31870182)

[通俗地解释卡尔曼滤波器（一）——从贝叶斯滤波器说起](https://zhuanlan.zhihu.com/p/31870182)

```
# http://www.cs.unc.edu/~welch/kalman/kalmanIntro.html
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt

#这里是假设A=1，H=1的情况

iterTimes = 50
sz = (iterTimes,)
x = -0.37727  # truth value (typo in example at top of p. 13 calls this z)
z = np.random.normal(x,0.1,size=sz)  # observations (normal about x, sigma=0.1)

Q = 1e-5  # process variance

xhat = np.zeros(sz)
P = np.zeros(sz)
xhatminus = np.zeros(sz)
Pminus=np.zeros(sz)
K=np.zeros(sz)

R = 0.1**2 # estimate of measurement variance, change to see effect

# intial guesses
xhat[0] = 0.0
P[0] = 1.0

for k in range(1,iterTimes):
    # time update
    xhatminus[k] = xhat[k-1]  #X(k|k-1) = AX(k-1|k-1) + BU(k) + W(k),A=1,BU(k) = 0
    Pminus[k] = P[k-1]+Q      #P(k|k-1) = AP(k-1|k-1)A' + Q(k) ,A=1

    # measurement update
    K[k] = Pminus[k]/( Pminus[k]+R ) #Kg(k)=P(k|k-1)H'/[HP(k|k-1)H' + R],H=1
    xhat[k] = xhatminus[k]+K[k]*(z[k]-xhatminus[k]) #X(k|k) = X(k|k-1) + Kg(k)[Z(k) - HX(k|k-1)], H=1
    P[k] = (1-K[k])*Pminus[k] #P(k|k) = (1 - Kg(k)H)P(k|k-1), H=1

plt.figure(num=1,figsize=(8,6))
plt.xlabel("Iteration",size=14)
plt.ylabel("Voltage",size=14)
plt.plot(z,'k+',label='noisy measurements')     #测量值
plt.plot(xhat,'b-',label='a posteri estimate')  #过滤后的值
plt.axhline(x,color='g',label='truth value')    #系统值
plt.legend()
plt.savefig('kf.png',format='png')

plt.figure()
valid_iter = range(1,iterTimes) # Pminus not valid at step 0
plt.plot(valid_iter,Pminus[valid_iter],label='a priori error estimate')
plt.xlabel('Iteration')
plt.ylabel('$(Voltage)^2$')
plt.setp(plt.gca(),'ylim',[0,.01])
plt.savefig('kf1.png',format='png')
```

时间序列有三类重要的统计诊断，filter滤波,predict预测,smooth平滑。未来时刻用Kalman算法我们称之为预测，对当下的结果用Kalman算法我们称之为滤波，对过去的结果用Kalman算法我们称之为平滑。

KF算法可以从贝叶斯，也可以从最小二乘推导出

[贝叶斯滤波](http://blog.csdn.net/sinat_31425585/article/details/52457813)

[细说贝叶斯滤波：Bayes filters](http://www.cnblogs.com/ycwang16/p/5995702.html)

[细说Kalman滤波：The Kalman Filter](http://www.cnblogs.com/ycwang16/p/5999034.html)

[卡尔曼滤波器（THE KALMAN FILTER）的数学原理](http://blog.csdn.net/xiaocainiaodeboke/article/details/50868759) 这个也是从bayes角度推导

[最小二乘估计与卡尔曼滤波公式推导](http://blog.csdn.net/yoouzx/article/details/53434809)

[Kalman Filter(卡尔曼滤波) 与Least Square(最小二乘法) 的比较](http://blog.csdn.net/qinruiyan/article/details/50793114)

[卡尔曼滤波方程组的深刻理解有哪些？-最小二乘法](https://www.zhihu.com/question/53815343/answer/711709568)

[贝叶斯视角下的卡尔曼滤波](https://zhuanlan.zhihu.com/p/69534520)

## 参考佳文

[Kalman滤波器从原理到实现](http://xiahouzuoxin.github.io/notes/html/Kalman%E6%BB%A4%E6%B3%A2%E5%99%A8%E4%BB%8E%E5%8E%9F%E7%90%86%E5%88%B0%E5%AE%9E%E7%8E%B0.html)

[卡尔曼滤波 -- 从推导到应用(一)](http://blog.csdn.net/heyijia0327/article/details/17487467)\
[卡尔曼滤波 -- 从推导到应用(二)](http://blog.csdn.net/heyijia0327/article/details/17667341)

[基于Kalman滤波器的进行物体的跟踪](http://blog.jasonding.top/2014/10/23/Machine%20Learning/%E3%80%90%E8%AE%A1%E7%AE%97%E6%9C%BA%E8%A7%86%E8%A7%89%E3%80%91%E5%9F%BA%E4%BA%8EKalman%E6%BB%A4%E6%B3%A2%E5%99%A8%E7%9A%84%E8%BF%9B%E8%A1%8C%E7%89%A9%E4%BD%93%E7%9A%84%E8%B7%9F%E8%B8%AA/)

徐亦达老师的视频课\
[Kalman and Bayesian Filters in Python](http://nbviewer.jupyter.org/github/rlabbe/Kalman-and-Bayesian-Filters-in-Python/blob/master/table_of_contents.ipynb)

[An Introduction to the Kalman Filter](http://www.cs.unc.edu/~welch/media/pdf/kalman_intro.pdf)

[如何通俗并尽可能详细解释卡尔曼滤波](https://www.zhihu.com/question/23971601)\
[Extended Kalman Filter、Particle filter在目标跟踪中的研究](http://mag.ieechina.com/OA/DArticle.aspx?type=view\&id=201202095)

[Kalman Filter](http://internetbuff.blog.163.com/blog/static/9425110720091501413932/)\
[How a Kalman filter works, in pictures](http://www.bzarg.com/p/how-a-kalman-filter-works-in-pictures/) 中文 [说说卡尔曼滤波](https://zhuanlan.zhihu.com/p/25598462)

<http://blog.csdn.net/baimafujinji/article/details/50650366>

[卡尔曼滤波(KF)与扩展卡尔曼滤波(EKF)的一种理解思路及相应推导](http://blog.csdn.net/qq_18163961/article/details/52505591)

[时间序列分析补充----结合ARMA的卡尔曼滤波算法](https://uqer.io/community/share/58221fb8228e5ba8f857197f)
