Mercurial > hg > trimatlab
comparison mc_global_info1.m @ 18:062d46712995 tip
Moved mc_global_info1 back to public folder
author | samer |
---|---|
date | Mon, 02 Apr 2012 21:50:43 +0100 |
parents | private/mc_global_info1.m@a6d5597bd922 |
children |
comparison
equal
deleted
inserted
replaced
17:4e61b949e73d | 18:062d46712995 |
---|---|
1 % mc_global_info1 - Three global information measures about stationary MC | |
2 % | |
3 % mc_global_info1 :: | |
4 % [[N,N]] ~'transmat', | |
5 % nonneg ~'tolerance for fixed point check' | |
6 % -> [[3]],bool. | |
7 % | |
8 % The three measures in order are | |
9 % Entropy rate H(X|Z) | |
10 % Redundancy I(X,Z) | |
11 % Predictive information rate I(X,Y|Z) | |
12 % All in NATS, not bits. | |
13 function [Info,ergodic]=mc_global_info1(T,tol) | |
14 | |
15 n=size(T,1); | |
16 pz=mc_fixpt2(T,tol); | |
17 ergodic=(size(pz,2)==1); | |
18 if ergodic && all(isfinite(pz)) | |
19 | |
20 TlogT=T.*slog(T); | |
21 HXZ=-sum(TlogT,1)*pz; | |
22 | |
23 pxz=T; % p(x|z) | |
24 pyxz=repmat(T,[1,1,n]).*repmat(shiftdim(T,-1),[n,1,1]); % p(y,x|z) | |
25 pyz=sum(pyxz,2); % p(y|z) | |
26 pyz(pyz==0)=realmin; | |
27 pxyz=permute(pyxz./repmat(pyz,[1,n,1]),[2,1,3]); % p(x|y,z) | |
28 | |
29 HXYz=-shiftdim(sum(sum(permute(pyxz,[2,1,3]).*slog(pxyz),1),2),2); % H(X|Y,z) | |
30 IXYZ=(-HXYz' - sum(pxz.*slog(pxz)))*pz; | |
31 | |
32 IXZ=max(0,(sum(TlogT)-slog(pz)')*pz); | |
33 | |
34 | |
35 Info=[HXZ;IXZ;IXYZ]; | |
36 if any(~isreal(Info)) | |
37 disp('unreal information'); | |
38 ergodic=0; | |
39 elseif any(Info<0) | |
40 if any(Info<-eps) | |
41 disp('negative information'); | |
42 disp(pz'); | |
43 disp(Info'); | |
44 ergodic=0; | |
45 else | |
46 Info=max(0,Info); | |
47 end | |
48 end | |
49 else | |
50 Info=[0;0;0]; | |
51 end | |
52 end | |
53 | |
54 function y=slog(x) | |
55 % slog - safe log, x<=0 => slog(x)=0 | |
56 % | |
57 % slog :: real -> real. | |
58 | |
59 x(x<=0)=1; | |
60 y=log(x); | |
61 end | |
62 | |
63 function p=mc_fixpt2(Pt,tol) | |
64 [U,S,V]=svd(Pt-eye(size(Pt)),0); | |
65 p=V(:,diag(S)<=tol); | |
66 p=max(0,p*diag(1./sum(p))); | |
67 end | |
68 | |
69 | |
70 % mc_fixpt - fixed point(s) of Markov chain (stationary state distribution) | |
71 % | |
72 % mc_fixpt :: [[K,K]]~'transition matrix'-> [[K,L]]. | |
73 function p=mc_fixpt(Pt,tol) | |
74 [V,D]=eig(Pt); | |
75 p=V(:,abs(diag(D)-1)<tol); | |
76 p=real(p*diag(1./sum(p))); | |
77 p(abs(p)<eps)=0; | |
78 end |