samer@0
|
1 function Info=mc_global_info(T,pz)
|
samer@0
|
2 % mc_global_info - Three global information measures about stationary MC
|
samer@0
|
3 %
|
samer@0
|
4 % mc_global_info :: [[N,N]] ~'transmat' -> [[3]].
|
samer@0
|
5 %
|
samer@0
|
6 % mc_global_info ::
|
samer@0
|
7 % [[N,N]] ~'transmat',
|
samer@0
|
8 % [[N]] ~'stationary distribution if already known'
|
samer@0
|
9 % -> [[3]].
|
samer@0
|
10 %
|
samer@0
|
11 % The three measures in order are
|
samer@0
|
12 % Entropy rate H(X|Z)
|
samer@0
|
13 % Redundancy I(X,Z)
|
samer@0
|
14 % Predictive information rate I(X,Y|Z)
|
samer@7
|
15 % All in NATS, not bits.
|
samer@0
|
16
|
samer@0
|
17
|
samer@0
|
18 n=size(T,1);
|
samer@0
|
19 if nargin<2, pz=mc_fixpt(T); end
|
samer@0
|
20 TlogT=T.*slog(T);
|
samer@0
|
21 HXZ=-sum(TlogT,1)*pz;
|
samer@0
|
22
|
samer@0
|
23 pxz=T; % p(x|z)
|
samer@0
|
24 pyxz=repmat(T,[1,1,n]).*repmat(shiftdim(T,-1),[n,1,1]); % p(y,x|z)
|
samer@0
|
25 pyz=sum(pyxz,2); % p(y|z)
|
samer@0
|
26 pyz(pyz==0)=realmin;
|
samer@0
|
27 pxyz=permute(pyxz./repmat(pyz,[1,n,1]),[2,1,3]); % p(x|y,z)
|
samer@0
|
28
|
samer@0
|
29 HXYz=-shiftdim(sum(sum(permute(pyxz,[2,1,3]).*slog(pxyz),1),2),2); % H(X|Y,z)
|
samer@0
|
30 IXYZ=(-HXYz' - sum(pxz.*slog(pxz)))*pz;
|
samer@0
|
31
|
samer@0
|
32 IXZ=max(0,(sum(TlogT)-slog(pz)')*pz);
|
samer@0
|
33
|
samer@0
|
34
|
samer@0
|
35 Info=[HXZ;IXZ;IXYZ];
|
samer@0
|
36 if any(~isreal(Info))
|
samer@0
|
37 disp('unreal information');
|
samer@0
|
38 keyboard
|
samer@0
|
39 elseif any(Info<0)
|
samer@0
|
40 if any(Info<-eps)
|
samer@0
|
41 disp('negative information');
|
samer@0
|
42 disp(Info);
|
samer@0
|
43 keyboard
|
samer@0
|
44 else
|
samer@0
|
45 Info=max(0,Info);
|
samer@0
|
46 end
|
samer@0
|
47 end
|
samer@0
|
48
|
samer@0
|
49 function y=slog(x)
|
samer@0
|
50 % slog - safe log, x<=0 => slog(x)=0
|
samer@0
|
51 %
|
samer@0
|
52 % slog :: real -> real.
|
samer@0
|
53
|
samer@0
|
54 x(x<=0)=1;
|
samer@0
|
55 y=log(x);
|
samer@0
|
56
|