马尔可夫 java_隐马尔可夫模型的前向算法(java实现),今天奉上

1 package jxutcm.edu.cn.hmm.model;

2

3 import jxutcm.edu.cn.hmm.bean.HMMHelper;

4

5 /**6 * 实现了 HMM(隐马尔可夫模型, Hidden Markov Models)的 前向(Forward), 后向(Backward),7 * 前向-后向(Baum-Welch)算法 这里均计算对数概率(将乘法转换为加法)8 *  HMM五元素:λ=(N, M, A, B, pi)9 *  N:隐藏状态数 hidden states10 *  M:观测状态数 observed states11 *  A: 状态转移矩阵 transition matrix12 *  B:发射矩阵 emission matrix13 *  pi:初始隐状态向量 initial state vector14 *                                                                           P(q_t|q_t-1)15 * 隐藏状态: q1 ——>q2 ——> ……——>q_t-1——>q_t——>——>q_T16 *                      |                 |                                 |                  |                          |17 *                      |                 |                                 |                  |                          |18 *  显示状态:O1            O2                             O_t-1        O_t                    O_T19 *  隐数目——N个;显数目——M个20 */

21 public class HMM {

22     /**23 * 隐藏状态名称列表——在获取最优隐藏状态序列时需要24 * 如enum Box {one, two, three}; // 隐藏状态(箱子编号)25 */

26     public String[] state;

27     /**28 * 观察状态名称列表——在根据观测索引获取获取观测序列时需要29 * enum Color {red, yellow, blue, green}; // 观察状态(观测到的颜色值)30 */

31     public String[] syms;

32

33     public int N;

34     /**35 *  观测状态数 observed states36 *  观测状态名称叫syms37 */

38     public int M;

39     /**40 * 隐藏状态转移矩阵41 * logA[ i ][ j ] = log(P( i -> j )) 表示从i状态转移到 j 状态的概率——取自然对数42 */

43     public double[][] logA;

44     /**45 * 发射矩阵(混淆矩阵)46 * logB[ i ][Ot] = log(P(emit Ot in state i)) 表示状态 i 下发射出 Ot 的概率——Ot为第t时刻的显示序列单号47 */

48     public double[][] logB;

49     /**50 * 初始状态概率分布——每个隐藏状态发生的概率51 */

52     public double[] logPI;

53

54     /**55 * 观察到的序列56 */

57     //public int[] O;//如yellow red blue yellow green 这些在enum Color {red,yellow,blue,green }的索引位置58

59     public HMM(){}

60

61     public  HMM(int N, int M){

62         this.N=N;

63         this.M=M;

64         this.logA=new double[N][N];

65         this.logB=new double[N][M];

66         this.logPI=new double[N];

67         for(int i=0; i

68             for(int j=0; j

69                 this.logA[ i ][ j ] = Double.NEGATIVE_INFINITY;

70             }

71             for(int j=0; j

72                 this.logB[ i ][ j ]=Double.NEGATIVE_INFINITY;

73             }

74             this.logPI[ i ] = Double.NEGATIVE_INFINITY;

75         }

76     }

77

78     /**79 * (已经有具体HMM的各个参数情况下调用)80 *@paramstate隐藏状态名称81 *            enum Box {one,two,three }82 *@paramsyms 观测状态名称83 *            enum Color {red,yellow,blue,green}84 *@paramA85 *            隐藏状态的转移矩阵86 *                         1             2           387 *                      one        two        three88 *           one  {0.500,   0.375,   0.125}89 *           two  {0.250,   0.125,   0.625},90 *           three{0.250,   0.375,   0.375}91 *@paramB 从隐藏状态——观察状态的发射矩阵(混淆矩阵)92 *                            0        1         2        393 *                          red yellow blue  green94 *            one     {0.60,  0.20,  0.15,  0.05},95 *            two     {0.25,  0.25,  0.25,  0.25},96 *            three  {0.05,  0.10,  0.35,  0.50}97 */

98     public HMM(double[][] A, double[][] B, double[] PI){

99         this.N=A.length;

100         this.M=B[0].length;

101         this.logA=new double[N][N];

102         this.logB=new double[N][M];

103         this.logPI=new double[N];

104         for(int i=0; i

105             for(int j=0; j

106                 this.logA[ i ][ j ] = Math.log( A[ i ][ j ] );

107             }

108             for(int j=0; j

109                 this.logB[ i ][ j ]=Math.log( B[ i ][ j ] );

110             }

111             this.logPI[ i ] = Math.log( PI[ i ] );

112         }

113     }

114

115     /**116 * 打印转移矩阵117 */

118     public void printA() {

119         System.out.println("转移矩阵:");

120         for (int i = 1; i 

121             for (int j = 1; j 

122                 System.out.print(HMMHelper.fmtlog(logA[i][j]));

123             }

124             System.out.println();

125         }

126     }

127

128     /**129 * 打印发射矩阵130 */

131     public void printB() {

132         System.out.println("发射矩阵:");

133         for (int i = 1; i 

134             for (int j = 1; j 

135                 System.out.print(HMMHelper.fmtlog(logB[i][j]));

136             }

137             System.out.println();

138         }

139     }

140

141

142 }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值