隐马尔科夫模型C#语言算法实现

来源:互联网 发布:ie浏览器for mac 编辑:程序博客网 时间:2024/05/21 09:02

开发工具:

Visual Studio v2010

.NET Framework 4 Client Profile

版本历史:

V1.1 2011年06月09日

  • 修正UMDHMM在Baum-Welch算法中存在的模型参数调整错误。

 

V1.0 2011年06月08日

  • 将C语言实现的隐马尔科夫模型算法(UMDHMM)改为C#语言实现。

功能描述:

  • 前向算法(forward algorithm):给定HMM求一个观察序列的概率(评估)
  • 后向算法(backward algorithm):给定HMM求一个观察序列的概率(评估)
  • 前向-后向算法(forward-backward algorithm):根据观察序列生成隐马尔科夫模型(学习)
  • 维特比算法(Viterbi algorithm):搜索最有可能生成一个观察序列的隐藏状态序列(解码)

下载地址:

隐马尔科夫模型.zip

源代码:

HMM.cs(类构造函数)

[c-sharp] view plaincopyprint?
  1. /* ---------------------------------------------------------- 
  2. 文件名称:HMM.cs 
  3.  
  4. 作者:秦建辉 
  5.  
  6. MSN:splashcn@msn.com 
  7. QQ:36748897 
  8.  
  9. 版本历史: 
  10.     V1.1    2011年06月09日 
  11.             修正UMDHMM在Baum-Welch算法中存在的模型参数调整错误。 
  12.  
  13.     V1.0    2011年06月08日 
  14.             将C语言实现的隐马尔科夫模型算法(UMDHMM)改为C#语言实现。 
  15.  
  16. 功能描述: 
  17.     1.前向算法(forward algorithm):给定HMM求一个观察序列的概率(评估) 
  18.     2.后向算法(backward algorithm):给定HMM求一个观察序列的概率(评估) 
  19.     3.前向-后向算法(forward-backward algorithm):根据观察序列生成隐马尔科夫模型(学习) 
  20.     4.维特比算法(Viterbi algorithm):搜索最有可能生成一个观察序列的隐藏状态序列(解码) 
  21.  
  22. 参考资料: 
  23.     C代码:http://www.kanungo.com/software/umdhmm-v1.02.zip 
  24.     学习资料:http://www.52nlp.cn/category/hidden-markov-model 
  25.  ------------------------------------------------------------ */  
  26. using System;  
  27.   
  28. namespace Splash  
  29. {  
  30.     public partial class HMM  
  31.     {  
  32.         /// <summary>   
  33.         /// 隐藏状态数目 N   
  34.         /// </summary>   
  35.         public readonly Int32 N;  
  36.   
  37.         /// <summary>   
  38.         /// 观察符号数目 M   
  39.         /// </summary>   
  40.         public readonly Int32 M;   
  41.   
  42.         /// <summary>   
  43.         /// 状态转移矩阵 A   
  44.         /// </summary>   
  45.         public Double[,]  A;  
  46.   
  47.         /// <summary>   
  48.         /// 混淆矩阵(confusion matrix)B   
  49.         /// </summary>   
  50.         public Double[,]  B;  
  51.   
  52.         /// <summary>   
  53.         /// 初始概率向量 PI   
  54.         /// </summary>   
  55.         public Double[]   PI;  
  56.   
  57.         /// <summary>   
  58.         /// 构造函数   
  59.         /// </summary>   
  60.         /// <param name="StatesNum">隐藏状态数目</param>   
  61.         /// <param name="ObservationSymbolsNum">观察符号数目</param>  
  62.         public HMM(Int32 StatesNum, Int32 ObservationSymbolsNum)  
  63.         {  
  64.             N = StatesNum;              // 隐藏状态数目   
  65.             M = ObservationSymbolsNum;  // 观察符号数目  
  66.   
  67.             A = new Double[N, N];   // 状态转移矩阵  
  68.             B = new Double[N, M];   // 混淆矩阵   
  69.             PI = new Double[N];     // 初始概率向量  
  70.         }          
  71.     }  
  72. }  

Viterbi.cs(维特比算法)

[c-sharp] view plaincopyprint?
  1. using System;  
  2.   
  3. namespace Splash  
  4. {  
  5.     partial class HMM  
  6.     {  
  7.         /// <summary>   
  8.         /// 维特比算法:通过给定的观察序列,推算出可能性最大的隐藏状态序列   
  9.         /// Viterbi Algorithm: Finding most probable sequence of hidden states  
  10.         /// </summary>   
  11.         /// <param name="OB">已知的观察序列</param>  
  12.         /// <param name="Probability">可能性最大的隐藏状态序列的概率</param>  
  13.         /// <returns>可能性最大的隐藏状态序列</returns>  
  14.         /// <remarks>使用双精度运算,不输出中间结果</remarks>   
  15.         public Int32[] Viterbi(Int32[] OB, out Double Probability)  
  16.         {  
  17.             Double[,] DELTA;  
  18.             Int32[,] PSI;  
  19.   
  20.             return Viterbi(OB, out DELTA, out PSI, out Probability);  
  21.         }  
  22.   
  23.         /// <summary>   
  24.         /// 维特比算法:通过给定的观察序列,推算出可能性最大的隐藏状态序列   
  25.         /// </summary>   
  26.         /// <param name="OB">已知的观察序列</param>   
  27.         /// <param name="DELTA">输出中间结果:局部最大概率</param>  
  28.         /// <param name="PSI">输出中间结果:反向指针指示最可能路径</param>  
  29.         /// <param name="Probability">可能性最大的隐藏状态序列的概率</param>  
  30.         /// <returns>可能性最大的隐藏状态序列</returns>    
  31.         /// <remarks>使用双精度运算,且输出中间结果</remarks>  
  32.         public Int32[] Viterbi(Int32[] OB, out Double[,] DELTA, out Int32[,] PSI, out Double Probability)  
  33.         {              
  34.             DELTA = new Double[OB.Length, N];   // 局部概率  
  35.             PSI = new Int32[OB.Length, N];      // 反向指针  
  36.   
  37.             // 1. 初始化   
  38.             for (Int32 j = 0; j < N; j++)  
  39.             {  
  40.                 DELTA[0, j] = PI[j] * B[j, OB[0]];  
  41.             }  
  42.   
  43.             // 2. 递归   
  44.             for (Int32 t = 1; t < OB.Length; t++)  
  45.             {  
  46.                 for (Int32 j = 0; j < N; j++)  
  47.                 {  
  48.                     Double MaxValue = DELTA[t - 1, 0] * A[0, j];  
  49.                     Int32 MaxValueIndex = 0;  
  50.                     for (Int32 i = 1; i < N; i++)  
  51.                     {  
  52.                         Double Value = DELTA[t - 1, i] * A[i, j];  
  53.                         if (Value > MaxValue)  
  54.                         {  
  55.                             MaxValue = Value;  
  56.                             MaxValueIndex = i;  
  57.                         }  
  58.                     }  
  59.   
  60.                     DELTA[t, j] = MaxValue * B[j, OB[t]];  
  61.                     PSI[t, j] = MaxValueIndex; // 记录下最有可能到达此状态的上一个状态  
  62.                 }  
  63.             }  
  64.   
  65.             // 3. 终止   
  66.             Int32[] Q = new Int32[OB.Length];   // 定义最佳路径  
  67.             Probability = DELTA[OB.Length - 1, 0];  
  68.             Q[OB.Length - 1] = 0;  
  69.             for (Int32 i = 1; i < N; i++)  
  70.             {  
  71.                 if (DELTA[OB.Length - 1, i] > Probability)  
  72.                 {  
  73.                     Probability = DELTA[OB.Length - 1, i];  
  74.                     Q[OB.Length - 1] = i;  
  75.                 }  
  76.             }  
  77.   
  78.             // 4. 路径回溯   
  79.             for (Int32 t = OB.Length - 2; t >= 0; t--)  
  80.             {  
  81.                 Q[t] = PSI[t + 1, Q[t + 1]];  
  82.             }  
  83.   
  84.             return Q;  
  85.         }  
  86.   
  87.         /// <summary>   
  88.         /// 维特比算法:通过给定的观察序列,推算出可能性最大的隐藏状态序列   
  89.         /// </summary>   
  90.         /// <param name="OB">已知的观察序列</param>   
  91.         /// <param name="Probability">可能性最大的隐藏状态序列的概率</param>  
  92.         /// <returns>可能性最大的隐藏状态序列</returns>   
  93.         /// <remarks>使用对数运算,不输出中间结果</remarks>  
  94.         public Int32[] ViterbiLog(Int32[] OB, out Double Probability)  
  95.         {  
  96.             Double[,] DELTA;  
  97.             Int32[,] PSI;    
  98.   
  99.             return ViterbiLog(OB, out DELTA, out PSI, out Probability);  
  100.         }  
  101.   
  102.         /// <summary>   
  103.         /// 维特比算法:通过给定的观察序列,推算出可能性最大的隐藏状态序列  
  104.         /// </summary>   
  105.         /// <param name="OB">已知的观察序列</param>  
  106.         /// <param name="DELTA">输出中间结果:局部最大概率。结果为自然对数值</param>  
  107.         /// <param name="PSI">输出中间结果:反向指针指示最可能路径</param>  
  108.         /// <param name="Probability">可能性最大的隐藏状态序列的概率</param>  
  109.         /// <returns>可能性最大的隐藏状态序列</returns>   
  110.         /// <remarks>使用对数运算,且输出中间结果</remarks>   
  111.         public Int32[] ViterbiLog(Int32[] OB, out Double[,] DELTA, out Int32[,] PSI, out Double Probability)  
  112.         {              
  113.             DELTA = new Double[OB.Length, N];   // 局部概率  
  114.             PSI = new Int32[OB.Length, N];      // 反向指针  
  115.   
  116.             // 0. 预处理   
  117.             Double[,] LogA = new Double[N, N];  
  118.             for (Int32 i = 0; i < N; i++)  
  119.             {  
  120.                 for (Int32 j = 0; j < N; j++)  
  121.                 {  
  122.                     LogA[i, j] = Math.Log(A[i, j]);  
  123.                 }  
  124.             }  
  125.   
  126.             Double[,] LogBIOT = new Double[N, OB.Length];  
  127.             for (Int32 i = 0; i < N; i++)  
  128.             {  
  129.                 for (Int32 t = 0; t < OB.Length; t++)  
  130.                 {  
  131.                     LogBIOT[i, t] = Math.Log(B[i, OB[t]]);  
  132.                 }  
  133.             }  
  134.   
  135.             Double[] LogPI = new Double[N];  
  136.             for (Int32 i = 0; i < N; i++)  
  137.             {  
  138.                 LogPI[i] = Math.Log(PI[i]);  
  139.             }  
  140.   
  141.             // 1. 初始化   
  142.             for (Int32 j = 0; j < N; j++)  
  143.             {  
  144.                 DELTA[0, j] = LogPI[j] + LogBIOT[j, 0];  
  145.             }  
  146.   
  147.             // 2. 递归   
  148.             for (Int32 t = 1; t < OB.Length; t++)  
  149.             {  
  150.                 for (Int32 j = 0; j < N; j++)  
  151.                 {  
  152.                     Double MaxValue = DELTA[t - 1, 0] + LogA[0, j];  
  153.                     Int32 MaxValueIndex = 0;  
  154.                     for (Int32 i = 1; i < N; i++)  
  155.                     {  
  156.                         Double Value = DELTA[t - 1, i] + LogA[i, j];  
  157.                         if (Value > MaxValue)  
  158.                         {  
  159.                             MaxValue = Value;  
  160.                             MaxValueIndex = i;  
  161.                         }  
  162.                     }  
  163.   
  164.                     DELTA[t, j] = MaxValue + LogBIOT[j, t];  
  165.                     PSI[t, j] = MaxValueIndex; // 记录下最有可能到达此状态的上一个状态  
  166.                 }  
  167.             }  
  168.   
  169.             // 3. 终止   
  170.             Int32[] Q = new Int32[OB.Length];   // 定义最佳路径  
  171.             Probability = DELTA[OB.Length - 1, 0];  
  172.             Q[OB.Length - 1] = 0;  
  173.             for (Int32 i = 1; i < N; i++)  
  174.             {  
  175.                 if (DELTA[OB.Length - 1, i] > Probability)  
  176.                 {  
  177.                     Probability = DELTA[OB.Length - 1, i];  
  178.                     Q[OB.Length - 1] = i;  
  179.                 }  
  180.             }  
  181.   
  182.             // 4. 路径回溯   
  183.             Probability = Math.Exp(Probability);  
  184.             for (Int32 t = OB.Length - 2; t >= 0; t--)  
  185.             {  
  186.                 Q[t] = PSI[t + 1, Q[t + 1]];  
  187.             }  
  188.   
  189.             return Q;  
  190.         }  
  191.     }  
  192. }  

Forward.cs(前向算法)

[c-sharp] view plaincopyprint?
  1. using System;  
  2.   
  3. namespace Splash  
  4. {  
  5.     partial class HMM  
  6.     {  
  7.         /// <summary>   
  8.         /// 前向算法:计算观察序列的概率   
  9.         /// Forward Algorithm: Finding the probability of an observed sequence  
  10.         /// </summary>   
  11.         /// <param name="OB">已知的观察序列</param>  
  12.         /// <returns>观察序列的概率</returns>   
  13.         /// <remarks>使用双精度运算,不输出中间结果</remarks>  
  14.         public Double Forward(Int32[] OB)  
  15.         {  
  16.             Double[,] ALPHA;    // 只声明,不定义   
  17.   
  18.             return Forward(OB, out ALPHA);  
  19.         }  
  20.   
  21.         /// <summary>   
  22.         /// 前向算法:计算观察序列的概率   
  23.         /// </summary>   
  24.         /// <param name="OB">已知的观察序列</param>   
  25.         /// <param name="ALPHA">输出中间结果:局部概率</param>  
  26.         /// <returns>观察序列的概率</returns>   
  27.         /// <remarks>使用双精度运算,输出中间结果</remarks>  
  28.         public Double Forward(Int32[] OB, out Double[,] ALPHA)  
  29.         {              
  30.             ALPHA = new Double[OB.Length, N];   // 局部概率  
  31.   
  32.             // 1. 初始化:计算初始时刻所有状态的局部概率   
  33.             for (Int32 j = 0; j < N; j++)  
  34.             {  
  35.                 ALPHA[0, j] = PI[j] * B[j, OB[0]];  
  36.             }  
  37.   
  38.             // 2. 归纳:递归计算每个时间点的局部概率   
  39.             for (Int32 t = 1; t < OB.Length; t++)  
  40.             {  
  41.                 for (Int32 j = 0; j < N; j++)  
  42.                 {  
  43.                     Double Sum = 0;  
  44.                     for (Int32 i = 0; i < N; i++)  
  45.                     {  
  46.                         Sum += ALPHA[t - 1, i] * A[i, j];  
  47.                     }  
  48.   
  49.                     ALPHA[t, j] = Sum * B[j, OB[t]];  
  50.                 }  
  51.             }  
  52.   
  53.             // 3. 终止:观察序列的概率等于最终时刻所有局部概率之和   
  54.             Double Probability = 0;  
  55.             for (Int32 i = 0; i < N; i++)  
  56.             {  
  57.                 Probability += ALPHA[OB.Length - 1, i];  
  58.             }  
  59.   
  60.             return Probability;  
  61.         }  
  62.   
  63.         /// <summary>   
  64.         /// 带比例因子修正的前向算法:计算观察序列的概率   
  65.         /// </summary>   
  66.         /// <param name="OB">已知的观察序列</param>   
  67.         /// <param name="ALPHA">中间结果:局部概率</param>  
  68.         /// <param name="SCALE">中间结果:比例因子</param>   
  69.         /// <returns>观察序列的概率(自然对数值)</returns>  
  70.         private Double ForwardWithScale(Int32[] OB, ref Double[,] ALPHA, ref Double[] SCALE)  
  71.         {  
  72.             if(ALPHA == null) ALPHA = new Double[OB.Length, N];  
  73.             if(SCALE == null) SCALE = new Double[OB.Length];  
  74.   
  75.             // 1. 初始化   
  76.             SCALE[0] = 0;  
  77.             for (Int32 j = 0; j < N; j++)  
  78.             {  
  79.                 ALPHA[0, j] = PI[j] * B[j, OB[0]];  
  80.                 SCALE[0] += ALPHA[0, j];  
  81.             }  
  82.   
  83.             for (Int32 j = 0; j < N; j++)  
  84.             {  
  85.                 ALPHA[0, j] /= SCALE[0];  
  86.             }  
  87.   
  88.             // 2. 归纳   
  89.             for (Int32 t = 1; t < OB.Length; t++)  
  90.             {  
  91.                 SCALE[t] = 0;  
  92.                 for (Int32 j = 0; j < N; j++)  
  93.                 {  
  94.                     Double Sum = 0;  
  95.                     for (Int32 i = 0; i < N; i++)  
  96.                     {  
  97.                         Sum += ALPHA[t - 1, i] * A[i, j];  
  98.                     }  
  99.   
  100.                     ALPHA[t, j] = Sum * B[j, OB[t]];  
  101.                     SCALE[t] += ALPHA[t, j];  
  102.                 }  
  103.   
  104.                 for (Int32 j = 0; j < N; j++)  
  105.                 {  
  106.                     ALPHA[t, j] /= SCALE[t];  
  107.                 }  
  108.             }              
  109.   
  110.             // 3. 终止   
  111.             Double Probability = 0;  
  112.             for (Int32 t = 0; t < OB.Length; t++)  
  113.             {  
  114.                 Probability += Math.Log(SCALE[t]);  
  115.             }  
  116.   
  117.             return Probability;     // 自然对数值  
  118.         }  
  119.     }  
  120. }  

Backward.cs(后向算法)

[c-sharp] view plaincopyprint?
  1. using System;  
  2.   
  3. namespace Splash  
  4. {  
  5.     partial class HMM  
  6.     {  
  7.         /// <summary>   
  8.         /// 后向算法:计算观察序列的概率   
  9.         /// </summary>   
  10.         /// <param name="OB">已知的观察序列</param>   
  11.         /// <returns>观察序列的概率</returns>   
  12.         public Double Backward(Int32[] OB)  
  13.         {  
  14.             Double[,] BETA;     // 只声明,不定义   
  15.   
  16.             return Backward(OB, out BETA);  
  17.         }  
  18.   
  19.         /// <summary>   
  20.         /// 后向算法:计算观察序列的概率   
  21.         /// </summary>   
  22.         /// <param name="OB">已知的观察序列</param>   
  23.         /// <param name="BETA">中间结果</param>  
  24.         /// <returns>观察序列的概率</returns>   
  25.         public Double Backward(Int32[] OB, out Double[,] BETA)  
  26.         {              
  27.             BETA = new Double[OB.Length, N];  
  28.   
  29.             // 初始化   
  30.             for (Int32 j = 0; j < N; j++)  
  31.             {  
  32.                 BETA[OB.Length - 1, j] = 1.0;  
  33.             }  
  34.   
  35.             // 归纳   
  36.             for (Int32 t = OB.Length - 2; t >= 0; t--)  
  37.             {  
  38.                 for (Int32 j = 0; j < N; j++)  
  39.                 {  
  40.                     Double Sum = 0;  
  41.                     for (Int32 i = 0; i < N; i++)  
  42.                     {  
  43.                         Sum += A[j, i] * B[i, OB[t + 1]] * BETA[t + 1, i];  
  44.                     }  
  45.   
  46.                     BETA[t, j] = Sum;  
  47.                 }  
  48.             }  
  49.   
  50.             // 终止   
  51.             Double Probability = 0;  
  52.             for (Int32 i = 0; i < N; i++)  
  53.             {  
  54.                 Probability += BETA[0, i];  
  55.             }  
  56.   
  57.             return Probability;  
  58.         }  
  59.   
  60.         /// <summary>   
  61.         /// 带比例因子修正的后向算法   
  62.         /// </summary>   
  63.         /// <param name="OB">已知的观察序列</param>  
  64.         /// <param name="SCALE">用于修正的比例因子</param>   
  65.         /// <param name="BETA">中间结果:局部概率</param>  
  66.         private void BackwardWithScale(Int32[] OB, Double[] SCALE, ref Double[,] BETA)  
  67.         {  
  68.             if(BETA == null) BETA = new Double[OB.Length, N];  
  69.   
  70.             // 初始化   
  71.             for (Int32 j = 0; j < N; j++)  
  72.             {  
  73.                 BETA[OB.Length - 1, j] = 1.0 / SCALE[OB.Length - 1];  
  74.             }  
  75.   
  76.             // 归纳   
  77.             for (Int32 t = OB.Length - 2; t >= 0; t--)  
  78.             {  
  79.                 for (Int32 j = 0; j < N; j++)  
  80.                 {  
  81.                     Double Sum = 0;  
  82.                     for (Int32 i = 0; i < N; i++)  
  83.                     {  
  84.                         Sum += A[j, i] * B[i, OB[t + 1]] * BETA[t + 1, i];  
  85.                     }  
  86.   
  87.                     BETA[t, j] = Sum / SCALE[t];  
  88.                 }  
  89.             }  
  90.         }  
  91.     }  
  92. }  

BaumWelch.cs(前向-后向算法)

[c-sharp] view plaincopyprint?
  1. using System;  
  2.   
  3. namespace Splash  
  4. {  
  5.     partial class HMM  
  6.     {  
  7.         /// <summary>   
  8.         /// 前向-后向算法,用于参数学习   
  9.         /// Forward-backward algorithm: Generating a HMM from a sequence of obersvations  
  10.         /// </summary>   
  11.         /// <param name="OB">已知的观察序列</param>          
  12.         /// <param name="LogProbInit">初始自然对数概率</param>   
  13.         /// <param name="LogProbFinal">最终自然对数概率</param>  
  14.         /// <param name="ExitError">迭代中允许的自然对数概率误差,缺省0.001</param>  
  15.         /// <param name="MSP">状态概率最小值,缺省0.001</param>  
  16.         /// <param name="MOP">观察概率最小值,缺省0.001</param>   
  17.         /// <returns>迭代次数</returns>   
  18.         /// <remarks>修正UMDHMM在模型参数调整中的错误</remarks>          
  19.         public Int32 BaumWelch(Int32[] OB, out Double LogProbInit, out Double LogProbFinal,   
  20.             Double ExitError = 0.001, Double MSP = 0.001, Double MOP = 0.001)  
  21.         {  
  22.             Double[,] ALPHA = null;  
  23.             Double[,] BETA = null;              
  24.             Double[] SCALE = null;  
  25.             Double[,] GAMMA = null;  
  26.             Double[, ,] XI = null;  
  27.   
  28.             Double LogProbForward = LogProbInit = ForwardWithScale(OB, ref ALPHA, ref SCALE); // 前向算法  
  29.             BackwardWithScale(OB, SCALE, ref BETA); // 后向算法  
  30.             ComputeGamma(ALPHA, BETA, ref GAMMA);   // 求解各时刻位于各隐藏状态的概率矩阵  
  31.             ComputeXI(OB, ALPHA, BETA, ref XI);     // 求解各时刻位于各隐藏状态及下一时刻位于各隐藏状态的关联概率矩阵  
  32.               
  33.             Int32 Iterations;  
  34.             Double LogProbPrev = LogProbForward;  
  35.             for(Iterations = 1; ; Iterations++)  
  36.             {   // 重新估计初始概率向量   
  37.                 for (Int32 i = 0; i < N; i++)  
  38.                 {   // 注意:此处修正UMDHMM错误,以保证概率总和为1   
  39.                     PI[i] = MSP + (1 - MSP * N) * GAMMA[0, i];  
  40.                 }  
  41.   
  42.                 for(Int32 i = 0; i < N; i++)  
  43.                 {   // 重新估计状态转移矩阵   
  44.                     Double DenominatorA = 0;  
  45.                     for(Int32 t = 0; t < OB.Length - 1; t++)  
  46.                         DenominatorA += GAMMA[t, i];  
  47.                       
  48.                     for(Int32 j = 0; j < N; j++)  
  49.                     {  
  50.                         Double NumeratorA = 0;  
  51.                         for(Int32 t = 0; t < OB.Length - 1; t++)  
  52.                             NumeratorA += XI[t,i,j];  
  53.   
  54.                         // 注意:此处修正UMDHMM错误,以保证概率总和为1   
  55.                         A[i, j] = MSP + (1 - MSP * N) * NumeratorA / DenominatorA;  
  56.                     }  
  57.   
  58.                     // 重新估计混淆矩阵   
  59.                     Double DenominatorB = DenominatorA + GAMMA[OB.Length - 1, i];  
  60.                     for(Int32 k = 0; k < M; k++)  
  61.                     {  
  62.                         Double NumeratorB = 0;  
  63.                         for(Int32 t = 0; t < OB.Length; t++)  
  64.                         {  
  65.                             if(OB[t] == k) NumeratorB += GAMMA[t,i];  
  66.                         }  
  67.   
  68.                         // 注意:此处修正UMDHMM错误,以保证概率总和为1   
  69.                         B[i, k] = MOP + (1 - MOP * M) * NumeratorB / DenominatorB;  
  70.                     }                     
  71.                 } // End for i   
  72.   
  73.                 // 计算概率差,决定是否停止迭代   
  74.                 LogProbForward = ForwardWithScale(OB, ref ALPHA, ref SCALE);  
  75.                 if (LogProbForward - LogProbPrev <= ExitError) break;  
  76.   
  77.                 BackwardWithScale(OB, SCALE, ref BETA);  
  78.                 ComputeGamma(ALPHA, BETA, ref GAMMA);  
  79.                 ComputeXI(OB, ALPHA, BETA, ref XI);  
  80.                 LogProbPrev = LogProbForward;  
  81.             } // End for Iterations   
  82.   
  83.             LogProbFinal = LogProbForward;  // 最终概率  
  84.   
  85.             // 返回迭代次数   
  86.             return Iterations;  
  87.         }  
  88.   
  89.         /// <summary>   
  90.         /// 求解t时刻位于隐藏状态Si的概率矩阵   
  91.         /// </summary>   
  92.         /// <param name="ALPHA">前向算法局部概率</param>   
  93.         /// <param name="BETA">后向算法局部概率</param>  
  94.         /// <param name="GAMMA">输出:各时刻位于各隐藏状态的概率矩阵</param>  
  95.         private void ComputeGamma(Double[,] ALPHA, Double[,] BETA, ref Double[,] GAMMA)  
  96.         {  
  97.             Int32 T = ALPHA.GetLength(0);  
  98.             if (GAMMA == null) GAMMA = new Double[T, N];  
  99.   
  100.             for (Int32 t = 0; t < T; t++)  
  101.             {  
  102.                 Double Denominator = 0;  
  103.                 for (Int32 i = 0; i < N; i++)  
  104.                 {  
  105.                     GAMMA[t, i] = ALPHA[t, i] * BETA[t, i];  
  106.                     Denominator += GAMMA[t, i];  
  107.                 }  
  108.   
  109.                 for (Int32 i = 0; i < N; i++)  
  110.                 {  
  111.                     GAMMA[t, i] /= Denominator; // 保证各时刻的概率总和等于1  
  112.                 }  
  113.             }  
  114.         }  
  115.   
  116.         /// <summary>   
  117.         /// 求解t时刻位于隐藏状态Si及t+1时刻位于隐藏状态Sj的概率矩阵  
  118.         /// </summary>   
  119.         /// <param name="OB">观察序列</param>   
  120.         /// <param name="ALPHA">前向算法局部概率</param>   
  121.         /// <param name="BETA">后向算法局部概率</param>  
  122.         /// <param name="XI">输出:求解各时刻位于各隐藏状态及下一时刻位于各隐藏状态的关联概率矩阵</param>  
  123.         private void ComputeXI(Int32[] OB, Double[,] ALPHA, Double[,] BETA, ref Double[,,] XI)  
  124.         {  
  125.             Int32 T = OB.Length;  
  126.             if (XI == null) XI = new Double[T, N, N];  
  127.   
  128.             for (Int32 t = 0; t < T - 1; t++)  
  129.             {  
  130.                 Double Sum = 0;  
  131.                 for (Int32 i = 0; i < N; i++)  
  132.                 {  
  133.                     for (Int32 j = 0; j < N; j++)  
  134.                     {  
  135.                         XI[t, i, j] = ALPHA[t, i] * A[i, j] * B[j, OB[t + 1]] * BETA[t + 1, j];  
  136.                         Sum += XI[t, i, j];  
  137.                     }  
  138.                 }  
  139.   
  140.                 // 保证各时刻的概率总和等于1   
  141.                 for (Int32 i = 0; i < N; i++)  
  142.                     for (Int32 j = 0; j < N; j++)  
  143.                         XI[t, i, j] /= Sum;   
  144.             }  
  145.         }  
  146.     }  
  147. }  
转载:http://blog.csdn.net/jhqin/article/details/6534916

0 0