% Demonstrate discrete Markov processes: learn a DMP of order 1 from English
% text and sample from it.
% Data: strings of chars, case-sensitive and including blanks and punctuation.

% Example datasets of sequences, stored as a cell array of strings X:

% 1. English sentences, each ending with period-blank:
X = {'A nice cat. ','She is not a bad girl. ','I like his car. ',...
     'She missed it. ','It can be fun. ','It is a pity. ','Good luck. '};

% 2. Formulas:
%X = {'a+b=c','c-b=a','a+a=c','b+b=a','c/c=a','c/a=b'};

% 3. Sentences read from a file:
%filename = 'MobyDick.txt';
%Y = textscan(fileread(filename),'%s','delimiter','\n'); Y = Y{1};
%j=1; X={}; for i=1:length(Y) if ~isempty(Y{i}) X{j} = Y{i}; j = j+1; end; end

% Randomly generated text:
%Q = ' ,.:;abcdefghijklmnopqrstuvwxyz';
%X = num2cell(Q(randi(length(Q),50,75)),2)';

% Learn discrete Markov process parameters from the sequence dataset:
[Q,A,P] = dmptrain(X); N = length(P);

% ---------------------------------- Plots ----------------------------------

% Initial state distribution and transition matrix
figure(1); clf; set(gcf,'Name','discrete Markov process parameters');
imagesc([A;NaN(1,N);P'],[0 1]); colormap(hot(256));
hold on; fill(0.5+[0 N N 0],N-0.5+[1 1 2 2],204/255*[1 1 1]); hold off;
text(0,(N+1)/2,'A \rightarrow  ','HorizontalAlignment','right');
text(0,N+2,'\pi \rightarrow  ','HorizontalAlignment','right');
text(0,N+3.5,'states \rightarrow  ','HorizontalAlignment','right');
set(gca,'YTick',1:N,'YTickLabel',Q','XTick',1:N,'XTickLabel',Q');
axis image; colorbar;
title(['N= ' num2str(N) ...
       ' states: transition matrix A and initial state distribution \pi']);

% Sample K sequences of length T:
K = 15; T = 75; rng(1778);
S = dmpsample(Q,A,P,K,T)	% from the discrete Markov process
Q(randi(N,K,T))			% purely random sequences

