annotate toolboxes/FullBNT-1.0.7/bnt/CPDs/@softmax_CPD/convert_to_table.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
rev   line source
wolffd@0 1 function T = convert_to_table(CPD, domain, evidence)
wolffd@0 2 % CONVERT_TO_TABLE Convert a softmax CPD to a table, incorporating any evidence
wolffd@0 3 % T = convert_to_table(CPD, domain, evidence)
wolffd@0 4
wolffd@0 5 self = domain(end);
wolffd@0 6 ps = domain(1:end-1);
wolffd@0 7 cnodes = domain(CPD.cpndx);
wolffd@0 8 cps = myintersect(ps, cnodes);
wolffd@0 9 dps = domain(CPD.dpndx);
wolffd@0 10 dps_as_cps = domain(CPD.dps_as_cps.ndx);
wolffd@0 11 all_dps = union(dps,dps_as_cps);
wolffd@0 12 odom = domain(~isemptycell(evidence(domain)));
wolffd@0 13 if ~isempty(cps), assert(myismember(cps, odom)); end % all cts parents must be observed
wolffd@0 14
wolffd@0 15 ns = zeros(1, max(domain));
wolffd@0 16 ns(domain) = CPD.sizes;
wolffd@0 17 ens = ns; % effective node sizes
wolffd@0 18 ens(odom) = 1;
wolffd@0 19
wolffd@0 20 % dpsize >= glimsz because the glm parameters are tied across the dps_as_cps parents
wolffd@0 21 dpsize = prod(ens(all_dps)); % size of ALL self'discrete parents
wolffd@0 22 dpvals = cat(1, evidence{myintersect(all_dps, odom)});
wolffd@0 23 cpvals = cat(1, evidence{cps});
wolffd@0 24 if ~isempty(dps_as_cps),
wolffd@0 25 separator = CPD.dps_as_cps.separator;
wolffd@0 26 dp_as_cpmap = find_equiv_posns(dps_as_cps, all_dps);
wolffd@0 27 dops_map = find_equiv_posns(myintersect(all_dps, odom), all_dps);
wolffd@0 28 puredp_map = find_equiv_posns(dps, all_dps);
wolffd@0 29 subs = ind2subv(ens(all_dps), 1:prod(ens(all_dps)));
wolffd@0 30 if ~isempty(dops_map), subs(:,dops_map) = subs(:,dops_map)+repmat(dpvals(:)',[size(subs,1) 1])-1; end
wolffd@0 31 end
wolffd@0 32
wolffd@0 33 [w,b] = extract_params(CPD);
wolffd@0 34 T = zeros(dpsize, ns(self));
wolffd@0 35 for i=1:dpsize,
wolffd@0 36 active_glm = i;
wolffd@0 37 dp_as_cpvals=zeros(1,sum(ns(dps_as_cps)));
wolffd@0 38 if ~isempty(dps_as_cps),
wolffd@0 39 active_glm = max([1,subv2ind(ns(dps), subs(i,puredp_map))]);
wolffd@0 40 % Extract the params compatible with the observations (if any) on the 'pure' discrete parents (if any)
wolffd@0 41 where_one = separator + subs(i,dp_as_cpmap);
wolffd@0 42 % and get in the dp_as_cp parents...
wolffd@0 43 dp_as_cpvals(where_one)=1;
wolffd@0 44 end
wolffd@0 45 T(i,:) = normalise(exp([dp_as_cpvals(:); cpvals(:)]'*w(:,:,active_glm) + b(:,active_glm)'));
wolffd@0 46 end
wolffd@0 47 if myismember(self, odom)
wolffd@0 48 r = evidence{self};
wolffd@0 49 T = T(:,r);
wolffd@0 50 end
wolffd@0 51
wolffd@0 52 T = myreshape(T, ens(domain));