annotate toolboxes/FullBNT-1.0.7/bnt/general/linear_gaussian_to_cpot.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 pot = linear_gaussian_to_cpot(mu, Sigma, W, domain, ns, cnodes, evidence)
Daniel@0 2 % LINEAR_GAUSSIAN_TO_CPOT Convert a linear Gaussian CPD to a canonical potential.
Daniel@0 3 % pot = linear_gaussian_to_cpot(mu, Sigma, W, domain, ns, cnodes, evidence)
Daniel@0 4 %
Daniel@0 5 % We include any cts evidence, but ignore any discrete evidence.
Daniel@0 6 % (Use gaussian_CPD_params_given_dps to use discrete evidence to select mu, Sigma, W.)
Daniel@0 7
Daniel@0 8 odom = domain(~isemptycell(evidence(domain)));
Daniel@0 9 hdom = domain(isemptycell(evidence(domain)));
Daniel@0 10 cobs = myintersect(cnodes, odom);
Daniel@0 11 chid = myintersect(cnodes, hdom);
Daniel@0 12 cvals = cat(1, evidence{cobs});
Daniel@0 13
Daniel@0 14 %[g,h,K] = gaussian_to_canonical(mu, Sigma, W);
Daniel@0 15 Sinv = inv(Sigma);
Daniel@0 16 g = -0.5*mu'*Sinv*mu + log(normal_coef(Sigma));
Daniel@0 17 if isempty(W) | (size(W,2)==0) % no cts parents
Daniel@0 18 h = Sinv*mu;
Daniel@0 19 K = Sinv;
Daniel@0 20 else
Daniel@0 21 h = [-W'*Sinv*mu; Sinv*mu];
Daniel@0 22 K = [W'*Sinv*W -W'*Sinv';
Daniel@0 23 -Sinv*W Sinv];
Daniel@0 24 end
Daniel@0 25
Daniel@0 26 if ~isempty(cvals)
Daniel@0 27 %[g, h, K] = enter_evidence_canonical(g, h, K, chid, cobs, cvals(:), ns);
Daniel@0 28 [hx, hy, KXX, KXY, KYX, KYY] = partition_matrix_vec(h, K, chid, cobs, ns);
Daniel@0 29 y = cvals(:);
Daniel@0 30 g = g + hy'*y - 0.5*y'*KYY*y;
Daniel@0 31 if length(hx)==0 % isempty(X) % i.e., we have instantiated everything away
Daniel@0 32 h = [];
Daniel@0 33 K = [];
Daniel@0 34 else
Daniel@0 35 h = hx - KXY*y;
Daniel@0 36 K = KXX;
Daniel@0 37 end
Daniel@0 38 end
Daniel@0 39
Daniel@0 40 ns(odom) = 0;
Daniel@0 41 pot = cpot(domain, ns(domain), g, h, K);
Daniel@0 42
Daniel@0 43