SIMD Kalman

repository·master·Indexed 41 days ago

https://github.com/oseiskar/simdkalman

A high-performance Python library for running multiple independent Kalman filters in parallel using data vectorization. It provides the `KalmanFilter` class for smoothing and predicting data, as well as the `simdkalman.primitives` module for low-level computation steps. Designed for speed, it significantly outperforms libraries like pykalman and filterpy when processing many time series simultaneously.

Tokens
1.6K
Snippets
4
Records
8
Agent score
51%

What's inside SIMD Kalman

  1. Overview of SIMD Kalman

    master

    SIMD Kalman is a Python library designed for fast Kalman filtering by leveraging single-instruction multiple-data (SIMD) vectorization. It is optimized for running n similar Kalman filters on n independent series of observations simultaneously.

    Note: In this context, SIMD refers to data vectorization (running multiple filters in parallel) rather than specific CPU SIMD processor instructions.

  2. Use simdkalman for Kalman filtering

    master

    SIMD Kalman provides fast Kalman filters by leveraging single-instruction multiple-data vectorization. This allows you to run n similar Kalman filters on n independent series of observations simultaneously.

    Key capabilities:

    • Smoothing: Use .smooth(data) to smooth and explain existing data.
    • Prediction: Use .predict(data, steps) to predict new data for a specified number of steps.

    Performance note: When vectorized over many independent timeseries, it can be up to 100x faster than pykalman and 70x faster than filterpy.

    import simdkalman
    import numpy as np
    
    kf = simdkalman.KalmanFilter(
        state_transition = np.array([[1,1],[0,1]]),
        process_noise = np.diag([0.1, 0.01]),
        observation_model = np.array([[1,0]]),
        observation_noise = 1.0)
    
    data = np.random.normal(size=(200, 1000))
    
    # smooth and explain existing data
    smoothed = kf.smooth(data)
    
    # predict new data
    pred = kf.predict(data, 15)
  3. Apply a prediction step to multiple states using `predict`

    master

    The predict function performs the Kalman filter prediction step for one or more states simultaneously. To leverage SIMD-like performance, you can stack multiple states, transition matrices, and process noise into single arrays.

    When stacking:

    • States (mean): Should be stacked into a shape of (N, dim, 1), where N is the number of states and dim is the state dimension.
    • Transition Matrices (state_transition): Should be stacked into a shape of (N, dim, dim).
    • Covariance (covariance): Can be a single matrix of shape (dim, dim) applied to all states, or a stacked array of shape (N, dim, dim).
    • Process Noise (process_noise): Can be a single matrix of shape (dim, dim) applied to all states, or a stacked array of shape (N, dim, dim).
    from simdkalman.primitives import predict
    import numpy
    
    # different states (3 states of dimension 2)
    m1, m2, m3 = [numpy.array([[0],[i]]) for i in range(3)]
    # with the same covariance (initially)
    P = numpy.eye(2)
    
    # different transition matrices
    A1, A2, A3 = [numpy.eye(2)*i for i in range(3)]
    # same process noise
    Q = numpy.eye(2)*0.01
    
    # stack correctly
    m = numpy.vstack([
        m1[numpy.newaxis, ...],
        m2[numpy.newaxis, ...],
        m3[numpy.newaxis, ...]
    ])
    A = numpy.vstack([
        A1[numpy.newaxis, ...],
        A2[numpy.newaxis, ...],
        A3[numpy.newaxis, ...]
    ])
    
    # predict
    m, P = predict(m, P, A, Q)
    
    print(m.shape) # Expected: (3, 2, 1)
    print(P.shape) # Expected: (3, 2, 2)
  4. Use low-level primitives for custom Kalman filters

    master
    The simdkalman.primitives module provides low-level Kalman filter computation steps designed for multi-dimensional input arrays. This module is useful for implementing specialized filters, such as Extended Kalman Filters (EKF).
  5. Use `simdkalman.primitives` functions

    master

    The simdkalman.primitives module provides the core mathematical operations for Kalman filtering and smoothing. These functions are designed to work with stacked arrays for efficient batch processing.

    Available functions:

    • predict(mean, covariance, state_transition, process_noise): Performs the prediction step.
    • update(mean, covariance, observation_model, observation_noise): Performs the update step.
    • update_with_nan_check(mean, covariance, observation_model, observation_noise): Performs the update step with NaN handling.
    • smooth(mean, covariance, state_transition, observation_model, observation_noise): Performs the smoothing step.
    • predict_observation(mean, covariance, observation_model, observation_noise): Predicts the observation given the current state.
  6. Initialize KalmanFilter

    master

    The KalmanFilter class is the primary interface for performing filtering operations. It requires defining the system dynamics and noise parameters.

    Parameters:

    • state_transition: The matrix defining how the state evolves.
    • process_noise: The covariance matrix of the process noise.
    • observation_model: The matrix mapping the state to the observations.
    • observation_noise: The scalar or matrix representing observation noise.
    import simdkalman
    import numpy as np
    
    kf = simdkalman.KalmanFilter(
        state_transition = np.array([[1,1],[0,1]]),
        process_noise = np.diag([0.1, 0.01]),
        observation_model = np.array([[1,0]]),
        observation_noise = 1.0)