Mercurial > hg > trimatlab
annotate private/mc_fixpt.m @ 8:e1534c7329e2
FIX: missing end
author | samer |
---|---|
date | Mon, 13 Feb 2012 11:21:15 +0000 |
parents | be936975f254 |
children |
rev | line source |
---|---|
samer@0 | 1 function p=mc_fixpt(Pt) |
samer@0 | 2 % mc_fixpt - fixed point of Markov chain (stationary state distribution) |
samer@0 | 3 % |
samer@0 | 4 % mc_fixpt :: [[K,K]]~'transition matrix'-> [[K]]. |
samer@0 | 5 |
samer@0 | 6 |
samer@0 | 7 % for ergodic HMM we want to get stationary distribution |
samer@0 | 8 % use eigenvalues of transition matrix. |
samer@0 | 9 [V,D]=eig(Pt); |
samer@0 | 10 |
samer@0 | 11 % one of the eigenvalues should be 1 |
samer@0 | 12 [dummy,k]=max(-abs(diag(D)-1)); |
samer@0 | 13 |
samer@0 | 14 % check that it is really close |
samer@0 | 15 if abs(D(k,k)-1)>0.001 |
samer@0 | 16 disp('mc_fixpt: failed to find eigenvalue of 1 in '); |
samer@0 | 17 disp(Pt) |
samer@0 | 18 error('mc_fixpt:noeig','failed to find eigenvalue of 1'); |
samer@0 | 19 end |
samer@0 | 20 |
samer@0 | 21 % get eigenvector and flip it over if all negative |
samer@0 | 22 v=V(:,k); if sum(v)<0, v=-v; end |
samer@0 | 23 |
samer@0 | 24 if ~isreal(v) |
samer@0 | 25 disp('mc_fixpt: discarding complex parts of eigenvector'); |
samer@0 | 26 v=real(v); |
samer@0 | 27 end |
samer@0 | 28 if any(v<0) |
samer@0 | 29 %fprintf('mc_fixpt: discarding negative parts of eigenvector: %s\n',mat2str(v(v<0))); |
samer@0 | 30 v=max(0,v); |
samer@0 | 31 end |
samer@0 | 32 |
samer@0 | 33 % final normalisation |
samer@0 | 34 p=v/sum(v); |