Simultaneous Kalman Filters

1.0.4 · active · verified Fri Apr 17

simdkalman provides Kalman filters vectorized as Single Instruction, Multiple Data, enabling efficient processing of multiple independent time series simultaneously. It is designed for performance when dealing with large numbers of parallel Kalman filter estimations. The library is actively maintained and currently at version 1.0.4, with a stable release cadence focused on performance and bug fixes.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a `KalmanFilter` instance and use it to smooth a batch of time series data. It generates synthetic 1D random walk data for multiple series, then applies a simple Kalman filter model. Crucially, input observations are reshaped to the expected `(n_series, n_timesteps, n_features)` format.

import numpy as np
import simdkalman

# Generate some synthetic data for 3 series, 50 time steps each
n_series = 3
n_timesteps = 50
rand = np.random.RandomState(0)
true_states = np.cumsum(rand.normal(0, 0.1, size=(n_series, n_timesteps)), axis=1)
observations = true_states + rand.normal(0, 1, size=(n_series, n_timesteps))

# Initialize a Kalman Filter
# Simple 1-D random walk model:
#   state_transition = [[1]] (state at t is state at t-1)
#   process_noise = [[0.1]] (noise in state transition)
#   observation_model = [[1]] (observation is directly the state)
#   observation_noise = [[1.0]] (noise in observation)
kf = simdkalman.KalmanFilter(
    state_transition = np.array([[1]]), # 1D state, previous state is current state
    process_noise = np.diag([0.1]), # Variance of 0.1 for state evolution
    observation_model = np.array([[1]]), # Observation is the state itself
    observation_noise = np.diag([1.0]) # Observation noise variance of 1.0
)

# Smooth the observations
# `observations` needs to be 3D: (n_series, n_timesteps, n_features)
# Here n_features is 1 for 1D data.
smoothed_states = kf.smooth(observations[:, :, np.newaxis]).states.mean

print(f"Smoothed states shape: {smoothed_states.shape}")
# Expected: (n_series, n_timesteps, 1)

# You can also predict new values
predicted_states = kf.predict(observations[:, :, np.newaxis], n_test=10).states.mean
print(f"Predicted states shape: {predicted_states.shape}")

view raw JSON →