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