Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Please help me to generate a markov chain.
Date: Sat, 3 Apr 2010 01:26:06 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 36
Message-ID: <hp65fe$d88$1@fred.mathworks.com>
References: <hokljp$c4f$1@fred.mathworks.com> <holf1i$92h$1@fred.mathworks.com> <hp1e6n$6hj$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1270257966 13576 172.30.248.37 (3 Apr 2010 01:26:06 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sat, 3 Apr 2010 01:26:06 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: news.mathworks.com comp.soft-sys.matlab:623057

"DEVANAND " <devanandiamin7@gmail.com> wrote in message <hp1e6n$6hj$1@fred.mathworks.com>...
> 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.
> 
> DEVANAND T

  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

 http://www.mathworks.com/matlabcentral/fileexchange/9700 )

  (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