ESTMC estimates transition matrix P from a time series of a Markov chain. [Pest,N] = estmc(z,r); z = time series of a Markov chain. [1xT] r = number of states in Markov chain. Pest = Estimated transition matrix. N =
Script to computer exercises 2 | |
Quick test of the routines in module 'markov' |
001 function [Pest,N,Ni] = estmc(z,r) 002 % ESTMC estimates transition matrix P from a time series of a Markov chain. 003 % 004 % [Pest,N] = estmc(z,r); 005 % 006 % z = time series of a Markov chain. [1xT] 007 % r = number of states in Markov chain. 008 % 009 % Pest = Estimated transition matrix. 010 % N = 011 012 013 T = length(z); 014 015 N = zeros(r,r); 016 Pest = zeros(r,r); 017 018 for i = 1:r 019 for j=1:r 020 N(i,j) = sum( (z(1:T-1)==i) & (z(2:T)==j) ); 021 % if i==j 022 % N(i,i) = sum(z(1:T-1)==i); 023 % else 024 % N(i,j) = sum((z(2:T)-z(1:T-1))==j-i); 025 % end 026 end 027 end 028 029 Ni = sum(N,2); 030 031 for i = 1:r 032 if Ni(i) > 0 033 Pest(i,:) = N(i,:)/Ni(i); 034 else 035 Pest(i,:) = ones(1,r)/r; 036 end 037 Pest(i,i) = 0; 038 Pest(i,i) = 1-sum(Pest(i,:)); 039 end 040 041 042 043
Comments or corrections to the WAFO group