Ale*_*lex 5 python numpy prediction kalman-filter
我一直在努力实现卡尔曼滤波器来搜索二维数据集中的异常.非常类似于我在这里找到的优秀帖子.作为下一步,我想预测下一个值将落入的置信区间(例如,对于最低值和上限值的95%置信度).所以除了下面的行之外,我还想成为能够生成两条额外的线,这些线代表95%的置信度,即下一个值将高于最低值或低于最高值.
我假设我想要使用由卡尔曼滤波器生成的每个预测返回的不确定性协方差矩阵(P),但我不确定它是否正确.任何指导或参考如何做到这一点将不胜感激!
上面帖子中的代码随着时间的推移生成一组测量值,并使用卡尔曼滤波器来平滑结果.
import numpy as np
import matplotlib.pyplot as plt
def kalman_xy(x, P, measurement, R,
motion = np.matrix('0. 0. 0. 0.').T,
Q = np.matrix(np.eye(4))):
"""
Parameters:
x: initial state 4-tuple of location and velocity: (x0, x1, x0_dot, x1_dot)
P: initial uncertainty convariance matrix
measurement: observed position
R: measurement noise
motion: external motion added to state vector x
Q: motion noise (same shape as P)
"""
return kalman(x, P, measurement, R, motion, Q,
F = np.matrix('''
1. 0. 1. 0.;
0. 1. 0. 1.;
0. 0. 1. 0.;
0. 0. 0. 1.
'''),
H = np.matrix('''
1. 0. 0. 0.;
0. 1. 0. 0.'''))
def kalman(x, P, measurement, R, motion, Q, F, H):
'''
Parameters:
x: initial state
P: initial uncertainty convariance matrix
measurement: observed position (same shape as H*x)
R: measurement noise (same shape as H)
motion: external motion added to state vector x
Q: motion noise (same shape as P)
F: next state function: x_prime = F*x
H: measurement function: position = H*x
Return: the updated and predicted new values for (x, P)
See also http://en.wikipedia.org/wiki/Kalman_filter
This version of kalman can be applied to many different situations by
appropriately defining F and H
'''
# UPDATE x, P based on measurement m
# distance between measured and current position-belief
y = np.matrix(measurement).T - H * x
S = H * P * H.T + R # residual convariance
K = P * H.T * S.I # Kalman gain
x = x + K*y
I = np.matrix(np.eye(F.shape[0])) # identity matrix
P = (I - K*H)*P
# PREDICT x, P based on motion
x = F*x + motion
P = F*P*F.T + Q
return x, P
def demo_kalman_xy():
x = np.matrix('0. 0. 0. 0.').T
P = np.matrix(np.eye(4))*1000 # initial uncertainty
N = 20
true_x = np.linspace(0.0, 10.0, N)
true_y = true_x**2
observed_x = true_x + 0.05*np.random.random(N)*true_x
observed_y = true_y + 0.05*np.random.random(N)*true_y
plt.plot(observed_x, observed_y, 'ro')
result = []
R = 0.01**2
for meas in zip(observed_x, observed_y):
x, P = kalman_xy(x, P, meas, R)
result.append((x[:2]).tolist())
kalman_x, kalman_y = zip(*result)
plt.plot(kalman_x, kalman_y, 'g-')
plt.show()
demo_kalman_xy()
Run Code Online (Sandbox Code Playgroud)
1-sigma 区间的二维推广是置信椭圆,其特征为方程(x-mx).T P^{-1}.(x-mx)==1,其中x为参数 2D-Vector、mx2D 均值或椭圆中心以及P^{-1}逆协方差矩阵。有关如何绘制一个的信息,请参阅此答案。与西格玛区间一样,椭圆区域对应于真实值所在的固定概率。通过缩放因子n(缩放间隔长度或椭圆半径),可以获得更高的置信度。请注意,因子n在一维和二维中具有不同的概率:
|`n` | 1D-Intverval | 2D Ellipse |
==================================
1 | 68.27% | 39.35%
2 | 95.5% | 86.47%
3 | 99.73% | 98.89%
Run Code Online (Sandbox Code Playgroud)
在 2D 中计算这些值有点复杂,不幸的是我没有公开引用它。