隐马尔科夫模型C#类库调用示例
2011-06-09 21:05
453 查看
测试代码
using System; using Splash; namespace SplashCheck { class TestHMMCS { enum Weather { Sunny, Cloudy, Rainy }; // 隐藏状态(天气) enum Seaweed { Dry, Dryish, Damp, Soggy }; // 观察状态(海藻湿度) static void Main(string[] args) { // 测试前向算法和后向算法 CheckForwardAndBackward(); Console.WriteLine(); // 测试维特比算法 CheckViterbi(); Console.WriteLine(); // 测试HMM学习算法 CheckBaumWelch(); } // 测试前向算法和后向算法 static void 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 Probability = hmm.Forward(OB); Console.WriteLine("Probability =" + Probability.ToString("0.###E+0")); Console.WriteLine(); // 观察序列的概率 Console.WriteLine("------------后向算法:双精度运算-----------------"); Probability = hmm.Backward(OB); Console.WriteLine("Probability =" + Probability.ToString("0.###E+0")); } // 测试维特比算法 static void CheckViterbi() { // 状态转移矩阵 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 = A; hmm.B = B; hmm.PI = PI; // 找出最有可能的隐藏状态序列 Double Probability; Console.WriteLine("------------维特比算法:双精度运算-----------------"); Int32[] Q = hmm.Viterbi(OB, out Probability); Console.WriteLine("Probability =" + Probability.ToString("0.###E+0")); foreach (Int32 Value in Q) { Console.WriteLine(((Weather)Value).ToString()); } Console.WriteLine(); Console.WriteLine("------------维特比算法:对数运算-----------------"); Q = hmm.ViterbiLog(OB, out Probability); Console.WriteLine("Probability =" + Probability.ToString("0.###E+0")); foreach (Int32 Value in Q) { Console.WriteLine(((Weather)Value).ToString()); } } 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(); } } }
相关文章推荐
- 隐马尔科夫模型C#类库调用示例
- C#:全排列类库调用示例
- 用VS2005写一个 C 的类库和用 C#来调用的示例
- 用VS2005写一个 C 的类库和用 C#来调用的示例
- C#拼音类库调用示例
- 拼写检查C#类库调用示例
- Delphi调用C#类库.doc
- C++调用C#dll类库中的方法(非显性COM)
- c#使用反射调用类型成员示例
- C#编程调用Cards.dll实现图形化发牌功能示例
- C++调用C#类库 &amp;&amp; 非托管C++字符串和托管字符串的转换
- 使用 HttpWebRequest 类调用 WEB 服务的示例(C#) 转
- C# 委托的三种调用示例(同步调用 异步调用 异步回调)
- JAVA通过JNI调用C#dll方法说明(包含示例)
- 关于C++调用C#语言DLL(类库)
- C# 调用 Delphi Dll链接库方法及示例
- C#异步调用示例详解
- C# 创建、部署和调用WebService简单示例
- C++调用C#类库; 非托管C++字符串和托管字符串的转换
- java 调用 c# webservice 压缩 List<string>示例