-
Notifications
You must be signed in to change notification settings - Fork 3
EkfdPredict and GmekfPredict classes and tests #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bschneiderheinze
wants to merge
4
commits into
scope-lab:main
Choose a base branch
from
bschneiderheinze:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
058a27e
Added EkfdUpdate to KalmanFilter.py and GmekfUpdate to Gmkf.py
bschneiderheinze e5a0f3b
Added tests for EkfdPredict and GmekfPredict to test_filters.py, whic…
bschneiderheinze 762f9f4
Added comments defining h function to EkfdUpdate and GmekfUpdate
bschneiderheinze 21b98cc
Fixes and clairfications in response to pull request comments.
bschneiderheinze File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| import numpy as np | ||
| import pyest.gm as pygm | ||
| from pyest.filters import GaussianMixturePredict, GaussianMixtureUpdate | ||
| from pyest.filters import KfdPredict, KfdUpdate, EkfdPredict | ||
| from pyest.filters import KfdPredict, KfdUpdate, EkfdPredict, EkfdUpdate | ||
| from pyest.utils import make_tuple | ||
|
|
||
|
|
||
|
|
@@ -250,3 +250,148 @@ def lin_gauss_likelihood_agreement(z, m, P, H, R, L=None, h_args=()): | |
| h_args = make_tuple(h_args) | ||
| Hk = H(*h_args) | ||
| return pygm.eval_mvnpdf(z, Hk@m, Hk@[email protected] + LRLt) | ||
|
|
||
| class GmekfUpdate(EkfdUpdate, GaussianMixtureUpdate): | ||
| """ Discrete Gaussian Mixture Extended Kalman Filter Update | ||
|
|
||
| Parameters | ||
| ---------- | ||
| h : callable | ||
| measurement function of the form :math:`h(x, ...)` | ||
| H : ndarray or callable | ||
| (nz,nx) measurement Jacobian matrix | ||
| z_k = H(tk, xk, *args) @ x. If provided an ndarray instead, H will | ||
| automatically be recast as a callable. | ||
| R : ndarray | ||
| (ny,ny) measurement noise covariance matrix | ||
| L : (optional) ndarray | ||
| (nz,ny) mapping matrix mapping measurement noise into | ||
| measurement space | ||
| cov_method : (optional) string | ||
| method to use for covariance update. Valid options include 'general' | ||
| (default), 'Joseph', 'standard', and 'KWK'. | ||
|
|
||
| Written by Keith LeGrand, March 2019 | ||
| """ | ||
|
|
||
| def __init__(self, *args, **kwargs): | ||
| super().__init__(*args, **kwargs) | ||
|
|
||
| def __lin_gauss_likelihood_agreement(self, z, zhat, W): | ||
| return pygm.eval_mvnpdf(z, zhat, W) | ||
|
|
||
| def lin_gauss_cond_likelihood_prod(self, m, P, z, h_args=(), interm_vals=False): | ||
| """ compute the product of the linear Gaussian likelihood function and | ||
| another Gaussian pdf | ||
|
|
||
| Parameters | ||
| ---------- | ||
| m : ndarray | ||
| (nx,) prior mean | ||
| P : ndarray | ||
| (nx,nx) prior covariance matrix | ||
| z : ndarray | ||
| (nz,) measurement | ||
| h_args : (optional) tuple | ||
| deterministic parameters to be passed to measurement function | ||
| interm_vals : (optional) Boolean | ||
| if True, returns intermediate values from computation. False by default | ||
|
|
||
| Returns | ||
| ------- | ||
| mp : ndarray | ||
| (nx,) posterior mean | ||
| Pp : ndarray | ||
| (nx,nx) posterior state error covariance | ||
| q : float | ||
| likelihood agreement, :math:`q = N(z; Hm, HPH' + R)` | ||
|
|
||
| If interm_vals is true, additionally returns a dictionary containing: | ||
| W : ndarray | ||
| (nz,nz) innovatations covariance | ||
| C : (ndarray) | ||
| (nx,nz) cross-covariance | ||
| K : (ndarray) | ||
| gain matrix | ||
| zhat : (ndarray) | ||
| predicted measurement | ||
|
|
||
| """ | ||
| mp, Pp, interm = super().update(m, P, z, interm_vals=True, h_args=h_args) | ||
| q = self.__lin_gauss_likelihood_agreement(z, interm['zhat'], interm['W']) | ||
|
|
||
| if not interm_vals: | ||
| return mp, Pp, q | ||
| else: | ||
| return mp, Pp, q, interm | ||
|
|
||
| def cond_likelihood_prod(self, m, P, z, h_args=(), interm_vals=False): | ||
| """ compute the product of the linear Gaussian likelihood function and | ||
| another Gaussian pdf | ||
|
|
||
| Parameters | ||
| ---------- | ||
| m : ndarray | ||
| (nx,) prior mean | ||
| P : ndarray | ||
| (nx,nx) prior covariance matrix | ||
| z : ndarray | ||
| (nz,) measurement | ||
| h_args : (optional) tuple | ||
| deterministic parameters to be passed to measurement function | ||
| interm_vals : (optional) Boolean | ||
| if True, returns intermediate values from computation. False by default | ||
|
|
||
| Returns | ||
| ------- | ||
| mp : ndarray | ||
| (nx,) posterior mean | ||
| Pp : ndarray | ||
| (nx,nx) posterior state error covariance | ||
| q : float | ||
| likelihood agreement, :math:`q = N(z; Hm, HPH' + R)` | ||
|
|
||
| If interm_vals is true, additionally returns a dictionary containing: | ||
| W : ndarray | ||
| (nz,nz) innovatations covariance | ||
| C : ndarray | ||
| (nx,nz) cross-covariance | ||
| K : ndarray | ||
| gain matrix | ||
| zhat : (ndarray) | ||
| predicted measurement | ||
|
|
||
| """ | ||
| return self.lin_gauss_cond_likelihood_prod(m, P, z, h_args=h_args, interm_vals=interm_vals) | ||
|
|
||
| def update(self, pkm, zk, unnormalized=False, h_args=(), *args, **kwargs): | ||
| """ measurement-update of gm | ||
|
|
||
| Parameters | ||
| ---------- | ||
| pkm : GaussianMixture | ||
| prior density at time tk | ||
| zk : ndarray | ||
| (nz,) measurement at time k | ||
| unnormalized : optional | ||
| if True, returns unnormalized distribution. False by default | ||
| h_args : (optional) tuple | ||
| deterministic parameters to be passed to measurement function | ||
| """ | ||
| wkp = np.empty_like(pkm.w, dtype=float) | ||
| mkp = np.empty_like(pkm.m, dtype=float) | ||
| Pkp = np.empty_like(pkm.P, dtype=float) | ||
| for i, (wm,mm,Pm) in enumerate(pkm): | ||
| print(f"updating mixand {i+1} out of {len(pkm)}") | ||
| mkp[i], Pkp[i], q, interm_vals = self.lin_gauss_cond_likelihood_prod(mm, Pm, zk, h_args=h_args, interm_vals=True) | ||
| print(f"Q values: {q}") | ||
| print(f"true z: {zk}") | ||
| print(f"z_hat: {interm_vals['zhat']}") | ||
| # print(f"W: {interm_vals["W"]}") | ||
| # TODO: test (z-zhat).T @ inv(W) @ (z-zhat) | ||
| wkp[i] = wm*q | ||
|
|
||
| if not unnormalized: | ||
| wkp /= np.sum(wkp) | ||
|
|
||
| return pygm.GaussianMixture(wkp, mkp, Pkp) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.