Package: kalmantv
kalmantv: High-Performance Kalman Filtering and Smoothing in PythonMohan Wu, Martin LysyDescriptionkalmantvprovides a simple Python interface to the time-varying Kalman filtering and smoothing algorithms.  Various low-level backends are provided in the following modules:kalmantv.cython: This module performs the underlying linear algebra using the BLAS/LAPACK routines provided by NumPy through a Cython interface.  To maximize speed, no input checks are provided.  All inputs must befloat64NumPy arrays inFortranorder.kalmantv.eigen: This module uses the C++ Eigen library for linear algebra.  The interface is also through Cython.  Here again we have the same input requirements and lack of checks.  Eigen is known to be faster than most BLAS/LAPACK implementations, but it needs to be compiled properly to achieve maximum performance.  In particular this involves linking against an installed version of Eigen (not provided) and setting the right compiler flags for SIMD and OpenMP support.  Some defaults are provided insetup.py, but tweaks may be required depending on the user's system.kalmantv.numba: This module once again uses BLAS/LAPACK but the interface is through Numba.  Here input checks are performed and the inputs can be in either C or Fortran order, and single or double precision (float32andfloat64).  However, C ordered arrays are first converted to Fortran order, so the latter is preferable for performance considerations.Installationgitclonehttps://github.com/mlysy/kalmantvcdkalmantv
pipinstall.Unit TestingThe unit tests are done against thepykalmanlibrary to ensure the same results.  From inside the root folder of thekalmantvsource code:pipinstall.[tests]toxDocumentationThe HTML documentation can be compiled from thekalmantvroot folder:pipinstall.[docs]cddocs
makehtmlThis will create the documentation indocs/build.UsageThe usage of the library can be demonstrated through this simple example.  Here we use theKalmanTVclass from thekalmantv.cythonmodule.  The same class is defined inkalmantv.eigenandkalmantv.numbawith exactly the same methods and signatures.Suppose we start with given mean and variance of the state variablemu_state_pastandvar_state_past, our goal is to computemu_state_filtandvar_state_filtat the next time step. We definemu_state_predandvar_state_predfor the prediction step in the Kalman filtering algorithm. More details can be found in the documentation.importnumpyasnpfromkalmantv.cythonimportKalmanTVn_meas=2# Set the size of the measurementn_state=4# Set the size of the state# Set initial mu and var of the state variablemu_state_past=np.random.rand(n_state)var_state_past=np.random.rand(n_state,n_state)var_state_past=var_state_past.dot(var_state_past.T)#Ensure positive semidefinitevar_state_past=np.asfortranarray(var_state_past)# must use Fortran order# Parameters to the Kalman Filtermu_state=np.random.rand(n_state)wgt_state=np.random.rand(n_state,n_state).T# converts to Fortran ordervar_state=np.random.rand(n_state,n_state)var_state=var_state.dot(var_state.T).Tx_meas=np.random.rand(n_meas)mu_meas=np.random.rand(n_meas)wgt_meas=np.random.rand(n_state,n_meas).Tvar_meas=np.random.rand(n_meas,n_meas)var_meas=var_meas.dot(var_meas.T).T# Initialize the KalmanTV classktv=KalmanTV(n_meas,n_state)# Allocate memory for storing the outputmu_state_pred=np.empty(n_state)var_state_pred=np.empty((n_state,n_state),order='F')mu_state_filt=np.empty(n_state)var_state_filt=np.empty((n_state,n_state),order='F')# Run the filtering algorithmktv.filter(mu_state_pred,var_state_pred,mu_state_filt,var_state_filt,mu_state_past,var_state_past,mu_state,wgt_state,var_state,x_meas,mu_meas,wgt_meas,var_meas)