SMALLTALK LAB
Viterbi decoding in Squeak: follow the most likely path
Decode a hidden state sequence from four visible signals. Keep rival paths alive with exact probability scores and use backpointers to recover the winner.
Before you begin
Use native Squeak 6.1 and open a Workspace. Select and Print it on one complete snippet at a time. Each snippet includes its own sample tables and assumes valid probability tables and at least one observation. Fractions are exact in this small example.
These are native Squeak 6.1 examples. Each checkpoint is independent: open the download as text, copy one complete expression into Workspace and choose Print it. Do not file in the checkpoint collection as a class definition.
Expected values are Smalltalk expressions. The printer may display equivalent Smalltalk literals with different spaces or delimiters.
01 - Read the model tables
An imaginary controller has hidden modes A and B (indices 1 and 2) and emits signals X and Y (also indices 1 and 2). We see X Y Y Y. The initial prior is [A,B]; transition rows are previous state and columns next state. Emission rows are state and columns signal. Every probability row sums to 1.
| observed start move emit |
observed := #(1 2 2 2).
start := {3/5. 2/5}.
move := {{9/10. 1/10}. {1/10. 9/10}}.
emit := {{1/2. 1/2}. {1/4. 3/4}}.
{observed. start. move. emit}Expected: {#(1 2 2 2). {3/5. 2/5}. {{9/10. 1/10}. {1/10. 9/10}}. {{1/2. 1/2}. {1/4. 3/4}}}

Try it: B stays B with probability 9/10 and emits Y with probability 3/4.
02 - Score the first signal
The first signal uses one prior and one emission, with no transition. A leads B three to one, but do not discard B.
| observed start move emit first |
observed := #(1 2 2 2).
start := {3/5. 2/5}.
move := {{9/10. 1/10}. {1/10. 9/10}}.
emit := {{1/2. 1/2}. {1/4. 3/4}}.
first := (1 to: start size) collect: [:j |
(start at: j) * ((emit at: j) at: observed first)].
first asArrayExpected: {3/10. 1/10}

Try it: A scores (3/5) × (1/2), and B scores (2/5) × (1/4).
03 - Compare both incoming paths into B
For the first Y, B is better reached from B than from A. The result shows both candidates and the winning predecessor index. Keeping the best path ending in each state is enough: all futures from the same state multiply both prefixes by the same factors.
| observed start move emit first fromA fromB emission |
observed := #(1 2 2 2).
start := {3/5. 2/5}.
move := {{9/10. 1/10}. {1/10. 9/10}}.
emit := {{1/2. 1/2}. {1/4. 3/4}}.
first := (1 to: start size) collect: [:j |
(start at: j) * ((emit at: j) at: observed first)].
emission := (emit at: 2) at: (observed at: 2).
fromA := (first at: 1) * ((move at: 1) at: 2) * emission.
fromB := (first at: 2) * ((move at: 2) at: 2) * emission.
{fromA. fromB. 2}Expected: {9/400. 27/400. 2}

Try it: A globally leads at this time, but B still has a different best predecessor.
04 - Fill all score and predecessor rows
At each time, compare every predecessor for each destination. Store the highest joint path score and the index that produced it. Zero in the first predecessor row means there is no previous state. A strict greater-than comparison makes the lower scanned index win a tie.
| observed start move emit n scores back |
observed := #(1 2 2 2).
start := {3/5. 2/5}.
move := {{9/10. 1/10}. {1/10. 9/10}}.
emit := {{1/2. 1/2}. {1/4. 3/4}}.
n := start size.
scores := Array new: observed size. back := Array new: observed size.
scores at: 1 put: ((1 to: n) collect: [:j |
(start at: j) * ((emit at: j) at: observed first)]) asArray.
back at: 1 put: (Array new: n withAll: 0).
2 to: observed size do: [:t | | prev row pointers |
prev := scores at: t - 1.
row := Array new: n. pointers := Array new: n.
1 to: n do: [:j | | best winner emission |
best := -1. winner := 1.
emission := (emit at: j) at: (observed at: t).
1 to: n do: [:i | | candidate |
candidate := (prev at: i) * ((move at: i) at: j) * emission.
candidate > best ifTrue: [best := candidate. winner := i]].
row at: j put: best. pointers at: j put: winner].
scores at: t put: row. back at: t put: pointers].
{scores. back}Expected: {{{3/10. 1/10}. {27/200. 27/400}. {243/4000. 729/16000}. {2187/80000. 19683/640000}}. #((0 0) (1 2) (1 2) (1 2))}

Try it: B only overtakes A in the final score row.
05 - Follow backpointers from the final winner
Choose B in the final column, then follow the stored predecessor one time step at a time. The output is B B B B with joint score 19683/640000. This is not a normalized posterior; the denominator P(observations) is common to every candidate and is not needed to choose a winner.
| observed start move emit n scores back last bestScore path |
observed := #(1 2 2 2).
start := {3/5. 2/5}.
move := {{9/10. 1/10}. {1/10. 9/10}}.
emit := {{1/2. 1/2}. {1/4. 3/4}}.
n := start size.
scores := Array new: observed size. back := Array new: observed size.
scores at: 1 put: ((1 to: n) collect: [:j |
(start at: j) * ((emit at: j) at: observed first)]) asArray.
back at: 1 put: (Array new: n withAll: 0).
2 to: observed size do: [:t | | prev row pointers |
prev := scores at: t - 1.
row := Array new: n. pointers := Array new: n.
1 to: n do: [:j | | best winner emission |
best := -1. winner := 1.
emission := (emit at: j) at: (observed at: t).
1 to: n do: [:i | | candidate |
candidate := (prev at: i) * ((move at: i) at: j) * emission.
candidate > best ifTrue: [best := candidate. winner := i]].
row at: j put: best. pointers at: j put: winner].
scores at: t put: row. back at: t put: pointers].
last := 1.
2 to: n do: [:j |
(scores last at: j) > (scores last at: last) ifTrue: [last := j]].
bestScore := scores last at: last.
path := Array new: observed size.
observed size to: 1 by: -1 do: [:t |
path at: t put: last.
t > 1 ifTrue: [last := (back at: t) at: last]].
{path. bestScore}Expected: {#(2 2 2 2). 19683/640000}

Try it: The path starts in B, even though A scored higher at time one.
06 - Watch a greedy decoder commit too early
This decoder picks the best immediate extension of one path. It starts in A and becomes committed to staying A. Its score 2187/80000 equals 17496/640000, less than the Viterbi winner.
| observed start move emit previous path score |
observed := #(1 2 2 2).
start := {3/5. 2/5}.
move := {{9/10. 1/10}. {1/10. 9/10}}.
emit := {{1/2. 1/2}. {1/4. 3/4}}.
previous := 0. score := 1.
path := observed collect: [:signal | | best winner |
best := -1. winner := 1.
1 to: start size do: [:j | | prior candidate |
prior := previous = 0
ifTrue: [start at: j]
ifFalse: [(move at: previous) at: j].
candidate := prior * ((emit at: j) at: signal).
candidate > best ifTrue: [best := candidate. winner := j]].
previous := winner. score := score * best. winner].
{path. score}Expected: {#(1 1 1 1). 2187/80000}

Try it: At a Y, staying A has local weight 9/20; switching to B has weight 3/40.
07 - Check every complete path with an exhaustive oracle
The oracle branches into both states for each of four signals. It never merges prefixes and checks all 16 complete paths, independently confirming both the winning path and its score for this model.
| observed start move emit path bestPath best tries visit |
observed := #(1 2 2 2).
start := {3/5. 2/5}.
move := {{9/10. 1/10}. {1/10. 9/10}}.
emit := {{1/2. 1/2}. {1/4. 3/4}}.
path := Array new: observed size.
best := -1. tries := 0.
visit := [:t :previous :score |
t > observed size
ifTrue: [
tries := tries + 1.
score > best ifTrue: [best := score. bestPath := path copy]]
ifFalse: [
1 to: start size do: [:j | | prior emission |
prior := previous = 0
ifTrue: [start at: j]
ifFalse: [(move at: previous) at: j].
emission := (emit at: j) at: (observed at: t).
path at: t put: j.
visit value: t + 1 value: j value: score * prior * emission]]].
visit value: 1 value: 0 value: 1.
{tries. bestPath. best}Expected: {16. #(2 2 2 2). 19683/640000}

Try it: Each saved candidate is copied because the working path is overwritten during recursion.
Replay the checkpoints

Reason about correctness and limits
Keep a maximum per ending state, not a sum. Summing incoming paths answers a different question. Picking the highest score independently at each time is not traceback.
For T observations and N states, the dense decoder runs O(TN²) score operations with O(TN) stored cells. The oracle has N^T leaves. Exact fractions are useful for the workshop but grow; long practical decoders normally compare sums of log probabilities and represent zero probability as negative infinity.
The worked model has a positive winning score. If a model allows zeros and every final score is zero, report no possible path instead of presenting the tie-selected zero-score path as a valid explanation.
Three practice challenges
Stop the signal early
Change the observations to X Y Y. Which path now wins?
Show solution
A A A, scored 243/4000. The decisive last Y was removed.
Try one signal
Decode a single X. How many pointers do you follow?
Show solution
A wins with 3/10. There is no prior state and no pointer to follow.
Count the next oracle
With two states and six observations, how many complete candidates does the exhaustive oracle score?
Show solution
2 raised to 6, or 64. Contrast the six rows of the dynamic program.
Reference
Continue learning
Probability Lab | Edit Distance | Weighted Interval Scheduling
Environment reference: official Squeak documentation. This lesson and its examples are original. Verified using native Squeak 6.1 build 23976 on Linux.