% dice(n,r) alternates between fair and loaded dice, runs r simulations of length n each, and estimates the hidden sequence function [void] = dice(n,r) HS = 'FL'; % names of the hidden states OS = '123456'; % names of the observed states A = [[.95 .05]; [.1 .9]]; % transition probabilities among hidden states mu = [0.5 0.5]; % initial distribution of hidden states E = [[1/6 1/6 1/6 1/6 1/6 1/6]; [1/10 1/10 1/10 1/10 1/10 1/2]]; % emission probabilities Pi = hmmsim(mu,A,n); % simulate hidden Markov model fprintf('Die '); show(Pi,HS); % display the hidden states X = []; for i=1:r, x = obssim(Pi,E); % simulate observed states fprintf('Roll '); show(x,OS); % display the observed states X = [X; x]; % add these states as another row of X end PiStar = viterbi(mu,A,E,X); % find most likely hidden sequence fprintf('Viterbi '); show(PiStar,HS); % display it fprintf('Errors '); show((PiStar==Pi)+1,'* '); % mark errors