# FilterPy Source¶

For your convienence I have loaded several of FilterPy's core algorithms into this appendix.

## KalmanFilter¶

In [ ]:
# %load https://raw.githubusercontent.com/rlabbe/filterpy/master/filterpy/kalman/kalman_filter.py

"""Copyright 2014 Roger R Labbe Jr.

filterpy library.
http://github.com/rlabbe/filterpy

Documentation at:

Supporting book at:
https://github.com/rlabbe/Kalman-and-Bayesian-Filters-in-Python

"""

from __future__ import (absolute_import, division, print_function,
unicode_literals)
import numpy as np
import scipy.linalg as linalg
from numpy import dot, zeros, eye, isscalar
from filterpy.common import setter, setter_1d, setter_scalar, dot3

class KalmanFilter(object):
""" Implements a Kalman filter. You are responsible for setting the
various state variables to reasonable values; the defaults  will
not give you a functional filter.

You will have to set the following attributes after constructing this
object for the filter to perform properly. Please note that there are
various checks in place to ensure that you have made everything the
'correct' size. However, it is possible to provide incorrectly sized
arrays such that the linear algebra can not perform an operation.
It can also fail silently - you can end up with matrices of a size that
allows the linear algebra to work, but are the wrong shape for the problem
you are trying to solve.

**Attributes**

x : numpy.array(dim_x, 1)
state estimate vector

P : numpy.array(dim_x, dim_x)
covariance estimate matrix

R : numpy.array(dim_z, dim_z)
measurement noise matrix

Q : numpy.array(dim_x, dim_x)
process noise matrix

F : numpy.array()
State Transition matrix

H : numpy.array(dim_x, dim_x)

You may read the following attributes.

y : numpy.array
Residual of the update step.

K : numpy.array(dim_x, dim_z)
Kalman gain of the update step

S :  numpy.array
Systen uncertaintly projected to measurement space

"""

def __init__(self, dim_x, dim_z, dim_u=0):
""" Create a Kalman filter. You are responsible for setting the
various state variables to reasonable values; the defaults below will
not give you a functional filter.

**Parameters**

dim_x : int
Number of state variables for the Kalman filter. For example, if
you are tracking the position and velocity of an object in two
dimensions, dim_x would be 4.

This is used to set the default size of P, Q, and u

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.

dim_u : int (optional)
size of the control input, if it is being used.
Default value of 0 indicates it is not used.
"""

assert dim_x > 0
assert dim_z > 0
assert dim_u >= 0

self.dim_x = dim_x
self.dim_z = dim_z
self.dim_u = dim_u

self._x = zeros((dim_x,1)) # state
self._P = eye(dim_x)       # uncertainty covariance
self._Q = eye(dim_x)       # process uncertainty
self._B = 0                # control transition matrix
self._F = 0                # state transition matrix
self.H = 0                 # Measurement function
self.R = eye(dim_z)        # state uncertainty
self._alpha_sq = 1.        # fading memory control

# gain and residual are computed during the innovation step. We
# save them so that in case you want to inspect them for various
# purposes
self._K = 0 # kalman gain
self._y = zeros((dim_z, 1))
self._S = 0 # system uncertainty in measurement space

# identity matrix. Do not alter this.
self._I = np.eye(dim_x)

def update(self, z, R=None, H=None):
"""
Add a new measurement (z) to the kalman filter. If z is None, nothing
is changed.

**Parameters**

z : np.array
measurement for this update.

R : np.array, scalar, or None
Optionally provide R to override the measurement noise for this
one call, otherwise  self.R will be used.
"""

if z is None:
return

if R is None:
R = self.R
elif isscalar(R):
R = eye(self.dim_z) * R

# rename for readability and a tiny extra bit of speed
if H is None:
H = self.H
P = self._P
x = self._x

# y = z - Hx
# error (residual) between measurement and prediction
self._y = z - dot(H, x)

# S = HPH' + R
# project system uncertainty into measurement space
S = dot3(H, P, H.T) + R

# K = PH'inv(S)
# map system uncertainty into kalman gain
K = dot3(P, H.T, linalg.inv(S))

# x = x + Ky
# predict new x with residual scaled by the kalman gain
self._x = x + dot(K, self._y)

# P = (I-KH)P(I-KH)' + KRK'
I_KH = self._I - dot(K, H)
self._P = dot3(I_KH, P, I_KH.T) + dot3(K, R, K.T)

self._S = S
self._K = K

def test_matrix_dimensions(self):
""" Performs a series of asserts to check that the size of everything
is what it should be. This can help you debug problems in your design.

This is only a test; you do not need to use it while filtering.
However, to use you will want to perform at least one predict() and
one update() before calling; some bad designs will cause the shapes
of x and P to change in a silent and bad way. For example, if you
pass in a badly dimensioned z into update that can cause x to be
misshapen."""

assert self._x.shape == (self.dim_x, 1), \
"Shape of x must be ({},{}), but is {}".format(
self.dim_x, 1, self._x.shape)

assert self._P.shape == (self.dim_x, self.dim_x), \
"Shape of P must be ({},{}), but is {}".format(
self.dim_x, self.dim_x, self._P.shape)

assert self._Q.shape == (self.dim_x, self.dim_x), \
"Shape of P must be ({},{}), but is {}".format(
self.dim_x, self.dim_x, self._P.shape)

def predict(self, u=0):
""" Predict next position.

**Parameters**

u : np.array
Optional control vector. If non-zero, it is multiplied by B
to create the control input into the system.
"""

# x = Fx + Bu
self._x = dot(self._F, self.x) + dot(self._B, u)

# P = FPF' + Q
self._P = self._alpha_sq * dot3(self._F, self._P, self._F.T) + self._Q

def batch_filter(self, zs, Rs=None, update_first=False):
""" Batch processes a sequences of measurements.

**Parameters**

zs : list-like
list of measurements at each time step self.dt Missing
measurements must be represented by 'None'.

Rs : list-like, optional
optional list of values to use for the measurement error
covariance; a value of None in any position will cause the filter
to use self.R for that time step.

update_first : bool, optional,
controls whether the order of operations is update followed by
predict, or predict followed by update. Default is predict->update.

**Returns**

means: np.array((n,dim_x,1))
array of the state for each time step after the update. Each entry
is an np.array. In other words means[k,:] is the state at step
k.

covariance: np.array((n,dim_x,dim_x))
array of the covariances for each time step after the update.
In other words covariance[k,:,:] is the covariance at step k.

means_predictions: np.array((n,dim_x,1))
array of the state for each time step after the predictions. Each
entry is an np.array. In other words means[k,:] is the state at
step k.

covariance_predictions: np.array((n,dim_x,dim_x))
array of the covariances for each time step after the prediction.
In other words covariance[k,:,:] is the covariance at step k.
"""

try:
z = zs[0]
except:
assert not isscalar(zs), 'zs must be list-like'

if self.dim_z == 1:
assert isscalar(z) or (z.ndim==1 and len(z) == 1), \
'zs must be a list of scalars or 1D, 1 element arrays'

else:
assert len(z) == self.dim_z, 'each element in zs must be a'
'1D array of length {}'.format(self.dim_z)

n = np.size(zs,0)
if Rs is None:
Rs = [None]*n

# mean estimates from Kalman Filter
if self.x.ndim == 1:
means   = zeros((n, self.dim_x))
means_p = zeros((n, self.dim_x))
else:
means   = zeros((n, self.dim_x, 1))
means_p = zeros((n, self.dim_x, 1))

# state covariances from Kalman Filter
covariances   = zeros((n, self.dim_x, self.dim_x))
covariances_p = zeros((n, self.dim_x, self.dim_x))

if update_first:
for i, (z, r) in enumerate(zip(zs, Rs)):
self.update(z, r)
means[i,:]         = self._x
covariances[i,:,:] = self._P

self.predict()
means_p[i,:]         = self._x
covariances_p[i,:,:] = self._P
else:
for i, (z, r) in enumerate(zip(zs, Rs)):
self.predict()
means_p[i,:]         = self._x
covariances_p[i,:,:] = self._P

self.update(z, r)
means[i,:]         = self._x
covariances[i,:,:] = self._P

return (means, covariances, means_p, covariances_p)

def rts_smoother(self, Xs, Ps, Qs=None):
""" Runs the Rauch-Tung-Striebal Kalman smoother on a set of
means and covariances computed by a Kalman filter. The usual input
would come from the output of KalmanFilter.batch_filter().

**Parameters**

Xs : numpy.array
array of the means (state variable x) of the output of a Kalman
filter.

Ps : numpy.array
array of the covariances of the output of a kalman filter.

Q : list-like collection of numpy.array, optional
Process noise of the Kalman filter at each time step. Optional,
if not provided the filter's self.Q will be used

**Returns**

'x' : numpy.ndarray
smoothed means

'P' : numpy.ndarray
smoothed state covariances

'K' : numpy.ndarray
smoother gain at each step

**Example**::

zs = [t + random.randn()*4 for t in range (40)]

(mu, cov, _, _) = kalman.batch_filter(zs)
(x, P, K) = rts_smoother(mu, cov, fk.F, fk.Q)

"""

assert len(Xs) == len(Ps)
shape = Xs.shape
n = shape[0]
dim_x = shape[1]

F = self._F
if not Qs:
Qs = [self.Q] * n

# smoother gain
K = zeros((n,dim_x,dim_x))

x, P = Xs.copy(), Ps.copy()

for k in range(n-2,-1,-1):
P_pred = dot3(F, P[k], F.T) + Qs[k]

K[k]  = dot3(P[k], F.T, linalg.inv(P_pred))
x[k] += dot (K[k], x[k+1] - dot(F, x[k]))
P[k] += dot3 (K[k], P[k+1] - P_pred, K[k].T)

return (x, P, K)

def get_prediction(self, u=0):
""" Predicts the next state of the filter and returns it. Does not
alter the state of the filter.

**Parameters**

u : np.array
optional control input

**Returns**

(x, P)
State vector and covariance array of the prediction.
"""

x = dot(self._F, self._x) + dot(self._B, u)
P = self._alpha_sq * dot3(self._F, self._P, self._F.T) + self._Q
return (x, P)

def residual_of(self, z):
""" returns the residual for the given measurement (z). Does not alter
the state of the filter.
"""
return z - dot(self.H, self._x)

def measurement_of_state(self, x):
""" Helper function that converts a state into a measurement.

**Parameters**

x : np.array
kalman state vector

**Returns**

z : np.array
measurement corresponding to the given state
"""

return dot(self.H, x)

@property
def alpha(self):
""" Fading memory setting. 1.0 gives the normal Kalman filter, and
values slightly larger than 1.0 (such as 1.02) give a fading
memory effect - previous measurements have less influence on the
filter's estimates. This formulation of the Fading memory filter
(there are many) is due to Dan Simon [1].

** References **

[1] Dan Simon. "Optimal State Estimation." John Wiley & Sons.
p. 208-212. (2006)
"""

return self._alpha_sq**.5

@alpha.setter
def alpha(self, value):
assert np.isscalar(value)
assert value > 0

self._alpha_sq = value**2

@property
def Q(self):
""" Process uncertainty"""
return self._Q

@Q.setter
def Q(self, value):
self._Q = setter_scalar(value, self.dim_x)

@property
def P(self):
""" covariance matrix"""
return self._P

@P.setter
def P(self, value):
self._P = setter_scalar(value, self.dim_x)

@property
def F(self):
""" state transition matrix"""
return self._F

@F.setter
def F(self, value):
self._F = setter(value, self.dim_x, self.dim_x)

@property
def B(self):
""" control transition matrix"""
return self._B

@B.setter
def B(self, value):
""" control transition matrix"""
self._B = setter (value, self.dim_x, self.dim_u)

@property
def x(self):
""" filter state vector."""
return self._x

@x.setter
def x(self, value):
self._x = setter_1d(value, self.dim_x)

@property
def K(self):
""" Kalman gain """
return self._K

@property
def y(self):
""" measurement residual (innovation) """
return self._y

@property
def S(self):
""" system uncertainty in measurement space """
return self._S


## ExtendedKalmanFilter¶

In [ ]:
# %load https://raw.githubusercontent.com/rlabbe/filterpy/master/filterpy/kalman/EKF.py

"""Copyright 2014 Roger R Labbe Jr.

filterpy library.
http://github.com/rlabbe/filterpy

Documentation at:

Supporting book at:
https://github.com/rlabbe/Kalman-and-Bayesian-Filters-in-Python

"""

from __future__ import (absolute_import, division, print_function,
unicode_literals)
import numpy as np
import scipy.linalg as linalg
from numpy import dot, zeros, eye
from filterpy.common import setter, setter_1d, setter_scalar, dot3

class ExtendedKalmanFilter(object):

def __init__(self, dim_x, dim_z, dim_u=0):
""" Extended Kalman filter. You are responsible for setting the
various state variables to reasonable values; the defaults below will
not give you a functional filter.

**Parameters**

dim_x : int
Number of state variables for the Kalman filter. For example, if
you are tracking the position and velocity of an object in two
dimensions, dim_x would be 4.

This is used to set the default size of P, Q, and u

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.
"""

self.dim_x = dim_x
self.dim_z = dim_z

self._x = zeros((dim_x,1)) # state
self._P = eye(dim_x)       # uncertainty covariance
self._B = 0                # control transition matrix
self._F = 0                # state transition matrix
self._R = eye(dim_z)       # state uncertainty
self._Q = eye(dim_x)       # process uncertainty
self._y = zeros((dim_z, 1))

# identity matrix. Do not alter this.
self._I = np.eye(dim_x)

def predict_update(self, z, HJacobian, Hx, u=0):
""" Performs the predict/update innovation of the extended Kalman
filter.

**Parameters**

z : np.array
measurement for this step.
If None, only predict step is perfomed.

HJacobian : function
function which computes the Jacobian of the H matrix (measurement
function). Takes state variable (self.x) as input, returns H.

Hx : function
function which takes a state variable and returns the measurement
that would correspond to that state.

u : np.array or scalar
optional control vector input to the filter.
"""
if np.isscalar(z) and self.dim_z == 1:
z = np.asarray([z], float)

F = self._F
B = self._B
P = self._P
Q = self._Q
R = self._R
x = self._x

H = HJacobian(x)

# predict step
x = dot(F, x) + dot(B, u)
P = dot3(F, P, F.T) + Q

# update step
S = dot3(H, P, H.T) + R
K = dot3(P, H.T, linalg.inv (S))

self._x = x + dot(K, (z - Hx(x)))

I_KH = self._I - dot(K, H)
self._P = dot3(I_KH, P, I_KH.T) + dot3(K, R, K.T)

def update(self, z, HJacobian, Hx, R=None):
""" Performs the update innovation of the extended Kalman filter.

**Parameters**

z : np.array
measurement for this step.
If None, only predict step is perfomed.

HJacobian : function
function which computes the Jacobian of the H matrix (measurement
function). Takes state variable (self.x) as input, returns H.

Hx : function
function which takes a state variable and returns the measurement
that would correspond to that state.
"""

P = self._P
if R is None:
R = self._R
elif np.isscalar(R):
R = eye(self.dim_z) * R

if np.isscalar(z) and self.dim_z == 1:
z = np.asarray([z], float)

x = self._x

H = HJacobian(x)

S = dot3(H, P, H.T) + R
K = dot3(P, H.T, linalg.inv (S))

y = z - Hx(x)
self._x = x + dot(K, y)

I_KH = self._I - dot(K, H)
self._P = dot3(I_KH, P, I_KH.T) + dot3(K, R, K.T)

def predict_x(self, u=0):
""" predicts the next state of X. If you need to
compute the next state yourself, override this function. You would
need to do this, for example, if the usual Taylor expansion to
generate F is not providing accurate results for you. """

self._x = dot(self._F, self._x) + dot(self._B, u)

def predict(self, u=0):
""" Predict next position.

**Parameters**

u : np.array
Optional control vector. If non-zero, it is multiplied by B
to create the control input into the system.
"""

self.predict_x(u)
self._P = dot3(self._F, self._P, self._F.T) + self._Q

@property
def Q(self):
""" Process uncertainty"""
return self._Q

@Q.setter
def Q(self, value):
self._Q = setter_scalar(value, self.dim_x)

@property
def P(self):
""" covariance matrix"""
return self._P

@P.setter
def P(self, value):
self._P = setter_scalar(value, self.dim_x)

@property
def R(self):
""" measurement uncertainty"""
return self._R

@R.setter
def R(self, value):
self._R = setter_scalar(value, self.dim_z)

@property
def F(self):
return self._F

@F.setter
def F(self, value):
self._F = setter(value, self.dim_x, self.dim_x)

@property
def B(self):
return self._B

@B.setter
def B(self, value):
""" control transition matrix"""
self._B = setter(value, self.dim_x, self.dim_u)

@property
def x(self):
return self._x

@x.setter
def x(self, value):
self._x = setter_1d(value, self.dim_x)

@property
def K(self):
""" Kalman gain """
return self._K

@property
def y(self):
""" measurement residual (innovation) """
return self._y

@property
def S(self):
""" system uncertainty in measurement space """
return self._S


## UnscentedKalmanFilter¶

In [ ]:
# %load https://raw.githubusercontent.com/rlabbe/filterpy/master/filterpy/kalman/UKF.py
"""Copyright 2014 Roger R Labbe Jr.

filterpy library.
http://github.com/rlabbe/filterpy

Documentation at:

Supporting book at:
https://github.com/rlabbe/Kalman-and-Bayesian-Filters-in-Python

"""

# pylint bug - warns about numpy functions which do in fact exist.
# pylint: disable=E1101

#I like aligning equal signs for readability of math
# pylint: disable=C0326

from __future__ import (absolute_import, division, print_function,
unicode_literals)

from numpy.linalg import inv, cholesky
import numpy as np
from numpy import asarray, eye, zeros, dot, isscalar, outer
from filterpy.common import dot3

class UnscentedKalmanFilter(object):
# pylint: disable=too-many-instance-attributes
# pylint: disable=C0103
""" Implements the Unscented Kalman filter (UKF) as defined by Simon J.
Julier and Jeffery K. Uhlmann [1]. Succintly, the UKF selects a set of
sigma points and weights inside the covariance matrix of the filter's
state. These points are transformed through the nonlinear process being
filtered, and are rebuilt into a mean and covariance by computed the
weighted mean and expected value of the transformed points. Read the paper;
it is excellent. My book "Kalman and Bayesian Filters in Python" [2]
explains the algorithm, develops this code, and provides examples of the
filter in use.

You will have to set the following attributes after constructing this
object for the filter to perform properly.

**Attributes**

x : numpy.array(dim_x)
state estimate vector

P : numpy.array(dim_x, dim_x)
covariance estimate matrix

R : numpy.array(dim_z, dim_z)
measurement noise matrix

Q : numpy.array(dim_x, dim_x)
process noise matrix

You may read the following attributes.

Pxz : numpy.aray(dim_x, dim_z)
Cross variance of x and z computed during update() call.

**References**

.. [1] Julier, Simon J.; Uhlmann, Jeffrey "A New Extension of the Kalman
Filter to Nonlinear Systems". Proc. SPIE 3068, Signal Processing,
Sensor Fusion, and Target Recognition VI, 182 (July 28, 1997)

.. [2] Labbe, Roger R. "Kalman and Bayesian Filters in Python"

https://github.com/rlabbe/Kalman-and-Bayesian-Filters-in-Python
"""

def __init__(self, dim_x, dim_z, dt, hx, fx, kappa=0.):
""" Create a Kalman filter. You are responsible for setting the
various state variables to reasonable values; the defaults below will
not give you a functional filter.

**Parameters**

dim_x : int
Number of state variables for the filter. For example, if
you are tracking the position and velocity of an object in two
dimensions, dim_x would be 4.

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.

dt : float
Time between steps in seconds.

hx : function(x)
Measurement function. Converts state vector x into a measurement
vector of shape (dim_z).

fx : function(x,dt)
function that returns the state x transformed by the
state transistion function. dt is the time step in seconds.

kappa : float, default=0.
Scaling factor that can reduce high order errors. kappa=0 gives
the standard unscented filter. According to [1], if you set
kappa to 3-dim_x for a Gaussian x you will minimize the fourth
order errors in x and P.

**References**

[1] S. Julier, J. Uhlmann, and H. Durrant-Whyte. "A new method for
the nonlinear transformation of means and covariances in filters
and estimators," IEEE Transactions on Automatic Control, 45(3),
pp. 477-482 (March 2000).
"""

self.Q = eye(dim_x)
self.R = eye(dim_z)
self.x = zeros(dim_x)
self.P = eye(dim_x)
self._dim_x = dim_x
self._dim_z = dim_z
self._dt = dt
self._num_sigmas = 2*dim_x + 1
self.kappa = kappa
self.hx = hx
self.fx = fx

# weights for the sigma points
self.W = self.weights(dim_x, kappa)

# sigma points transformed through f(x) and h(x)
# variables for efficiency so we don't recreate every update
self.sigmas_f = zeros((self._num_sigmas, self._dim_x))

def update(self, z, R=None, residual=np.subtract, UT=None):
""" Update the UKF with the given measurements. On return,
self.x and self.P contain the new mean and covariance of the filter.

**Parameters**

z : numpy.array of shape (dim_z)
measurement vector

R : numpy.array((dim_z, dim_z)), optional
Measurement noise. If provided, overrides self.R for
this function call.

residual : function (z, z2), optional
Optional function that computes the residual (difference) between
the two measurement vectors. If you do not provide this, then the
built in minus operator will be used. You will normally want to use
the built in unless your residual computation is nonlinear (for
example, if they are angles)

UT : function(sigmas, Wm, Wc, noise_cov), optional
Optional function to compute the unscented transform for the sigma
points passed through hx. Typically the default function will
work, but if for example you are using angles the default method
of computing means and residuals will not work, and you will have
to define how to compute it.
"""

if isscalar(z):
dim_z = 1
else:
dim_z = len(z)

if R is None:
R = self.R
elif np.isscalar(R):
R = eye(self._dim_z) * R

# rename for readability
sigmas_f = self.sigmas_f
sigmas_h = zeros((self._num_sigmas, dim_z))

if UT is None:
UT = unscented_transform

# transform sigma points into measurement space
for i in range(self._num_sigmas):
sigmas_h[i] = self.hx(sigmas_f[i])

# mean and covariance of prediction passed through inscented transform
zp, Pz = UT(sigmas_h, self.W, self.W, R)

# compute cross variance of the state and the measurements
'''self.Pxz = zeros((self._dim_x, dim_z))
for i in range(self._num_sigmas):
self.Pxz += self.W[i] * np.outer(sigmas_f[i] - self.x,
residual(sigmas_h[i], zp))'''

# this is the unreadable but fast implementation of the
# commented out loop above
yh = sigmas_f - self.x[np.newaxis, :]
yz = residual(sigmas_h, zp[np.newaxis, :])
self.Pxz = yh.T.dot(np.diag(self.W)).dot(yz)

K = dot(self.Pxz, inv(Pz)) # Kalman gain
y = residual(z, zp)

self.x = self.x + dot(K, y)
self.P = self.P - dot3(K, Pz, K.T)

def predict(self, dt=None):
""" Performs the predict step of the UKF. On return, self.xp and
self.Pp contain the predicted state (xp) and covariance (Pp). 'p'
stands for prediction.

**Parameters**
dt : double, optional
If specified, the time step to be used for this prediction.
self._dt is used if this is not provided.

Important: this MUST be called before update() is called for the
first time.
"""

if dt is None:
dt = self._dt

# calculate sigma points for given mean and covariance
sigmas = self.sigma_points(self.x, self.P, self.kappa)

for i in range(self._num_sigmas):
self.sigmas_f[i] = self.fx(sigmas[i], dt)

self.x, self.P = unscented_transform(
self.sigmas_f, self.W, self.W, self.Q)

def batch_filter(self, zs, Rs=None, residual=np.subtract, UT=None):
""" Performs the UKF filter over the list of measurement in zs.

**Parameters**

zs : list-like
list of measurements at each time step self._dt Missing
measurements must be represented by 'None'.

Rs : list-like, optional
optional list of values to use for the measurement error
covariance; a value of None in any position will cause the filter
to use self.R for that time step.

residual : function (z, z2), optional
Optional function that computes the residual (difference) between
the two measurement vectors. If you do not provide this, then the
built in minus operator will be used. You will normally want to use
the built in unless your residual computation is nonlinear (for
example, if they are angles)

UT : function(sigmas, Wm, Wc, noise_cov), optional
Optional function to compute the unscented transform for the sigma
points passed through hx. Typically the default function will
work, but if for example you are using angles the default method
of computing means and residuals will not work, and you will have
to define how to compute it.

**Returns**

means: np.array((n,dim_x,1))
array of the state for each time step after the update. Each entry
is an np.array. In other words means[k,:] is the state at step
k.

covariance: np.array((n,dim_x,dim_x))
array of the covariances for each time step after the update.
In other words covariance[k,:,:] is the covariance at step k.

"""

try:
z = zs[0]
except:
assert not isscalar(zs), 'zs must be list-like'

if self._dim_z == 1:
assert isscalar(z) or (z.ndim==1 and len(z) == 1), \
'zs must be a list of scalars or 1D, 1 element arrays'

else:
assert len(z) == self._dim_z, 'each element in zs must be a' \
'1D array of length {}'.format(self._dim_z)

n = np.size(zs,0)
if Rs is None:
Rs = [None]*n

# mean estimates from Kalman Filter
if self.x.ndim == 1:
means = zeros((n, self._dim_x))
else:
means = zeros((n, self._dim_x, 1))

# state covariances from Kalman Filter
covariances = zeros((n, self._dim_x, self._dim_x))

for i, (z, r) in enumerate(zip(zs, Rs)):
self.predict()
self.update(z, r)
means[i,:]         = self.x
covariances[i,:,:] = self.P

return (means, covariances)

def rts_smoother(self, Xs, Ps, Qs=None, dt=None):
""" Runs the Rauch-Tung-Striebal Kalman smoother on a set of
means and covariances computed by the UKF. The usual input
would come from the output of batch_filter().

**Parameters**

Xs : numpy.array
array of the means (state variable x) of the output of a Kalman
filter.

Ps : numpy.array
array of the covariances of the output of a kalman filter.

Q : list-like collection of numpy.array, optional
Process noise of the Kalman filter at each time step. Optional,
if not provided the filter's self.Q will be used

dt : optional, float or array-like of float
If provided, specifies the time step of each step of the filter.
If float, then the same time step is used for all steps. If
an array, then each element k contains the time  at step k.
Units are seconds.

**Returns**

'x' : numpy.ndarray
smoothed means

'P' : numpy.ndarray
smoothed state covariances

'K' : numpy.ndarray
smoother gain at each step

**Example**::

zs = [t + random.randn()*4 for t in range (40)]

(mu, cov, _, _) = kalman.batch_filter(zs)
(x, P, K) = rts_smoother(mu, cov, fk.F, fk.Q)

"""
assert len(Xs) == len(Ps)
n, dim_x = Xs.shape

if dt is None:
dt = [self._dt] * n
elif isscalar(dt):
dt = [dt] * n

if Qs is None:
Qs = [self.Q] * n

# smoother gain
Ks = zeros((n,dim_x,dim_x))

num_sigmas = 2*dim_x + 1

xs, ps = Xs.copy(), Ps.copy()
sigmas_f = zeros((num_sigmas, dim_x))

for k in range(n-2,-1,-1):
# create sigma points from state estimate, pass through state func
sigmas = self.sigma_points(xs[k], ps[k], self.kappa)
for i in range(num_sigmas):
sigmas_f[i] = self.fx(sigmas[i], dt[k])

# compute backwards prior state and covariance
xb = dot(self.W, sigmas_f)
Pb = 0
x = Xs[k]
for i in range(num_sigmas):
y = sigmas_f[i] - x
Pb += self.W[i] * outer(y, y)
Pb += Qs[k]

# compute cross variance
Pxb = 0
for i in range(num_sigmas):
z = sigmas[i] - Xs[k]
y = sigmas_f[i] - xb
Pxb += self.W[i] * outer(z, y)

# compute gain
K = dot(Pxb, inv(Pb))

# update the smoothed estimates
xs[k] += dot (K, xs[k+1] - xb)
ps[k] += dot3(K, ps[k+1] - Pb, K.T)
Ks[k] = K

return (xs, ps, Ks)

@staticmethod
def weights(n, kappa):
""" Computes the weights for an unscented Kalman filter.  See
__init__() for meaning of parameters.
"""

assert n > 0, "n must be greater than 0, it's value is {}".format(n)

k = .5 / (n+kappa)
W = np.full(2*n+1, k)
W[0] = kappa / (n+kappa)
return W

@staticmethod
def sigma_points(x, P, kappa):
""" Computes the sigma points for an unscented Kalman filter
given the mean (x) and covariance(P) of the filter.
kappa is an arbitrary constant. Returns sigma points.

Works with both scalar and array inputs:
sigma_points (5, 9, 2) # mean 5, covariance 9
sigma_points ([5, 2], 9*eye(2), 2) # means 5 and 2, covariance 9I

**Parameters**

X An array-like object of the means of length n
Can be a scalar if 1D.
examples: 1, [1,2], np.array([1,2])

P : scalar, or np.array
Covariance of the filter. If scalar, is treated as eye(n)*P.

kappa : float
Scaling factor.

**Returns**

sigmas : np.array, of size (n, 2n+1)
2D array of sigma points. Each column contains all of
the sigmas for one dimension in the problem space. They
are ordered as:

.. math::
sigmas[0]    = x \n
sigmas[1..n] = x + [\sqrt{(n+\kappa)P}]_k \n
sigmas[n+1..2n] = x - [\sqrt{(n+\kappa)P}]_k
"""

if np.isscalar(x):
x = asarray([x])
n = np.size(x)  # dimension of problem

if np.isscalar(P):
P = eye(n)*P

sigmas = zeros((2*n+1, n))

# implements U'*U = (n+kappa)*P. Returns lower triangular matrix.
# Take transpose so we can access with U[i]
U = cholesky((n+kappa)*P).T
#U = sqrtm((n+kappa)*P).T

sigmas[0] = x
sigmas[1:n+1]     = x + U
sigmas[n+1:2*n+2] = x - U

return sigmas

def unscented_transform(Sigmas, Wm, Wc, noise_cov):
""" Computes unscented transform of a set of sigma points and weights.
returns the mean and covariance in a tuple.
"""

kmax, n = Sigmas.shape

# new mean is just the sum of the sigmas * weight
x = dot(Wm, Sigmas)    # dot = \Sigma^n_1 (W[k]*Xi[k])

# new covariance is the sum of the outer product of the residuals
# times the weights
'''P = zeros((n, n))
for k in range(kmax):
y = Sigmas[k] - x
P += Wc[k] * np.outer(y, y)'''

# this is the fast way to do the commented out code above
y = Sigmas - x[np.newaxis,:]
P = y.T.dot(np.diag(Wc)).dot(y)

if noise_cov is not None:
P += noise_cov

return (x, P)