comparison toolboxes/FullBNT-1.0.7/bnt/inference/static/@pearl_inf_engine/bethe_free_energy.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e9a9cd732c1e
1 function loglik = bethe_free_energy(engine, evidence)
2 % BETHE_FREE_ENERGY Compute Bethe free energy approximation to the log likelihood
3 % loglik = bethe_free_energy(engine, evidence)
4 %
5 % The Bethe free energy is given by an exact energy term and an approximate entropy term.
6 % Energy
7 % E = -sum_f sum_i b(f,i) ln theta(f,i)
8 % where b(f,i) = approximate Pr(family f = i)
9 % and theta(f,i) = Pr(f = i)
10 % Entropy
11 % S = H1 - H2
12 % H1 = sum_f sum_p H(b(f))
13 % where b(f) = belief on family f, H(.) = entropy
14 % H2 = sum_n (q(n)-1) H(b(n))
15 % where q(n) = num. neighbors of n
16 %
17 % This function was written by Yair Weiss, 8/22/01.
18
19 hidden = find(isemptycell(evidence));
20 bnet = bnet_from_engine(engine);
21 N = length(bnet.dag);
22
23 add_ev = 1;
24 E=0;H1=0;H2=0;
25 loglik=0;
26 for n=1:N
27 ps=parents(bnet.dag,n);
28 if (length(ps)==0) % root node
29 qi=length(children(bnet.dag,n))-1;
30 else
31 qi=length(children(bnet.dag,n));
32 end
33 bf = marginal_family(engine, n, add_ev);
34 bf = bf.T(:);
35 e = bnet.equiv_class(n);
36 T = CPD_to_CPT(bnet.CPD{e});
37 T = T(:);
38 E = E-sum(log(T+(T==0)).*bf);
39
40 if length(ps) > 0
41 % root nodes don't count as fmailies
42 H1 = H1+sum(log(bf+(bf==0)).*bf);
43 end
44
45 bi = marginal_nodes(engine, n, add_ev);
46 bi = bi.T(:);
47 H2 = H2+qi*sum(log(bi+(bi==0)).*bi);
48 end
49 loglik=E+H1-H2;
50 loglik=-loglik;