Below is my matlab code snippet for generating first order markov chain. I am having trouble with extending to 2nd order markov chain. Can some one help me in doing so? Any suggestions, hints, links, pseudo-code, algorithm, python or matlab snippets would be helpful.
cdist
is the cumulative distribution vector (size-28*1) my 27 symbols. I am writing the output to file called chain
.p
and q
are uniform random numbers. CTRANS
is the cumulative matrix corresponding to my first order transition matrix TRANS
(which is not shown here, its size is 729*27). CTRANS
besides being the cumulative version also has a row vector of zeros appended on top for programming ease. cols
is the column size of CTRANS
.
%generate sequence according to distribution and transition matrix
fileID = fopen('chain','w');
for k=1:10000
p=rand;
for l=2:numel(cdist)%2 to 28
if ((p >= cdist(l-1)) && (p <= cdist(l)))
fprintf(fileID,'%s\n',num2str(l-1));
q=rand;
for m=2:cols%2 to 28
if ((q >= CTRANS(l,m-1)) && (q <= CTRANS(l,m)))
fprintf(fileID,'%s\n',num2str(m-1));
end
end
end
end
end
fclose(fileID);
I am struggling with the second order case. I can provide more details if required. My input data from where I extract the statistics is english text of length around 4000 characters. I have removed the punctuations etc and converted capital letters to small letters, so now there are 27 symbols where number 1 represents 'a' till 26 represents 'z' and 27 for space. Also I have created the bi-gram distribution vector for the second order case.