%参考:matlab help 文件
%例子中:状态变量为离散分布,值域为{'fair','loaded'};观测变量为离散分布,值域为{'one','two','three','four','five','six'}
%符号说明:
%states: 隐变量序列;
%seq:观测变量系列;
%tr:状态转移矩阵;
%e:发射矩阵;
%% hmmgenerate函数
tr = [0.95,0.05; %状态转移矩阵tr [2x2]
0.10,0.90];
e = [1/6, 1/6, 1/6, 1/6, 1/6, 1/6; %发射矩阵e 的维度 o=b'*i; [6x1]=[6x2]'x*[2x1];
1/10, 1/10, 1/10, 1/10, 1/10, 1/2;];
[seq, states] = hmmgenerate(100,tr,e); %100个时间序列单元
%符号说明:
%states: 隐变量序列;
%seq:观测变量系列;
%tr:状态转移矩阵;
%e:发射矩阵;
%% hmmgenerate函数
tr = [0.95,0.05; %状态转移矩阵tr [2x2]
0.10,0.90];
e = [1/6, 1/6, 1/6, 1/6, 1/6, 1/6; %发射矩阵e 的维度 o=b'*i; [6x1]=[6x2]'x*[2x1];
1/10, 1/10, 1/10, 1/10, 1/10, 1/2;];
[seq, states] = hmmgenerate(100,tr,e); %100个时间序列单元
% [seq, states] = hmmgenerate(100,tr,e,'Symbols',... %设置隐变量2个可能取值的名称和观测变量6个可能取值的名称
% {'one','two','three','four','five','six'},...
% 'Statenames',{'fair';'loaded'});
% {'one','two','three','four','five','six'},...
% 'Statenames',{'fair';'loaded'});
%% hmmviterbi函数
% estimatedStates = hmmviterbi(seq,tr,e); %利用viterbi算法求解HMM的decoding问题P(Z|X),在本例中大概能够达成70%及以上的识别准确率,这是利用有监督的方式,如果是无监督,验证模型有效的指标是什么?
% %查看识别准确率命令:length(find(states==estimatedStates))
% %查看识别准确率命令:length(find(states==estimatedStates))
% [seq, states] = hmmgenerate(100,tr,e,'Statenames',{'fair';'loaded'});
% estimatesStates = hmmviterbi(seq,tr,e,'Statenames',{'fair';'loaded'});
% %查看识别准确率命令:length(find(strcmp(states,estimatesStates)))
% estimatesStates = hmmviterbi(seq,tr,e,'Statenames',{'fair';'loaded'});
% %查看识别准确率命令:length(find(strcmp(states,estimatesStates)))
%% hmmdecode函数
pStates = hmmdecode(seq,tr,e); %p矩阵为[2x100]的矩阵,每一列表示一个时间序列单元对应的隐变量服从的离散概率分布
% %以下为验证decoding出的概率分布与原始的真实state的值相符合的程度,即验证hmmdecode的有效性
% deco_1index=find(sign(pStates(1,:)-0.5)>0);
% real_1index=find(states==1);
% count=0;
% for i=1:length(real_1index)
% for j=1:length(deco_1index)
% if real_1index(i)==deco_1index(j)
% count=count+1;
% end
% end
% end
% %得到混淆矩阵参数
% tp=count; %true positive tp=57;
% fp=length(deco_1index)-count; %fp=72-57=15;
% fn=length(real_1index)-count; %fn=65-57=8;
% tn=length(seq)-length(real_1index)-fp; %tn=100-65-15=20
% accuracy=(tp+tn)/(tp+tn+fp+fn); %准确率=0.7700
% %也可根据confusion_matrix自行计算对应的二级指标,比如召回率,F1指标等等,详见另一篇博客:分类模型评价指标
%
% [seq, states] = hmmgenerate(100,tr,e,'Symbols',...
% {'one','two','three','four','five','six'});
% pStates = hmmdecode(seq,tr,e,'Symbols',...
% {'one','two','three','four','five','six'});
pStates = hmmdecode(seq,tr,e); %p矩阵为[2x100]的矩阵,每一列表示一个时间序列单元对应的隐变量服从的离散概率分布
% %以下为验证decoding出的概率分布与原始的真实state的值相符合的程度,即验证hmmdecode的有效性
% deco_1index=find(sign(pStates(1,:)-0.5)>0);
% real_1index=find(states==1);
% count=0;
% for i=1:length(real_1index)
% for j=1:length(deco_1index)
% if real_1index(i)==deco_1index(j)
% count=count+1;
% end
% end
% end
% %得到混淆矩阵参数
% tp=count; %true positive tp=57;
% fp=length(deco_1index)-count; %fp=72-57=15;
% fn=length(real_1index)-count; %fn=65-57=8;
% tn=length(seq)-length(real_1index)-fp; %tn=100-65-15=20
% accuracy=(tp+tn)/(tp+tn+fp+fn); %准确率=0.7700
% %也可根据confusion_matrix自行计算对应的二级指标,比如召回率,F1指标等等,详见另一篇博客:分类模型评价指标
%
% [seq, states] = hmmgenerate(100,tr,e,'Symbols',...
% {'one','two','three','four','five','six'});
% pStates = hmmdecode(seq,tr,e,'Symbols',...
% {'one','two','three','four','five','six'});
%% 区别hmmestimate和hmmtrain
% hmmestimate已知state和seq,求模型参数tr,e
% hmmtrain已知seq,估计参数tr,e
%% hmmestimate函数
[estimateTR, estimateE] = hmmestimate(seq,states); %已知隐变量序列和观测变量序列,估计参数
% hmmestimate已知state和seq,求模型参数tr,e
% hmmtrain已知seq,估计参数tr,e
%% hmmestimate函数
[estimateTR, estimateE] = hmmestimate(seq,states); %已知隐变量序列和观测变量序列,估计参数
%% hmmtrain函数
seq1 = hmmgenerate(100,tr,e);
seq2 = hmmgenerate(200,tr,e);
seqs = [seq1,seq2]; %也可以换成seqs = {seq1,seq2};
[estTR, estE] = hmmtrain(seqs,tr,e); %tr,e是tr和e的初始概率分布(时刻0到时刻1)
seq1 = hmmgenerate(100,tr,e);
seq2 = hmmgenerate(200,tr,e);
seqs = [seq1,seq2]; %也可以换成seqs = {seq1,seq2};
[estTR, estE] = hmmtrain(seqs,tr,e); %tr,e是tr和e的初始概率分布(时刻0到时刻1)
t->t+1 | s1 | s2 |
s1 | 0.95 | 0.05 |
s1 | 0.10 | 0.90 |
状态转移矩阵
o1 | o2 | o3 | o4 | o5 | o6 | |
s1 | 1/6 | 1/6 | 1/6 | 1/6 | 1/6 | 1/6 |
s2 | 1/10 | 1/10 | 1/10 | 1/10 | 1/10 | 1/2 |
发射矩阵(还是发射矩阵的转置?,由于o=b*s=> [6x1]=[6x2]*[2x1])
实际-预测 | “1” | “2” |
“1” | TP | FN |
“2” | FP | TN |
二分类的混淆矩阵形式