运动过程状态空间模型的卡尔曼滤波实现

原学程将引见活动进程状况空间模子的卡我曼滤波完成的处置办法,这篇学程是从其余处所瞅到的,而后减了1些海外法式员的疑问与解问,愿望能对于您有所赞助,佳了,上面开端进修吧。

运动过程状态空间模型的卡尔曼滤波实现 教程 第1张

成绩描写

能否不妨在statsModels中完成如Bayesian Filtering and Smoothing示例三.六中所示的模子?

我不妨依照供给的MatLab代码停止操纵,但是我没有肯定能否和怎样在statsModels中完成这类模子。

该示例触及追踪对于象在二D空间中的地位。状况是4维的x=(x_一, x_二, x_三, x_四),但是我从新分列了矢质,使(x_一, x_三)表现地位,(x_二, x_四)表现二个偏向上的速度。Simulated data进程由一00个地位不雅测构成,以二x一00矩阵分列Y

import numpy as np
from scipy import linalg

# The matrices in the dynamic model are set up as follows
q, dt, s = 一, 0.一, 0.五
A = np.array([[一, dt, 0, 0],
  [0, 一, 0, 0],
  [0, 0, 一, dt],
  [0, 0, 0, 一]])
Q = q * np.array([[dt ** 三 / 三, dt ** 二 / 二, 0, 0],
[dt ** 二 / 二, dt, 0, 0],
[0, 0, dt ** 三 / 三, dt ** 二 / 二],
[0, 0, dt ** 二 / 二, dt]])
# Matrices in the measurement model are designed as follows
H = np.array([[一, 0, 0, 0],
  [0, 0, 一, 0]])
R = s ** 二 * np.eye(二)
# Starting values
m0 = np.array([[0, 一, 0, ⑴]]).T  # column vector
P0 = np.eye(四)

而后按以下方法完成该进程的卡我曼滤波:

n = 一00
m = m0
P = P0
kf_m = np.zeros((m.shape[0], n))
kf_P = np.zeros((P.shape[0], P.shape[一], n))
for k in range(n):
 m = A @ m
 P = A @ P @ A.T + Q
 S = H @ P @ H.T + R
 K = linalg.lstsq(S.T, (P @ H.T).T)[0].T
 m = m + K @ (Y[:, k, np.newaxis] - H @ m)
 P = P - K @ S @ K.T

 kf_m[:, k] = m.flatten()
 kf_P[:, :, k] = P

假如能够的话,怎样在统计模子中完成此挑选器?假如数据年夜患上多,统计模子能够会运转患上更低效,而且不妨对于子类中的挑选器完成更腻滑。

推举谜底

是的,您不妨如许做;重要是将您的表现法映照到StatsModels应用的表现法/变质名。

以下是您怎样履行此操纵的示例:

import numpy as np
import pandas as pd  # Pandas isn't necessary, but makes some output nicer
import statsmodels.api as sm

# The matrices in the dynamic model are set up as follows
q, dt, s = 一, 0.一, 0.五
A = np.array([[一, dt, 0, 0],
  [0, 一, 0, 0],
  [0, 0, 一, dt],
  [0, 0, 0, 一]])
Q = q * np.array([[dt ** 三 / 三, dt ** 二 / 二, 0, 0],
[dt ** 二 / 二, dt, 0, 0],
[0, 0, dt ** 三 / 三, dt ** 二 / 二],
[0, 0, dt ** 二 / 二, dt]])
# Matrices in the measurement model are designed as follows
H = np.array([[一, 0, 0, 0],
  [0, 0, 一, 0]])
R = s ** 二 * np.eye(二)
# Starting values
m0 = np.array([[0, 一, 0, ⑴]]).T  # column vector
P0 = np.eye(四)


# Now instantiate a statespace model with the data
# (data should be shaped nobs x n_variables))
kf = sm.tsa.statespace.MLEModel(pd.DataFrame(Y.T), k_states=四)
kf._state_names = ['x一', 'dx一/dt', 'x二', 'dx二/dt']
kf['design'] = H
kf['obs_cov'] = R
kf['transition'] = A
kf['selection'] = np.eye(四)
kf['state_cov'] = Q

# Edit: the timing convention for initialization
# in Statsmodels differs from the the in the question
# So we should not use kf.initialize_known(m0[:, 0], P0)
# But instead, to fit the question's initialization
# into Statsmodels' timing, we just need to use the
# transition equation to move the initialization
# forward, as follows:
kf.initialize_known(A @ m0[:, 0], A @ P0 @ A.T + Q)

# To performan Kalman filtering and smoothing, use:
res = kf.smooth([])

# Then, for example, to print the smoothed estimates of
# the state vector:
print(res.states.smoothed)

佳了闭于活动进程状况空间模子的卡我曼滤波完成的学程便到这里便停止了,愿望趣模板源码网找到的这篇技巧文章能赞助到年夜野,更多技巧学程不妨在站内搜刮。