Kalman filtering and smoothing in Rust

Overview

Kalman filtering and smoothing library written in Rust

Build Status License: GPL v3

Access documentation for the library here. Library is also referenced in Cargo index.

Currently, library provides only time-invariant linear Kalman filtering and smoothing technique is known as fixed-interval smoothing (Rauch-Tung-Striebel smoother) which relies on Kalman filter estimates for the entire dataset. linearkalman relies on rulinalg library to implement linear algebra structures and operations and so input data is expected to be a std::vec::Vec of Vector objects, i.e. a vector of vectors.

In order to use this library, make sure your Cargo.toml file contains the following:

[dependencies]
linearkalman = "0.1.3"

Library can then be imported using:

extern crate linearkalman;

Example

Below example assumes 3-dimensional measurement data with an underlying 2-dimensional state space model. With the help of a few macros from rulinalg, a simple attempt to use the library to run Kalman filter and smoother would be as follows.

#[macro_use]
extern crate rulinalg;
extern crate linearkalman;

use rulinalg::vector::Vector;
use linearkalman::KalmanFilter;

fn main() {

  let kalman_filter = KalmanFilter {
    // Process noise covariance
    q: matrix![1.0, 0.1;
               0.1, 1.0],
    // Measurement noise matrix
    r: matrix![1.0, 0.2, 0.1;
               0.2, 0.8, 0.5;
               0.1, 0.5, 1.2],
    // Observation matrix
    h: matrix![1.0, 0.7;
               0.5, 0.7;
               0.8, 0.1],
    // State transition matrix
    f: matrix![0.6, 0.2;
               0.1, 0.3],
    // Initial guess for state mean at time 1
    x0: vector![1.0, 1.0],
    // Initial guess for state covariance at time 1
    p0: matrix![1.0, 0.0;
                0.0, 1.0],
  };

  let data: Vec<Vector<f64>> = vec![vector![1.04, 2.20, 3.12],
                                    vector![1.11, 2.33, 3.34],
                                    vector![1.23, 2.21, 3.45]];

  let run_filter = kalman_filter.filter(&data);
  let run_smooth = kalman_filter.smooth(&run_filter.0, &run_filter.1);

  // Print filtered and smoothened state variable coordinates
  println!("filtered.1,filtered.2,smoothed.1,smoothed.2");
  for k in 0..3 {
      println!("{:.6},{:.6},{:.6},{:.6}",
               &run_filter.0[k].x[0], &run_filter.0[k].x[1],
               &run_smooth[k].x[0], &run_smooth[k].x[1])
  }
}

examples directory contains code sample which allows to import data from a CSV file and returns filtered and smoothed data to stdout.

License

This project is licensed under GPL3.

You might also like...
Distributed compute platform implemented in Rust, and powered by Apache Arrow.
Distributed compute platform implemented in Rust, and powered by Apache Arrow.

Ballista: Distributed Compute Platform Overview Ballista is a distributed compute platform primarily implemented in Rust, powered by Apache Arrow. It

Tensors and differentiable operations (like TensorFlow) in Rust

autograd Differentiable operations and tensors backed by ndarray. Motivation Machine learning is one of the field where Rust lagging behind other lang

A fast, safe and easy to use reinforcement learning framework in Rust.
A fast, safe and easy to use reinforcement learning framework in Rust.

RSRL (api) Reinforcement learning should be fast, safe and easy to use. Overview rsrl provides generic constructs for reinforcement learning (RL) expe

Rust implementation of real-coded GA for solving optimization problems and training of neural networks
Rust implementation of real-coded GA for solving optimization problems and training of neural networks

revonet Rust implementation of real-coded genetic algorithm for solving optimization problems and training of neural networks. The latter is also know

A real-time implementation of
A real-time implementation of "Ray Tracing in One Weekend" using nannou and rust-gpu.

Real-time Ray Tracing with nannou & rust-gpu An attempt at a real-time implementation of "Ray Tracing in One Weekend" by Peter Shirley. This was a per

Tensors and dynamic neural networks in pure Rust.
Tensors and dynamic neural networks in pure Rust.

Neuronika is a machine learning framework written in pure Rust, built with a focus on ease of use, fast prototyping and performance. Dynamic neural ne

A neural network, and tensor dynamic automatic differentiation implementation for Rust.

Corgi A neural network, and tensor dynamic automatic differentiation implementation for Rust. BLAS The BLAS feature can be enabled, and requires CBLAS

K-dimensional tree in Rust for fast geospatial indexing and lookup

kdtree K-dimensional tree in Rust for fast geospatial indexing and nearest neighbors lookup Crate Documentation Usage Benchmark License Usage Add kdtr

Label Propagation Algorithm by Rust. Label propagation (LP) is graph-based semi-supervised learning (SSL). LGC and CAMLP have been implemented.

label-propagation-rs Label Propagation Algorithm by Rust. Label propagation (LP) is graph-based semi-supervised learning (SSL). A simple LGC and a mor

Comments
  • Change interpretation of x0 and P0 inputs

    Change interpretation of x0 and P0 inputs

    x0 and P0 objects in the Kalman filter struct represent a posteriori estimates at time 0, i.e. x0 = x_{0|0} and P0 = P_{0|0}, as described in the Kalman filter wiki page.

    Having tested equivalence of rust-linearkalman output against pykalman module in Python or FKF package in R, it appears that the convention seems to set x0 and P0 as a priori estimates at time 1 based on information at time 0, i.e. x0 = x_{1|0} and P0 = P_{1|0}. If this change is controlled for, then the results coincide. Otherwise, small differences will appear in the beginning of the period and should even out by the end (depending on parameters)

    In order to make rust-linearkalman consistent with other methods, interpreation of x0 and P0 should be changed and documented accordingly.

    opened by rbagd 0
  • Smoothing step takes as input wrong predicted value

    Smoothing step takes as input wrong predicted value

    Smoothing step takes as input filtered[t-k-1] and predicted[t-k-1], i.e. both data points are generated at the same time.

    In reality, filtered[t] corresponds to x_{t|t} whereas predicted[t] corresponds to x_{t|t-1}. Since smoothing algorithm requires x_{t|t} and x_{t+1|t} to be supplied, this would mean that inputs should be filtered[t-k-1] and predicted[t-k] instead.

    opened by rbagd 0
Owner
Rytis Bagdziunas
Rytis Bagdziunas
Rust implementation of user-based collaborative filtering

Rucommender Recommendation system written in Rust Overview An implementation in Rust of a collaborative filtering recommendations algorithm with a use

null 0 Sep 15, 2018
Qdrant - vector similarity search engine with extended filtering support

Vector Similarity Search Engine with extended filtering support Qdrant (read: quadrant ) is a vector similarity search engine. It provides a productio

qdrant 3.5k Dec 30, 2022
Ecosystem of libraries and tools for writing and executing extremely fast GPU code fully in Rust.

Ecosystem of libraries and tools for writing and executing extremely fast GPU code fully in Rust.

Riccardo D'Ambrosio 2.1k Jan 5, 2023
Ecosystem of libraries and tools for writing and executing fast GPU code fully in Rust.

The Rust CUDA Project An ecosystem of libraries and tools for writing and executing extremely fast GPU code fully in Rust Guide | Getting Started | Fe

Rust GPU 2.1k Dec 30, 2022
Robust and Fast tokenizations alignment library for Rust and Python

Robust and Fast tokenizations alignment library for Rust and Python

Yohei Tamura 14 Dec 10, 2022
Narwhal and Tusk A DAG-based Mempool and Efficient BFT Consensus.

This repo contains a prototype of Narwhal and Tusk. It supplements the paper Narwhal and Tusk: A DAG-based Mempool and Efficient BFT Consensus.

Facebook Research 134 Dec 8, 2022
MesaTEE GBDT-RS : a fast and secure GBDT library, supporting TEEs such as Intel SGX and ARM TrustZone

MesaTEE GBDT-RS : a fast and secure GBDT library, supporting TEEs such as Intel SGX and ARM TrustZone MesaTEE GBDT-RS is a gradient boost decision tre

MesaLock Linux 179 Nov 18, 2022
[WIP] An experimental Java-like language and it's virtual machine, for learning Java and JVM.

Sky VM An experimental Java-like language and it's virtual machine, for learning Java and JVM. Dependencies Rust (rust-lang/rust) 2021 Edition, dual-l

Kk Shinkai 2 Jan 3, 2022
Some hacks and failed experiments surrounding nvidia's gamestream protocol and sunshine/moonlight implementations

Sunrise This repository contains a bunch of experiments surrounding the nvidia gamestream protocol and reimplementations in the form of sunshine and m

Victoria Brekenfeld 5 Dec 21, 2022
Msgpack serialization/deserialization library for Python, written in Rust using PyO3, and rust-msgpack. Reboot of orjson. msgpack.org[Python]

ormsgpack ormsgpack is a fast msgpack library for Python. It is a fork/reboot of orjson It serializes faster than msgpack-python and deserializes a bi

Aviram Hassan 139 Dec 30, 2022