From: DEVANAND on
I have a initial state (out of n states ) , a transition probability matrix and number of steps. How to generate a markov chain.Please help......
From: Roger Stafford on
"DEVANAND " <devanandiamin7(a)gmail.com> wrote in message <hokljp$c4f$1(a)fred.mathworks.com>...
> I have a initial state (out of n states ) , a transition probability matrix and number of steps. How to generate a markov chain.Please help......

What is it you mean when you ask "to generate a markov chain"? Do you just want to calculate the probabilities of the various states after a number of steps, given the initial state. That is simply a matter of matrix multiplication. Or perhaps you wish to simulate this markov chain with actual matlab variables using the 'rand' function. You need to describe what you want in some detail.

In either case could you please show us what you have accomplished so far in this problem.

I assume when you say "a transition probability matrix", you mean that the chain satisfies the Chapman&#8211;Kolmogorov stationary condition.

Roger Stafford
From: DEVANAND on
"Roger Stafford" <ellieandrogerxyzzy(a)mindspring.com.invalid> wrote in message <holf1i$92h$1(a)fred.mathworks.com>...
> "DEVANAND " <devanandiamin7(a)gmail.com> wrote in message <hokljp$c4f$1(a)fred.mathworks.com>...
> > I have a initial state (out of n states ) , a transition probability matrix and number of steps. How to generate a markov chain.Please help......
>
> What is it you mean when you ask "to generate a markov chain"? Do you just want to calculate the probabilities of the various states after a number of steps, given the initial state. That is simply a matter of matrix multiplication. Or perhaps you wish to simulate this markov chain with actual matlab variables using the 'rand' function. You need to describe what you want in some detail.
>
> In either case could you please show us what you have accomplished so far in this problem.
>
> I assume when you say "a transition probability matrix", you mean that the chain satisfies the Chapman&#8211;Kolmogorov stationary condition.
>
> Roger Stafford

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: Roger Stafford on
"DEVANAND " <devanandiamin7(a)gmail.com> wrote in message <hp1e6n$6hj$1(a)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
From: DEVANAND on
"Roger Stafford" <ellieandrogerxyzzy(a)mindspring.com.invalid> wrote in message <hp65fe$d88$1(a)fred.mathworks.com>...
> "DEVANAND " <devanandiamin7(a)gmail.com> wrote in message <hp1e6n$6hj$1(a)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

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.

DEVANAND T