annotate _FullBNT/BNT/CPDs/@softmax_CPD/convert_to_table.m @ 9:4ea6619cb3f5 tip

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