rlabbe / filterpy

Python Kalman filtering and optimal estimation library. Implements Kalman filter, particle filter, Extended Kalman filter, Unscented Kalman filter, g-h (alpha-beta), least squares, H Infinity, smoothers, and more. Has companion book 'Kalman and Bayesian Filters in Python'.
MIT License
3.23k stars 614 forks source link

Code block for rts_smoother is wrong #232

Open rlabbe opened 3 years ago

rlabbe commented 3 years ago

In kalman_filter.py there is a code-block in the docstring reading:

        (mu, cov, _, _) = kf.batch_filter(zs, Fs=Fs)
        (xs, Ps, Ks) = kf.rts_smoother(mu, cov, Fs=Fs)

However, rts_smoother returns 4 lists, not 3.

Also, get rid of those silly parens on the tuples!

Eheran1 commented 2 years ago

Also, this Line here is wrong: Fs = [np.array([[1., dt], [0, 1]] for dt in dts]

Not only is there a missing ")", but also it throws errors:

VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.
Fs = [np.array([[1., dt], [0., 1.]], dtype=object) for dt in dts]
  Input In [2] in <module>
    runfile('C:/Users/me/Desktop/Code/KF-3.12-tempdaten.py', wdir='C:/Users/me/Desktop/Code')
  File C:\ProgramData\Anaconda3\lib\site-packages\debugpy\_vendored\pydevd\_pydev_bundle\pydev_umd.py:167 in runfile
    execfile(filename, namespace)
  File C:\ProgramData\Anaconda3\lib\site-packages\debugpy\_vendored\pydevd\_pydev_imps\_pydev_execfile.py:25 in execfile
    exec(compile(contents + "\n", file, 'exec'), glob, loc)
  File C:/Users/me/Desktop/Code/KF-3.12-tempdaten.py:154 in <module>
    (mu, cov, _, _) = f.batch_filter(zs, Fs=Fs)
  File C:\ProgramData\Anaconda3\lib\site-packages\filterpy\kalman\kalman_filter.py:911 in batch_filter
    self.update(z, R=R, H=H)
  File C:\ProgramData\Anaconda3\lib\site-packages\filterpy\kalman\kalman_filter.py:545 in update
    self.SI = self.inv(self.S)
  File <__array_function__ internals>:5 in inv
  File C:\ProgramData\Anaconda3\lib\site-packages\numpy\linalg\linalg.py:545 in inv
    ainv = _umath_linalg.inv(a, signature=signature, extobj=extobj)
UFuncTypeError: Cannot cast ufunc 'inv' input from dtype('O') to dtype('float64') with casting rule 'same_kind'

This also happens with: Fs = [np.array([[1., dt], [0., 1.]], dtype=object) for dt in dts]

What should Fs look like to get it working? I have a np.array of my dts. Ah never mind, like this:

Fs = []
for t in range(0, len(mytime)):
        Fs.append(np.array([[1., dts[t], [0., 1.]]))
Ms, _, _, _ = f.rts_smoother(mu, cov, Fs=Fs)