Principle see an article in the previous chapter
Using system;using system.collections.generic;using system.linq;using system.text;namespace Viterbi{class Program { const int m = 2; Implied state number const int n = 3; Observed state number static int[] Sarray = new Int[n]; Implied state sequence static int[] Oarray = {0, 1, 2}; Observable state sequence static double[] IArray = {0.6, 0.4};//initial probability matrix static double[,] Aarray = {{0.7, 0.3}, {0.4, 0. 6}};//transfer probability matrix static double[,] Barray = {{0.5, 0.4, 0.1}, {0.1, 0.3, 0.6}};//emission probability matrix static double[,] Resultarray = new Double[n, m];//save result static void Main (string[] args) {Viterbi (Sarray, Oarray, IArray, Aarray, Barray); foreach (int i in Sarray) {Console.Write (i); } console.readkey (); } static void Viterbi (int[] s, int[] o, double[] I, double[,] A, double[,] b) {for (int j = 0; j < m; J + +) {resultarray[0, j] = Iarray[j] * Barray[j, oarray[0]]; } for (int j = 1; j < N; j + +) {for (int p = 0; p < m; p++) { for (int k = 0; k < m; k++) {Double tmp = resultarray[j-1 , p] * aarray[p, K] * barray[k, oarray[j]; if (tmp > Resultarray[j, K]) resultarray[j, K] = tmp; }}} for (int j = 0; J < N; j + +) {Double tmp = Resulta Rray[j, 0]; SARRAY[J] = 0; for (int p = 0; p < m; p++) {if (Resultarray[j, p] > tmp) SARRAY[J] = p; } } } }}
The results of the operation are as follows:
Enter an observable sequence of states: 012
Implied state sequence: 001
C # Implementation of HMM's Viterbi algorithm