Exemple #1
0
        enum Seaweed { Dry, Dryish, Damp, Soggy }; // 观察状态(海藻湿度)

        //static void Main(string[] args)
        //{
        // 测试前向<a href="http://lib.csdn.net/base/datastructure" class='replace_word' title="算法与数据结构知识库" target='_blank' style='color:#df3434; font-weight:bold;'>算法</a>和后向算法
        //CheckForwardAndBackward();
        //Console.WriteLine();

        //// 测试维特比算法
        //CheckViterbi();
        //Console.WriteLine();

        //// 测试HMM学习算法
        //CheckBaumWelch();
        //}

        // 测试前向算法和后向算法
        public static string CheckForwardAndBackward()
        {
            // 状态转移矩阵
            Double[,] A =
            {
                { 0.500, 0.375, 0.125 },
                { 0.250, 0.125, 0.625 },
                { 0.250, 0.375, 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 };

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

            hmm.A  = A;
            hmm.B  = B;
            hmm.PI = PI;

            // 观察序列的概率
            //Console.WriteLine("------------前向算法:双精度运算-----------------");
            Double ProbabilityFor = hmm.Forward(OB);
            //Console.WriteLine("Probability =" + Probability.ToString("0.###E+0"));
            //Console.WriteLine();

            // 观察序列的概率
            //Console.WriteLine("------------后向算法:双精度运算-----------------");
            Double ProbabilityBack = hmm.Backward(OB);

            //Console.WriteLine("Probability =" + Probability.ToString("0.###E+0"));

            return(ProbabilityFor.ToString("0.###E+0") + "|" + ProbabilityBack.ToString("0.###E+0"));
        }