kalmix kalmix logo

A compact, dependency-light R toolkit for state-space modelling: exact linear-Gaussian filtering and smoothing, regime switching, hidden Markov models, change-point detection, and causal read-outs built on the smoother. Imports only S7, stats and cli.

What it does

Example

library(kalmix)

## A mean-reverting spread with a calm and a turbulent half.
set.seed(1)
calm <- as.numeric(arima.sim(list(ar = 0.7), n = 150, sd = 0.4))
wild <- as.numeric(arima.sim(list(ar = 0.7), n = 150, sd = 2))
spread <- c(calm, wild)

model <- ou_fit(spread)             # OU / AR(1) mean-reversion fit
half_life(model)                    # trading half-life, in steps

fit <- kalmix_filter(model, spread) # two-regime mixture Kalman filter
tail(round(fit@regime_prob, 2))     # posterior probability of the wild regime

The vignettes walk through each verb family: Getting started: state-space models with kalmix, Pairs trading with kalmix, Change-points and hidden states with kalmix and Assimilative causal inference with kalmix.

Related packages

Each of these covers one of kalmix's areas in more depth. kalmix puts the linear-Gaussian filter, regime switching, hidden Markov models and change-points behind one small interface with three dependencies, and adds assimilative causal inference and the pairs-trading spread model.

Installation

remotes::install_github("max578/kalmix", build_vignettes = TRUE)

Lineage and credits

The pairs-trading application implements and extends the spread model of Elliott, van der Hoek and Malcolm (2005), Pairs trading, Quantitative Finance 5(3), 271-276. The package is developed with Johannes van der Hoek. The assimilative-causal-inference path follows Andreou, Chen and Bollt (2026), Assimilative causal inference, Nature Communications 17, 1854.

Contributing

Bug reports and suggestions are welcome as GitHub issues.

Citation

citation("kalmix")

Licence

MIT. See LICENSE.