Skip to content

Repository files navigation

bayes-hdc — probabilistic hyperdimensional computing in JAX

Tests Coverage PyPI DOI Python License

Docs · Colab quickstart · Examples · Benchmarks · Discussions

Hyperdimensional computing is fast, noise-robust, and edge-friendly — but its predictions are raw similarity scores with no notion of confidence. bayes-hdc fixes that: calibrated probabilities, conformal prediction sets, and anomaly detection with a guaranteed false-positive rate, all in JAX.

pip install bayes-hdc

Anomaly detection with a guaranteed false-positive rate

Fit on normal data only. Flag outliers at a false-positive rate that holds by theorem, not by threshold tuning — finite-sample, distribution-free.

Conformal anomaly detection: empirical false-positive rate tracks the target alpha

import numpy as np
from bayes_hdc.sklearn import HDAnomalyDetector

rng = np.random.default_rng(0)
X_normal = 5.0 + rng.normal(size=(500, 16))          # sensors around a setpoint
X_test   = np.vstack([5.0 + rng.normal(size=(50, 16)),   # healthy
                      rng.normal(size=(50, 16))])        # signal dropout

det = HDAnomalyDetector(alpha=0.05).fit(X_normal)
labels = det.predict(X_test)        # +1 inlier / -1 outlier; marginal FP rate <= alpha
pvals  = det.score_samples(X_test)  # split-conformal p-values

Against IsolationForest, LOF, and OneClassSVM it takes the best AUROC on two of three one-class benchmarks — while holding the false-positive rate at target, a knob none of those baselines have. The JAX-native pipeline (custom encoders, batch FDR control) is in tutorials/02_anomaly_detection.py.

Calibrated probabilities and prediction sets

Hypervectors carry distributions (GaussianHV, DirichletHV) with closed-form moment propagation. Any classifier's outputs wrap into temperature-scaled probabilities and conformal sets:

from bayes_hdc import TemperatureCalibrator, ConformalClassifier

probs = TemperatureCalibrator.create().fit(logits_cal, y_cal).calibrate(logits_test)

conformal = ConformalClassifier.create(alpha=0.1).fit(probs_cal, y_cal)
sets      = conformal.predict_set(probs)        # (n, k) bool mask
coverage  = conformal.coverage(probs, y_test)   # >= 1-alpha in expectation (marginal)

scikit-learn users get the whole thing as a drop-in estimator:

from bayes_hdc.sklearn import HDClassifier

HDClassifier(encoder="kernel").fit(X_train, y_train).predict_proba(X_test)

Benchmarks

Standard HDC datasets, 5 seeds, both encoders given the same bandwidth search on identical splits. Full protocol: BENCHMARKS.md.

Dataset bayes-hdc accuracy TorchHD accuracy (tuned) ECE raw → calibrated Coverage @ α=0.1
ISOLET 0.895 ± 0.004 0.882 ± 0.006 0.845 → 0.022 0.901
UCI-HAR 0.849 ± 0.006 0.871 ± 0.005 0.633 → 0.031 0.904
EMG gestures 0.944 ± 0.014 0.892 ± 0.005 0.618 → 0.045 0.947

Accuracy is competitive — ahead on two, behind on one, printed as-is. The right columns are the point: calibration and coverage the deterministic libraries don't provide. Every number reproduces via make bench-canonical.

In the HDC library landscape

Choose a library by the task and the APIs you need. The links below point to the relevant project documentation.

Library / runtime Use it for Concrete tools
TorchHD
PyTorch
Benchmarking HDC classification pipelines OnlineHD and AdaptHD classifiers; feature encoders; dataset loaders.
hdlib
NumPy
Selecting features for a vector-space classifier Forward/backward feature selection; binary and bipolar Vector / Space APIs.
HoloVec
NumPy; optional PyTorch/JAX
Comparing encoding and retrieval methods Scalar, sequence, and spatial encoders; item stores; resonator cleanup.
vsapy
NumPy
Encoding hierarchical documents or cyclic values CSPvec sequences; JSON encoding; linear and circular number-line encoders.
NengoSPA
Nengo
Simulating symbolic reasoning in spiking neural networks Semantic pointers; associative memories; action selection and routing.
bayes-hdc
JAX
Returning prediction sets, intervals, and anomaly p-values Gaussian/Dirichlet hypervectors; temperature scaling; split-conformal calibration.

Conformal coverage and false-positive bounds require a held-out calibration split and exchangeable calibration/test data. Temperature scaling alone does not provide these guarantees.

Design rationale and per-primitive paper attributions: DESIGN.md · docs/LITERATURE_AUDIT.md.

Examples

emg_gesture_recognition.py sEMG gestures with calibrated per-gesture probabilities
anomaly_detection_intrusion.py network intrusion flags at a guaranteed FP rate
vision_action_policy.py vision-action policy with per-DOF conformal intervals and abstention
kanerva_example.py "What's the Dollar of Mexico?" role-filler analogy

Sixteen more in examples/, two worked tutorials in tutorials/.

Status

Alpha (0.5.0a1); API may shift before 1.0. 666 tests, 93% coverage, CI on Ubuntu + macOS × Python 3.9–3.13: algebraic laws on randomized inputs, gradients vs finite differences, and the coverage/FDR guarantees tested directly. Pure Python on jax + numpy, no compiled extensions. Sharp edges: GPU/TPU tested on CPU CI only; the variational-training API is the most likely to change.

Contributing

Good first issues are scoped and mentored; setup in CONTRIBUTING.md. Questions and show-and-tell: Discussions. If this is useful to you, a star helps others find it.

Citing

@software{bayeshdc2026,
  author  = {Singh, Rajdeep},
  title   = {bayes-hdc: Calibrated, Differentiable Hyperdimensional Computing in JAX},
  url     = {https://github.com/rlogger/bayes-hdc},
  doi     = {10.5281/zenodo.20635099},
  version = {0.5.0a1},
  year    = {2026}
}

Or the "Cite this repository" button (CITATION.cff).

License

MIT. See also: JAX · TorchHD · awesome-jax · Kleyko et al.'s HDC/VSA surveys.

About

Hyperdimensional computing in JAX, with statistical guarantees.

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

33 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages