Hidden Markov Model (Gaussian)
Overview
The Hidden Markov Model (HMM) is a sequence-aware classifier that uses temporal continuity to improve predictions. Unlike frame-based classifiers (Random Forest, Naive Bayes, Logistic Regression) that classify each frame independently, the HMM considers what state the system was in at the previous frame when predicting the current frame.
This makes HMMs ideal for detecting behavioral states that persist over time — for example, periods of whisker contact vs. non-contact, where transitions between states are rare relative to the frame rate.
How It Works
The HMM assumes your data is generated by a system that:
- Has a fixed number of hidden states (e.g., “Contact” and “No Contact”)
- Transitions between states according to learned probabilities (e.g., 95% chance of staying in the same state)
- Emits observable features (e.g., curvature, distance) drawn from a Gaussian distribution specific to each state
Training learns these transition probabilities and per-state Gaussian distributions from labeled data. Prediction then finds the most likely sequence of hidden states given new observations.
Parameters
| Parameter | Default | Description |
|---|---|---|
| Number of States | 2 | Number of hidden states. Use 2 for binary classification tasks. |
| Tolerance | 1e-5 | Convergence tolerance for parameter estimation |
| Diagonal Covariance | Off | Use diagonal covariance matrices for emissions (see below) |
Diagonal Covariance
By default the HMM fits a full covariance matrix for each state’s Gaussian emission distribution. When diagonal covariance is enabled (checkbox in Model Configuration), each state stores only a variance per feature instead of a full covariance matrix. This:
- Reduces parameters dramatically — from \(M(M+1)/2\) to \(M\) per state (where \(M\) is the number of features)
- Improves numerical stability when training data is limited relative to the number of features
- Assumes conditional independence of features given the hidden state
Enable this option when you have many features relative to your training data, or if training with full covariance fails or produces unstable results.
When to Use
- Your classification target is a temporal state that persists across consecutive frames
- Frame-based classifiers produce “noisy” predictions with many rapid state switches
- You have labeled event data (
DigitalEventSeries) marking state occurrences - You want smoother predictions that respect temporal coherence
Label Sources
The HMM supports all label sources available in the ML Workflow widget. The most natural pairing is From Events, which converts point events to binary state labels:
| Label Source | How It Works |
|---|---|
| From Events (recommended for HMM) | Frames with a matching event → state 1; all others → state 0 |
| From Intervals | Frames inside the interval → positive class; outside → negative class |
| From Groups | Each entity group maps to a class label |
| From Data Entity Groups | Like Groups, but scoped to a specific data key |
To use event-based labels, select the “From Events” radio button in the Label Configuration panel and choose a DigitalEventSeries from the dropdown.
Sequence Segmentation
The HMM automatically detects temporal gaps in your feature data and splits it into contiguous sequences. For example, if your feature tensor covers frames 100–200 and 500–600, the HMM treats these as two separate sequences rather than assuming a continuous transition from frame 200 to frame 500.
Sequences shorter than 2 frames are discarded (a single frame carries no temporal information for an HMM).
Output
The HMM produces the same outputs as other classifiers:
| Output | Type | Description |
|---|---|---|
| State intervals | DigitalIntervalSeries |
One series per state (e.g., "HMM:Contact", "HMM:NoContact") |
| State probabilities | AnalogTimeSeries |
Per-frame probability of each state (if enabled) |
| Transition matrix | Displayed in Results panel | Learned state-to-state transition probabilities |
Transition Matrix
After training, the Results panel shows the learned transition matrix. For a 2-state model:
→ State 0 → State 1
State 0: 0.97 0.03
State 1: 0.05 0.95
This means: if the system is in State 0, there is a 97% chance it stays in State 0 at the next frame, and a 3% chance it transitions to State 1. High diagonal values indicate states that persist over many frames.
Tips
- Start with 2 states for binary classification. Only increase the state count if you know your data has more distinct behavioral modes.
- The HMM works best when states are temporally persistent — if your target state changes every few frames, a frame-based classifier may perform equally well.
- Z-score normalization is recommended, as Gaussian emissions are sensitive to feature scaling.
- If training fails, ensure your training data contains examples of all states and includes at least a few state transitions.
- Compare HMM predictions against Random Forest to see if temporal smoothing improves your results.
- Enable diagonal covariance if you have many features but limited training data.
Prediction Regions
By default, the HMM predicts on all feature rows. You can restrict predictions to a specific frame range by selecting a DigitalIntervalSeries in the Prediction panel. When a prediction region is selected:
- The HMM still runs on the full temporal range covering both your training labels and the prediction region (so it retains temporal context at boundaries)
- Output is filtered to only write results for the frames you selected
This is useful when you’ve labeled parts of your recording and want to predict just the unlabeled gap, while still benefiting from the HMM’s awareness of the labeled regions on either side.
Constrained Decoding (Warm-Start)
When a prediction region is adjacent to labeled training data, the HMM can warm-start predictions using the known state at the boundary. For example, if your training labels end at frame 1000 with state “Contact”, the HMM starts predicting frame 1001 from that known state rather than guessing.
This is controlled by the “Constrained decoding” checkbox in the Prediction panel (visible only when using HMM). It is enabled by default and generally improves prediction accuracy at region boundaries.
Constrained decoding has no effect on frame-independent classifiers (Random Forest, Naive Bayes, Logistic Regression).
Comparison with Frame-Based Classifiers
| Aspect | Frame-Based (e.g., Random Forest) | HMM |
|---|---|---|
| Temporal context | None — each frame classified independently | Considers previous state via transition probabilities |
| Prediction smoothness | May produce rapid state switches | Naturally produces smooth state sequences |
| Training data | Shuffled, order does not matter | Order matters — sequences must be temporally contiguous |
| Speed | Fast (independent per frame) | Moderate (Viterbi decoding, Forward-Backward) |
| Best for | Features that are strongly discriminative on their own | States that persist over time with noisy features |