Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
96131 views
1
# -*- coding: utf-8 -*-
2
"""Copyright 2015 Roger R Labbe Jr.
3
4
FilterPy library.
5
http://github.com/rlabbe/filterpy
6
7
Documentation at:
8
https://filterpy.readthedocs.org
9
10
Supporting book at:
11
https://github.com/rlabbe/Kalman-and-Bayesian-Filters-in-Python
12
13
This is licensed under an MIT license. See the readme.MD file
14
for more information.
15
"""
16
17
from __future__ import (absolute_import, division, print_function,
18
unicode_literals)
19
import numpy as np
20
from numpy import random
21
import matplotlib.pyplot as plt
22
from filterpy.kalman import KalmanFilter, FixedLagSmoother
23
24
25
26
27
DO_PLOT = False
28
29
30
def test_fls():
31
32
# it is possible for the fixed lag to rarely perform worse than the
33
# kalman filter. Let it happen once in 50 times before we become
34
# alarmed.
35
36
fail_count = 0
37
for i in range(50):
38
fail_count = one_run_test_fls()
39
40
assert fail_count < 2
41
42
43
44
def test_batch_equals_recursive():
45
""" ensures that the batch filter and the recursive version both
46
produce the same results.
47
"""
48
49
N = 4 # size of lag
50
51
fls = FixedLagSmoother(dim_x=2, dim_z=1, N=N)
52
53
fls.x = np.array([0., .5])
54
55
fls.F = np.array([[1.,1.],
56
[0.,1.]])
57
58
fls.H = np.array([[1.,0.]])
59
60
fls.P *= 200
61
fls.R *= 5.
62
fls.Q *= 0.001
63
64
65
nom = np.array([t/2. for t in range (0,40)])
66
zs = np.array([t + random.randn()*1.1 for t in nom])
67
68
xs, x = fls.smooth_batch(zs, N)
69
70
71
for k,z in enumerate(zs):
72
fls.smooth(z)
73
74
xSmooth = np.asarray(fls.xSmooth)
75
xfl = xs[:,0].T[0]
76
77
res = xSmooth.T[0,0] - xfl
78
79
assert np.sum(res) < 1.e-12
80
81
82
83
84
85
def one_run_test_fls():
86
fls = FixedLagSmoother(dim_x=2, dim_z=1)
87
88
fls.x = np.array([0., .5])
89
fls.F = np.array([[1.,1.],
90
[0.,1.]])
91
92
fls.H = np.array([[1.,0.]])
93
fls.P *= 200
94
fls.R *= 5.
95
fls.Q *= 0.001
96
97
kf = KalmanFilter(dim_x=2, dim_z=1)
98
99
kf.x = np.array([0., .5])
100
kf.F = np.array([[1.,1.],
101
[0.,1.]])
102
kf.H = np.array([[1.,0.]])
103
kf.P *= 2000
104
kf.R *= 1.
105
kf.Q *= 0.001
106
107
N = 4 # size of lag
108
109
nom = np.array([t/2. for t in range (0,40)])
110
zs = np.array([t + random.randn()*1.1 for t in nom])
111
112
xs, x = fls.smooth_batch(zs, N)
113
114
M,P,_,_ = kf.batch_filter(zs)
115
rts_x,_,_ = kf.rts_smoother(M, P)
116
117
xfl = xs[:,0].T[0]
118
xkf = M[:,0].T[0]
119
120
fl_res = abs(xfl-nom)
121
kf_res = abs(xkf-nom)
122
123
if DO_PLOT:
124
plt.cla()
125
plt.plot(zs,'o', alpha=0.5, marker='o', label='zs')
126
plt.plot(x[:,0], label='FLS')
127
plt.plot(xfl, label='FLS S')
128
plt.plot(xkf, label='KF')
129
plt.plot(rts_x[:,0], label='RTS')
130
plt.legend(loc=4)
131
plt.show()
132
133
134
print(fl_res)
135
print(kf_res)
136
137
print('std fixed lag:', np.mean(fl_res[N:]))
138
print('std kalman:', np.mean(kf_res[N:]))
139
140
return np.mean(fl_res) <= np.mean(kf_res)
141
142
143
if __name__ == '__main__':
144
DO_PLOT = True
145
146
147
one_run_test_fls()
148
149
DO_PLOT = False
150
test_fls()
151
152
test_batch_equals_recursive()
153
154