Example #1
0
        public static void CheckBaumWelch()
        {
            // 状态转移矩阵
            Double[,] A =
            {
                { 0.500, 0.250, 0.250 },
                { 0.375, 0.125, 0.375 },
                { 0.125, 0.675, 0.375 }
            };

            // 混淆矩阵
            Double[,] B =
            {
                { 0.60, 0.20, 0.15, 0.05 },
                { 0.25, 0.25, 0.25, 0.25 },
                { 0.05, 0.10, 0.35, 0.50 }
            };

            // 初始概率向量
            Double[] PI = { 0.63, 0.17, 0.20 };

            // 观察序列
            Int32[] OB = { (Int32)Seaweed.Dry, (Int32)Seaweed.Damp, (Int32)Seaweed.Soggy, (Int32)Seaweed.Dryish, (Int32)Seaweed.Dry };

            // 初始化HMM模型
            HMM hmm = new HMM(A.GetLength(0), B.GetLength(1));

            // 数组克隆,避免损坏原始数据
            hmm.A  = (Double[, ])A.Clone();
            hmm.B  = (Double[, ])B.Clone();
            hmm.PI = (Double[])PI.Clone();

            // 前向-后向算法
            Console.WriteLine("------------Baum-Welch算法-----------------");
            Double LogProbInit, LogProbFinal;
            Int32  Iterations = hmm.BaumWelch(OB, out LogProbInit, out LogProbFinal);

            Console.WriteLine("迭代次数 = {0}", Iterations);
            Console.WriteLine("初始概率 = {0}", Math.Exp(LogProbInit));
            Console.WriteLine("最终概率 = {0}", Math.Exp(LogProbFinal));
            Console.WriteLine();

            // 打印学习后的模型参数
            Console.WriteLine("新的模型参数:");
            Console.WriteLine("PI");
            for (Int32 i = 0; i < hmm.N; i++)
            {
                if (i == 0)
                {
                    Console.Write(hmm.PI[i].ToString("0.000"));
                }
                else
                {
                    Console.Write(" " + hmm.PI[i].ToString("0.000"));
                }
            }
            Console.WriteLine();
            Console.WriteLine();

            Console.WriteLine("A");
            for (Int32 i = 0; i < hmm.N; i++)
            {
                for (Int32 j = 0; j < hmm.N; j++)
                {
                    if (j == 0)
                    {
                        Console.Write(hmm.A[i, j].ToString("0.000"));
                    }
                    else
                    {
                        Console.Write(" " + hmm.A[i, j].ToString("0.000"));
                    }
                }
                Console.WriteLine();
            }
            Console.WriteLine();

            Console.WriteLine("B");
            for (Int32 i = 0; i < hmm.N; i++)
            {
                for (Int32 j = 0; j < hmm.M; j++)
                {
                    if (j == 0)
                    {
                        Console.Write(hmm.B[i, j].ToString("0.000"));
                    }
                    else
                    {
                        Console.Write(" " + hmm.B[i, j].ToString("0.000"));
                    }
                }
                Console.WriteLine();
            }
            Console.WriteLine();
        }