rhmm - Rust Hidden Markov Models
A Rust library for Hidden Markov Models (HMM), inspired by Python's hmmlearn. This library provides efficient implementations of various HMM models and algorithms using ndarray for numerical computations.
π Features
-
Multiple HMM Model Types
- Gaussian HMM: Models continuous data with Gaussian emission distributions
- Beta HMM: Models data in the range [0, 1] (e.g., conversion rates, proportions)
- Multinomial HMM(Not implemented yet, coming soon): Models discrete categorical data
- Gaussian Mixture Model HMM (GMM-HMM) (Not implemented yet, coming soon): Models complex continuous distributions
-
Standard HMM Algorithms
- Forward Algorithm: Compute observation probabilities
- Backward Algorithm: Compute backward probabilities
- Viterbi Algorithm: Find the most likely state sequence
- Baum-Welch Algorithm: Train model parameters using EM
-
Efficient Implementation
- Built on
ndarrayfor fast numerical operations - Support for multiple covariance types (diagonal, spherical, full, tied)
- Robust numerical stability with log-space computations
- Built on
π¦ Installation
Add this to your Cargo.toml:
[]
= "0.0.1"
Or install from source:
π§ Dependencies
ndarray- N-dimensional arraysndarray-linalg- Linear algebra operationsrand- Random number generationrand_distr- Probability distributionsthiserror- Error handlingserde- Serialization support
π Quick Start
Gaussian HMM Example
use array;
use GaussianHMM;
use HiddenMarkovModel;
Beta HMM Example (Conversion Rate Analysis)
use array;
use BetaHMM;
use HiddenMarkovModel;
π― Use Cases
Gaussian HMM
- Speech Recognition: Model acoustic features
- Financial Markets: Detect market regimes (bull/bear)
- Sensor Data: Analyze time-series sensor readings
- Bioinformatics: Gene sequence analysis
Beta HMM
- E-commerce: Conversion rate analysis
- Marketing: Click-through rate modeling
- Finance: Market share dynamics
- Quality Control: Success rate tracking
Multinomial HMM
- Natural Language Processing: Part-of-speech tagging
- Bioinformatics: DNA sequence modeling
- User Behavior: Clickstream analysis
- Weather Modeling: Discrete weather states
π API Overview
Core Trait: HiddenMarkovModel
All HMM models implement this trait:
Model Types
GaussianHMM
let model = new;
let model = with_covariance_type;
BetaHMM
let model = new;
MultinomialHMM
let model = new;
GaussianMixtureHMM
let model = new;
π¬ Examples
Run the included examples:
# Beta HMM example (conversion rate analysis)
# Polars integration example
π§ͺ Testing
Run the test suite:
# Run all tests
# Run with output
# Run specific test
π Performance
The library is optimized for performance:
- Uses
ndarrayfor efficient numerical operations - Log-space computations for numerical stability
- Vectorized operations where possible
- Minimal allocations in hot paths
π οΈ Advanced Usage
Multiple Sequences
Train on multiple sequences of different lengths:
let observations = array!;
let lengths = vec!; // Length of each sequence
model.fit.unwrap;
Custom Initialization
let mut model = new;
// Set custom initial parameters before fitting
// model.set_start_prob(...);
// model.set_transition_matrix(...);
model.fit.unwrap;
Covariance Types
use CovarianceType;
// Diagonal covariance (default)
let model = with_covariance_type;
// Spherical covariance (single variance)
let model = with_covariance_type;
// Full covariance matrix
let model = with_covariance_type;
π€ Contributing
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
π License
This project is licensed under the MIT License - see the LICENSE file for details.
π Acknowledgments
π§ Contact
- Author: YanXu Fu
- Email: unico-serein@hotmail.com
- GitHub: @unico-serein
πΊοΈ Roadmap
- Add more emission distributions (Poisson, Exponential)
- Implement parallel training for multiple sequences
- Add model serialization/deserialization
- Improve documentation with more examples
- Add benchmarks
- Support for GPU acceleration
Star β this repository if you find it helpful!