annotate toolboxes/FullBNT-1.0.7/bnt/CPDs/@gaussian_CPD/convert_to_pot.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 = convert_to_pot(CPD, pot_type, domain, evidence)
Daniel@0 2 % CONVERT_TO_POT Convert a Gaussian CPD to one or more potentials
Daniel@0 3 % pot = convert_to_pot(CPD, pot_type, domain, evidence)
Daniel@0 4
Daniel@0 5 sz = CPD.sizes;
Daniel@0 6 ns = zeros(1, max(domain));
Daniel@0 7 ns(domain) = sz;
Daniel@0 8
Daniel@0 9 odom = domain(~isemptycell(evidence(domain)));
Daniel@0 10 ps = domain(1:end-1);
Daniel@0 11 cps = ps(CPD.cps);
Daniel@0 12 dps = ps(CPD.dps);
Daniel@0 13 self = domain(end);
Daniel@0 14 cdom = [cps(:)' self];
Daniel@0 15 ddom = dps;
Daniel@0 16 cnodes = cdom;
Daniel@0 17
Daniel@0 18 switch pot_type
Daniel@0 19 case 'u',
Daniel@0 20 error('gaussian utility potentials not yet supported');
Daniel@0 21
Daniel@0 22 case 'd',
Daniel@0 23 T = convert_to_table(CPD, domain, evidence);
Daniel@0 24 ns(odom) = 1;
Daniel@0 25 pot = dpot(domain, ns(domain), T);
Daniel@0 26
Daniel@0 27 case {'c','g'},
Daniel@0 28 [m, C, W] = gaussian_CPD_params_given_dps(CPD, domain, evidence);
Daniel@0 29 pot = linear_gaussian_to_cpot(m, C, W, domain, ns, cnodes, evidence);
Daniel@0 30
Daniel@0 31 case 'cg',
Daniel@0 32 [m, C, W] = gaussian_CPD_params_given_dps(CPD, domain, evidence);
Daniel@0 33 % Convert each conditional Gaussian to a canonical potential
Daniel@0 34 cobs = myintersect(cdom, odom);
Daniel@0 35 dobs = myintersect(ddom, odom);
Daniel@0 36 ens = ns; % effective node size
Daniel@0 37 ens(cobs) = 0;
Daniel@0 38 ens(dobs) = 1;
Daniel@0 39 dpsize = prod(ens(dps));
Daniel@0 40 can = cell(1, dpsize);
Daniel@0 41 for i=1:dpsize
Daniel@0 42 if isempty(W)
Daniel@0 43 can{i} = linear_gaussian_to_cpot(m(:,i), C(:,:,i), [], cdom, ns, cnodes, evidence);
Daniel@0 44 else
Daniel@0 45 can{i} = linear_gaussian_to_cpot(m(:,i), C(:,:,i), W(:,:,i), cdom, ns, cnodes, evidence);
Daniel@0 46 end
Daniel@0 47 end
Daniel@0 48 pot = cgpot(ddom, cdom, ens, can);
Daniel@0 49
Daniel@0 50 case 'scg',
Daniel@0 51 [m, C, W] = gaussian_CPD_params_given_dps(CPD, domain, evidence);
Daniel@0 52 cobs = myintersect(cdom, odom);
Daniel@0 53 dobs = myintersect(ddom, odom);
Daniel@0 54 ens = ns; % effective node size
Daniel@0 55 ens(cobs) = 0;
Daniel@0 56 ens(dobs) = 1;
Daniel@0 57 dpsize = prod(ens(dps));
Daniel@0 58 cpsize = size(W, 2); % cts parents size
Daniel@0 59 ss = size(m, 1); % self size
Daniel@0 60 cheaddom = self;
Daniel@0 61 ctaildom = cps(:)';
Daniel@0 62 pot_array = cell(1, dpsize);
Daniel@0 63 for i=1:dpsize
Daniel@0 64 pot_array{i} = scgcpot(ss, cpsize, 1, m(:,i), W(:,:,i), C(:,:,i));
Daniel@0 65 end
Daniel@0 66 pot = scgpot(ddom, cheaddom, ctaildom, ens, pot_array);
Daniel@0 67
Daniel@0 68 otherwise,
Daniel@0 69 error(['unrecognized pot_type' pot_type])
Daniel@0 70 end
Daniel@0 71