{"id":286,"date":"2026-11-13T12:36:59","date_gmt":"2026-11-13T04:36:59","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-the-viterbi-algorithm-decodes-a-hidden-path\/"},"modified":"2026-11-13T21:58:33","modified_gmt":"2026-11-13T13:58:33","slug":"how-the-viterbi-algorithm-decodes-a-hidden-path","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-the-viterbi-algorithm-decodes-a-hidden-path\/","title":{"rendered":"How the Viterbi algorithm decodes a hidden path"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> The <strong>Viterbi algorithm<\/strong> finds the most probable sequence of hidden states for a sequence of observations in a hidden Markov model. The cell v[t][s] is the probability of the best path that emits the first t observations and ends in state s. It multiplies the best predecessor by the transition into s and by the emission of observation t from s. The time is <strong>O(T \u00b7 S\u00b2)<\/strong> for T observations and S states. In practice the recursion is computed in <strong>log space<\/strong> so the product does not underflow.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the problem actually asks<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">A hidden Markov model generates a state path by a Markov chain and emits an observation from each state. The joint probability of a particular path and the observed sequence is a product of transitions and emissions. The number of paths is S^T. The best path that ends in state s at time t must arrive from some state r at time t\u22121, and the prefix up to r must itself have been the best way to reach r. That is the DP.<\/p>\n\n\n<p class=\"wp-block-paragraph\">Multiplying hundreds of probabilities underflows double precision. Adding logarithms turns the product into a sum and the algorithm into a longest-path computation on the same graph. The argmax is unchanged.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the algorithm guarantees<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">The maximum over states of v[T][s], times the stop probability if the model has one, is the probability of the most probable path. A predecessor pointer reconstructs that path. Time is \u0398(T \u00b7 S\u00b2) for a dense transition matrix, and less when many transitions are structurally zero. Extra memory is the table, or two columns plus a predecessor matrix if the path is required. The algorithm returns the single best path, not the posterior probability of a state at time t. That posterior is the forward-backward algorithm.<\/p>\n\n\n<p class=\"wp-block-paragraph\">A tie between paths is broken by the argmax convention. The algorithm assumes the Markov property and the given emission model. It does not learn the probabilities. Baum-Welch, an expectation-maximization procedure, estimates parameters.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>The working steps<\/strong><\/h4>\n\n<ol class=\"wp-block-list\">\n<li>Convert every transition and emission probability to a log, or guard the code against underflow. Set the time-1 column from the initial-state distribution times the first emission.<\/li>\n<li>For each later time t and each state s, set v[t][s] to the emission of the current observation from s, times the maximum over r of v[t\u22121][r] times the transition r\u2192s.<\/li>\n<li>Store the argmax r as the predecessor of (t, s).<\/li>\n<li>At time T, choose the best final state. If the model has an end marker, include the transition into the end state.<\/li>\n<li>Walk predecessors backward to recover the path, then reverse it.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">Viterbi is the maximum-probability path, a decoding. Forward-backward is the sum over paths, a posterior. Mixing them returns a probability mass from a procedure that was asked for a path, or a path from a procedure that summed. The quadratic dependence on S is the dense case. A chain with a banded transition structure should iterate only the legal edges.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Approaches that look simpler and fail<\/strong><\/h4>\n\n<figure class=\"wp-block-table\">\n<table>\n<thead>\n<tr>\n<th>Approach<\/th>\n<th>Why it fails when the forward probabilities are normalized as if they were Viterbi<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Sum over predecessors instead of maximizing<\/td>\n<td>The sum is the forward algorithm. It computes the total probability of the observations, not the best path.<\/td>\n<\/tr>\n<tr>\n<td>Multiply raw probabilities for a long sequence<\/td>\n<td>The product underflows to zero and the argmax becomes noise. Logarithms keep the same argmax in range.<\/td>\n<\/tr>\n<tr>\n<td>Normalize each column and still report the final number as a path probability<\/td>\n<td>Normalization changes the value. The path recovered from predecessors can stay correct if the scale is identical for every state, and the reported probability is then wrong unless the scales are saved.<\/td>\n<\/tr>\n<tr>\n<td>Use the decoded path as the posterior marginal of a state<\/td>\n<td>The best path can disagree with the individually most likely state at a time. Marginals come from forward-backward.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">Posterior state probabilities are forward-backward. Parameter learning is Baum-Welch. Viterbi is the max-product decoding of a hidden Markov model.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>A working example<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">This Python fragment keeps the best predecessor, which is Viterbi, not the sum of paths. Two states, emissions that prefer state 0 on <code>a<\/code> and state 1 on <code>b<\/code>, decode <code>ab<\/code> as <code>0 1<\/code>.<\/p>\n\n<pre><code class=\"language-python\">states = [0, 1]\nemit = {0: {&quot;a&quot;: 0.8, &quot;b&quot;: 0.2}, 1: {&quot;a&quot;: 0.2, &quot;b&quot;: 0.8}}\ntrans = [[0.5, 0.5], [0.5, 0.5]]\nobs = &quot;ab&quot;\nn = len(obs)\ndp = [[0.0] * 2 for _ in range(n)]\nback = [[0] * 2 for _ in range(n)]\nfor s in states:\n    dp[0][s] = 0.5 * emit[s][obs[0]]\nfor t in range(1, n):\n    for s in states:\n        options = [(dp[t - 1][p] * trans[p][s], p) for p in states]\n        best, prev = max(options)\n        dp[t][s] = best * emit[s][obs[t]]\n        back[t][s] = prev\npath = [max(states, key=lambda s: dp[-1][s])]\nfor t in range(n - 1, 0, -1):\n    path.append(back[t][path[-1]])\npath.reverse()\nassert path == [0, 1]\nprint(*path)\n<\/code><\/pre>\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Where it is used<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">A <strong>Viterbi<\/strong> decoder is the standard decoder for convolutional codes in telecommunications, including GSM and deep-space links. The same dynamic program decodes a hidden Markov model in speech and in part-of-speech tagging. Beam search keeps fewer candidates.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>When you can drop this algorithm<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Drop Viterbi when the quantity required is a marginal or a total likelihood. Use the forward recursions. Keep Viterbi when the output is one most probable state sequence, and compute it in log space.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Why this stays published<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">We publish this so a search for the Viterbi algorithm returns the max-product recurrence and the reason it is computed in log space. <strong>The Viterbi algorithm recovers the most probable hidden-state path in O(T \u00b7 S\u00b2) time by keeping, for each time and state, the best predecessor times the emission.<\/strong><\/p>\n\n\n<p class=\"wp-block-paragraph\">\u672c\u6587\u7531 HoHo \u8207 AI \u5354\u4f5c\u6574\u7406\uff0c\u6700\u5f8c\u66f4\u65b0\u65bc 2026 \u5e74 11 \u6708 13 \u65e5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Short answer: The Viterbi algorithm finds the most probable sequence of hidden states for a sequence of observations in a hidden Markov model. The cell v[t][s] is the probability of the best path that emits the first t observations and ends in state s. It multiplies the best predecessor&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,33],"tags":[],"class_list":["post-286","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-dynamic-programming"],"_links":{"self":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/286","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/comments?post=286"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/286\/revisions"}],"predecessor-version":[{"id":685,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/286\/revisions\/685"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=286"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=286"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=286"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}