估计卡尔曼滤波器置信区间

5
我一直在实现一个卡尔曼滤波器来搜索二维数据集中的异常值,这与我在这里找到的优秀帖子非常相似。作为下一步,我想预测置信区间(例如,地板和天花板值的95%置信度),以预测下一个值将落在哪里。因此,除了下面的线条之外,我还想能够生成另外两条线,表示下一个值将在地板以下或天花板以上的95%置信度。
我假设我将使用由卡尔曼滤波器生成的每个预测返回的不确定性协方差矩阵(P),但我不确定是否正确。任何指导或如何执行此操作的参考都将不胜感激! Python中的kalman 2d滤波器 上述帖子中的代码会随时间生成一组测量值,并使用卡尔曼滤波器平滑结果。
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()
3个回答

4

1-sigma interval的二维推广是置信度椭圆,其方程为(x-mx).T P^{-1}.(x-mx)==1,其中x是参数二维矢量,mx是二维均值或椭圆中心,P^{-1}是协方差矩阵的逆。参见此答案了解如何绘制。与sigma区间一样,椭圆面积对应于真值在其中的固定概率。通过缩放因子n(缩放区间长度或椭圆半径),可以达到更高的置信度。请注意,因子n在一维和二维中具有不同的概率:

|`n` | 1D-Intverval | 2D Ellipse |
==================================
  1  |  68.27%      |  39.35%    
  2  |  95.5%       |  86.47%
  3  |  99.73%      |  98.89%

计算这些值在二维上有点复杂,不幸的是我没有公开的参考资料。

1
如果你想预测下一个数值落在95%区间内,那么你需要预测区间而不是置信区间(http://en.wikipedia.org/wiki/Prediction_interval)。
对于二维(三维)数据,可以通过计算数据的协方差矩阵的特征值并调整半轴大小以考虑必要的预测概率来找到椭圆(椭球)的半轴。
请参见Prediction ellipse and prediction ellipsoid以获取Python代码以计算95%预测椭圆或椭球。这可能会帮助您计算数据的预测椭圆。

0

因为你的统计数据当然是从一个样本中得出的,所以人口统计数据大于2标准差的概率为0.5。 因此,如果您尚未应用2倍标准偏差的上限置信因子,则应考虑是否有很好的预测值,您希望下一次测量的概率低于0.95。该因子的大小将取决于用于导出0.5人口概率的样本大小。用于导出协方差矩阵的样本大小越小,推导出0.95概率的因子就越大,人口0.95统计数据小于经过因子化的样本统计数据。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接