Ensemble Kalman Filter
EnsembleKalmanFilter¶
Introduction and Overview¶
This implements the Ensemble Kalman filter.
EnsembleKalmanFilter
¶
Bases: object
This implements the ensemble Kalman filter (EnKF). The EnKF uses an ensemble of hundreds to thousands of state vectors that are randomly sampled around the estimate, and adds perturbations at each update and predict step. It is useful for extremely large systems such as found in hydrophysics. As such, this class is admittedly a toy as it is far too slow with large N.
There are many versions of this sort of this filter. This formulation is due to Crassidis and Junkins [1]. It works with both linear and nonlinear systems.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
array(dim_x)
|
state mean |
required |
P
|
array((dim_x, dim_x))
|
covariance of the state |
required |
dim_z
|
int
|
Number of of measurement inputs. For example, if the sensor provides you with position in (x,y), dim_z would be 2. |
required |
dt
|
float
|
time step in seconds |
required |
N
|
int
|
number of sigma points (ensembles). Must be greater than 1. |
required |
hx
|
function hx(x)
|
Measurement function. May be linear or nonlinear - converts state x into a measurement. Return must be an np.array of the same dimensionality as the measurement vector. |
required |
fx
|
function fx(x, dt)
|
State transition function. May be linear or nonlinear. Projects state x into the next time period. Returns the projected state x. |
required |
Attributes:
| Name | Type | Description |
|---|---|---|
x |
array(dim_x, 1)
|
State estimate |
P |
array(dim_x, dim_x)
|
State covariance matrix |
x_prior |
array(dim_x, 1)
|
Prior (predicted) state estimate. The _prior and _post attributes are for convienence; they store the prior and posterior of the current epoch. Read Only. |
P_prior |
array(dim_x, dim_x)
|
Prior (predicted) state covariance matrix. Read Only. |
x_post |
array(dim_x, 1)
|
Posterior (updated) state estimate. Read Only. |
P_post |
array(dim_x, dim_x)
|
Posterior (updated) state covariance matrix. Read Only. |
z |
array
|
Last measurement used in update(). Read only. |
R |
array(dim_z, dim_z)
|
Measurement noise matrix |
Q |
array(dim_x, dim_x)
|
Process noise matrix |
fx |
callable(x, dt)
|
State transition function |
hx |
callable(x)
|
Measurement function. Convert state |
inv |
function, default numpy.linalg.inv
|
If you prefer another inverse function, such as the Moore-Penrose pseudo inverse, set it to that instead: kf.inv = np.linalg.pinv |
Examples:
.. code-block:: Python
def hx(x):
return np.array([x[0]])
F = np.array([[1., 1.],
[0., 1.]])
def fx(x, dt):
return np.dot(F, x)
x = np.array([0., 1.])
P = np.eye(2) * 100.
dt = 0.1
f = EnsembleKalmanFilter(x=x, P=P, dim_z=1, dt=dt,
N=8, hx=hx, fx=fx)
std_noise = 3.
f.R *= std_noise**2
f.Q = Q_discrete_white_noise(2, dt, .01)
while True:
z = read_sensor()
f.predict()
f.update(np.asarray([z]))
See my book Kalman and Bayesian Filters in Python https://github.com/rlabbe/Kalman-and-Bayesian-Filters-in-Python
References
- [1] John L Crassidis and John L. Junkins. "Optimal Estimation of Dynamic Systems. CRC Press, second edition. 2012. pp, 257-9.
Source code in bayesian_filters/kalman/ensemble_kalman_filter.py
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 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 | |
initialize(x, P)
¶
Initializes the filter with the specified mean and covariance. Only need to call this if you are using the filter to filter more than one set of data; this is called by init
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
array(dim_z)
|
state mean |
required |
P
|
array((dim_x, dim_x))
|
covariance of the state |
required |
Source code in bayesian_filters/kalman/ensemble_kalman_filter.py
predict()
¶
Predict next position.
Source code in bayesian_filters/kalman/ensemble_kalman_filter.py
update(z, R=None)
¶
Add a new measurement (z) to the kalman filter. If z is None, nothing is changed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
z
|
array
|
measurement for this update. |
required |
R
|
np.array, scalar, or None
|
Optionally provide R to override the measurement noise for this one call, otherwise self.R will be used. |
None
|