diff options
Diffstat (limited to 'sourcecodes/bnt-master/BNT/inference/dynamic/@jtree_dbn_inf_engine/Old')
8 files changed, 464 insertions, 0 deletions
diff --git a/sourcecodes/bnt-master/BNT/inference/dynamic/@jtree_dbn_inf_engine/Old/CVS/Entries b/sourcecodes/bnt-master/BNT/inference/dynamic/@jtree_dbn_inf_engine/Old/CVS/Entries new file mode 100644 index 00000000..0bc2d941 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/inference/dynamic/@jtree_dbn_inf_engine/Old/CVS/Entries @@ -0,0 +1,6 @@ +/enter_soft_evidence_nonint.m/1.1.1.1/Wed May 29 15:59:56 2002// +/enter_soft_evidence_trans.m/1.1.1.1/Wed May 29 15:59:56 2002// +/jtree_dbn_inf_engine.m/1.1.1.1/Wed May 29 15:59:56 2002// +/jtree_dbn_inf_engine1.m/1.1.1.1/Wed May 29 15:59:56 2002// +/jtree_dbn_inf_engine2.m/1.1.1.1/Wed May 29 15:59:56 2002// +D diff --git a/sourcecodes/bnt-master/BNT/inference/dynamic/@jtree_dbn_inf_engine/Old/CVS/Repository b/sourcecodes/bnt-master/BNT/inference/dynamic/@jtree_dbn_inf_engine/Old/CVS/Repository new file mode 100644 index 00000000..0b90d866 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/inference/dynamic/@jtree_dbn_inf_engine/Old/CVS/Repository @@ -0,0 +1 @@ +FullBNT/BNT/inference/dynamic/@jtree_dbn_inf_engine/Old diff --git a/sourcecodes/bnt-master/BNT/inference/dynamic/@jtree_dbn_inf_engine/Old/CVS/Root b/sourcecodes/bnt-master/BNT/inference/dynamic/@jtree_dbn_inf_engine/Old/CVS/Root new file mode 100644 index 00000000..f3bd14a6 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/inference/dynamic/@jtree_dbn_inf_engine/Old/CVS/Root @@ -0,0 +1 @@ +:ext:nsaunier@bnt.cvs.sourceforge.net:/cvsroot/bnt diff --git a/sourcecodes/bnt-master/BNT/inference/dynamic/@jtree_dbn_inf_engine/Old/enter_soft_evidence_nonint.m b/sourcecodes/bnt-master/BNT/inference/dynamic/@jtree_dbn_inf_engine/Old/enter_soft_evidence_nonint.m new file mode 100644 index 00000000..72641580 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/inference/dynamic/@jtree_dbn_inf_engine/Old/enter_soft_evidence_nonint.m @@ -0,0 +1,135 @@ +function [clpot, loglik] = enter_soft_evidence(engine, CPDpot, observed, pot_type) +% ENTER_SOFT_EVIDENCE Add the specified soft evidence to the network (jtree_dbn) +% [clpot, loglik] = enter_soft_evidence(engine, CPDpot, observed, pot_type, filter) + +[ss T] = size(CPDpot); +Q = length(engine.jtree_struct.cliques); +clpot = cell(Q,T); % clpot{t} contains evidence from slices (t-1, t) +seppot = cell(Q,Q,T); +ll = zeros(1,Q); +logscale = zeros(1,T); +bnet = bnet_from_engine(engine); + +% Forwards pass. +% Compute distribution on clq C, +% where C is the out interface to (t-1,t). +% Then pass this to clq D, where D is the in inferface to (t+1,t). +% Then propagate from D to later slices. + +C = engine.out_clq; +assert(C==engine.jtree_struct.root_clq); +D = engine.in_clq; +slice1 = 1:ss; +slice2 = slice1 + ss; +Nnonint = length(engine.nonint); +nonint = cell(Nnonint, 1); +for t=1:T + if t==1 + pots = [CPDpot(:,1); CPDpot(engine.interface, 2)]; + clqs = engine.jtree_struct.clq_ass_to_node([slice1 engine.interface+ss]); + obs = find(observed(:,1:2)); + elseif t==T + clqs = [D engine.jtree_struct.clq_ass_to_node(engine.nonint)]; + phiC = set_domain_pot(phiC, engine.interface); % shift back to slice 1 + for i=1:Nnonint + nonint{i} = CPDpot{engine.nonint(i), t}; + nonint{i} = set_domain_pot(nonint{i}, domain_pot(nonint{i})-ss); % shift back to slice 1 + end + pots = [ {phiC}; nonint]; + obs = find(observed(:,T)); + else + clqs = [D engine.jtree_struct.clq_ass_to_node([engine.nonint engine.interface+ss])]; + phiC = set_domain_pot(phiC, engine.interface); % shift back to slice 1 + for i=1:Nnonint + nonint{i} = CPDpot{engine.nonint(i), t}; + nonint{i} = set_domain_pot(nonint{i}, domain_pot(nonint{i})-ss); % shift back to slice 1 + end + pots = [ {phiC}; nonint; CPDpot(engine.interface, t+1)]; + obs = find(observed(:,t:t+1)); + end + [clpot(:,t), seppot(:,:,t)] = init_pot(engine.jtree_struct.cliques, clqs, pots, pot_type, ... + obs, bnet.node_sizes(:), bnet.cnodes); + [clpot(:,t), seppot(:,:,t)] = collect_evidence(clpot(:,t), seppot(:,:,t), engine.maximize, ... + engine.jtree_struct.postorder, ... + engine.jtree_struct.postorder_parents,... + engine.jtree_struct.separator); + + for c=1:Q + [clpot{c,t}, ll(c)] = normalize_pot(clpot{c,t}); + end + logscale(t) = ll(C); + + phiC = marginalize_pot(clpot{C,t}, engine.interface+ss, engine.maximize); +end + + + +% Backwards pass. +% Pass evidence from clq C to clq D, +% where C is the in interface to (t,t+1) and D is the out inferface to (t-1,t) +% Then propagate evidence from D to earlier slices. +C = engine.in_clq; +D = engine.out_clq; +for t=T:-1:1 + [clpot(:,t), seppot(:,:,t)] = distribute_evidence(clpot(:,t), seppot(:,:,t), engine.maximize, ... + engine.jtree_struct.preorder, ... + engine.jtree_struct.preorder_children, ... + engine.jtree_struct.separator); + for c=1:Q + [clpot{c,t}, ll(c)] = normalize_pot(clpot{c,t}); + end + %logscale(t) = ll(C); + + if t >= 2 + phiC = marginalize_pot(clpot{C,t}, engine.interface, engine.maximize); + phiC = set_domain_pot(phiC, engine.interface+ss); % shift forward to slice 2 + phiD = marginalize_pot(clpot{D,t-1}, engine.interface+ss, engine.maximize); + ratio = divide_by_pot(phiC, phiD); + clpot{D,t-1} = multiply_by_pot(clpot{D,t-1}, ratio); + end +end + +loglik = sum(logscale); + + +%%%%%%% +function [clpot, seppot] = init_pot(cliques, clqs, pots, pot_type, onodes, ns, cnodes); + +% Set the clique potentials to all 1s +C = length(cliques); +clpot = cell(1,C); +for i=1:C + clpot{i} = mk_initial_pot(pot_type, cliques{i}, ns, cnodes, onodes); +end + +% Multiply on specified potentials +for i=1:length(clqs) + c = clqs(i); + clpot{c} = multiply_by_pot(clpot{c}, pots{i}); +end + +seppot = cell(C,C); % implicitely initialized to 1 + + +%%%% +function [clpot, seppot] = collect_evidence(clpot, seppot, maximize, postorder, postorder_parents,... + separator) +for n=postorder %postorder(1:end-1) + for p=postorder_parents{n} + %clpot{p} = divide_by_pot(clpot{n}, seppot{p,n}); % dividing by 1 is redundant + seppot{p,n} = marginalize_pot(clpot{n}, separator{p,n}, maximize); + clpot{p} = multiply_by_pot(clpot{p}, seppot{p,n}); + end +end + + +%%%% +function [clpot, seppot] = distribute_evidence(clpot, seppot, maximize, preorder, preorder_children,... + separator) +for n=preorder + for c=preorder_children{n} + clpot{c} = divide_by_pot(clpot{c}, seppot{n,c}); + seppot{n,c} = marginalize_pot(clpot{n}, separator{n,c}, maximize); + clpot{c} = multiply_by_pot(clpot{c}, seppot{n,c}); + end +end diff --git a/sourcecodes/bnt-master/BNT/inference/dynamic/@jtree_dbn_inf_engine/Old/enter_soft_evidence_trans.m b/sourcecodes/bnt-master/BNT/inference/dynamic/@jtree_dbn_inf_engine/Old/enter_soft_evidence_trans.m new file mode 100644 index 00000000..b9c85b80 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/inference/dynamic/@jtree_dbn_inf_engine/Old/enter_soft_evidence_trans.m @@ -0,0 +1,135 @@ +function [clpot, loglik] = enter_soft_evidence(engine, CPDpot, observed, pot_type) +% ENTER_SOFT_EVIDENCE Add the specified soft evidence to the network (jtree_dbn) +% [clpot, loglik] = enter_soft_evidence(engine, CPDpot, observed, pot_type, filter) + +[ss T] = size(CPDpot); +Q = length(engine.jtree_struct.cliques); +clpot = cell(Q,T); % clpot{t} contains evidence from slices (t-1, t) +seppot = cell(Q,Q,T); +ll = zeros(1,Q); +logscale = zeros(1,T); +bnet = bnet_from_engine(engine); + +% Forwards pass. +% Compute distribution on clq C, +% where C is the out interface to (t-1,t). +% Then pass this to clq D, where D is the in inferface to (t+1,t). +% Then propagate from D to later slices. + +C = engine.out_clq; +assert(C==engine.jtree_struct.root_clq); +D = engine.in_clq; +slice1 = 1:ss; +slice2 = slice1 + ss; +Ntransient = length(engine.transient); +trans = cell(Ntransient,1); +for t=1:T + if t==1 + pots = [CPDpot(:,1); CPDpot(engine.persist, 2)]; + clqs = engine.jtree_struct.clq_ass_to_node([slice1 engine.persist+ss]); + obs = find(observed(:,1:2)); + elseif t==T + clqs = [D engine.jtree_struct.clq_ass_to_node(engine.transient)]; + phiC = set_domain_pot(phiC, engine.interface); % shift back to slice 1 + for i=1:Ntransient + trans{i} = CPDpot{engine.transient(i), t}; + trans{i} = set_domain_pot(trans{i}, domain_pot(trans{i})-ss); % shift back to slice 1 + end + pots = [ {phiC}; trans]; + obs = find(observed(:,T)); + else + clqs = [D engine.jtree_struct.clq_ass_to_node([engine.transient engine.persist+ss])]; + phiC = set_domain_pot(phiC, engine.interface); % shift back to slice 1 + for i=1:Ntransient + trans{i} = CPDpot{engine.transient(i), t}; + trans{i} = set_domain_pot(trans{i}, domain_pot(trans{i})-ss); % shift back to slice 1 + end + pots = [ {phiC}; trans; CPDpot(engine.persist, t+1)]; + obs = find(observed(:,t:t+1)); + end + [clpot(:,t), seppot(:,:,t)] = init_pot(engine.jtree_struct.cliques, clqs, pots, pot_type, ... + obs, bnet.node_sizes(:), bnet.cnodes); + [clpot(:,t), seppot(:,:,t)] = collect_evidence(clpot(:,t), seppot(:,:,t), engine.maximize, ... + engine.jtree_struct.postorder, ... + engine.jtree_struct.postorder_parents,... + engine.jtree_struct.separator); + + for c=1:Q + [clpot{c,t}, ll(c)] = normalize_pot(clpot{c,t}); + end + logscale(t) = ll(C); + + phiC = marginalize_pot(clpot{C,t}, engine.interface+ss, engine.maximize); +end + + + +% Backwards pass. +% Pass evidence from clq C to clq D, +% where C is the in interface to (t,t+1) and D is the out inferface to (t-1,t) +% Then propagate evidence from D to earlier slices. +C = engine.in_clq; +D = engine.out_clq; +for t=T:-1:1 + [clpot(:,t), seppot(:,:,t)] = distribute_evidence(clpot(:,t), seppot(:,:,t), engine.maximize, ... + engine.jtree_struct.preorder, ... + engine.jtree_struct.preorder_children, ... + engine.jtree_struct.separator); + for c=1:Q + [clpot{c,t}, ll(c)] = normalize_pot(clpot{c,t}); + end + %logscale(t) = ll(C); + + if t >= 2 + phiC = marginalize_pot(clpot{C,t}, engine.interface, engine.maximize); + phiC = set_domain_pot(phiC, engine.interface+ss); % shift forward to slice 2 + phiD = marginalize_pot(clpot{D,t-1}, engine.interface+ss, engine.maximize); + ratio = divide_by_pot(phiC, phiD); + clpot{D,t-1} = multiply_by_pot(clpot{D,t-1}, ratio); + end +end + +loglik = sum(logscale); + + +%%%%%%% +function [clpot, seppot] = init_pot(cliques, clqs, pots, pot_type, onodes, ns, cnodes); + +% Set the clique potentials to all 1s +C = length(cliques); +clpot = cell(1,C); +for i=1:C + clpot{i} = mk_initial_pot(pot_type, cliques{i}, ns, cnodes, onodes); +end + +% Multiply on specified potentials +for i=1:length(clqs) + c = clqs(i); + clpot{c} = multiply_by_pot(clpot{c}, pots{i}); +end + +seppot = cell(C,C); % implicitely initialized to 1 + + +%%%% +function [clpot, seppot] = collect_evidence(clpot, seppot, maximize, postorder, postorder_parents,... + separator) +for n=postorder %postorder(1:end-1) + for p=postorder_parents{n} + %clpot{p} = divide_by_pot(clpot{n}, seppot{p,n}); % dividing by 1 is redundant + seppot{p,n} = marginalize_pot(clpot{n}, separator{p,n}, maximize); + clpot{p} = multiply_by_pot(clpot{p}, seppot{p,n}); + end +end + + +%%%% +function [clpot, seppot] = distribute_evidence(clpot, seppot, maximize, preorder, preorder_children,... + separator) +for n=preorder + for c=preorder_children{n} + clpot{c} = divide_by_pot(clpot{c}, seppot{n,c}); + seppot{n,c} = marginalize_pot(clpot{n}, separator{n,c}, maximize); + clpot{c} = multiply_by_pot(clpot{c}, seppot{n,c}); + end +end diff --git a/sourcecodes/bnt-master/BNT/inference/dynamic/@jtree_dbn_inf_engine/Old/jtree_dbn_inf_engine.m b/sourcecodes/bnt-master/BNT/inference/dynamic/@jtree_dbn_inf_engine/Old/jtree_dbn_inf_engine.m new file mode 100644 index 00000000..5b5cc8ba --- /dev/null +++ b/sourcecodes/bnt-master/BNT/inference/dynamic/@jtree_dbn_inf_engine/Old/jtree_dbn_inf_engine.m @@ -0,0 +1,67 @@ +function engine = jtree_dbn_inf_engine(bnet, varargin) +% JTREE_DBN_INF_ENGINE Junction tree inference algorithm for DBNs. + +ss = length(bnet.intra); + +onodes = []; + +if nargin >= 2 + args = varargin; + nargs = length(args); + for i=1:2:nargs + switch args{i}, + case 'observed', onodes = args{i+1}; + end + end +end + +[int, engine.persist, engine.transient] = compute_interface_nodes(bnet.intra, bnet.inter); +%engine.interface = engine.persist; % WRONG! +engine.interface = int; +engine.nonint = mysetdiff(1:ss, int); + +if 0 + % Create a 2 slice jtree + % We force there to be cliques containing the in and out interfaces for slices t and t+1. + obs_nodes = [onodes(:) onodes(:)+ss]; + engine.jtree_engine = jtree_inf_engine(bnet, 'observed', obs_nodes(:), ... + 'clusters', {int, int+ss}, 'root', int+ss); +else + % Create a "1.5 slice" jtree, containing slice 1 and the interface nodes of slice 2 + nodes15 = [1:ss int+ss]; + N = length(nodes15); + dag15 = bnet.dag(nodes15, nodes15); + ns15 = bnet.node_sizes(nodes15); + eclass15 = bnet.equiv_class(nodes15); + discrete_bitv = zeros(1,2*ss); + discrete_bitv(bnet.dnodes) = 1; + discrete15 = find(discrete_bitv(nodes15)); + bnet15 = mk_bnet(dag15, ns15, 'equiv_class', eclass15, 'discrete', discrete15); + bnet15.CPD = bnet.CPD; % CPDs for non-interface nodes in slice 2 will not be used + obs_bitv = zeros(1, 2*ss); + obs_bitv([onodes onodes+ss]) = 1; + obs_nodes15 = find(obs_bitv(nodes15)); + int_bitv = zeros(1,ss); + int_bitv(int) = 1; + engine.jtree_engine = jtree_inf_engine(bnet15, 'observed', obs_nodes15(:), ... + 'clusters', {int, int+ss}, 'root', int+ss); +end + +engine.in_clq = clq_containing_nodes(engine.jtree_engine, int); +engine.out_clq = clq_containing_nodes(engine.jtree_engine, int+ss); + +engine.clq_ass_to_node = zeros(ss, 2); +for i=1:ss + engine.clq_ass_to_node(i, 1) = clq_containing_nodes(engine.jtree_engine, i); + engine.clq_ass_to_node(i, 2) = clq_containing_nodes(engine.jtree_engine, i+ss); +end + +engine.jtree_struct = struct(engine.jtree_engine); % violate object privacy + +% stuff needed by marginal_nodes +engine.clpot = []; +engine.maximize = []; +engine.T = []; + +engine = class(engine, 'jtree_dbn_inf_engine', inf_engine(bnet)); + diff --git a/sourcecodes/bnt-master/BNT/inference/dynamic/@jtree_dbn_inf_engine/Old/jtree_dbn_inf_engine1.m b/sourcecodes/bnt-master/BNT/inference/dynamic/@jtree_dbn_inf_engine/Old/jtree_dbn_inf_engine1.m new file mode 100644 index 00000000..5edad836 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/inference/dynamic/@jtree_dbn_inf_engine/Old/jtree_dbn_inf_engine1.m @@ -0,0 +1,62 @@ +function engine = jtree_dbn_inf_engine(bnet, varargin) +% JTREE_DBN_INF_ENGINE Junction tree inference algorithm for DBNs. + +ss = length(bnet.intra); + +onodes = []; + +if nargin >= 2 + args = varargin; + nargs = length(args); + for i=1:2:nargs + switch args{i}, + case 'observed', onodes = args{i+1}; + end + end +end + +[int, engine.persist, engine.transient] = compute_interface_nodes(bnet.intra, bnet.inter); +%engine.interface = engine.persist; % WRONG! +engine.interface = int; +engine.nonint = mysetdiff(1:ss, int); + +if 1 + % Create a 2 slice jtree + % We force there to be cliques containing the in and out interfaces for slices t and t+1. + obs_nodes = [onodes(:) onodes(:)+ss]; + engine.jtree_engine = jtree_inf_engine(bnet, 'observed', obs_nodes(:), ... + 'clusters', {int, int+ss}, 'root', int+ss); +else + % Create a "1.5 slice" jtree, containing slice 1 and the interface nodes of slice 2 + % To keep the node numbering the same, we simply disconnect the non-interface nodes + % from slice 2. + intra15 = bnet.intra; + for i=engine.nonint(:)' + intra15(i,:) = 0; + intra15(:,i) = 0; + end + bnet15 = mk_dbn(intra15, bnet.inter, bnet.node_sizes_slice, bnet.dnodes_slice, ... + bnet.equiv_class(:,1), bnet.equiv_class(:,2), bnet.intra); + obs_nodes = [onodes(:) onodes(:)+ss]; + engine.jtree_engine = jtree_inf_engine(bnet15, 'observed', obs_nodes(:), ... + 'clusters', {int, int+ss}, 'root', int+ss); +end + +engine.in_clq = clq_containing_nodes(engine.jtree_engine, int); +engine.out_clq = clq_containing_nodes(engine.jtree_engine, int+ss); + +engine.clq_ass_to_node = zeros(ss, 2); +for i=1:ss + engine.clq_ass_to_node(i, 1) = clq_containing_nodes(engine.jtree_engine, i); + engine.clq_ass_to_node(i, 2) = clq_containing_nodes(engine.jtree_engine, i+ss); +end + +engine.jtree_struct = struct(engine.jtree_engine); % violate object privacy + +% stuff needed by marginal_nodes +engine.clpot = []; +engine.maximize = []; +engine.T = []; + +engine = class(engine, 'jtree_dbn_inf_engine', inf_engine(bnet)); + diff --git a/sourcecodes/bnt-master/BNT/inference/dynamic/@jtree_dbn_inf_engine/Old/jtree_dbn_inf_engine2.m b/sourcecodes/bnt-master/BNT/inference/dynamic/@jtree_dbn_inf_engine/Old/jtree_dbn_inf_engine2.m new file mode 100644 index 00000000..68ac2aff --- /dev/null +++ b/sourcecodes/bnt-master/BNT/inference/dynamic/@jtree_dbn_inf_engine/Old/jtree_dbn_inf_engine2.m @@ -0,0 +1,57 @@ +function engine = jtree_dbn_inf_engine(bnet, varargin) +% JTREE_DBN_INF_ENGINE Junction tree inference algorithm for DBNs. + +ss = length(bnet.intra); + +onodes = []; + +if nargin >= 2 + args = varargin; + nargs = length(args); + for i=1:2:nargs + switch args{i}, + case 'observed', onodes = args{i+1}; + end + end +end + +[int, engine.persist, engine.transient] = compute_interface_nodes(bnet.intra, bnet.inter); +%engine.interface = engine.persist; % WRONG! +engine.interface = int; +engine.nonint = mysetdiff(1:ss, int); + + +% Create a 2 slice jtree +% We force there to be cliques containing the in and out interfaces for slices t and t+1. +obs_nodes = [onodes(:) onodes(:)+ss]; +engine.jtree_engine = jtree_inf_engine(bnet, 'observed', obs_nodes(:), ... + 'clusters', {int, int+ss}, 'root', int+ss); + +engine.in_clq = clq_containing_nodes(engine.jtree_engine, int); +engine.out_clq = clq_containing_nodes(engine.jtree_engine, int+ss); +engine.jtree_struct = struct(engine.jtree_engine); % violate object privacy + + + +% Also create an engine just for slice 1 +bnet1 = mk_bnet(bnet.intra1, bnet.node_sizes_slice, bnet.dnodes, bnet.equiv_class(:,1)); +for i=1:max(bnet1.equiv_class) + bnet1.CPD{i} = bnet.CPD{i}; +end + +engine.jtree_engine1 = jtree_inf_engine(bnet1, 'observed', onodes, 'clusters', {int}, ... + 'root', int); + +engine.in_clq1 = clq_containing_nodes(engine.jtree_engine1, int); +engine.jtree_struct1 = struct(engine.jtree_engine1); % violate object privacy + + + + +% stuff needed by marginal_nodes +engine.clpot = []; +engine.T = []; +engine.maximize = []; + +engine = class(engine, 'jtree_dbn_inf_engine', inf_engine(bnet)); + |
