MATLAB中的隐马尔可夫模型

时间:2012-06-15 17:22:13

标签: matlab markov-chains hidden-markov-models

我有11个状态和一个转移概率矩阵,但我没有排放,因为我的模型没有隐藏。它仅由状态(1,2,3,...,11)组成 我想基于我的转移概率矩阵生成随机状态,但是HMM工具箱需要发射概率矩阵。我该怎么办?

[seq, states] = hmmgenerate(100, Trans, Emis) 

1 个答案:

答案 0 :(得分:7)

请考虑以下事项:

%# number of states
N = 11;

%# some random transition matrix
trans = rand(N,N);
trans = bsxfun(@rdivide, trans, sum(trans,2));

%# fake emission matrix (only one symbol)
emis = ones(N,1);

%# get a sample of length = 10
[~,states] = hmmgenerate(10, trans, emis)

生成的状态序列:

>> states
states =
    10     1     3    11     9     4    11     1     4     6

编辑:

事实上,使用马尔可夫链相对容易,我们可以自己做。这是另一个不使用统计工具箱中的HMM函数的例子。

%# number of states
N = 3;

%# transition matrix
trans = rand(N,N);
trans = bsxfun(@rdivide, trans, sum(trans,2));

%# probability of being in state i at time t=0
prior = rand(1,N);
prior = prior ./ sum(prior);

%# generate a sequence of states
len = 100;          %# length of sequence
states = zeros(1,len);
states(1) = randsample(N, 1, true, prior);
for t=2:len
    states(t) = randsample(N, 1, true, trans(states(t-1),:));
end

%# show sequence
stairs(states, 'LineWidth',2)
set(gca, 'YGrid','on', 'YLim',[0 N+1])
xlabel('time'), ylabel('states')
title('sequence of states')

sequence of states

我正在使用RANDSAMPLE函数在每次迭代时进行采样。如果您只想使用核心功能(无工具箱),请参阅Weighted random numbers in MATLAB以获取替代方案。