Nasty.Statistics.SequenceLabeling.Viterbi (Nasty v0.3.0)
View SourceViterbi algorithm for sequence labeling with linear-chain CRFs.
Finds the most likely label sequence given feature weights and transition scores using dynamic programming.
Algorithm
- Initialize scores for first position
- For each subsequent position:
- Compute emission score (from features)
- Compute transition score (from previous label)
- Keep track of best previous label (backpointer)
- Backtrack from best final label to reconstruct sequence
Complexity
Time: O(n × L²) where n = sequence length, L = number of labels Space: O(n × L)
Summary
Functions
Computes backward probabilities (for training).
Decodes the most likely label sequence using Viterbi algorithm.
Computes emission score for a label given features.
Computes forward probabilities (for training).
Computes partition function Z(x) for normalization.
Computes transition score between two labels.
Types
Functions
@spec backward_probabilities([feature_vector()], map(), map(), [label()]) :: map()
Computes backward probabilities (for training).
Returns map of {position, label} → backward probability.
@spec decode([feature_vector()], map(), map(), [label()], keyword()) :: {:ok, label_sequence(), score()}
Decodes the most likely label sequence using Viterbi algorithm.
Parameters
feature_sequence- List of feature vectors (one per token)feature_weights- Map of feature → label → weighttransition_weights- Map of {prev_label, curr_label} → weightlabels- List of all possible labelsopts- Options::log_domain- Use log probabilities (default: true)
Returns
{:ok, label_sequence, score} - Best label sequence and its score
@spec emission_score(feature_vector(), atom(), map(), boolean()) :: score()
Computes emission score for a label given features.
Sum of all feature weights for features present in the feature vector.
@spec forward_probabilities([feature_vector()], map(), map(), [label()]) :: map()
Computes forward probabilities (for training).
Used in CRF training to compute feature expectations.
Returns map of {position, label} → forward probability.
@spec partition_function(map(), [label()], non_neg_integer()) :: float()
Computes partition function Z(x) for normalization.
Z(x) = sum over all possible label sequences of exp(score(x, y))
Computes transition score between two labels.