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