annotate toolboxes/FullBNT-1.0.7/bnt/CPDs/@gaussian_CPD/CPD_to_scgpot.m @ 0:cc4b1211e677 tip

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