-
-
Notifications
You must be signed in to change notification settings - Fork 342
Feat viterbi #232
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Prathameshk2024
wants to merge
7
commits into
TheAlgorithms:master
from
Prathameshk2024:feat-viterbi
Closed
Feat viterbi #232
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8b30acf
bidirectional_bfs
Prathameshk2024 4921341
viterbi
Prathameshk2024 bc40269
Merge branch 'master' into feat-viterbi
Prathameshk2024 a0236f8
Update dynamic_programming/viterbi.r
Prathameshk2024 e73a1b8
Update dynamic_programming/viterbi.r
Prathameshk2024 9d8d9d3
Update dynamic_programming/viterbi.r
Prathameshk2024 411216b
Update dynamic_programming/viterbi.r
Prathameshk2024 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| # ============================================================== | ||
| # Viterbi Algorithm — Hidden Markov Model (HMM) Decoding | ||
| # ============================================================== | ||
| # | ||
| # Description: | ||
| # The Viterbi algorithm finds the most probable sequence of | ||
| # hidden states (state path) that results in a given sequence of | ||
| # observed events in a Hidden Markov Model. | ||
| # | ||
| # Time Complexity: O(N * T) | ||
| # - N = number of hidden states | ||
| # - T = length of observation sequence | ||
| # | ||
| # Space Complexity: O(N * T) | ||
| # | ||
| # Input: | ||
| # states - vector of hidden states | ||
| # observations - vector of observed symbols | ||
| # start_prob - named vector of initial probabilities (state → prob) | ||
| # trans_prob - matrix of transition probabilities (from_state → to_state) | ||
| # emit_prob - matrix of emission probabilities (state → observation) | ||
| # | ||
| # Output: | ||
| # A list containing: | ||
| # best_path - most probable state sequence | ||
| # best_prob - probability of the best path | ||
| # | ||
| # Example usage provided at bottom of file. | ||
| # ============================================================== | ||
|
|
||
| viterbi <- function(states, observations, start_prob, trans_prob, emit_prob) { | ||
| N <- length(states) | ||
| T_len <- length(observations) | ||
Prathameshk2024 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| # Initialize matrices | ||
| V <- matrix(0, nrow = N, ncol = T_len) # probability table | ||
| path <- matrix(NA, nrow = N, ncol = T_len) # backpointer table | ||
|
|
||
| # Initialization step | ||
| for (i in 1:N) { | ||
| V[i, 1] <- start_prob[states[i]] * emit_prob[states[i], observations[1]] | ||
| path[i, 1] <- 0 | ||
Prathameshk2024 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| # Recursion step | ||
| if (T_len > 1) { | ||
| for (t in 2:T_len) { | ||
| for (j in 1:N) { | ||
| probs <- V[, t - 1] * trans_prob[, states[j]] * emit_prob[states[j], observations[t]] | ||
| V[j, t] <- max(probs) | ||
| path[j, t] <- which.max(probs) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| # Termination step | ||
| best_last_state <- which.max(V[, T_len]) | ||
| best_prob <- V[best_last_state, T_len] | ||
|
|
||
| # Backtrack the best path | ||
| best_path <- rep(NA, T_len) | ||
| best_path[T_len] <- best_last_state | ||
|
|
||
| if (T_len > 1) { | ||
| for (t in (T_len - 1):1) { | ||
| best_path[t] <- path[best_path[t + 1], t + 1] | ||
| } | ||
| } | ||
|
|
||
| best_state_sequence <- states[best_path] | ||
|
|
||
| return(list( | ||
| best_path = best_state_sequence, | ||
| best_prob = best_prob | ||
| )) | ||
| } | ||
|
|
||
| # ============================================================== | ||
| # Example Usage and Test | ||
| # ============================================================== | ||
|
|
||
| if (!exists(".test_mode")) { | ||
| cat("=== Viterbi Algorithm — Hidden Markov Model ===\n") | ||
|
|
||
| # Example: Weather HMM | ||
| # States: Rainy, Sunny | ||
| # Observations: walk, shop, clean | ||
| states <- c("Rainy", "Sunny") | ||
| observations <- c("walk", "shop", "clean") | ||
|
|
||
| # Start probabilities | ||
| start_prob <- c(Rainy = 0.6, Sunny = 0.4) | ||
|
|
||
| # Transition probabilities | ||
| trans_prob <- matrix(c( | ||
| 0.7, 0.3, # from Rainy to (Rainy, Sunny) | ||
| 0.4, 0.6 # from Sunny to (Rainy, Sunny) | ||
| ), nrow = 2, byrow = TRUE) | ||
| rownames(trans_prob) <- states | ||
| colnames(trans_prob) <- states | ||
|
|
||
| # Emission probabilities | ||
| emit_prob <- matrix(c( | ||
| 0.1, 0.4, 0.5, # Rainy emits (walk, shop, clean) | ||
| 0.6, 0.3, 0.1 # Sunny emits (walk, shop, clean) | ||
| ), nrow = 2, byrow = TRUE) | ||
| rownames(emit_prob) <- states | ||
| colnames(emit_prob) <- observations | ||
|
|
||
| # Observed sequence | ||
| obs_seq <- c("walk", "shop", "clean") | ||
|
|
||
| cat("Observation sequence:", paste(obs_seq, collapse = ", "), "\n") | ||
| result <- viterbi(states, obs_seq, start_prob, trans_prob, emit_prob) | ||
|
|
||
| cat("Most probable state sequence:\n") | ||
| cat(paste(result$best_path, collapse = " -> "), "\n") | ||
| cat("Probability of this sequence:", result$best_prob, "\n") | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.