Skip to content

Commit

Permalink
Added stats.logpdf. Prepare for release.
Browse files Browse the repository at this point in the history
  • Loading branch information
rlabbe committed Nov 24, 2016
1 parent a5166f3 commit dc2d6cd
Show file tree
Hide file tree
Showing 11 changed files with 1,801 additions and 24 deletions.
3 changes: 3 additions & 0 deletions creating_a_release.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
Steps to Create Release
=======================

* update filterpy/filterpy/__init__.py with the version number.

* update filterpy/filterpy/changelog.txt with the changes for this release.

* 'rm *' in dist
* If necessary, edit filterpy/docs/index.rst to add any classes. Add .rst file for those new classes to the /docs subdirectories.
Expand Down
4 changes: 4 additions & 0 deletions docs/stats/stats.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ to Bayesian filters.

-----

.. autofunction:: logpdf

-----

.. autofunction:: multivariate_gaussian

-----
Expand Down
2 changes: 1 addition & 1 deletion filterpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
for more information.
"""

__version__ = "0.1.4"
__version__ = "0.1.5"
4 changes: 4 additions & 0 deletions filterpy/changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ Version 0.1.5
* Fix #53: UKF rts_smoother does not use residual functions
* Fixed #54: Comments in multivariate_multiply incorrectly called the
covariance the mean.
* Added logpdf to stats. Computes logpdf - mostly a wrapper around
stats.multivariate_normal.logpdf as older versions of that function
do not support the allow_singular keyword. But it also flattens out the
vectors for you so you do not have to do anything special with column vectors.

Version 0.1.4
=============
Expand Down
14 changes: 11 additions & 3 deletions filterpy/kalman/UKF.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@

from filterpy.common import dot3
from filterpy.kalman import unscented_transform
from filterpy.stats import logpdf
import math
import numpy as np
from numpy import eye, zeros, dot, isscalar, outer
from scipy.linalg import inv, cholesky
from scipy.stats import multivariate_normal

class UnscentedKalmanFilter(object):
# pylint: disable=too-many-instance-attributes
Expand Down Expand Up @@ -359,8 +359,16 @@ def update(self, z, R=None, UT=None, hx_args=()):
self.x = self.x + dot(self.K, self.y)
self.P = self.P - dot3(self.K, Pz, self.K.T)

self.log_likelihood = multivariate_normal.logpdf(
x=self.y, mean=np.zeros(len(self.y)), cov=Pz, allow_singular=True)
self.log_likelihood = logpdf(self.y, np.zeros(len(self.y)), Pz)


def cross_variance(self, x, z, sigmas_f, sigmas_h):
Pxz = zeros((sigmas_f.shape[1], sigmas_h.shape[1]))
N = sigmas_f.shape[0]
for i in range(N):
dx = self.residual_x(sigmas_f[i], x)
dz = self.residual_z(sigmas_h[i], z)
Pxz += self.Wc[i] * outer(dx, dz)


@property
Expand Down
21 changes: 5 additions & 16 deletions filterpy/kalman/kalman_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@

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


class KalmanFilter(object):
Expand Down Expand Up @@ -198,12 +198,7 @@ def update(self, z, R=None, H=None):
self._S = S
self._K = K

# compute log likelihood
mean = np.asarray(dot(H, x)).flatten()
flatz = np.asarray(z).flatten()
self.log_likelihood = multivariate_normal.logpdf(
flatz, mean, cov=S, allow_singular=True)

self.log_likelihood = logpdf(z, dot(H, x), S)


def update_correlated(self, z, R=None, H=None):
Expand Down Expand Up @@ -270,11 +265,7 @@ def update_correlated(self, z, R=None, H=None):
self._K = K

# compute log likelihood
mean = np.asarray(dot(H, x)).flatten()
flatz = np.asarray(z).flatten()

self.log_likelihood = multivariate_normal.logpdf(
flatz, mean, cov=S, allow_singular=True)
self.log_likelihood = logpdf(z, dot(H, x), S)


def test_matrix_dimensions(self, z=None, H=None, R=None, F=None, Q=None):
Expand Down Expand Up @@ -897,12 +888,10 @@ def update(x, P, z, R, H=None, return_all=False):
I_KH = np.array(1 - KH)
P = dot3(I_KH, P, I_KH.T) + dot3(K, R, K.T)

# compute log likelihood
mean = np.array(dot(H, x)).flatten()
flatz = np.asarray(z).flatten()
log_likelihood = multivariate_normal.logpdf(flatz, mean, cov=S, allow_singular=True)

if return_all:
# compute log likelihood
log_likelihood = logpdf(z, dot(H, x), S)
return x, P, y, K, S, log_likelihood
else:
return x, P
Expand Down
4 changes: 2 additions & 2 deletions filterpy/kalman/tests/test_enkf.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ def fx(x, dt):

if __name__ == '__main__':
DO_PLOT = True
test_circle ()
#test_1d_const_vel()
#test_circle ()
test_1d_const_vel()


#test_noisy_1d()
Loading

0 comments on commit dc2d6cd

Please sign in to comment.