Daniel@0: function pot = convert_to_pot(CPD, pot_type, domain, evidence) Daniel@0: % CONVERT_TO_POT Convert a Gaussian CPD to one or more potentials Daniel@0: % pot = convert_to_pot(CPD, pot_type, domain, evidence) Daniel@0: Daniel@0: sz = CPD.sizes; Daniel@0: ns = zeros(1, max(domain)); Daniel@0: ns(domain) = sz; Daniel@0: Daniel@0: odom = domain(~isemptycell(evidence(domain))); Daniel@0: ps = domain(1:end-1); Daniel@0: cps = ps(CPD.cps); Daniel@0: dps = ps(CPD.dps); Daniel@0: self = domain(end); Daniel@0: cdom = [cps(:)' self]; Daniel@0: ddom = dps; Daniel@0: cnodes = cdom; Daniel@0: Daniel@0: switch pot_type Daniel@0: case 'u', Daniel@0: error('gaussian utility potentials not yet supported'); Daniel@0: Daniel@0: case 'd', Daniel@0: T = convert_to_table(CPD, domain, evidence); Daniel@0: ns(odom) = 1; Daniel@0: pot = dpot(domain, ns(domain), T); Daniel@0: Daniel@0: case {'c','g'}, Daniel@0: [m, C, W] = gaussian_CPD_params_given_dps(CPD, domain, evidence); Daniel@0: pot = linear_gaussian_to_cpot(m, C, W, domain, ns, cnodes, evidence); Daniel@0: Daniel@0: case 'cg', Daniel@0: [m, C, W] = gaussian_CPD_params_given_dps(CPD, domain, evidence); Daniel@0: % Convert each conditional Gaussian to a canonical potential Daniel@0: cobs = myintersect(cdom, odom); Daniel@0: dobs = myintersect(ddom, odom); Daniel@0: ens = ns; % effective node size Daniel@0: ens(cobs) = 0; Daniel@0: ens(dobs) = 1; Daniel@0: dpsize = prod(ens(dps)); Daniel@0: can = cell(1, dpsize); Daniel@0: for i=1:dpsize Daniel@0: if isempty(W) Daniel@0: can{i} = linear_gaussian_to_cpot(m(:,i), C(:,:,i), [], cdom, ns, cnodes, evidence); Daniel@0: else Daniel@0: can{i} = linear_gaussian_to_cpot(m(:,i), C(:,:,i), W(:,:,i), cdom, ns, cnodes, evidence); Daniel@0: end Daniel@0: end Daniel@0: pot = cgpot(ddom, cdom, ens, can); Daniel@0: Daniel@0: case 'scg', Daniel@0: [m, C, W] = gaussian_CPD_params_given_dps(CPD, domain, evidence); Daniel@0: cobs = myintersect(cdom, odom); Daniel@0: dobs = myintersect(ddom, odom); Daniel@0: ens = ns; % effective node size Daniel@0: ens(cobs) = 0; Daniel@0: ens(dobs) = 1; Daniel@0: dpsize = prod(ens(dps)); Daniel@0: cpsize = size(W, 2); % cts parents size Daniel@0: ss = size(m, 1); % self size Daniel@0: cheaddom = self; Daniel@0: ctaildom = cps(:)'; Daniel@0: pot_array = cell(1, dpsize); Daniel@0: for i=1:dpsize Daniel@0: pot_array{i} = scgcpot(ss, cpsize, 1, m(:,i), W(:,:,i), C(:,:,i)); Daniel@0: end Daniel@0: pot = scgpot(ddom, cheaddom, ctaildom, ens, pot_array); Daniel@0: Daniel@0: otherwise, Daniel@0: error(['unrecognized pot_type' pot_type]) Daniel@0: end Daniel@0: