diff options
| author | ziejd2 | 2017-09-28 15:04:40 -0500 |
|---|---|---|
| committer | ziejd2 | 2017-09-28 15:04:40 -0500 |
| commit | 8070dc963753142bb86c4ed698d91fd623ed28e7 (patch) | |
| tree | d0f6dd8fc46a49b819aa55c1a90faa14d8448883 /sourcecodes/bnt-master/BNT/potentials/@cgpot | |
| parent | 7cc31810d53176e805532b2789955f4eedbce6bb (diff) | |
| download | BNW-8070dc963753142bb86c4ed698d91fd623ed28e7.tar.gz | |
BNW using Octave instead of Matlab.
This version of BNW should perform the same as the original version. The only difference is that it uses Octave instead of Matlab when running BayesNet Toolbox during parameter learning. I am calling this BNW_1.02. It can be accessed at: compbio.uthsc.edu/BNW_1.02
Diffstat (limited to 'sourcecodes/bnt-master/BNT/potentials/@cgpot')
24 files changed, 513 insertions, 0 deletions
diff --git a/sourcecodes/bnt-master/BNT/potentials/@cgpot/CVS/Entries b/sourcecodes/bnt-master/BNT/potentials/@cgpot/CVS/Entries new file mode 100644 index 00000000..25f4b469 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/potentials/@cgpot/CVS/Entries @@ -0,0 +1,15 @@ +/cg_can_to_mom.m/1.1.1.1/Wed May 29 15:59:58 2002// +/cg_mom_to_can.m/1.1.1.1/Wed May 29 15:59:58 2002// +/cgpot.m/1.1.1.1/Wed May 29 15:59:58 2002// +/display.m/1.1.1.1/Wed May 29 15:59:58 2002// +/divide_by_pot.m/1.1.1.1/Wed May 29 15:59:58 2002// +/domain_pot.m/1.1.1.1/Wed May 29 15:59:58 2002// +/enter_cts_evidence_pot.m/1.1.1.1/Wed May 29 15:59:58 2002// +/enter_discrete_evidence_pot.m/1.1.1.1/Wed May 29 15:59:58 2002// +/marginalize_pot.m/1.1.1.1/Wed May 29 15:59:58 2002// +/multiply_by_pot.m/1.1.1.1/Wed May 29 15:59:58 2002// +/multiply_pots.m/1.1.1.1/Wed May 29 15:59:58 2002// +/normalize_pot.m/1.1.1.1/Wed May 29 15:59:58 2002// +/pot_to_marginal.m/1.1.1.1/Wed May 29 15:59:58 2002// +/set_domain_pot.m/1.1.1.1/Wed Jul 30 13:38:24 2003// +D diff --git a/sourcecodes/bnt-master/BNT/potentials/@cgpot/CVS/Entries.Log b/sourcecodes/bnt-master/BNT/potentials/@cgpot/CVS/Entries.Log new file mode 100644 index 00000000..24f16336 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/potentials/@cgpot/CVS/Entries.Log @@ -0,0 +1 @@ +A D/Old//// diff --git a/sourcecodes/bnt-master/BNT/potentials/@cgpot/CVS/Repository b/sourcecodes/bnt-master/BNT/potentials/@cgpot/CVS/Repository new file mode 100644 index 00000000..ee9924e1 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/potentials/@cgpot/CVS/Repository @@ -0,0 +1 @@ +FullBNT/BNT/potentials/@cgpot diff --git a/sourcecodes/bnt-master/BNT/potentials/@cgpot/CVS/Root b/sourcecodes/bnt-master/BNT/potentials/@cgpot/CVS/Root new file mode 100644 index 00000000..f3bd14a6 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/potentials/@cgpot/CVS/Root @@ -0,0 +1 @@ +:ext:nsaunier@bnt.cvs.sourceforge.net:/cvsroot/bnt diff --git a/sourcecodes/bnt-master/BNT/potentials/@cgpot/Old/CVS/Entries b/sourcecodes/bnt-master/BNT/potentials/@cgpot/Old/CVS/Entries new file mode 100644 index 00000000..680a9046 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/potentials/@cgpot/Old/CVS/Entries @@ -0,0 +1,4 @@ +/normalize_pot.m/1.1.1.1/Wed May 29 15:59:58 2002// +/set_domain_pot.m/1.1.1.1/Wed Jul 30 13:38:08 2003// +/simple_marginalize_pot.m/1.1.1.1/Wed May 29 15:59:58 2002// +D diff --git a/sourcecodes/bnt-master/BNT/potentials/@cgpot/Old/CVS/Repository b/sourcecodes/bnt-master/BNT/potentials/@cgpot/Old/CVS/Repository new file mode 100644 index 00000000..7feb051e --- /dev/null +++ b/sourcecodes/bnt-master/BNT/potentials/@cgpot/Old/CVS/Repository @@ -0,0 +1 @@ +FullBNT/BNT/potentials/@cgpot/Old diff --git a/sourcecodes/bnt-master/BNT/potentials/@cgpot/Old/CVS/Root b/sourcecodes/bnt-master/BNT/potentials/@cgpot/Old/CVS/Root new file mode 100644 index 00000000..f3bd14a6 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/potentials/@cgpot/Old/CVS/Root @@ -0,0 +1 @@ +:ext:nsaunier@bnt.cvs.sourceforge.net:/cvsroot/bnt diff --git a/sourcecodes/bnt-master/BNT/potentials/@cgpot/Old/normalize_pot.m b/sourcecodes/bnt-master/BNT/potentials/@cgpot/Old/normalize_pot.m new file mode 100644 index 00000000..1debd5bc --- /dev/null +++ b/sourcecodes/bnt-master/BNT/potentials/@cgpot/Old/normalize_pot.m @@ -0,0 +1,24 @@ +function [pot, loglik] = normalize_pot(pot) +% NORMALIZE_POT Convert the CG potential Pr(X,E) into Pr(X|E) and return log Pr(E). +% [pot, loglik] = normalize_pot(pot) + +% Marginalize down to [], so that the normalizing constant becomes Pr(E) +temp = marginalize_pot(cg_can_to_mom(pot), []); +%loglik = temp.mom{1}.logp; +[temp2, loglik] = normalize_pot(temp.mom{1}); + +% Adjust scale factor to reflect the fact that the pot now represents Pr(X | E) instead of Pr(X,E). + +scale = -loglik; +if 1 +switch pot.subtype + case 'm' + for i=1:pot.dsize + pot.mom{i} = rescale_pot(pot.mom{i}, scale); + end + case 'c' + for i=1:pot.dsize + pot.can{i} = rescale_pot(pot.can{i}, scale); + end +end +end diff --git a/sourcecodes/bnt-master/BNT/potentials/@cgpot/Old/set_domain_pot.m b/sourcecodes/bnt-master/BNT/potentials/@cgpot/Old/set_domain_pot.m new file mode 100644 index 00000000..f0df6ea7 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/potentials/@cgpot/Old/set_domain_pot.m @@ -0,0 +1,9 @@ +function pot = set_domain_pot(pot, domain) +% SET_DOMAIN_POT Change the domain of a potential (cgpot) +% pot = set_domain_pot(pot, domain) + +delta = domain(1) - pot.domain(1); +assert(all(domain == pot.domain + delta)); +pot.domain = pot.domain + delta; +pot.ddom = pot.ddom + delta; +pot.cdom = pot.cdom + delta; diff --git a/sourcecodes/bnt-master/BNT/potentials/@cgpot/Old/simple_marginalize_pot.m b/sourcecodes/bnt-master/BNT/potentials/@cgpot/Old/simple_marginalize_pot.m new file mode 100644 index 00000000..a2774636 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/potentials/@cgpot/Old/simple_marginalize_pot.m @@ -0,0 +1,86 @@ +function smallpot = marginalize_pot(bigpot, keep) +% MARGINALIZE_POT Marginalize a cgpot onto a smaller domain. +% smallpot = marginalize_pot(bigpot, keep) + +sumover = mysetdiff(bigpot.domain, keep); +csumover = myintersect(sumover, bigpot.cdom); +dsumover = myintersect(sumover, bigpot.ddom); +dkeep = myintersect(keep, bigpot.ddom); +ckeep = myintersect(keep, bigpot.cdom); +%ns = sparse(1, max(bigpot.domain)); % must be full, so I is an integer +ns = zeros(1, max(bigpot.domain)); +ns(bigpot.ddom) = bigpot.dsizes; +ns(bigpot.cdom) = bigpot.csizes; + +% sum(ns(csumover))==0 is like isempty(csumover) but handles observed nodes. +% Similarly, prod(ns(dsumover))==1 is like isempty(dsumover) + +% Marginalize the cts parts. +% If we are in canonical form, we stay that way, since moment form might not exist. +% Besides, we would like to minimize the number of conversions. +if sum(ns(csumover)) > 0 + if bigpot.subtype == 'm' + for i=1:bigpot.dsize + bigpot.mom{i} = marginalize_pot(bigpot.mom{i}, ckeep); + end + else + for i=1:bigpot.dsize + bigpot.can{i} = marginalize_pot(bigpot.can{i}, ckeep); + end + end +end + +% If we are not marginalizing over any discrete nodes, we are done. +if prod(ns(dsumover))==1 + smallpot = cgpot(dkeep, ckeep, ns, bigpot.can, bigpot.mom, bigpot.subtype); + return; +end + +% To marginalize the discrete parts, we must be in moment form. +bigpot = cg_can_to_mom(bigpot); + +I = prod(ns(dkeep)); +J = prod(ns(dsumover)); +C = sum(ns(ckeep)); + +% Reshape bigpot into the form mu1(:,j,i), where i is in dkeep, j is in dsumover +T1 = zeros(I,J); +mu1 = zeros(C,J,I); +Sigma1 = zeros(C,C,J,I); +sum_map = find_equiv_posns(dsumover, bigpot.ddom); +keep_map = find_equiv_posns(dkeep, bigpot.ddom); +iv = zeros(1, length(bigpot.ddom)); % index vector +for i=1:I + keep_iv = ind2subv(ns(dkeep), i); + iv(keep_map) = keep_iv; + for j=1:J + sum_iv = ind2subv(ns(dsumover), j); + iv(sum_map) = sum_iv; + k = subv2ind(ns(bigpot.ddom), iv); + mom = struct(bigpot.mom{k}); % violate object privacy + T1(i,j) = exp(mom.logp); + if C > 0 % so mu1 and Sigma1 are non-empty + mu1(:,j,i) = mom.mu; + Sigma1(:,:,j,i) = mom.Sigma; + end + end +end + +% Collapse the mixture of Gaussians +coef = mk_stochastic(T1); % coef must be convex combination +T2 = sum(T1,2); +T2 = T2 + (T2==0)*eps; +%if C > 0, disp('collapsing onto '); disp(leep); end +mu = []; +Sigma = []; +mom = cell(1,I); +for i=1:I + if C > 0 + [mu, Sigma] = collapse_mog(mu1(:,:,i), Sigma1(:,:,:,i), coef(i,:)); + end + logp = log(T2(i)); + mom{i} = mpot(ckeep, ns(ckeep), logp, mu, Sigma); +end + +smallpot = cgpot(dkeep, ckeep, ns, [], mom, 'm'); + diff --git a/sourcecodes/bnt-master/BNT/potentials/@cgpot/cg_can_to_mom.m b/sourcecodes/bnt-master/BNT/potentials/@cgpot/cg_can_to_mom.m new file mode 100644 index 00000000..951dc910 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/potentials/@cgpot/cg_can_to_mom.m @@ -0,0 +1,10 @@ +function pot = cg_can_to_mom(pot) +% CG_CAN_TO_MOM Convert a CG potential from canonical to moment form, if necessary. +% pot = cg_can_to_mom(pot) + +if pot.subtype ~= 'm' + for i=1:pot.dsize + pot.mom{i} = cpot_to_mpot(pot.can{i}); + end + pot.subtype = 'm'; +end diff --git a/sourcecodes/bnt-master/BNT/potentials/@cgpot/cg_mom_to_can.m b/sourcecodes/bnt-master/BNT/potentials/@cgpot/cg_mom_to_can.m new file mode 100644 index 00000000..61aa053f --- /dev/null +++ b/sourcecodes/bnt-master/BNT/potentials/@cgpot/cg_mom_to_can.m @@ -0,0 +1,10 @@ +function pot = cg_mom_to_can(pot) +% CG_MOM_TO_CAN Convert a CG potential from moment to canonical form, if necessary. +% pot = cg_mom_to_can(pot) + +if pot.subtype ~= 'c' + for i=1:pot.dsize + pot.can{i} = mpot_to_cpot(pot.mom{i}); + end + pot.subtype = 'c'; +end diff --git a/sourcecodes/bnt-master/BNT/potentials/@cgpot/cgpot.m b/sourcecodes/bnt-master/BNT/potentials/@cgpot/cgpot.m new file mode 100644 index 00000000..676e3ce1 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/potentials/@cgpot/cgpot.m @@ -0,0 +1,38 @@ +function pot = cgpot(ddom, cdom, node_sizes, can, mom, subtype) +% CPOT Make a canonical CG potential. +% function pot = cgpot(ddom, cdom, node_sizes, can, mom, subtype) +% +% node_sizes(i) is the size of the i'th node. +% can and mom default to 0s. +% subtype defaults to 'c'. + +if nargin < 6, subtype = 'c'; end + +pot.ddom = ddom; +pot.cdom = cdom; +node_sizes = node_sizes(:)'; % row vectors print better +pot.domain = myunion(ddom, cdom); +pot.dsizes = node_sizes(pot.ddom); +pot.dsize = prod(node_sizes(pot.ddom)); +pot.csizes = node_sizes(pot.cdom); +pot.csize = sum(node_sizes(pot.cdom)); +pot.subtype = subtype; + +if nargin < 4 + can = cell(1, pot.dsize); + for i=1:pot.dsize + can{i} = cpot(cdom, node_sizes(cdom)); + end +end +pot.can = can; + +if nargin < 5 + mom = cell(1, pot.dsize); + for i=1:pot.dsize + mom{i} = mpot(cdom, node_sizes(cdom)); + end +end +pot.mom = mom; + +pot = class(pot, 'cgpot'); + diff --git a/sourcecodes/bnt-master/BNT/potentials/@cgpot/display.m b/sourcecodes/bnt-master/BNT/potentials/@cgpot/display.m new file mode 100644 index 00000000..78813bec --- /dev/null +++ b/sourcecodes/bnt-master/BNT/potentials/@cgpot/display.m @@ -0,0 +1,4 @@ +function display(pot) + +disp('conditional Gaussian potential object'); +disp(struct(pot)); diff --git a/sourcecodes/bnt-master/BNT/potentials/@cgpot/divide_by_pot.m b/sourcecodes/bnt-master/BNT/potentials/@cgpot/divide_by_pot.m new file mode 100644 index 00000000..d3c6e7f9 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/potentials/@cgpot/divide_by_pot.m @@ -0,0 +1,19 @@ +function bigpot = divide_by_pot(bigpot, smallpot) +% DIVIDE_BY_POT bigpot /= smallpot for cgpot +% bigpot = divide_by_pot(bigpot, smallpot) +% +% smallpot's domain must be a subset of bigpot's domain. + +bigpot = cg_mom_to_can(bigpot); +smallpot = cg_mom_to_can(smallpot); + +mask = find_equiv_posns(smallpot.ddom, bigpot.ddom); +for i=1:bigpot.dsize + if isempty(smallpot.ddom) + src = 1; + else + sub = ind2subv(bigpot.dsizes, i); + src = subv2ind(smallpot.dsizes, sub(mask)); + end + bigpot.can{i} = divide_by_pot(bigpot.can{i}, smallpot.can{src}); +end diff --git a/sourcecodes/bnt-master/BNT/potentials/@cgpot/domain_pot.m b/sourcecodes/bnt-master/BNT/potentials/@cgpot/domain_pot.m new file mode 100644 index 00000000..a09e39e6 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/potentials/@cgpot/domain_pot.m @@ -0,0 +1,5 @@ +function dom = domain_pot(pot) +% DOMAIN_POT Return the domain of this cgpot. +% dom = domain_pot(pot) + +dom = pot.domain; diff --git a/sourcecodes/bnt-master/BNT/potentials/@cgpot/enter_cts_evidence_pot.m b/sourcecodes/bnt-master/BNT/potentials/@cgpot/enter_cts_evidence_pot.m new file mode 100644 index 00000000..e5267bbd --- /dev/null +++ b/sourcecodes/bnt-master/BNT/potentials/@cgpot/enter_cts_evidence_pot.m @@ -0,0 +1,8 @@ +function pot = enter_cts_evidence_pot(pot, Y, y) +% function pot = enter_cts_evidence_pot(pot, Y, y) cgpot + + +pot = cg_mom_to_can(pot); +for i=1:pot.dsize + pot.can{i} = enter_cts_evidence_pot(pot.can{i}, Y, y); +end diff --git a/sourcecodes/bnt-master/BNT/potentials/@cgpot/enter_discrete_evidence_pot.m b/sourcecodes/bnt-master/BNT/potentials/@cgpot/enter_discrete_evidence_pot.m new file mode 100644 index 00000000..972df966 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/potentials/@cgpot/enter_discrete_evidence_pot.m @@ -0,0 +1,16 @@ +function pot = enter_discrete_evidence_pot(pot, Y, y) + +%ns = sparse(1, max(pot.domain)); +ns = zeros(1, max(pot.domain)); +ns(pot.ddom) = pot.dsizes; +ns(pot.cdom) = pot.csizes; + +ddom = pot.ddom; +S = prod(ns(ddom)); +sub = ind2subv(ns(ddom), 1:S); +mask = find_equiv_posns(Y, ddom); +sub(mask) = y; +ndx = subv2ind(ns(ddom), sub); + +pot.can = pot.can(ndx); +pot.mom = pot.mom(ndx); diff --git a/sourcecodes/bnt-master/BNT/potentials/@cgpot/marginalize_pot.m b/sourcecodes/bnt-master/BNT/potentials/@cgpot/marginalize_pot.m new file mode 100644 index 00000000..4e666b1a --- /dev/null +++ b/sourcecodes/bnt-master/BNT/potentials/@cgpot/marginalize_pot.m @@ -0,0 +1,153 @@ +function smallpot = marginalize_pot(bigpot, keep, maximize, useC) +% MARGINALIZE_POT Marginalize a cgpot onto a smaller domain. +% smallpot = marginalize_pot(bigpot, keep, maximize, useC) +% +% If maximize = 1, we raise an error. +% useC is ignored. + +if nargin < 3, maximize = 0; end +assert(~maximize); + + +sumover = mysetdiff(bigpot.domain, keep); +csumover = myintersect(sumover, bigpot.cdom); +dsumover = myintersect(sumover, bigpot.ddom); +dkeep = myintersect(keep, bigpot.ddom); +ckeep = myintersect(keep, bigpot.cdom); +%ns = sparse(1, max(bigpot.domain)); % must be full, so I is an integer +ns = zeros(1, max(bigpot.domain)); +ns(bigpot.ddom) = bigpot.dsizes; +ns(bigpot.cdom) = bigpot.csizes; + +% sum(ns(csumover))==0 is like isempty(csumover) but handles observed nodes. +% Similarly, prod(ns(dsumover))==1 is like isempty(dsumover) + +% Marginalize the cts parts. +% If we are in canonical form, we stay that way, since moment form might not exist. +% Besides, we would like to minimize the number of conversions. +if sum(ns(csumover)) > 0 + if bigpot.subtype == 'm' + for i=1:bigpot.dsize + bigpot.mom{i} = marginalize_pot(bigpot.mom{i}, ckeep); + end + else + for i=1:bigpot.dsize + bigpot.can{i} = marginalize_pot(bigpot.can{i}, ckeep); + end + end +end + +% If we are not marginalizing over any discrete nodes, we are done. +if prod(ns(dsumover))==1 + smallpot = cgpot(dkeep, ckeep, ns, bigpot.can, bigpot.mom, bigpot.subtype); + return; +end + +% To marginalize the discrete parts, we partition the cts parts into those that depend +% on dkeep (i) and those that depend on on dsumover (j). + +I = prod(ns(dkeep)); +J = prod(ns(dsumover)); +C = sum(ns(ckeep)); +sum_map = find_equiv_posns(dsumover, bigpot.ddom); +keep_map = find_equiv_posns(dkeep, bigpot.ddom); +iv = zeros(1, length(bigpot.ddom)); % index vector + +% If in canonical form, marginalize if possible, else convert to moment form. +if 0 && bigpot.subtype == 'c' + p1 = zeros(I,J); + h1 = zeros(C,J,I); + K1 = zeros(C,C,J,I); + for i=1:I + keep_iv = ind2subv(ns(dkeep), i); + iv(keep_map) = keep_iv; + for j=1:J + sum_iv = ind2subv(ns(dsumover), j); + iv(sum_map) = sum_iv; + k = subv2ind(ns(bigpot.ddom), iv); + can = struct(bigpot.can{k}); % violate object privacy + p1(i,j) = exp(can.g); + if C > 0 % so mu1 and Sigma1 are non-empty + h1(:,j,i) = can.h; + K1(:,:,j,i) = can.K; + end + end + end + + % If the cts parts do not depend on j, we can just marginalize the weighting coefficient g. + jdepends = 0; + for i=1:I + for j=2:J + if ~approxeq(h1(:,j,i), h1(:,1,i)) | ~approxeq(K1(:,:,j,i), K1(:,:,1,i)) + jdepends = 1; + break + end + end + end + + if ~jdepends + %g2 = log(sum(p1, 2)); + g2 = zeros(I,1); + for i=1:I + s = sum(p1(i,:)); + if s > 0 + g2(i) = log(s); + end + end + h2 = h1; + K2 = K1; + can = cell(1,I); + j = 1; % arbitrary + for i=1:I + can{i} = cpot(ckeep, ns(ckeep), g2(i), h2(:,j,i), K2(:,:,j,i)); + end + smallpot = cgpot(dkeep, ckeep, ns, can, [], 'c'); + return; + else + % Since the cts parts depend on j, we must convert to moment form + bigpot = cg_can_to_mom(bigpot); + end +end + + +% Marginalize in moment form +bigpot = cg_can_to_mom(bigpot); + +% Now partition the moment components. +T1 = zeros(I,J); +mu1 = zeros(C,J,I); +Sigma1 = zeros(C,C,J,I); +for i=1:I + keep_iv = ind2subv(ns(dkeep), i); + iv(keep_map) = keep_iv; + for j=1:J + sum_iv = ind2subv(ns(dsumover), j); + iv(sum_map) = sum_iv; + k = subv2ind(ns(bigpot.ddom), iv); + mom = struct(bigpot.mom{k}); % violate object privacy + T1(i,j) = exp(mom.logp); + if C > 0 % so mu1 and Sigma1 are non-empty + mu1(:,j,i) = mom.mu; + Sigma1(:,:,j,i) = mom.Sigma; + end + end +end + +% Collapse the mixture of Gaussians +coef = mk_stochastic(T1); % coef must be convex combination +T2 = sum(T1,2); +T2 = T2 + (T2==0)*eps; +%if C > 0, disp('collapsing onto '); disp(leep); end +mu = []; +Sigma = []; +mom = cell(1,I); +for i=1:I + if C > 0 + [mu, Sigma] = collapse_mog(mu1(:,:,i), Sigma1(:,:,:,i), coef(i,:)); + end + logp = log(T2(i)); + mom{i} = mpot(ckeep, ns(ckeep), logp, mu, Sigma); +end + +smallpot = cgpot(dkeep, ckeep, ns, [], mom, 'm'); + diff --git a/sourcecodes/bnt-master/BNT/potentials/@cgpot/multiply_by_pot.m b/sourcecodes/bnt-master/BNT/potentials/@cgpot/multiply_by_pot.m new file mode 100644 index 00000000..0dcad3fc --- /dev/null +++ b/sourcecodes/bnt-master/BNT/potentials/@cgpot/multiply_by_pot.m @@ -0,0 +1,19 @@ +function bigpot = multiply_by_pot(bigpot, smallpot, varargin) +% MULTIPLY_BY_POT bigpot *= smallpot for cgpot +% bigpot = multiply_by_pot(bigpot, smallpot) +% +% smallpot's domain must be a subset of bigpot's domain. + +bigpot = cg_mom_to_can(bigpot); +smallpot = cg_mom_to_can(smallpot); + +mask = find_equiv_posns(smallpot.ddom, bigpot.ddom); +for i=1:bigpot.dsize + if isempty(smallpot.ddom) + src = 1; + else + sub = ind2subv(bigpot.dsizes, i); + src = subv2ind(smallpot.dsizes, sub(mask)); + end + bigpot.can{i} = multiply_by_pot(bigpot.can{i}, smallpot.can{src}); +end diff --git a/sourcecodes/bnt-master/BNT/potentials/@cgpot/multiply_pots.m b/sourcecodes/bnt-master/BNT/potentials/@cgpot/multiply_pots.m new file mode 100644 index 00000000..8035d6de --- /dev/null +++ b/sourcecodes/bnt-master/BNT/potentials/@cgpot/multiply_pots.m @@ -0,0 +1,16 @@ +function T = multiply_pots(T1, T2) +% MULTIPLY_POTS Multiply a pair of dpots together pointwise (cgpot) +% T = multiply_pots(pots) + +ddom = myunion(T1.ddom, T2.ddom); +cdom = myunion(T1.cdom, T2.cdom); +dom = myunion(ddom, cdom); +ns = zeros(1, max(dom)); +ns(T1.ddom) = T1.dsizes; +ns(T2.ddom) = T2.dsizes; +ns(T1.cdom) = T1.csizes; +ns(T2.cdom) = T2.csizes; + +T = cgpot(ddom, cdom, ns); +T = multiply_by_pot(T, T1); +T = multiply_by_pot(T, T2); diff --git a/sourcecodes/bnt-master/BNT/potentials/@cgpot/normalize_pot.m b/sourcecodes/bnt-master/BNT/potentials/@cgpot/normalize_pot.m new file mode 100644 index 00000000..0c05ae5d --- /dev/null +++ b/sourcecodes/bnt-master/BNT/potentials/@cgpot/normalize_pot.m @@ -0,0 +1,24 @@ +function [pot, loglik] = normalize_pot(pot) +% NORMALIZE_POT Convert the CG potential Pr(X,E) into Pr(X|E) and return log Pr(E). +% [pot, loglik] = normalize_pot(pot) + +% Marginalize down to [], so that the normalizing constant becomes Pr(E) +temp = cg_can_to_mom(marginalize_pot(pot, [])); +%loglik = temp.mom{1}.logp; +[temp2, loglik] = normalize_pot(temp.mom{1}); + +% Adjust scale factor to reflect the fact that the pot now represents Pr(X | E) instead of Pr(X,E). + +scale = -loglik; +if 1 +switch pot.subtype + case 'm' + for i=1:pot.dsize + pot.mom{i} = rescale_pot(pot.mom{i}, scale); + end + case 'c' + for i=1:pot.dsize + pot.can{i} = rescale_pot(pot.can{i}, scale); + end +end +end diff --git a/sourcecodes/bnt-master/BNT/potentials/@cgpot/pot_to_marginal.m b/sourcecodes/bnt-master/BNT/potentials/@cgpot/pot_to_marginal.m new file mode 100644 index 00000000..a651e192 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/potentials/@cgpot/pot_to_marginal.m @@ -0,0 +1,27 @@ +function m = pot_to_marginal(pot) +% POT_TO_MARGINAL Convert a cgpot to a marginal structure. +% m = pot_to_marginal(pot) + +pot = cg_can_to_mom(pot); +m.domain = pot.domain; +n = pot.csize; +d = length(pot.mom); +if n==0 + m.mu = []; + m.Sigma = []; +else + m.mu = zeros(n, d); + m.Sigma = zeros(n, n, d); +end +m.T = 0*myones(pot.dsizes); +for i=1:pot.dsize + s = struct(pot.mom{i}); % violate privacy of object + if n > 0 + m.mu(:,i) = s.mu; + m.Sigma(:,:,i) = s.Sigma; + end + m.T(i) = exp(s.logp); +end +if isvectorBNT(m.T) + m.T = m.T(:)'; +end diff --git a/sourcecodes/bnt-master/BNT/potentials/@cgpot/set_domain_pot.m b/sourcecodes/bnt-master/BNT/potentials/@cgpot/set_domain_pot.m new file mode 100644 index 00000000..07364648 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/potentials/@cgpot/set_domain_pot.m @@ -0,0 +1,21 @@ +function pot = set_domain_pot(pot, domain) +% SET_DOMAIN_POT Change the domain of a potential (cgpot) +% pot = set_domain_pot(pot, domain) + +delta = domain(1) - pot.domain(1); +assert(all(domain == pot.domain + delta)); +pot.domain = pot.domain + delta; +pot.ddom = pot.ddom + delta; +pot.cdom = pot.cdom + delta; +cdomain = pot.cdom; +n = prod(pot.dsizes); +if(pot.subtype == 'm') + for i = 1: n + pot.mom{i} = set_domain_pot(pot.mom{i}, cdomain); + end +end +if(pot.subtype == 'c') + for i = 1: n + pot.can{i} = set_domain_pot(pot.can{i}, cdomain); + end +end |
