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