卡尔曼滤波器预测未来的前一步

Kalman filter to predict previous step from future

我是卡尔曼滤波器的新手,正在尝试使用它来预测缺失值以及从 GPS 数据(纬度和经度)中获得平滑的观察结果。

我正在使用 pykalman,我的代码块如下所示:

data = data[['Lat', 'Lon']]
measurements = np.asarray(data, dtype='float')
measurements_masked = np.ma.masked_invalid(measurements)

# initial state of the form  [x0, x0_dot, x1, x1_dot]
initial_state_mean = [
    measurements[0, 0],
    0,
    measurements[0, 1],
    0
]

initial_state_covariance = [[ 10, 0, 0, 0], 
                            [  0, 1, 0, 0],
                            [  0, 0, 1, 0],
                            [  0, 0, 0, 1]]

# transition matrix to estimate new position given old position
transition_matrix = [
    [1, 1, 0, 0],
    [0, 1, 0, 0],
    [0, 0, 1, 1],
    [0, 0, 0, 1]
]

observation_matrix = [
    [1, 0, 0, 0],
    [0, 0, 1, 0]
]

kf = KalmanFilter(
    transition_matrices=transition_matrix,
    observation_matrices=observation_matrix,
    initial_state_mean=initial_state_mean,
)

filtered_state_means = np.zeros((len(measurements), 4))
filtered_state_covariances = np.zeros((len(measurements), 4, 4))

for i in range(len(measurements)):
    if i == 0:
        filtered_state_means[i] = initial_state_mean
        filtered_state_covariances[i] = initial_state_covariance
    else:
        filtered_state_means[i], filtered_state_covariances[i] = (
        kf.filter_update(
            filtered_state_means[i-1],
            filtered_state_covariances[i-1],
            observation = measurements_masked[i])
        )

其中数据是一个 pandas 数据框,从中提取纬度和经度。

这个逻辑对吗?此外,我想做的是采用更接近缺失观察的观察来预测缺失值。例如,如果在 10 个样本的数组中,第 5、6 和 7 个观察值缺失,则使用第 4 个样本预测第 5 个,使用第 8 个样本预测第 7 个,并通过取第 5 个和第 7 个样本的平均值预测第 6 个更有意义.

这种方法有意义吗?如果是,如何使用 pykalman 来实现?如果不是,在数组中缺少大量连续值的情况下,如何更准确地预测缺失值?

我认为卡尔曼滤波器非常适合您的需求。下面是一些虚拟数据的示例,其中我从过滤器中屏蔽(隐藏)了一些 samples/measurements 。如您所见,KF 在重建中间缺失的 3 点方面做得很好。 KF 将处理这样一个事实,即接近特定时间戳的观察结果与估计该时间戳最相关(通过假设的动力学)。

这有点乐观,因为输入数据完全符合 KF 中的假设(物体以恒定速度运动)。请注意,当速度实际发生变化时,KF 也应该能正常工作。我之前在 pykalman 库上发布了一个更长的答案:,这可能有助于理解 KF 的工作原理。

import numpy as np
import matplotlib.pyplot as plt
from pykalman import KalmanFilter

# Some dummy values, assume we're heading in straightline
# at constant speed
lat_ideal = np.array(range(10))
lon_ideal = np.array(lat_ideal*3.5 + 10)

lat = lat_ideal + np.random.uniform(-0.5, 0.5, 10)
lon = lon_ideal + np.random.uniform(-0.5, 0.5, 10)

# Assing some indexes as missing
measurementMissingIdx = [False, False, False, False, True, True, True, False, False, False]

# Create the starte measurement matrix and mark some of the time-steps
# (rows) as missing (masked)
measurements = np.ma.asarray([lat, lon]).transpose()
measurements[measurementMissingIdx] = np.ma.masked

# Kalman filter settings:
# state vector is [lat, lat_dot, lon, lon_dot]
Transition_Matrix=[[1,1,0,0],[0,1,0,0],[0,0,1,1],[0,0,0,1]]
Observation_Matrix=[[1,0,0,0],[0,0,1,0]]

initial_state_mean = [measurements[0, 0], 0,
                      measurements[0, 1], 0]

kf=KalmanFilter(transition_matrices=Transition_Matrix,
            observation_matrices =Observation_Matrix,
            em_vars=['initial_state_covariance', 'initial_state_mean'
                     'transition_covariance', 'observation_covariance'])

kf.em(measurements, n_iter=5)

# Increase observation co-variance
kf.observation_covariance = kf.observation_covariance*10

(smoothed_state_means, smoothed_state_covariances) = kf.smooth(measurements)

plt.plot(lat_ideal,lon_ideal,'sb', label='ideal values', markerfacecolor='none')
plt.plot(measurements[:,0],measurements[:,1],'og',label='input measurements', markerfacecolor='none')
plt.plot(smoothed_state_means[:,0],smoothed_state_means[:,2],'xr',label='kalman output')

plt.xlabel("Latitude")
plt.ylabel("Longitude")
legend = plt.legend(loc=2)
plt.title("Constant Velocity Kalman Filter")
plt.show()

生成下图: