From: "DEVANAND " <>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Please help me to generate a markov chain.
Date: Mon, 5 Apr 2010 01:42:05 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 41
Message-ID: <hpbf5d$f77$>
References: <hokljp$c4f$> <holf1i$92h$> <hp1e6n$6hj$> <hp65fe$d88$>
Reply-To: "DEVANAND " <>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: 1270431725 15591 (5 Apr 2010 01:42:05 GMT)
NNTP-Posting-Date: Mon, 5 Apr 2010 01:42:05 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 897626
Xref: comp.soft-sys.matlab:623438

"Roger Stafford" <> wrote in message <hp65fe$d88$>...
> "DEVANAND " <> wrote in message <hp1e6n$6hj$>...
> > sir,
> >     I actually wanted to test if computer can generate music.While searching internet I found a page in wikipedia on markov chains and its application in music.I found also 2 transition matrices(one a firstorder and other second order markov chain) where the states are music notes. To be true , I doesnot have any prior knowledge on markov chain.But was interested to simulate my experiment using MATLAB. In Matlab but I saw only Hidden markov chains. A command 'hmmgenerate' which has a syntax as below
> > 
> >  [seq,states] = hmmgenerate(len,TRANS,EMIS)
> > 
> > takes a known Markov model, specified by transition probability matrix TRANS and emission probability matrix EMIS, and uses it to generate
> >     *A random sequence seq of emission symbols
> >     *A random sequence states of states
> > The length of both seq and states is len.
> > 
> > But now I am not clear about EMISSION MATRIX. I was trying to get some code related to markov chain for my experiment.My aim is to simulate the experiment in matlab and then learn the basics. Do you have any suggestion for me. Which books are good for these topics to read.
> > 
>   From your reply you apparently want to simulate an actual Markov chain defined by a probability transition array using the 'rand' function.  For your own home-grown chain generator, let us suppose that your "notes" are represented by the integers from 1 to n.  For an m-th order Markov chain, let M be an m+1-dimensional array of size n x n x n x ... x n in which the first dimension columns, M(:,s_n-1,s_n-2,...,s_n-m), give the probabilities of transition to the values 1:n given each combination of previous states, s_n-1,s_n-2,...,s_n-m.
>   Then use a for-loop to work your way forward step-by-step from an initial set of m successive states (notes) to the next state as follows:
>  p = M(:,s_n-1,s_n-2,...,s_n-m); % Get the appropriate column of M
>  % Then generate the next state, s_n, with these probabilities
>  [t,s_n] = histc(rand,[-inf;cumsum(p(1:n-1));inf]);
>  (Store this newly created s_n in a vector of successive "notes")
> where s_n will be the next state.  At the next trip through the for-loop, you will of course be using the updated (shifted)
>  M(:,s_n,s_n-1,s_n-2,...,s_n-m+1).
>   The result will be a Markov chain of as many steps beyond the initial set of states as trips you choose to take through the for-loop.
>   For this to work properly you need to ensure that each of the columns of M consist of non-negative quantities whose sum is one.  (If you want to create random columns with this property you might like to use my 'randfixedsum' at
> )
>   (Note: The above use of 'histc' may not be the most efficient way of choosing the next s from just a single value of rand.  You might consider using a specialized binary search technique instead.)
> Roger Stafford

Thanks very much for such a detailed and helpful reply.I have written a script for 1st order markov chain and trying for 2nd order.