Você está na página 1de 2

3/22/13

Hidden markov model in MATLAB - Stack Overflow

Hidden markov model in MATLAB

I have 11 states, and a transition probability matrix, but I don't have emissions as my model is not hidden.
It consists only of states (1,2,3, ..., 11)
I want to generate random states based on my transition probability matrix, but the HMM tool box requires
an emission probability matrix. What should I do?
[seq, states] = hmmgenerate(100, Trans, Emis)
matlab

matlab-toolbox

markov-chains

hidden-markov-models

edited Jun 15 '12 at 20:25


Amro
45.5k 8 47 115

asked Jun 15 '12 at 17:22


Radwa Mohamed
6 2

if you don't have the hidden component of HMM, then its simply a Markov Chain Amro Jun 15 '12 at 18:55

1 Answer
Consider the following:
%# 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)
The sequence of states generated:
>> states
states =
10
1

11

11

EDIT:
In fact working with a Markov chain is relatively easy, that we can do it ourselves. Here is another
example without using HMM functions from the stats toolbox.

stackoverflow.com/questions/11055410/hidden-markov-model-in-matlab

1/2

3/22/13

Hidden markov model in MATLAB - Stack Overflow


%# 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')

I am using RANDSAMPLE function to sample at each iteration. If you want to use only core functinos (no
toolboxes), see Weighted random numbers in MATLAB for an alternative.
edited Jun 15 '12 at 23:41

answered Jun 15 '12 at 19:08


Amro
45.5k 8 47 115

Thanks a lot for your helpful answer, I tried it but it is not giving me a satisfaction final result, coz I am
applying it on monte carlo simulation and compare the results to something and there's a big error.
Radwa Mohamed Jun 15 '12 at 23:08
@RadwaMohamed: When you say "error" I have to ask how are you comparing the results? Remember that
the sampling here involves randomness so its unlikely you will ever get the same result on two different
runs... Amro Jun 15 '12 at 23:26
If you are interested, see here for an example application of Markov chains to generate random words (the
implementation is very similar to the one I added) Amro Jun 15 '12 at 23:47
yes I know that I will not get the same result on two different runs, but if u run this experiment for 2500 times
you will get the same average. That's how monte carlo works. So I am comparing the average to the actual
results I have. It should be the same. That's what I mean. Thanks a lot for your help. Radwa Mohamed
Jun 16 '12 at 20:18

@RadwaMohamed: there is no way for us to compare the results unless you share your actual code...
Amro Jun 16 '12 at 20:57

show 1 more comment

Not the answer you're looking for? Browse other questions tagged matlab
matlab-toolbox markov-chains

hidden-markov-models or ask your own question.

stackoverflow.com/questions/11055410/hidden-markov-model-in-matlab

2/2

Você também pode gostar