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