Skip to main content

Neural Hidden Semi-Markov Model (NHSMM) implementation in PyTorch

Project description

NHSMM — Neural Hidden Semi-Markov Models


⚠️ Alpha stage — NHSMM is a proof-of-concept and actively evolving. Public APIs may change before stable 1.0.0.

NHSMM is a modular PyTorch library for context-aware sequential modeling, forming the foundation of the State Aware Engine (SAE). NHSMM-INTERFACES defines domain-level contracts for integrating NHSMM in diverse systems.

Designed for developers, data scientists, and system integrators, NHSMM enables rapid understanding, deployment, and extension of latent state models for domains such as finance, IoT, robotics, health, and cybersecurity.

PyPI License: Apache-2.0 Python Version


🌟 Highlights

  • Neural HSMM — integrates Hidden Semi-Markov Models with neural parameterization for expressive latent dynamics.
  • Context-Aware Modulation — initial, transition, duration, and emission distributions adapt to external covariates.
  • Flexible Architectures — supports hierarchical and hybrid models.
  • PyTorch & GPU Ready — scalable multi-domain deployment.
  • Modular Foundation — for research, experimentation, and production-ready sequence models.

🧩 Overview

NHSMM explicitly models:

  • Context-Dependent State Durations — variable dwell-times per hidden state influenced by covariates.
  • Context-Dependent Transitions — dynamic transition probabilities adapting to time-varying features.

Suitable for non-stationary, heterogeneous, and time-aware sequences across real-world applications.


NHSMM Project Covenant

  1. Forever Open Corenhsmm remains fully open-source and actively maintained.
  2. No Hidden Dependencies — core library uses only open components; experimental modules (nhsmm-interfaces) are optional.
  3. Transparent Evolution — research previews and pre-release interfaces are clearly marked.
  4. Community Respect — contributions are acknowledged; experimental previews may close but knowledge remains accessible.
  5. Clear Upgrade Path — experimental work informs SAE; core NHSMM is stable and independent.

🚀 Key Features

  • Contextual HSMM — dynamic modulation of initial, transition, duration, and emission probabilities.
  • Duration Models — explicit, context-aware state dwell-times.
  • Emission Models — Gaussian, Student-t, or discrete outputs; differentiable and context-aware.
  • Transition Models — learnable, covariate-aware with gating and temperature scaling; supports low-rank factorization.
  • Hybrid HSMM-HMM Inference — forward-backward and Viterbi adapted for neural latent states.
  • Subclassable Distributions — extend Initial, Duration, Transition, Emission modules.
  • Differentiable Training — gradient-based optimization, temperature annealing, neural modulation.
  • Neural Context Encoders — CNN, LSTM, or hybrid encoders for time-varying covariates.
  • GPU-Ready — fully batched operations.
  • Multi-Domain Applicability — finance, IoT, robotics, health, cybersecurity.
  • Extensible Architecture — foundation for SAE interfaces, API integration, and research projects.
  • Hybrid Update Modes — neural gradient-based updates, optional alternative schemes.

⚡ Performance & Scalability

  • Vectorized forward-backward for batched likelihood computation.
  • Optional low-rank transitions for large state spaces.
  • Supports long sequences efficiently.
  • Memory-efficient Viterbi optimized for GPU.
  • Handles variable-length sequences with padding and masking.

📌 Milestones

Stage Status Notes
Proof of Concept ✅ Done Alpha release (0.0.1-alpha)
Testing/Enhancement ⚠️ Todo Improve performance, extend API
Production Release ⚠️ Todo Stable 1.0.0 release with documentation

📦 Installation

🔹 From PyPI (alpha stage)

pip install nhsmm

🔹 From Source (recommended)

git clone https://github.com/awa-si/NHSMM.git
cd NHSMM
pip install -e .

Editable mode allows modification and testing without reinstalling.


🧠 Usage Example — Market Regime Detection

See: State Occupancy & Duration/Transition Diagnostics

Works similarly for IoT signals, health telemetry, robotics, or cybersecurity logs.

from nhsmm.models import HSMM
model = HSMM(n_states=3, n_features=5)
seq_features, canonical = model.encoder.encode(sequences)

🔍 Conceptual Flow

External Input → Neural Initial Module (π)
                → Neural Transition Module (A)
                → Neural Duration Module (D)
                → Emission Module (Gaussian/Student-t/Discrete)
                → Forward-Backward / Viterbi → Backprop
  • External Input: features, covariates, embeddings.
  • Neural Modules: context-conditioned initial, transition, duration, and emission distributions.
  • Inference: latent states inferred via forward-backward and Viterbi.
  • Backpropagation: updates all neural modules jointly.

🌐 Multi-Domain Applicability

  • Security & Cyber-Physical Systems: anomaly and hidden state detection.
  • Finance & Trading: regime detection, forecasting, adaptive strategies.
  • IoT & Industrial: predictive maintenance, fault detection.
  • Health & Wearables: activity and state tracking, multimodal fusion.
  • Robotics: behavior monitoring, safe human-robot interaction.
  • Telecommunications & Energy: latent state monitoring, resource optimization.
  • Research & AI: temporal modeling, neural-probabilistic experiments.

⚙️ Development

Contributions welcome! Bug reports, feature suggestions, or documentation improvements strengthen NHSMM.

git clone https://github.com/awa-si/NHSMM.git
cd NHSMM
pip install -e ".[dev]"
pytest -v
black nhsmm
ruff check nhsmm

Support

Development is supported via GitHub Sponsors, Patreon, Medium. See FUNDING.md for details.


🧾 License

Apache License 2.0 © 2024 AWA.SI Full terms: LICENSE

If used in academic work, please cite the repository.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nhsmm-0.0.4a0.tar.gz (80.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nhsmm-0.0.4a0-py3-none-any.whl (38.6 kB view details)

Uploaded Python 3

File details

Details for the file nhsmm-0.0.4a0.tar.gz.

File metadata

  • Download URL: nhsmm-0.0.4a0.tar.gz
  • Upload date:
  • Size: 80.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.11

File hashes

Hashes for nhsmm-0.0.4a0.tar.gz
Algorithm Hash digest
SHA256 8c2efd921de93338186aec5ec17ad212a000f5abc18b5d0ff216889c3ced1399
MD5 b05fd83c68e8d6f725c837069827b7b1
BLAKE2b-256 a9f48fa92d6f3ab6179692dc9f92edad7a2616636e41c3322c406aaa3f6c0e02

See more details on using hashes here.

File details

Details for the file nhsmm-0.0.4a0-py3-none-any.whl.

File metadata

  • Download URL: nhsmm-0.0.4a0-py3-none-any.whl
  • Upload date:
  • Size: 38.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.11

File hashes

Hashes for nhsmm-0.0.4a0-py3-none-any.whl
Algorithm Hash digest
SHA256 729fc2ae3022e42e1bd7c48530a9afccf58077c23b580e9f13175cb50e4eeb2b
MD5 c02cea1d5c4f7a403735deb57ce2f7ff
BLAKE2b-256 a2e4c373a456d04ec9f273c34311210b44b5c68cc780af0b0013ccff7669a723

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page