wolffd@0
|
1 function pot = CPD_to_scgpot(CPD, domain, ns, cnodes, evidence)
|
wolffd@0
|
2 % CPD_TO_CGPOT Convert a Gaussian CPD to a CG potential, incorporating any evidence
|
wolffd@0
|
3 % pot = CPD_to_cgpot(CPD, domain, ns, cnodes, evidence)
|
wolffd@0
|
4
|
wolffd@0
|
5 self = CPD.self;
|
wolffd@0
|
6 dnodes = mysetdiff(1:length(ns), cnodes);
|
wolffd@0
|
7 odom = domain(~isemptycell(evidence(domain)));
|
wolffd@0
|
8 cdom = myintersect(cnodes, domain);
|
wolffd@0
|
9 cheaddom = myintersect(self, domain);
|
wolffd@0
|
10 ctaildom = mysetdiff(cdom,cheaddom);
|
wolffd@0
|
11 ddom = myintersect(dnodes, domain);
|
wolffd@0
|
12 cobs = myintersect(cdom, odom);
|
wolffd@0
|
13 dobs = myintersect(ddom, odom);
|
wolffd@0
|
14 ens = ns; % effective node size
|
wolffd@0
|
15 ens(cobs) = 0;
|
wolffd@0
|
16 ens(dobs) = 1;
|
wolffd@0
|
17
|
wolffd@0
|
18 % Extract the params compatible with the observations (if any) on the discrete parents (if any)
|
wolffd@0
|
19 % parents are all but the last domain element
|
wolffd@0
|
20 ps = domain(1:end-1);
|
wolffd@0
|
21 dps = myintersect(ps, ddom);
|
wolffd@0
|
22 dops = myintersect(dps, odom);
|
wolffd@0
|
23
|
wolffd@0
|
24 map = find_equiv_posns(dops, dps);
|
wolffd@0
|
25 dpvals = cat(1, evidence{dops});
|
wolffd@0
|
26 index = mk_multi_index(length(dps), map, dpvals);
|
wolffd@0
|
27
|
wolffd@0
|
28 dpsize = prod(ens(dps));
|
wolffd@0
|
29 cpsize = size(CPD.weights(:,:,1), 2); % cts parents size
|
wolffd@0
|
30 ss = size(CPD.mean, 1); % self size
|
wolffd@0
|
31 % the reshape acts like a squeeze
|
wolffd@0
|
32 m = reshape(CPD.mean(:, index{:}), [ss dpsize]);
|
wolffd@0
|
33 C = reshape(CPD.cov(:, :, index{:}), [ss ss dpsize]);
|
wolffd@0
|
34 W = reshape(CPD.weights(:, :, index{:}), [ss cpsize dpsize]);
|
wolffd@0
|
35
|
wolffd@0
|
36
|
wolffd@0
|
37 % Convert each conditional Gaussian to a canonical potential
|
wolffd@0
|
38 pot = cell(1, dpsize);
|
wolffd@0
|
39 for i=1:dpsize
|
wolffd@0
|
40 %pot{i} = linear_gaussian_to_scgcpot(m(:,i), C(:,:,i), W(:,:,i), cdom, ns, cnodes, evidence);
|
wolffd@0
|
41 pot{i} = scgcpot(ss, cpsize, 1, m(:,i), W(:,:,i), C(:,:,i));
|
wolffd@0
|
42 end
|
wolffd@0
|
43
|
wolffd@0
|
44 pot = scgpot(ddom, cheaddom, ctaildom, ens, pot);
|
wolffd@0
|
45
|
wolffd@0
|
46
|
wolffd@0
|
47 function pot = linear_gaussian_to_scgcpot(mu, Sigma, W, domain, ns, cnodes, evidence)
|
wolffd@0
|
48 % LINEAR_GAUSSIAN_TO_CPOT Convert a linear Gaussian CPD to a stable conditional potential element.
|
wolffd@0
|
49 % pot = linear_gaussian_to_cpot(mu, Sigma, W, domain, ns, cnodes, evidence)
|
wolffd@0
|
50
|
wolffd@0
|
51 p = 1;
|
wolffd@0
|
52 A = mu;
|
wolffd@0
|
53 B = W;
|
wolffd@0
|
54 C = Sigma;
|
wolffd@0
|
55 ns(odom) = 0;
|
wolffd@0
|
56 %pot = scgcpot(, ns(domain), p, A, B, C);
|
wolffd@0
|
57
|
wolffd@0
|
58
|