-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathekf.py
More file actions
256 lines (197 loc) · 7.63 KB
/
ekf.py
File metadata and controls
256 lines (197 loc) · 7.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
"""
Notation:
----------
x is generally used for either the state or the mean of a gaussian. It should be clear from context which it is.
P is used about the state covariance
z is a single measurement
Z are multiple measurements so that z = Z[k] at a given time step k
v is the innovation z - h(x)
S is the innovation covariance
"""
# %% Imports
# types
from typing import Union, Callable, Any, Dict, Optional, List, Sequence, Tuple, Iterable
from typing_extensions import Final
# packages
from dataclasses import dataclass, field
import numpy as np
import scipy.linalg as la
import scipy
# local
import dynamicmodels as dynmods
import measurementmodels as measmods
from gaussparams import GaussParams, GaussParamList
from mixturedata import MixtureParameters
import mixturereduction
# %% The EKF
def isPSD(arr: np.ndarray, do_print: bool = False) -> bool:
# This block only works for positive definite matrices (no zero eigvals)
# try:
# arr_chol = np.linalg.cholesky(arr)
# except LinAlgError as e:
# if do_print:
# print(e)
# return False
# return True
return np.allclose(arr, arr.T) and np.all(np.linalg.eigvals(arr) >= 0)
@dataclass
class EKF:
# A Protocol so duck typing can be used
dynamic_model: dynmods.DynamicModel
# A Protocol so duck typing can be used
sensor_model: measmods.MeasurementModel
# _MLOG2PIby2: float = field(init=False, repr=False)
def __post_init__(self) -> None:
self._MLOG2PIby2: Final[float] = self.sensor_model.m * np.log(2 * np.pi) / 2
def predict(
self,
ekfstate: GaussParams,
# The sampling time in units specified by dynamic_model
Ts: float,
) -> GaussParams:
"""Predict the EKF state Ts seconds ahead."""
x, P = ekfstate
assert isPSD(P), "P input to EKF.predict not PSD"
F = self.dynamic_model.F(x, Ts)
Q = self.dynamic_model.Q(x, Ts)
x_pred = self.dynamic_model.f(x, Ts)
P_pred = F @ P @ F.T + Q
assert np.all(np.isfinite(P_pred)) and np.all(
np.isfinite(x_pred)
), "Non-finite EKF prediction."
assert isPSD(P_pred), "P_pred calculated by EKF.predict not PSD"
state_pred = GaussParams(x_pred, P_pred)
return state_pred
def innovation_mean(
self,
z: np.ndarray,
ekfstate: GaussParams,
*,
sensor_state: Optional[Dict[str, Any]] = None,
) -> np.ndarray:
"""Calculate the innovation mean for ekfstate at z in sensor_state."""
x = ekfstate.mean
zbar = self.sensor_model.h(x, sensor_state=sensor_state)
v = z - zbar
return v
def innovation_cov(
self,
z: np.ndarray,
ekfstate: GaussParams,
*,
sensor_state: Optional[Dict[str, Any]] = None,
) -> np.ndarray:
"""Calculate the innovation covariance for ekfstate at z in sensorstate."""
x, P = ekfstate
assert isPSD(P), "P input to EKF.innovation_cov not PSD"
H = self.sensor_model.H(x, sensor_state=sensor_state)
R = self.sensor_model.R(x, sensor_state=sensor_state, z=z)
S = H @ P @ H.T + R
assert isPSD(P), "S calculated by EKF.innovation_cov not PSD"
return S
def innovation(
self,
z: np.ndarray,
ekfstate: GaussParams,
*,
sensor_state: Optional[Dict[str, Any]] = None,
) -> GaussParams:
"""Calculate the innovation for ekfstate at z in sensor_state."""
v = self.innovation_mean(z, ekfstate, sensor_state=sensor_state)
S = self.innovation_cov(z, ekfstate, sensor_state=sensor_state)
innovationstate = GaussParams(v, S)
return innovationstate
def update(
self,
z: np.ndarray,
ekfstate: GaussParams,
*,
sensor_state: Optional[Dict[str, Any]] = None,
) -> GaussParams:
"""Update ekfstate with z in sensor_state"""
x, P = ekfstate
assert isPSD(P), "P input to EKF.update not PSD"
v, S = self.innovation(z, ekfstate, sensor_state=sensor_state)
H = self.sensor_model.H(x, sensor_state=sensor_state)
W = P @ la.solve(S, H).T
x_upd = x + W @ v
I = np.eye(*P.shape)
# standard form seem to give numerical instability causing non-PSD matrices for certain setups,
# or that some other calculate increases it in IMM etc.
# P_upd = P - W @ H @ P
# Better to use the more numerically stable Joseph form
P_upd = (I - W @ H) @ P @ (I - W @ H).T + W @ self.sensor_model.R(x) @ W.T
ekfstate_upd = GaussParams(x_upd, P_upd)
assert isPSD(P), "P_upd calculated by EKF.update not PSD"
return ekfstate_upd
def step(
self,
z: np.ndarray,
ekfstate: GaussParams,
# sampling time
Ts: float,
*,
sensor_state: Optional[Dict[str, Any]] = None,
) -> GaussParams:
"""Predict ekfstate Ts units ahead and then update this prediction with z in sensor_state."""
ekfstate_pred = self.predict(ekfstate, Ts)
ekfstate_upd = self.update(z, ekfstate_pred, sensor_state=sensor_state)
return ekfstate_upd
def NIS(
self,
z: np.ndarray,
ekfstate: GaussParams,
*,
sensor_state: Optional[Dict[str, Any]] = None,
) -> float:
"""Calculate the normalized innovation squared for ekfstate at z in sensor_state"""
v, S = self.innovation(z, ekfstate, sensor_state=sensor_state)
cholS = la.cholesky(S, lower=True)
invcholS_v = la.solve_triangular(cholS, v, lower=True)
NIS = (invcholS_v ** 2).sum()
# alternative:
# NIS = v @ la.solve(S, v)
return NIS
@classmethod
def estimate(cls, ekfstate: GaussParams) -> GaussParams:
"""Get the estimate from the state with its covariance. (Compatibility method)"""
return ekfstate
def loglikelihood(
self,
z: np.ndarray,
ekfstate: GaussParams,
*,
sensor_state: Optional[Dict[str, Any]] = None,
) -> float:
"""Calculate the log likelihood of ekfstate at z in sensor_state"""
v, S = self.innovation(z, ekfstate, sensor_state=sensor_state)
cholS = la.cholesky(S, lower=True)
invcholS_v = la.solve_triangular(cholS, np.transpose(v), lower=True)
NISby2 = (invcholS_v ** 2).sum() / 2
# alternative self.NIS(...) /2 or v @ la.solve(S, v)/2
logdetSby2 = np.log(cholS.diagonal()).sum()
# alternative use la.slogdet(S), or np.log(la.det(S))
ll = -(NISby2 + logdetSby2 + self._MLOG2PIby2)
# simplest overall alternative
# ll = scipy.stats.multivariate_normal.logpdf(v, cov=S)
return ll
def reduce_mixture(
self, ekfstate_mixture: MixtureParameters[GaussParams]
) -> GaussParams:
"""Merge a Gaussian mixture into single mixture"""
w = ekfstate_mixture.weights
x = np.array([c.mean for c in ekfstate_mixture.components], dtype=float)
P = np.array([c.cov for c in ekfstate_mixture.components], dtype=float)
x_reduced, P_reduced = mixturereduction.gaussian_mixture_moments(w, x, P)
return GaussParams(x_reduced, P_reduced)
def gate(
self,
z: np.ndarray,
ekfstate: GaussParams,
gate_size_square: float,
*,
sensor_state: Optional[Dict[str, Any]],
) -> bool:
""" Check if z is inside sqrt(gate_sized_squared)-sigma ellipse of ekfstate in sensor_state """
NIS = self.NIS(z, ekfstate, sensor_state=sensor_state)
return (NIS < gate_size_square) #a simple comparison should suffice here