matthiasm@8: function T = convert_to_table(CPD, domain, evidence) matthiasm@8: % CONVERT_TO_TABLE Convert a softmax CPD to a table, incorporating any evidence matthiasm@8: % T = convert_to_table(CPD, domain, evidence) matthiasm@8: matthiasm@8: self = domain(end); matthiasm@8: ps = domain(1:end-1); matthiasm@8: cnodes = domain(CPD.cpndx); matthiasm@8: cps = myintersect(ps, cnodes); matthiasm@8: dps = domain(CPD.dpndx); matthiasm@8: dps_as_cps = domain(CPD.dps_as_cps.ndx); matthiasm@8: all_dps = union(dps,dps_as_cps); matthiasm@8: odom = domain(~isemptycell(evidence(domain))); matthiasm@8: if ~isempty(cps), assert(myismember(cps, odom)); end % all cts parents must be observed matthiasm@8: matthiasm@8: ns = zeros(1, max(domain)); matthiasm@8: ns(domain) = CPD.sizes; matthiasm@8: ens = ns; % effective node sizes matthiasm@8: ens(odom) = 1; matthiasm@8: matthiasm@8: % dpsize >= glimsz because the glm parameters are tied across the dps_as_cps parents matthiasm@8: dpsize = prod(ens(all_dps)); % size of ALL self'discrete parents matthiasm@8: dpvals = cat(1, evidence{myintersect(all_dps, odom)}); matthiasm@8: cpvals = cat(1, evidence{cps}); matthiasm@8: if ~isempty(dps_as_cps), matthiasm@8: separator = CPD.dps_as_cps.separator; matthiasm@8: dp_as_cpmap = find_equiv_posns(dps_as_cps, all_dps); matthiasm@8: dops_map = find_equiv_posns(myintersect(all_dps, odom), all_dps); matthiasm@8: puredp_map = find_equiv_posns(dps, all_dps); matthiasm@8: subs = ind2subv(ens(all_dps), 1:prod(ens(all_dps))); matthiasm@8: if ~isempty(dops_map), subs(:,dops_map) = subs(:,dops_map)+repmat(dpvals(:)',[size(subs,1) 1])-1; end matthiasm@8: end matthiasm@8: matthiasm@8: [w,b] = extract_params(CPD); matthiasm@8: T = zeros(dpsize, ns(self)); matthiasm@8: for i=1:dpsize, matthiasm@8: active_glm = i; matthiasm@8: dp_as_cpvals=zeros(1,sum(ns(dps_as_cps))); matthiasm@8: if ~isempty(dps_as_cps), matthiasm@8: active_glm = max([1,subv2ind(ns(dps), subs(i,puredp_map))]); matthiasm@8: % Extract the params compatible with the observations (if any) on the 'pure' discrete parents (if any) matthiasm@8: where_one = separator + subs(i,dp_as_cpmap); matthiasm@8: % and get in the dp_as_cp parents... matthiasm@8: dp_as_cpvals(where_one)=1; matthiasm@8: end matthiasm@8: T(i,:) = normalise(exp([dp_as_cpvals(:); cpvals(:)]'*w(:,:,active_glm) + b(:,active_glm)')); matthiasm@8: end matthiasm@8: if myismember(self, odom) matthiasm@8: r = evidence{self}; matthiasm@8: T = T(:,r); matthiasm@8: end matthiasm@8: matthiasm@8: T = myreshape(T, ens(domain));