Skip to content
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

kalman.py: column vector or row vector? #81

Closed
oyamad opened this issue Sep 11, 2014 · 3 comments
Closed

kalman.py: column vector or row vector? #81

oyamad opened this issue Sep 11, 2014 · 3 comments

Comments

@oyamad
Copy link
Member

oyamad commented Sep 11, 2014

Hi,
in the study group session today, we found some unexpected behavior in the script given in the solution to Exercise 3 in "A First Look at the Kalman Filter".
I think I identified the problem: kn.current_x_hat has to be "flattened":

    # Wrong
    e1[t] = np.sum((x - kn.current_x_hat)**2)
    # Correct
    e1[t] = np.sum((x - kn.current_x_hat.flatten())**2)

kn.current_x_hat is a column vector (2x1), while x is a row vector (1x2), so x - kn.current_x_hat becomes 2x2, and hence np.sum((x - kn.current_x_hat)**2) gets doubled.

Here's how we got the "unexpected behavior":
http://nbviewer.ipython.org/gist/oyamad/1f9b66fda3679f85f794
(Please see the second half part starting with "Exercise 3".)

One may say that the problem is caused by the implementation design of Kalman, where current_xhat is implemented as a column vector. Mathematically it is legitimate, but in python we do not have to care in many cases as numpy guesses correctly to compute e.g. np.dot(A, x) even if x is a row vector. It might be better to consistently stick to row vectors.

@albop
Copy link
Contributor

albop commented Sep 12, 2014

Could you define current_x_hat as a 1d vector instead ? Then it would be broadcasted to a 2x1 array or 1x2 array depending on the orientation of x. Would that work ?

To me, one good feature of numpy is that it has 1d vectors, while in matlab everything is a matrix by default. For the dot operator, it works likewise: provided that the dimensions match you can write dot(A,x) or dot(x,A) without caring whether x is a vector or a linear form.

@oyamad
Copy link
Member Author

oyamad commented Sep 12, 2014

@albop As you suggest, I dropped conversion to 2d array in the Kalman class, then the solution script worked as expected without flatten(). (But I only tried one particular case with R = np.zeros((2, 2)) and x_hat = np.array([0, 0]).)
http://nbviewer.ipython.org/gist/oyamad/1f9b66fda3679f85f794/kalman_ex3_2.ipynb

@jstac
Copy link
Contributor

jstac commented Oct 21, 2014

Thanks for letting me know about this error. In the end I just corrected the exercise by flattening the vector ex post. I didn't flatten everything inside Kalman because I want the inner workings to be transparent --- this module is more instructive rather than for heavy duty use.

@jstac jstac closed this as completed Oct 21, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants