diff options
Diffstat (limited to 'sourcecodes/bnt-master/BNT/examples')
296 files changed, 18527 insertions, 0 deletions
diff --git a/sourcecodes/bnt-master/BNT/examples/CVS/Entries b/sourcecodes/bnt-master/BNT/examples/CVS/Entries new file mode 100644 index 00000000..5d1ca70d --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/CVS/Entries @@ -0,0 +1,4 @@ +/dummy/1.1.1.1/Sat Jan 18 22:22:06 2003// +D/dynamic//// +D/limids//// +D/static//// diff --git a/sourcecodes/bnt-master/BNT/examples/CVS/Repository b/sourcecodes/bnt-master/BNT/examples/CVS/Repository new file mode 100644 index 00000000..9b5676b5 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/CVS/Repository @@ -0,0 +1 @@ +FullBNT/BNT/examples diff --git a/sourcecodes/bnt-master/BNT/examples/CVS/Root b/sourcecodes/bnt-master/BNT/examples/CVS/Root new file mode 100644 index 00000000..f3bd14a6 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/CVS/Root @@ -0,0 +1 @@ +:ext:nsaunier@bnt.cvs.sourceforge.net:/cvsroot/bnt diff --git a/sourcecodes/bnt-master/BNT/examples/dummy b/sourcecodes/bnt-master/BNT/examples/dummy new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dummy diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/CVS/Entries b/sourcecodes/bnt-master/BNT/examples/dynamic/CVS/Entries new file mode 100644 index 00000000..6f98ef27 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/CVS/Entries @@ -0,0 +1,38 @@ +/arhmm1.m/1.1.1.1/Thu Nov 14 01:03:34 2002// +/bat1.m/1.1.1.1/Wed May 29 15:59:54 2002// +/bkff1.m/1.1.1.1/Wed May 29 15:59:54 2002// +/chmm1.m/1.1.1.1/Wed May 29 15:59:54 2002// +/cmp_inference_dbn.m/1.1.1.1/Wed May 29 15:59:54 2002// +/cmp_learning_dbn.m/1.1.1.1/Wed May 29 15:59:54 2002// +/cmp_online_inference.m/1.1.1.1/Wed May 29 15:59:54 2002// +/dhmm1.m/1.1.1.1/Sun May 4 22:23:18 2003// +/ehmm1.m/1.1.1.1/Sat Jan 18 22:16:24 2003// +/fhmm_infer.m/1.1.1.1/Wed May 29 15:59:54 2002// +/filter_test1.m/1.1.1.1/Wed May 29 15:59:54 2002// +/ghmm1.m/1.1.1.1/Sun May 4 22:23:32 2003// +/ho1.m/1.1.1.1/Fri Mar 28 17:22:36 2003// +/jtree_clq_test.m/1.1.1.1/Sat Jan 18 22:16:38 2003// +/jtree_clq_test2.m/1.1.1.1/Thu Oct 10 23:45:12 2002// +/kalman1.m/1.1.1.1/Wed May 29 15:59:54 2002// +/kjaerulff1.m/1.1.1.1/Wed May 29 15:59:54 2002// +/loopy_dbn1.m/1.1.1.1/Wed May 29 15:59:54 2002// +/mhmm1.m/1.1.1.1/Sun May 4 22:23:40 2003// +/mildew1.m/1.1.1.1/Thu Jun 20 20:30:24 2002// +/mk_bat_dbn.m/1.1.1.1/Mon Jun 7 19:07:18 2004// +/mk_chmm.m/1.1.1.1/Tue May 11 19:23:14 2004// +/mk_collage_from_clqs.m/1.1.1.1/Wed May 29 15:59:54 2002// +/mk_fhmm.m/1.1.1.1/Wed May 29 15:59:54 2002// +/mk_mildew_dbn.m/1.1.1.1/Thu Oct 10 23:14:36 2002// +/mk_orig_bat_dbn.m/1.1.1.1/Wed Feb 4 23:53:06 2004// +/mk_orig_water_dbn.m/1.1.1.1/Sat Jan 31 02:57:52 2004// +/mk_ps_from_clqs.m/1.1.1.1/Wed Oct 9 20:36:56 2002// +/mk_uffe_dbn.m/1.1.1.1/Thu Oct 10 23:14:54 2002// +/mk_water_dbn.m/1.1.1.1/Tue May 11 18:45:38 2004// +/orig_water1.m/1.1.1.1/Mon Nov 22 22:41:42 2004// +/reveal1.m/1.1.1.1/Wed May 29 15:59:54 2002// +/scg_dbn.m/1.1.1.1/Wed May 29 15:59:54 2002// +/skf_data_assoc_gmux.m/1.1.1.1/Wed May 29 15:59:54 2002// +/viterbi1.m/1.1.1.1/Tue May 13 14:35:40 2003// +/water1.m/1.1.1.1/Thu Nov 14 20:07:56 2002// +/water2.m/1.1.1.1/Thu Nov 14 20:33:42 2002// +D diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/CVS/Entries.Log b/sourcecodes/bnt-master/BNT/examples/dynamic/CVS/Entries.Log new file mode 100644 index 00000000..0634c982 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/CVS/Entries.Log @@ -0,0 +1,3 @@ +A D/HHMM//// +A D/Old//// +A D/SLAM//// diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/CVS/Repository b/sourcecodes/bnt-master/BNT/examples/dynamic/CVS/Repository new file mode 100644 index 00000000..587019a2 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/CVS/Repository @@ -0,0 +1 @@ +FullBNT/BNT/examples/dynamic diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/CVS/Root b/sourcecodes/bnt-master/BNT/examples/dynamic/CVS/Root new file mode 100644 index 00000000..f3bd14a6 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/CVS/Root @@ -0,0 +1 @@ +:ext:nsaunier@bnt.cvs.sourceforge.net:/cvsroot/bnt diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/CVS/Entries b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/CVS/Entries new file mode 100644 index 00000000..6c5b3923 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/CVS/Entries @@ -0,0 +1,9 @@ +/abcd_hhmm.m/1.1.1.1/Sat Sep 21 21:37:54 2002// +/add_hhmm_end_state.m/1.1.1.1/Wed May 29 15:59:54 2002// +/hhmm_jtree_clqs.m/1.1.1.1/Wed May 29 15:59:54 2002// +/mk_hhmm.m/1.1.1.1/Sat Sep 21 20:58:06 2002// +/mk_hhmm_topo.m/1.1.1.1/Wed May 29 15:59:54 2002// +/mk_hhmm_topo_F1.m/1.1.1.1/Wed May 29 15:59:54 2002// +/pretty_print_hhmm_parse.m/1.1.1.1/Wed May 29 15:59:54 2002// +/remove_hhmm_end_state.m/1.1.1.1/Mon Dec 16 19:16:50 2002// +D diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/CVS/Entries.Log b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/CVS/Entries.Log new file mode 100644 index 00000000..1b0fe64f --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/CVS/Entries.Log @@ -0,0 +1,5 @@ +A D/Map//// +A D/Mgram//// +A D/Motif//// +A D/Old//// +A D/Square//// diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/CVS/Repository b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/CVS/Repository new file mode 100644 index 00000000..2ba8b9e6 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/CVS/Repository @@ -0,0 +1 @@ +FullBNT/BNT/examples/dynamic/HHMM diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/CVS/Root b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/CVS/Root new file mode 100644 index 00000000..f3bd14a6 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/CVS/Root @@ -0,0 +1 @@ +:ext:nsaunier@bnt.cvs.sourceforge.net:/cvsroot/bnt diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/CVS/Entries b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/CVS/Entries new file mode 100644 index 00000000..dc32f52b --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/CVS/Entries @@ -0,0 +1,6 @@ +/disp_map_hhmm.m/1.1.1.1/Tue Sep 24 22:45:56 2002// +/learn_map.m/1.1.1.1/Sat Jan 11 18:48:46 2003// +/mk_map_hhmm.m/1.1.1.1/Tue Sep 24 10:49:52 2002// +/mk_rnd_map_hhmm.m/1.1.1.1/Tue Sep 24 22:13:48 2002// +/sample_from_map.m/1.1.1.1/Tue Sep 24 13:02:30 2002// +D/Old//// diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/CVS/Repository b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/CVS/Repository new file mode 100644 index 00000000..66b47bbc --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/CVS/Repository @@ -0,0 +1 @@ +FullBNT/BNT/examples/dynamic/HHMM/Map diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/CVS/Root b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/CVS/Root new file mode 100644 index 00000000..f3bd14a6 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/CVS/Root @@ -0,0 +1 @@ +:ext:nsaunier@bnt.cvs.sourceforge.net:/cvsroot/bnt diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/Old/CVS/Entries b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/Old/CVS/Entries new file mode 100644 index 00000000..6079d451 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/Old/CVS/Entries @@ -0,0 +1,2 @@ +/mk_map_hhmm.m/1.1.1.1/Tue Sep 24 07:02:44 2002// +D diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/Old/CVS/Repository b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/Old/CVS/Repository new file mode 100644 index 00000000..354057a9 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/Old/CVS/Repository @@ -0,0 +1 @@ +FullBNT/BNT/examples/dynamic/HHMM/Map/Old diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/Old/CVS/Root b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/Old/CVS/Root new file mode 100644 index 00000000..f3bd14a6 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/Old/CVS/Root @@ -0,0 +1 @@ +:ext:nsaunier@bnt.cvs.sourceforge.net:/cvsroot/bnt diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/Old/mk_map_hhmm.m b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/Old/mk_map_hhmm.m new file mode 100644 index 00000000..7b646745 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/Old/mk_map_hhmm.m @@ -0,0 +1,156 @@ +function bnet = mk_map_hhmm(varargin) + +% p is the prob of a successful move (defines the reliability of motors) +p = 1; +num_obs_nodes = 1; + +for i=1:2:length(varargin) + switch varargin{i}, + case 'p', p = varargin{i+1}; + case 'numobs', num_obs_node = varargin{i+1}; + end +end + + +q = 1-p; + +% assign numbers to the nodes in topological order +U = 1; A = 2; C = 3; F = 4; O = 5; + +% create graph structure + +ss = 5; % slice size +intra = zeros(ss,ss); +intra(U,F)=1; +intra(A,[C F O])=1; +intra(C,[F O])=1; + +inter = zeros(ss,ss); +inter(U,[A C])=1; +inter(A,[A C])=1; +inter(F,[A C])=1; +inter(C,C)=1; + +% node sizes +ns = zeros(1,ss); +ns(U) = 2; % left/right +ns(A) = 2; +ns(C) = 3; +ns(F) = 2; +ns(O) = 5; % we will assign each state a unique symbol +l = 1; r = 2; % left/right +L = 1; R = 2; + +% Make the DBN +bnet = mk_dbn(intra, inter, ns, 'observed', O); +eclass = bnet.equiv_class; + + + +% Define CPDs for slice 1 +% We clamp all of them, i.e., do not try to learn them. + +% uniform probs over actions (the input could be chosen from a policy) +bnet.CPD{eclass(U,1)} = tabular_CPD(bnet, U, 'CPT', mk_stochastic(ones(ns(U),1)), ... + 'adjustable', 0); + +% uniform probs over starting abstract state +bnet.CPD{eclass(A,1)} = tabular_CPD(bnet, A, 'CPT', mk_stochastic(ones(ns(A),1)), ... + 'adjustable', 0); + +% Uniform probs over starting concrete state, modulo the fact +% that corridor 2 is only of length 2. +CPT = zeros(ns(A), ns(C)); % CPT(i,j) = P(C starts in j | A=i) +CPT(1, :) = [1/3 1/3 1/3]; +CPT(2, :) = [1/2 1/2 0]; +bnet.CPD{eclass(C,1)} = tabular_CPD(bnet, C, 'CPT', CPT, 'adjustable', 0); + +% Termination probs +CPT = zeros(ns(U), ns(A), ns(C), ns(F)); +CPT(r,1,1,:) = [1 0]; +CPT(r,1,2,:) = [1 0]; +CPT(r,1,3,:) = [q p]; +CPT(r,2,1,:) = [1 0]; +CPT(r,2,2,:) = [q p]; +CPT(l,1,1,:) = [q p]; +CPT(l,1,2,:) = [1 0]; +CPT(l,1,3,:) = [1 0]; +CPT(l,2,1,:) = [q p]; +CPT(l,2,2,:) = [1 0]; + +bnet.CPD{eclass(F,1)} = tabular_CPD(bnet, F, 'CPT', CPT); + + +% Assign each state a unique observation +CPT = zeros(ns(A), ns(C), ns(O)); +CPT(1,1,1)=1; +CPT(1,2,2)=1; +CPT(1,3,3)=1; +CPT(2,1,4)=1; +CPT(2,2,5)=1; +%CPT(2,3,:) undefined + +bnet.CPD{eclass(O,1)} = tabular_CPD(bnet, O, 'CPT', CPT); + + +% Define the CPDs for slice 2 + +% Abstract + +% Since the top level never resets, the starting distribution is irrelevant: +% A2 will be determined by sampling from transmat(A1,:). +% But the code requires we specify it anyway; we make it all 0s, a dummy value. +startprob = zeros(ns(U), ns(A)); + +transmat = zeros(ns(U), ns(A), ns(A)); +transmat(R,1,:) = [q p]; +transmat(R,2,:) = [0 1]; +transmat(L,1,:) = [1 0]; +transmat(L,2,:) = [p q]; + +% Qps are the parents we condition the parameters on, in this case just +% the past action. +bnet.CPD{eclass(A,2)} = hhmm2Q_CPD(bnet, A+ss, 'Fbelow', F, ... + 'startprob', startprob, 'transprob', transmat); + + + +% Concrete + +transmat = zeros(ns(C), ns(U), ns(A), ns(C)); +transmat(1,r,1,:) = [q p 0.0]; +transmat(2,r,1,:) = [0.0 q p]; +transmat(3,r,1,:) = [0.0 0.0 1.0]; +transmat(1,r,2,:) = [q p 0.0]; +transmat(2,r,2,:) = [0.0 1.0 0.0]; +% +transmat(1,l,1,:) = [1.0 0.0 0.0]; +transmat(2,l,1,:) = [p q 0.0]; +transmat(3,l,1,:) = [0.0 p q]; +transmat(1,l,2,:) = [1.0 0.0 0.0]; +transmat(2,l,2,:) = [p q 0.0]; + +% Add a new dimension for A(t-1), by copying old vals, +% so the matrix is the same size as startprob + + +transmat = reshape(transmat, [ns(C) ns(U) ns(A) 1 ns(C)]); +transmat = repmat(transmat, [1 1 1 ns(A) 1]); + +% startprob(C(t-1), U(t-1), A(t-1), A(t), C(t)) +startprob = zeros(ns(C), ns(U), ns(A), ns(A), ns(C)); +startprob(1,L,1,1,:) = [1.0 0.0 0.0]; +startprob(3,R,1,2,:) = [1.0 0.0 0.0]; +startprob(3,R,1,1,:) = [0.0 0.0 1.0]; +% +startprob(1,L,2,1,:) = [0.0 0.0 010]; +startprob(2,L,2,1,:) = [1.0 0.0 0.0]; +startprob(2,R,2,2,:) = [0.0 1.0 0.0]; + +% want transmat(U,A,C,At,Ct), ie. in topo order +transmat = permute(transmat, [2 3 1 4 5]); +startprob = permute(startprob, [2 3 1 4 5]); +bnet.CPD{eclass(C,2)} = hhmm2Q_CPD(bnet, C+ss, 'Fself', F, ... + 'startprob', startprob, 'transprob', transmat); + + diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/disp_map_hhmm.m b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/disp_map_hhmm.m new file mode 100644 index 00000000..0aadf2bb --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/disp_map_hhmm.m @@ -0,0 +1,13 @@ +function disp_map_hhmm(bnet) + +eclass = bnet.equiv_class; +U = 1; A = 2; C = 3; F = 4; + +S = struct(bnet.CPD{eclass(A,2)}); +disp('abstract trans') +dispcpt(S.transprob) + +S = struct(bnet.CPD{eclass(C,2)}); +disp('concrete trans for go left') % UAC AC +dispcpt(squeeze(S.transprob(1,:,:,:,:))) + diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/learn_map.m b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/learn_map.m new file mode 100644 index 00000000..ac36586a --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/learn_map.m @@ -0,0 +1,40 @@ +seed = 1; +rand('state', seed); +randn('state', seed); + +obs_model = 'unique'; % each cell has a unique label (essentially fully observable) +%obs_model = 'four'; % each cell generates 4 observations, NESW + +% Generate the true network, and a randomization of it +realnet = mk_map_hhmm('p', 0.9, 'obs_model', obs_model); +rndnet = mk_rnd_map_hhmm('obs_model', obs_model); +eclass = realnet.equiv_class; +U = 1; A = 2; C = 3; F = 4; onodes = 5; + +ss = realnet.nnodes_per_slice; +T = 100; +evidence = sample_dbn(realnet, 'length', T); +ev = cell(ss,T); +ev(onodes,:) = evidence(onodes,:); + +infeng = jtree_dbn_inf_engine(rndnet); + +if 0 +% suppose we do not observe the final finish node, but only know +% it is more likely to be on that off +ev2 = ev; +infeng = enter_evidence(infeng, ev2, 'soft_evidence_nodes', [F T], 'soft_evidence', {[0.3 0.7]'}); +end + + +learnednet = learn_params_dbn_em(infeng, {evidence}, 'max_iter', 5); + +disp('real model') +disp_map_hhmm(realnet) + +disp('learned model') +disp_map_hhmm(learnednet) + +disp('rnd model') +disp_map_hhmm(rndnet) + diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/mk_map_hhmm.m b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/mk_map_hhmm.m new file mode 100644 index 00000000..7b077ddb --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/mk_map_hhmm.m @@ -0,0 +1,181 @@ +function bnet = mk_map_hhmm(varargin) + +% p is the prob of a successful move (defines the reliability of motors) +p = 1; +obs_model = 'unique'; + +for i=1:2:length(varargin) + switch varargin{i}, + case 'p', p = varargin{i+1}; + case 'obs_model', obs_model = varargin{i+1}; + end +end + + +q = 1-p; +unique_obs = strcmp(obs_model, 'unique'); + +% assign numbers to the nodes in topological order +U = 1; A = 2; C = 3; F = 4; +if unique_obs + onodes = 5; +else + N = 5; E = 6; S = 7; W = 8; % north, east, south, west + onodes = [N E S W]; +end + +% create graph structure + +ss = 4 + length(onodes); % slice size +intra = zeros(ss,ss); +intra(U,F)=1; +intra(A,[C F onodes])=1; +intra(C,[F onodes])=1; + +inter = zeros(ss,ss); +inter(U,[A C])=1; +inter(A,[A C])=1; +inter(F,[A C])=1; +inter(C,C)=1; + +% node sizes +ns = zeros(1,ss); +ns(U) = 2; % left/right +ns(A) = 2; +ns(C) = 3; +ns(F) = 2; +if unique_obs + ns(onodes) = 5; % we will assign each state a unique symbol +else + ns(onodes) = 2; +end +l = 1; r = 2; % left/right +L = 1; R = 2; + +% Make the DBN +bnet = mk_dbn(intra, inter, ns, 'observed', onodes); +eclass = bnet.equiv_class; + + + +% Define CPDs for slice 1 +% We clamp all the CPDs that are not tied, +% since we cannot learn them from a single sequence. + +% uniform probs over actions (the input could be chosen from a policy) +bnet.CPD{eclass(U,1)} = tabular_CPD(bnet, U, 'CPT', mk_stochastic(ones(ns(U),1)), ... + 'adjustable', 0); + +% uniform probs over starting abstract state +bnet.CPD{eclass(A,1)} = tabular_CPD(bnet, A, 'CPT', mk_stochastic(ones(ns(A),1)), ... + 'adjustable', 0); + +% Uniform probs over starting concrete state, modulo the fact +% that corridor 2 is only of length 2. +CPT = zeros(ns(A), ns(C)); % CPT(i,j) = P(C starts in j | A=i) +CPT(1, :) = [1/3 1/3 1/3]; +CPT(2, :) = [1/2 1/2 0]; +bnet.CPD{eclass(C,1)} = tabular_CPD(bnet, C, 'CPT', CPT, 'adjustable', 0); + +% Termination probs +CPT = zeros(ns(U), ns(A), ns(C), ns(F)); +CPT(r,1,1,:) = [1 0]; +CPT(r,1,2,:) = [1 0]; +CPT(r,1,3,:) = [q p]; +CPT(r,2,1,:) = [1 0]; +CPT(r,2,2,:) = [q p]; +CPT(l,1,1,:) = [q p]; +CPT(l,1,2,:) = [1 0]; +CPT(l,1,3,:) = [1 0]; +CPT(l,2,1,:) = [q p]; +CPT(l,2,2,:) = [1 0]; + +bnet.CPD{eclass(F,1)} = tabular_CPD(bnet, F, 'CPT', CPT); + + +% Observation model +if unique_obs + CPT = zeros(ns(A), ns(C), 5); + CPT(1,1,1)=1; % Theo state 4 + CPT(1,2,2)=1; % Theo state 5 + CPT(1,3,3)=1; % Theo state 6 + CPT(2,1,4)=1; % Theo state 9 + CPT(2,2,5)=1; % Theo state 10 + %CPT(2,3,:) undefined + O = onodes(1); + bnet.CPD{eclass(O,1)} = tabular_CPD(bnet, O, 'CPT', CPT); +else + % north/east/south/west can see wall (1) or opening (2) + CPT = zeros(ns(A), ns(C), 2); + CPT(:,:,1) = q; + CPT(:,:,2) = p; + bnet.CPD{eclass(W,1)} = tabular_CPD(bnet, W, 'CPT', CPT); + bnet.CPD{eclass(E,1)} = tabular_CPD(bnet, E, 'CPT', CPT); + CPT = zeros(ns(A), ns(C), 2); + CPT(:,:,1) = p; + CPT(:,:,2) = q; + bnet.CPD{eclass(S,1)} = tabular_CPD(bnet, S, 'CPT', CPT); + bnet.CPD{eclass(N,1)} = tabular_CPD(bnet, N, 'CPT', CPT); +end + +% Define the CPDs for slice 2 + +% Abstract + +% Since the top level never resets, the starting distribution is irrelevant: +% A2 will be determined by sampling from transmat(A1,:). +% But the code requires we specify it anyway; we make it all 0s, a dummy value. +startprob = zeros(ns(U), ns(A)); + +transmat = zeros(ns(U), ns(A), ns(A)); +transmat(R,1,:) = [q p]; +transmat(R,2,:) = [0 1]; +transmat(L,1,:) = [1 0]; +transmat(L,2,:) = [p q]; + +% Qps are the parents we condition the parameters on, in this case just +% the past action. +bnet.CPD{eclass(A,2)} = hhmm2Q_CPD(bnet, A+ss, 'Fbelow', F, ... + 'startprob', startprob, 'transprob', transmat); + + + +% Concrete + +transmat = zeros(ns(C), ns(U), ns(A), ns(C)); +transmat(1,r,1,:) = [q p 0.0]; +transmat(2,r,1,:) = [0.0 q p]; +transmat(3,r,1,:) = [0.0 0.0 1.0]; +transmat(1,r,2,:) = [q p 0.0]; +transmat(2,r,2,:) = [0.0 1.0 0.0]; +% +transmat(1,l,1,:) = [1.0 0.0 0.0]; +transmat(2,l,1,:) = [p q 0.0]; +transmat(3,l,1,:) = [0.0 p q]; +transmat(1,l,2,:) = [1.0 0.0 0.0]; +transmat(2,l,2,:) = [p q 0.0]; + +% Add a new dimension for A(t-1), by copying old vals, +% so the matrix is the same size as startprob + + +transmat = reshape(transmat, [ns(C) ns(U) ns(A) 1 ns(C)]); +transmat = repmat(transmat, [1 1 1 ns(A) 1]); + +% startprob(C(t-1), U(t-1), A(t-1), A(t), C(t)) +startprob = zeros(ns(C), ns(U), ns(A), ns(A), ns(C)); +startprob(1,L,1,1,:) = [1.0 0.0 0.0]; +startprob(3,R,1,2,:) = [1.0 0.0 0.0]; +startprob(3,R,1,1,:) = [0.0 0.0 1.0]; +% +startprob(1,L,2,1,:) = [0.0 0.0 010]; +startprob(2,L,2,1,:) = [1.0 0.0 0.0]; +startprob(2,R,2,2,:) = [0.0 1.0 0.0]; + +% want transmat(U,A,C,At,Ct), ie. in topo order +transmat = permute(transmat, [2 3 1 4 5]); +startprob = permute(startprob, [2 3 1 4 5]); +bnet.CPD{eclass(C,2)} = hhmm2Q_CPD(bnet, C+ss, 'Fself', F, ... + 'startprob', startprob, 'transprob', transmat); + + diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/mk_rnd_map_hhmm.m b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/mk_rnd_map_hhmm.m new file mode 100644 index 00000000..76b06fc7 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/mk_rnd_map_hhmm.m @@ -0,0 +1,73 @@ +function bnet = mk_rnd_map_hhmm(varargin) + +% We copy the deterministic structure of the real HHMM, +% but randomize the probabilities of the adjustable CPDs. +% The key trick is that 0s in the real HHMM remain 0 +% even when multiplied by a randon number. + +obs_model = 'unique'; + +for i=1:2:length(varargin) + switch varargin{i}, + case 'obs_model', obs_model = varargin{i+1}; + end +end + + +unique_obs = strcmp(obs_model, 'unique'); + +psuccess = 0.9; +% must be less than 1, so that pfail > 0 +% otherwise we copy too many 0s +bnet = mk_map_hhmm('p', psuccess, 'obs_model', obs_model); +ns = bnet.node_sizes; +ss = bnet.nnodes_per_slice; + +U = 1; A = 2; C = 3; F = 4; +%unique_obs = (bnet.nnodes_per_slice == 5); +if unique_obs + onodes = 5; +else + north = 5; east = 6; south = 7; west = 8; + onodes = [north east south west]; +end + +eclass = bnet.equiv_class; +S=struct(bnet.CPD{eclass(F,1)}); +CPT = mk_stochastic(rand(size(S.CPT)) .* S.CPT); +bnet.CPD{eclass(F,1)} = tabular_CPD(bnet, F, 'CPT', CPT); + + +% Observation model +if unique_obs + CPT = zeros(ns(A), ns(C), 5); + CPT(1,1,1)=1; % Theo state 4 + CPT(1,2,2)=1; % Theo state 5 + CPT(1,3,3)=1; % Theo state 6 + CPT(2,1,4)=1; % Theo state 9 + CPT(2,2,5)=1; % Theo state 10 + %CPT(2,3,:) undefined + O = onodes(1); + bnet.CPD{eclass(O,1)} = tabular_CPD(bnet, O, 'CPT', CPT); +else + for i=[north east south west] + CPT = mk_stochastic(rand(ns(A), ns(C), 2)); + bnet.CPD{eclass(i,1)} = tabular_CPD(bnet, i, 'CPT', CPT); + end +end + +% Define the CPDs for slice 2 + +startprob = zeros(ns(U), ns(A)); +S = struct(bnet.CPD{eclass(A,2)}); +transprob = mk_stochastic(rand(size(S.transprob)) .* S.transprob); +bnet.CPD{eclass(A,2)} = hhmm2Q_CPD(bnet, A+ss, 'Fbelow', F, ... + 'startprob', startprob, 'transprob', transprob); + +S = struct(bnet.CPD{eclass(C,2)}); +transprob = mk_stochastic(rand(size(S.transprob)) .* S.transprob); +startprob = mk_stochastic(rand(size(S.startprob)) .* S.startprob); +bnet.CPD{eclass(C,2)} = hhmm2Q_CPD(bnet, C+ss, 'Fself', F, ... + 'startprob', startprob, 'transprob', transprob); + + diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/sample_from_map.m b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/sample_from_map.m new file mode 100644 index 00000000..816b741e --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/sample_from_map.m @@ -0,0 +1,41 @@ +if 0 +% Generate some sample paths + +bnet = mk_map_hhmm('p', 1); +% assign numbers to the nodes in topological order +U = 1; A = 2; C = 3; F = 4; O = 5; + + +seed = 0; +rand('state', seed); +randn('state', seed); + +% control policy = sweep right then left +T = 10; +ss = 5; +ev = cell(ss, T); +ev(U,:) = num2cell([R*ones(1,5) L*ones(1,5)]); + +% fix initial conditions to be in left most state +ev{A,1} = 1; +ev{C,1} = 1; +evidence = sample_dbn(bnet, 'length', T, 'evidence', ev) + + +% Now do same but with noisy actuators + +bnet = mk_map_hhmm('p', 0.8); +evidence = sample_dbn(bnet, 'length', T, 'evidence', ev) + +end + +% Now do same but with 4 observations per slice + +bnet = mk_map_hhmm('p', 0.8, 'obs_model', 'four'); +ss = bnet.nnodes_per_slice; + +ev = cell(ss, T); +ev(U,:) = num2cell([R*ones(1,5) L*ones(1,5)]); +ev{A,1} = 1; +ev{C,1} = 1; +evidence = sample_dbn(bnet, 'length', T, 'evidence', ev) diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Mgram/CVS/Entries b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Mgram/CVS/Entries new file mode 100644 index 00000000..c4379581 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Mgram/CVS/Entries @@ -0,0 +1,6 @@ +/letter2num.m/1.1.1.1/Fri Nov 22 23:10:20 2002// +/mgram1.m/1.1.1.1/Fri Nov 22 23:59:00 2002// +/mgram2.m/1.1.1.1/Tue Nov 26 22:04:24 2002// +/mgram3.m/1.1.1.1/Tue Nov 26 22:14:10 2002// +/num2letter.m/1.1.1.1/Fri Nov 22 23:07:40 2002// +D/Old//// diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Mgram/CVS/Repository b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Mgram/CVS/Repository new file mode 100644 index 00000000..5ede715e --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Mgram/CVS/Repository @@ -0,0 +1 @@ +FullBNT/BNT/examples/dynamic/HHMM/Mgram diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Mgram/CVS/Root b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Mgram/CVS/Root new file mode 100644 index 00000000..f3bd14a6 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Mgram/CVS/Root @@ -0,0 +1 @@ +:ext:nsaunier@bnt.cvs.sourceforge.net:/cvsroot/bnt diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Mgram/Old/CVS/Entries b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Mgram/Old/CVS/Entries new file mode 100644 index 00000000..07b688e2 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Mgram/Old/CVS/Entries @@ -0,0 +1,2 @@ +/mgram2.m/1.1.1.1/Sat Nov 23 00:44:34 2002// +D diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Mgram/Old/CVS/Repository b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Mgram/Old/CVS/Repository new file mode 100644 index 00000000..ba3ae6df --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Mgram/Old/CVS/Repository @@ -0,0 +1 @@ +FullBNT/BNT/examples/dynamic/HHMM/Mgram/Old diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Mgram/Old/CVS/Root b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Mgram/Old/CVS/Root new file mode 100644 index 00000000..f3bd14a6 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Mgram/Old/CVS/Root @@ -0,0 +1 @@ +:ext:nsaunier@bnt.cvs.sourceforge.net:/cvsroot/bnt diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Mgram/Old/mgram2.m b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Mgram/Old/mgram2.m new file mode 100644 index 00000000..719a3167 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Mgram/Old/mgram2.m @@ -0,0 +1,191 @@ +% like mgram1, except we use a durational HMM instead of an HHMM2 + +past = 0; + +words = {'the', 't', 'h', 'e'}; +data = 'the'; +nwords = length(words); +word_len = zeros(1, nwords); +word_prob = normalise(ones(1,nwords)); +word_logprob = log(word_prob); +for wi=1:nwords + word_len(wi)=length(words{wi}); +end +D = max(word_len); + + +alphasize = 26*2; +data = letter2num(data); +T = length(data); + +% node numbers +W = 1; % top level state = word id +L = 2; % bottom level state = letter position within word +F = 3; +O = 4; + +ss = 4; +intra = zeros(ss,ss); +intra(W,[F L O])=1; +intra(L,[O F])=1; + +inter = zeros(ss,ss); +inter(W,W)=1; +inter(L,L)=1; +inter(F,[W L O])=1; + +% node sizes +ns = zeros(1,ss); +ns(W) = nwords; +ns(L) = D; +ns(F) = 2; +ns(O) = alphasize; +ns2 = [ns ns]; + +% Make the DBN +bnet = mk_dbn(intra, inter, ns, 'observed', O); +eclass = bnet.equiv_class; + +% uniform start distrib over words, uniform trans mat +Wstart = normalise(ones(1,nwords)); +Wtrans = mk_stochastic(ones(nwords,nwords)); + +% always start in state d = length(word) for each bottom level HMM +Lstart = zeros(nwords, D); +for i=1:nwords + l = length(words{i}); + Lstart(i,l)=1; +end + +% make downcounters +RLtrans = mk_rightleft_transmat(D, 0); % 0 self loop prob +Ltrans = repmat(RLtrans, [1 1 nwords]); + +% Finish when downcoutner = 1 +Fprob = zeros(nwords, D, 2); +Fprob(:,1,2)=1; +Fprob(:,2:end,1)=1; + + +% Define CPDs for slice +bnet.CPD{eclass(W,1)} = tabular_CPD(bnet, W, 'CPT', Wstart); +bnet.CPD{eclass(L,1)} = tabular_CPD(bnet, L, 'CPT', Lstart); +bnet.CPD{eclass(F,1)} = tabular_CPD(bnet, F, 'CPT', Fprob); + + +% Define CPDs for slice 2 +bnet.CPD{eclass(W,2)} = hhmmQ_CPD(bnet, W+ss, 'Fbelow', F, 'startprob', Wstart, 'transprob', Wtrans); +bnet.CPD{eclass(L,2)} = hhmmQ_CPD(bnet, L+ss, 'Fself', F, 'Qps', W+ss, 'startprob', Lstart, 'transprob', Ltrans); + + +if 0 +% To test it is generating correctly, we create an artificial +% observation process that capitalizes at the start of a new segment +% Oprob(Ft-1,Qt,Dt,Yt) +Oprob = zeros(2,nwords,D,alphasize); +Oprob(1,1,3,letter2num('t'),1)=1; +Oprob(1,1,2,letter2num('h'),1)=1; +Oprob(1,1,1,letter2num('e'),1)=1; +Oprob(2,1,3,letter2num('T'),1)=1; +Oprob(2,1,2,letter2num('H'),1)=1; +Oprob(2,1,1,letter2num('E'),1)=1; +Oprob(1,2,1,letter2num('a'),1)=1; +Oprob(2,2,1,letter2num('A'),1)=1; +Oprob(1,3,1,letter2num('b'),1)=1; +Oprob(2,3,1,letter2num('B'),1)=1; +Oprob(1,4,1,letter2num('c'),1)=1; +Oprob(2,4,1,letter2num('C'),1)=1; + +% Oprob1(Qt,Dt,Yt) +Oprob1 = zeros(nwords,D,alphasize); +Oprob1(1,3,letter2num('t'),1)=1; +Oprob1(1,2,letter2num('h'),1)=1; +Oprob1(1,1,letter2num('e'),1)=1; +Oprob1(2,1,letter2num('a'),1)=1; +Oprob1(3,1,letter2num('b'),1)=1; +Oprob1(4,1,letter2num('c'),1)=1; + +bnet.CPD{eclass(O,2)} = tabular_CPD(bnet, O+ss, 'CPT', Oprob); +bnet.CPD{eclass(O,1)} = tabular_CPD(bnet, O, 'CPT', Oprob1); + +evidence = cell(ss,T); +%evidence{W,1}=1; +sample = cell2num(sample_dbn(bnet, 'length', T, 'evidence', evidence)); +str = num2letter(sample(4,:)) +end + + + + +[log_obslik, obslik, match] = mk_mgram_obslik(lower(data), words, word_len, word_prob); +% obslik(j,t,d) +softCPDpot = cell(ss,T); +ens = ns; +ens(O)=1; +ens2 = [ens ens]; +for t=2:T + dom = [F W+ss L+ss O+ss]; + % tab(Ft-1, Q2, Dt) + tab = ones(2, nwords, D); + if past + tab(1,:,:)=1; % if haven't finished previous word, likelihood is 1 + tab(2,:,:) = squeeze(obslik(:,t,:)); % otherwise likelihood of this segment + else + for d=1:max(1,min(D,T+1-t)) + tab(2,:,d) = squeeze(obslik(:,t+d-1,d)); + end + end + softCPDpot{O,t} = dpot(dom, ens2(dom), tab); +end +t = 1; +dom = [W L O]; +% tab(Q2, Dt) +tab = ones(nwords, D); +if past + tab = squeeze(obslik(:,t,:)); +else + for d=1:min(D,T-t) + tab(:,d) = squeeze(obslik(:,t+d-1,d)); + end +end +softCPDpot{O,t} = dpot(dom, ens(dom), tab); + + +%bnet.observed = []; +% uniformative observations +%bnet.CPD{eclass(O,2)} = tabular_CPD(bnet, O+ss, 'CPT', mk_stochastic(ones(2,nwords,D,alphasize))); +%bnet.CPD{eclass(O,1)} = tabular_CPD(bnet, O, 'CPT', mk_stochastic(ones(nwords,D,alphasize))); + +engine = jtree_dbn_inf_engine(bnet); +evidence = cell(ss,T); +% we add dummy data to O to force its effective size to be 1. +% The actual values have already been incorporated into softCPDpot +evidence(O,:) = num2cell(ones(1,T)); +[engine, ll_dbn] = enter_evidence(engine, evidence, 'softCPDpot', softCPDpot); + + +%evidence(F,:) = num2cell(2*ones(1,T)); +%[engine, ll_dbn] = enter_evidence(engine, evidence); + + +gamma = zeros(nwords, T); +for t=1:T + m = marginal_nodes(engine, [W F], t); + gamma(:,t) = m.T(:,2); +end + +gamma + +xidbn = zeros(nwords, nwords); +for t=1:T-1 + m = marginal_nodes(engine, [W F W+ss], t); + xidbn = xidbn + squeeze(m.T(:,2,:)); +end + +% thee +% xidbn(1,4) = 0.9412 the->e +% (2,3)=0.0588 t->h +% (3,4)=0.0588 h-e +% (4,4)=0.0588 e-e + + diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Mgram/letter2num.m b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Mgram/letter2num.m new file mode 100644 index 00000000..f4e3f1d9 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Mgram/letter2num.m @@ -0,0 +1,12 @@ +function n = letter2num(l) + +% map a-z to 1:26 and A-Z to 27:52 +punct_code = [32:47 58:64 91:96 123:126]; +digits_code = 48:57; +upper_code = 65:90; +lower_code = 97:122; + +c = double(l); +n = c-96; +ndx = find(n <= 0); % upper case +n(ndx) = c(ndx) - 64 + 26; diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Mgram/mgram1.m b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Mgram/mgram1.m new file mode 100644 index 00000000..52ca472e --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Mgram/mgram1.m @@ -0,0 +1,116 @@ +% a multigram is a degenerate 2HHMM where the bottom level HMMs emit deterministic strings +% and the the top level abstract states are independent of each other +% cf. HSMM/test_mgram2 + +words = {'the', 't', 'h', 'e'}; +data = 'the'; +nwords = length(words); +word_len = zeros(1, nwords); +word_prob = normalise(ones(1,nwords)); +word_logprob = log(word_prob); +for wi=1:nwords + word_len(wi)=length(words{wi}); +end +D = max(word_len); + +alphasize = 26; +data = letter2num(data); +T = length(data); + +% node numbers +W = 1; % top level state = word id +L = 2; % bottom level state = letter position within word +F = 3; +O = 4; + +ss = 4; +intra = zeros(ss,ss); +intra(W,[F L O])=1; +intra(L,[O F])=1; + +inter = zeros(ss,ss); +inter(W,W)=1; +inter(L,L)=1; +inter(F,[W L])=1; + +% node sizes +ns = zeros(1,ss); +ns(W) = nwords; +ns(L) = D; +ns(F) = 2; +ns(O) = alphasize; + + +% Make the DBN +bnet = mk_dbn(intra, inter, ns, 'observed', O); +eclass = bnet.equiv_class; + + + +% uniform start distrib over words, uniform trans mat +Wstart = normalise(ones(1,nwords)); +Wtrans = mk_stochastic(ones(nwords,nwords)); + +% always start in state 1 for each bottom level HMM +delta1_start = zeros(1, D); +delta1_start(1) = 1; +Lstart = repmat(delta1_start, nwords, 1); +LRtrans = mk_leftright_transmat(D, 0); % 0 self loop prob +Ltrans = repmat(LRtrans, [1 1 nwords]); + +% Finish in the last letter of each word +Fprob = zeros(nwords, D, 2); +Fprob(:,:,1)=1; +for i=1:nwords + Fprob(i,length(words{i}),2)=1; + Fprob(i,length(words{i}),1)=0; +end + +% Each state uniquely emits a letter +Oprob = zeros(nwords, D, alphasize); +for i=1:nwords + for l=1:length(words{i}) + a = double(words{i}(l))-96; + Oprob(i,l,a)=1; + end +end + + +% Define CPDs for slice +bnet.CPD{eclass(W,1)} = tabular_CPD(bnet, W, 'CPT', Wstart); +bnet.CPD{eclass(L,1)} = tabular_CPD(bnet, L, 'CPT', Lstart); +bnet.CPD{eclass(F,1)} = tabular_CPD(bnet, F, 'CPT', Fprob); +bnet.CPD{eclass(O,1)} = tabular_CPD(bnet, O, 'CPT', Oprob); + +% Define CPDs for slice 2 +bnet.CPD{eclass(W,2)} = hhmmQ_CPD(bnet, W+ss, 'Fbelow', F, 'startprob', Wstart, 'transprob', Wtrans); +bnet.CPD{eclass(L,2)} = hhmmQ_CPD(bnet, L+ss, 'Fself', F, 'Qps', W+ss, 'startprob', Lstart, 'transprob', Ltrans); + +evidence = cell(ss,T); +evidence{W,1}=1; +sample = cell2num(sample_dbn(bnet, 'length', T, 'evidence', evidence)); +str = lower(sample(4,:)) + +engine = jtree_dbn_inf_engine(bnet); +evidence = cell(ss,T); +evidence(O,:) = num2cell(data); +[engine, ll_dbn] = enter_evidence(engine, evidence); + +gamma = zeros(nwords, T); +for t=1:T + m = marginal_nodes(engine, [W F], t); + gamma(:,t) = m.T(:,2); +end +gamma + +xidbn = zeros(nwords, nwords); +for t=1:T-1 + m = marginal_nodes(engine, [W F W+ss], t); + xidbn = xidbn + squeeze(m.T(:,2,:)); +end + +% thee +% xidbn(1,4) = 0.9412 the->e +% (2,3)=0.0588 t->h +% (3,4)=0.0588 h-e +% (4,4)=0.0588 e-e diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Mgram/mgram2.m b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Mgram/mgram2.m new file mode 100644 index 00000000..c61f855a --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Mgram/mgram2.m @@ -0,0 +1,200 @@ +% Like a durational HMM, except we use soft evidence on the observed nodes. +% Should give the same results as HSMM/test_mgram2. + +past = 1; +% If past=1, P(Yt|Qt=j,Dt=d) = P(y_{t-d+1:t}|j) +% If past=0, P(Yt|Qt=j,Dt=d) = P(y_{t:t+d-1}|j) - future evidence + +words = {'the', 't', 'h', 'e'}; +data = 'the'; +nwords = length(words); +word_len = zeros(1, nwords); +word_prob = normalise(ones(1,nwords)); +word_logprob = log(word_prob); +for wi=1:nwords + word_len(wi)=length(words{wi}); +end +D = max(word_len); + + +alphasize = 26*2; +data = letter2num(data); +T = length(data); + +% node numbers +W = 1; % top level state = word id +L = 2; % bottom level state = letter position within word +F = 3; +O = 4; + +ss = 4; +intra = zeros(ss,ss); +intra(W,[F L O])=1; +intra(L,[O F])=1; + +inter = zeros(ss,ss); +inter(W,W)=1; +inter(L,L)=1; +inter(F,[W L O])=1; + +% node sizes +ns = zeros(1,ss); +ns(W) = nwords; +ns(L) = D; +ns(F) = 2; +ns(O) = alphasize; +ns2 = [ns ns]; + +% Make the DBN +bnet = mk_dbn(intra, inter, ns, 'observed', O); +eclass = bnet.equiv_class; + +% uniform start distrib over words, uniform trans mat +Wstart = normalise(ones(1,nwords)); +Wtrans = mk_stochastic(ones(nwords,nwords)); +%Wtrans = ones(nwords,nwords); + +% always start in state d = length(word) for each bottom level HMM +Lstart = zeros(nwords, D); +for i=1:nwords + l = length(words{i}); + Lstart(i,l)=1; +end + +% make downcounters +RLtrans = mk_rightleft_transmat(D, 0); % 0 self loop prob +Ltrans = repmat(RLtrans, [1 1 nwords]); + +% Finish when downcoutner = 1 +Fprob = zeros(nwords, D, 2); +Fprob(:,1,2)=1; +Fprob(:,2:end,1)=1; + + +% Define CPDs for slice 1 +bnet.CPD{eclass(W,1)} = tabular_CPD(bnet, W, 'CPT', Wstart); +bnet.CPD{eclass(L,1)} = tabular_CPD(bnet, L, 'CPT', Lstart); +bnet.CPD{eclass(F,1)} = tabular_CPD(bnet, F, 'CPT', Fprob); + + +% Define CPDs for slice 2 +bnet.CPD{eclass(W,2)} = hhmmQ_CPD(bnet, W+ss, 'Fbelow', F, 'startprob', Wstart, 'transprob', Wtrans); +bnet.CPD{eclass(L,2)} = hhmmQ_CPD(bnet, L+ss, 'Fself', F, 'Qps', W+ss, 'startprob', Lstart, 'transprob', Ltrans); + + +if 0 +% To test it is generating correctly, we create an artificial +% observation process that capitalizes at the start of a new segment +% Oprob(Ft-1,Qt,Dt,Yt) +Oprob = zeros(2,nwords,D,alphasize); +Oprob(1,1,3,letter2num('t'),1)=1; +Oprob(1,1,2,letter2num('h'),1)=1; +Oprob(1,1,1,letter2num('e'),1)=1; +Oprob(2,1,3,letter2num('T'),1)=1; +Oprob(2,1,2,letter2num('H'),1)=1; +Oprob(2,1,1,letter2num('E'),1)=1; +Oprob(1,2,1,letter2num('a'),1)=1; +Oprob(2,2,1,letter2num('A'),1)=1; +Oprob(1,3,1,letter2num('b'),1)=1; +Oprob(2,3,1,letter2num('B'),1)=1; +Oprob(1,4,1,letter2num('c'),1)=1; +Oprob(2,4,1,letter2num('C'),1)=1; + +% Oprob1(Qt,Dt,Yt) +Oprob1 = zeros(nwords,D,alphasize); +Oprob1(1,3,letter2num('t'),1)=1; +Oprob1(1,2,letter2num('h'),1)=1; +Oprob1(1,1,letter2num('e'),1)=1; +Oprob1(2,1,letter2num('a'),1)=1; +Oprob1(3,1,letter2num('b'),1)=1; +Oprob1(4,1,letter2num('c'),1)=1; + +bnet.CPD{eclass(O,2)} = tabular_CPD(bnet, O+ss, 'CPT', Oprob); +bnet.CPD{eclass(O,1)} = tabular_CPD(bnet, O, 'CPT', Oprob1); + +evidence = cell(ss,T); +%evidence{W,1}=1; +sample = cell2num(sample_dbn(bnet, 'length', T, 'evidence', evidence)); +str = num2letter(sample(4,:)) +end + + +if 1 + +[log_obslik, obslik, match] = mk_mgram_obslik(lower(data), words, word_len, word_prob); +% obslik(j,t,d) +softCPDpot = cell(ss,T); +ens = ns; +ens(O)=1; +ens2 = [ens ens]; +for t=2:T + dom = [F W+ss L+ss O+ss]; + % tab(Ft-1, Q2, Dt) + tab = ones(2, nwords, D); + if past + tab(1,:,:)=1; % if haven't finished previous word, likelihood is 1 + %tab(2,:,:) = squeeze(obslik(:,t,:)); % otherwise likelihood of this segment + for d=1:min(t,D) + tab(2,:,d) = squeeze(obslik(:,t,d)); + end + else + for d=1:max(1,min(D,T+1-t)) + tab(2,:,d) = squeeze(obslik(:,t+d-1,d)); + end + end + softCPDpot{O,t} = dpot(dom, ens2(dom), tab); +end +t = 1; +dom = [W L O]; +% tab(Q2, Dt) +tab = ones(nwords, D); +if past + %tab = squeeze(obslik(:,t,:)); + tab(:,1) = squeeze(obslik(:,t,1)); +else + for d=1:min(D,T-t) + tab(:,d) = squeeze(obslik(:,t+d-1,d)); + end +end +softCPDpot{O,t} = dpot(dom, ens(dom), tab); + + +%bnet.observed = []; +% uniformative observations +%bnet.CPD{eclass(O,2)} = tabular_CPD(bnet, O+ss, 'CPT', mk_stochastic(ones(2,nwords,D,alphasize))); +%bnet.CPD{eclass(O,1)} = tabular_CPD(bnet, O, 'CPT', mk_stochastic(ones(nwords,D,alphasize))); + +engine = jtree_dbn_inf_engine(bnet); +evidence = cell(ss,T); +% we add dummy data to O to force its effective size to be 1. +% The actual values have already been incorporated into softCPDpot +evidence(O,:) = num2cell(ones(1,T)); +[engine, ll_dbn] = enter_evidence(engine, evidence, 'softCPDpot', softCPDpot); + + +%evidence(F,:) = num2cell(2*ones(1,T)); +%[engine, ll_dbn] = enter_evidence(engine, evidence); + + +gamma = zeros(nwords, T); +for t=1:T + m = marginal_nodes(engine, [W F], t); + gamma(:,t) = m.T(:,2); +end + +gamma + +xidbn = zeros(nwords, nwords); +for t=1:T-1 + m = marginal_nodes(engine, [W F W+ss], t); + xidbn = xidbn + squeeze(m.T(:,2,:)); +end + +% thee +% xidbn(1,4) = 0.9412 the->e +% (2,3)=0.0588 t->h +% (3,4)=0.0588 h-e +% (4,4)=0.0588 e-e + + +end diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Mgram/mgram3.m b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Mgram/mgram3.m new file mode 100644 index 00000000..49228584 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Mgram/mgram3.m @@ -0,0 +1,235 @@ +% like mgram2, except we unroll the DBN so we can use smaller +% state spaces for the early duration nodes: +% the state spaces are D1 in {1}, D2 in {1,2} + +past = 1; + +words = {'the', 't', 'h', 'e'}; +data = 'the'; +nwords = length(words); +word_len = zeros(1, nwords); +word_prob = normalise(ones(1,nwords)); +word_logprob = log(word_prob); +for wi=1:nwords + word_len(wi)=length(words{wi}); +end +D = max(word_len); + + +alphasize = 26*2; +data = letter2num(data); +T = length(data); + +% node numbers +W = 1; % top level state = word id +L = 2; % bottom level state = letter position within word +F = 3; +O = 4; + +ss = 4; +intra = zeros(ss,ss); +intra(W,[F L O])=1; +intra(L,[O F])=1; + +inter = zeros(ss,ss); +inter(W,W)=1; +inter(L,L)=1; +inter(F,[W L O])=1; + +T = 3; +dag = unroll_dbn_topology(intra, inter, T); + +% node sizes +ns = zeros(1,ss); +ns(W) = nwords; +ns(L) = D; +ns(F) = 2; +ns(O) = alphasize; +ns = repmat(ns(:), [1 T]); +for d=1:D + ns(d,L)=d; % max duration +end +ns = ns(:); + +% Equiv class in brackets for D=3 +% The Lt's are not tied until t>=D, since they have different sizes. +% W1 and W2 are not tied since they have different parent sets. + +% W1 (1) W2 (5) W3 (5) W4 (5) +% L1 (2) L2 (6) L3 (7) L4 (7) +% F1 (3) F2 (3) F3 (4) F3 (4) +% O1 (4) O2 (4) O2 (4) O4 (4) + +% Since we are not learning, we can dispense with tying + +% Make the bnet +Wnodes = unroll_set(W, ss, T); +Lnodes = unroll_set(L, ss, T); +Fnodes = unroll_set(F, ss, T); +Onodes = unroll_set(O, ss, T); + +bnet = mk_bnet(dag, ns); +eclass = bnet.equiv_class; + +% uniform start distrib over words, uniform trans mat +Wstart = normalise(ones(1,nwords)); +Wtrans = mk_stochastic(ones(nwords,nwords)); +bnet.CPD{eclass(Wnodes(1))} = tabular_CPD(bnet, Wnodes(1), 'CPT', Wstart); +for t=2:T +bnet.CPD{eclass(Wnodes(t))} = hhmmQ_CPD(bnet, Wnodes(t), 'Fbelow', Fnodes(t-1), ... + 'startprob', Wstart, 'transprob', Wtrans); +end + +% always start in state d = length(word) for each bottom level HMM +% and then count down +% make downcounters +RLtrans = mk_rightleft_transmat(D, 0); % 0 self loop prob +Ltrans = repmat(RLtrans, [1 1 nwords]); + +for t=1:T + Lstart = zeros(nwords, min(t,D)); + for i=1:nwords + l = length(words{i}); + Lstart(i,l)=1; + if d==1 + bnet.CPD{eclass(Lnodes(1))} = tabular_CPD(bnet, Lnodes(1), 'CPT', Lstart); + else + bnet.CPD{eclass(Lnodes(t))} = hhmmQ_CPD(bnet, Lnodes(t), 'Fself', Fnodes(t-1), 'Qps', Wnodes(t), ... + 'startprob', Lstart, 'transprob', Ltrans); + end + end +end + + +% Finish when downcoutner = 1 +Fprob = zeros(nwords, D, 2); +Fprob(:,1,2)=1; +Fprob(:,2:end,1)=1; + + +% Define CPDs for slice +bnet.CPD{eclass(W,1)} = tabular_CPD(bnet, W, 'CPT', Wstart); +bnet.CPD{eclass(L,1)} = tabular_CPD(bnet, L, 'CPT', Lstart); +bnet.CPD{eclass(F,1)} = tabular_CPD(bnet, F, 'CPT', Fprob); + + +% Define CPDs for slice 2 +bnet.CPD{eclass(W,2)} = hhmmQ_CPD(bnet, W+ss, 'Fbelow', F, 'startprob', Wstart, 'transprob', Wtrans); +bnet.CPD{eclass(L,2)} = hhmmQ_CPD(bnet, L+ss, 'Fself', F, 'Qps', W+ss, 'startprob', Lstart, 'transprob', Ltrans); + + +if 0 +% To test it is generating correctly, we create an artificial +% observation process that capitalizes at the start of a new segment +% Oprob(Ft-1,Qt,Dt,Yt) +Oprob = zeros(2,nwords,D,alphasize); +Oprob(1,1,3,letter2num('t'),1)=1; +Oprob(1,1,2,letter2num('h'),1)=1; +Oprob(1,1,1,letter2num('e'),1)=1; +Oprob(2,1,3,letter2num('T'),1)=1; +Oprob(2,1,2,letter2num('H'),1)=1; +Oprob(2,1,1,letter2num('E'),1)=1; +Oprob(1,2,1,letter2num('a'),1)=1; +Oprob(2,2,1,letter2num('A'),1)=1; +Oprob(1,3,1,letter2num('b'),1)=1; +Oprob(2,3,1,letter2num('B'),1)=1; +Oprob(1,4,1,letter2num('c'),1)=1; +Oprob(2,4,1,letter2num('C'),1)=1; + +% Oprob1(Qt,Dt,Yt) +Oprob1 = zeros(nwords,D,alphasize); +Oprob1(1,3,letter2num('t'),1)=1; +Oprob1(1,2,letter2num('h'),1)=1; +Oprob1(1,1,letter2num('e'),1)=1; +Oprob1(2,1,letter2num('a'),1)=1; +Oprob1(3,1,letter2num('b'),1)=1; +Oprob1(4,1,letter2num('c'),1)=1; + +bnet.CPD{eclass(O,2)} = tabular_CPD(bnet, O+ss, 'CPT', Oprob); +bnet.CPD{eclass(O,1)} = tabular_CPD(bnet, O, 'CPT', Oprob1); + +evidence = cell(ss,T); +%evidence{W,1}=1; +sample = cell2num(sample_dbn(bnet, 'length', T, 'evidence', evidence)); +str = num2letter(sample(4,:)) +end + + + + +[log_obslik, obslik, match] = mk_mgram_obslik(lower(data), words, word_len, word_prob); +% obslik(j,t,d) +softCPDpot = cell(ss,T); +ens = ns; +ens(O)=1; +ens2 = [ens ens]; +for t=2:T + dom = [F W+ss L+ss O+ss]; + % tab(Ft-1, Q2, Dt) + tab = ones(2, nwords, D); + if past + tab(1,:,:)=1; % if haven't finished previous word, likelihood is 1 + %tab(2,:,:) = squeeze(obslik(:,t,:)); % otherwise likelihood of this segment + for d=1:min(t,D) + tab(2,:,d) = squeeze(obslik(:,t,d)); + end + else + for d=1:max(1,min(D,T+1-t)) + tab(2,:,d) = squeeze(obslik(:,t+d-1,d)); + end + end + softCPDpot{O,t} = dpot(dom, ens2(dom), tab); +end +t = 1; +dom = [W L O]; +% tab(Q2, Dt) +tab = ones(nwords, D); +if past + %tab = squeeze(obslik(:,t,:)); + tab(:,1) = squeeze(obslik(:,t,1)); +else + for d=1:min(D,T-t) + tab(:,d) = squeeze(obslik(:,t+d-1,d)); + end +end +softCPDpot{O,t} = dpot(dom, ens(dom), tab); + + +%bnet.observed = []; +% uniformative observations +%bnet.CPD{eclass(O,2)} = tabular_CPD(bnet, O+ss, 'CPT', mk_stochastic(ones(2,nwords,D,alphasize))); +%bnet.CPD{eclass(O,1)} = tabular_CPD(bnet, O, 'CPT', mk_stochastic(ones(nwords,D,alphasize))); + +engine = jtree_dbn_inf_engine(bnet); +evidence = cell(ss,T); +% we add dummy data to O to force its effective size to be 1. +% The actual values have already been incorporated into softCPDpot +evidence(O,:) = num2cell(ones(1,T)); +[engine, ll_dbn] = enter_evidence(engine, evidence, 'softCPDpot', softCPDpot); + + +%evidence(F,:) = num2cell(2*ones(1,T)); +%[engine, ll_dbn] = enter_evidence(engine, evidence); + + +gamma = zeros(nwords, T); +for t=1:T + m = marginal_nodes(engine, [W F], t); + gamma(:,t) = m.T(:,2); +end + +gamma + +xidbn = zeros(nwords, nwords); +for t=1:T-1 + m = marginal_nodes(engine, [W F W+ss], t); + xidbn = xidbn + squeeze(m.T(:,2,:)); +end + +% thee +% xidbn(1,4) = 0.9412 the->e +% (2,3)=0.0588 t->h +% (3,4)=0.0588 h-e +% (4,4)=0.0588 e-e + + diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Mgram/num2letter.m b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Mgram/num2letter.m new file mode 100644 index 00000000..139b81fa --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Mgram/num2letter.m @@ -0,0 +1,10 @@ +function l = num2letter(n) + +% map 1:26 to a-z and 27:52 to A-Z +punct_code = [32:47 58:64 91:96 123:126]; +digits_code = 48:57; +upper_code = 65:90; +lower_code = 97:122; + +letters = [char(lower_code) char(upper_code)]; +l = letters(n); diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Motif/CVS/Entries b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Motif/CVS/Entries new file mode 100644 index 00000000..93258c2a --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Motif/CVS/Entries @@ -0,0 +1,5 @@ +/fixed_args_mk_motif_hhmm.m/1.1.1.1/Wed May 29 15:59:54 2002// +/learn_motif_hhmm.m/1.1.1.1/Tue Jul 2 22:56:14 2002// +/mk_motif_hhmm.m/1.1.1.1/Wed May 29 15:59:54 2002// +/sample_motif_hhmm.m/1.1.1.1/Wed May 29 15:59:54 2002// +D diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Motif/CVS/Repository b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Motif/CVS/Repository new file mode 100644 index 00000000..5062cfd4 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Motif/CVS/Repository @@ -0,0 +1 @@ +FullBNT/BNT/examples/dynamic/HHMM/Motif diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Motif/CVS/Root b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Motif/CVS/Root new file mode 100644 index 00000000..f3bd14a6 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Motif/CVS/Root @@ -0,0 +1 @@ +:ext:nsaunier@bnt.cvs.sourceforge.net:/cvsroot/bnt diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Motif/fixed_args_mk_motif_hhmm.m b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Motif/fixed_args_mk_motif_hhmm.m new file mode 100644 index 00000000..ce4e288b --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Motif/fixed_args_mk_motif_hhmm.m @@ -0,0 +1,99 @@ +function bnet = fixed_args_mk_motif_hhmm(motif_length, motif_pattern, background_char) +% +% BNET = MK_MOTIF_HHMM(MOTIF_LENGTH) +% Make the following HHMM +% +% S2 <----------------------> S1 +% | | +% | | +% M1 -> M2 -> M3 -> end B1 -> end +% +% where Mi represents the i'th letter in the motif +% and B is the background state. +% Si chooses between running the motif or the background. +% The Si and B states have self loops (not shown). +% +% The transition params are defined to respect the above topology. +% The background is uniform; each motif state has a random obs. distribution. +% +% BNET = MK_MOTIF_HHMM(MOTIF_LENGTH, MOTIF_PATTERN) +% In this case, we make the motif submodel deterministically +% emit the motif pattern. +% +% BNET = MK_MOTIF_HHMM(MOTIF_LENGTH, MOTIF_PATTERN, BACKGROUND_CHAR) +% In this case, we make the background submodel +% deterministically emit the specified character (to make the pattern +% easier to see). + +if nargin < 2, motif_pattern = []; end +if nargin < 3, background_char = []; end + +chars = ['a', 'c', 'g', 't']; +Osize = length(chars); + +motif_length = length(motif_pattern); +Qsize = [2 motif_length]; +Qnodes = 1:2; +D = 2; +transprob = cell(1,D); +termprob = cell(1,D); +startprob = cell(1,D); + +% startprob{d}(k,j), startprob{1}(1,j) +% transprob{d}(i,k,j), transprob{1}(i,j) +% termprob{d}(k,j) + + +% LEVEL 1 + +startprob{1} = zeros(1, 2); +startprob{1} = [1 0]; % always start in the background model + +% When in the background state, we stay there with high prob +% When in the motif state, we immediately return to the background state. +transprob{1} = [0.8 0.2; + 1.0 0.0]; + + +% LEVEL 2 +startprob{2} = 'leftstart'; % both submodels start in substate 1 +transprob{2} = zeros(motif_length, 2, motif_length); +termprob{2} = zeros(2, motif_length); + +% In the background model, we only use state 1. +transprob{2}(1,1,1) = 1; % self loop +termprob{2}(1,1) = 0.2; % prob transition to end state + +% Motif model +transprob{2}(:,2,:) = mk_leftright_transmat(motif_length, 0); % no self loops +termprob{2}(2,end) = 1.0; % last state immediately terminates + + +% OBS LEVEl + +obsprob = zeros([Qsize Osize]); +if isempty(background_char) + % uniform background model + obsprob(1,1,:) = normalise(ones(Osize,1)); +else + % deterministic background model (easy to see!) + m = find(chars==background_char); + obsprob(1,1,m) = 1.0; +end + +if gen_motif + % initialise with true motif (cheating) + for i=1:motif_length + m = find(chars == motif_pattern(i)); + obsprob(2,i,m) = 1.0; + end +else + obsprob(2,:,:) = mk_stochastic(ones(motif_length, Osize)); +end + +Oargs = {'CPT', obsprob}; + +[bnet, Qnodes, Fnodes, Onode] = mk_hhmm('Qsizes', Qsize, 'Osize', Osize, 'discrete_obs', 1, ... + 'Oargs', Oargs, 'Ops', Qnodes(1:2), ... + 'startprob', startprob, 'transprob', transprob, 'termprob', termprob); + diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Motif/learn_motif_hhmm.m b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Motif/learn_motif_hhmm.m new file mode 100644 index 00000000..54553460 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Motif/learn_motif_hhmm.m @@ -0,0 +1,75 @@ + +seed = 0; +rand('state', seed); +randn('state', seed); + +chars = ['a', 'c', 'g', 't']; +motif = 'accca'; +motif_length = length(motif); +motif_code = zeros(1, motif_length); +for i=1:motif_length + motif_code(i) = find(chars == motif(i)); +end + +[bnet_init, Qnodes, Fnodes, Onode] = mk_motif_hhmm('motif_length', length(motif)); +%[bnet_init, Qnodes, Fnodes, Onode] = mk_motif_hhmm('motif_pattern', motif); +ss = bnet_init.nnodes_per_slice; + + + +% We generate a training set by creating uniform sequences, +% and inserting a single motif at a random location. +ntrain = 100; +T = 20; +cases = cell(1, ntrain); + +if 1 + % uniform background + background_dist = normalise(ones(1, length(chars))); +end +if 0 + % use a constant background + background_dist = zeros(1, length(chars)); + m = find(chars=='t'); + background_dist(m) = 1.0; +end +if 0 + % use a background skewed away from the motif + p = 0.01; q = (1-(2*p))/2; + background_dist = [p p q q]; +end + +unif_pos = normalise(ones(1, T-length(motif))); +cases = cell(1, ntrain); +data = zeros(1,T); +for i=1:ntrain + data = sample_discrete(background_dist, 1, T); + L = sample_discrete(unif_pos, 1, 1); + data(L:L+length(motif)-1) = motif_code; + cases{i} = cell(ss, T); + cases{i}(Onode,:) = num2cell(data); +end +disp('sample training cases') +for i=1:5 + chars(cell2num(cases{i}(Onode,:))) +end + +engine_init = hmm_inf_engine(bnet_init); + +[bnet_learned, LL, engine_learned] = ... + learn_params_dbn_em(engine_init, cases, 'max_iter', 100, 'thresh', 1e-2); +% 'anneal', 1, 'anneal_rate', 0.7); + +% extract the learned motif profile +eclass = bnet_learned.equiv_class; +CPDO=struct(bnet_learned.CPD{eclass(Onode,1)}); +fprintf('columns = chars, rows = states\n'); +profile_learned = squeeze(CPDO.CPT(2,:,:)) +[m,ndx] = max(profile_learned, [], 2); +map_motif_learned = chars(ndx) +back_learned = squeeze(CPDO.CPT(1,1,:))' +%map_back_learned = chars(argmax(back_learned)) + +CPDO_init = struct(bnet_init.CPD{eclass(Onode,1)}); +profile_init = squeeze(CPDO_init.CPT(2,:,:)); +back_init = squeeze(CPDO_init.CPT(1,1,:))'; diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Motif/mk_motif_hhmm.m b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Motif/mk_motif_hhmm.m new file mode 100644 index 00000000..32980397 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Motif/mk_motif_hhmm.m @@ -0,0 +1,137 @@ +function [bnet, Qnodes, Fnodes, Onode] = mk_motif_hhmm(varargin) +% [bnet, Qnodes, Fnodes, Onode] = mk_motif_hhmm(...) +% +% Make the following HHMM +% +% S2 <----------------------> S1 +% | | +% | | +% M1 -> M2 -> M3 -> end B1 -> end +% +% where Mi represents the i'th letter in the motif +% and B is the background state. +% Si chooses between running the motif or the background. +% The Si and B states have self loops (not shown). +% +% The transition params are defined to respect the above topology. +% The background is uniform; each motif state has a random obs. distribution. +% +% Optional params: +% motif_length - required, unless we specify motif_pattern +% motif_pattern - if specified, we make the motif submodel deterministically +% emit this pattern +% background - if specified, we make the background submodel +% deterministically emit this (makes the motif easier to see!) + + +args = varargin; +nargs = length(args); + +% extract pattern, if any +motif_pattern = []; +for i=1:2:nargs + switch args{i}, + case 'motif_pattern', motif_pattern = args{i+1}; + end +end + +% set defaults +motif_length = length(motif_pattern); +background_char = []; + +% get params +for i=1:2:nargs + switch args{i}, + case 'motif_length', motif_length = args{i+1}; + case 'background', background_char = args{i+1}; + end +end + + +chars = ['a', 'c', 'g', 't']; +Osize = length(chars); + +Qsize = [2 motif_length]; +Qnodes = 1:2; +D = 2; +transprob = cell(1,D); +termprob = cell(1,D); +startprob = cell(1,D); + +% startprob{d}(k,j), startprob{1}(1,j) +% transprob{d}(i,k,j), transprob{1}(i,j) +% termprob{d}(k,j) + + +% LEVEL 1 + +startprob{1} = zeros(1, 2); +startprob{1} = [1 0]; % always start in the background model + +% When in the background state, we stay there with high prob +% When in the motif state, we immediately return to the background state. +transprob{1} = [0.8 0.2; + 1.0 0.0]; + + +% LEVEL 2 +startprob{2} = 'leftstart'; % both submodels start in substate 1 +transprob{2} = zeros(motif_length, 2, motif_length); +termprob{2} = zeros(2, motif_length); + +% In the background model, we only use state 1. +transprob{2}(1,1,1) = 1; % self loop +termprob{2}(1,1) = 0.2; % prob transition to end state + +% Motif model +transprob{2}(:,2,:) = mk_leftright_transmat(motif_length, 0); % no self loops +termprob{2}(2,end) = 1.0; % last state immediately terminates + + +% OBS LEVEl + +obsprob = zeros([Qsize Osize]); +if isempty(background_char) + % uniform background model + %obsprob(1,1,:) = normalise(ones(Osize,1)); + obsprob(1,1,:) = normalise(rand(Osize,1)); +else + % deterministic background model (easy to see!) + m = find(chars==background_char); + obsprob(1,1,m) = 1.0; +end + +if ~isempty(motif_pattern) + % initialise with true motif (cheating) + for i=1:motif_length + m = find(chars == motif_pattern(i)); + obsprob(2,i,m) = 1.0; + end +else + obsprob(2,:,:) = mk_stochastic(rand(motif_length, Osize)); +end + +if 0 + Oargs = {'CPT', obsprob}; +else + % We use a minent prior for the emission distribution for the states in the motif model + % (but not the background model). This encourages nearly deterministic distributions. + % We create an index matrix (where M = motif length) + % [2 1 + % 2 2 + % ... + % 2 M] + % and then convert this to a list of integers, which + % specifies when to use the minent prior (Q1=2 specifies motif model). + M = motif_length; + ndx = [2*ones(M,1) (1:M)']; + pcases = subv2ind([2 motif_length], ndx); + Oargs = {'CPT', obsprob, 'prior_type', 'entropic', 'entropic_pcases', pcases}; +end + + + +[bnet, Qnodes, Fnodes, Onode] = mk_hhmm('Qsizes', Qsize, 'Osize', Osize, 'discrete_obs', 1, ... + 'Oargs', Oargs, 'Ops', Qnodes(1:2), ... + 'startprob', startprob, 'transprob', transprob, 'termprob', termprob); + diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Motif/sample_motif_hhmm.m b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Motif/sample_motif_hhmm.m new file mode 100644 index 00000000..b5822e10 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Motif/sample_motif_hhmm.m @@ -0,0 +1,10 @@ +%bnet = mk_motif_hhmm('motif_pattern', 'acca', 'background', 't'); +bnet = mk_motif_hhmm('motif_pattern', 'accaggggga', 'background', []); + +chars = ['a', 'c', 'g', 't']; +Tmax = 100; + +for seqi=1:5 + evidence = cell2num(sample_dbn(bnet, 'length', Tmax)); + chars(evidence(end,:)) +end diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Old/CVS/Entries b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Old/CVS/Entries new file mode 100644 index 00000000..6caae4a6 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Old/CVS/Entries @@ -0,0 +1,8 @@ +/mk_abcd_hhmm.m/1.1.1.1/Wed May 29 15:59:54 2002// +/mk_arrow_alpha_hhmm3.m/1.1.1.1/Wed May 29 15:59:54 2002// +/mk_hhmm2.m/1.1.1.1/Wed May 29 15:59:54 2002// +/mk_hhmm3.m/1.1.1.1/Wed May 29 15:59:54 2002// +/mk_hhmm3_args.m/1.1.1.1/Wed May 29 15:59:54 2002// +/motif_hhmm.m/1.1.1.1/Wed May 29 15:59:54 2002// +/remove_hhmm_end_state.m/1.1.1.1/Wed May 29 15:59:54 2002// +D diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Old/CVS/Repository b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Old/CVS/Repository new file mode 100644 index 00000000..cc9acc63 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Old/CVS/Repository @@ -0,0 +1 @@ +FullBNT/BNT/examples/dynamic/HHMM/Old diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Old/CVS/Root b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Old/CVS/Root new file mode 100644 index 00000000..f3bd14a6 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Old/CVS/Root @@ -0,0 +1 @@ +:ext:nsaunier@bnt.cvs.sourceforge.net:/cvsroot/bnt diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Old/mk_abcd_hhmm.m b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Old/mk_abcd_hhmm.m new file mode 100644 index 00000000..330bc304 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Old/mk_abcd_hhmm.m @@ -0,0 +1,109 @@ +% Make the HHMM in Figure 1 of the NIPS'01 paper + +Qsize = [2 3 2]; +D = 3; + +% transprob{d}(i,k,j), transprob{1}(i,j) +% termprob{d}(k,j), termprob{1}(1,j) +% startprob{d}(k,j), startprob{1}(1,j) +% obsprob(k, o) for discrete outputs + +% LEVEL 1 +% 1 2 e +A{1} = [0 0 1; + 0 0 1]; +[transprob{1}, termprob{1}] = remove_hhmm_end_state(A{1}); +startprob{1} = [0.5 0.5]; +Q1args = {'startprob', startprob{1}, 'transprob', transprob{1}}; + +% LEVEL 2 +A{2} = zeros(Qsize(2), Qsize(1), Qsize(2)+1); + +% 1 2 3 e +A{2}(:,1,:) = [0 1 0 0 + 0 0 1 0 + 0 0 0 1]; + +% 1 2 3 e +A{2}(:,2,:) = [0 1 0 0 + 0 0 1 0 + 0 0 0 1]; + +[transprob{2}, termprob{2}] = remove_hhmm_end_state(A{2}); + +% always enter level 2 in state 1 +startprob{2} = [1 0 0 + 1 0 0]; + +Q2args = {'startprob', startprob{2}, 'transprob', transprob{2}}; +F2args = {'CPT', termprob{2}}; + + +% LEVEL 3 + +A{3} = zeros([Qsize(3) Qsize(1:2) Qsize(3)+1]); +endstate = Qsize(3)+1; +% Qt-1(3) Qt(1) Qt(2) Qt(3) +% 1 2 e +A{3}(1, 1, 1, endstate) = 1.0; +A{3}(:, 1, 2, :) = [0.0 1.0 0.0 + 0.5 0.0 0.5]; +A{3}(1, 1, 3, endstate) = 1.0; + +A{3}(1, 2, 1, endstate) = 1.0; +A{3}(:, 2, 2, :) = [0.0 1.0 0.0 + 0.5 0.0 0.5]; +A{3}(1, 2, 3, endstate) = 1.0; + +A{3} = reshape(A{3}, [Qsize(3) prod(Qsize(1:2)) Qsize(3)+1]); +[transprob{3}, termprob{3}] = remove_hhmm_end_state(A{3}); + +% define the vertical entry points to level 3 +startprob{3} = zeros(Qsize); +% Q1 Q2 Q3 +startprob{3}(1, 1, 1) = 1.0; +startprob{3}(1, 2, 1) = 1.0; +startprob{3}(1, 3, 1) = 1.0; + +startprob{3}(2, 1, 1) = 1.0; +startprob{3}(2, 2, 1) = 1.0; +startprob{3}(2, 3, 1) = 1.0; + +startprob{3} = reshape(startprob{3}, prod(Qsize(1:2)), Qsize(3)); + +chars = ['a', 'b', 'c', 'd', 'x', 'y']; +Osize = length(chars); + +obsprob = zeros([Qsize Osize]); +% 1 2 3 O +obsprob(1,1,1,find(chars == 'a')) = 1.0; + +obsprob(1,2,1,find(chars == 'x')) = 1.0; +obsprob(1,2,2,find(chars == 'y')) = 1.0; + +obsprob(1,3,1,find(chars == 'b')) = 1.0; + +obsprob(2,1,1,find(chars == 'c')) = 1.0; + +obsprob(2,2,1,find(chars == 'x')) = 1.0; +obsprob(2,2,2,find(chars == 'y')) = 1.0; + +obsprob(2,3,1,find(chars == 'd')) = 1.0; + +obsprob = reshape(obsprob, prod(Qsize), Osize); + +[intra, inter, Qnodes, Fnodes, Onode] = mk_hhmm_topo(D); + +hhmm.Qnodes = Qnodes; +hhmm.Fnodes = Fnodes; +hhmm.Onode = Onode; +hhmm.D = D; +hhmm.Qsize = Qsize; +hhmm.Osize = Osize; +hhmm.startprob = startprob; +hhmm.transprob = transprob; +hhmm.termprob = termprob; +hhmm.obsprob = obsprob; +hhmm.A = A; + + diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Old/mk_arrow_alpha_hhmm3.m b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Old/mk_arrow_alpha_hhmm3.m new file mode 100644 index 00000000..ba1aa8cb --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Old/mk_arrow_alpha_hhmm3.m @@ -0,0 +1,86 @@ +% Make the following HHMM +% +% LH RH +% / \ +% / \ +% LR -> UD -> RL -> DU RL -> UD -> LR -> DU +% \ +% \ +% Q1 -> Q2 +% +% where level 1 is fully interconnected (not shown) +% level 2 is left-right +% and each model at level 3 is a 2 state LR shared HMM + +Qsizes = [2 4 2]; +D = 3; + +% LEVEL 1 + +startprob1 = 'ergodic'; +transprob1 = 'ergodic'; + + +% LEVEL 2 + +startprob = zeros(2, 4); +% Q1 Q2 +startprob(1, 1) = 1; +startprob(2, 3) = 1; + +transprob = zeros(2, 4, 4); +transprob(1,:,:) = [0 1 0 0 + 0 0 1 0 + 0 0 0 1 + 0 0 0 1]; +transprob(2,:,:) = [0 0 0 1 + 1 0 0 0 + 0 1 0 0 + 0 0 0 1]; + +Q2args = {'startprob', startprob, 'transprob', transprob}; + +% always terminate in state 4 (default) +% F2args + +% LEVEL 3 + +% Defaults are fine: always start in state 1, left-right model, finish in state 2 + + +% OBS LEVEl + +chars = ['L', 'l', 'U', 'u', 'R', 'r', 'D', 'd']; +Osize = length(chars); + +obsprob = zeros([4 2 Osize]); +% Q2 Q3 O +obsprob(1, 1, find(chars == 'L')) = 1.0; +obsprob(1, 2, find(chars == 'l')) = 1.0; + +obsprob(2, 1, find(chars == 'U')) = 1.0; +obsprob(2, 2, find(chars == 'u')) = 1.0; + +obsprob(3, 1, find(chars == 'R')) = 1.0; +obsprob(3, 2, find(chars == 'r')) = 1.0; + +obsprob(4, 1, find(chars == 'D')) = 1.0; +obsprob(4, 2, find(chars == 'd')) = 1.0; + +Oargs = {'CPT', obsprob}; + + +bnet = mk_hhmm3('Qsizes', Qsizes, 'Osize', Osize', 'discrete_obs', 1, 'Oargs', Oargs, 'Q1args', Q1args, 'Q2args', Q2args); + +T = 20; +usecell = 0; +evidence = sample_dbn(bnet, T, usecell); +%chars(evidence(end,:)) + +Q1 = 1; Q2 = 2; Q3 = 3; F3 = 4; F2 = 5; obs = 6; +Qnodes = [Q1 Q2 Q3]; Fnodes = [F2 F3]; + +pretty_print_hhmm_parse(evidence, Qnodes, Fnodes, obs, chars); + +eclass = bnet.equiv_class; +S=struct(bnet.CPD{eclass(Q2,2)}) diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Old/mk_hhmm2.m b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Old/mk_hhmm2.m new file mode 100644 index 00000000..032015ba --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Old/mk_hhmm2.m @@ -0,0 +1,111 @@ +function bnet = mk_hhmm2(varargin) +% MK_HHMM2 Make a 2 level Hierarchical HMM +% bnet = mk_hhmm2(...) +% +% 2-layer hierarchical HMM (node numbers in parens) +% +% Q1(1) ---------> Q1(5) +% / | \ / | +% | | v / | +% | | F2(3) --- / | +% | | ^ \ | +% | | / \ | +% | v \ v +% | Q2(2)--------> Q2 (6) +% | | +% \ | +% v v +% O(4) +% +% +% Optional arguments [default] +% +% discrete_obs - 1 means O is tabular_CPD, 0 means O is gaussian_CPD [0] +% obsCPT - CPT(o,q1,q2) params for O ['rnd'] +% mu - mu(:,q1,q2) params for O [ [] ] +% Sigma - Sigma(:,q1,q2) params for O [ [] ] +% +% F2toQ1 - 1 if Q2 is an hhmm_CPD, 0 if F2 -> Q2 arc is absent, so level 2 never resets [1] +% Q1args - arguments to be passed to the constructors for Q1(t=2) [ {} ] +% Q2args - arguments to be passed to the constructors for Q2(t=2) [ {} ] +% +% F2 only turns on (wp 0.5) when Q2 enters its final state. +% Q1 (slice 1) is clamped to be uniform. +% Q2 (slice 1) is clamped to always start in state 1. + +[os nmodels nstates] = size(mu); + +ss = 4; +Q1 = 1; Q2 = 2; F2 = 3; obs = 4; +Qnodes = [Q1 Q2]; +names = {'Q1', 'Q2', 'F2', 'obs'}; +intra = zeros(ss); +intra(Q1, [Q2 F2 obs]) = 1; +intra(Q2, [F2 obs]) = 1; + +inter = zeros(ss); +inter(Q1,Q1) = 1; +inter(F2,Q1) = 1; +if F2toQ2 + inter(F2,Q2)=1; +end +inter(Q2,Q2) = 1; + +ns = zeros(1,ss); + +ns(Q1) = nmodels; +ns(Q2) = nstates; +ns(F2) = 2; +ns(obs) = os; + +dnodes = [Q1 Q2 F2]; +if discrete_obs + dnodes = [dnodes obs]; +end +onodes = [obs]; + +bnet = mk_dbn(intra, inter, ns, 'observed', onodes, 'discrete', dnodes, 'names', names); +eclass = bnet.equiv_class; + +% SLICE 1 + +% We clamp untied nodes in the first slice, since their params can't be estimated +% from just one sequence + +% uniform prior on initial model +CPT = normalise(ones(1,nmodels)); +bnet.CPD{eclass(Q1,1)} = tabular_CPD(bnet, Q1, 'CPT', CPT, 'adjustable', 0); + +% each model always starts in state 1 +CPT = zeros(ns(Q1), ns(Q2)); +CPT(:, 1) = 1.0; +bnet.CPD{eclass(Q2,1)} = tabular_CPD(bnet, Q2, 'CPT', CPT, 'adjustable', 0); + +% Termination probability +CPT = zeros(ns(Q1), ns(Q2), 2); +if 1 + % Each model can only terminate in its final state. + % 0 params will remain 0 during EM, thus enforcing this constraint. + CPT(:, :, 1) = 1.0; % all states turn F off ... + p = 0.5; + CPT(:, ns(Q2), 2) = p; % except the last one + CPT(:, ns(Q2), 1) = 1-p; +end +bnet.CPD{eclass(F2,1)} = tabular_CPD(bnet, F2, 'CPT', CPT); + +if discrete_obs + bnet.CPD{eclass(obs,1)} = tabular_CPD(bnet, obs, obs_args{:}); +else + bnet.CPD{eclass(obs,1)} = gaussian_CPD(bnet, obs, obs_args{:}); +end + +% SLICE 2 + + +bnet.CPD{eclass(Q1,2)} = hhmm_CPD(bnet, Q1+ss, Qnodes, 1, D, 'args', Q1args); + +if F2toQ2 + bnet.CPD{eclass(Q2,2)} = hhmmQD_CPD(bnet, Q2+ss, Qnodes, 2, D, Q2args{:}); +else + bnet.CPD{eclass(Q2,2)} = tabular_CPD(bnet, Q2+ss, Q2args{:}); +end diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Old/mk_hhmm3.m b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Old/mk_hhmm3.m new file mode 100644 index 00000000..5b2cd6aa --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Old/mk_hhmm3.m @@ -0,0 +1,181 @@ +function bnet = mk_hhmm3(varargin) +% MK_HHMM3 Make a 3 level Hierarchical HMM +% bnet = mk_hhmm3(...) +% +% 3-layer hierarchical HMM where level 1 only connects to level 2, not 3 or obs. +% This enforces sub-models (which differ only in their Q1 index) to be shared. +% Also, we enforce the fact that each model always starts in its initial state +% and only finishes in its final state. However, the prob. of finishing (as opposed to +% self-transitioning to the final state) can be learned. +% The fact that we always finish from the same state means we do not need to condition +% F(i) on Q(i-1), since finishing prob is indep of calling context. +% +% The DBN is the same as Fig 10 in my tech report. +% +% Q1 ----------> Q1 +% | / | +% | / | +% | F2 ------- | +% | ^ \ | +% | /| \ | +% v | v v +% Q2-| --------> Q2 +% /| | ^ +% / | | /| +% | | F3 ---------/ | +% | | ^ \ | +% | v / v +% | Q3 -----------> Q3 +% | | +% \ | +% v v +% O +% +% +% Optional arguments in name/value format [default] +% +% Qsizes - sizes at each level [ none ] +% Osize - size of O node [ none ] +% discrete_obs - 1 means O is tabular_CPD, 0 means O is gaussian_CPD [0] +% Oargs - cell array of args to pass to the O CPD [ {} ] +% transprob1 - transprob1(i,j) = P(Q1(t)=j|Q1(t-1)=i) ['ergodic'] +% startprob1 - startprob1(j) = P(Q1(t)=j) ['leftstart'] +% transprob2 - transprob2(i,k,j) = P(Q2(t)=j|Q2(t-1)=i,Q1(t)=k) ['leftright'] +% startprob2 - startprob2(k,j) = P(Q2(t)=j|Q1(t)=k) ['leftstart'] +% termprob2 - termprob2(j,f) = P(F2(t)=f|Q2(t)=j) ['rightstop'] +% transprob3 - transprob3(i,k,j) = P(Q3(t)=j|Q3(t-1)=i,Q2(t)=k) ['leftright'] +% startprob3 - startprob3(k,j) = P(Q3(t)=j|Q2(t)=k) ['leftstart'] +% termprob3 - termprob3(j,f) = P(F3(t)=f|Q3(t)=j) ['rightstop'] +% +% leftstart means the model always starts in state 1. +% rightstop means the model always finished in its last state (Qsize(d)). +% +% Q1:Q3 in slice 1 are of type tabular_CPD +% Q1:Q3 in slice 2 are of type hhmmQ_CPD. +% F2 is of type hhmmF_CPD, F3 is of type tabular_CPD. + +ss = 6; D = 3; +Q1 = 1; Q2 = 2; Q3 = 3; F3 = 4; F2 = 5; obs = 6; +Qnodes = [Q1 Q2 Q3]; Fnodes = [F2 F3]; +names = {'Q1', 'Q2', 'Q3', 'F3', 'F2', 'obs'}; + +intra = zeros(ss); +intra(Q1, Q2) = 1; +intra(Q2, [F2 Q3 obs]) = 1; +intra(Q3, [F3 obs]) = 1; +intra(F3, F2) = 1; + +inter = zeros(ss); +inter(Q1,Q1) = 1; +inter(Q2,Q2) = 1; +inter(Q3,Q3) = 1; +inter(F2,[Q1 Q2]) = 1; +inter(F3,[Q2 Q3]) = 1; + + +% get sizes of nodes +args = varargin; +nargs = length(args); +Qsizes = []; +Osize = 0; +for i=1:2:nargs + switch args{i}, + case 'Qsizes', Qsizes = args{i+1}; + case 'Osize', Osize = args{i+1}; + end +end +if isempty(Qsizes), error('must specify Qsizes'); end +if Osize==0, error('must specify Osize'); end + +% set default params +discrete_obs = 0; +Oargs = {}; +startprob1 = 'ergodic'; +startprob2 = 'leftstart'; +startprob3 = 'leftstart'; +transprob1 = 'ergodic'; +transprob2 = 'leftright'; +transprob3 = 'leftright'; +termprob2 = 'rightstop'; +termprob3 = 'rightstop'; + + +for i=1:2:nargs + switch args{i}, + case 'discrete_obs', discrete_obs = args{i+1}; + case 'Oargs', Oargs = args{i+1}; + case 'Q1args', Q1args = args{i+1}; + case 'Q2args', Q2args = args{i+1}; + case 'Q3args', Q3args = args{i+1}; + case 'F2args', F2args = args{i+1}; + case 'F3args', F3args = args{i+1}; + end +end + + +ns = zeros(1,ss); +ns(Qnodes) = Qsizes; +ns(obs) = Osize; +ns(Fnodes) = 2; + +dnodes = [Qnodes Fnodes]; +if discrete_obs + dnodes = [dnodes obs]; +end +onodes = [obs]; + +bnet = mk_dbn(intra, inter, ns, 'observed', onodes, 'discrete', dnodes, 'names', names); +eclass = bnet.equiv_class; + +if strcmp(startprob1, 'ergodic') + startprob1 = normalise(ones(1,ns(Q1))); +end +if strcmp(startprob2, 'leftstart') + startprob2 = zeros(ns(Q1), ns(Q2)); + starpbrob2(:, 1) = 1.0; +end +if strcmp(startprob3, 'leftstart') + startprob3 = zeros(ns(Q2), ns(Q3)); + starpbrob3(:, 1) = 1.0; +end + +if strcmp(termprob2, 'rightstop') + p = 0.9; + termprob2 = zeros(Qsize(2),2); + termprob2(:, 2) = p; + termprob2(:, 1) = 1-p; + termprob2(1:(Qsize(2)-1), 1) = 1; +end +if strcmp(termprob3, 'rightstop') + p = 0.9; + termprob3 = zeros(Qsize(3),2); + termprob3(:, 2) = p; + termprob3(:, 1) = 1-p; + termprob3(1:(Qsize(3)-1), 1) = 1; +end + + +% SLICE 1 + +% We clamp untied nodes in the first slice, since their params can't be estimated +% from just one sequence + +bnet.CPD{eclass(Q1,1)} = tabular_CPD(bnet, Q1, 'CPT', startprob1, 'adjustable', 0); +bnet.CPD{eclass(Q2,1)} = tabular_CPD(bnet, Q2, 'CPT', startprob2, 'adjustable', 0); +bnet.CPD{eclass(Q3,1)} = tabular_CPD(bnet, Q3, 'CPT', startprob3, 'adjustable', 0); + +bnet.CPD{eclass(F2,1)} = hhmmF_CPD(bnet, F2, Qnodes, 2, D, 'termprob', termprob2); +bnet.CPD{eclass(F3,1)} = tabular_CPD(bnet, F3, 'CPT', termprob3); + +if discrete_obs + bnet.CPD{eclass(obs,1)} = tabular_CPD(bnet, obs, Oargs{:}); +else + bnet.CPD{eclass(obs,1)} = gaussian_CPD(bnet, obs, Oargs{:}); +end + +% SLICE 2 + +bnet.CPD{eclass(Q1,2)} = hhmmQ_CPD(bnet, Q1+ss, Qnodes, 1, D, 'transprob', transprob1, 'startprob', startprob1); +bnet.CPD{eclass(Q2,2)} = hhmmQ_CPD(bnet, Q2+ss, Qnodes, 2, D, 'transprob', transprob2, 'startprob', startprob2); +bnet.CPD{eclass(Q3,2)} = hhmmQ_CPD(bnet, Q3+ss, Qnodes, 3, D, 'transprob', transprob3, 'startprob', startprob3); + diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Old/mk_hhmm3_args.m b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Old/mk_hhmm3_args.m new file mode 100644 index 00000000..bc3ec886 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Old/mk_hhmm3_args.m @@ -0,0 +1,165 @@ +function bnet = mk_hhmm3(varargin) +% MK_HHMM3 Make a 3 level Hierarchical HMM +% bnet = mk_hhmm3(...) +% +% 3-layer hierarchical HMM where level 1 only connects to level 2, not 3 or obs. +% This enforces sub-models (which differ only in their Q1 index) to be shared. +% Also, we enforce the fact that each model always starts in its initial state +% and only finishes in its final state. However, the prob. of finishing (as opposed to +% self-transitioning to the final state) can be learned. +% The fact that we always finish from the same state means we do not need to condition +% F(i) on Q(i-1), since finishing prob is indep of calling context. +% +% The DBN is the same as Fig 10 in my tech report. +% +% Q1 ----------> Q1 +% | / | +% | / | +% | F2 ------- | +% | ^ \ | +% | /| \ | +% v | v v +% Q2-| --------> Q2 +% /| | ^ +% / | | /| +% | | F3 ---------/ | +% | | ^ \ | +% | v / v +% | Q3 -----------> Q3 +% | | +% \ | +% v v +% O +% +% Q1 (slice 1) is clamped to be uniform. +% Q2 (slice 1) is clamped to always start in state 1. +% Q3 (slice 1) is clamped to always start in state 1. +% F3 by default will only finish if Q3 is in its last state (F3 is a tabular_CPD) +% F2 by default gets the default hhmmF_CPD params. +% Q1:Q3 (slice 2) by default gets the default hhmmQ_CPD params. +% O by default gets the default tabular/Gaussian params. +% +% Optional arguments in name/value format [default] +% +% Qsizes - sizes at each level [ none ] +% Osize - size of O node [ none ] +% discrete_obs - 1 means O is tabular_CPD, 0 means O is gaussian_CPD [0] +% Oargs - cell array of args to pass to the O CPD [ {} ] +% Q1args - args to be passed to constructor for Q1 (slice 2) [ {} ] +% Q2args - args to be passed to constructor for Q2 (slice 2) [ {} ] +% Q3args - args to be passed to constructor for Q3 (slice 2) [ {} ] +% F2args - args to be passed to constructor for F2 [ {} ] +% F3args - args to be passed to constructor for F3 [ {'CPT', finish in last Q3 state} ] +% + +ss = 6; D = 3; +Q1 = 1; Q2 = 2; Q3 = 3; F3 = 4; F2 = 5; obs = 6; +Qnodes = [Q1 Q2 Q3]; Fnodes = [F2 F3]; +names = {'Q1', 'Q2', 'Q3', 'F3', 'F2', 'obs'}; + +intra = zeros(ss); +intra(Q1, Q2) = 1; +intra(Q2, [F2 Q3 obs]) = 1; +intra(Q3, [F3 obs]) = 1; +intra(F3, F2) = 1; + +inter = zeros(ss); +inter(Q1,Q1) = 1; +inter(Q2,Q2) = 1; +inter(Q3,Q3) = 1; +inter(F2,[Q1 Q2]) = 1; +inter(F3,[Q2 Q3]) = 1; + + +% get sizes of nodes +args = varargin; +nargs = length(args); +Qsizes = []; +Osize = 0; +for i=1:2:nargs + switch args{i}, + case 'Qsizes', Qsizes = args{i+1}; + case 'Osize', Osize = args{i+1}; + end +end +if isempty(Qsizes), error('must specify Qsizes'); end +if Osize==0, error('must specify Osize'); end + +% set default params +discrete_obs = 0; +Oargs = {}; +Q1args = {}; +Q2args = {}; +Q3args = {}; +F2args = {}; + +% P(Q3, F3) +CPT = zeros(Qsizes(3), 2); +% Each model can only terminate in its final state. +% 0 params will remain 0 during EM, thus enforcing this constraint. +CPT(:, 1) = 1.0; % all states turn F off ... +p = 0.5; +CPT(Qsizes(3), 2) = p; % except the last one +CPT(Qsizes(3), 1) = 1-p; +F3args = {'CPT', CPT}; + +for i=1:2:nargs + switch args{i}, + case 'discrete_obs', discrete_obs = args{i+1}; + case 'Oargs', Oargs = args{i+1}; + case 'Q1args', Q1args = args{i+1}; + case 'Q2args', Q2args = args{i+1}; + case 'Q3args', Q3args = args{i+1}; + case 'F2args', F2args = args{i+1}; + case 'F3args', F3args = args{i+1}; + end +end + +ns = zeros(1,ss); +ns(Qnodes) = Qsizes; +ns(obs) = Osize; +ns(Fnodes) = 2; + +dnodes = [Qnodes Fnodes]; +if discrete_obs + dnodes = [dnodes obs]; +end +onodes = [obs]; + +bnet = mk_dbn(intra, inter, ns, 'observed', onodes, 'discrete', dnodes, 'names', names); +eclass = bnet.equiv_class; + +% SLICE 1 + +% We clamp untied nodes in the first slice, since their params can't be estimated +% from just one sequence + +% uniform prior on initial model +CPT = normalise(ones(1,ns(Q1))); +bnet.CPD{eclass(Q1,1)} = tabular_CPD(bnet, Q1, 'CPT', CPT, 'adjustable', 0); + +% each model always starts in state 1 +CPT = zeros(ns(Q1), ns(Q2)); +CPT(:, 1) = 1.0; +bnet.CPD{eclass(Q2,1)} = tabular_CPD(bnet, Q2, 'CPT', CPT, 'adjustable', 0); + +% each model always starts in state 1 +CPT = zeros(ns(Q2), ns(Q3)); +CPT(:, 1) = 1.0; +bnet.CPD{eclass(Q3,1)} = tabular_CPD(bnet, Q3, 'CPT', CPT, 'adjustable', 0); + +bnet.CPD{eclass(F2,1)} = hhmmF_CPD(bnet, F2, Qnodes, 2, D, F2args{:}); + +bnet.CPD{eclass(F3,1)} = tabular_CPD(bnet, F3, F3args{:}); + +if discrete_obs + bnet.CPD{eclass(obs,1)} = tabular_CPD(bnet, obs, Oargs{:}); +else + bnet.CPD{eclass(obs,1)} = gaussian_CPD(bnet, obs, Oargs{:}); +end + +% SLICE 2 + +bnet.CPD{eclass(Q1,2)} = hhmmQ_CPD(bnet, Q1+ss, Qnodes, 1, D, Q1args{:}); +bnet.CPD{eclass(Q2,2)} = hhmmQ_CPD(bnet, Q2+ss, Qnodes, 2, D, Q2args{:}); +bnet.CPD{eclass(Q3,2)} = hhmmQ_CPD(bnet, Q3+ss, Qnodes, 3, D, Q3args{:}); diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Old/motif_hhmm.m b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Old/motif_hhmm.m new file mode 100644 index 00000000..10144b58 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Old/motif_hhmm.m @@ -0,0 +1,95 @@ +% Make the following HHMM +% +% S1 <----------------------> S2 +% | | +% | | +% M1 -> M2 -> M3 -> end B1 -> end +% +% where Mi represents the i'th letter in the motif +% and B is the background state. +% Si chooses between running the motif or the background. +% The Si and B states have self loops (not shown). + +if 0 +seed = 0; +rand('state', seed); +randn('state', seed); +end + +chars = ['a', 'c', 'g', 't']; +Osize = length(chars); + +motif_pattern = 'acca'; +motif_length = length(motif_pattern); +Qsize = [2 motif_length]; +Qnodes = 1:2; +D = 2; +transprob = cell(1,D); +termprob = cell(1,D); +startprob = cell(1,D); + +% startprob{d}(k,j), startprob{1}(1,j) +% transprob{d}(i,k,j), transprob{1}(i,j) +% termprob{d}(k,j) + + +% LEVEL 1 + +startprob{1} = zeros(1, 2); +startprob{1} = [1 0]; % always start in the background model + +% When in the background state, we stay there with high prob +% When in the motif state, we immediately return to the background state. +transprob{1} = [0.8 0.2; + 1.0 0.0]; + + +% LEVEL 2 +startprob{2} = 'leftstart'; % both submodels start in substate 1 +transprob{2} = zeros(motif_length, 2, motif_length); +termprob{2} = zeros(2, motif_length); + +% In the background model, we only use state 1. +transprob{2}(1,1,1) = 1; % self loop +termprob{2}(1,1) = 0.2; % prob transition to end state + +% Motif model +transprob{2}(:,2,:) = mk_leftright_transmat(motif_length, 0); +termprob{2}(2,end) = 1.0; % last state immediately terminates + + +% OBS LEVEl + +obsprob = zeros([Qsize Osize]); +if 0 + % uniform background model + obsprob(1,1,:) = normalise(ones(Osize,1)); +else + % deterministic background model (easy to see!) + m = find(chars=='t'); + obsprob(1,1,m) = 1.0; +end +if 1 + % initialise with true motif (cheating) + for i=1:motif_length + m = find(chars == motif_pattern(i)); + obsprob(2,i,m) = 1.0; + end +end + +Oargs = {'CPT', obsprob}; + +[bnet, Qnodes, Fnodes, Onode] = mk_hhmm('Qsizes', Qsize, 'Osize', Osize, 'discrete_obs', 1, ... + 'Oargs', Oargs, 'Ops', Qnodes(1:2), ... + 'startprob', startprob, 'transprob', transprob, 'termprob', termprob); + + +Tmax = 20; +usecell = 0; + +for seqi=1:5 + evidence = sample_dbn(bnet, Tmax, usecell); + chars(evidence(end,:)) + %T = size(evidence, 2) + %pretty_print_hhmm_parse(evidence, Qnodes, Fnodes, Onode, chars); +end diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Old/remove_hhmm_end_state.m b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Old/remove_hhmm_end_state.m new file mode 100644 index 00000000..2bf10d72 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Old/remove_hhmm_end_state.m @@ -0,0 +1,37 @@ +function [transprob, termprob] = remove_hhmm_end_state(A) +% REMOVE_END_STATE Infer transition and termination probabilities from automaton with an end state +% [transprob, termprob] = remove_end_state(A) +% A(i,k,j) = Pr( i->j | Qps=k), where i in 1:Q, j in 1:(Q+1), and Q+1 is the end state + +if ndims(A)==2 % top level + Q = size(A,1); + transprob = A(:,1:Q); + termprob = A(:,Q+1)'; + + % rescale + for i=1:Q + for j=1:Q + denom = (1-termprob(i)); + denom = denom + (denom==0)*eps; + transprob(i,j) = transprob(i,j) / denom; + end + end +else + Q = size(A,1); + Qk = size(A,2); + transprob = A(:, :, 1:Q); + termprob = A(:,:,Q+1)'; + + % rescale + for k=1:Qk + for i=1:Q + for j=1:Q + denom = (1-termprob(k,i)); + denom = denom + (denom==0)*eps; + transprob(i,k,j) = transprob(i,k,j) / denom; + end + end + end + +end + diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/CVS/Entries b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/CVS/Entries new file mode 100644 index 00000000..fdee19ae --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/CVS/Entries @@ -0,0 +1,14 @@ +/get_square_data.m/1.1.1.1/Wed May 29 15:59:54 2002// +/hhmm_inference.m/1.1.1.1/Wed May 29 15:59:54 2002// +/is_F2_true_D3.m/1.1.1.1/Wed May 29 15:59:54 2002// +/learn_square_hhmm_cts.m/1.1.1.1/Thu Jun 20 00:19:22 2002// +/learn_square_hhmm_discrete.m/1.1.1.1/Wed May 29 15:59:54 2002// +/mk_square_hhmm.m/1.1.1.1/Wed May 29 15:59:54 2002// +/plot_square_hhmm.m/1.1.1.1/Wed May 29 15:59:54 2002// +/sample_square_hhmm_cts.m/1.1.1.1/Wed May 29 15:59:54 2002// +/sample_square_hhmm_discrete.m/1.1.1.1/Wed May 29 15:59:54 2002// +/square4.mat/1.1.1.1/Wed May 29 15:59:54 2002// +/square4_cases.mat/1.1.1.1/Wed May 29 15:59:54 2002// +/test_square_fig.m/1.1.1.1/Wed May 29 15:59:54 2002// +/test_square_fig.mat/1.1.1.1/Wed May 29 15:59:54 2002// +D/Old//// diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/CVS/Repository b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/CVS/Repository new file mode 100644 index 00000000..e926a0d5 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/CVS/Repository @@ -0,0 +1 @@ +FullBNT/BNT/examples/dynamic/HHMM/Square diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/CVS/Root b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/CVS/Root new file mode 100644 index 00000000..f3bd14a6 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/CVS/Root @@ -0,0 +1 @@ +:ext:nsaunier@bnt.cvs.sourceforge.net:/cvsroot/bnt diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/Old/CVS/Entries b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/Old/CVS/Entries new file mode 100644 index 00000000..6d415d0d --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/Old/CVS/Entries @@ -0,0 +1,5 @@ +/learn_square_hhmm.m/1.1.1.1/Wed May 29 15:59:54 2002// +/mk_square_hhmm.m/1.1.1.1/Wed May 29 15:59:54 2002// +/plot_square_hhmm.m/1.1.1.1/Wed May 29 15:59:54 2002// +/sample_square_hhmm.m/1.1.1.1/Wed May 29 15:59:54 2002// +D diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/Old/CVS/Repository b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/Old/CVS/Repository new file mode 100644 index 00000000..47df1a8c --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/Old/CVS/Repository @@ -0,0 +1 @@ +FullBNT/BNT/examples/dynamic/HHMM/Square/Old diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/Old/CVS/Root b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/Old/CVS/Root new file mode 100644 index 00000000..f3bd14a6 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/Old/CVS/Root @@ -0,0 +1 @@ +:ext:nsaunier@bnt.cvs.sourceforge.net:/cvsroot/bnt diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/Old/learn_square_hhmm.m b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/Old/learn_square_hhmm.m new file mode 100644 index 00000000..695ae047 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/Old/learn_square_hhmm.m @@ -0,0 +1,294 @@ +% Learn a 3 level HHMM similar to mk_square_hhmm + +% Because startprob should be shared for t=1:T, +% but in the DBN is shared for t=2:T, we train using a single long sequence. + +discrete_obs = 0; +supervised = 1; +obs_finalF2 = 0; +% It is not possible to observe F2 if we learn +% because the update_ess method for hhmmF_CPD and hhmmQ_CPD assume +% the F nodes are always hidden (for speed). +% However, for generating, we might want to set the final F2=true +% to force all subroutines to finish. + +ss = 6; +Q1 = 1; Q2 = 2; Q3 = 3; F3 = 4; F2 = 5; Onode = 6; +Qnodes = [Q1 Q2 Q3]; Fnodes = [F2 F3]; + +seed = 1; +rand('state', seed); +randn('state', seed); + +if discrete_obs + Qsizes = [2 4 2]; +else + Qsizes = [2 4 1]; +end + +D = 3; +Qnodes = 1:D; +startprob = cell(1,D); +transprob = cell(1,D); +termprob = cell(1,D); + +startprob{1} = 'unif'; +transprob{1} = 'unif'; + +% In the unsupervised case, it is essential that we break symmetry +% in the initial param estimates. +%startprob{2} = 'unif'; +%transprob{2} = 'unif'; +%termprob{2} = 'unif'; +startprob{2} = 'rnd'; +transprob{2} = 'rnd'; +termprob{2} = 'rnd'; + +leftright = 0; +if leftright + % Initialise base-level models as left-right. + % If we initialise with delta functions, + % they will remain delat funcitons after learning + startprob{3} = 'leftstart'; + transprob{3} = 'leftright'; + termprob{3} = 'rightstop'; +else + % If we want to be able to run a base-level model backwards... + startprob{3} = 'rnd'; + transprob{3} = 'rnd'; + termprob{3} = 'rnd'; +end + +if discrete_obs + % Initialise observations of lowest level primitives in a way which we can interpret + chars = ['L', 'l', 'U', 'u', 'R', 'r', 'D', 'd']; + L=find(chars=='L'); l=find(chars=='l'); + U=find(chars=='U'); u=find(chars=='u'); + R=find(chars=='R'); r=find(chars=='r'); + D=find(chars=='D'); d=find(chars=='d'); + Osize = length(chars); + + p = 0.9; + obsprob = (1-p)*ones([4 2 Osize]); + % Q2 Q3 O + obsprob(1, 1, L) = p; + obsprob(1, 2, l) = p; + obsprob(2, 1, U) = p; + obsprob(2, 2, u) = p; + obsprob(3, 1, R) = p; + obsprob(3, 2, r) = p; + obsprob(4, 1, D) = p; + obsprob(4, 2, d) = p; + obsprob = mk_stochastic(obsprob); + Oargs = {'CPT', obsprob}; + +else + % Initialise means of lowest level primitives in a way which we can interpret + % These means are little vectors in the east, south, west, north directions. + % (left-right=east, up-down=south, right-left=west, down-up=north) + Osize = 2; + mu = zeros(2, Qsizes(2), Qsizes(3)); + noise = 0; + scale = 3; + for q3=1:Qsizes(3) + mu(:, 1, q3) = scale*[1;0] + noise*rand(2,1); + end + for q3=1:Qsizes(3) + mu(:, 2, q3) = scale*[0;-1] + noise*rand(2,1); + end + for q3=1:Qsizes(3) + mu(:, 3, q3) = scale*[-1;0] + noise*rand(2,1); + end + for q3=1:Qsizes(3) + mu(:, 4, q3) = scale*[0;1] + noise*rand(2,1); + end + Sigma = repmat(reshape(scale*eye(2), [2 2 1 1 ]), [1 1 Qsizes(2) Qsizes(3)]); + Oargs = {'mean', mu, 'cov', Sigma, 'cov_type', 'diag'}; +end + +bnet = mk_hhmm('Qsizes', Qsizes, 'Osize', Osize', 'discrete_obs', discrete_obs,... + 'Oargs', Oargs, 'Ops', Qnodes(2:3), ... + 'startprob', startprob, 'transprob', transprob, 'termprob', termprob); + +if supervised + bnet.observed = [Q1 Q2 Onode]; +else + bnet.observed = [Onode]; +end + +if obs_finalF2 + engine = jtree_dbn_inf_engine(bnet); + % can't use ndx version because sometimes F2 is hidden, sometimes observed + error('can''t observe F when learning') +else + if supervised + engine = jtree_ndx_dbn_inf_engine(bnet); + else + engine = jtree_hmm_inf_engine(bnet); + end +end + +if discrete_obs + % generate some synthetic data (easier to debug) + cases = {}; + + T = 8; + ev = cell(ss, T); + ev(Onode,:) = num2cell([L l U u R r D d]); + if supervised + ev(Q1,:) = num2cell(1*ones(1,T)); + ev(Q2,:) = num2cell( [1 1 2 2 3 3 4 4]); + end + cases{1} = ev; + cases{3} = ev; + + T = 8; + ev = cell(ss, T); + if leftright % base model is left-right + ev(Onode,:) = num2cell([R r U u L l D d]); + else + ev(Onode,:) = num2cell([r R u U l L d D]); + end + if supervised + ev(Q1,:) = num2cell(2*ones(1,T)); + ev(Q2,:) = num2cell( [3 3 2 2 1 1 4 4]); + end + + cases{2} = ev; + cases{4} = ev; + + if obs_finalF2 + for i=1:length(cases) + T = size(cases{i},2); + cases{i}(F2,T)={2}; % force F2 to be finished at end of seq + end + end + + if 0 + ev = cases{4}; + engine2 = enter_evidence(engine2, ev); + T = size(ev,2); + for t=1:T + m=marginal_family(engine2, F2, t); + fprintf('t=%d\n', t); + reshape(m.T, [2 2]) + end + end + + % [bnet2, LL] = learn_params_dbn_em(engine, cases, 'max_iter', 10); + long_seq = cat(2, cases{:}); + [bnet2, LL, engine2] = learn_params_dbn_em(engine, {long_seq}, 'max_iter', 200); + + % figure out which subsequence each model is responsible for + mpe = calc_mpe_dbn(engine2, long_seq); + pretty_print_hhmm_parse(mpe, Qnodes, Fnodes, Onode, chars); + +else + load 'square4_cases' % cases{seq}{i,t} for i=1:ss + %plot_square_hhmm(cases{1}) + %long_seq = cat(2, cases{:}); + train_cases = cases(1:2); + long_seq = cat(2, train_cases{:}); + if ~supervised + T = size(long_seq,2); + for t=1:T + long_seq{Q1,t} = []; + long_seq{Q2,t} = []; + end + end + [bnet2, LL, engine2] = learn_params_dbn_em(engine, {long_seq}, 'max_iter', 100); + + CPDO=struct(bnet2.CPD{eclass(Onode,1)}); + mu = CPDO.mean; + Sigma = CPDO.cov; + CPDO_full = CPDO; + + % force diagonal covs after training + for k=1:size(Sigma,3) + Sigma(:,:,k) = diag(diag(Sigma(:,:,k))); + end + bnet2.CPD{6} = set_fields(bnet.CPD{6}, 'cov', Sigma); + + if 0 + % visualize each model by concatenating means for each model for nsteps in a row + nsteps = 5; + ev = cell(ss, nsteps*prod(Qsizes(2:3))); + t = 1; + for q2=1:Qsizes(2) + for q3=1:Qsizes(3) + for i=1:nsteps + ev{Onode,t} = mu(:,q2,q3); + ev{Q2,t} = q2; + t = t + 1; + end + end + end + plot_square_hhmm(ev) + end + + % bnet3 is the same as the learned model, except we will use it in testing mode + if supervised + bnet3 = bnet2; + bnet3.observed = [Onode]; + engine3 = hmm_inf_engine(bnet3); + %engine3 = jtree_ndx_dbn_inf_engine(bnet3); + else + bnet3 = bnet2; + engine3 = engine2; + end + + if 0 + % segment whole sequence + mpe = calc_mpe_dbn(engine3, long_seq); + pretty_print_hhmm_parse(mpe, Qnodes, Fnodes, Onode, []); + end + + % segment each sequence + test_cases = cases(3:4); + for i=1:2 + ev = test_cases{i}; + T = size(ev, 2); + for t=1:T + ev{Q1,t} = []; + ev{Q2,t} = []; + end + mpe = calc_mpe_dbn(engine3, ev); + subplot(1,2,i) + plot_square_hhmm(mpe) + %pretty_print_hhmm_parse(mpe, Qnodes, Fnodes, Onode, []); + q1s = cell2num(mpe(Q1,:)); + h = hist(q1s, 1:Qsizes(1)); + map_q1 = argmax(h); + str = sprintf('test seq %d is of type %d\n', i, map_q1); + title(str) + end + +end + +if 0 +% Estimate gotten by couting transitions in the labelled data +% Note that a self transition shouldnt count if F2=off. +Q2ev = cell2num(ev(Q2,:)); +Q2a = Q2ev(1:end-1); +Q2b = Q2ev(2:end); +counts = compute_counts([Q2a; Q2b], [4 4]); +end + +eclass = bnet2.equiv_class; +CPDQ1=struct(bnet2.CPD{eclass(Q1,2)}); +CPDQ2=struct(bnet2.CPD{eclass(Q2,2)}); +CPDQ3=struct(bnet2.CPD{eclass(Q3,2)}); +CPDF2=struct(bnet2.CPD{eclass(F2,1)}); +CPDF3=struct(bnet2.CPD{eclass(F3,1)}); + + +A=add_hhmm_end_state(CPDQ2.transprob, CPDF2.termprob(:,:,2)); +squeeze(A(:,1,:)) +squeeze(A(:,2,:)) +CPDQ2.startprob + +if 0 +S=struct(CPDF2.sub_CPD_term); +S.nsamples +reshape(S.counts, [2 4 2]) +end diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/Old/mk_square_hhmm.m b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/Old/mk_square_hhmm.m new file mode 100644 index 00000000..608b6784 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/Old/mk_square_hhmm.m @@ -0,0 +1,183 @@ +function bnet = mk_square_hhmm(discrete_obs, true_params, topright) + +% Make a 3 level HHMM described by the following grammar +% +% Square -> CLK | CCK % clockwise or counterclockwise +% CLK -> LR UD RL DU start on top left (1 2 3 4) +% CCK -> RL UD LR DU if start at top right (3 2 1 4) +% CCK -> UD LR DU RL if start at top left (2 1 4 3) +% +% LR = left-right, UD = up-down, RL = right-left, DU = down-up +% LR, UD, RL, DU are sub HMMs. +% +% For discrete observations, the subHMMs are 2-state left-right. +% LR emits L then l, etc. +% +% For cts observations, the subHMMs are 1 state. +% LR emits a vector in the -> direction, with a little noise. +% Since there is no constraint that we remain in the LR state as long as the RL state, +% the sides of the square might have different lengths, +% so the result is not really a square! +% +% If true_params = 0, we use random parameters at the top 2 levels +% (ready for learning). At the bottom level, we use noisy versions +% of the "true" observations. +% +% If topright=1, counter-clockwise starts at top right, not top left +% This example was inspired by Ivanov and Bobick. + +if nargin < 3, topright = 1; end + +if 1 % discrete_obs + Qsizes = [2 4 2]; +else + Qsizes = [2 4 1]; +end + +D = 3; +Qnodes = 1:D; +startprob = cell(1,D); +transprob = cell(1,D); +termprob = cell(1,D); + +% LEVEL 1 + +startprob{1} = 'unif'; +transprob{1} = 'unif'; + +% LEVEL 2 + +if true_params + startprob{2} = zeros(2, 4); + startprob{2}(1, :) = [1 0 0 0]; + if topright + startprob{2}(2, :) = [0 0 1 0]; + else + startprob{2}(2, :) = [0 1 0 0]; + end + + transprob{2} = zeros(4, 2, 4); + + transprob{2}(:,1,:) = [0 1 0 0 + 0 0 1 0 + 0 0 0 1 + 0 0 0 1]; % 4->e + if topright + transprob{2}(:,2,:) = [0 0 0 1 + 1 0 0 0 + 0 1 0 0 + 0 0 0 1]; % 4->e + else + transprob{2}(:,2,:) = [0 0 0 1 + 1 0 0 0 + 0 0 1 0 % 3->e + 0 0 1 0]; + end + + %termprob{2} = 'rightstop'; + termprob{2} = zeros(2,4,2); + pfin = 0.8; + termprob{2}(1,:,2) = [0 0 0 pfin]; % finish in state 4 (DU) + termprob{2}(1,:,1) = 1 - [0 0 0 pfin]; + if topright + termprob{2}(2,:,2) = [0 0 0 pfin]; + termprob{2}(2,:,1) = 1 - [0 0 0 pfin]; + else + termprob{2}(2,:,2) = [0 0 pfin 0]; % finish in state 3 (RL) + termprob{2}(2,:,1) = 1 - [0 0 pfin 0]; + end +else + % In the unsupervised case, it is essential that we break symmetry + % in the initial param estimates. + %startprob{2} = 'unif'; + %transprob{2} = 'unif'; + %termprob{2} = 'unif'; + startprob{2} = 'rnd'; + transprob{2} = 'rnd'; + termprob{2} = 'rnd'; +end + +% LEVEL 3 + +if 1 | true_params + startprob{3} = 'leftstart'; + transprob{3} = 'leftright'; + termprob{3} = 'rightstop'; +else + % If we want to be able to run a base-level model backwards... + startprob{3} = 'rnd'; + transprob{3} = 'rnd'; + termprob{3} = 'rnd'; +end + + +% OBS LEVEl + +if discrete_obs + % Initialise observations of lowest level primitives in a way which we can interpret + chars = ['L', 'l', 'U', 'u', 'R', 'r', 'D', 'd']; + L=find(chars=='L'); l=find(chars=='l'); + U=find(chars=='U'); u=find(chars=='u'); + R=find(chars=='R'); r=find(chars=='r'); + D=find(chars=='D'); d=find(chars=='d'); + Osize = length(chars); + + if true_params + p = 1; % makes each state fully observed + else + p = 0.9; + end + + obsprob = (1-p)*ones([4 2 Osize]); + % Q2 Q3 O + obsprob(1, 1, L) = p; + obsprob(1, 2, l) = p; + obsprob(2, 1, U) = p; + obsprob(2, 2, u) = p; + obsprob(3, 1, R) = p; + obsprob(3, 2, r) = p; + obsprob(4, 1, D) = p; + obsprob(4, 2, d) = p; + obsprob = mk_stochastic(obsprob); + Oargs = {'CPT', obsprob}; +else + % Initialise means of lowest level primitives in a way which we can interpret + % These means are little vectors in the east, south, west, north directions. + % (left-right=east, up-down=south, right-left=west, down-up=north) + Osize = 2; + mu = zeros(2, Qsizes(2), Qsizes(3)); + scale = 3; + if true_params + noise = 0; + else + noise = 0.5*scale; + end + for q3=1:Qsizes(3) + mu(:, 1, q3) = scale*[1;0] + noise*rand(2,1); + end + for q3=1:Qsizes(3) + mu(:, 2, q3) = scale*[0;-1] + noise*rand(2,1); + end + for q3=1:Qsizes(3) + mu(:, 3, q3) = scale*[-1;0] + noise*rand(2,1); + end + for q3=1:Qsizes(3) + mu(:, 4, q3) = scale*[0;1] + noise*rand(2,1); + end + Sigma = repmat(reshape(scale*eye(2), [2 2 1 1 ]), [1 1 Qsizes(2) Qsizes(3)]); + Oargs = {'mean', mu, 'cov', Sigma, 'cov_type', 'diag'}; +end + +if discrete_obs + selfprob = 0.5; +else + selfprob = 0.95; + % If less than this, it won't look like a square + % because it doesn't spend enough time in each state + % Unfortunately, the variance on durations (lengths of each side) + % is very large +end +bnet = mk_hhmm('Qsizes', Qsizes, 'Osize', Osize', 'discrete_obs', discrete_obs, ... + 'Oargs', Oargs, 'Ops', Qnodes(2:3), 'selfprob', selfprob, ... + 'startprob', startprob, 'transprob', transprob, 'termprob', termprob); + diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/Old/plot_square_hhmm.m b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/Old/plot_square_hhmm.m new file mode 100644 index 00000000..e6701e45 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/Old/plot_square_hhmm.m @@ -0,0 +1,32 @@ +function plot_square_hhmm(ev) +% Plot the square shape implicit in the evidence. +% ev{i,t} is the value of node i in slice t. +% The observed node contains a velocity (delta increment), which is converted +% into a position. +% The Q2 node specifies which model is used; each segment is color-coded +% in the order red, green, blue, black. + +Q1 = 1; Q2 = 2; Q3 = 3; F3 = 4; F2 = 5; Onode = 6; + +delta = cell2num(ev(Onode,:)); % delta(:,t) +Q2label = cell2num(ev(Q2,:)); + +T = size(delta, 2); +pos = zeros(2,T+1); +clf +hold on +cols = {'r', 'g', 'b', 'k'}; +boundary = 0; +coli = 1; +for t=2:T+1 + pos(:,t) = pos(:,t-1) + delta(:,t-1); + plot(pos(1,t), pos(2,t), sprintf('%c.', cols{coli})); + if t < T + boundary = (Q2label(t) ~= Q2label(t-1)); + end + if boundary + coli = coli + 1; + coli = mod(coli-1, length(cols)) + 1; + end +end + diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/Old/sample_square_hhmm.m b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/Old/sample_square_hhmm.m new file mode 100644 index 00000000..a0f9007e --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/Old/sample_square_hhmm.m @@ -0,0 +1,160 @@ + +seed = 0; +rand('state', seed); +randn('state', seed); + +discrete_obs = 1; +topright = 0; + +Qsizes = [2 4 2]; +D = 3; +Qnodes = 1:D; +startprob = cell(1,D); +transprob = cell(1,D); +termprob = cell(1,D); + +% LEVEL 1 + +startprob{1} = 'ergodic'; +transprob{1} = 'ergodic'; + +% LEVEL 2 + +startprob{2} = zeros(2, 4); +startprob{2}(1, :) = [1 0 0 0]; +if topright + startprob{2}(2, :) = [0 0 1 0]; +else + startprob{2}(2, :) = [0 1 0 0]; +end + +transprob{2} = zeros(4, 2, 4); + +transprob{2}(:,1,:) = [0 1 0 0 + 0 0 1 0 + 0 0 0 1 + 0 0 0 1]; % 4->e +if topright + transprob{2}(:,2,:) = [0 0 0 1 + 1 0 0 0 + 0 1 0 0 + 0 0 0 1]; % 4->e +else + transprob{2}(:,2,:) = [0 0 0 1 + 1 0 0 0 + 0 0 1 0 % 3->e + 0 0 1 0]; +end + +%termprob{2} = 'rightstop'; +termprob{2} = zeros(2,4,2); +pfin = 0.8; +termprob{2}(1,:,2) = [0 0 0 pfin]; % finish in state 4 (DU) +termprob{2}(1,:,1) = 1 - [0 0 0 pfin]; +if topright + termprob{2}(2,:,2) = [0 0 0 pfin]; + termprob{2}(2,:,1) = 1 - [0 0 0 pfin]; +else + termprob{2}(2,:,2) = [0 0 pfin 0]; % finish in state 3 (RL) + termprob{2}(2,:,1) = 1 - [0 0 pfin 0]; +end + +% LEVEL 3 + +startprob{3} = 'leftstart'; +transprob{3} = 'leftright'; +termprob{3} = 'rightstop'; + + +% OBS LEVEl + +if discrete_obs + chars = ['L', 'l', 'U', 'u', 'R', 'r', 'D', 'd']; + L=find(chars=='L'); l=find(chars=='l'); + U=find(chars=='U'); u=find(chars=='u'); + R=find(chars=='R'); r=find(chars=='r'); + D=find(chars=='D'); d=find(chars=='d'); + Osize = length(chars); + + obsprob = zeros([4 2 Osize]); + % Q2 Q3 O + obsprob(1, 1, L) = 1.0; + obsprob(1, 2, l) = 1.0; + obsprob(2, 1, U) = 1.0; + obsprob(2, 2, u) = 1.0; + obsprob(3, 1, R) = 1.0; + obsprob(3, 2, r) = 1.0; + obsprob(4, 1, D) = 1.0; + obsprob(4, 2, d) = 1.0; + + Oargs = {'CPT', obsprob}; +else + Osize = 2; + mu = zeros(2, 4, 2); + noise = 0; + scale = 10; + for q3=1:2 + mu(:, 1, q3) = scale*[1;0] + noise*rand(2,1); + end + for q3=1:2 + mu(:, 2, q3) = scale*[0;-1] + noise*rand(2,1); + end + for q3=1:2 + mu(:, 3, q3) = scale*[-1;0] + noise*rand(2,1); + end + for q3=1:2 + mu(:, 4, q3) = scale*[0;1] + noise*rand(2,1); + end + Sigma = repmat(reshape(0.01*eye(2), [2 2 1 1 ]), [1 1 4 2]); + Oargs = {'mean', mu, 'cov', Sigma}; +end + +bnet = mk_hhmm('Qsizes', Qsizes, 'Osize', Osize', 'discrete_obs', discrete_obs, ... + 'Oargs', Oargs, 'Ops', Qnodes(2:3), ... + 'startprob', startprob, 'transprob', transprob, 'termprob', termprob); + +if discrete_obs + Tmax = 30; +else + Tmax = 200; +end +usecell = ~discrete_obs; +Q1 = 1; Q2 = 2; Q3 = 3; F3 = 4; F2 = 5; Onode = 6; +Qnodes = [Q1 Q2 Q3]; Fnodes = [F2 F3]; + +for seqi=1:3 + evidence = sample_dbn(bnet, Tmax, usecell, 'stop_sampling_F2'); + T = size(evidence, 2) + if discrete_obs + pretty_print_hhmm_parse(evidence, Qnodes, Fnodes, Onode, chars); + else + pos = zeros(2,T+1); + delta = cell2num(evidence(Onode,:)); + clf + hold on + cols = {'r', 'g', 'k', 'b'}; + boundary = cell2num(evidence(F3,:))-1; + coli = 1; + for t=2:T+1 + pos(:,t) = pos(:,t-1) + delta(:,t-1); + plot(pos(1,t), pos(2,t), sprintf('%c.', cols{coli})); + if boundary(t-1) + coli = coli + 1; + coli = mod(coli-1, length(cols)) + 1; + end + end + %plot(pos(1,:), pos(2,:), '.') + %pretty_print_hhmm_parse(evidence, Qnodes, Fnodes, Onode, []); + pause + end +end + +eclass = bnet.equiv_class; +S=struct(bnet.CPD{eclass(Q2,2)}); + + + + + + + diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/get_square_data.m b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/get_square_data.m new file mode 100644 index 00000000..9790221c --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/get_square_data.m @@ -0,0 +1,70 @@ +% Let the user draw a square with the mouse, +% and then click on the corners to do a manual segmentation + +ss = 6; +Q1 = 1; Q2 = 2; Q3 = 3; obsvel = 6; +CLOCKWISE = 1; ANTICLOCK = 2; +LR = 1; UD = 2; RL = 3; DU = 4; + +% repeat this block manually incrementing the sequence number +% and setting ori. +% (since I don't know how to call getmouse as a call-return function). +seq = 4; +%ori = CLOCKWISE +ori = ANTICLOCK; +clear xpos ypos +getmouse +% end block + +% manual segmentation with the mouse +startseg(1) = 1; +for i=2:4 + fprintf('click on start of segment %d\n', i); + [x,y] = ginput(1); + plot(x,y,'ro') + d = dist2([xpos; ypos]', [x y]); + startseg(i) = argmin(d); +end + +% plot corners in green +%ti = first point in (i+1)st segment +t1 = startseg(1); t2 = startseg(2); t3 = startseg(3); t4 = startseg(4); +plot(xpos(t2), ypos(t2), 'g*') +plot(xpos(t3), ypos(t3), 'g*') +plot(xpos(t4), ypos(t4), 'g*') + + +xvel = xpos(2:end) - xpos(1:end-1); +yvel = ypos(2:end) - ypos(1:end-1); +speed = [xvel(:)'; yvel(:)']; +pos_data{seq} = [xpos(:)'; ypos(:)']; +vel_data{seq} = [xvel(:)'; yvel(:)']; +T = length(xvel); +Q1label{seq} = num2cell(repmat(ori, 1, T)); +Q2label{seq} = zeros(1, T); +if ori == CLOCKWISE + Q2label{seq}(t1:t2) = LR; + Q2label{seq}(t2+1:t3) = UD; + Q2label{seq}(t3+1:t4) = RL; + Q2label{seq}(t4+1:T) = DU; +else + Q2label{seq}(t1:t2) = RL; + Q2label{seq}(t2+1:t3) = UD; + Q2label{seq}(t3+1:t4) = LR; + Q2label{seq}(t4+1:T) = DU; +end + +% pos_data{seq}(:,t), vel_data{seq}(:,t) Q1label{seq}(t) Q2label{seq}(t) +save 'square4' pos_data vel_data Q1label Q2label + +nseq = 4; +cases = cell(1,nseq); +for seq=1:nseq + T = size(vel_data{seq},2); + ev = cell(ss,T); + ev(obsvel,:) = num2cell(vel_data{seq},1); + ev(Q1,:) = Q1label{seq}; + ev(Q2,:) = num2cell(Q2label{seq}); + cases{seq} = ev; +end +save 'square4_cases' cases diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/hhmm_inference.m b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/hhmm_inference.m new file mode 100644 index 00000000..c3bc8441 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/hhmm_inference.m @@ -0,0 +1,13 @@ +bnet = mk_square_hhmm(1, 1); + +engine = {}; +engine{end+1} = hmm_inf_engine(bnet); +engine{end+1} = smoother_engine(jtree_2TBN_inf_engine(bnet)); + +exact = 1:length(engine); +filter = 0; +single = 0; +maximize = 0; +T = 4; + +[err, inf_time, engine] = cmp_inference(bnet, engine, exact, T, filter, single, maximize); diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/is_F2_true_D3.m b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/is_F2_true_D3.m new file mode 100644 index 00000000..38d0b6e8 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/is_F2_true_D3.m @@ -0,0 +1,12 @@ +function stop = is_F2_true_D3(vals) +% function stop = is_F2_true_D3(vals) +% +% If vals(F2)=2 then level 2 has finished, so we return stop=1 +% to stop sample_dbn. Otherwise we return stop=0. +% We assume this is for a D=3 level HHMM. + +Q1 = 1; Q2 = 2; Q3 = 3; F3 = 4; F2 = 5; Onode = 6; +stop = 0; +if (iscell(vals) & vals{F2}==2) | (~iscell(vals) & vals(F2)==2) + stop = 1; +end diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/learn_square_hhmm_cts.m b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/learn_square_hhmm_cts.m new file mode 100644 index 00000000..77bdaec3 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/learn_square_hhmm_cts.m @@ -0,0 +1,152 @@ +% Try to learn a 3 level HHMM similar to mk_square_hhmm +% from hand-drawn squares. + +% Because startprob should be shared for t=1:T, +% but in the DBN is shared for t=2:T, we train using a single long sequence. + +discrete_obs = 0; +supervised = 1; +obs_finalF2 = 0; +% It is not possible to observe F2 if we learn +% because the update_ess method for hhmmF_CPD and hhmmQ_CPD assume +% the F nodes are always hidden (for speed). +% However, for generating, we might want to set the final F2=true +% to force all subroutines to finish. + +seed = 1; +rand('state', seed); +randn('state', seed); + +bnet = mk_square_hhmm(discrete_obs, 0); + +ss = 6; +Q1 = 1; Q2 = 2; Q3 = 3; F3 = 4; F2 = 5; Onode = 6; +Qnodes = [Q1 Q2 Q3]; Fnodes = [F2 F3]; +Qsizes = [2 4 1]; + +if supervised + bnet.observed = [Q1 Q2 Onode]; +else + bnet.observed = [Onode]; +end + +if obs_finalF2 + engine = jtree_dbn_inf_engine(bnet); + % can't use ndx version because sometimes F2 is hidden, sometimes observed + error('can''t observe F when learning') +else + if supervised + engine = jtree_ndx_dbn_inf_engine(bnet); + else + engine = jtree_hmm_inf_engine(bnet); + end +end + +load 'square4_cases' % cases{seq}{i,t} for i=1:ss +%plot_square_hhmm(cases{1}) +%long_seq = cat(2, cases{:}); +train_cases = cases(1:2); +long_seq = cat(2, train_cases{:}); +if ~supervised + T = size(long_seq,2); + for t=1:T + long_seq{Q1,t} = []; + long_seq{Q2,t} = []; + end +end +[bnet2, LL, engine2] = learn_params_dbn_em(engine, {long_seq}, 'max_iter', 2); + +eclass = bnet2.equiv_class; +CPDO=struct(bnet2.CPD{eclass(Onode,1)}); +mu = CPDO.mean; +Sigma = CPDO.cov; +CPDO_full = CPDO; + +% force diagonal covs after training +for k=1:size(Sigma,3) + Sigma(:,:,k) = diag(diag(Sigma(:,:,k))); +end +bnet2.CPD{6} = set_fields(bnet.CPD{6}, 'cov', Sigma); + +if 0 + % visualize each model by concatenating means for each model for nsteps in a row + nsteps = 5; + ev = cell(ss, nsteps*prod(Qsizes(2:3))); + t = 1; + for q2=1:Qsizes(2) + for q3=1:Qsizes(3) + for i=1:nsteps + ev{Onode,t} = mu(:,q2,q3); + ev{Q2,t} = q2; + t = t + 1; + end + end + end + plot_square_hhmm(ev) +end + +% bnet3 is the same as the learned model, except we will use it in testing mode +if supervised + bnet3 = bnet2; + bnet3.observed = [Onode]; + engine3 = hmm_inf_engine(bnet3); + %engine3 = jtree_ndx_dbn_inf_engine(bnet3); +else + bnet3 = bnet2; + engine3 = engine2; +end + +if 0 + % segment whole sequence + mpe = calc_mpe_dbn(engine3, long_seq); + pretty_print_hhmm_parse(mpe, Qnodes, Fnodes, Onode, []); +end + +% segment each sequence +test_cases = cases(3:4); +for i=1:2 + ev = test_cases{i}; + T = size(ev, 2); + for t=1:T + ev{Q1,t} = []; + ev{Q2,t} = []; + end + %mpe = calc_mpe_dbn(engine3, ev); + mpe = find_mpe(engine3, ev) + subplot(1,2,i) + plot_square_hhmm(mpe) + %pretty_print_hhmm_parse(mpe, Qnodes, Fnodes, Onode, []); + q1s = cell2num(mpe(Q1,:)); + h = hist(q1s, 1:Qsizes(1)); + map_q1 = argmax(h); + str = sprintf('test seq %d is of type %d\n', i, map_q1); + title(str) +end + + +if 0 +% Estimate gotten by couting transitions in the labelled data +% Note that a self transition shouldnt count if F2=off. +Q2ev = cell2num(ev(Q2,:)); +Q2a = Q2ev(1:end-1); +Q2b = Q2ev(2:end); +counts = compute_counts([Q2a; Q2b], [4 4]); +end + +eclass = bnet2.equiv_class; +CPDQ1=struct(bnet2.CPD{eclass(Q1,2)}); +CPDQ2=struct(bnet2.CPD{eclass(Q2,2)}); +CPDQ3=struct(bnet2.CPD{eclass(Q3,2)}); +CPDF2=struct(bnet2.CPD{eclass(F2,1)}); +CPDF3=struct(bnet2.CPD{eclass(F3,1)}); + + +A=add_hhmm_end_state(CPDQ2.transprob, CPDF2.termprob(:,:,2)); +squeeze(A(:,1,:)); +CPDQ2.startprob; + +if 0 +S=struct(CPDF2.sub_CPD_term); +S.nsamples +reshape(S.counts, [2 4 2]) +end diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/learn_square_hhmm_discrete.m b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/learn_square_hhmm_discrete.m new file mode 100644 index 00000000..3110ae5e --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/learn_square_hhmm_discrete.m @@ -0,0 +1,171 @@ +% Try to learn a 3 level HHMM similar to mk_square_hhmm +% from synthetic discrete sequences + + +discrete_obs = 1; +supervised = 0; +obs_finalF2 = 0; + +seed = 1; +rand('state', seed); +randn('state', seed); + +bnet_init = mk_square_hhmm(discrete_obs, 0); + +ss = 6; +Q1 = 1; Q2 = 2; Q3 = 3; F3 = 4; F2 = 5; Onode = 6; +Qnodes = [Q1 Q2 Q3]; Fnodes = [F2 F3]; + +if supervised + bnet_init.observed = [Q1 Q2 Onode]; +else + bnet_init.observed = [Onode]; +end + +if obs_finalF2 + engine_init = jtree_dbn_inf_engine(bnet_init); + % can't use ndx version because sometimes F2 is hidden, sometimes observed + error('can''t observe F when learning') + % It is not possible to observe F2 if we learn + % because the update_ess method for hhmmF_CPD and hhmmQ_CPD assume + % the F nodes are always hidden (for speed). + % However, for generating, we might want to set the final F2=true + % to force all subroutines to finish. +else + if supervised + engine_init = jtree_ndx_dbn_inf_engine(bnet_init); + else + engine_init = hmm_inf_engine(bnet_init); + end +end + +% generate some synthetic data (easier to debug) +chars = ['L', 'l', 'U', 'u', 'R', 'r', 'D', 'd']; +L=find(chars=='L'); l=find(chars=='l'); +U=find(chars=='U'); u=find(chars=='u'); +R=find(chars=='R'); r=find(chars=='r'); +D=find(chars=='D'); d=find(chars=='d'); + +cases = {}; + +T = 8; +ev = cell(ss, T); +ev(Onode,:) = num2cell([L l U u R r D d]); +if supervised + ev(Q1,:) = num2cell(1*ones(1,T)); + ev(Q2,:) = num2cell( [1 1 2 2 3 3 4 4]); +end +cases{1} = ev; +cases{3} = ev; + +T = 8; +ev = cell(ss, T); +%we start with R then r, even though we are running the model 'backwards'! +ev(Onode,:) = num2cell([R r U u L l D d]); + +if supervised + ev(Q1,:) = num2cell(2*ones(1,T)); + ev(Q2,:) = num2cell( [3 3 2 2 1 1 4 4]); +end + +cases{2} = ev; +cases{4} = ev; + +if obs_finalF2 + for i=1:length(cases) + T = size(cases{i},2); + cases{i}(F2,T)={2}; % force F2 to be finished at end of seq + end +end + + +% startprob should be shared for t=1:T, +% but in the DBN it is shared for t=2:T, +% so we train using a single long sequence. +long_seq = cat(2, cases{:}); +[bnet_learned, LL, engine_learned] = ... + learn_params_dbn_em(engine_init, {long_seq}, 'max_iter', 200); + +% figure out which subsequence each model is responsible for +mpe = calc_mpe_dbn(engine_learned, long_seq); +pretty_print_hhmm_parse(mpe, Qnodes, Fnodes, Onode, chars); + + +% The "true" segmentation of the training sequence is +% Q1: 1 2 +% O: L l U u R r D d | R r U u L l D d | etc. +% +% When we learn in a supervised fashion, we recover the "truth". + +% When we learn in an unsupervised fashion with seed=1, we get +% Q1: 2 1 +% O: L l U u R r D d R r | U u L l D d | etc. +% +% This means for model 1: +% starts in state 2 +% transitions 2->1, 1->4, 4->e, 3->2 +% +% For model 2, +% starts in state 1 +% transitions 1->2, 2->3, 3->4 or e, 4->3 + +% examine the params +eclass = bnet_learned.equiv_class; +CPDQ1=struct(bnet_learned.CPD{eclass(Q1,2)}); +CPDQ2=struct(bnet_learned.CPD{eclass(Q2,2)}); +CPDQ3=struct(bnet_learned.CPD{eclass(Q3,2)}); +CPDF2=struct(bnet_learned.CPD{eclass(F2,1)}); +CPDF3=struct(bnet_learned.CPD{eclass(F3,1)}); +CPDO=struct(bnet_learned.CPD{eclass(Onode,1)}); + +A_learned =add_hhmm_end_state(CPDQ2.transprob, CPDF2.termprob(:,:,2)); +squeeze(A_learned(:,1,:)) +squeeze(A_learned(:,2,:)) + + +% Does the "true" model have higher likelihood than the learned one? +% i.e., Does the unsupervised method learn the wrong model because +% we have the wrong cost fn, or because of local minima? + +bnet_true = mk_square_hhmm(discrete_obs,1); + +% examine the params +eclass = bnet_learned.equiv_class; +CPDQ1_true=struct(bnet_true.CPD{eclass(Q1,2)}); +CPDQ2_true=struct(bnet_true.CPD{eclass(Q2,2)}); +CPDQ3_true=struct(bnet_true.CPD{eclass(Q3,2)}); +CPDF2_true=struct(bnet_true.CPD{eclass(F2,1)}); +CPDF3_true=struct(bnet_true.CPD{eclass(F3,1)}); + +A_true =add_hhmm_end_state(CPDQ2_true.transprob, CPDF2_true.termprob(:,:,2)); +squeeze(A_true(:,1,:)) + + +if supervised + engine_true = jtree_ndx_dbn_inf_engine(bnet_true); +else + engine_true = hmm_inf_engine(bnet_true); +end + +%[engine_learned, ll_learned] = enter_evidence(engine_learned, long_seq); +%[engine_true, ll_true] = enter_evidence(engine_true, long_seq); +[engine_learned, ll_learned] = enter_evidence(engine_learned, cases{2}); +[engine_true, ll_true] = enter_evidence(engine_true, cases{2}); +ll_learned +ll_true + + +% remove concatentation artefacts +ll_learned = 0; +ll_true = 0; +for m=1:length(cases) + [engine_learned, ll_learned_tmp] = enter_evidence(engine_learned, cases{m}); + [engine_true, ll_true_tmp] = enter_evidence(engine_true, cases{m}); + ll_learned = ll_learned + ll_learned_tmp; + ll_true = ll_true + ll_true_tmp; +end +ll_learned +ll_true + +% In both cases, ll_learned >> ll_true +% which shows we are using the wrong cost function! diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/mk_square_hhmm.m b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/mk_square_hhmm.m new file mode 100644 index 00000000..41cfc539 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/mk_square_hhmm.m @@ -0,0 +1,180 @@ +function bnet = mk_square_hhmm(discrete_obs, true_params, topright) + +% Make a 3 level HHMM described by the following grammar +% +% Square -> CLK | CCK % clockwise or counterclockwise +% CLK -> LR UD RL DU start on top left (1 2 3 4) +% CCK -> RL UD LR DU if start at top right (3 2 1 4) +% CCK -> UD LR DU RL if start at top left (2 1 4 3) +% +% LR = left-right, UD = up-down, RL = right-left, DU = down-up +% LR, UD, RL, DU are sub HMMs. +% +% For discrete observations, the subHMMs are 2-state left-right. +% LR emits L then l, etc. +% +% For cts observations, the subHMMs are 1 state. +% LR emits a vector in the -> direction, with a little noise. +% Since there is no constraint that we remain in the LR state as long as the RL state, +% the sides of the square might have different lengths, +% so the result is not really a square! +% +% If true_params = 0, we use random parameters at the top 2 levels +% (ready for learning). At the bottom level, we use noisy versions +% of the "true" observations. +% +% If topright=1, counter-clockwise starts at top right, not top left +% This example was inspired by Ivanov and Bobick. + +if nargin < 3, topright = 1; end + +if 1 % discrete_obs + Qsizes = [2 4 2]; +else + Qsizes = [2 4 1]; +end + +D = 3; +Qnodes = 1:D; +startprob = cell(1,D); +transprob = cell(1,D); +termprob = cell(1,D); + +% LEVEL 1 + +startprob{1} = 'unif'; +transprob{1} = 'unif'; + +% LEVEL 2 + +if true_params + startprob{2} = zeros(2, 4); + startprob{2}(1, :) = [1 0 0 0]; + if topright + startprob{2}(2, :) = [0 0 1 0]; + else + startprob{2}(2, :) = [0 1 0 0]; + end + + transprob{2} = zeros(4, 2, 4); + + transprob{2}(:,1,:) = [0 1 0 0 + 0 0 1 0 + 0 0 0 1 + 0 0 0 1]; % 4->e + if topright + transprob{2}(:,2,:) = [0 0 0 1 + 1 0 0 0 + 0 1 0 0 + 0 0 0 1]; % 4->e + else + transprob{2}(:,2,:) = [0 0 0 1 + 1 0 0 0 + 0 0 1 0 % 3->e + 0 0 1 0]; + end + + %termprob{2} = 'rightstop'; + termprob{2} = zeros(2,4); + pfin = 0.8; + termprob{2}(1,:) = [0 0 0 pfin]; % finish in state 4 (DU) + if topright + termprob{2}(2,:) = [0 0 0 pfin]; + else + termprob{2}(2,:) = [0 0 pfin 0]; % finish in state 3 (RL) + end +else + % In the unsupervised case, it is essential that we break symmetry + % in the initial param estimates. + %startprob{2} = 'unif'; + %transprob{2} = 'unif'; + %termprob{2} = 'unif'; + startprob{2} = 'rnd'; + transprob{2} = 'rnd'; + termprob{2} = 'rnd'; +end + +% LEVEL 3 + +if 1 | true_params + startprob{3} = 'leftstart'; + transprob{3} = 'leftright'; + termprob{3} = 'rightstop'; +else + % If we want to be able to run a base-level model backwards... + startprob{3} = 'rnd'; + transprob{3} = 'rnd'; + termprob{3} = 'rnd'; +end + + +% OBS LEVEl + +if discrete_obs + % Initialise observations of lowest level primitives in a way which we can interpret + chars = ['L', 'l', 'U', 'u', 'R', 'r', 'D', 'd']; + L=find(chars=='L'); l=find(chars=='l'); + U=find(chars=='U'); u=find(chars=='u'); + R=find(chars=='R'); r=find(chars=='r'); + D=find(chars=='D'); d=find(chars=='d'); + Osize = length(chars); + + if true_params + p = 1; % makes each state fully observed + else + p = 0.9; + end + + obsprob = (1-p)*ones([4 2 Osize]); + % Q2 Q3 O + obsprob(1, 1, L) = p; + obsprob(1, 2, l) = p; + obsprob(2, 1, U) = p; + obsprob(2, 2, u) = p; + obsprob(3, 1, R) = p; + obsprob(3, 2, r) = p; + obsprob(4, 1, D) = p; + obsprob(4, 2, d) = p; + obsprob = mk_stochastic(obsprob); + Oargs = {'CPT', obsprob}; +else + % Initialise means of lowest level primitives in a way which we can interpret + % These means are little vectors in the east, south, west, north directions. + % (left-right=east, up-down=south, right-left=west, down-up=north) + Osize = 2; + mu = zeros(2, Qsizes(2), Qsizes(3)); + scale = 3; + if true_params + noise = 0; + else + noise = 0.5*scale; + end + for q3=1:Qsizes(3) + mu(:, 1, q3) = scale*[1;0] + noise*rand(2,1); + end + for q3=1:Qsizes(3) + mu(:, 2, q3) = scale*[0;-1] + noise*rand(2,1); + end + for q3=1:Qsizes(3) + mu(:, 3, q3) = scale*[-1;0] + noise*rand(2,1); + end + for q3=1:Qsizes(3) + mu(:, 4, q3) = scale*[0;1] + noise*rand(2,1); + end + Sigma = repmat(reshape(scale*eye(2), [2 2 1 1 ]), [1 1 Qsizes(2) Qsizes(3)]); + Oargs = {'mean', mu, 'cov', Sigma, 'cov_type', 'diag'}; +end + +if discrete_obs + selfprob = 0.5; +else + selfprob = 0.95; + % If less than this, it won't look like a square + % because it doesn't spend enough time in each state + % Unfortunately, the variance on durations (lengths of each side) + % is very large +end +bnet = mk_hhmm('Qsizes', Qsizes, 'Osize', Osize', 'discrete_obs', discrete_obs, ... + 'Oargs', Oargs, 'Ops', Qnodes(2:3), 'selfprob', selfprob, ... + 'startprob', startprob, 'transprob', transprob, 'termprob', termprob); + diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/plot_square_hhmm.m b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/plot_square_hhmm.m new file mode 100644 index 00000000..e61e5669 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/plot_square_hhmm.m @@ -0,0 +1,27 @@ +function plot_square_hhmm(ev) +% Plot the square shape implicit in the evidence. +% ev{i,t} is the value of node i in slice t. +% The observed node contains a velocity (delta increment), which is converted +% into a position. +% The Q2 node specifies which model is used, and hence which color +% to use: 1=red, 2=green, 3=blue, 4=black. + +Q1 = 1; Q2 = 2; Q3 = 3; F3 = 4; F2 = 5; Onode = 6; + +delta = cell2num(ev(Onode,:)); % delta(:,t) +Q2label = cell2num(ev(Q2,:)); + +T = size(delta, 2); +pos = zeros(2,T+1); +hold on +cols = {'r', 'g', 'b', 'k'}; +for t=2:T+1 + pos(:,t) = pos(:,t-1) + delta(:,t-1); + plot(pos(1,t), pos(2,t), sprintf('%c.', cols{Q2label(t-1)})); + if (t==2) + text(pos(1,t-1),pos(2,t-1),sprintf('%d',t)) + elseif (mod(t,20)==0) + text(pos(1,t),pos(2,t),sprintf('%d',t)) + end +end + diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/sample_square_hhmm_cts.m b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/sample_square_hhmm_cts.m new file mode 100644 index 00000000..3ab2abb5 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/sample_square_hhmm_cts.m @@ -0,0 +1,20 @@ +% Generate samples from the HHMM with the true params. + +seed = 1; +rand('state', seed); +randn('state', seed); + +discrete_obs = 0; + +bnet = mk_square_hhmm(discrete_obs, 1); +Q1 = 1; Q2 = 2; Q3 = 3; F3 = 4; F2 = 5; Onode = 6; +Qnodes = [Q1 Q2 Q3]; Fnodes = [F2 F3]; + +for seqi=1:1 + evidence = sample_dbn(bnet, 'stop_test', 'is_F2_true_D3'); + clf + plot_square_hhmm(evidence); + %pretty_print_hhmm_parse(evidence, Qnodes, Fnodes, Onode, []); + fprintf('sequence %d has length %d; press key to continue\n', seqi, size(evidence,2)) + pause +end diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/sample_square_hhmm_discrete.m b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/sample_square_hhmm_discrete.m new file mode 100644 index 00000000..20279899 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/sample_square_hhmm_discrete.m @@ -0,0 +1,20 @@ +% Generate samples from the HHMM with the true params. + +seed = 0; +rand('state', seed); +randn('state', seed); + +discrete_obs = 1; + +bnet = mk_square_hhmm(discrete_obs, 1); + +Tmax = 30; +Q1 = 1; Q2 = 2; Q3 = 3; F3 = 4; F2 = 5; Onode = 6; +Qnodes = [Q1 Q2 Q3]; Fnodes = [F2 F3]; +chars = ['L', 'l', 'U', 'u', 'R', 'r', 'D', 'd']; + +for seqi=1:3 + evidence = cell2num(sample_dbn(bnet, 'stop_test', 'is_F2_true_D3')); + T = size(evidence, 2) + pretty_print_hhmm_parse(evidence, Qnodes, Fnodes, Onode, chars); +end diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/square4.mat b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/square4.mat new file mode 100644 index 00000000..cda0585b --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/square4.mat Binary files differdiff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/square4_cases.mat b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/square4_cases.mat new file mode 100644 index 00000000..788c3239 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/square4_cases.mat Binary files differdiff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/test_square_fig.m b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/test_square_fig.m new file mode 100644 index 00000000..e983af14 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/test_square_fig.m @@ -0,0 +1,1310 @@ +function fig = test_square_fig() +% This is the machine-generated representation of a Handle Graphics object +% and its children. Note that handle values may change when these objects +% are re-created. This may cause problems with any callbacks written to +% depend on the value of the handle at the time the object was saved. +% +% To reopen this object, just type the name of the M-file at the MATLAB +% prompt. The M-file and its associated MAT-file must be on your path. + +load test_square_fig + +h0 = figure('Color',[0.8 0.8 0.8], ... + 'Colormap',mat0, ... + 'PointerShapeCData',mat1, ... + 'Position',[540 374 476 292]); +h1 = axes('Parent',h0, ... + 'CameraUpVector',[0 1 0], ... + 'Color',[1 1 1], ... + 'ColorOrder',mat2, ... + 'NextPlot','add', ... + 'Position',[0.13 0.11 0.3270231213872832 0.8149999999999998], ... + 'XColor',[0 0 0], ... + 'XLim',[-10 50], ... + 'XLimMode','manual', ... + 'YColor',[0 0 0], ... + 'YLim',[-60 10], ... + 'YLimMode','manual', ... + 'ZColor',[0 0 0]); +h2 = line('Parent',h1, ... + 'Color',[1 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',0.4608294930875587, ... + 'YData',0.2923976608187218); +h2 = text('Parent',h1, ... + 'Color',[0 0 0], ... + 'String','2'); +h2 = line('Parent',h1, ... + 'Color',[1 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',1.152073732718893, ... + 'YData',0.2923976608187218); +h2 = line('Parent',h1, ... + 'Color',[1 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',2.995391705069125, ... + 'YData',0.8771929824561511); +h2 = line('Parent',h1, ... + 'Color',[1 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',3.686635944700463, ... + 'YData',0.8771929824561511); +h2 = line('Parent',h1, ... + 'Color',[1 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',6.451612903225808, ... + 'YData',0.8771929824561511); +h2 = line('Parent',h1, ... + 'Color',[1 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',9.677419354838712, ... + 'YData',1.461988304093566); +h2 = line('Parent',h1, ... + 'Color',[1 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',10.36866359447005, ... + 'YData',1.461988304093566); +h2 = line('Parent',h1, ... + 'Color',[1 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',15.43778801843318, ... + 'YData',2.046783625730996); +h2 = line('Parent',h1, ... + 'Color',[1 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',17.51152073732719, ... + 'YData',2.046783625730996); +h2 = line('Parent',h1, ... + 'Color',[1 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',19.81566820276498, ... + 'YData',2.046783625730996); +h2 = line('Parent',h1, ... + 'Color',[1 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',20.50691244239631, ... + 'YData',2.046783625730996); +h2 = line('Parent',h1, ... + 'Color',[1 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',23.73271889400922, ... + 'YData',2.046783625730996); +h2 = line('Parent',h1, ... + 'Color',[1 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',25.57603686635945, ... + 'YData',2.046783625730996); +h2 = line('Parent',h1, ... + 'Color',[1 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',29.95391705069125, ... + 'YData',2.046783625730996); +h2 = line('Parent',h1, ... + 'Color',[1 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',31.79723502304147, ... + 'YData',2.046783625730996); +h2 = line('Parent',h1, ... + 'Color',[1 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',35.02304147465438, ... + 'YData',2.046783625730996); +h2 = line('Parent',h1, ... + 'Color',[1 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',35.71428571428572, ... + 'YData',2.046783625730996); +h2 = line('Parent',h1, ... + 'Color',[1 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',38.47926267281106, ... + 'YData',1.461988304093566); +h2 = line('Parent',h1, ... + 'Color',[1 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',40.3225806451613, ... + 'YData',1.461988304093566); +h2 = text('Parent',h1, ... + 'Color',[0 0 0], ... + 'Position',[40.3225806451613 1.461988304093566 0], ... + 'String','20'); +h2 = line('Parent',h1, ... + 'Color',[1 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',42.62672811059908, ... + 'YData',mat3); +h2 = line('Parent',h1, ... + 'Color',[1 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',43.31797235023042, ... + 'YData',mat4); +h2 = line('Parent',h1, ... + 'Color',[1 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',43.31797235023042, ... + 'YData',0.8771929824561511); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',43.54838709677419, ... + 'YData',0); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',43.77880184331798, ... + 'YData',-0.5847953216374293); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',44.47004608294931, ... + 'YData',-2.339181286549703); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',44.93087557603687, ... + 'YData',-4.385964912280699); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',46.7741935483871, ... + 'YData',-9.064327485380119); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',47.00460829493088, ... + 'YData',-10.81871345029239); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',47.69585253456221, ... + 'YData',mat5); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',47.69585253456221, ... + 'YData',-15.20467836257309); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',47.23502304147466, ... + 'YData',-19.00584795321637); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',47.23502304147466, ... + 'YData',-19.88304093567251); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',47.23502304147466, ... + 'YData',-22.51461988304093); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',47.23502304147466, ... + 'YData',-23.09941520467836); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',47.23502304147466, ... + 'YData',-26.02339181286549); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',47.23502304147466, ... + 'YData',-26.31578947368421); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',47.23502304147466, ... + 'YData',-27.77777777777777); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',47.23502304147466, ... + 'YData',-28.3625730994152); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',47.23502304147466, ... + 'YData',-30.99415204678362); +h2 = text('Parent',h1, ... + 'Color',[0 0 0], ... + 'Position',[47.23502304147466 -30.99415204678362 0], ... + 'String','40'); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',47.46543778801843, ... + 'YData',-31.57894736842105); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',47.46543778801843, ... + 'YData',-33.62573099415204); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',47.46543778801843, ... + 'YData',-34.50292397660818); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',47.46543778801843, ... + 'YData',-37.42690058479531); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',47.46543778801843, ... + 'YData',-38.01169590643274); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',47.00460829493088, ... + 'YData',-42.39766081871344); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',47.00460829493088, ... + 'YData',-42.98245614035087); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',47.00460829493088, ... + 'YData',-46.49122807017543); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',46.7741935483871, ... + 'YData',-46.78362573099415); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',46.54377880184332, ... + 'YData',-49.41520467836257); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',46.54377880184332, ... + 'YData',-49.70760233918128); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',45.85253456221199, ... + 'YData',-51.46198830409356); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',45.85253456221199, ... + 'YData',-51.75438596491227); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',44.93087557603687, ... + 'YData',-53.21637426900584); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',44.70046082949308, ... + 'YData',-53.21637426900584); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',44.00921658986175, ... + 'YData',-54.09356725146198); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',43.77880184331798, ... + 'YData',-54.38596491228069); +h2 = line('Parent',h1, ... + 'Color',[0 0 1], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',41.93548387096774, ... + 'YData',-54.97076023391811); +h2 = line('Parent',h1, ... + 'Color',[0 0 1], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',41.47465437788019, ... + 'YData',-55.26315789473683); +h2 = line('Parent',h1, ... + 'Color',[0 0 1], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',39.1705069124424, ... + 'YData',-55.55555555555554); +h2 = text('Parent',h1, ... + 'Color',[0 0 0], ... + 'Position',[39.1705069124424 -55.55555555555554 0], ... + 'String','60'); +h2 = line('Parent',h1, ... + 'Color',[0 0 1], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',38.94009216589862, ... + 'YData',-55.84795321637426); +h2 = line('Parent',h1, ... + 'Color',[0 0 1], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',36.63594470046083, ... + 'YData',-55.55555555555554); +h2 = line('Parent',h1, ... + 'Color',[0 0 1], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',36.17511520737327, ... + 'YData',-55.55555555555554); +h2 = line('Parent',h1, ... + 'Color',[0 0 1], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',32.94930875576037, ... + 'YData',-54.97076023391811); +h2 = line('Parent',h1, ... + 'Color',[0 0 1], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',32.48847926267281, ... + 'YData',-54.97076023391811); +h2 = line('Parent',h1, ... + 'Color',[0 0 1], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',28.11059907834102, ... + 'YData',-53.80116959064326); +h2 = line('Parent',h1, ... + 'Color',[0 0 1], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',27.64976958525346, ... + 'YData',-53.50877192982455); +h2 = line('Parent',h1, ... + 'Color',[0 0 1], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',23.963133640553, ... + 'YData',-53.50877192982455); +h2 = line('Parent',h1, ... + 'Color',[0 0 1], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',23.27188940092166, ... + 'YData',-53.50877192982455); +h2 = line('Parent',h1, ... + 'Color',[0 0 1], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',19.5852534562212, ... + 'YData',-54.97076023391811); +h2 = line('Parent',h1, ... + 'Color',[0 0 1], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',19.12442396313364, ... + 'YData',-54.97076023391811); +h2 = line('Parent',h1, ... + 'Color',[0 0 1], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',mat6, ... + 'YData',-56.14035087719297); +h2 = line('Parent',h1, ... + 'Color',[0 0 1], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',mat7, ... + 'YData',-56.14035087719297); +h2 = line('Parent',h1, ... + 'Color',[0 0 1], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',9.907834101382491, ... + 'YData',-57.30994152046782); +h2 = line('Parent',h1, ... + 'Color',[0 0 1], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',9.447004608294932, ... + 'YData',-57.30994152046782); +h2 = line('Parent',h1, ... + 'Color',[0 0 1], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',6.221198156682029, ... + 'YData',-57.30994152046782); +h2 = line('Parent',h1, ... + 'Color',[0 0 1], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',4.838709677419356, ... + 'YData',-56.7251461988304); +h2 = line('Parent',h1, ... + 'Color',[0 0 1], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',2.764976958525345, ... + 'YData',-56.14035087719297); +h2 = line('Parent',h1, ... + 'Color',[0 0 1], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',2.534562211981569, ... + 'YData',-56.14035087719297); +h2 = line('Parent',h1, ... + 'Color',[0 0 1], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',0.9216589861751174, ... + 'YData',-53.80116959064327); +h2 = text('Parent',h1, ... + 'Color',[0 0 0], ... + 'Position',[0.9216589861751174 -53.80116959064327 0], ... + 'String','80'); +h2 = line('Parent',h1, ... + 'Color',[0 0 1], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',0.6912442396313381, ... + 'YData',-53.21637426900584); +h2 = line('Parent',h1, ... + 'Color',[0 0 1], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-1.152073732718893, ... + 'YData',-48.24561403508771); +h2 = line('Parent',h1, ... + 'Color',[0 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-1.152073732718893, ... + 'YData',-47.953216374269); +h2 = line('Parent',h1, ... + 'Color',[0 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-1.843317972350228, ... + 'YData',-44.73684210526315); +h2 = line('Parent',h1, ... + 'Color',[0 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-1.843317972350228, ... + 'YData',-44.44444444444444); +h2 = line('Parent',h1, ... + 'Color',[0 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-2.304147465437787, ... + 'YData',-39.76608187134502); +h2 = line('Parent',h1, ... + 'Color',[0 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-2.764976958525345, ... + 'YData',-38.01169590643274); +h2 = line('Parent',h1, ... + 'Color',[0 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-3.225806451612904, ... + 'YData',-30.99415204678362); +h2 = line('Parent',h1, ... + 'Color',[0 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-3.225806451612904, ... + 'YData',-29.82456140350877); +h2 = line('Parent',h1, ... + 'Color',[0 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-3.225806451612904, ... + 'YData',-24.85380116959064); +h2 = line('Parent',h1, ... + 'Color',[0 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-3.225806451612904, ... + 'YData',-24.26900584795321); +h2 = line('Parent',h1, ... + 'Color',[0 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-2.534562211981566, ... + 'YData',-17.5438596491228); +h2 = line('Parent',h1, ... + 'Color',[0 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-2.304147465437787, ... + 'YData',-16.95906432748537); +h2 = line('Parent',h1, ... + 'Color',[0 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-1.612903225806452, ... + 'YData',-11.98830409356725); +h2 = line('Parent',h1, ... + 'Color',[0 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-1.612903225806452, ... + 'YData',-11.40350877192982); +h2 = line('Parent',h1, ... + 'Color',[0 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',mat8, ... + 'YData',-8.47953216374269); +h2 = line('Parent',h1, ... + 'Color',[0 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',mat9, ... + 'YData',-8.187134502923968); +h2 = line('Parent',h1, ... + 'Color',[0 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-1.152073732718893, ... + 'YData',-5.263157894736835); +h2 = line('Parent',h1, ... + 'Color',[0 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-1.152073732718893, ... + 'YData',-4.970760233918128); +h2 = line('Parent',h1, ... + 'Color',[0 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-0.9216589861751139, ... + 'YData',-2.923976608187132); +h2 = text('Parent',h1, ... + 'Color',[0 0 0], ... + 'Position',[-0.9216589861751139 -2.923976608187132 0], ... + 'String','100'); +h2 = line('Parent',h1, ... + 'Color',[0 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-0.9216589861751139, ... + 'YData',-2.631578947368411); +h2 = line('Parent',h1, ... + 'Color',[0 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-0.6912442396313345, ... + 'YData',mat10); +h2 = line('Parent',h1, ... + 'Color',[0 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-0.6912442396313345, ... + 'YData',-0.8771929824561369); +h2 = line('Parent',h1, ... + 'Color',[0 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-0.6912442396313345, ... + 'YData',-0.5847953216374293); +h2 = text('Parent',h1, ... + 'Color',[0 0 0], ... + 'HandleVisibility','off', ... + 'HorizontalAlignment','center', ... + 'Position',[19.80645161290322 12.0675105485232 17.32050807568877], ... + 'VerticalAlignment','bottom'); +set(get(h2,'Parent'),'Title',h2); +h1 = axes('Parent',h0, ... + 'CameraUpVector',[0 1 0], ... + 'Color',[1 1 1], ... + 'ColorOrder',mat11, ... + 'NextPlot','add', ... + 'Position',[0.5779768786127169 0.11 0.3270231213872832 0.8149999999999998], ... + 'XColor',[0 0 0], ... + 'YColor',[0 0 0], ... + 'ZColor',[0 0 0]); +h2 = line('Parent',h1, ... + 'Color',[0 0 1], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-0.4608294930875587, ... + 'YData',0); +h2 = text('Parent',h1, ... + 'Color',[0 0 0], ... + 'String','2'); +h2 = line('Parent',h1, ... + 'Color',[0 0 1], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-2.764976958525345, ... + 'YData',-0.2923976608187076); +h2 = line('Parent',h1, ... + 'Color',[0 0 1], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-3.456221198156683, ... + 'YData',-0.2923976608187076); +h2 = line('Parent',h1, ... + 'Color',[0 0 1], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-7.834101382488477, ... + 'YData',-0.2923976608187076); +h2 = line('Parent',h1, ... + 'Color',[0 0 1], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-11.52073732718894, ... + 'YData',-0.2923976608187076); +h2 = line('Parent',h1, ... + 'Color',[0 0 1], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',mat12, ... + 'YData',-0.2923976608187076); +h2 = line('Parent',h1, ... + 'Color',[0 0 1], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-19.35483870967742, ... + 'YData',0.2923976608187076); +h2 = line('Parent',h1, ... + 'Color',[0 0 1], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-23.50230414746544, ... + 'YData',0.2923976608187076); +h2 = line('Parent',h1, ... + 'Color',[0 0 1], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-24.88479262672811, ... + 'YData',0.8771929824561369); +h2 = line('Parent',h1, ... + 'Color',[0 0 1], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-28.11059907834102, ... + 'YData',0.8771929824561369); +h2 = line('Parent',h1, ... + 'Color',[0 0 1], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-29.49308755760369, ... + 'YData',1.461988304093566); +h2 = line('Parent',h1, ... + 'Color',[0 0 1], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-31.10599078341014, ... + 'YData',1.461988304093566); +h2 = line('Parent',h1, ... + 'Color',[0 0 1], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-32.02764976958525, ... + 'YData',1.461988304093566); +h2 = line('Parent',h1, ... + 'Color',[0 0 1], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-33.17972350230414, ... + 'YData',1.461988304093566); +h2 = line('Parent',h1, ... + 'Color',[0 0 1], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-33.6405529953917, ... + 'YData',1.461988304093566); +h2 = line('Parent',h1, ... + 'Color',[0 0 1], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-34.7926267281106, ... + 'YData',1.461988304093566); +h2 = line('Parent',h1, ... + 'Color',[0 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-35.02304147465438, ... + 'YData',1.461988304093566); +h2 = line('Parent',h1, ... + 'Color',[0 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-35.48387096774194, ... + 'YData',1.461988304093566); +h2 = line('Parent',h1, ... + 'Color',[0 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-35.71428571428572, ... + 'YData',1.461988304093566); +h2 = text('Parent',h1, ... + 'Color',[0 0 0], ... + 'Position',[-35.71428571428572 1.461988304093566 0], ... + 'String','20'); +h2 = line('Parent',h1, ... + 'Color',[0 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-36.17511520737327, ... + 'YData',mat13); +h2 = line('Parent',h1, ... + 'Color',[0 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-36.40552995391705, ... + 'YData',0.8771929824561369); +h2 = line('Parent',h1, ... + 'Color',[0 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-36.63594470046083, ... + 'YData',0.2923976608187076); +h2 = line('Parent',h1, ... + 'Color',[0 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-36.63594470046083, ... + 'YData',0); +h2 = line('Parent',h1, ... + 'Color',[0 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-36.63594470046083, ... + 'YData',mat14); +h2 = line('Parent',h1, ... + 'Color',[0 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-36.40552995391705, ... + 'YData',-2.339181286549703); +h2 = line('Parent',h1, ... + 'Color',[0 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-36.40552995391705, ... + 'YData',-2.631578947368425); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-36.40552995391705, ... + 'YData',-4.67836257309942); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-36.40552995391705, ... + 'YData',-5.555555555555557); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-36.40552995391705, ... + 'YData',-8.187134502923982); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-36.40552995391705, ... + 'YData',-8.771929824561397); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-36.86635944700461, ... + 'YData',-13.15789473684211); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-37.09677419354839, ... + 'YData',mat15); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-37.09677419354839, ... + 'YData',-16.08187134502924); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-37.09677419354839, ... + 'YData',-17.54385964912281); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-37.09677419354839, ... + 'YData',-18.12865497076023); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-37.32718894009217, ... + 'YData',-19.88304093567251); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-37.32718894009217, ... + 'YData',-20.17543859649123); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-37.09677419354839, ... + 'YData',-21.92982456140351); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-37.09677419354839, ... + 'YData',-22.22222222222222); +h2 = text('Parent',h1, ... + 'Color',[0 0 0], ... + 'Position',[-37.09677419354839 -22.22222222222222 0], ... + 'String','40'); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-37.09677419354839, ... + 'YData',-23.09941520467836); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-37.09677419354839, ... + 'YData',-23.39181286549707); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-37.32718894009217, ... + 'YData',-25.14619883040935); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-37.32718894009217, ... + 'YData',-25.43859649122807); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-37.32718894009217, ... + 'YData',-28.3625730994152); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-37.55760368663595, ... + 'YData',-28.94736842105263); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-37.78801843317973, ... + 'YData',-31.87134502923976); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-37.78801843317973, ... + 'YData',-32.16374269005848); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-37.78801843317973, ... + 'YData',-34.7953216374269); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-37.78801843317973, ... + 'YData',-35.38011695906432); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-37.78801843317973, ... + 'YData',-38.88888888888889); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-37.78801843317973, ... + 'YData',-39.76608187134503); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-37.78801843317973, ... + 'YData',-43.27485380116958); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-37.78801843317973, ... + 'YData',-43.5672514619883); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-37.78801843317973, ... + 'YData',-44.44444444444444); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-37.55760368663595, ... + 'YData',-45.32163742690058); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-37.55760368663595, ... + 'YData',-45.61403508771929); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-37.32718894009217, ... + 'YData',-47.36842105263158); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-37.32718894009217, ... + 'YData',-47.95321637426901); +h2 = line('Parent',h1, ... + 'Color',[0 1 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-36.86635944700461, ... + 'YData',-49.70760233918129); +h2 = text('Parent',h1, ... + 'Color',[0 0 0], ... + 'Position',[-36.86635944700461 -49.70760233918129 0], ... + 'String','60'); +h2 = line('Parent',h1, ... + 'Color',[1 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-36.86635944700461, ... + 'YData',-50); +h2 = line('Parent',h1, ... + 'Color',[1 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-35.71428571428572, ... + 'YData',-50.29239766081872); +h2 = line('Parent',h1, ... + 'Color',[1 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-35.25345622119816, ... + 'YData',-50.29239766081872); +h2 = line('Parent',h1, ... + 'Color',[1 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-32.02764976958527, ... + 'YData',-50.29239766081872); +h2 = line('Parent',h1, ... + 'Color',[1 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-31.33640552995393, ... + 'YData',-50.29239766081872); +h2 = line('Parent',h1, ... + 'Color',[1 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-27.88018433179725, ... + 'YData',-50.58479532163743); +h2 = line('Parent',h1, ... + 'Color',[1 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-27.41935483870969, ... + 'YData',-50.58479532163743); +h2 = line('Parent',h1, ... + 'Color',[1 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-18.20276497695854, ... + 'YData',-50.58479532163743); +h2 = line('Parent',h1, ... + 'Color',[1 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-16.82027649769586, ... + 'YData',-51.16959064327486); +h2 = line('Parent',h1, ... + 'Color',[1 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-12.21198156682029, ... + 'YData',-50.58479532163743); +h2 = line('Parent',h1, ... + 'Color',[1 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-11.52073732718895, ... + 'YData',-50.58479532163743); +h2 = line('Parent',h1, ... + 'Color',[1 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-6.912442396313377, ... + 'YData',-51.16959064327486); +h2 = line('Parent',h1, ... + 'Color',[1 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-5.069124423963142, ... + 'YData',-51.75438596491229); +h2 = line('Parent',h1, ... + 'Color',[1 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',mat16, ... + 'YData',-52.046783625731); +h2 = line('Parent',h1, ... + 'Color',[1 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',-0.9216589861751281, ... + 'YData',-52.33918128654972); +h2 = line('Parent',h1, ... + 'Color',[1 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',0.2304147465437687, ... + 'YData',-52.33918128654972); +h2 = line('Parent',h1, ... + 'Color',[1 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',0.4608294930875481, ... + 'YData',-52.33918128654972); +h2 = line('Parent',h1, ... + 'Color',[1 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',2.304147465437776, ... + 'YData',-52.63157894736843); +h2 = line('Parent',h1, ... + 'Color',[1 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',2.534562211981548, ... + 'YData',-52.63157894736843); +h2 = line('Parent',h1, ... + 'Color',[1 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',3.917050691244224, ... + 'YData',-52.63157894736843); +h2 = text('Parent',h1, ... + 'Color',[0 0 0], ... + 'Position',[3.917050691244224 -52.63157894736843 0], ... + 'String','80'); +h2 = line('Parent',h1, ... + 'Color',[0 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',4.147465437788011, ... + 'YData',-52.63157894736843); +h2 = line('Parent',h1, ... + 'Color',[0 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',4.147465437788011, ... + 'YData',-52.33918128654972); +h2 = line('Parent',h1, ... + 'Color',[0 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',5.529953917050673, ... + 'YData',-46.19883040935674); +h2 = line('Parent',h1, ... + 'Color',[0 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',5.990783410138231, ... + 'YData',-44.44444444444446); +h2 = line('Parent',h1, ... + 'Color',[0 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',7.834101382488466, ... + 'YData',-28.0701754385965); +h2 = line('Parent',h1, ... + 'Color',[0 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',8.294930875576025, ... + 'YData',-22.80701754385966); +h2 = line('Parent',h1, ... + 'Color',[0 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',8.755760368663584, ... + 'YData',mat17); +h2 = line('Parent',h1, ... + 'Color',[0 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',8.525345622119797, ... + 'YData',-14.9122807017544); +h2 = line('Parent',h1, ... + 'Color',[0 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',7.373271889400908, ... + 'YData',-10.23391812865498); +h2 = line('Parent',h1, ... + 'Color',[0 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',7.142857142857135, ... + 'YData',-9.94152046783627); +h2 = line('Parent',h1, ... + 'Color',[0 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',6.221198156682018, ... + 'YData',-7.602339181286567); +h2 = line('Parent',h1, ... + 'Color',[0 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',6.221198156682018, ... + 'YData',-7.309941520467845); +h2 = line('Parent',h1, ... + 'Color',[0 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',5.760368663594459, ... + 'YData',-5.555555555555571); +h2 = line('Parent',h1, ... + 'Color',[0 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',5.529953917050673, ... + 'YData',-5.555555555555571); +h2 = line('Parent',h1, ... + 'Color',[0 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',5.2995391705069, ... + 'YData',-4.093567251462005); +h2 = line('Parent',h1, ... + 'Color',[0 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',5.069124423963128, ... + 'YData',-2.631578947368439); +h2 = line('Parent',h1, ... + 'Color',[0 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',5.069124423963128, ... + 'YData',-2.339181286549717); +h2 = line('Parent',h1, ... + 'Color',[0 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',5.069124423963128, ... + 'YData',mat18); +h2 = line('Parent',h1, ... + 'Color',[0 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',5.069124423963128, ... + 'YData',-1.169590643274873); +h2 = line('Parent',h1, ... + 'Color',[0 0 0], ... + 'LineStyle','none', ... + 'Marker','.', ... + 'XData',4.838709677419342, ... + 'YData',-0.2923976608187218); +h2 = text('Parent',h1, ... + 'Color',[0 0 0], ... + 'Position',[4.838709677419342 -0.2923976608187218 0], ... + 'String','100'); +h2 = text('Parent',h1, ... + 'Color',[0 0 0], ... + 'HandleVisibility','off', ... + 'HorizontalAlignment','center', ... + 'Position',[-10.38961038961038 12.0675105485232 17.32050807568877], ... + 'VerticalAlignment','bottom'); +set(get(h2,'Parent'),'Title',h2); +if nargout > 0, fig = h0; end diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/test_square_fig.mat b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/test_square_fig.mat new file mode 100644 index 00000000..5b2b5f53 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Square/test_square_fig.mat Binary files differdiff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/abcd_hhmm.m b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/abcd_hhmm.m new file mode 100644 index 00000000..13e6d39f --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/abcd_hhmm.m @@ -0,0 +1,97 @@ +% Make the HHMM in Figure 1 of the NIPS'01 paper + +Qsize = [2 3 2]; +Qnodes = 1:3; +D = 3; +transprob = cell(1,D); +termprob = cell(1,D); +startprob = cell(1,D); +clear A; + +% transprob{d}(i,k,j), transprob{1}(i,j) +% termprob{d}(k,j), termprob{1}(1,j) +% startprob{d}(k,j), startprob{1}(1,j) + + +% LEVEL 1 + +% 1 2 e +A{1} = [0 0 1; + 0 0 1]; +[transprob{1}, termprob{1}] = remove_hhmm_end_state(A{1}); +startprob{1} = [0.5 0.5]; + +% LEVEL 2 +A{2} = zeros(Qsize(2), Qsize(1), Qsize(2)+1); + +% 1 2 3 e +A{2}(:,1,:) = [0 1 0 0 % Q1=1 => model below state 0 + 0 0 1 0 + 0 0 0 1]; + +% 1 2 3 e +A{2}(:,2,:) = [0 1 0 0 % Q1=2 => model below state 1 + 0 0 1 0 + 0 0 0 1]; + +[transprob{2}, termprob{2}] = remove_hhmm_end_state(A{2}); + +% always enter level 2 in state 1 +startprob{2} = [1 0 0 + 1 0 0]; + +% LEVEL 3 + +A{3} = zeros([Qsize(3) Qsize(2) Qsize(3)+1]); +endstate = Qsize(3)+1; +% Qt-1(3) Qt(2) Qt(3) +% 1 2 e +A{3}(1, 1, endstate) = 1.0; % Q2=1 => model below state 2/5 +A{3}(:, 2, :) = [0.0 1.0 0.0 % Q2=2 => model below state 3/6 + 0.5 0.0 0.5]; +A{3}(1, 3, endstate) = 1.0; % Q2=3 => model below state 4/7 + +[transprob{3}, termprob{3}] = remove_hhmm_end_state(A{3}); + +startprob{3} = 'leftstart'; + + + +% OBS LEVEl + +chars = ['a', 'b', 'c', 'd', 'x', 'y']; +Osize = length(chars); + +obsprob = zeros([Qsize Osize]); +% 1 2 3 O +obsprob(1,1,1,find(chars == 'a')) = 1.0; + +obsprob(1,2,1,find(chars == 'x')) = 1.0; +obsprob(1,2,2,find(chars == 'y')) = 1.0; + +obsprob(1,3,1,find(chars == 'b')) = 1.0; + +obsprob(2,1,1,find(chars == 'c')) = 1.0; + +obsprob(2,2,1,find(chars == 'x')) = 1.0; +obsprob(2,2,2,find(chars == 'y')) = 1.0; + +obsprob(2,3,1,find(chars == 'd')) = 1.0; + +Oargs = {'CPT', obsprob}; + +bnet = mk_hhmm('Qsizes', Qsize, 'Osize', Osize, 'discrete_obs', 1, ... + 'Oargs', Oargs, 'Ops', Qnodes(1:3), ... + 'startprob', startprob, 'transprob', transprob, 'termprob', termprob); + + +Q1 = 1; Q2 = 2; Q3 = 3; F3 = 4; F2 = 5; Onode = 6; +Qnodes = [Q1 Q2 Q3]; Fnodes = [F2 F3]; + +for seqi=1:3 + evidence = sample_dbn(bnet, 'stop_test', 'is_F2_true_D3'); + ev = cell2num(evidence); + chars(ev(end,:)) + %T = size(evidence, 2) + %pretty_print_hhmm_parse(evidence, Qnodes, Fnodes, Onode, chars); +end diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/add_hhmm_end_state.m b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/add_hhmm_end_state.m new file mode 100644 index 00000000..84c3c653 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/add_hhmm_end_state.m @@ -0,0 +1,34 @@ +function A = add_hhmm_end_state(transprob, termprob) +% ADD_HMM_END_STATE Combine trans and term probs into transmat for automaton with an end state +% function A = add_hhmm_end_state(transprob, termprob) +% +% A(i,k,j) = Pr( i->j | Qps=k), where i in 1:Q, j in 1:(Q+1), and Q+1 is the end state +% This implements the equation in sec 4.6 of my tech report, where +% transprob(i,k,j) = \tilde{A}_k(i,j), termprob(k,j) = \tau_k(j) +% +% For the top level, the k index is missing. + +Q = size(transprob,1); +toplevel = (ndims(transprob)==2); +if toplevel + Qk = 1; + transprob = reshape(transprob, [Q 1 Q]); + termprob = reshape(termprob, [1 Q]); +else + Qk = size(transprob, 2); +end + +A = zeros(Q, Qk, Q+1); +A(:,:,Q+1) = termprob'; + +for k=1:Qk + for i=1:Q + for j=1:Q + A(i,k,j) = transprob(i,k,j) * (1-termprob(k,i)); + end + end +end + +if toplevel + A = squeeze(A); +end diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/hhmm_jtree_clqs.m b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/hhmm_jtree_clqs.m new file mode 100644 index 00000000..4192af11 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/hhmm_jtree_clqs.m @@ -0,0 +1,143 @@ +% Find out how big the cliques are in an HHMM as a function of depth +% (This is how we get the complexity bound of O(D K^{1.5D}).) + +if 0 +Qsize = []; +Fsize = []; +Nclqs = []; +end + +ds = 1:15; + +for d = ds + allQ = 1; + [intra, inter, Qnodes, Fnodes, Onode] = mk_hhmm_topo(d, allQ); + + N = length(intra); + ns = 2*ones(1,N); + + bnet = mk_dbn(intra, inter, ns); + for i=1:N + bnet.CPD{i} = tabular_CPD(bnet, i); + end + + if 0 + T = 5; + dag = unroll_dbn_topology(intra, inter, T); + engine = jtree_unrolled_dbn_inf_engine(bnet, T, 'constrained', 1); + S = struct(engine); + S1 = struct(S.sub_engine); + end + + engine = jtree_dbn_inf_engine(bnet); + S = struct(engine); + J = S.jtree_struct; + + ss = 2*d+1; + Qnodes2 = Qnodes + ss; + QQnodes = [Qnodes Qnodes2]; + + % find out how many Q nodes in each clique, and how many F nodes + C = length(J.cliques); + Nclqs(d) = 0; + for c=1:C + Qsize(c,d) = length(myintersect(J.cliques{c}, QQnodes)); + Fsize(c,d) = length(myintersect(J.cliques{c}, Fnodes)); + if length(J.cliques{c}) > 1 % exclude observed leaves + Nclqs(d) = Nclqs(d) + 1; + end + end + %pred_max_Qsize(d) = ceil(d+(d+1)/2); + pred_max_Qsize(d) = ceil(1.5*d); + + fprintf('d=%d\n', d); + %fprintf('D=%d, max F = %d. max Q = %d, pred max Q = %d\n', ... + % D, max(Fsize), max(Qsize), ceil(D+(D+1)/2)); + + %histc(Qsize,1:max(Qsize)) % how many of each size? +end % next d + + +Q = 2; +pred_mass = ds.*(Q.^ds) + Q.^(ceil(1.5 * ds)) +pred_mass2 = Q.^(ceil(1.5 * ds)) + +for d=ds + mass(d) = 0; + for c=1:C + mass(d) = mass(d) + Q^Qsize(c,d); + end +end + + +if 0 +%plot(ds, max(Qsize), 'o-', ds, pred_max_Qsize, '*--'); +%plot(ds, max(Qsize), 'o-', ds, 1.5*ds, '*--'); +%plot(ds, mass, 'o-', ds, pred_mass, '*--'); +D = 15; +%plot(ds(1:D), mass(1:D), 'bo-', ds(1:D), pred_mass(1:D), 'g*--', ds(1:D), pred_mass2(1:D), 'k+-.'); +plot(ds(1:D), log(mass(1:D)), 'bo-', ds(1:D), log(pred_mass(1:D)), 'g*--', ds(1:D), log(pred_mass2(1:D)), 'k+-.'); + +grid on +xlabel('depth of hierarchy') +title('max num Q nodes in any clique vs. depth') +legend('actual', 'predicted') + +%previewfig(gcf, 'width', 3, 'height', 1.5, 'color', 'bw'); +%exportfig(gcf, '/home/cs/murphyk/WP/ConferencePapers/HHMM/clqsize2.eps', ... +% 'width', 3, 'height', 1.5, 'color', 'bw'); + +end + + +if 0 +for d=ds + effnumclqs(d) = length(find(Qsize(:,d)>0)); +end +ds = 1:10; +Qs = 2:10; +maxC = size(Qsize, 1); +cost = []; +cost_bound = []; +for qi=1:length(Qs) + Q = Qs(qi); + for d=ds + cost(d,qi) = 0; + for c=1:maxC + if length(Qsize(c,d) > 0) % this clique contains Q nodes + cost(d,qi) = cost(d,qi) + Q^Qsize(c,d)*2^Fsize(c,d); + end + end + %cost_bound(d,qi) = effnumclqs(d) * 8 * Q^(max(Qsize(:,d))); + cost_bound(d,qi) = (effnumclqs(d)*8) + Q^(max(Qsize(:,d))); + end +end + +qi=2; plot(ds, cost(:,qi), 'o-', ds, cost_bound(:,qi), '*--'); +end + + +if 0 +% convert numbers in cliques into names +for d=1:D + Fdecode(Fnodes(d)) = d; +end +for c=8:15 + clqs = J.cliques{c}; + fprintf('clique %d: ', c); + for k=clqs + if myismember(k, Qnodes) + fprintf('Q%d ', k) + elseif myismember(k, Fnodes) + fprintf('F%d ', Fdecode(k)) + elseif isequal(k, Onode) + fprintf('O ') + elseif myismember(k, Qnodes2) + fprintf('Q%d* ', k-ss) + else + error(['unrecognized node ' k]) + end + end + fprintf('\n'); +end +end diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/mk_hhmm.m b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/mk_hhmm.m new file mode 100644 index 00000000..85ff7f6a --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/mk_hhmm.m @@ -0,0 +1,258 @@ +function [bnet, Qnodes, Fnodes, Onode] = mk_hhmm(varargin) +% MK_HHMM Make a Hierarchical HMM +% function [bnet, Qnodes, Fnodes, Onode] = mk_hhmm(...) +% +% e.g. 3-layer hierarchical HMM where level 1 only connects to level 2 +% and the parents of the observed node are levels 2 and 3. +% (This DBN is the same as Fig 10 in my tech report.) +% +% Q1 ----------> Q1 +% | \ ^ | +% | v / | +% | F2 ------/ | +% | ^ ^ \ | +% | / | \ | +% | / | || +% v | vv +% Q2----| --------> Q2 +% /| \ | ^| +% / | v | / | +% | | F3 --------/ | +% | | ^ \ | +% | v / v v +% | Q3 -----------> Q3 +% | | +% \ | +% v v +% O +% +% +% Optional arguments in name/value format [default value in brackets] +% +% Qsizes - sizes at each level [ none ] +% allQ - 1 means level i connects to all Q levels below, 0 means just to i+1 [0] +% transprob - transprob{d}(i,k,j) = P(Q(d,t)=j|Q(d,t-1)=i,Q(1:d-1,t)=k) ['leftright'] +% startprob - startprob{d}(k,j) = P(Q(d,t)=j|Q(1:d-1,t)=k) ['leftstart'] +% termprob - termprob{d}(k,j) = P(F(d,t)=2|Q(1:d-1,t)=k,Q(d,t)=j) for d>1 ['rightstop'] +% selfprop - prob of a self transition (termprob default = 1-selfprop) [0.8] +% Osize - size of O node +% discrete_obs - 1 means O is tabular_CPD, 0 means gaussian_CPD [0] +% Oargs - cell array of args to pass to the O CPD [ {} ] +% Ops - Q parents of O [Qnodes(end)] +% F1 - 1 means level 1 can finish (restart), else there is no F1->Q1 arc [0] +% clamp1 - 1 means we clamp the params of the Q nodes in slice 1 (Qt1params) [1] +% Note: the Qt1params are startprob, which should be shared with other slices. +% However, in the current implementation, the Qt1params will only be estimated +% from the initial state of each sequence. +% +% For d=1, startprob{1}(1,j) is only used in the first slice and +% termprob{1} is ignored, since we assume the top level never resets. +% Also, transprob{1}(i,j) can be used instead of transprob{1}(i,1,j). +% +% leftstart means the model always starts in state 1. +% rightstop means the model can only finish in its last state (Qsize(d)). +% unif means each state is equally like to reach any other +% rnd means the transition/starting probs are random (drawn from rand) +% +% Q1:QD in slice 1 are of type tabular_CPD +% Q1:QD in slice 2 are of type hhmmQ_CPD. +% F(2:D-1) is of type hhmmF_CPD, FD is of type tabular_CPD. + +args = varargin; +nargs = length(args); + +% get sizes of nodes and topology +Qsizes = []; +Osize = []; +allQ = 0; +Ops = []; +F1 = 0; +for i=1:2:nargs + switch args{i}, + case 'Qsizes', Qsizes = args{i+1}; + case 'Osize', Osize = args{i+1}; + case 'allQ', allQ = args{i+1}; + case 'Ops', Ops = args{i+1}; + case 'F1', F1 = args{i+1}; + end +end +if isempty(Qsizes), error('must specify Qsizes'); end +if Osize==0, error('must specify Osize'); end +D = length(Qsizes); +Qnodes = 1:D; + +if isempty(Ops), Ops = Qnodes(end); end + + +[intra, inter, Qnodes, Fnodes, Onode] = mk_hhmm_topo(D, allQ, Ops, F1); +ss = length(intra); +names = {}; + +if F1 + Fnodes_ndx = Fnodes; +else + Fnodes_ndx = [-1 Fnodes]; % Fnodes(1) is a dummy index +end + +% set default params +discrete_obs = 0; +Oargs = {}; +startprob = cell(1,D); +startprob{1} = 'unif'; +for d=2:D + startprob{d} = 'leftstart'; +end +transprob = cell(1,D); +transprob{1} = 'unif'; +for d=2:D + transprob{d} = 'leftright'; +end +termprob = cell(1,D); +for d=2:D + termprob{d} = 'rightstop'; +end +selfprob = 0.8; +clamp1 = 1; + +for i=1:2:nargs + switch args{i}, + case 'discrete_obs', discrete_obs = args{i+1}; + case 'Oargs', Oargs = args{i+1}; + case 'startprob', startprob = args{i+1}; + case 'transprob', transprob = args{i+1}; + case 'termprob', termprob = args{i+1}; + case 'selfprob', selfprob = args{i+1}; + case 'clamp1', clamp1 = args{i+1}; + end +end + +ns = zeros(1,ss); +ns(Qnodes) = Qsizes; +ns(Onode) = Osize; +ns(Fnodes) = 2; + +dnodes = [Qnodes Fnodes]; +if discrete_obs + dnodes = [dnodes Onode]; +end +onodes = [Onode]; + +bnet = mk_dbn(intra, inter, ns, 'observed', onodes, 'discrete', dnodes, 'names', names); +eclass = bnet.equiv_class; + +for d=1:D + if d==1 + Qps = []; + elseif allQ + Qps = Qnodes(1:d-1); + else + Qps = Qnodes(d-1); + end + Qpsz = prod(ns(Qps)); + Qsz = ns(Qnodes(d)); + if isstr(startprob{d}) + switch startprob{d} + case 'unif', startprob{d} = mk_stochastic(ones(Qpsz, Qsz)); + case 'rnd', startprob{d} = mk_stochastic(rand(Qpsz, Qsz)); + case 'leftstart', startprob{d} = zeros(Qpsz, Qsz); startprob{d}(:,1) = 1; + end + end + if isstr(transprob{d}) + switch transprob{d} + case 'unif', transprob{d} = mk_stochastic(ones(Qsz, Qpsz, Qsz)); + case 'rnd', transprob{d} = mk_stochastic(rand(Qsz, Qpsz, Qsz)); + case 'leftright', + LR = mk_leftright_transmat(Qsz, selfprob); + temp = repmat(reshape(LR, [1 Qsz Qsz]), [Qpsz 1 1]); % transprob(k,i,j) + transprob{d} = permute(temp, [2 1 3]); % now transprob(i,k,j) + end + end + if isstr(termprob{d}) + switch termprob{d} + case 'unif', termprob{d} = mk_stochastic(ones(Qpsz, Qsz, 2)); + case 'rnd', termprob{d} = mk_stochastic(rand(Qpsz, Qsz, 2)); + case 'rightstop', + %termprob(k,i,t) Might terminate if i=Qsz; will not terminate if i<Qsz + stopprob = 1-selfprob; + termprob{d} = zeros(Qpsz, Qsz, 2); + termprob{d}(:,Qsz,2) = stopprob; + termprob{d}(:,Qsz,1) = 1-stopprob; + termprob{d}(:,1:(Qsz-1),1) = 1; + otherwise, error(['unrecognized termprob ' termprob{d}]) + end + elseif d>1 % passed in termprob{d}(k,j) + temp = termprob{d}; + termprob{d} = zeros(Qpsz, Qsz, 2); + termprob{d}(:,:,2) = temp; + termprob{d}(:,:,1) = ones(Qpsz,Qsz) - temp; + end +end + + +% SLICE 1 + +for d=1:D + bnet.CPD{eclass(Qnodes(d),1)} = tabular_CPD(bnet, Qnodes(d), 'CPT', startprob{d}, 'adjustable', clamp1); +end + +if F1 + d = 1; + bnet.CPD{eclass(Fnodes_ndx(d),1)} = hhmmF_CPD(bnet, Fnodes_ndx(d), Qnodes(d), Fnodes_ndx(d+1), ... + 'termprob', termprob{d}); +end +for d=2:D-1 + if allQ + Qps = Qnodes(1:d-1); + else + Qps = Qnodes(d-1); + end + bnet.CPD{eclass(Fnodes_ndx(d),1)} = hhmmF_CPD(bnet, Fnodes_ndx(d), Qnodes(d), Fnodes_ndx(d+1), ... + 'Qps', Qps, 'termprob', termprob{d}); +end +bnet.CPD{eclass(Fnodes_ndx(D),1)} = tabular_CPD(bnet, Fnodes_ndx(D), 'CPT', termprob{D}); + +if discrete_obs + bnet.CPD{eclass(Onode,1)} = tabular_CPD(bnet, Onode, Oargs{:}); +else + bnet.CPD{eclass(Onode,1)} = gaussian_CPD(bnet, Onode, Oargs{:}); +end + +% SLICE 2 + +%for d=1:D +% bnet.CPD{eclass(Qnodes(d),2)} = hhmmQ_CPD(bnet, Qnodes(d)+ss, Qnodes, d, D, ... +% 'startprob', startprob{d}, 'transprob', transprob{d}, ... +% 'allQ', allQ); +%end + +d = 1; +if F1 + bnet.CPD{eclass(Qnodes(d),2)} = hhmmQ_CPD(bnet, Qnodes(d)+ss, 'Fself', Fnodes_ndx(d), ... + 'Fbelow', Fnodes_ndx(d+1), ... + 'startprob', startprob{d}, 'transprob', transprob{d}); +else + bnet.CPD{eclass(Qnodes(d),2)} = hhmmQ_CPD(bnet, Qnodes(d)+ss, ... + 'Fbelow', Fnodes_ndx(d+1), ... + 'startprob', startprob{d}, 'transprob', transprob{d}); +end +for d=2:D-1 + if allQ + Qps = Qnodes(1:d-1); + else + Qps = Qnodes(d-1); + end + Qps = Qps + ss; % since all in slice 2 + bnet.CPD{eclass(Qnodes(d),2)} = hhmmQ_CPD(bnet, Qnodes(d)+ss, 'Fself', Fnodes_ndx(d), ... + 'Fbelow', Fnodes_ndx(d+1), 'Qps', Qps, ... + 'startprob', startprob{d}, 'transprob', transprob{d}); +end +d = D; +if allQ + Qps = Qnodes(1:d-1); +else + Qps = Qnodes(d-1); +end +Qps = Qps + ss; % since all in slice 2 +bnet.CPD{eclass(Qnodes(d),2)} = hhmmQ_CPD(bnet, Qnodes(d)+ss, 'Fself', Fnodes_ndx(d), ... + 'Qps', Qps, ... + 'startprob', startprob{d}, 'transprob', transprob{d}); diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/mk_hhmm_topo.m b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/mk_hhmm_topo.m new file mode 100644 index 00000000..7a5fe57c --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/mk_hhmm_topo.m @@ -0,0 +1,76 @@ +function [intra, inter, Qnodes, Fnodes, Onode] = mk_hhmm_topo(D, all_Q_to_Qs, Ops, F1) +% MK_HHMM_TOPO Make Hierarchical HMM topology +% function [intra, inter, Qnodes, Fnodes, Onode] = mk_hhmm_topo(D, all_Q_to_Qs, Ops, F1) +% +% D is the depth of the hierarchy +% If all_Q_to_Qs = 1, level i connects to all levels below, else just to i+1 [0] +% Ops are the Q parents of the observed node [Qnodes(end)] +% If F1=1, level 1 can finish (restart), else there is no F1->Q1 arc [0] + +Qnodes = 1:D; + +if nargin < 2, all_Q_to_Qs = 1; end +if nargin < 3, Ops = Qnodes(D); end +if nargin < 4, F1 = 0; end + +if F1 + Fnodes = 2*D:-1:D+1; % must number from bottom to top + Onode = 2*D+1; + ss = 2*D+1; +else + Fnodes = [-1 (2*D)-1:-1:D+1]; % Fnodes(1) is a dummy index + Onode = 2*D; + ss = 2*D; +end + +intra = zeros(ss); +intra(Ops, Onode) = 1; +for d=1:D-1 + if all_Q_to_Qs + intra(Qnodes(d), Qnodes(d+1:end)) = 1; + else + intra(Qnodes(d), Qnodes(d+1)) = 1; + end +end +for d=D:-1:3 + intra(Fnodes(d), Fnodes(d-1)) = 1; +end +if F1 + intra(Fnodes(2), Fnodes(1)) = 1; +end +if all_Q_to_Qs + if F1 + intra(Qnodes(1), Fnodes(1:end)) = 1; + else + intra(Qnodes(1), Fnodes(2:end)) = 1; + end + for d=2:D + intra(Qnodes(d), Fnodes(d:end)) = 1; + end +else + if F1 + intra(Qnodes(1), Fnodes([1 2])) = 1; + else + intra(Qnodes(1), Fnodes(2)) = 1; + end + for d=2:D-1 + intra(Qnodes(d), Fnodes([d d+1])) = 1; + end + intra(Qnodes(D), Fnodes(D)) = 1; +end + + +inter = zeros(ss); +for d=1:D + inter(Qnodes(d), Qnodes(d)) = 1; +end +if F1 + inter(Fnodes(1), Qnodes(1)) = 1; +end +for d=2:D + inter(Fnodes(d), Qnodes([d-1 d])) = 1; +end + +if ~F1 + Fnodes = Fnodes(2:end); % strip off dummy -1 term +end diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/mk_hhmm_topo_F1.m b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/mk_hhmm_topo_F1.m new file mode 100644 index 00000000..2fc5f912 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/mk_hhmm_topo_F1.m @@ -0,0 +1,65 @@ +function [intra, inter, Qnodes, Fnodes, Onode] = mk_hhmm_topo_F1(D, all_Q_to_Qs, Ops) +% MK_HHMM_TOPO Make Hierarchical HMM topology assuming level 1 can finish +% function [intra, inter, Qnodes, Fnodes, Onode] = mk_hhmm_topo(D, all_Q_to_Qs, Ops, F1) +% +% D is the depth of the hierarchy +% If all_Q_to_Qs = 1, level i connects to all levels below, else just to i+1 [0] +% Ops are the Q parents of the observed node [Qnodes(end)] +% If F1=1, level 1 can finish (restart), else there is no F1->Q1 arc [0] + +Qnodes = 1:D; + +if nargin < 2, all_Q_to_Qs = 1; end +if nargin < 3, Ops = Qnodes(D); end +if nargin < 4, F1 = 0; end + +if F1 + Fnodes = 2*D:-1:D+1; % must number from bottom to top + Onode = 2*D+1; + ss = 2*D+1; +else + Fnodes = (2*D)-1:-1:D+1; + Onode = 2*D; + ss = 2*D; +end + +intra = zeros(ss); +intra(Ops, Onode) = 1; +for d=1:D-1 + if all_Q_to_Qs + intra(Qnodes(d), Qnodes(d+1:end)) = 1; + else + intra(Qnodes(d), Qnodes(d+1)) = 1; + end +end +for d=D:-1:3 + intra(Fnodes(d), Fnodes(d-1)) = 1; +end +if F1 + intra(Fnodes(2), Fnodes(1)) = 1; +end +if all_Q_to_Qs + for d=1:D + intra(Qnodes(d), Fnodes(d:end)) = 1; + end +else + for d=1:D + if d < D + intra(Qnodes(d), Fnodes([d d+1])) = 1; + else + intra(Qnodes(d), Fnodes(d)) = 1; + end + end +end + +inter = zeros(ss); +for d=1:D + inter(Qnodes(d), Qnodes(d)) = 1; +end +for d=1:D + if d==1 + inter(Fnodes(d), Qnodes(d)) = 1; + else + inter(Fnodes(d), Qnodes([d-1 d])) = 1; + end +end diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/pretty_print_hhmm_parse.m b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/pretty_print_hhmm_parse.m new file mode 100644 index 00000000..81b965d5 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/pretty_print_hhmm_parse.m @@ -0,0 +1,67 @@ +function pretty_print_hhmm_parse(mpe, Qnodes, Fnodes, Onode, alphabet) +% function pretty_print_hhmm_parse(mpe, Qnodes, Fnodes, Onode, alphabet) +% +% mpe(i,t) is the most probable value of node i at time t +% Qnodes(1:D), Fnodes = [F2 .. FD], Onode contain the node ids +% alphabet(i) is the i'th output symbol, or [] if don't want displayed + +T = size(mpe,2); +ncols = 20; +t1 = 1; t2 = min(T, t1+ncols-1); +while (t1 < T) + %fprintf('%d:%d\n', t1, t2); + if iscell(mpe) + print_block_cell(mpe(:,t1:t2), Qnodes, Fnodes, Onode, alphabet, t1); + else + print_block(mpe(:,t1:t2), Qnodes, Fnodes, Onode, alphabet, t1); + end + fprintf('\n\n'); + t1 = t2+1; t2 = min(T, t1+ncols-1); +end + +%%%%%% + +function print_block_cell(mpe, Qnodes, Fnodes, Onode, alphabet, start) + +D = length(Qnodes); +T = size(mpe, 2); +fprintf('%3d ', start:start+T-1); fprintf('\n'); +for d=1:D + for t=1:T + if (d > 1) & (mpe{Fnodes(d-1),t} == 2) + fprintf('%3d|', mpe{Qnodes(d), t}); + else + fprintf('%3d ', mpe{Qnodes(d), t}); + end + end + fprintf('\n'); +end +if ~isempty(alphabet) + a = cell2num(mpe(Onode,:)); + %fprintf('%3c ', alphabet(mpe{Onode,:})); + fprintf('%3c ', alphabet(a)) + fprintf('\n'); +end + + +%%%%%% + +function print_block(mpe, Qnodes, Fnodes, Onode, alphabet, start) + +D = length(Qnodes); +T = size(mpe, 2); +fprintf('%3d ', start:start+T-1); fprintf('\n'); +for d=1:D + for t=1:T + if (d > 1) & (mpe(Fnodes(d-1),t) == 2) + fprintf('%3d|', mpe(Qnodes(d), t)); + else + fprintf('%3d ', mpe(Qnodes(d), t)); + end + end + fprintf('\n'); +end +if ~isempty(alphabet) + fprintf('%3c ', alphabet(mpe(Onode,:))); + fprintf('\n'); +end diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/remove_hhmm_end_state.m b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/remove_hhmm_end_state.m new file mode 100644 index 00000000..1ef9ded9 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/remove_hhmm_end_state.m @@ -0,0 +1,41 @@ +function [transprob, termprob] = remove_hhmm_end_state(A) +% REMOVE_END_STATE Infer transition and termination probabilities from automaton with an end state +% [transprob, termprob] = remove_end_state(A) +% +% A(i,k,j) = Pr( i->j | Qps=k), where i in 1:Q, j in 1:(Q+1), and Q+1 is the end state +% This implements the equation in footnote 3 of my NIPS 01 paper, +% transprob(i,k,j) = \tilde{A}_k(i,j) +% termprob(k,j) = \tau_k(j) +% +% For the top level, the k index is missing. + +Q = size(A,1); +toplevel = (ndims(A)==2); +if toplevel + Qk = 1; + A = reshape(A, [Q 1 Q+1]); +else + Qk = size(A, 2); +end + +transprob = A(:, :, 1:Q); +term = A(:,:,Q+1)'; % term(k,j) = P(Qj -> end | k) +termprob = term; +%termprob = zeros(Qk, Q, 2); +%termprob(:,:,2) = term; +%termprob(:,:,1) = 1-term; + +for k=1:Qk + for i=1:Q + for j=1:Q + denom = (1-termprob(k,i)); + denom = denom + (denom==0)*eps; + transprob(i,k,j) = transprob(i,k,j) / denom; + end + end +end + +if toplevel + termprob = squeeze(termprob); + transprob = squeeze(transprob); +end diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/Old/CVS/Entries b/sourcecodes/bnt-master/BNT/examples/dynamic/Old/CVS/Entries new file mode 100644 index 00000000..b5b0dc43 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/Old/CVS/Entries @@ -0,0 +1,8 @@ +/chmm1.m/1.1.1.1/Wed May 29 15:59:54 2002// +/cmp_inference.m/1.1.1.1/Wed May 29 15:59:54 2002// +/kalman1.m/1.1.1.1/Wed May 29 15:59:54 2002// +/old.water1.m/1.1.1.1/Wed May 29 15:59:54 2002// +/online1.m/1.1.1.1/Wed May 29 15:59:54 2002// +/online2.m/1.1.1.1/Wed May 29 15:59:54 2002// +/scg_dbn.m/1.1.1.1/Wed May 29 15:59:54 2002// +D diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/Old/CVS/Repository b/sourcecodes/bnt-master/BNT/examples/dynamic/Old/CVS/Repository new file mode 100644 index 00000000..b23f8f6d --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/Old/CVS/Repository @@ -0,0 +1 @@ +FullBNT/BNT/examples/dynamic/Old diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/Old/CVS/Root b/sourcecodes/bnt-master/BNT/examples/dynamic/Old/CVS/Root new file mode 100644 index 00000000..f3bd14a6 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/Old/CVS/Root @@ -0,0 +1 @@ +:ext:nsaunier@bnt.cvs.sourceforge.net:/cvsroot/bnt diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/Old/chmm1.m b/sourcecodes/bnt-master/BNT/examples/dynamic/Old/chmm1.m new file mode 100644 index 00000000..d4195c97 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/Old/chmm1.m @@ -0,0 +1,40 @@ +% Compare the speeds of various inference engines on a coupled HMM + +N = 2; +Q = 2; +rand('state', 0); +randn('state', 0); +discrete = 1; +if discrete + Y = 2; % size of output alphabet +else + Y = 1; +end +coupled = 1; +[bnet, onodes] = mk_chmm(N, Q, Y, discrete, coupled); +ss = N*2; + +T = 3; + + +engine = {}; +tic; engine{end+1} = jtree_dbn_inf_engine(bnet, 'observed', onodes); toc +%tic; engine{end+1} = jtree_ndxSD_dbn_inf_engine(bnet, onodes); toc +%tic; engine{end+1} = jtree_ndxB_dbn_inf_engine(bnet, onodes); toc +engine{end+1} = hmm_inf_engine(bnet, onodes); +%engine{end+1} = dhmm_inf_engine(bnet, onodes); +tic; engine{end+1} = jtree_unrolled_dbn_inf_engine(bnet, T, onodes); toc + +%engine{end+1} = bk_inf_engine(bnet, 'ff', onodes); +%engine{end+1} = loopy_dbn_inf_engine(bnet, onodes); + +exact = [1 2 3]; + +filter = 0; +single = 0; +maximize = 0; + +[err, time, engine] = cmp_inference(bnet, onodes, engine, exact, T, filter, single, maximize); +%err = cmp_learning(bnet, onodes, engine, exact, T); + + diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/Old/cmp_inference.m b/sourcecodes/bnt-master/BNT/examples/dynamic/Old/cmp_inference.m new file mode 100644 index 00000000..b5c936f0 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/Old/cmp_inference.m @@ -0,0 +1,75 @@ +function [err, time, engine] = cmp_inference(bnet, engine, exact, T, filter, singletons, maximize) +% CMP_INFERENCE Compare several inference engines on a DBN +% [err, time, engine] = cmp_inference(bnet, engine, exact, T, filter, singletons, maximize) +% +% engine{i} is the i'th inference engine. +% 'exact' specifies which engines do exact inference - +% we check that these all give the same results. +% 'T' is the length of the random sequence we generate. +% If filter=1, we do filtering, else smoothing (default: smoothing) +% If singletons=1, we compare marginal_nodes, else marginal_family (default: family) +% +% err(e,n,t) = sum_i | Pr_exact(X(n,t)=i) - Pr_e(X(n,t)=i) | +% where Pr_e = prob. according to engine e +% time(e) = elapsed time for doing inference with engine e + +err = []; + +if nargin < 5, filter = 0; end +if nargin < 6, singletons = 0; end +if nargin < 7, maximize = 0; end + +check_ll = 1; + +assert(~maximize); + +E = length(engine); +ref = exact(1); % reference + +ss = length(bnet.intra); +ev = sample_dbn(bnet, 'length', T); +evidence = cell(ss,T); +onodes = bnet.observed; +evidence(onodes,:) = ev(onodes, :); + +assert(~filter); +for i=1:E + tic; + %[engine{i}, ll(i)] = enter_evidence(engine{i}, evidence, 'maximize', maximize); + [engine{i}, ll(i)] = enter_evidence(engine{i}, evidence); + time(i)=toc; + fprintf('engine %d took %6.4f seconds\n', i, time(i)); +end + +cmp = mysetdiff(exact, ref); +if check_ll +for i=cmp(:)' + if ~approxeq(ll(ref), ll(i)) + error(['engine ' num2str(i) ' has wrong ll']) + end +end +end +ll + +hnodes = mysetdiff(1:ss, onodes); +m = cell(1,E); +for t=1:T + for n=hnodes(:)' + for e=1:E + if singletons + m{e} = marginal_nodes(engine{e}, n, t); + else + m{e} = marginal_family(engine{e}, n, t); + end + end + for e=1:E + assert(isequal(m{e}.domain, m{ref}.domain)); + end + for e=cmp(:)' + if ~approxeq(m{ref}.T(:), m{e}.T(:)) + str= sprintf('engine %d is wrong; n=%d, t=%d', e, n, t); + error(str) + end + end + end +end diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/Old/kalman1.m b/sourcecodes/bnt-master/BNT/examples/dynamic/Old/kalman1.m new file mode 100644 index 00000000..c068ab3e --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/Old/kalman1.m @@ -0,0 +1,127 @@ +% Make a linear dynamical system +% X1 -> X2 +% | | +% v v +% Y1 Y2 + +intra = zeros(2); +intra(1,2) = 1; +inter = zeros(2); +inter(1,1) = 1; +n = 2; + +X = 2; % size of hidden state +Y = 2; % size of observable state + +ns = [X Y]; +dnodes = []; +onodes = [2]; +eclass1 = [1 2]; +eclass2 = [3 2]; +bnet = mk_dbn(intra, inter, ns, 'discrete', dnodes, 'eclass1', eclass1, 'eclass2', eclass2, ... + 'observed', onodes); + +x0 = rand(X,1); +V0 = eye(X); +C0 = rand(Y,X); +R0 = eye(Y); +A0 = rand(X,X); +Q0 = eye(X); + +bnet.CPD{1} = gaussian_CPD(bnet, 1, 'mean', x0, 'cov', V0, 'cov_prior_weight', 0); +bnet.CPD{2} = gaussian_CPD(bnet, 2, 'mean', zeros(Y,1), 'cov', R0, 'weights', C0, ... + 'clamp_mean', 1, 'cov_prior_weight', 0); +bnet.CPD{3} = gaussian_CPD(bnet, 3, 'mean', zeros(X,1), 'cov', Q0, 'weights', A0, ... + 'clamp_mean', 1, 'cov_prior_weight', 0); + + +T = 5; % fixed length sequences + +clear engine; +engine{1} = kalman_inf_engine(bnet); +engine{2} = jtree_unrolled_dbn_inf_engine(bnet, T); +engine{3} = jtree_dbn_inf_engine(bnet); +N = length(engine); + +% inference + +ev = sample_dbn(bnet, T); +evidence = cell(n,T); +evidence(onodes,:) = ev(onodes, :); + +t = 1; +query = [1 3]; +m = cell(1, N); +ll = zeros(1, N); +for i=1:N + [engine{i}, ll(i)] = enter_evidence(engine{i}, evidence); + m{i} = marginal_nodes(engine{i}, query, t); +end + +% compare all engines to engine{1} +for i=2:N + assert(approxeq(m{1}.mu, m{i}.mu)); + assert(approxeq(m{1}.Sigma, m{i}.Sigma)); + assert(approxeq(ll(1), ll(i))); +end + +if 0 +for i=2:N + approxeq(m{1}.mu, m{i}.mu) + approxeq(m{1}.Sigma, m{i}.Sigma) + approxeq(ll(1), ll(i)) +end +end + +% learning + +ncases = 5; +cases = cell(1, ncases); +for i=1:ncases + ev = sample_dbn(bnet, T); + cases{i} = cell(n,T); + cases{i}(onodes,:) = ev(onodes, :); +end + +max_iter = 2; +bnet2 = cell(1,N); +LLtrace = cell(1,N); +for i=1:N + [bnet2{i}, LLtrace{i}] = learn_params_dbn_em(engine{i}, cases, 'max_iter', max_iter); +end + +for i=1:N + temp = bnet2{i}; + for e=1:3 + CPD{i,e} = struct(temp.CPD{e}); + end +end + +for i=2:N + assert(approxeq(LLtrace{i}, LLtrace{1})); + for e=1:3 + assert(approxeq(CPD{i,e}.mean, CPD{1,e}.mean)); + assert(approxeq(CPD{i,e}.cov, CPD{1,e}.cov)); + assert(approxeq(CPD{i,e}.weights, CPD{1,e}.weights)); + end +end + + +% Compare to KF toolbox + +data = zeros(Y, T, ncases); +for i=1:ncases + data(:,:,i) = cell2num(cases{i}(onodes, :)); +end +[A2, C2, Q2, R2, x2, V2, LL2trace] = learn_kalman(data, A0, C0, Q0, R0, x0, V0, max_iter); + + +e = 1; +assert(approxeq(x2, CPD{e,1}.mean)) +assert(approxeq(V2, CPD{e,1}.cov)) +assert(approxeq(C2, CPD{e,2}.weights)) +assert(approxeq(R2, CPD{e,2}.cov)); +assert(approxeq(A2, CPD{e,3}.weights)) +assert(approxeq(Q2, CPD{e,3}.cov)); +assert(approxeq(LL2trace, LLtrace{1})) + diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/Old/old.water1.m b/sourcecodes/bnt-master/BNT/examples/dynamic/Old/old.water1.m new file mode 100644 index 00000000..0a356ef8 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/Old/old.water1.m @@ -0,0 +1,48 @@ +% Compare the speeds of various inference engines on the water DBN + +[bnet, onodes] = mk_water_dbn; + +T = 3; + +engine = {}; +engine{end+1} = jtree_unrolled_dbn_inf_engine(bnet, T, onodes); +engine{end+1} = hmm_inf_engine(bnet, onodes); +engine{end+1} = frontier_inf_engine(bnet, onodes); +engine{end+1} = jtree_dbn_inf_engine(bnet, onodes); +engine{end+1} = bk_inf_engine(bnet, 'exact', onodes); + +engine{end+1} = bk_inf_engine(bnet, 'ff', onodes); +engine{end+1} = bk_inf_engine(bnet, { [1 2], [3 4 5 6], [7 8] }, onodes); + +N = length(engine); +exact = 1:5; + + +filter = 0; +err = cmp_inference(bnet, onodes, engine, exact, T, filter); + +% elapsed times for enter_evidence (matlab 5.3 on PIII with 256MB running Redhat linux) + +% T = 5, 4/20/00 +% 0.6266 unrolled * +% 0.3490 hmm * +% 1.1743 frontier +% 1.4621 old frontier +% 0.3270 fast frontier * +% 1.3926 jtree +% 1.3790 bk +% 0.4916 fast bk +% 0.4190 fast bk compiled +% 0.3574 fast jtree * + + +err = cmp_learning(bnet, onodes, engine, exact, T); + +% elapsed times for learn_params_dbn_em (matlab 5.3 on PIII with 256MB running Redhat linux) + +% T = 5, 2cases, 2 iter, 4/20/00 +% 3.5750 unrolled +% 3.7475 hmm +% 2.1452 fast frontier +% 2.5724 fast bk compiled +% 2.3387 fast jtree diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/Old/online1.m b/sourcecodes/bnt-master/BNT/examples/dynamic/Old/online1.m new file mode 100644 index 00000000..05708e10 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/Old/online1.m @@ -0,0 +1,59 @@ +% Check that online inference gives same results as filtering for various algorithms + +N = 3; +Q = 2; +ss = N*2; + +rand('state', 0); +randn('state', 0); + + +obs_size = 1; +discrete_obs = 0; +bnet = mk_chmm(N, Q, obs_size, discrete_obs); +ns = bnet.node_sizes_slice; + +engine = {}; +engine{end+1} = hmm_inf_engine(bnet); +E = length(engine); + +onodes = (1:N)+N; + +T = 4; +ev = cell(ss,T); +ev(onodes,:) = num2cell(randn(N, T)); + + +filter = 1; +loglik2 = zeros(1,E); +for e=1:E + [engine2{e}, loglik2(e)] = enter_evidence(engine{e}, ev, 'filter', filter); +end + +loglik = zeros(1,E); +marg1 = cell(E,N,T); +for e=1:E + ll = zeros(1,T); + engine{e} = dbn_init_bel(engine{e}); + for t=1:T + [engine{e}, ll(t)] = dbn_update_bel(engine{e}, ev(:,t), t); + for i=1:N + marg1{e,i,t} = dbn_marginal_from_bel(engine{e}, i); + end + end + loglik1(e) = sum(ll); +end + +assert(approxeq(loglik1, loglik2)) + +a = zeros(E,N,T); +for e=1:E + for t=1:T + for i=1:N + marg2{e,i,t} = marginal_nodes(engine2{e}, i, t); + a(e,i,t) = (approxeq(marg2{e,i,t}.T(:), marg1{e,i,t}.T(:))); + end + end +end + +assert(all(a(:)==1)) diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/Old/online2.m b/sourcecodes/bnt-master/BNT/examples/dynamic/Old/online2.m new file mode 100644 index 00000000..6b141f19 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/Old/online2.m @@ -0,0 +1,33 @@ +N = 1; % regular HMM +Q = 2; +ss = 2; +hnodes = 1; +onodes = 2; + +rand('state', 0); +randn('state', 0); +O = 2; +discrete_obs = 1; +bnet = mk_chmm(N, Q, O, discrete_obs); +ns = bnet.node_sizes_slice; + +engine = hmm_inf_engine(bnet, onodes); + +T = 4; +ev = cell(ss,T); +ev(onodes,:) = num2cell(sample_discrete([0.5 0.5], N, T)); + + +engine = dbn_init_bel(engine); +for t=1:T + if t==1 + [engine, ll(t)] = dbn_update_bel1(engine, ev(:,t)); + else + [engine, ll(t)] = dbn_update_bel(engine, ev(:,t-1:t)); + end + % one-step ahead prediction + lag = 1; + engine2 = dbn_predict_bel(engine, lag); + marg = dbn_marginal_from_bel(engine2, 1) + marg = dbn_marginal_from_bel(engine2, 2) +end diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/Old/scg_dbn.m b/sourcecodes/bnt-master/BNT/examples/dynamic/Old/scg_dbn.m new file mode 100644 index 00000000..0ddabb34 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/Old/scg_dbn.m @@ -0,0 +1,70 @@ +% to test whether scg inference engine can handl dynameic BN +% Make a linear dynamical system +% X1 -> X2 +% | | +% v v +% Y1 Y2 + +intra = zeros(2); +intra(1,2) = 1; +inter = zeros(2); +inter(1,1) = 1; +n = 2; + +X = 2; % size of hidden state +Y = 2; % size of observable state + +ns = [X Y]; +dnodes = []; +onodes = [2]; +eclass1 = [1 2]; +eclass2 = [3 2]; +bnet = mk_dbn(intra, inter, ns, dnodes, eclass1, eclass2); + +x0 = rand(X,1); +V0 = eye(X); +C0 = rand(Y,X); +R0 = eye(Y); +A0 = rand(X,X); +Q0 = eye(X); + +bnet.CPD{1} = gaussian_CPD(bnet, 1, 'mean', x0, 'cov', V0); +%bnet.CPD{2} = gaussian_CPD(bnet, 2, 'mean', zeros(Y,1), 'cov', R0, 'weights', C0, 'full', 'untied', 'clamped_mean'); +%bnet.CPD{3} = gaussian_CPD(bnet, 3, 'mean', zeros(X,1), 'cov', Q0, 'weights', A0, 'full', 'untied', 'clamped_mean'); +bnet.CPD{2} = gaussian_CPD(bnet, 2, 'mean', zeros(Y,1), 'cov', R0, 'weights', C0); +bnet.CPD{3} = gaussian_CPD(bnet, 3, 'mean', zeros(X,1), 'cov', Q0, 'weights', A0); + + +T = 5; % fixed length sequences + +clear engine; +%engine{1} = kalman_inf_engine(bnet, onodes); +engine{1} = scg_unrolled_dbn_inf_engine(bnet, T, onodes); +engine{2} = jtree_unrolled_dbn_inf_engine(bnet, T); + +N = length(engine); + +% inference + +ev = sample_dbn(bnet, T); +evidence = cell(n,T); +evidence(onodes,:) = ev(onodes, :); + +t = 2; +query = [1 3]; +m = cell(1, N); +ll = zeros(1, N); + +engine{1} = enter_evidence(engine{1}, evidence); +[engine{2}, ll(2)] = enter_evidence(engine{2}, evidence); +m{1} = marginal_nodes(engine{1}, query); +m{2} = marginal_nodes(engine{2}, query, t); + + +% compare all engines to engine{1} +for i=2:N + assert(approxeq(m{1}.mu, m{i}.mu)); + assert(approxeq(m{1}.Sigma, m{i}.Sigma)); +% assert(approxeq(ll(1), ll(i))); +end + diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/SLAM/CVS/Entries b/sourcecodes/bnt-master/BNT/examples/dynamic/SLAM/CVS/Entries new file mode 100644 index 00000000..6810ce1d --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/SLAM/CVS/Entries @@ -0,0 +1,7 @@ +/mk_gmux_robot_dbn.m/1.1.1.1/Wed May 29 15:59:54 2002// +/mk_linear_slam.m/1.1.1.1/Wed May 29 15:59:54 2002// +/slam_kf.m/1.1.1.1/Wed May 29 15:59:54 2002// +/slam_offline_loopy.m/1.1.1.1/Wed May 29 15:59:54 2002// +/slam_partial_kf.m/1.1.1.1/Wed May 29 15:59:54 2002// +/slam_stationary_loopy.m/1.1.1.1/Wed May 29 15:59:54 2002// +D/Old//// diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/SLAM/CVS/Repository b/sourcecodes/bnt-master/BNT/examples/dynamic/SLAM/CVS/Repository new file mode 100644 index 00000000..e32a23fa --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/SLAM/CVS/Repository @@ -0,0 +1 @@ +FullBNT/BNT/examples/dynamic/SLAM diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/SLAM/CVS/Root b/sourcecodes/bnt-master/BNT/examples/dynamic/SLAM/CVS/Root new file mode 100644 index 00000000..f3bd14a6 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/SLAM/CVS/Root @@ -0,0 +1 @@ +:ext:nsaunier@bnt.cvs.sourceforge.net:/cvsroot/bnt diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/SLAM/Old/CVS/Entries b/sourcecodes/bnt-master/BNT/examples/dynamic/SLAM/Old/CVS/Entries new file mode 100644 index 00000000..37fe6bb1 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/SLAM/Old/CVS/Entries @@ -0,0 +1,5 @@ +/offline_loopy_slam.m/1.1.1.1/Wed May 29 15:59:54 2002// +/paskin1.m/1.1.1.1/Wed May 29 15:59:54 2002// +/skf_data_assoc_gmux2.m/1.1.1.1/Wed May 29 15:59:54 2002// +/slam_kf.m/1.1.1.1/Wed May 29 15:59:54 2002// +D diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/SLAM/Old/CVS/Repository b/sourcecodes/bnt-master/BNT/examples/dynamic/SLAM/Old/CVS/Repository new file mode 100644 index 00000000..1bae1a70 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/SLAM/Old/CVS/Repository @@ -0,0 +1 @@ +FullBNT/BNT/examples/dynamic/SLAM/Old diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/SLAM/Old/CVS/Root b/sourcecodes/bnt-master/BNT/examples/dynamic/SLAM/Old/CVS/Root new file mode 100644 index 00000000..f3bd14a6 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/SLAM/Old/CVS/Root @@ -0,0 +1 @@ +:ext:nsaunier@bnt.cvs.sourceforge.net:/cvsroot/bnt diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/SLAM/Old/offline_loopy_slam.m b/sourcecodes/bnt-master/BNT/examples/dynamic/SLAM/Old/offline_loopy_slam.m new file mode 100644 index 00000000..377a4659 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/SLAM/Old/offline_loopy_slam.m @@ -0,0 +1,231 @@ +% We navigate a robot around a square using a fixed control policy and no noise. +% We assume the robot observes the relative distance to the nearest landmark. +% Everything is linear-Gaussian. + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Create toy data set + +seed = 0; +rand('state', seed); +randn('state', seed); + +if 1 + T = 20; + ctrl_signal = [repmat([1 0]', 1, T/4) repmat([0 1]', 1, T/4) ... + repmat([-1 0]', 1, T/4) repmat([0 -1]', 1, T/4)]; +else + T = 5; + ctrl_signal = repmat([1 0]', 1, T); +end + +nlandmarks = 4; +true_landmark_pos = [1 1; + 4 1; + 4 4; + 1 4]'; +init_robot_pos = [0 0]'; + +true_robot_pos = zeros(2, T); +true_data_assoc = zeros(1, T); +true_rel_dist = zeros(2, T); +for t=1:T + if t>1 + true_robot_pos(:,t) = true_robot_pos(:,t-1) + ctrl_signal(:,t); + else + true_robot_pos(:,t) = init_robot_pos + ctrl_signal(:,t); + end + nn = argmin(dist2(true_robot_pos(:,t)', true_landmark_pos')); + %nn = t; % observe 1, 2, 3 + true_data_assoc(t) = nn; + true_rel_dist(:,t) = true_landmark_pos(:, nn) - true_robot_pos(:,t); +end + +figure(1); +%clf; +hold on +%plot(true_landmark_pos(1,:), true_landmark_pos(2,:), '*'); +for i=1:nlandmarks + text(true_landmark_pos(1,i), true_landmark_pos(2,i), sprintf('L%d',i)); +end +for t=1:T + text(true_robot_pos(1,t), true_robot_pos(2,t), sprintf('%d',t)); +end +hold off +axis([-1 6 -1 6]) + +R = 1e-3*eye(2); % noise added to observation +Q = 1e-3*eye(2); % noise added to robot motion + +% Create data set +obs_noise_seq = sample_gaussian([0 0]', R, T)'; +obs_rel_pos = true_rel_dist + obs_noise_seq; +%obs_rel_pos = true_rel_dist; + + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Create params for inference + +% X(t) = A X(t-1) + B U(t) + noise(Q) + +% [L1] = [1 ] * [L1] + [0] * Ut + [0 ] +% [L2] [ 1 ] [L2] [0] [ 0 ] +% [R ]t [ 1] [R ]t-1 [1] [ Q] + +% Y(t)|S(t)=s = C(s) X(t) + noise(R) +% Yt|St=1 = [1 0 -1] * [L1] + R +% [L2] +% [R ] + +% Create indices into block structure +bs = 2*ones(1, nlandmarks+1); % sizes of blocks in state space +robot_block = block(nlandmarks+1, bs); +for i=1:nlandmarks + landmark_block(:,i) = block(i, bs)'; +end +Xsz = 2*(nlandmarks+1); % 2 values for each landmark plus robot +Ysz = 2; % observe relative location +Usz = 2; % input is (dx, dy) + + +% create block-diagonal trans matrix for each switch +A = zeros(Xsz, Xsz); +for i=1:nlandmarks + bi = landmark_block(:,i); + A(bi, bi) = eye(2); +end +bi = robot_block; +A(bi, bi) = eye(2); +A = repmat(A, [1 1 nlandmarks]); % same for all switch values + +% create block-diagonal system cov + + +Qbig = zeros(Xsz, Xsz); +bi = robot_block; +Qbig(bi,bi) = Q; % only add noise to robot motion +Qbig = repmat(Qbig, [1 1 nlandmarks]); + +% create input matrix +B = zeros(Xsz, Usz); +B(robot_block,:) = eye(2); % only add input to robot position +B = repmat(B, [1 1 nlandmarks]); + +% create observation matrix for each value of the switch node +% C(:,:,i) = (0 ... I ... -I) where the I is in the i'th posn. +% This computes L(i) - R +C = zeros(Ysz, Xsz, nlandmarks); +for i=1:nlandmarks + C(:, landmark_block(:,i), i) = eye(2); + C(:, robot_block, i) = -eye(2); +end + +% create observation cov for each value of the switch node +Rbig = repmat(R, [1 1 nlandmarks]); + +% initial conditions +init_x = zeros(Xsz, 1); +init_v = zeros(Xsz, Xsz); +bi = robot_block; +init_x(bi) = init_robot_pos; +init_V(bi, bi) = 1e-5*eye(2); % very sure of robot posn +for i=1:nlandmarks + bi = landmark_block(:,i); + init_V(bi,bi)= 1e5*eye(2); % very uncertain of landmark psosns + %init_x(bi) = true_landmark_pos(:,i); + %init_V(bi,bi)= 1e-5*eye(2); % very sure of landmark psosns +end + +%%%%%%%%%%%%%%%%%%%%% +% Inference +if 1 +[xsmooth, Vsmooth] = kalman_smoother(obs_rel_pos, A, C, Qbig, Rbig, init_x, init_V, ... + 'model', true_data_assoc, 'u', ctrl_signal, 'B', B); + +est_robot_pos = xsmooth(robot_block, :); +est_robot_pos_cov = Vsmooth(robot_block, robot_block, :); + +for i=1:nlandmarks + bi = landmark_block(:,i); + est_landmark_pos(:,i) = xsmooth(bi, T); + est_landmark_pos_cov(:,:,i) = Vsmooth(bi, bi, T); +end +end + + +if 0 +figure(1); hold on +for i=1:nlandmarks + h=plotgauss2d(est_landmark_pos(:,i), est_landmark_pos_cov(:,:,i)); + set(h, 'color', 'r') +end +hold off + +hold on +for t=1:T + h=plotgauss2d(est_robot_pos(:,t), est_robot_pos_cov(:,:,t)); + set(h,'color','r') + h=text(est_robot_pos(1,t), est_robot_pos(2,2), sprintf('R%d', t)); + set(h,'color','r') +end +hold off +end + + +if 0 +figure(3) +if 0 + for t=1:T + imagesc(inv(Vsmooth(:,:,t))) + colorbar + fprintf('t=%d; press key to continue\n', t); + pause + end +else + for t=1:T + subplot(5,4,t) + imagesc(inv(Vsmooth(:,:,t))) + end +end +end + + + + + +%%%%%%%%%%%%%%%%% +% DBN inference + +if 1 + [bnet, Unode, Snode, Lnodes, Rnode, Ynode, Lsnode] = ... + mk_gmux_robot_dbn(nlandmarks, Q, R, init_x, init_V, robot_block, landmark_block); + engine = pearl_unrolled_dbn_inf_engine(bnet, 'max_iter', 50, 'filename', ... + '/home/eecs/murphyk/matlab/loopyslam.txt'); +else + [bnet, Unode, Snode, Lnodes, Rnode, Ynode] = ... + mk_gmux2_robot_dbn(nlandmarks, Q, R, init_x, init_V, robot_block, landmark_block); + engine = jtree_dbn_inf_engine(bnet); +end + +nnodes = bnet.nnodes_per_slice; +evidence = cell(nnodes, T); +evidence(Ynode, :) = num2cell(obs_rel_pos, 1); +evidence(Unode, :) = num2cell(ctrl_signal, 1); +evidence(Snode, :) = num2cell(true_data_assoc); + + +[engine, ll, niter] = enter_evidence(engine, evidence); +niter + +loopy_est_robot_pos = zeros(2, T); +for t=1:T + m = marginal_nodes(engine, Rnode, t); + loopy_est_robot_pos(:,t) = m.mu; +end + +for i=1:nlandmarks + m = marginal_nodes(engine, Lnodes(i), T); + loopy_est_landmark_pos(:,i) = m.mu; + loopy_est_landmark_pos_cov(:,:,i) = m.Sigma; +end + + diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/SLAM/Old/paskin1.m b/sourcecodes/bnt-master/BNT/examples/dynamic/SLAM/Old/paskin1.m new file mode 100644 index 00000000..286793d3 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/SLAM/Old/paskin1.m @@ -0,0 +1,238 @@ +% This is like robot1, except we only use a Kalman filter. +% The goal is to study how the precision matrix changes. + +seed = 1; +rand('state', seed); +randn('state', seed); + +if 0 + T = 20; + ctrl_signal = [repmat([1 0]', 1, T/4) repmat([0 1]', 1, T/4) ... + repmat([-1 0]', 1, T/4) repmat([0 -1]', 1, T/4)]; +else + T = 60; + ctrl_signal = repmat([1 0]', 1, T); +end + +nlandmarks = 6; +if 0 + true_landmark_pos = [1 1; + 4 1; + 4 4; + 1 4]'; +else + true_landmark_pos = 10*rand(2,nlandmarks); +end +if 0 +figure(1); clf +hold on +for i=1:nlandmarks + %text(true_landmark_pos(1,i), true_landmark_pos(2,i), sprintf('L%d',i)); + plot(true_landmark_pos(1,i), true_landmark_pos(2,i), '*') +end +hold off +end + +init_robot_pos = [0 0]'; + +true_robot_pos = zeros(2, T); +true_data_assoc = zeros(1, T); +true_rel_dist = zeros(2, T); +for t=1:T + if t>1 + true_robot_pos(:,t) = true_robot_pos(:,t-1) + ctrl_signal(:,t); + else + true_robot_pos(:,t) = init_robot_pos + ctrl_signal(:,t); + end + nn = argmin(dist2(true_robot_pos(:,t)', true_landmark_pos')); + %true_data_assoc(t) = nn; + %true_data_assoc = wrap(t, nlandmarks); % observe 1, 2, 3, 4, 1, 2, ... + true_data_assoc = sample_discrete(normalise(ones(1,nlandmarks)),1,T); + true_rel_dist(:,t) = true_landmark_pos(:, nn) - true_robot_pos(:,t); +end + +R = 1e-3*eye(2); % noise added to observation +Q = 1e-3*eye(2); % noise added to robot motion + +% Create data set +obs_noise_seq = sample_gaussian([0 0]', R, T)'; +obs_rel_pos = true_rel_dist + obs_noise_seq; +%obs_rel_pos = true_rel_dist; + + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Create params for inference + +% X(t) = A X(t-1) + B U(t) + noise(Q) + +% [L1] = [1 ] * [L1] + [0] * Ut + [0 ] +% [L2] [ 1 ] [L2] [0] [ 0 ] +% [R ]t [ 1] [R ]t-1 [1] [ Q] + +% Y(t)|S(t)=s = C(s) X(t) + noise(R) +% Yt|St=1 = [1 0 -1] * [L1] + R +% [L2] +% [R ] + +% Create indices into block structure +bs = 2*ones(1, nlandmarks+1); % sizes of blocks in state space +robot_block = block(nlandmarks+1, bs); +for i=1:nlandmarks + landmark_block(:,i) = block(i, bs)'; +end +Xsz = 2*(nlandmarks+1); % 2 values for each landmark plus robot +Ysz = 2; % observe relative location +Usz = 2; % input is (dx, dy) + + +% create block-diagonal trans matrix for each switch +A = zeros(Xsz, Xsz); +for i=1:nlandmarks + bi = landmark_block(:,i); + A(bi, bi) = eye(2); +end +bi = robot_block; +A(bi, bi) = eye(2); +A = repmat(A, [1 1 nlandmarks]); % same for all switch values + +% create block-diagonal system cov + + +Qbig = zeros(Xsz, Xsz); +bi = robot_block; +Qbig(bi,bi) = Q; % only add noise to robot motion +Qbig = repmat(Qbig, [1 1 nlandmarks]); + +% create input matrix +B = zeros(Xsz, Usz); +B(robot_block,:) = eye(2); % only add input to robot position +B = repmat(B, [1 1 nlandmarks]); + +% create observation matrix for each value of the switch node +% C(:,:,i) = (0 ... I ... -I) where the I is in the i'th posn. +% This computes L(i) - R +C = zeros(Ysz, Xsz, nlandmarks); +for i=1:nlandmarks + C(:, landmark_block(:,i), i) = eye(2); + C(:, robot_block, i) = -eye(2); +end + +% create observation cov for each value of the switch node +Rbig = repmat(R, [1 1 nlandmarks]); + +% initial conditions +init_x = zeros(Xsz, 1); +init_v = zeros(Xsz, Xsz); +bi = robot_block; +init_x(bi) = init_robot_pos; +%init_V(bi, bi) = 1e-5*eye(2); % very sure of robot posn +init_V(bi, bi) = Q; % simualate uncertainty due to 1 motion step +for i=1:nlandmarks + bi = landmark_block(:,i); + init_V(bi,bi)= 1e5*eye(2); % very uncertain of landmark psosns + %init_x(bi) = true_landmark_pos(:,i); + %init_V(bi,bi)= 1e-5*eye(2); % very sure of landmark psosns +end + +%k = nlandmarks-1; % exact +k = 3; +ndx = {}; +for t=1:T + landmarks = unique(true_data_assoc(t:-1:max(t-k,1))); + tmp = [landmark_block(:, landmarks) robot_block']; + ndx{t} = tmp(:); +end + +[xa, Va] = kalman_filter(obs_rel_pos, A, C, Qbig, Rbig, init_x, init_V, ... + 'model', true_data_assoc, 'u', ctrl_signal, 'B', B, ... + 'ndx', ndx); + +[xe, Ve] = kalman_filter(obs_rel_pos, A, C, Qbig, Rbig, init_x, init_V, ... + 'model', true_data_assoc, 'u', ctrl_signal, 'B', B); + + +if 0 +est_robot_pos = x(robot_block, :); +est_robot_pos_cov = V(robot_block, robot_block, :); + +for i=1:nlandmarks + bi = landmark_block(:,i); + est_landmark_pos(:,i) = x(bi, T); + est_landmark_pos_cov(:,:,i) = V(bi, bi, T); +end +end + + + +nrows = 10; +stepsize = T/(2*nrows); +ts = 1:stepsize:T; + +if 1 % plot + +clim = [0 max(max(Va(:,:,end)))]; + +figure(2) +if 0 + imagesc(Ve(1:2:end,1:2:end, T)) + clim = get(gca,'clim'); +else + i = 1; + for t=ts(:)' + subplot(nrows,2,i) + i = i + 1; + imagesc(Ve(1:2:end,1:2:end, t)) + set(gca, 'clim', clim) + colorbar + end +end +suptitle('exact') + + +figure(3) +if 0 + imagesc(Va(1:2:end,1:2:end, T)) + set(gca,'clim', clim) +else + i = 1; + for t=ts(:)' + subplot(nrows,2,i) + i = i+1; + imagesc(Va(1:2:end,1:2:end, t)) + set(gca, 'clim', clim) + colorbar + end +end +suptitle('approx') + + +figure(4) +i = 1; +for t=ts(:)' + subplot(nrows,2,i) + i = i+1; + Vd = Va(1:2:end,1:2:end, t) - Ve(1:2:end,1:2:end,t); + imagesc(Vd) + set(gca, 'clim', clim) + colorbar +end +suptitle('diff') + +end % all plot + + +for t=1:T + i = 1:2*nlandmarks; + denom = Ve(i,i,t) + (Ve(i,i,t)==0); + Vd =(Va(i,i,t)-Ve(i,i,t)) ./ denom; + Verr(t) = max(Vd(:)); +end +figure(6); plot(Verr) +title('max relative Verr') + +for t=1:T + %err(t)=rms(xa(:,t), xe(:,t)); + err(t)=rms(xa(1:end-2,t), xe(1:end-2,t)); % exclude robot +end +figure(5);plot(err) +title('rms mean pos') diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/SLAM/Old/skf_data_assoc_gmux2.m b/sourcecodes/bnt-master/BNT/examples/dynamic/SLAM/Old/skf_data_assoc_gmux2.m new file mode 100644 index 00000000..0272d3f6 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/SLAM/Old/skf_data_assoc_gmux2.m @@ -0,0 +1,155 @@ +% This is like skf_data_assoc_gmux, except the objects don't move. +% We are uncertain of their initial positions, and get more and more observations +% over time. The goal is to test deterministic links (0 covariance). +% This is like robot1, except the robot doesn't move and is always at [0 0], +% so the relative location is simply L(s). + +nobj = 2; +N = nobj+2; +Xs = 1:nobj; +S = nobj+1; +Y = nobj+2; + +intra = zeros(N,N); +inter = zeros(N,N); +intra([Xs S], Y) =1; +for i=1:nobj + inter(Xs(i), Xs(i))=1; +end + +Xsz = 2; % state space = (x y) +Ysz = 2; +ns = zeros(1,N); +ns(Xs) = Xsz; +ns(Y) = Ysz; +ns(S) = nobj; + +bnet = mk_dbn(intra, inter, ns, 'discrete', S, 'observed', [S Y]); + +% For each object, we have +% X(t+1) = F X(t) + noise(Q) +% Y(t) = H X(t) + noise(R) +F = eye(2); +H = eye(2); +Q = 0*eye(Xsz); % no noise in dynamics +R = eye(Ysz); + +init_state{1} = [10 10]'; +init_state{2} = [10 -10]'; +init_cov = eye(2); + +% Uncertain of initial state (position) +for i=1:nobj + bnet.CPD{Xs(i)} = gaussian_CPD(bnet, Xs(i), 'mean', init_state{i}, 'cov', init_cov); +end +bnet.CPD{S} = root_CPD(bnet, S); % always observed +bnet.CPD{Y} = gmux_CPD(bnet, Y, 'cov', repmat(R, [1 1 nobj]), 'weights', repmat(H, [1 1 nobj])); +% slice 2 +eclass = bnet.equiv_class; +for i=1:nobj + bnet.CPD{eclass(Xs(i), 2)} = gaussian_CPD(bnet, Xs(i)+N, 'mean', zeros(Xsz,1), 'cov', Q, 'weights', F); +end + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Create LDS params + +% X(t) = A X(t-1) + B U(t) + noise(Q) + +% [L11] = [1 ] * [L1] + [Q ] +% [L2] [ 1] [L2] [ Q] + +% Y(t)|S(t)=s = C(s) X(t) + noise(R) +% Yt|St=1 = [1 0] * [L1] + R +% [L2] + +nlandmarks = nobj; + +% Create indices into block structure +bs = 2*ones(1, nobj); % sizes of blocks in state space +for i=1:nlandmarks + landmark_block(:,i) = block(i, bs)'; +end +Xsz = 2*(nlandmarks); % 2 values for each landmark plus robot +Ysz = 2; % observe relative location + +% create block-diagonal trans matrix for each switch +A = zeros(Xsz, Xsz); +for i=1:nlandmarks + bi = landmark_block(:,i); + A(bi, bi) = eye(2); +end +A = repmat(A, [1 1 nlandmarks]); % same for all switch values + +% create block-diagonal system cov +Qbig = zeros(Xsz, Xsz); +Qbig = repmat(Qbig, [1 1 nlandmarks]); + + +% create observation matrix for each value of the switch node +% C(:,:,i) = (0 ... I ...) where the I is in the i'th posn. +C = zeros(Ysz, Xsz, nlandmarks); +for i=1:nlandmarks + C(:, landmark_block(:,i), i) = eye(2); +end + +% create observation cov for each value of the switch node +Rbig = repmat(R, [1 1 nlandmarks]); + +% initial conditions +init_x = [init_state{1}; init_state{2}]; +init_V = zeros(Xsz, Xsz); +for i=1:nlandmarks + bi = landmark_block(:,i); + init_V(bi,bi) = init_cov; +end + + + +%%%%%%%%%%%%%%%% +% Observe objects at random +T = 10; +evidence = cell(N, T); +data_assoc = sample_discrete(normalise(ones(1,nobj)), 1, T); +evidence(S,:) = num2cell(data_assoc); +evidence = sample_dbn(bnet, 'evidence', evidence); + + +% Inference +ev = cell(N,T); +ev(bnet.observed,:) = evidence(bnet.observed, :); +y = cell2num(evidence(Y,:)); + +engine = pearl_unrolled_dbn_inf_engine(bnet); +engine = enter_evidence(engine, ev); + +loopy_est_pos = zeros(2, nlandmarks); +loopy_est_pos_cov = zeros(2, 2, nlandmarks); +for i=1:nobj + m = marginal_nodes(engine, Xs(i), T); + loopy_est_pos(:,i) = m.mu; + loopy_est_pos_cov(:,:,i) = m.Sigma; +end + + +[xsmooth, Vsmooth] = kalman_smoother(y, A, C, Qbig, Rbig, init_x, init_V, 'model', data_assoc); + +kf_est_pos = zeros(2, nlandmarks); +kf_est_pos_cov = zeros(2, 2, nlandmarks); +for i=1:nlandmarks + bi = landmark_block(:,i); + kf_est_pos(:,i) = xsmooth(bi, T); + kf_est_pos_cov(:,:,i) = Vsmooth(bi, bi, T); +end + + +kf_est_pos +loopy_est_pos + +kf_est_pos_time = zeros(2, nlandmarks, T); +for t=1:T + for i=1:nlandmarks + bi = landmark_block(:,i); + kf_est_pos_time(:,i,t) = xsmooth(bi, t); + end +end +kf_est_pos_time % same for all t since smoothed diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/SLAM/Old/slam_kf.m b/sourcecodes/bnt-master/BNT/examples/dynamic/SLAM/Old/slam_kf.m new file mode 100644 index 00000000..ba98140f --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/SLAM/Old/slam_kf.m @@ -0,0 +1,172 @@ +% This is like robot1, except we only use a Kalman filter. +% The goal is to study how the precision matrix changes. + +seed = 0; +rand('state', seed); +randn('state', seed); + +if 0 + T = 20; + ctrl_signal = [repmat([1 0]', 1, T/4) repmat([0 1]', 1, T/4) ... + repmat([-1 0]', 1, T/4) repmat([0 -1]', 1, T/4)]; +else + T = 12; + ctrl_signal = repmat([1 0]', 1, T); +end + +nlandmarks = 6; +if 0 + true_landmark_pos = [1 1; + 4 1; + 4 4; + 1 4]'; +else + true_landmark_pos = 10*rand(2,nlandmarks); +end +figure(1); clf +hold on +for i=1:nlandmarks + %text(true_landmark_pos(1,i), true_landmark_pos(2,i), sprintf('L%d',i)); + plot(true_landmark_pos(1,i), true_landmark_pos(2,i), '*') +end +hold off + +init_robot_pos = [0 0]'; + +true_robot_pos = zeros(2, T); +true_data_assoc = zeros(1, T); +true_rel_dist = zeros(2, T); +for t=1:T + if t>1 + true_robot_pos(:,t) = true_robot_pos(:,t-1) + ctrl_signal(:,t); + else + true_robot_pos(:,t) = init_robot_pos + ctrl_signal(:,t); + end + %nn = argmin(dist2(true_robot_pos(:,t)', true_landmark_pos')); + nn = wrap(t, nlandmarks); % observe 1, 2, 3, 4, 1, 2, ... + true_data_assoc(t) = nn; + true_rel_dist(:,t) = true_landmark_pos(:, nn) - true_robot_pos(:,t); +end + +R = 1e-3*eye(2); % noise added to observation +Q = 1e-3*eye(2); % noise added to robot motion + +% Create data set +obs_noise_seq = sample_gaussian([0 0]', R, T)'; +obs_rel_pos = true_rel_dist + obs_noise_seq; +%obs_rel_pos = true_rel_dist; + + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Create params for inference + +% X(t) = A X(t-1) + B U(t) + noise(Q) + +% [L1] = [1 ] * [L1] + [0] * Ut + [0 ] +% [L2] [ 1 ] [L2] [0] [ 0 ] +% [R ]t [ 1] [R ]t-1 [1] [ Q] + +% Y(t)|S(t)=s = C(s) X(t) + noise(R) +% Yt|St=1 = [1 0 -1] * [L1] + R +% [L2] +% [R ] + +% Create indices into block structure +bs = 2*ones(1, nlandmarks+1); % sizes of blocks in state space +robot_block = block(nlandmarks+1, bs); +for i=1:nlandmarks + landmark_block(:,i) = block(i, bs)'; +end +Xsz = 2*(nlandmarks+1); % 2 values for each landmark plus robot +Ysz = 2; % observe relative location +Usz = 2; % input is (dx, dy) + + +% create block-diagonal trans matrix for each switch +A = zeros(Xsz, Xsz); +for i=1:nlandmarks + bi = landmark_block(:,i); + A(bi, bi) = eye(2); +end +bi = robot_block; +A(bi, bi) = eye(2); +A = repmat(A, [1 1 nlandmarks]); % same for all switch values + +% create block-diagonal system cov + + +Qbig = zeros(Xsz, Xsz); +bi = robot_block; +Qbig(bi,bi) = Q; % only add noise to robot motion +Qbig = repmat(Qbig, [1 1 nlandmarks]); + +% create input matrix +B = zeros(Xsz, Usz); +B(robot_block,:) = eye(2); % only add input to robot position +B = repmat(B, [1 1 nlandmarks]); + +% create observation matrix for each value of the switch node +% C(:,:,i) = (0 ... I ... -I) where the I is in the i'th posn. +% This computes L(i) - R +C = zeros(Ysz, Xsz, nlandmarks); +for i=1:nlandmarks + C(:, landmark_block(:,i), i) = eye(2); + C(:, robot_block, i) = -eye(2); +end + +% create observation cov for each value of the switch node +Rbig = repmat(R, [1 1 nlandmarks]); + +% initial conditions +init_x = zeros(Xsz, 1); +init_v = zeros(Xsz, Xsz); +bi = robot_block; +init_x(bi) = init_robot_pos; +init_V(bi, bi) = 1e-5*eye(2); % very sure of robot posn +for i=1:nlandmarks + bi = landmark_block(:,i); + init_V(bi,bi)= 1e5*eye(2); % very uncertain of landmark psosns + %init_x(bi) = true_landmark_pos(:,i); + %init_V(bi,bi)= 1e-5*eye(2); % very sure of landmark psosns +end + +[xsmooth, Vsmooth] = kalman_filter(obs_rel_pos, A, C, Qbig, Rbig, init_x, init_V, ... + 'model', true_data_assoc, 'u', ctrl_signal, 'B', B); + +est_robot_pos = xsmooth(robot_block, :); +est_robot_pos_cov = Vsmooth(robot_block, robot_block, :); + +for i=1:nlandmarks + bi = landmark_block(:,i); + est_landmark_pos(:,i) = xsmooth(bi, T); + est_landmark_pos_cov(:,:,i) = Vsmooth(bi, bi, T); +end + + + +P = zeros(size(Vsmooth)); +for t=1:T + P(:,:,t) = inv(Vsmooth(:,:,t)); +end + +figure(1) +for t=1:T + subplot(T/2,2,t) + imagesc(P(1:2:end,1:2:end, t)) + colorbar +end + +figure(2) +for t=1:T + subplot(T/2,2,t) + imagesc(Vsmooth(1:2:end,1:2:end, t)) + colorbar +end + + + +% marginalize out robot position and then check structure +bi = landmark_block(:); +V = Vsmooth(bi,bi,T); +P = inv(V); +P(1:2:end,1:2:end) diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/SLAM/mk_gmux_robot_dbn.m b/sourcecodes/bnt-master/BNT/examples/dynamic/SLAM/mk_gmux_robot_dbn.m new file mode 100644 index 00000000..8ee3a7ca --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/SLAM/mk_gmux_robot_dbn.m @@ -0,0 +1,85 @@ +function [bnet, Unode, Snode, Lnodes, Rnode, Ynode, Lsnode] = ... + mk_gmux_robot_dbn(nlandmarks, Q, R, init_x, init_V, robot_block, landmark_block) + +% Make DBN + +% S +% | L1 -------> L1' +% | | L2 ----------> L2' +% \ | / +% v v v +% Ls +% | +% v +% Y +% ^ +% | +% R -------> R' +% ^ +% | +% U +% +% +% S is a switch, Ls is a deterministic gmux, Y = Ls-R, +% R(t+1) = R(t) + U(t+1), L(t+1) = L(t) + + +% number nodes topologically +Snode = 1; +Lnodes = 2:nlandmarks+1; +Lsnode = nlandmarks+2; +Unode = nlandmarks+3; +Rnode = nlandmarks+4; +Ynode = nlandmarks+5; + +nnodes = nlandmarks+5; +intra = zeros(nnodes, nnodes); +intra([Snode Lnodes], Lsnode) =1; +intra(Unode,Rnode)=1; +intra([Rnode Lsnode], Ynode)=1; + +inter = zeros(nnodes, nnodes); +inter(Rnode, Rnode)=1; +for i=1:nlandmarks + inter(Lnodes(i), Lnodes(i))=1; +end + +Lsz = 2; % (x y) posn of landmark +Rsz = 2; % (x y) posn of robot +Ysz = 2; % relative distance +Usz = 2; % (dx dy) ctrl +Ssz = nlandmarks; % can switch between any landmark + +ns = zeros(1,nnodes); +ns(Snode) = Ssz; +ns(Lnodes) = Lsz; +ns(Lsnode) = Lsz; +ns(Ynode) = Ysz; +ns(Rnode) = Rsz; +ns(Ynode) = Usz; +ns(Unode) = Usz; + +bnet = mk_dbn(intra, inter, ns, 'discrete', Snode, 'observed', [Snode Ynode Unode]); + + +bnet.CPD{Snode} = root_CPD(bnet, Snode); % always observed +bnet.CPD{Unode} = root_CPD(bnet, Unode); % always observed +for i=1:nlandmarks + bi = landmark_block(:,i); + bnet.CPD{Lnodes(i)} = gaussian_CPD(bnet, Lnodes(i), 'mean', init_x(bi), 'cov', init_V(bi,bi)); +end +bi = robot_block; +bnet.CPD{Rnode} = gaussian_CPD(bnet, Rnode, 'mean', init_x(bi), 'cov', init_V(bi,bi), 'weights', eye(2)); +bnet.CPD{Lsnode} = gmux_CPD(bnet, Lsnode, 'cov', repmat(zeros(Lsz,Lsz), [1 1 nlandmarks]), ... + 'weights', repmat(eye(Lsz,Lsz), [1 1 nlandmarks])); +W = [eye(2) -eye(2)]; % Y = Ls - R, where Ls is the lower-numbered parent +bnet.CPD{Ynode} = gaussian_CPD(bnet, Ynode, 'mean', zeros(Ysz,1), 'cov', R, 'weights', W); + +% slice 2 +eclass = bnet.equiv_class; +W = [eye(2) eye(2)]; % R(t) = R(t-1) + U(t), where R(t-1) is the lower-numbered parent +bnet.CPD{eclass(Rnode,2)} = gaussian_CPD(bnet, Rnode+nnodes, 'mean', zeros(Rsz,1), 'cov', Q, 'weights', W); +for i=1:nlandmarks + bnet.CPD{eclass(Lnodes(i), 2)} = gaussian_CPD(bnet, Lnodes(i)+nnodes, 'mean', zeros(2,1), ... + 'cov', zeros(2,2), 'weights', eye(2)); +end diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/SLAM/mk_linear_slam.m b/sourcecodes/bnt-master/BNT/examples/dynamic/SLAM/mk_linear_slam.m new file mode 100644 index 00000000..b8a819a2 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/SLAM/mk_linear_slam.m @@ -0,0 +1,164 @@ +function [A,B,C,Q,R,Qbig,Rbig,init_x,init_V,robot_block,landmark_block,... + true_landmark_pos, true_robot_pos, true_data_assoc, ... + obs_rel_pos, ctrl_signal] = mk_linear_slam(varargin) + +% We create data from a linear system for testing SLAM algorithms. +% i.e. , new robot pos = old robot pos + ctrl_signal, which is just a displacement vector. +% and observation = landmark_pos - robot_pos, which is just a displacement vector. +% +% The behavior is determined by the following optional arguments: +% +% 'nlandmarks' - num. landmarks +% 'landmarks' - 'rnd' means random locations in the unit sqyare +% 'square' means at [1 1], [4 1], [4 4] and [1 4] +% 'T' - num steps to run +% 'ctrl' - 'stationary' means the robot remains at [0 0], +% 'leftright' means the robot receives a constant contol of [1 0], +% 'square' means we navigate the robot around the square +% 'data-assoc' - 'rnd' means we observe landmarks at random +% 'nn' means we observe the nearest neighbor landmark +% 'cycle' means we observe landmarks in order 1,2,.., 1, 2, ... + +args = varargin; +% get mandatory params +for i=1:2:length(args) + switch args{i}, + case 'nlandmarks', nlandmarks = args{i+1}; + case 'T', T = args{i+1}; + end +end + +% set defaults +true_landmark_pos = rand(2,nlandmarks); +true_data_assoc = []; + +% get args +for i=1:2:length(args) + switch args{i}, + case 'landmarks', + switch args{i+1}, + case 'rnd', true_landmark_pos = rand(2,nlandmarks); + case 'square', true_landmark_pos = [1 1; 4 1; 4 4; 1 4]'; + end + case 'ctrl', + switch args{i+1}, + case 'stationary', ctrl_signal = repmat([0 0]', 1, T); + case 'leftright', ctrl_signal = repmat([1 0]', 1, T); + case 'square', ctrl_signal = [repmat([1 0]', 1, T/4) repmat([0 1]', 1, T/4) ... + repmat([-1 0]', 1, T/4) repmat([0 -1]', 1, T/4)]; + end + case 'data-assoc', + switch args{i+1}, + case 'rnd', true_data_assoc = sample_discrete(normalise(ones(1,nlandmarks)),1,T); + case 'cycle', true_data_assoc = wrap(1:T, nlandmarks); + end + end +end +if isempty(true_data_assoc) + use_nn = 1; +else + use_nn = 0; +end + +%%%%%%%%%%%%%%%%%%%%%%%% +% generate data + +init_robot_pos = [0 0]'; +true_robot_pos = zeros(2, T); +true_rel_dist = zeros(2, T); +for t=1:T + if t>1 + true_robot_pos(:,t) = true_robot_pos(:,t-1) + ctrl_signal(:,t); + else + true_robot_pos(:,t) = init_robot_pos + ctrl_signal(:,t); + end + nn = argmin(dist2(true_robot_pos(:,t)', true_landmark_pos')); + if use_nn + true_data_assoc(t) = nn; + end + true_rel_dist(:,t) = true_landmark_pos(:, nn) - true_robot_pos(:,t); +end + + +R = 1e-3*eye(2); % noise added to observation +Q = 1e-3*eye(2); % noise added to robot motion + +% Create data set +obs_noise_seq = sample_gaussian([0 0]', R, T)'; +obs_rel_pos = true_rel_dist + obs_noise_seq; +%obs_rel_pos = true_rel_dist; + +%%%%%%%%%%%%%%%%%% +% Create params + + +% X(t) = A X(t-1) + B U(t) + noise(Q) + +% [L1] = [1 ] * [L1] + [0] * Ut + [0 ] +% [L2] [ 1 ] [L2] [0] [ 0 ] +% [R ]t [ 1] [R ]t-1 [1] [ Q] + +% Y(t)|S(t)=s = C(s) X(t) + noise(R) +% Yt|St=1 = [1 0 -1] * [L1] + R +% [L2] +% [R ] + +% Create indices into block structure +bs = 2*ones(1, nlandmarks+1); % sizes of blocks in state space +robot_block = block(nlandmarks+1, bs); +for i=1:nlandmarks + landmark_block(:,i) = block(i, bs)'; +end +Xsz = 2*(nlandmarks+1); % 2 values for each landmark plus robot +Ysz = 2; % observe relative location +Usz = 2; % input is (dx, dy) + + +% create block-diagonal trans matrix for each switch +A = zeros(Xsz, Xsz); +for i=1:nlandmarks + bi = landmark_block(:,i); + A(bi, bi) = eye(2); +end +bi = robot_block; +A(bi, bi) = eye(2); +A = repmat(A, [1 1 nlandmarks]); % same for all switch values + +% create block-diagonal system cov + + +Qbig = zeros(Xsz, Xsz); +bi = robot_block; +Qbig(bi,bi) = Q; % only add noise to robot motion +Qbig = repmat(Qbig, [1 1 nlandmarks]); + +% create input matrix +B = zeros(Xsz, Usz); +B(robot_block,:) = eye(2); % only add input to robot position +B = repmat(B, [1 1 nlandmarks]); + +% create observation matrix for each value of the switch node +% C(:,:,i) = (0 ... I ... -I) where the I is in the i'th posn. +% This computes L(i) - R +C = zeros(Ysz, Xsz, nlandmarks); +for i=1:nlandmarks + C(:, landmark_block(:,i), i) = eye(2); + C(:, robot_block, i) = -eye(2); +end + +% create observation cov for each value of the switch node +Rbig = repmat(R, [1 1 nlandmarks]); + +% initial conditions +init_x = zeros(Xsz, 1); +init_v = zeros(Xsz, Xsz); +bi = robot_block; +init_x(bi) = init_robot_pos; +%init_V(bi, bi) = 1e-5*eye(2); % very sure of robot posn +init_V(bi, bi) = Q; % simualate uncertainty due to 1 motion step +for i=1:nlandmarks + bi = landmark_block(:,i); + init_V(bi,bi)= 1e5*eye(2); % very uncertain of landmark psosns + %init_x(bi) = true_landmark_pos(:,i); + %init_V(bi,bi)= 1e-5*eye(2); % very sure of landmark psosns +end diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/SLAM/slam_kf.m b/sourcecodes/bnt-master/BNT/examples/dynamic/SLAM/slam_kf.m new file mode 100644 index 00000000..9844352b --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/SLAM/slam_kf.m @@ -0,0 +1,78 @@ +% Plot how precision matrix changes over time for KF solution + +seed = 0; +rand('state', seed); +randn('state', seed); + +[A,B,C,Q,R,Qbig,Rbig,init_x,init_V,robot_block,landmark_block,... + true_landmark_pos, true_robot_pos, true_data_assoc, ... + obs_rel_pos, ctrl_signal] = mk_linear_slam(... + 'nlandmarks', 6, 'T', 12, 'ctrl', 'leftright', 'data-assoc', 'cycle'); + +figure(1); clf +hold on +for i=1:nlandmarks + %text(true_landmark_pos(1,i), true_landmark_pos(2,i), sprintf('L%d',i)); + plot(true_landmark_pos(1,i), true_landmark_pos(2,i), '*') +end +hold off + + +[x, V] = kalman_filter(obs_rel_pos, A, C, Qbig, Rbig, init_x, init_V, ... + 'model', true_data_assoc, 'u', ctrl_signal, 'B', B); + +est_robot_pos = x(robot_block, :); +est_robot_pos_cov = V(robot_block, robot_block, :); + +for i=1:nlandmarks + bi = landmark_block(:,i); + est_landmark_pos(:,i) = x(bi, T); + est_landmark_pos_cov(:,:,i) = V(bi, bi, T); +end + + +if 0 +figure(1); hold on +for i=1:nlandmarks + h=plotgauss2d(est_landmark_pos(:,i), est_landmark_pos_cov(:,:,i)); + set(h, 'color', 'r') +end +hold off + +hold on +for t=1:T + h=plotgauss2d(est_robot_pos(:,t), est_robot_pos_cov(:,:,t)); + set(h,'color','r') + h=text(est_robot_pos(1,t), est_robot_pos(2,2), sprintf('R%d', t)); + set(h,'color','r') +end +hold off +end + + +P = zeros(size(V)); +for t=1:T + P(:,:,t) = inv(V(:,:,t)); +end + +if 0 + figure(2) + for t=1:T + subplot(T/2,2,t) + imagesc(P(1:2:end,1:2:end, t)) + colorbar + end +else + figure(2) + for t=1:T + subplot(T/2,2,t) + imagesc(V(1:2:end,1:2:end, t)) + colorbar + end +end + +% marginalize out robot position and then check structure +bi = landmark_block(:); +V = V(bi,bi,T); +P = inv(V); +P(1:2:end,1:2:end) diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/SLAM/slam_offline_loopy.m b/sourcecodes/bnt-master/BNT/examples/dynamic/SLAM/slam_offline_loopy.m new file mode 100644 index 00000000..6abc0fe0 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/SLAM/slam_offline_loopy.m @@ -0,0 +1,59 @@ +% Compare Kalman smoother with loopy + +seed = 0; +rand('state', seed); +randn('state', seed); +nlandmarks = 6; +T = 12; + +[A,B,C,Q,R,Qbig,Rbig,init_x,init_V,robot_block,landmark_block,... + true_landmark_pos, true_robot_pos, true_data_assoc, ... + obs_rel_pos, ctrl_signal] = mk_linear_slam(... + 'nlandmarks', nlandmarks, 'T', T, 'ctrl', 'leftright', 'data-assoc', 'cycle'); + +[xsmooth, Vsmooth] = kalman_smoother(obs_rel_pos, A, C, Qbig, Rbig, init_x, init_V, ... + 'model', true_data_assoc, 'u', ctrl_signal, 'B', B); + +est_robot_pos = xsmooth(robot_block, :); +est_robot_pos_cov = Vsmooth(robot_block, robot_block, :); + +for i=1:nlandmarks + bi = landmark_block(:,i); + est_landmark_pos(:,i) = xsmooth(bi, T); + est_landmark_pos_cov(:,:,i) = Vsmooth(bi, bi, T); +end + + +if 1 + [bnet, Unode, Snode, Lnodes, Rnode, Ynode, Lsnode] = ... + mk_gmux_robot_dbn(nlandmarks, Q, R, init_x, init_V, robot_block, landmark_block); + engine = pearl_unrolled_dbn_inf_engine(bnet, 'max_iter', 50, 'filename', ... + '/home/eecs/murphyk/matlab/loopyslam.txt'); +else + [bnet, Unode, Snode, Lnodes, Rnode, Ynode] = ... + mk_gmux2_robot_dbn(nlandmarks, Q, R, init_x, init_V, robot_block, landmark_block); + engine = jtree_dbn_inf_engine(bnet); +end + +nnodes = bnet.nnodes_per_slice; +evidence = cell(nnodes, T); +evidence(Ynode, :) = num2cell(obs_rel_pos, 1); +evidence(Unode, :) = num2cell(ctrl_signal, 1); +evidence(Snode, :) = num2cell(true_data_assoc); + +[engine, ll, niter] = enter_evidence(engine, evidence); +niter + +loopy_est_robot_pos = zeros(2, T); +for t=1:T + m = marginal_nodes(engine, Rnode, t); + loopy_est_robot_pos(:,t) = m.mu; +end + +for i=1:nlandmarks + m = marginal_nodes(engine, Lnodes(i), T); + loopy_est_landmark_pos(:,i) = m.mu; + loopy_est_landmark_pos_cov(:,:,i) = m.Sigma; +end + + diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/SLAM/slam_partial_kf.m b/sourcecodes/bnt-master/BNT/examples/dynamic/SLAM/slam_partial_kf.m new file mode 100644 index 00000000..3fe998be --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/SLAM/slam_partial_kf.m @@ -0,0 +1,107 @@ +% See how well partial Kalman filter updates work + +seed = 0; +rand('state', seed); +randn('state', seed); +nlandmarks = 6; +T = 12; + +[A,B,C,Q,R,Qbig,Rbig,init_x,init_V,robot_block,landmark_block,... + true_landmark_pos, true_robot_pos, true_data_assoc, ... + obs_rel_pos, ctrl_signal] = mk_linear_slam(... + 'nlandmarks', nlandmarks, 'T', T, 'ctrl', 'leftright', 'data-assoc', 'cycle'); + +% exact +[xe, Ve] = kalman_filter(obs_rel_pos, A, C, Qbig, Rbig, init_x, init_V, ... + 'model', true_data_assoc, 'u', ctrl_signal, 'B', B); + + +% approx +%k = nlandmarks-1; % exact +k = 3; +ndx = {}; +for t=1:T + landmarks = unique(true_data_assoc(t:-1:max(t-k,1))); + tmp = [landmark_block(:, landmarks) robot_block']; + ndx{t} = tmp(:); +end + +[xa, Va] = kalman_filter(obs_rel_pos, A, C, Qbig, Rbig, init_x, init_V, ... + 'model', true_data_assoc, 'u', ctrl_signal, 'B', B, ... + 'ndx', ndx); + + + +nrows = 10; +stepsize = T/(2*nrows); +ts = 1:stepsize:T; + +if 1 % plot + +clim = [0 max(max(Va(:,:,end)))]; + +figure(2) +if 0 + imagesc(Ve(1:2:end,1:2:end, T)) + clim = get(gca,'clim'); +else + i = 1; + for t=ts(:)' + subplot(nrows,2,i) + i = i + 1; + imagesc(Ve(1:2:end,1:2:end, t)) + set(gca, 'clim', clim) + colorbar + end +end +suptitle('exact') + + +figure(3) +if 0 + imagesc(Va(1:2:end,1:2:end, T)) + set(gca,'clim', clim) +else + i = 1; + for t=ts(:)' + subplot(nrows,2,i) + i = i+1; + imagesc(Va(1:2:end,1:2:end, t)) + set(gca, 'clim', clim) + colorbar + end +end +suptitle('approx') + + +figure(4) +i = 1; +for t=ts(:)' + subplot(nrows,2,i) + i = i+1; + Vd = Va(1:2:end,1:2:end, t) - Ve(1:2:end,1:2:end,t); + imagesc(Vd) + set(gca, 'clim', clim) + colorbar +end +suptitle('diff') + +end % all plot + + +for t=1:T + %err(t)=rms(xa(:,t), xe(:,t)); + err(t)=rms(xa(1:end-2,t), xe(1:end-2,t)); % exclude robot +end +figure(5);plot(err) +title('rms mean pos') + + +for t=1:T + i = 1:2*nlandmarks; + denom = Ve(i,i,t) + (Ve(i,i,t)==0); + Vd =(Va(i,i,t)-Ve(i,i,t)) ./ denom; + Verr(t) = max(Vd(:)); +end +figure(6); plot(Verr) +title('max relative Verr') diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/SLAM/slam_stationary_loopy.m b/sourcecodes/bnt-master/BNT/examples/dynamic/SLAM/slam_stationary_loopy.m new file mode 100644 index 00000000..0272d3f6 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/SLAM/slam_stationary_loopy.m @@ -0,0 +1,155 @@ +% This is like skf_data_assoc_gmux, except the objects don't move. +% We are uncertain of their initial positions, and get more and more observations +% over time. The goal is to test deterministic links (0 covariance). +% This is like robot1, except the robot doesn't move and is always at [0 0], +% so the relative location is simply L(s). + +nobj = 2; +N = nobj+2; +Xs = 1:nobj; +S = nobj+1; +Y = nobj+2; + +intra = zeros(N,N); +inter = zeros(N,N); +intra([Xs S], Y) =1; +for i=1:nobj + inter(Xs(i), Xs(i))=1; +end + +Xsz = 2; % state space = (x y) +Ysz = 2; +ns = zeros(1,N); +ns(Xs) = Xsz; +ns(Y) = Ysz; +ns(S) = nobj; + +bnet = mk_dbn(intra, inter, ns, 'discrete', S, 'observed', [S Y]); + +% For each object, we have +% X(t+1) = F X(t) + noise(Q) +% Y(t) = H X(t) + noise(R) +F = eye(2); +H = eye(2); +Q = 0*eye(Xsz); % no noise in dynamics +R = eye(Ysz); + +init_state{1} = [10 10]'; +init_state{2} = [10 -10]'; +init_cov = eye(2); + +% Uncertain of initial state (position) +for i=1:nobj + bnet.CPD{Xs(i)} = gaussian_CPD(bnet, Xs(i), 'mean', init_state{i}, 'cov', init_cov); +end +bnet.CPD{S} = root_CPD(bnet, S); % always observed +bnet.CPD{Y} = gmux_CPD(bnet, Y, 'cov', repmat(R, [1 1 nobj]), 'weights', repmat(H, [1 1 nobj])); +% slice 2 +eclass = bnet.equiv_class; +for i=1:nobj + bnet.CPD{eclass(Xs(i), 2)} = gaussian_CPD(bnet, Xs(i)+N, 'mean', zeros(Xsz,1), 'cov', Q, 'weights', F); +end + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Create LDS params + +% X(t) = A X(t-1) + B U(t) + noise(Q) + +% [L11] = [1 ] * [L1] + [Q ] +% [L2] [ 1] [L2] [ Q] + +% Y(t)|S(t)=s = C(s) X(t) + noise(R) +% Yt|St=1 = [1 0] * [L1] + R +% [L2] + +nlandmarks = nobj; + +% Create indices into block structure +bs = 2*ones(1, nobj); % sizes of blocks in state space +for i=1:nlandmarks + landmark_block(:,i) = block(i, bs)'; +end +Xsz = 2*(nlandmarks); % 2 values for each landmark plus robot +Ysz = 2; % observe relative location + +% create block-diagonal trans matrix for each switch +A = zeros(Xsz, Xsz); +for i=1:nlandmarks + bi = landmark_block(:,i); + A(bi, bi) = eye(2); +end +A = repmat(A, [1 1 nlandmarks]); % same for all switch values + +% create block-diagonal system cov +Qbig = zeros(Xsz, Xsz); +Qbig = repmat(Qbig, [1 1 nlandmarks]); + + +% create observation matrix for each value of the switch node +% C(:,:,i) = (0 ... I ...) where the I is in the i'th posn. +C = zeros(Ysz, Xsz, nlandmarks); +for i=1:nlandmarks + C(:, landmark_block(:,i), i) = eye(2); +end + +% create observation cov for each value of the switch node +Rbig = repmat(R, [1 1 nlandmarks]); + +% initial conditions +init_x = [init_state{1}; init_state{2}]; +init_V = zeros(Xsz, Xsz); +for i=1:nlandmarks + bi = landmark_block(:,i); + init_V(bi,bi) = init_cov; +end + + + +%%%%%%%%%%%%%%%% +% Observe objects at random +T = 10; +evidence = cell(N, T); +data_assoc = sample_discrete(normalise(ones(1,nobj)), 1, T); +evidence(S,:) = num2cell(data_assoc); +evidence = sample_dbn(bnet, 'evidence', evidence); + + +% Inference +ev = cell(N,T); +ev(bnet.observed,:) = evidence(bnet.observed, :); +y = cell2num(evidence(Y,:)); + +engine = pearl_unrolled_dbn_inf_engine(bnet); +engine = enter_evidence(engine, ev); + +loopy_est_pos = zeros(2, nlandmarks); +loopy_est_pos_cov = zeros(2, 2, nlandmarks); +for i=1:nobj + m = marginal_nodes(engine, Xs(i), T); + loopy_est_pos(:,i) = m.mu; + loopy_est_pos_cov(:,:,i) = m.Sigma; +end + + +[xsmooth, Vsmooth] = kalman_smoother(y, A, C, Qbig, Rbig, init_x, init_V, 'model', data_assoc); + +kf_est_pos = zeros(2, nlandmarks); +kf_est_pos_cov = zeros(2, 2, nlandmarks); +for i=1:nlandmarks + bi = landmark_block(:,i); + kf_est_pos(:,i) = xsmooth(bi, T); + kf_est_pos_cov(:,:,i) = Vsmooth(bi, bi, T); +end + + +kf_est_pos +loopy_est_pos + +kf_est_pos_time = zeros(2, nlandmarks, T); +for t=1:T + for i=1:nlandmarks + bi = landmark_block(:,i); + kf_est_pos_time(:,i,t) = xsmooth(bi, t); + end +end +kf_est_pos_time % same for all t since smoothed diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/arhmm1.m b/sourcecodes/bnt-master/BNT/examples/dynamic/arhmm1.m new file mode 100644 index 00000000..ac1f7fce --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/arhmm1.m @@ -0,0 +1,42 @@ +% Make an HMM with autoregressive Gaussian observations (switching AR model) +% X1 -> X2 +% | | +% v v +% Y1 -> Y2 + +seed = 0; +rand('state', seed); +randn('state', seed); + +intra = zeros(2); +intra(1,2) = 1; +inter = zeros(2); +inter(1,1) = 1; +inter(2,2) = 1; +n = 2; + +Q = 2; % num hidden states +O = 2; % size of observed vector + +ns = [Q O]; +dnodes = 1; +onodes = [2]; +bnet = mk_dbn(intra, inter, ns, 'discrete', dnodes, 'observed', onodes); + +bnet.CPD{1} = tabular_CPD(bnet, 1); +bnet.CPD{2} = gaussian_CPD(bnet, 2); +bnet.CPD{3} = tabular_CPD(bnet, 3); +bnet.CPD{4} = gaussian_CPD(bnet, 4); + + +T = 10; % fixed length sequences + +engine = {}; +%engine{end+1} = hmm_inf_engine(bnet); +engine{end+1} = jtree_unrolled_dbn_inf_engine(bnet, T); +%engine{end+1} = smoother_engine(hmm_2TBN_inf_engine(bnet)); +%engine{end+1} = smoother_engine(jtree_2TBN_inf_engine(bnet)); + +inf_time = cmp_inference_dbn(bnet, engine, T, 'check_ll',1); +learning_time = cmp_learning_dbn(bnet, engine, T, 'check_ll', 1); + diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/bat1.m b/sourcecodes/bnt-master/BNT/examples/dynamic/bat1.m new file mode 100644 index 00000000..3b24e144 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/bat1.m @@ -0,0 +1,44 @@ +% Compare the speeds of various inference engines on the BAT DBN +[bnet, names] = mk_bat_dbn; + +T = 3; % fixed length sequence - we make it short just for speed + +USEC = exist('@jtree_C_inf_engine/collect_evidence','file'); + +disp('constructing engines for BAT'); +engine = {}; % time in seconds for inference +engine{end+1} = jtree_unrolled_dbn_inf_engine(bnet, T, 'useC', USEC); % 0.39 +engine{end+1} = smoother_engine(jtree_2TBN_inf_engine(bnet)); % 4.89 +engine{end+1} = jtree_dbn_inf_engine(bnet); % 4.45 +if 0 +engine{end+1} = jtree_ndx_dbn_inf_engine(bnet, 'ndx_type', 'SD'); % 2.98 +engine{end+1} = jtree_ndx_dbn_inf_engine(bnet, 'ndx_type', 'D'); % 3.52 +engine{end+1} = jtree_ndx_dbn_inf_engine(bnet, 'ndx_type', 'B'); % 2.40 +if USEC, engine{end+1} = jtree_C_dbn_inf_engine(bnet); end % 3.54 +%engine{end+1} = hmm_inf_engine(bnet, onodes); % too big +end + +%tic; engine{end+1} = frontier_inf_engine(bnet); toc % very slow +% The frontier engine thrashes badly on the BAT network +%tic; engine{end+1} = bk_inf_engine(bnet, 'exact', onodes); toc % SLOW! + +%tic; engine{end+1} = bk_inf_engine(bnet, 'ff', onodes); toc + +%clusters{1} = [stringmatch({'LeftClr', 'RightClr', 'LatAct', 'Xdot', 'InLane'}, names)]; +%clusters{2} = [stringmatch({'FwdAct', 'Ydot', 'Stopped', 'EngStatus', 'FBStatus'}, names)]; + +%tic; engine{end+1} = bk_inf_engine(bnet, clusters, onodes); toc + +disp('inference') +time = cmp_inference_dbn(bnet, engine, T) + +disp('learning') +time = cmp_learning_dbn(bnet, engine, T) + + + + + + + + diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/bkff1.m b/sourcecodes/bnt-master/BNT/examples/dynamic/bkff1.m new file mode 100644 index 00000000..c5b62332 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/bkff1.m @@ -0,0 +1,23 @@ +% Compare different implementations of fully factored Boyen Koller + +water = 1; +if water + bnet = mk_water_dbn; +else + N = 5; + Q = 2; + Y = 2; + bnet = mk_chmm(N, Q, Y); +end +ss = length(bnet.intra); + +engine = {}; +engine{end+1} = bk_inf_engine(bnet, 'clusters', 'ff'); +engine{end+1} = bk_ff_hmm_inf_engine(bnet); +E = length(engine); + +T = 5; +time = cmp_inference_dbn(bnet, engine, T, 'singletons_only', 1) + + + diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/chmm1.m b/sourcecodes/bnt-master/BNT/examples/dynamic/chmm1.m new file mode 100644 index 00000000..10df79d8 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/chmm1.m @@ -0,0 +1,41 @@ +% Compare the speeds of various inference engines on a coupled HMM + +N = 3; +Q = 2; +rand('state', 0); +randn('state', 0); +discrete = 0; +if discrete + Y = 2; % size of output alphabet +else + Y = 3; % size of observed vectors +end +coupled = 1; +bnet = mk_chmm(N, Q, Y, discrete, coupled); +%bnet = mk_fhmm(N, Q, Y, discrete); % factorial HMM +ss = length(bnet.node_sizes_slice); + +T = 3; + +USEC = exist('@jtree_C_inf_engine/collect_evidence','file'); + +engine = {}; +engine{end+1} = jtree_dbn_inf_engine(bnet); +%engine{end+1} = jtree_ndx_dbn_inf_engine(bnet, 'ndx_type', 'SD'); +%engine{end+1} = jtree_ndx_dbn_inf_engine(bnet, 'ndx_type', 'D'); +%engine{end+1} = jtree_ndx_dbn_inf_engine(bnet, 'ndx_type', 'B'); +if USEC, engine{end+1} = jtree_C_dbn_inf_engine(bnet); end +engine{end+1} = hmm_inf_engine(bnet); +engine{end+1} = jtree_unrolled_dbn_inf_engine(bnet, T); + +% times in matlab N=4 Q=4 T=5 (* = winner) +% jtree SD B hmm dhmm unrolled +% 0.6266 1.1563 8.3815 0.3069 0.1948* 0.8654 inf +% 0.9057* 2.1522 12.6314 2.6847 2.3107 3.1905 learn + +%engine{end+1} = bk_inf_engine(bnet, 'ff', onodes); +%engine{end+1} = pearl_unrolled_dbn_inf_engine(bnet, T); + +inf_time = cmp_inference_dbn(bnet, engine, T) +learning_time = cmp_learning_dbn(bnet, engine, T) + diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/cmp_inference_dbn.m b/sourcecodes/bnt-master/BNT/examples/dynamic/cmp_inference_dbn.m new file mode 100644 index 00000000..da54095f --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/cmp_inference_dbn.m @@ -0,0 +1,100 @@ +function [time, engine] = cmp_inference_dbn(bnet, engine, T, varargin) +% CMP_INFERENCE_DBN Compare several inference engines on a DBN +% function [time, engine] = cmp_inference_dbn(bnet, engine, T, ...) +% +% engine{i} is the i'th inference engine. +% time(e) = elapsed time for doing inference with engine e +% +% The list below gives optional arguments [default value in brackets]. +% +% exact - specifies which engines do exact inference [ 1:length(engine) ] +% singletons_only - if 1, we only call marginal_nodes, else this and marginal_family [0] +% check_ll - 1 means we check that the log-likelihoods are correct [1] + +% set default params +exact = 1:length(engine); +singletons_only = 0; +check_ll = 1; +onodes = bnet.observed; + +args = varargin; +nargs = length(args); +for i=1:2:nargs + switch args{i}, + case 'exact', exact = args{i+1}; + case 'singletons_only', singletons_only = args{i+1}; + case 'check_ll', check_ll = args{i+1}; + case 'observed', onodes = args{i+1}; + otherwise, + error(['unrecognized argument ' args{i}]) + end +end + +E = length(engine); +ref = exact(1); % reference + +ss = length(bnet.intra); +ev = sample_dbn(bnet, 'length', T); +evidence = cell(ss,T); +evidence(onodes,:) = ev(onodes, :); + +for i=1:E + tic; + [engine{i}, ll(i)] = enter_evidence(engine{i}, evidence); + time(i)=toc; + fprintf('engine %d took %6.4f seconds\n', i, time(i)); +end + +cmp = mysetdiff(exact, ref); +if check_ll + for i=cmp(:)' + if ~approxeq(ll(ref), ll(i)) + error(['engine ' num2str(i) ' has wrong ll']) + end + end +end +ll + +hnodes = mysetdiff(1:ss, onodes); + +if ~singletons_only + get_marginals(engine, hnodes, exact, 0, T); +end +get_marginals(engine, hnodes, exact, 1, T); + +%%%%%%%%%% + +function get_marginals(engine, hnodes, exact, singletons, T) + +bnet = bnet_from_engine(engine{1}); +N = length(bnet.intra); +cnodes_bitv = zeros(1,N); +cnodes_bitv(bnet.cnodes) = 1; +ref = exact(1); % reference +cmp = exact(2:end); +E = length(engine); +m = cell(1,E); + +for t=1:T + for n=1:N + %for n=hnodes(:)' + for e=1:E + if singletons + m{e} = marginal_nodes(engine{e}, n, t); + else + m{e} = marginal_family(engine{e}, n, t); + end + end + for e=cmp(:)' + assert(isequal(m{e}.domain, m{ref}.domain)); + if cnodes_bitv(n) & isfield(m{e}, 'mu') & isfield(m{ref}, 'mu') + wrong = ~approxeq(m{ref}.mu, m{e}.mu) | ~approxeq(m{ref}.Sigma, m{e}.Sigma); + else + wrong = ~approxeq(m{ref}.T(:), m{e}.T(:)); + end + if wrong + error(sprintf('engine %d is wrong; n=%d, t=%d, fam=%d', e, n, t, ~singletons)) + end + end + end +end diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/cmp_learning_dbn.m b/sourcecodes/bnt-master/BNT/examples/dynamic/cmp_learning_dbn.m new file mode 100644 index 00000000..d6138f9d --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/cmp_learning_dbn.m @@ -0,0 +1,89 @@ +function [time, CPD, LL, cases] = cmp_learning_dbn(bnet, engine, T, varargin) +% CMP_LEARNING_DBN Compare a bunch of inference engines by learning a DBN +% function [time, CPD, LL, cases] = cmp_learning_dbn(bnet, engine, exact, T, ncases, max_iter) +% +% engine{i} is the i'th inference engine. +% time(e) = elapsed time for doing inference with engine e +% CPD{e,c} is the learned CPD for eclass c in engine e +% LL{e} is the learning curve for engine e +% cases{i} is the i'th training case +% +% The list below gives optional arguments [default value in brackets]. +% +% exact - specifies which engines do exact inference [ 1:length(engine) ] +% check_ll - 1 means we check that the log-likelihoods are correct [1] +% ncases - num. random training cases [2] +% max_iter - max. num EM iterations [2] + +% set default params +exact = 1:length(engine); +check_ll = 1; +ncases = 2; +max_iter = 2; + +args = varargin; +nargs = length(args); +for i=1:2:nargs + switch args{i}, + case 'exact', exact = args{i+1}; + case 'check_ll', check_ll = args{i+1}; + case 'ncases', ncases = args{i+1}; + case 'max_iter', max_iter = args{i+1}; + otherwise, + error(['unrecognized argument ' args{i}]) + end +end + +E = length(engine); +ss = length(bnet.intra); +onodes = bnet.observed; + +cases = cell(1, ncases); +for i=1:ncases + ev = sample_dbn(bnet, 'length', T); + cases{i} = cell(ss,T); + cases{i}(onodes,:) = ev(onodes, :); +end + +LL = cell(1,E); +time = zeros(1,E); +for i=1:E + tic + [bnet2{i}, LL{i}] = learn_params_dbn_em(engine{i}, cases, 'max_iter', max_iter); + time(i) = toc; + fprintf('engine %d took %6.4f seconds\n', i, time(i)); +end + +ref = exact(1); % reference +cmp = mysetdiff(exact, ref); +if check_ll + for i=cmp(:)' + if ~approxeq(LL{ref}, LL{i}) + error(['engine ' num2str(i) ' has wrong ll']) + end + end +end + +nCPDs = length(bnet.CPD); +CPD = cell(E, nCPDs); +tabular = zeros(1, nCPDs); +for i=1:E + temp = bnet2{i}; + for c=1:nCPDs + tabular(c) = isa(temp.CPD{c}, 'tabular_CPD'); + CPD{i,c} = struct(temp.CPD{c}); + end +end + +for i=cmp(:)' + for c=1:nCPDs + if tabular(c) + assert(approxeq(CPD{i,c}.CPT, CPD{ref,c}.CPT)); + else + assert(approxeq(CPD{i,c}.mean, CPD{ref,c}.mean)); + assert(approxeq(CPD{i,c}.cov, CPD{ref,c}.cov)); + assert(approxeq(CPD{i,c}.weights, CPD{ref,c}.weights)); + end + end +end + diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/cmp_online_inference.m b/sourcecodes/bnt-master/BNT/examples/dynamic/cmp_online_inference.m new file mode 100644 index 00000000..5360d120 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/cmp_online_inference.m @@ -0,0 +1,97 @@ +function [time, engine] = cmp_online_inference(bnet, engine, T, varargin) +% CMP_ONLINE_INFERENCE Compare several online inference engines on a DBN +% function [time, engine] = cmp_online_inference(bnet, engine, T, ...) +% +% engine{i} is the i'th inference engine. +% time(e) = elapsed time for doing inference with engine e +% +% The list below gives optional arguments [default value in brackets]. +% +% exact - specifies which engines do exact inference [ 1:length(engine) ] +% singletons_only - if 1, we only call marginal_nodes, else this and marginal_family [0] +% check_ll - 1 means we check that the log-likelihoods are correct [1] + +% set default params +exact = 1:length(engine); +singletons_only = 0; +check_ll = 1; +onodes = bnet.observed; + +args = varargin; +nargs = length(args); +for i=1:2:nargs + switch args{i}, + case 'exact', exact = args{i+1}; + case 'singletons_only', singletons_only = args{i+1}; + case 'check_ll', check_ll = args{i+1}; + case 'observed', onodes = args{i+1}; + otherwise, + error(['unrecognized argument ' args{i}]) + end +end + +E = length(engine); +ref = exact(1); % reference +cmp = mysetdiff(exact, ref); + +ss = length(bnet.intra); +hnodes = mysetdiff(1:ss, onodes); +ev = sample_dbn(bnet, 'length', T); +evidence = cell(ss,T); +evidence(onodes,:) = ev(onodes, :); + +time = zeros(1,E); +for t=1:T + for e=1:E + tic; + [engine{e}, ll(e)] = enter_evidence(engine{e}, evidence(:,t), t); + time(e)= time(e) + toc; + end + if check_ll + for e=cmp(:)' + if ~approxeq(ll(ref), ll(e)) + error(['engine ' num2str(e) ' has wrong ll']) + end + end + end + if ~singletons_only + check_marginals(engine, hnodes, exact, 0, t); + end + check_marginals(engine, hnodes, exact, 1, t); +end + + +%%%%%%%%%% + +function check_marginals(engine, hnodes, exact, singletons, t) + +bnet = bnet_from_engine(engine{1}); +N = length(bnet.intra); +cnodes_bitv = zeros(1,N); +cnodes_bitv(bnet.cnodes) = 1; +ref = exact(1); % reference +cmp = exact(2:end); +E = length(engine); +m = cell(1,E); + +for n=1:N + %for n=hnodes(:)' + for e=1:E + if singletons + m{e} = marginal_nodes(engine{e}, n, t); + else + m{e} = marginal_family(engine{e}, n, t); + end + end + for e=cmp(:)' + assert(isequal(m{e}.domain, m{ref}.domain)); + if cnodes_bitv(n) & isfield(m{e}, 'mu') & isfield(m{ref}, 'mu') + wrong = ~approxeq(m{ref}.mu, m{e}.mu) | ~approxeq(m{ref}.Sigma, m{e}.Sigma); + else + wrong = ~approxeq(m{ref}.T(:), m{e}.T(:)); + end + if wrong + error(sprintf('engine %d is wrong; n=%d, t=%d, fam=%d', e, n, t, ~singletons)) + end + end +end diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/dhmm1.m b/sourcecodes/bnt-master/BNT/examples/dynamic/dhmm1.m new file mode 100644 index 00000000..a3c4084d --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/dhmm1.m @@ -0,0 +1,66 @@ +% Make an HMM with discrete observations +% X1 -> X2 +% | | +% v v +% Y1 Y2 + +intra = zeros(2); +intra(1,2) = 1; +inter = zeros(2); +inter(1,1) = 1; +n = 2; + +Q = 2; % num hidden states +O = 2; % num observable symbols + +ns = [Q O]; +dnodes = 1:2; +onodes = [2]; +eclass1 = [1 2]; +eclass2 = [3 2]; +bnet = mk_dbn(intra, inter, ns, 'discrete', dnodes, 'eclass1', eclass1, 'eclass2', eclass2, ... + 'observed', onodes); + +rand('state', 0); +prior1 = normalise(rand(Q,1)); +transmat1 = mk_stochastic(rand(Q,Q)); +obsmat1 = mk_stochastic(rand(Q,O)); +bnet.CPD{1} = tabular_CPD(bnet, 1, prior1); +bnet.CPD{2} = tabular_CPD(bnet, 2, obsmat1); +bnet.CPD{3} = tabular_CPD(bnet, 3, transmat1); + + +T = 5; % fixed length sequences + +engine = {}; +engine{end+1} = jtree_unrolled_dbn_inf_engine(bnet, T); +engine{end+1} = hmm_inf_engine(bnet); +engine{end+1} = smoother_engine(hmm_2TBN_inf_engine(bnet)); +engine{end+1} = smoother_engine(jtree_2TBN_inf_engine(bnet)); +if 1 +%engine{end+1} = frontier_inf_engine(bnet); % broken +engine{end+1} = bk_inf_engine(bnet, 'clusters', {[1]}); +engine{end+1} = jtree_dbn_inf_engine(bnet); +end + +inf_time = cmp_inference_dbn(bnet, engine, T); + +ncases = 2; +max_iter = 2; +[learning_time, CPD, LL, cases] = cmp_learning_dbn(bnet, engine, T, 'ncases', ncases, 'max_iter', max_iter); + +% Compare to HMM toolbox + +data = zeros(ncases, T); +for i=1:ncases + %data(i,:) = cat(2, cases{i}{onodes,:}); + data(i,:) = cell2num(cases{i}(onodes,:)); +end +[LL2, prior2, transmat2, obsmat2] = dhmm_em(data, prior1, transmat1, obsmat1, 'max_iter', max_iter); + +e = 1; +assert(approxeq(prior2, CPD{e,1}.CPT)) +assert(approxeq(obsmat2, CPD{e,2}.CPT)) +assert(approxeq(transmat2, CPD{e,3}.CPT)) +assert(approxeq(LL2, LL{e})) + diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/ehmm1.m b/sourcecodes/bnt-master/BNT/examples/dynamic/ehmm1.m new file mode 100644 index 00000000..d6af4147 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/ehmm1.m @@ -0,0 +1,24 @@ +% make the structure of an embedded HMM with 2 rows and 3 columns + +% 1------------>2 +% |\ \ | \ \ +% 3->4->5 6->7->8 + +n = 8; +dag = zeros(n); +dag(1,[2 3 4 5])=1; +dag(2,[6 7 8])=1; +for i=3:4 + dag(i,i+1)=1; +end +for i=6:7 + dag(i,i+1)=1; +end +ns = 2*ones(1,n); +bnet = mk_bnet(dag,ns); +for i=1:n + bnet.CPD{i}=tabular_CPD(bnet,i); +end +[jtree, root, cliques] = graph_to_jtree(moralize(bnet.dag), ones(1,n), {}, {}); +%[jtree, root, cliques, B, w, elim_order, moral_edges, fill_in_edges] = dag_to_jtree(bnet); + diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/fhmm_infer.m b/sourcecodes/bnt-master/BNT/examples/dynamic/fhmm_infer.m new file mode 100644 index 00000000..62eec4a3 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/fhmm_infer.m @@ -0,0 +1,324 @@ +function [loglik, gamma] = fhmm_infer(inter, CPTs_slice1, CPTs, obsmat, node_sizes) +% FHMM_INFER Exact inference for a factorial HMM. +% [loglik, gamma] = fhmm_infer(inter, CPTs_slice1, CPTs, obsmat, node_sizes) +% +% Inputs: +% inter - the inter-slice adjacency matrix +% CPTs_slice1{s}(j) = Pr(Q(s,1) = j) where Q(s,t) = hidden node s in slice t +% CPT{s}(i1, i2, ..., j) = Pr(Q(s,t) = j | Pa(s,t-1) = i1, i2, ...), +% obsmat(i,t) = Pr(y(t) | Q(t)=i) +% node_sizes is a vector with the cardinality of the hidden nodes +% +% Outputs: +% gamma(i,t) = Pr(X(t)=i | O(1:T)) as in an HMM, +% except that i is interpreted as an M digit, base-K number (if there are M chains each of cardinality K). +% +% +% For M chains each of cardinality K, the frontiers (i.e., cliques) +% contain M+1 nodes, and it takes M steps to advance the frontier by one time step, +% so the run time is O(T M K^(M+1)). +% An HMM takes O(T S^2) where S is the size of the state space. +% Collapsing the FHMM to an HMM results in S = K^M. +% For details, see +% "The Factored Frontier Algorithm for Approximate Inference in DBNs", +% Kevin Murphy and Yair Weiss, submitted to NIPS 2000. +% +% The frontier algorithm makes the following topological assumptions: +% +% - All nodes are persistent (connect to the next slice) +% - No connections within a timeslice +% - There is a single observation variable, which depends on all the hidden nodes +% - Each node can have several parents in the previous time slice (generalizes a FHMM slightly) +% + +% The forwards pass of the frontier algorithm can be explained with the following example. +% Suppose we have 3 hidden nodes per slice, A, B, C. +% The goal is to compute alpha(j, t) = Pr( (A_t,B_t,C_t)=j | Y(1:t)) +% We move alpha from t to t+1 one node at a time, as follows. +% We define the following quantities: +% s([a1 b1 c1], 1) = Prob(A(t)=a1, B(t)=b1, C(t)=c1 | Y(1:t)) = alpha(j, t) +% s([a2 b1 c1], 2) = Prob(A(t+1)=a2, B(t)=b1, C(t)=c1 | Y(1:t)) +% s([a2 b2 c1], 3) = Prob(A(t+1)=a2, B(t+1)=b2, C(t)=c1 | Y(1:t)) +% s([a2 b2 c2], 4) = Prob(A(t+1)=a2, B(t+1)=b2, C(t+1)=c2 | Y(1:t)) +% s([a2 b2 c2], 5) = Prob(A(t+1)=a2, B(t+1)=b2, C(t+1)=c2 | Y(1:t+1)) = alpha(j, t+1) +% +% These can be computed recursively as follows: +% +% s([a2 b1 c1], 2) = sum_{a1} P(a2|a1) s([a1 b1 c1], 1) +% s([a2 b2 c1], 3) = sum_{b1} P(b2|b1) s([a2 b1 c1], 2) +% s([a2 b2 c2], 4) = sum_{c1} P(c2|c1) s([a2 b2 c1], 1) +% s([a2 b2 c2], 5) = normalise( s([a2 b2 c2], 4) .* P(Y(t+1)|a2,b2,c2) + + +[kk,ll,mm] = make_frontier_indices(inter, node_sizes); % can pass in as args + +scaled = 1; + +M = length(node_sizes); +S = prod(node_sizes); +T = size(obsmat, 2); + +alpha = zeros(S, T); +beta = zeros(S, T); +gamma = zeros(S, T); +scale = zeros(1,T); +tiny = exp(-700); + + +alpha(:,1) = make_prior_from_CPTs(CPTs_slice1, node_sizes); +alpha(:,1) = alpha(:,1) .* obsmat(:, 1); + +if scaled + s = sum(alpha(:,1)); + if s==0, s = s + tiny; end + scale(1) = 1/s; +else + scale(1) = 1; +end +alpha(:,1) = alpha(:,1) * scale(1); + +%a = zeros(S, M+1); +%b = zeros(S, M+1); +anew = zeros(S,1); +aold = zeros(S,1); +bnew = zeros(S,1); +bold = zeros(S,1); + +for t=2:T + %a(:,1) = alpha(:,t-1); + aold = alpha(:,t-1); + + c = 1; + for i=1:M + ns = node_sizes(i); + cpt = CPTs{i}; + for j=1:S + s = 0; + for xx=1:ns + %k = kk(xx,j,i); + %l = ll(xx,j,i); + k = kk(c); + l = ll(c); + c = c + 1; + % s = s + a(k,i) * CPTs{i}(l); + s = s + aold(k) * cpt(l); + end + %a(j,i+1) = s; + anew(j) = s; + end + aold = anew; + end + + %alpha(:,t) = a(:,M+1) .* obsmat(:, obs(t)); + alpha(:,t) = anew .* obsmat(:, t); + + if scaled + s = sum(alpha(:,t)); + if s==0, s = s + tiny; end + scale(t) = 1/s; + else + scale(t) = 1; + end + alpha(:,t) = alpha(:,t) * scale(t); + +end + + +beta(:,T) = ones(S,1) * scale(T); +for t=T-1:-1:1 + %b(:,1) = beta(:,t+1) .* obsmat(:, obs(t+1)); + bold = beta(:,t+1) .* obsmat(:, t+1); + + c = 1; + for i=1:M + ns = node_sizes(i); + cpt = CPTs{i}; + for j=1:S + s = 0; + for xx=1:ns + %k = kk(xx,j,i); + %m = mm(xx,j,i); + k = kk(c); + m = mm(c); + c = c + 1; + % s = s + b(k,i) * CPTs{i}(m); + s = s + bold(k) * cpt(m); + end + %b(j,i+1) = s; + bnew(j) = s; + end + bold = bnew; + end + % beta(:,t) = b(:,M+1) * scale(t); + beta(:,t) = bnew * scale(t); +end + + +if scaled + loglik = -sum(log(scale)); % scale(i) is finite +else + lik = alpha(:,1)' * beta(:,1); + loglik = log(lik+tiny); +end + +for t=1:T + gamma(:,t) = normalise(alpha(:,t) .* beta(:,t)); +end + +%%%%%%%%%%% + +function [kk,ll,mm] = make_frontier_indices(inter, node_sizes) +% +% Precompute indices for use in the frontier algorithm. +% These only depend on the topology, not the parameters or data. +% Hence we can compute them outside of fhmm_infer. +% This saves a lot of run-time computation. + +M = length(node_sizes); +S = prod(node_sizes); + +mns = max(node_sizes); +kk = zeros(mns, S, M); +ll = zeros(mns, S, M); +mm = zeros(mns, S, M); + +for i=1:M + for j=1:S + u = ind2subv(node_sizes, j); + x = u(i); + for xx=1:node_sizes(i) + uu = u; + uu(i) = xx; + k = subv2ind(node_sizes, uu); + kk(xx,j,i) = k; + ps = find(inter(:,i)==1); + ps = ps(:)'; + l = subv2ind(node_sizes([ps i]), [uu(ps) x]); % sum over parent + ll(xx,j,i) = l; + m = subv2ind(node_sizes([ps i]), [u(ps) xx]); % sum over child + mm(xx,j,i) = m; + end + end +end + +%%%%%%%%% + +function prior=make_prior_from_CPTs(indiv_priors, node_sizes) +% +% composite_prior=make_prior(individual_priors, node_sizes) +% Make the prior for the first node in a Markov chain +% from the priors on each node in the equivalent DBN. +% prior{i}(j) = Pr(X_i=j), where X_i is the i'th node in slice 1. +% composite_prior(i) = Pr(slice1 = i). + +n = length(indiv_priors); +S = prod(node_sizes); +prior = zeros(S,1); +for i=1:S + vi = ind2subv(node_sizes, i); + p = 1; + for k=1:n + p = p * indiv_priors{k}(vi(k)); + end + prior(i) = p; +end + + + +%%%%%%%%%%% + +function [loglik, alpha, beta] = FHMM_slow(inter, CPTs_slice1, CPTs, obsmat, node_sizes, data) +% +% Same as the above, except we don't use the optimization of computing the indices outside the loop. + + +scaled = 1; + +M = length(node_sizes); +S = prod(node_sizes); +[numex T] = size(data); + +obs = data; + +alpha = zeros(S, T); +beta = zeros(S, T); +a = zeros(S, M+1); +b = zeros(S, M+1); +scale = zeros(1,T); + +alpha(:,1) = make_prior_from_CPTs(CPTs_slice1, node_sizes); +alpha(:,1) = alpha(:,1) .* obsmat(:, obs(1)); +if scaled + s = sum(alpha(:,1)); + if s==0, s = s + tiny; end + scale(1) = 1/s; +else + scale(1) = 1; +end +alpha(:,1) = alpha(:,1) * scale(1); + +for t=2:T + fprintf(1, 't %d\n', t); + a(:,1) = alpha(:,t-1); + for i=1:M + for j=1:S + u = ind2subv(node_sizes, j); + xnew = u(i); + s = 0; + for xold=1:node_sizes(i) + uold = u; + uold(i) = xold; + k = subv2ind(node_sizes, uold); + ps = find(inter(:,i)==1); + ps = ps(:)'; + l = subv2ind(node_sizes([ps i]), [uold(ps) xnew]); + s = s + a(k,i) * CPTs{i}(l); + end + a(j,i+1) = s; + end + end + alpha(:,t) = a(:,M+1) .* obsmat(:, obs(t)); + + if scaled + s = sum(alpha(:,t)); + if s==0, s = s + tiny; end + scale(t) = 1/s; + else + scale(t) = 1; + end + alpha(:,t) = alpha(:,t) * scale(t); + +end + + +beta(:,T) = ones(S,1) * scale(T); +for t=T-1:-1:1 + fprintf(1, 't %d\n', t); + b(:,1) = beta(:,t+1) .* obsmat(:, obs(t+1)); + for i=1:M + for j=1:S + u = ind2subv(node_sizes, j); + xold = u(i); + s = 0; + for xnew=1:node_sizes(i) + unew = u; + unew(i) = xnew; + k = subv2ind(node_sizes, unew); + ps = find(inter(:,i)==1); + ps = ps(:)'; + l = subv2ind(node_sizes([ps i]), [u(ps) xnew]); + s = s + b(k,i) * CPTs{i}(l); + end + b(j,i+1) = s; + end + end + beta(:,t) = b(:,M+1) * scale(t); +end + + +if scaled + loglik = -sum(log(scale)); % scale(i) is finite +else + lik = alpha(:,1)' * beta(:,1); + loglik = log(lik+tiny); +end diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/filter_test1.m b/sourcecodes/bnt-master/BNT/examples/dynamic/filter_test1.m new file mode 100644 index 00000000..9e65508a --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/filter_test1.m @@ -0,0 +1,24 @@ +% Compare online filtering algorithms on some DBNs + +seed = 0; +rand('state', seed); +randn('state', seed); + +if 0 + N = 3; + Q = 2; + obs_size = 1; + discrete_obs = 0; + bnet = mk_chmm(N, Q, obs_size, discrete_obs); +else + %bnet = mk_bat_dbn; + bnet = mk_water_dbn; +end + +T = 3; + +engine = {}; +engine{end+1} = filter_engine(hmm_2TBN_inf_engine(bnet)); +engine{end+1} = filter_engine(jtree_2TBN_inf_engine(bnet)); + +time = cmp_online_inference(bnet, engine, T); diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/ghmm1.m b/sourcecodes/bnt-master/BNT/examples/dynamic/ghmm1.m new file mode 100644 index 00000000..8590a25a --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/ghmm1.m @@ -0,0 +1,64 @@ +% Make an HMM with Gaussian observations +% X1 -> X2 +% | | +% v v +% Y1 Y2 + +intra = zeros(2); +intra(1,2) = 1; +inter = zeros(2); +inter(1,1) = 1; +n = 2; + +Q = 2; % num hidden states +O = 2; % size of observed vector +ns = [Q O]; +bnet = mk_dbn(intra, inter, ns, 'discrete', 1, 'observed', 2); + +prior0 = normalise(rand(Q,1)); +transmat0 = mk_stochastic(rand(Q,Q)); +mu0 = rand(O,Q); +Sigma0 = repmat(eye(O), [1 1 Q]); +bnet.CPD{1} = tabular_CPD(bnet, 1, prior0); +%% we set the cov prior to 0 to give same results as HMM toolbox +%bnet.CPD{2} = gaussian_CPD(bnet, 2, 'mean', mu0, 'cov', Sigma0, 'cov_prior_weight', 0); +bnet.CPD{2} = gaussian_CPD(bnet, 2, 'mean', mu0, 'cov', Sigma0); +bnet.CPD{3} = tabular_CPD(bnet, 3, transmat0); + + +T = 5; % fixed length sequences + +engine = {}; +engine{end+1} = smoother_engine(jtree_2TBN_inf_engine(bnet)); +engine{end+1} = smoother_engine(hmm_2TBN_inf_engine(bnet)); +engine{end+1} = hmm_inf_engine(bnet); +engine{end+1} = jtree_unrolled_dbn_inf_engine(bnet, T); +%engine{end+1} = frontier_inf_engine(bnet); +engine{end+1} = bk_inf_engine(bnet, 'clusters', {[1]}); +engine{end+1} = jtree_dbn_inf_engine(bnet); + + +inf_time = cmp_inference_dbn(bnet, engine, T); + +ncases = 2; +max_iter = 2; +[learning_time, CPD, LL, cases] = cmp_learning_dbn(bnet, engine, T, 'ncases', ncases, 'max_iter', max_iter); + +% Compare to HMM toolbox + +data = zeros(O, T, ncases); +for i=1:ncases + data(:,:,i) = cell2num(cases{i}(bnet.observed, :)); +end + +tic +[LL2, prior2, transmat2, mu2, Sigma2] = mhmm_em(data, prior0, transmat0, mu0, Sigma0, [], 'max_iter', max_iter); +t=toc; +disp(['HMM toolbox took ' num2str(t) ' seconds ']) + +e = 1; +assert(approxeq(prior2, CPD{e,1}.CPT)) +assert(approxeq(mu2, CPD{e,2}.mean)) +assert(approxeq(Sigma2, CPD{e,2}.cov)) +assert(approxeq(transmat2, CPD{e,3}.CPT)) +assert(approxeq(LL2, LL{e})) diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/ho1.m b/sourcecodes/bnt-master/BNT/examples/dynamic/ho1.m new file mode 100644 index 00000000..b6825ce1 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/ho1.m @@ -0,0 +1,156 @@ +function ho1() + +% Example of how to create a higher order DBN +% Written by Rainer Deventer <deventer@informatik.uni-erlangen.de> 3/28/03 + +bnet = createBNetNL(); + +%%%%%%%%%%%% + + +function bnet = createBNetNL(varargin) + % Generate a Bayesian network, which is able to model nonlinearities at +% the input. The only input is the order of the dynamic system. If this +% parameter is missing, the an order of two is assumed +if nargin > 0 + order = varargin{1} +else + order = 2; +end + +ss = 6; % For each time slice the following nodes are modeled + % ud(t_k) Discrete node, which decides whether saturation is reached. + % Node number 2 + % uv(t_k) Visible input node with node number 2 + % uh(t_k) Hidden input node with node number 3 + % y(t_k) Modeled output, Number 4 + % z(t_k) Disturbing variable, number 5 + % q(t_k), number6 6 + +intra = zeros(ss,ss); +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Within each timeslice ud(t_k) is connected with uv(t_k) and uh(t_k) % +% This part is used to model saturation % +% A connection from uv(t_k) to uh(t_k) is omitted % +% Additionally y(t_k) is connected with q(t_k). To model the disturbing% +% value z(t_k) is connected with q(t_k). % +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +intra(1,2:3) = 1; % Connections ud(t_k) -> uv(t_k) and ud(t_k) -> uh(t_k) +intra(4:5,6) = 1; % Connectios y(t_k) -> q(t_k) and z(t_k) -> q(t_k) + + + +inter = zeros(ss,ss,order); +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% The Markov assumption is not met as connections from time slice t to t+2 % +% exist. % +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +for i = 1:order + if i == 1 + inter(1,1,i) = 1; %Connect the discrete nodes. This is necessary to improve + %the disturbing reaction + inter(3,4,i) = 1; %Connect uh(t_{k-1}) with y(t_k) + inter(4,4,i) = 1; %Connect y(t_{k-1}) with y(t_k) + inter(5,5,i) = 1; %Connect z(t_{k-1}) with z(t_k) + else + inter(3,4,i) = 1; %Connect uh(t_{k-i}) with y(t_k) + inter(4,4,i) = 1; %Connect y(t_{k-i}) with y(t_k) + end +end + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Define the dimensions of the discrete nodes. Node 1 has two states % +% 1 = lower saturation reached % +% 2 = Upper saturation reached % +% Values in between are model by probabilities between 0 and 1 % +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +node_sizes = ones(1,ss); +node_sizes(1) = 2; +dnodes = [1]; + +eclass = [1:6;7 2:3 8 9 6;7 2:3 10 11 6]; +bnet = mk_higher_order_dbn(intra,inter,node_sizes,... + 'discrete',dnodes,... + 'eclass',eclass); + +cov_high = 400; +cov_low = 0.01; +weight1 = randn(1,1); +weight2 = randn(1,1); +weight3 = randn(1,1); +weight4 = randn(1,1); + +numOfNodes = 5 + order; +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Nodes of the first time-slice % +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Discrete input node, +bnet.CPD{1} = tabular_CPD(bnet,1,'CPT',[1/2 1/2],'adjustable',0); + + +% Modeled visible input +bnet.CPD{2} = gaussian_CPD(bnet,2,'mean',[0 10],'clamp_mean',1,... + 'cov',[10 10],'clamp_cov',1); + +% Modeled hidden input +bnet.CPD{3} = gaussian_CPD(bnet,3,'mean',[0, 10],'clamp_mean',1,... + 'cov',[0.1 0.1],'clamp_cov',1); + +% Modeled output in the first timeslice, thus there are no parents +% Usuallz the output nodes get a low covariance. But in the first +% time-slice a prediction of the output is not possible due to +% missing information +bnet.CPD{4} = gaussian_CPD(bnet,4,'mean',0,'clamp_mean',1,... + 'cov',cov_high,'clamp_cov',1); + +%Disturbance +bnet.CPD{5} = gaussian_CPD(bnet,5,'mean',0,... + 'cov',[4],... + 'clamp_mean',1,... + 'clamp_cov',1); + +%Observed output. +bnet.CPD{6} = gaussian_CPD(bnet,6,'mean',0,... + 'clamp_mean',1,... + 'cov',cov_low,'clamp_cov',1,... + 'weights',[1 1],'clamp_weights',1); + +% Discrete node at second time slice +bnet.CPD{7} = tabular_CPD(bnet,7,'CPT',[0.6 0.4 0.4 0.6],'adjustable',0); +%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Node for the model output % +%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +bnet.CPD{8} = gaussian_CPD(bnet,10,'mean',0,... + 'cov',cov_high,... + 'clamp_mean',1,... + 'clamp_cov',1); +% 'weights',[0.0791 0.9578]); + + +%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Node for the disturbance % +%%%%%%%%%%%%%%%%%%%%%%%%%%%% +bnet.CPD{9} = gaussian_CPD(bnet,11,'mean',0,'clamp_mean',1,... + 'cov',[4],'clamp_cov',1,... + 'weights',[1],'clamp_weights',1); + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Node for the model output % +%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +bnet.CPD{10} = gaussian_CPD(bnet,16,'mean',0,'clamp_mean',1,... + 'cov',cov_low,'clamp_cov',1); +% 'weights',[0.0188 -0.0067 0.0791 0.9578]); + + + + +%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Node for the disturbance % +%%%%%%%%%%%%%%%%%%%%%%%%%%%% +bnet.CPD{11} = gaussian_CPD(bnet,17,'mean',0,'clamp_mean',1,... + 'cov',[0.2],'clamp_cov',1,... + 'weights',[1],'clamp_weights',1); + + + + diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/jtree_clq_test.m b/sourcecodes/bnt-master/BNT/examples/dynamic/jtree_clq_test.m new file mode 100644 index 00000000..647a2763 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/jtree_clq_test.m @@ -0,0 +1,150 @@ +% Construct various DBNs and examine their clique structure. +% This was used to generate various figures in chap 3-4 of my thesis. + +% Examine the cliques in the unrolled mildew net + +%dbn = mk_mildew_dbn; +dbn = mk_chmm(4); +ss = dbn.nnodes_per_slice; +T = 7; +N = ss*T; +bnet = dbn_to_bnet(dbn, T); + +constrained = 0; +if constrained + stages = num2cell(unroll_set(1:ss, ss, T), 1); +else + stages = { 1:N; }; +end +clusters = {}; +%[jtree, root, cliques, B, w, elim_order, moral_edges, fill_in_edges] = ... +% dag_to_jtree(bnet, bnet.observed, stages, clusters); +[jtree, root, cliques] = graph_to_jtree(moralize(bnet.dag), ones(1,N), stages, clusters); + +flip=1; +clf;[dummyx, dummyy, h] = draw_dbn(dbn.intra, dbn.inter, flip, T, -1); +dir = '/home/eecs/murphyk/WP/Thesis/Figures/Inf/MildewUnrolled'; +mk_ps_from_clqs(dbn, T, cliques, []) +%mk_collage_from_clqs(dir, cliques) + + +% Examine the cliques in the cascade DBN + +% A-A +% \ +% B B +% \ +% C C +% \ +% D D +ss = 4; +intra = zeros(ss); +inter = zeros(ss); +inter(1, [1 2])=1; +for i=2:ss-1 + inter(i,i+1)=1; +end + + +% 2 coupled HMMs 1,3 and 2,4 +ss = 4; +intra = zeros(ss); +inter = zeros(ss); % no persistent edges +%inter = diag(ones(ss,1)); % persitence edges +inter(1,3)=1; inter(3,1)=1; +inter(2,4)=1; inter(4,2)=1; + +%bnet = mk_fhmm(3); +bnet = mk_chmm(4); +intra = bnet.intra; +inter = bnet.inter; + +clqs = compute_minimal_interface(intra, inter); +celldisp(clqs) + + + + +% A A +% \ +% B B +% \ +% C C +% \ +% D-D +ss = 4; +intra = zeros(ss); +inter = zeros(ss); +for i=1:ss-1 + inter(i,i+1)=1; +end +inter(4,4)=1; + + + +ns = 2*ones(1,ss); +dbn = mk_dbn(intra, inter, ns); +for i=2*ss + dbn.CPD{i} = tabular_CPD(bnet, i); +end + +T = 4; +N = ss*T; +bnet = dbn_to_bnet(dbn, T); + +constrained = 1; +if constrained + % elim first 3 slices first in any order + stages = {1:12, 13:16}; + %stages = num2cell(unroll_set(1:ss, ss, T), 1); +else + stages = { 1:N; }; +end +clusters = {}; +%[jtree, root, cliques, B, w, elim_order, moral_edges, fill_in_edges] = ... +% dag_to_jtree(bnet, bnet.observed, stages, clusters); +[jtree, root, cliques] = graph_to_jtree(moralize(bnet.dag), ones(1,N), stages, clusters); + + + + + +% Examine the cliques in the 1.5 slice DBN + +%dbn = mk_mildew_dbn; +dbn = mk_water_dbn; +%dbn = mk_bat_dbn; +ss = dbn.nnodes_per_slice; +int = compute_fwd_interface(dbn); +bnet15 = mk_slice_and_half_dbn(dbn, int); +N = length(bnet15.dag); +stages = {1:N}; + +% bat +%cl1 = [16 17 19 7 14]; +%cl2 = [27 25 21 23 20]; +%clusters = {cl1, cl2, cl1+ss, cl2+ss}; + +% water +%cl1 = 1:2; cl2 = 3:6; cl3 = 7:8; +%clusters = {cl1, cl2, cl3, cl1+ss, cl2+ss, cl3+ss}; + +%clusters = {}; +clusters = {int, int+ss}; +%[jtree, root, cliques, B, w, elim_order, moral_edges, fill_in_edges] = ... +% dag_to_jtree(bnet15, bnet.observed, stages, clusters); +[jtree, root, cliques] = graph_to_jtree(moralize(bnet15.dag), ones(1,N), stages, clusters); + +clq_len = []; +for c=1:length(cliques) + clq_len(c) = length(cliques{c}); +end +hist(clq_len, 1:max(clq_len)); +h=hist(clq_len, 1:max(clq_len)); +axis([1 max(clq_len)+1 0 max(h)+1]) +xlabel('clique size','fontsize',16) +ylabel('number','fontsize',16) + + + + diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/jtree_clq_test2.m b/sourcecodes/bnt-master/BNT/examples/dynamic/jtree_clq_test2.m new file mode 100644 index 00000000..975cc46b --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/jtree_clq_test2.m @@ -0,0 +1,23 @@ + +%bnet = mk_uffe_dbn; +bnet = mk_mildew_dbn; +ss = length(bnet.intra); + +% construct jtree from 1.5 slice DBN + +int = compute_fwd_interface(bnet.intra, bnet.inter); +bnet15 = mk_slice_and_half_dbn(bnet, int); + +% use unconstrained elimination, +% but force there to be a clique containing both interfaces +clusters = {int, int+ss}; +jtree_engine = jtree_inf_engine(bnet15, 'clusters', clusters, 'root', int+ss); +S=struct(jtree_engine) +in_clq = clq_containing_nodes(jtree_engine, int); +out_clq = clq_containing_nodes(jtree_engine, int+ss) + + +% Also make a jtree from slice 1 +bnet1 = mk_bnet(bnet.intra1, bnet.node_sizes_slice); +jtree_engine1 = jtree_inf_engine(bnet1, 'clusters', {int}, 'root', int); +S1=struct(jtree_engine1) diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/kalman1.m b/sourcecodes/bnt-master/BNT/examples/dynamic/kalman1.m new file mode 100644 index 00000000..32c3583c --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/kalman1.m @@ -0,0 +1,66 @@ +% Make a linear dynamical system +% X1 -> X2 +% | | +% v v +% Y1 Y2 + +intra = zeros(2); +intra(1,2) = 1; +inter = zeros(2); +inter(1,1) = 1; +n = 2; + +X = 2; % size of hidden state +Y = 2; % size of observable state +ns = [X Y]; +bnet = mk_dbn(intra, inter, ns, 'discrete', [], 'observed', 2); + +x0 = rand(X,1); +V0 = eye(X); +C0 = rand(Y,X); +R0 = eye(Y); +A0 = rand(X,X); +Q0 = eye(X); + +bnet.CPD{1} = gaussian_CPD(bnet, 1, 'mean', x0, 'cov', V0, 'cov_prior_weight', 0); +bnet.CPD{2} = gaussian_CPD(bnet, 2, 'mean', zeros(Y,1), 'cov', R0, 'weights', C0, ... + 'clamp_mean', 1, 'cov_prior_weight', 0); +bnet.CPD{3} = gaussian_CPD(bnet, 3, 'mean', zeros(X,1), 'cov', Q0, 'weights', A0, ... + 'clamp_mean', 1, 'cov_prior_weight', 0); + + +T = 5; % fixed length sequences + +clear engine; +engine{1} = kalman_inf_engine(bnet); +engine{2} = jtree_unrolled_dbn_inf_engine(bnet, T); +engine{3} = jtree_dbn_inf_engine(bnet); +engine{end+1} = smoother_engine(jtree_2TBN_inf_engine(bnet)); +N = length(engine); + + +inf_time = cmp_inference_dbn(bnet, engine, T); + +ncases = 2; +max_iter = 2; +[learning_time, CPD, LL, cases] = cmp_learning_dbn(bnet, engine, T, 'ncases', ncases, 'max_iter', max_iter); + + +% Compare to KF toolbox + +data = zeros(Y, T, ncases); +for i=1:ncases + data(:,:,i) = cell2num(cases{i}(onodes, :)); +end +[A2, C2, Q2, R2, x2, V2, LL2trace] = learn_kalman(data, A0, C0, Q0, R0, x0, V0, max_iter); + + +e = 1; +assert(approxeq(x2, CPD{e,1}.mean)) +assert(approxeq(V2, CPD{e,1}.cov)) +assert(approxeq(C2, CPD{e,2}.weights)) +assert(approxeq(R2, CPD{e,2}.cov)); +assert(approxeq(A2, CPD{e,3}.weights)) +assert(approxeq(Q2, CPD{e,3}.cov)); +assert(approxeq(LL2trace, LL{1})) + diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/kjaerulff1.m b/sourcecodes/bnt-master/BNT/examples/dynamic/kjaerulff1.m new file mode 100644 index 00000000..28b315fc --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/kjaerulff1.m @@ -0,0 +1,55 @@ +% Compare the speeds of various inference engines on the DBN in Kjaerulff +% "dHugin: A computational system for dynamic time-sliced {B}ayesian networks", +% Intl. J. Forecasting 11:89-111, 1995. +% +% The intra structure is (all arcs point downwards) +% +% 1 -> 2 +% \ / +% 3 +% | +% 4 +% / \ +% 5 6 +% \ / +% 7 +% | +% 8 +% +% The inter structure is 1->1, 4->4, 8->8 + +seed = 0; +rand('state', seed); +randn('state', seed); + +ss = 8; +intra = zeros(ss); +intra(1,[2 3])=1; +intra(2,3)=1; +intra(3,4)=1; +intra(4,[5 6])=1; +intra([5 6], 7)=1; +intra(7,8)=1; + +inter = zeros(ss); +inter(1,1)=1; +inter(4,4)=1; +inter(8,8)=1; + +ns = 2*ones(1,ss); +onodes = 2; +bnet = mk_dbn(intra, inter, ns, 'observed', onodes, 'eclass2', (1:ss)+ss); +for i=1:2*ss + bnet.CPD{i} = tabular_CPD(bnet, i); +end + +T = 4; + +engine = {}; +engine{end+1} = jtree_unrolled_dbn_inf_engine(bnet, T); +engine{end+1} = jtree_dbn_inf_engine(bnet); +engine{end+1} = smoother_engine(jtree_2TBN_inf_engine(bnet)); +%engine{end+1} = smoother_engine(hmm_2TBN_inf_engine(bnet)); % observed nodes have children + +inf_time = cmp_inference_dbn(bnet, engine, T) +learning_time = cmp_learning_dbn(bnet, engine, T) diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/loopy_dbn1.m b/sourcecodes/bnt-master/BNT/examples/dynamic/loopy_dbn1.m new file mode 100644 index 00000000..27facf0b --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/loopy_dbn1.m @@ -0,0 +1,24 @@ +N = 1; % single chain = HMM - should give exact answers +Q = 2; +rand('state', 0); +randn('state', 0); +discrete = 1; +if discrete + Y = 2; % size of output alphabet +else + Y = 1; +end +coupled = 1; +bnet = mk_chmm(N, Q, Y, discrete, coupled); +ss = N*2; + +T = 3; + +engine = {}; +engine{end+1} = jtree_dbn_inf_engine(bnet); +engine{end+1} = jtree_unrolled_dbn_inf_engine(bnet, T); +engine{end+1} = pearl_unrolled_dbn_inf_engine(bnet, 'protocol', 'tree'); + +inf_time = cmp_inference_dbn(bnet, engine, T) +learning_time = cmp_learning_dbn(bnet, engine, T) + diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/mhmm1.m b/sourcecodes/bnt-master/BNT/examples/dynamic/mhmm1.m new file mode 100644 index 00000000..6efb9f72 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/mhmm1.m @@ -0,0 +1,85 @@ +% Make an HMM with mixture of Gaussian observations +% Q1 ---> Q2 +% / | / | +% M1 | M2 | +% \ v \ v +% Y1 Y2 +% where Pr(m=j|q=i) is a multinomial and Pr(y|m,q) is a Gaussian + +%seed = 3; +%rand('state', seed); +%randn('state', seed); + +intra = zeros(3); +intra(1,[2 3]) = 1; +intra(2,3) = 1; +inter = zeros(3); +inter(1,1) = 1; +n = 3; + +Q = 2; % num hidden states +O = 2; % size of observed vector +M = 2; % num mixture components per state + +ns = [Q M O]; +dnodes = [1 2]; +onodes = [3]; +eclass1 = [1 2 3]; +eclass2 = [4 2 3]; +bnet = mk_dbn(intra, inter, ns, 'discrete', dnodes, 'eclass1', eclass1, 'eclass2', eclass2, ... + 'observed', onodes); + +prior0 = normalise(rand(Q,1)); +transmat0 = mk_stochastic(rand(Q,Q)); +mixmat0 = mk_stochastic(rand(Q,M)); +mu0 = rand(O,Q,M); +Sigma0 = repmat(eye(O), [1 1 Q M]); +bnet.CPD{1} = tabular_CPD(bnet, 1, prior0); +bnet.CPD{2} = tabular_CPD(bnet, 2, mixmat0); +%% we set the cov prior to 0 to give same results as HMM toolbox +%bnet.CPD{3} = gaussian_CPD(bnet, 3, 'mean', mu0, 'cov', Sigma0, 'cov_prior_weight', 0); +% new version of HMM toolbox uses the same default prior on Gaussians as BNT +bnet.CPD{3} = gaussian_CPD(bnet, 3, 'mean', mu0, 'cov', Sigma0); +bnet.CPD{4} = tabular_CPD(bnet, 4, transmat0); + + + +T = 5; % fixed length sequences + +engine = {}; +engine{end+1} = hmm_inf_engine(bnet); +engine{end+1} = smoother_engine(jtree_2TBN_inf_engine(bnet)); +engine{end+1} = smoother_engine(hmm_2TBN_inf_engine(bnet)); +if 0 +engine{end+1} = jtree_unrolled_dbn_inf_engine(bnet, T); +%engine{end+1} = frontier_inf_engine(bnet); +engine{end+1} = bk_inf_engine(bnet, 'clusters', 'exact'); +engine{end+1} = jtree_dbn_inf_engine(bnet); +end + +inf_time = cmp_inference_dbn(bnet, engine, T); + +ncases = 2; +max_iter = 2; +[learning_time, CPD, LL, cases] = cmp_learning_dbn(bnet, engine, T, 'ncases', ncases, 'max_iter', max_iter); + +% Compare to HMM toolbox + +data = zeros(O, T, ncases); +for i=1:ncases + data(:,:,i) = reshape(cell2num(cases{i}(onodes,:)), [O T]); +end +tic; +[LL2, prior2, transmat2, mu2, Sigma2, mixmat2] = ... + mhmm_em(data, prior0, transmat0, mu0, Sigma0, mixmat0, 'max_iter', max_iter); +t=toc; +disp(['HMM toolbox took ' num2str(t) ' seconds ']) + +for e = 1:length(engine) + assert(approxeq(prior2, CPD{e,1}.CPT)) + assert(approxeq(mixmat2, CPD{e,2}.CPT)) + assert(approxeq(mu2, CPD{e,3}.mean)) + assert(approxeq(Sigma2, CPD{e,3}.cov)) + assert(approxeq(transmat2, CPD{e,4}.CPT)) + assert(approxeq(LL2, LL{e})) +end diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/mildew1.m b/sourcecodes/bnt-master/BNT/examples/dynamic/mildew1.m new file mode 100644 index 00000000..f38aee91 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/mildew1.m @@ -0,0 +1,30 @@ +bnet = mk_mildew_dbn; + +T = 4; +engine = {}; +engine{end+1} = jtree_unrolled_dbn_inf_engine(bnet, T); +engine{end+1} = jtree_dbn_inf_engine(bnet); +%engine{end+1} = hmm_inf_engine(bnet); % 8 is observed but has kids +engine{end+1} = smoother_engine(jtree_2TBN_inf_engine(bnet)); +%engine{end+1} = smoother_engine(hmm_2TBN_inf_engine(bnet)); + +inf_time = cmp_inference_dbn(bnet, engine, T, 'check_ll', 0) +%learning_time = cmp_learning_dbn(bnet, engine, T) + +S = struct(engine{1}); +S1 = struct(S.unrolled_engine); +G = S1.jtree; +%graph_to_dot(G, 'directed', 0, 'leftright', 1, ... +% 'filename', '/home/eecs/murphyk/WP/Thesis/Figures/Inf/Mildew/jtree.dot') +%!dot -Tps jtree.dot -o jtree.ps +% The resulting ps file cannot be converted using ps2pdf. + +N = length(G); +for i=1:N + for j=1:N + if G(i,j) + G(j,i)=1; + end + end +end +draw_graph(G) diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/mk_bat_dbn.m b/sourcecodes/bnt-master/BNT/examples/dynamic/mk_bat_dbn.m new file mode 100644 index 00000000..e4d6f8b9 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/mk_bat_dbn.m @@ -0,0 +1,63 @@ +function [bnet, names] = mk_bat_dbn() +% MK_BAT_DBN Make the BAT DBN +% [bnet, names] = mk_bat_dbn() +% See +% - Forbes, Huang, Kanazawa and Russell, "The BATmobile: Towards a Bayesian Automated Taxi", IJCAI 95 +% - Boyen and Koller, "Tractable Inference for Complex Stochastic Processes", UAI98. + +names = {'LeftClr', 'RightClr', 'LatAct', 'Xdot', 'InLane', 'FwdAct', ... + 'Ydot', 'Stopped', 'EngStatus', 'FBStatus', ... + 'LeftClrSens', 'RightClrSens', 'TurnSignalSens', 'XdotSens', 'YdotSens', ... + 'FYdotDiffSens', 'FclrSens', 'BXdotSens', 'BclrSens', 'BYdotDiffSens', ... + 'SensorValid', 'FYdotDiff', 'FcloseSlow', 'Fclr', 'BXdot', 'BcloseFast', 'Bclr', 'BYdotDiff'}; +ss = length(names); + +intrac = {... + 'LeftClr', 'LeftClrSens'; + 'RightClr', 'RightClrSens'; + 'LatAct', 'TurnSignalSens'; 'LatAct', 'Xdot'; + 'Xdot', 'XdotSens'; + 'FwdAct', 'Ydot'; + 'Ydot', 'YdotSens'; 'Ydot', 'Stopped'; + 'EngStatus', 'Ydot'; 'EngStatus', 'FYdotDiff'; 'EngStatus', 'Fclr'; 'EngStatus', 'BXdot'; + 'SensorValid', 'XdotSens'; 'SensorValid', 'YdotSens'; + 'FYdotDiff', 'FYdotDiffSens'; 'FYdotDiff', 'FcloseSlow'; + 'FcloseSlow', 'FBStatus'; + 'Fclr', 'FclrSens'; 'Fclr', 'FcloseSlow'; + 'BXdot', 'BXdotSens'; + 'Bclr', 'BclrSens'; 'Bclr', 'BXdot'; 'Bclr', 'BcloseFast'; + 'BcloseFast', 'FBStatus'; + 'BYdotDiff', 'BYdotDiffSens'; 'BYdotDiff', 'BcloseFast'}; +[intra, names] = mk_adj_mat(intrac, names, 1); + + +interc = {... + 'LeftClr', 'LeftClr'; 'LeftClr', 'LatAct'; + 'RightClr', 'RightClr'; 'RightClr', 'LatAct'; + 'LatAct', 'LatAct'; 'LatAct', 'FwdAct'; + 'Xdot', 'Xdot'; 'Xdot', 'InLane'; + 'InLane', 'InLane'; 'InLane', 'LatAct'; + 'FwdAct', 'FwdAct'; + 'Ydot', 'Ydot'; + 'Stopped', 'Stopped'; + 'EngStatus', 'EngStatus'; + 'FBStatus', 'FwdAct'; 'FBStatus', 'LatAct'}; +inter = mk_adj_mat(interc, names, 0); + +obs = {'LeftClrSens', 'RightClrSens', 'TurnSignalSens', 'XdotSens', 'YdotSens', 'FYdotDiffSens', ... + 'FclrSens', 'BXdotSens', 'BclrSens', 'BYdotDiffSens'}; + +for i=1:length(obs) + onodes(i) = strmatch(obs{i}, names); %stringmatch(obs{i}, names); +end +onodes = sort(onodes); + +dnodes = 1:ss; +ns = 2*ones(1,ss); % binary nodes +bnet = mk_dbn(intra, inter, ns, 'discrete', dnodes, 'observed', onodes, 'eclass2', (1:ss)+ss); + +% make rnd params +for i=1:2*ss + bnet.CPD{i} = tabular_CPD(bnet, i); +end + diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/mk_chmm.m b/sourcecodes/bnt-master/BNT/examples/dynamic/mk_chmm.m new file mode 100644 index 00000000..8da245ef --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/mk_chmm.m @@ -0,0 +1,103 @@ +function bnet = mk_chmm(N, Q, Y, discrete_obs, coupled, CPD) +% MK_CHMM Make a coupled Hidden Markov Model +% +% There are N hidden nodes, each connected to itself and its two nearest neighbors in the next +% slice (apart from the edges, where there is 1 nearest neighbor). +% +% Example: If N = 3, the hidden backbone is as follows, where all arrows point to the righ+t +% +% X1--X2 +% \/ +% /\ +% X2--X2 +% \/ +% /\ +% X3--X3 +% +% Each hidden node has a "private" observed child (not shown). +% +% BNET = MK_CHMM(N, Q, Y) +% Each hidden node is discrete and has Q values. +% Each observed node is a Gaussian vector of length Y. +% +% BNET = MK_CHMM(N, Q, Y, DISCRETE_OBS) +% If discrete_obs = 1, the observations are discrete (values in {1, .., Y}). +% +% BNET = MK_CHMM(N, Q, Y, DISCRETE_OBS, COUPLED) +% If coupled = 0, the chains are not coupled, i.e., we make N parallel HMMs. +% +% BNET = MK_CHMM(N, Q, Y, DISCRETE_OBS, COUPLED, CPDs) +% means use the specified CPD structures instead of creating random params. +% CPD{i}.CPT, i=1:N specifies the prior +% CPD{i}.CPT, i=2N+1:3N specifies the transition model +% CPD{i}.mean, CPD{i}.cov, i=N+1:2N specifies the observation model if Gaussian +% CPD{i}.CPT, i=N+1:2N if discrete + + +if nargin < 2, Q = 2; end +if nargin < 3, Y = 1; end +if nargin < 4, discrete_obs = 0; end +if nargin < 5, coupled = 1; end +if nargin < 6, rnd = 1; else rnd = 0; end + +ss = N*2; +hnodes = 1:N; +onodes = (1:N)+N; + +intra = zeros(ss); +for i=1:N + intra(hnodes(i), onodes(i))=1; +end + +inter = zeros(ss); +if coupled + for i=1:N + inter(i, max(i-1,1):min(i+1,N))=1; + end +else + inter(1:N, 1:N) = eye(N); +end + +ns = [Q*ones(1,N) Y*ones(1,N)]; + +eclass1 = [hnodes onodes]; +eclass2 = [hnodes+ss onodes]; +if discrete_obs + dnodes = 1:ss; +else + dnodes = hnodes; +end +bnet = mk_dbn(intra, inter, ns, 'discrete', dnodes, 'eclass1', eclass1, 'eclass2', eclass2, ... + 'observed', onodes); + +if rnd + for i=hnodes(:)' + bnet.CPD{i} = tabular_CPD(bnet, i); + end + for i=onodes(:)' + if discrete_obs + bnet.CPD{i} = tabular_CPD(bnet, i); + else + bnet.CPD{i} = gaussian_CPD(bnet, i); + end + end + for i=hnodes(:)'+ss + bnet.CPD{i} = tabular_CPD(bnet, i); + end +else + for i=hnodes(:)' + bnet.CPD{i} = tabular_CPD(bnet, i, CPD{i}.CPT); + end + for i=onodes(:)' + if discrete_obs + bnet.CPD{i} = tabular_CPD(bnet, i, CPD{i}.CPT); + else + bnet.CPD{i} = gaussian_CPD(bnet, i, CPD{i}.mean, CPD{i}.cov); + end + end + for i=hnodes(:)'+ss + bnet.CPD{i} = tabular_CPD(bnet, i, CPD{i}.CPT); + end +end + + diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/mk_collage_from_clqs.m b/sourcecodes/bnt-master/BNT/examples/dynamic/mk_collage_from_clqs.m new file mode 100644 index 00000000..90159f7e --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/mk_collage_from_clqs.m @@ -0,0 +1,44 @@ +function mk_collage_from_clqs(dir, cliques) + +% For use with mk_ps_from_clqs. +% This generates a latex file that glues all the .ps files +% into one big figure. + +cd(dir) +C = length(cliques); + +ncols = 4; +width = 1.5; +fid = fopen('collage.tex', 'w'); +fprintf(fid, '\\documentclass{article}\n'); +fprintf(fid, '\\usepackage{psfig}\n'); +fprintf(fid, '\\begin{document}\n'); +fprintf(fid, '\\centerline{\n'); +fprintf(fid, '\\begin{tabular}{'); +for col=1:ncols, fprintf(fid, 'c'); end +fprintf(fid, '}\n'); +c = 1; +for row = 1:floor(C/ncols) + for col=1:ncols-1 + fname = sprintf('%s/clq%d.ps', dir, c); + fprintf(fid, '\\psfig{file=%s,width=%3fin} & \n', fname, width); + c = c + 1; + end + fname = sprintf('%s/clq%d.ps', dir, c); + fprintf(fid, '\\psfig{file=%s,width=%3fin} \\\\ \n', fname, width); + c = c + 1; +end +% last row +while (c <= C) + fname = sprintf('%s/clq%d.ps', dir, c); + fprintf(fid, '\\psfig{file=%s,width=%3fin} & \n', fname, width); + c = c + 1; +end +fprintf(fid, '\\end{tabular}\n'); +fprintf(fid, '}\n'); +fprintf(fid, '\\end{document}'); +fclose(fid); + +!latex collage.tex & +!dvips -o collage.ps collage.dvi & +!ghostview collage.ps & diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/mk_fhmm.m b/sourcecodes/bnt-master/BNT/examples/dynamic/mk_fhmm.m new file mode 100644 index 00000000..ffbe05a5 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/mk_fhmm.m @@ -0,0 +1,58 @@ +function bnet = mk_fhmm(N, Q, Y, discrete_obs) +% MK_FHMM Make a factorial Hidden Markov Model +% +% There are N independent parallel hidden chains, each connected to the output +% +% e.g., N = 2 (vertical/diagonal edges point down) +% +% A1--->A2 +% | B1--|->B2 +% | / |/ +% Y1 Y2 +% +% [bnet, onode] = mk_chmm(n, q, y, discrete_obs) +% +% Each hidden node is discrete and has Q values. +% If discrete_obs = 1, each observed node is discrete and has values 1..Y. +% If discrete_obs = 0, each observed node is a Gaussian vector of length Y. + +if nargin < 2, Q = 2; end +if nargin < 3, Y = 2; end +if nargin < 4, discrete_obs = 1; end + +ss = N+1; +hnodes = 1:N; +onode = N+1; + +intra = zeros(ss); +intra(hnodes, onode) = 1; + +inter = eye(ss); +inter(onode,onode) = 0; + +ns = [Q*ones(1,N) Y]; + +eclass1 = [hnodes onode]; +eclass2 = [hnodes+ss onode]; +if discrete_obs + dnodes = 1:ss; +else + dnodes = hnodes; +end +bnet = mk_dbn(intra, inter, ns, 'discrete', dnodes, 'eclass1', eclass1, 'eclass2', eclass2, ... + 'observed', onode); + +for i=hnodes(:)' + bnet.CPD{i} = tabular_CPD(bnet, i); +end +i = onode; +if discrete_obs + bnet.CPD{i} = tabular_CPD(bnet, i); +else + bnet.CPD{i} = gaussian_CPD(bnet, i); +end +for i=hnodes(:)'+ss + bnet.CPD{i} = tabular_CPD(bnet, i); +end + + diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/mk_mildew_dbn.m b/sourcecodes/bnt-master/BNT/examples/dynamic/mk_mildew_dbn.m new file mode 100644 index 00000000..71f393e3 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/mk_mildew_dbn.m @@ -0,0 +1,31 @@ +function bnet = mk_mildew_dbn() + +% DBN for foreacasting the gross yield of wheat based on climatic data, +% observations of leaf area index (LAI) and extension of mildew, +% and knowledge of amount of fungicides used and time of usage. +% From Kjaerulff '95. + +Fungi=1; Mildew=2; LAI=3; Precip=4; Temp=5; Micro=6; Solar=7; Photo=8; Dry=9; +n = 9; +intra = zeros(n,n); +intra(Mildew, LAI)=1; +intra(LAI,[Micro Photo])=1; +intra(Precip,Micro)=1; +intra(Temp,[Micro Photo])=1; +intra(Solar,Photo)=1; +intra(Photo,Dry)=1; + +inter = zeros(n,n); +inter(Fungi,Mildew)=1; +inter(Mildew,Mildew)=1; +inter(LAI,LAI)=1; +inter(Micro,Mildew)=1; +inter(Dry,Dry)=1; + +ns = 2*ones(1,n); +bnet = mk_dbn(intra, inter, ns, 'observed', [Photo]); + +for e=1:max(bnet.equiv_class(:)) + i = bnet.rep_of_eclass(e); + bnet.CPD{e} = tabular_CPD(bnet,i); +end diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/mk_orig_bat_dbn.m b/sourcecodes/bnt-master/BNT/examples/dynamic/mk_orig_bat_dbn.m new file mode 100644 index 00000000..0065ad5e --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/mk_orig_bat_dbn.m @@ -0,0 +1,198 @@ +function [bnet, names] = mk_orig_bat_dbn() +% MK_BAT_DBN Make the BAT DBN +% [bnet, names] = mk_bat_dbn() +% See +% - Forbes, Huang, Kanazawa and Russell, "The BATmobile: Towards a Bayesian Automated Taxi", IJCAI 95 +% - Boyen and Koller, "Tractable Inference for Complex Stochastic Processes", UAI98. + +names = {'LeftClr', 'RightClr', 'LatAct', 'Xdot', 'InLane', 'FwdAct', ... + 'Ydot', 'Stopped', 'EngStatus', 'FBStatus', ... + 'LeftClrSens', 'RightClrSens', 'TurnSignalSens', 'XdotSens', 'YdotSens', ... + 'FYdotDiffSens', 'FclrSens', 'BXdotSens', 'BclrSens', 'BYdotDiffSens', ... + 'SensorValid', 'FYdotDiff', 'FcloseSlow', 'Fclr', 'BXdot', 'BcloseFast', 'Bclr', 'BYdotDiff'}; +ss = length(names); + +intrac = {... + 'LeftClr', 'LeftClrSens'; + 'RightClr', 'RightClrSens'; + 'LatAct', 'TurnSignalSens'; 'LatAct', 'Xdot'; + 'Xdot', 'XdotSens'; + 'FwdAct', 'Ydot'; + 'Ydot', 'YdotSens'; 'Ydot', 'Stopped'; + 'EngStatus', 'Ydot'; 'EngStatus', 'FYdotDiff'; 'EngStatus', 'Fclr'; 'EngStatus', 'BXdot'; + 'SensorValid', 'XdotSens'; 'SensorValid', 'YdotSens'; + 'FYdotDiff', 'FYdotDiffSens'; 'FYdotDiff', 'FcloseSlow'; + 'FcloseSlow', 'FBStatus'; + 'Fclr', 'FclrSens'; 'Fclr', 'FcloseSlow'; + 'BXdot', 'BXdotSens'; + 'Bclr', 'BclrSens'; 'Bclr', 'BXdot'; 'Bclr', 'BcloseFast'; + 'BcloseFast', 'FBStatus'; + 'BYdotDiff', 'BYdotDiffSens'; 'BYdotDiff', 'BcloseFast'}; +[intra, names] = mk_adj_mat(intrac, names, 1); + + +interc = {... + 'LeftClr', 'LeftClr'; 'LeftClr', 'LatAct'; + 'RightClr', 'RightClr'; 'RightClr', 'LatAct'; + 'LatAct', 'LatAct'; 'LatAct', 'FwdAct'; + 'Xdot', 'Xdot'; 'Xdot', 'InLane'; + 'InLane', 'InLane'; 'InLane', 'LatAct'; + 'FwdAct', 'FwdAct'; + 'Ydot', 'Ydot'; + 'Stopped', 'Stopped'; + 'EngStatus', 'EngStatus'; + 'FBStatus', 'FwdAct'; 'FBStatus', 'LatAct'}; +inter = mk_adj_mat(interc, names, 0); + +obs = {'LeftClrSens', 'RightClrSens', 'TurnSignalSens', 'XdotSens', 'YdotSens', 'FYdotDiffSens', ... + 'FclrSens', 'BXdotSens', 'BclrSens', 'BYdotDiffSens'}; + +for i=1:length(obs) + onodes(i) = stringmatch(obs{i}, names); +end +onodes = sort(onodes); + +dnodes = 1:ss; +ns = zeros(1,ss); + +ns(stringmatch('LeftClr', names)) = 2; +ns(stringmatch('RightClr', names)) = 2; +ns(stringmatch('LatAct', names)) = 3; +ns(stringmatch('Xdot', names)) = 7; +ns(stringmatch('InLane', names)) = 2; +ns(stringmatch('FwdAct', names)) = 3; +ns(stringmatch('Ydot', names)) = 11; +ns(stringmatch('Stopped', names)) = 2; +ns(stringmatch('EngStatus', names)) = 2; +ns(stringmatch('FBStatus', names)) = 3; +ns(stringmatch('LeftClrSens', names)) = 2; +ns(stringmatch('RightClrSens', names)) = 2; +ns(stringmatch('TurnSignalSens', names)) = 3; +ns(stringmatch('XdotSens', names)) = 7; +ns(stringmatch('YdotSens', names)) = 11; +ns(stringmatch('FYdotDiffSens', names)) = 8; +ns(stringmatch('FclrSens', names)) = 20; +ns(stringmatch('BXdotSens', names)) = 8; +ns(stringmatch('BclrSens', names)) = 20; +ns(stringmatch('BYdotDiffSens', names)) = 8; +ns(stringmatch('SensorValid', names)) = 2; +ns(stringmatch('FYdotDiff', names)) = 4; +ns(stringmatch('FcloseSlow', names)) = 2; +ns(stringmatch('Fclr', names)) = 3; +ns(stringmatch('BXdot', names)) = 8; +ns(stringmatch('BcloseFast', names)) = 2; +ns(stringmatch('Bclr', names)) = 3; +ns(stringmatch('BYdotDiff', names)) = 4; + +%ns = 2*ones(1,ss); + + +bnet = mk_dbn(intra, inter, ns, 'discrete', dnodes, 'observed', onodes, 'eclass2', (1:ss)+ss); + +% make unif params +for i=1:2*ss + bnet.CPD{i} = tabular_CPD(bnet, i, 'CPT', 'unif'); +end + +i = stringmatch('LeftClr', names)+ss; +bnet.CPD{i} = tabular_CPD(bnet, i, [0.99 0.01 0.01 0.99]); + +i = stringmatch('RightClr', names)+ss; +bnet.CPD{i} = tabular_CPD(bnet, i, [0.99 0.01 0.01 0.99]); + +i = stringmatch('LatAct', names)+ss; +bnet.CPD{i} = tabular_CPD(bnet, i, [0.166666666666667 0.975609756097561 0.111111111111111 0.961538461538462 0.00980392156862745 0.0380952380952381 0.00952380952380952 0.037037037037037 0.166666666666667 0.444444444444444 0.0048780487804878 0.0192307692307692 0.0384615384615385 0.888888888888889 0.0344827586206897 0.87719298245614 0.00819672131147541 0.032 0.008 0.03125 0.0384615384615385 0.137931034482759 0.00444444444444444 0.0175438596491228 0.166666666666667 0.975609756097561 0.111111111111111 0.961538461538462 0.166666666666667 0.8 0.04 0.454545454545455 0.166666666666667 0.444444444444444 0.0048780487804878 0.0192307692307692 0.0384615384615385 0.888888888888889 0.0344827586206897 0.87719298245614 0.0384615384615385 0.444444444444444 0.0222222222222222 0.3125 0.0384615384615385 0.137931034482759 0.00444444444444444 0.0175438596491228 0.166666666666667 0.975609756097561 0.111111111111111 0.961538461538462 0.166666666666667 0.8 0.04 0.454545454545455 0.166666666666667 0.444444444444444 0.0048780487804878 0.0192307692307692 0.0384615384615385 0.888888888888889 0.0344827586206897 0.87719298245614 0.0384615384615385 0.444444444444444 0.0222222222222222 0.3125 0.0384615384615385 0.137931034482759 0.00444444444444444 0.0175438596491228 0.666666666666667 0.0195121951219512 0.444444444444444 0.0192307692307692 0.980392156862745 0.952380952380952 0.952380952380952 0.925925925925926 0.666666666666667 0.444444444444444 0.0195121951219512 0.0192307692307692 0.923076923076923 0.106666666666667 0.827586206896552 0.105263157894737 0.983606557377049 0.96 0.96 0.9375 0.923076923076923 0.827586206896552 0.106666666666667 0.105263157894737 0.666666666666667 0.0195121951219512 0.444444444444444 0.0192307692307692 0.666666666666667 0.16 0.16 0.0909090909090909 0.666666666666667 0.444444444444444 0.0195121951219512 0.0192307692307692 0.923076923076923 0.106666666666667 0.827586206896552 0.105263157894737 0.923076923076923 0.533333333333333 0.533333333333333 0.375 0.923076923076923 0.827586206896552 0.106666666666667 0.105263157894737 0.666666666666667 0.0195121951219512 0.444444444444444 0.0192307692307692 0.666666666666667 0.16 0.16 0.0909090909090909 0.666666666666667 0.444444444444444 0.0195121951219512 0.0192307692307692 0.923076923076923 0.106666666666667 0.827586206896552 0.105263157894737 0.923076923076923 0.533333333333333 0.533333333333333 0.375 0.923076923076923 0.827586206896552 0.106666666666667 0.105263157894737 0.166666666666667 0.0048780487804878 0.444444444444444 0.0192307692307692 0.00980392156862745 0.00952380952380952 0.0380952380952381 0.037037037037037 0.166666666666667 0.111111111111111 0.975609756097561 0.961538461538462 0.0384615384615385 0.00444444444444444 0.137931034482759 0.0175438596491228 0.00819672131147541 0.008 0.032 0.03125 0.0384615384615385 0.0344827586206897 0.888888888888889 0.87719298245614 0.166666666666667 0.0048780487804878 0.444444444444444 0.0192307692307692 0.166666666666667 0.04 0.8 0.454545454545455 0.166666666666667 0.111111111111111 0.975609756097561 0.961538461538462 0.0384615384615385 0.00444444444444444 0.137931034482759 0.0175438596491228 0.0384615384615385 0.0222222222222222 0.444444444444444 0.3125 0.0384615384615385 0.0344827586206897 0.888888888888889 0.87719298245614 0.166666666666667 0.0048780487804878 0.444444444444444 0.0192307692307692 0.166666666666667 0.04 0.8 0.454545454545455 0.166666666666667 0.111111111111111 0.975609756097561 0.961538461538462 0.0384615384615385 0.00444444444444444 0.137931034482759 0.0175438596491228 0.0384615384615385 0.0222222222222222 0.444444444444444 0.3125 0.0384615384615385 0.0344827586206897 0.888888888888889 0.87719298245614]); + +i = stringmatch('Xdot', names)+ss; +bnet.CPD{i} = tabular_CPD(bnet, i, [0.497512437810945 0.115207373271889 0.0564971751412429 0.0290697674418605 0.075187969924812 0.0300751879699248 0.0298507462686567 0.0980392156862745 0.0392156862745098 0.043956043956044 0.0294117647058824 0.0283687943262411 0.0392156862745098 0.0392156862745098 0.0373134328358209 0.0300751879699248 0.0300751879699248 0.0232558139534884 0.0225988700564972 0.0184331797235023 0.0199004975124378 0.373134328358209 0.460829493087558 0.282485875706215 0.290697674418605 0.37593984962406 0.075187969924812 0.0373134328358209 0.490196078431373 0.0980392156862745 0.043956043956044 0.0294117647058824 0.0283687943262411 0.0392156862745098 0.0392156862745098 0.0746268656716418 0.037593984962406 0.0300751879699248 0.0232558139534884 0.0225988700564972 0.0184331797235023 0.0199004975124378 0.0497512437810945 0.345622119815668 0.564971751412429 0.581395348837209 0.37593984962406 0.37593984962406 0.0746268656716418 0.245098039215686 0.490196078431373 0.21978021978022 0.0735294117647059 0.0354609929078014 0.0490196078431373 0.0392156862745098 0.373134328358209 0.075187969924812 0.037593984962406 0.0232558139534884 0.0225988700564972 0.0184331797235023 0.0199004975124378 0.0199004975124378 0.0230414746543779 0.0282485875706215 0.0290697674418605 0.075187969924812 0.37593984962406 0.373134328358209 0.0490196078431373 0.245098039215686 0.54945054945055 0.735294117647059 0.709219858156028 0.245098039215686 0.0490196078431373 0.373134328358209 0.37593984962406 0.075187969924812 0.0290697674418605 0.0282485875706215 0.0230414746543779 0.0199004975124378 0.0199004975124378 0.0184331797235023 0.0225988700564972 0.0232558139534884 0.037593984962406 0.075187969924812 0.373134328358209 0.0392156862745098 0.0490196078431373 0.0549450549450549 0.0735294117647059 0.141843971631206 0.490196078431373 0.245098039215686 0.0746268656716418 0.37593984962406 0.37593984962406 0.581395348837209 0.564971751412429 0.345622119815668 0.0497512437810945 0.0199004975124378 0.0184331797235023 0.0225988700564972 0.0232558139534884 0.0300751879699248 0.037593984962406 0.0746268656716418 0.0392156862745098 0.0392156862745098 0.043956043956044 0.0294117647058824 0.0283687943262411 0.0980392156862745 0.490196078431373 0.0373134328358209 0.075187969924812 0.37593984962406 0.290697674418605 0.282485875706215 0.460829493087558 0.373134328358209 0.0199004975124378 0.0184331797235023 0.0225988700564972 0.0232558139534884 0.0300751879699248 0.0300751879699248 0.0373134328358209 0.0392156862745098 0.0392156862745098 0.043956043956044 0.0294117647058824 0.0283687943262411 0.0392156862745098 0.0980392156862745 0.0298507462686567 0.0300751879699248 0.075187969924812 0.0290697674418605 0.0564971751412429 0.115207373271889 0.497512437810945]); + +i = stringmatch('InLane', names)+ss; +bnet.CPD{i} = tabular_CPD(bnet, i, [0.5 0.3 0.5 0.3 0.5 0.3 0.9 0.01 0.5 0.3 0.5 0.3 0.5 0.3 0.5 0.7 0.5 0.7 0.5 0.7 0.1 0.99 0.5 0.7 0.5 0.7 0.5 0.7]); + +i = stringmatch('FwdAct', names)+ss; +bnet.CPD{i} = tabular_CPD(bnet, i, [0.4 0.4 0.4 0.949050949050949 0.949050949050949 0.949050949050949 0.4 0.4 0.4 0.6 0.1 0.1 0.949050949050949 0.949050949050949 0.949050949050949 0.05 0.05 0.05 0.4 0.4 0.4 0.949050949050949 0.949050949050949 0.949050949050949 0.4 0.4 0.4 0.4 0.4 0.4 0.04995004995005 0.04995004995005 0.04995004995005 0.4 0.4 0.4 0.3 0.8 0.3 0.04995004995005 0.04995004995005 0.04995004995005 0.7 0.7 0.7 0.4 0.4 0.4 0.04995004995005 0.04995004995005 0.04995004995005 0.4 0.4 0.4 0.2 0.2 0.2 0.000999000999000999 0.000999000999000999 0.000999000999000999 0.2 0.2 0.2 0.1 0.1 0.6 0.000999000999000999 0.000999000999000999 0.000999000999000999 0.25 0.25 0.25 0.2 0.2 0.2 0.000999000999000999 0.000999000999000999 0.000999000999000999 0.2 0.2 0.2]); + +i = stringmatch('Ydot', names)+ss; +bnet.CPD{i} = tabular_CPD(bnet, i, [0.999880014398272 0.999880014398272 0.999880014398272 0.999880014398272 0.999880014398272 0.999880014398272 0.999880014398272 0.999880014398272 0.999880014398272 0.999880014398272 0.999880014398272 0.999880014398272 0.999880014398272 0.999880014398272 0.999880014398272 0.999880014398272 0.999880014398272 0.999880014398272 0.999880014398272 0.999880014398272 0.999880014398272 0.999880014398272 0.999880014398272 0.999880014398272 0.999880014398272 0.999880014398272 0.999880014398272 0.999880014398272 0.999880014398272 0.999880014398272 0.999880014398272 0.999880014398272 0.999880014398272 0.72463768115942 0.595238095238095 0.230414746543779 0.0442477876106195 0.00442477876106195 0.00442477876106195 0.00442477876106195 0.00442477876106195 0.00442477876106195 0.00442477876106195 0.00460829493087558 0.595238095238095 0.230414746543779 0.0442477876106195 0.00442477876106195 0.00442477876106195 0.00442477876106195 0.00442477876106195 0.00442477876106195 0.00442477876106195 0.00460829493087558 0.00595238095238095 0.230414746543779 0.0442477876106195 0.00442477876106195 0.00442477876106195 0.00442477876106195 0.00442477876106195 0.00442477876106195 0.00442477876106195 0.00460829493087558 0.00595238095238095 0.0144927536231884 9.99880014398272e-005 9.99880014398272e-005 9.99880014398272e-005 9.99880014398272e-005 9.99880014398272e-005 9.99880014398272e-005 9.99880014398272e-005 9.99880014398272e-005 9.99880014398272e-005 9.99880014398272e-005 9.99880014398272e-005 9.99880014398272e-005 9.99880014398272e-005 9.99880014398272e-005 9.99880014398272e-005 9.99880014398272e-005 9.99880014398272e-005 9.99880014398272e-005 9.99880014398272e-005 9.99880014398272e-005 9.99880014398272e-005 9.99880014398272e-005 9.99880014398272e-005 9.99880014398272e-005 9.99880014398272e-005 9.99880014398272e-005 9.99880014398272e-005 9.99880014398272e-005 9.99880014398272e-005 9.99880014398272e-005 9.99880014398272e-005 9.99880014398272e-005 9.99880014398272e-005 0.144927536231884 0.297619047619048 0.460829493087558 0.221238938053097 0.0442477876106195 0.00442477876106195 0.00442477876106195 0.00442477876106195 0.00442477876106195 0.00442477876106195 0.00460829493087558 0.297619047619048 0.460829493087558 0.221238938053097 0.0442477876106195 0.00442477876106195 0.00442477876106195 0.00442477876106195 0.00442477876106195 0.00442477876106195 0.00460829493087558 0.00595238095238095 0.460829493087558 0.221238938053097 0.0442477876106195 0.00442477876106195 0.00442477876106195 0.00442477876106195 0.00442477876106195 0.00442477876106195 0.00460829493087558 0.00595238095238095 0.0144927536231884 9.99880014398272e-006 9.99880014398272e-006 9.99880014398272e-006 9.99880014398272e-006 9.99880014398272e-006 9.99880014398272e-006 9.99880014398272e-006 9.99880014398272e-006 9.99880014398272e-006 9.99880014398272e-006 9.99880014398272e-006 9.99880014398272e-006 9.99880014398272e-006 9.99880014398272e-006 9.99880014398272e-006 9.99880014398272e-006 9.99880014398272e-006 9.99880014398272e-006 9.99880014398272e-006 9.99880014398272e-006 9.99880014398272e-006 9.99880014398272e-006 9.99880014398272e-006 9.99880014398272e-006 9.99880014398272e-006 9.99880014398272e-006 9.99880014398272e-006 9.99880014398272e-006 9.99880014398272e-006 9.99880014398272e-006 9.99880014398272e-006 9.99880014398272e-006 9.99880014398272e-006 0.0144927536231884 0.0595238095238095 0.230414746543779 0.442477876106195 0.221238938053097 0.0442477876106195 0.00442477876106195 0.00442477876106195 0.00442477876106195 0.00442477876106195 0.00460829493087558 0.0595238095238095 0.230414746543779 0.442477876106195 0.221238938053097 0.0442477876106195 0.00442477876106195 0.00442477876106195 0.00442477876106195 0.00442477876106195 0.00460829493087558 0.00595238095238095 0.230414746543779 0.442477876106195 0.221238938053097 0.0442477876106195 0.00442477876106195 0.00442477876106195 0.00442477876106195 0.00442477876106195 0.00460829493087558 0.00595238095238095 0.0144927536231884 9.99880014398272e-006 9.99880014398272e-006 9.99880014398272e-006 9.99880014398272e-006 9.99880014398272e-006 9.99880014398272e-006 9.99880014398272e-006 9.99880014398272e-006 9.99880014398272e-006 9.99880014398272e-006 9.99880014398272e-006 9.99880014398272e-006 9.99880014398272e-006 9.99880014398272e-006 9.99880014398272e-006 9.99880014398272e-006 9.99880014398272e-006 9.99880014398272e-006 9.99880014398272e-006 9.99880014398272e-006 9.99880014398272e-006 9.99880014398272e-006 9.99880014398272e-006 9.99880014398272e-006 9.99880014398272e-006 9.99880014398272e-006 9.99880014398272e-006 9.99880014398272e-006 9.99880014398272e-006 9.99880014398272e-006 9.99880014398272e-006 9.99880014398272e-006 9.99880014398272e-006 0.0144927536231884 0.00595238095238095 0.0460829493087558 0.221238938053097 0.442477876106195 0.221238938053097 0.0442477876106195 0.00442477876106195 0.00442477876106195 0.00442477876106195 0.00460829493087558 0.00595238095238095 0.0460829493087558 0.221238938053097 0.442477876106195 0.221238938053097 0.0442477876106195 0.00442477876106195 0.00442477876106195 0.00442477876106195 0.00460829493087558 0.00595238095238095 0.0460829493087558 0.221238938053097 0.442477876106195 0.221238938053097 0.0442477876106195 0.00442477876106195 0.00442477876106195 0.00442477876106195 0.00460829493087558 0.00595238095238095 0.0144927536231884 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0144927536231884 0.00595238095238095 0.00460829493087558 0.0442477876106195 0.221238938053097 0.442477876106195 0.221238938053097 0.0442477876106195 0.00442477876106195 0.00442477876106195 0.00460829493087558 0.00595238095238095 0.00460829493087558 0.0442477876106195 0.221238938053097 0.442477876106195 0.221238938053097 0.0442477876106195 0.00442477876106195 0.00442477876106195 0.00460829493087558 0.00595238095238095 0.00460829493087558 0.0442477876106195 0.221238938053097 0.442477876106195 0.221238938053097 0.0442477876106195 0.00442477876106195 0.00442477876106195 0.00460829493087558 0.00595238095238095 0.0144927536231884 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0144927536231884 0.00595238095238095 0.00460829493087558 0.00442477876106195 0.0442477876106195 0.221238938053097 0.442477876106195 0.221238938053097 0.0442477876106195 0.00442477876106195 0.00460829493087558 0.00595238095238095 0.00460829493087558 0.00442477876106195 0.0442477876106195 0.221238938053097 0.442477876106195 0.221238938053097 0.0442477876106195 0.00442477876106195 0.00460829493087558 0.00595238095238095 0.00460829493087558 0.00442477876106195 0.0442477876106195 0.221238938053097 0.442477876106195 0.221238938053097 0.0442477876106195 0.00442477876106195 0.00460829493087558 0.00595238095238095 0.0144927536231884 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0144927536231884 0.00595238095238095 0.00460829493087558 0.00442477876106195 0.00442477876106195 0.0442477876106195 0.221238938053097 0.442477876106195 0.221238938053097 0.0442477876106195 0.00460829493087558 0.00595238095238095 0.00460829493087558 0.00442477876106195 0.00442477876106195 0.0442477876106195 0.221238938053097 0.442477876106195 0.221238938053097 0.0442477876106195 0.00460829493087558 0.00595238095238095 0.00460829493087558 0.00442477876106195 0.00442477876106195 0.0442477876106195 0.221238938053097 0.442477876106195 0.221238938053097 0.0442477876106195 0.00460829493087558 0.00595238095238095 0.0144927536231884 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0144927536231884 0.00595238095238095 0.00460829493087558 0.00442477876106195 0.00442477876106195 0.00442477876106195 0.0442477876106195 0.221238938053097 0.442477876106195 0.221238938053097 0.0460829493087558 0.00595238095238095 0.00460829493087558 0.00442477876106195 0.00442477876106195 0.00442477876106195 0.0442477876106195 0.221238938053097 0.442477876106195 0.221238938053097 0.0460829493087558 0.00595238095238095 0.00460829493087558 0.00442477876106195 0.00442477876106195 0.00442477876106195 0.0442477876106195 0.221238938053097 0.442477876106195 0.221238938053097 0.0460829493087558 0.00595238095238095 0.0144927536231884 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0144927536231884 0.00595238095238095 0.00460829493087558 0.00442477876106195 0.00442477876106195 0.00442477876106195 0.00442477876106195 0.0442477876106195 0.221238938053097 0.442477876106195 0.230414746543779 0.00595238095238095 0.00460829493087558 0.00442477876106195 0.00442477876106195 0.00442477876106195 0.00442477876106195 0.0442477876106195 0.221238938053097 0.442477876106195 0.230414746543779 0.0595238095238095 0.00460829493087558 0.00442477876106195 0.00442477876106195 0.00442477876106195 0.00442477876106195 0.0442477876106195 0.221238938053097 0.442477876106195 0.230414746543779 0.0595238095238095 0.0144927536231884 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0144927536231884 0.00595238095238095 0.00460829493087558 0.00442477876106195 0.00442477876106195 0.00442477876106195 0.00442477876106195 0.00442477876106195 0.0442477876106195 0.221238938053097 0.460829493087558 0.00595238095238095 0.00460829493087558 0.00442477876106195 0.00442477876106195 0.00442477876106195 0.00442477876106195 0.00442477876106195 0.0442477876106195 0.221238938053097 0.460829493087558 0.297619047619048 0.00460829493087558 0.00442477876106195 0.00442477876106195 0.00442477876106195 0.00442477876106195 0.00442477876106195 0.0442477876106195 0.221238938053097 0.460829493087558 0.297619047619048 0.144927536231884 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0144927536231884 0.00595238095238095 0.00460829493087558 0.00442477876106195 0.00442477876106195 0.00442477876106195 0.00442477876106195 0.00442477876106195 0.00442477876106195 0.0442477876106195 0.230414746543779 0.00595238095238095 0.00460829493087558 0.00442477876106195 0.00442477876106195 0.00442477876106195 0.00442477876106195 0.00442477876106195 0.00442477876106195 0.0442477876106195 0.230414746543779 0.595238095238095 0.00460829493087558 0.00442477876106195 0.00442477876106195 0.00442477876106195 0.00442477876106195 0.00442477876106195 0.00442477876106195 0.0442477876106195 0.230414746543779 0.595238095238095 0.72463768115942]); + +i = stringmatch('Stopped', names)+ss; +bnet.CPD{i} = tabular_CPD(bnet, i, [0.0 0.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0]); + +i = stringmatch('EngStatus', names)+ss; +bnet.CPD{i} = tabular_CPD(bnet, i, [0.9 1.0e-006 0.1 0.999999]); + +i = stringmatch('FBStatus', names)+ss; +bnet.CPD{i} = tabular_CPD(bnet, i, [1.0 0.0 0.0 0.0 0.0 1.0 0.0 1.0 0.0 0.0 1.0 0.0]); + +i = stringmatch('SensorValid', names); +%bnet.CPD{i} = tabular_CPD(bnet, i, [1.0e-004 0.9999]); +bnet.CPD{i+ss} = tabular_CPD(bnet, i+ss, [1.0e-004 0.9999]); + +i = stringmatch('FYdotDiff', names); +%bnet.CPD{i} = tabular_CPD(bnet, i, [0.908182726364545 0.238095238095238 0.000908182726364545 0.476190476190476 9.08182726364545e-005 0.238095238095238 0.0908182726364545 0.0476190476190476]); +bnet.CPD{i+ss} = tabular_CPD(bnet, i+ss, [0.908182726364545 0.238095238095238 0.000908182726364545 0.476190476190476 9.08182726364545e-005 0.238095238095238 0.0908182726364545 0.0476190476190476]); + +i = stringmatch('FcloseSlow', names); +%bnet.CPD{i} = tabular_CPD(bnet, i, [1.0 1.0 1.0 1.0 1.0 1.0 0.0 1.0 1.0 1.0 1.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0]); +bnet.CPD{i+ss} = tabular_CPD(bnet, i+ss, [1.0 1.0 1.0 1.0 1.0 1.0 0.0 1.0 1.0 1.0 1.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0]); + +i = stringmatch('Fclr', names); +%bnet.CPD{i} = tabular_CPD(bnet, i, [0.000499750124937531 0.142857142857143 0.499750124937531 0.285714285714286 0.499750124937531 0.571428571428571]); +bnet.CPD{i+ss} = tabular_CPD(bnet, i+ss, [0.000499750124937531 0.142857142857143 0.499750124937531 0.285714285714286 0.499750124937531 0.571428571428571]); + +i = stringmatch('BXdot', names); +%bnet.CPD{i} = tabular_CPD(bnet, i, [0.00980392156862745 0.00980392156862745 0.00980392156862745 0.00980392156862745 0.0454545454545455 0.00980392156862745 0.0490196078431373 0.0490196078431373 0.0490196078431373 0.0490196078431373 0.136363636363636 0.0490196078431373 0.196078431372549 0.196078431372549 0.196078431372549 0.196078431372549 0.136363636363636 0.196078431372549 0.392156862745098 0.392156862745098 0.392156862745098 0.392156862745098 0.181818181818182 0.392156862745098 0.196078431372549 0.196078431372549 0.196078431372549 0.196078431372549 0.136363636363636 0.196078431372549 0.0490196078431373 0.0490196078431373 0.0490196078431373 0.0490196078431373 0.136363636363636 0.0490196078431373 0.00980392156862745 0.00980392156862745 0.00980392156862745 0.00980392156862745 0.0454545454545455 0.00980392156862745 0.0980392156862745 0.0980392156862745 0.0980392156862745 0.0980392156862745 0.181818181818182 0.0980392156862745]); +bnet.CPD{i+ss} = tabular_CPD(bnet, i+ss, [0.00980392156862745 0.00980392156862745 0.00980392156862745 0.00980392156862745 0.0454545454545455 0.00980392156862745 0.0490196078431373 0.0490196078431373 0.0490196078431373 0.0490196078431373 0.136363636363636 0.0490196078431373 0.196078431372549 0.196078431372549 0.196078431372549 0.196078431372549 0.136363636363636 0.196078431372549 0.392156862745098 0.392156862745098 0.392156862745098 0.392156862745098 0.181818181818182 0.392156862745098 0.196078431372549 0.196078431372549 0.196078431372549 0.196078431372549 0.136363636363636 0.196078431372549 0.0490196078431373 0.0490196078431373 0.0490196078431373 0.0490196078431373 0.136363636363636 0.0490196078431373 0.00980392156862745 0.00980392156862745 0.00980392156862745 0.00980392156862745 0.0454545454545455 0.00980392156862745 0.0980392156862745 0.0980392156862745 0.0980392156862745 0.0980392156862745 0.181818181818182 0.0980392156862745]); + +i = stringmatch('BcloseFast', names); +%bnet.CPD{i} = tabular_CPD(bnet, i, [0.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0]); +bnet.CPD{i+ss} = tabular_CPD(bnet, i+ss, [0.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0]); + +i = stringmatch('Bclr', names); +%bnet.CPD{i} = tabular_CPD(bnet, i, [0.142857142857143 0.285714285714286 0.571428571428571]); +bnet.CPD{i+ss} = tabular_CPD(bnet, i+ss, [0.142857142857143 0.285714285714286 0.571428571428571]); + +i = stringmatch('BYdotDiff', names); +%bnet.CPD{i} = tabular_CPD(bnet, i, [0.238095238095238 0.476190476190476 0.238095238095238 0.0476190476190476]); +bnet.CPD{i+ss} = tabular_CPD(bnet, i+ss, [0.238095238095238 0.476190476190476 0.238095238095238 0.0476190476190476]); + +i = stringmatch('LeftClrSens', names); +%bnet.CPD{i} = tabular_CPD(bnet, i, [0.909090909090909 0.0909090909090909 0.0909090909090909 0.909090909090909]); +bnet.CPD{i+ss} = tabular_CPD(bnet, i+ss, [0.909090909090909 0.0909090909090909 0.0909090909090909 0.909090909090909]); + +i = stringmatch('RightClrSens', names); +%bnet.CPD{i} = tabular_CPD(bnet, i, [0.909090909090909 0.0909090909090909 0.0909090909090909 0.909090909090909]); +bnet.CPD{i+ss} = tabular_CPD(bnet, i+ss, [0.909090909090909 0.0909090909090909 0.0909090909090909 0.909090909090909]); + +i = stringmatch('TurnSignalSens', names); +%bnet.CPD{i} = tabular_CPD(bnet, i, [0.75 0.000998003992015968 0.01 0.24 0.998003992015968 0.24 0.01 0.000998003992015968 0.75]); +bnet.CPD{i+ss} = tabular_CPD(bnet, i+ss, [0.75 0.000998003992015968 0.01 0.24 0.998003992015968 0.24 0.01 0.000998003992015968 0.75]); + +i = stringmatch('XdotSens', names); +%bnet.CPD{i} = tabular_CPD(bnet, i, [0.142857142857143 0.897666068222621 0.142857142857143 0.0824402308326463 0.142857142857143 0.00818330605564648 0.142857142857143 0.000818330605564648 0.142857142857143 0.000818330605564648 0.142857142857143 0.000824402308326463 0.142857142857143 0.000897666068222621 0.142857142857143 0.0897666068222621 0.142857142857143 0.824402308326463 0.142857142857143 0.0818330605564648 0.142857142857143 0.00818330605564648 0.142857142857143 0.000818330605564648 0.142857142857143 0.000824402308326463 0.142857142857143 0.000897666068222621 0.142857142857143 0.00897666068222621 0.142857142857143 0.0824402308326463 0.142857142857143 0.818330605564648 0.142857142857143 0.0818330605564648 0.142857142857143 0.00818330605564648 0.142857142857143 0.000824402308326463 0.142857142857143 0.000897666068222621 0.142857142857143 0.000897666068222621 0.142857142857143 0.00824402308326463 0.142857142857143 0.0818330605564648 0.142857142857143 0.818330605564648 0.142857142857143 0.0818330605564648 0.142857142857143 0.00824402308326463 0.142857142857143 0.000897666068222621 0.142857142857143 0.000897666068222621 0.142857142857143 0.000824402308326463 0.142857142857143 0.00818330605564648 0.142857142857143 0.0818330605564648 0.142857142857143 0.818330605564648 0.142857142857143 0.0824402308326463 0.142857142857143 0.00897666068222621 0.142857142857143 0.000897666068222621 0.142857142857143 0.000824402308326463 0.142857142857143 0.000818330605564648 0.142857142857143 0.00818330605564648 0.142857142857143 0.0818330605564648 0.142857142857143 0.824402308326463 0.142857142857143 0.0897666068222621 0.142857142857143 0.000897666068222621 0.142857142857143 0.000824402308326463 0.142857142857143 0.000818330605564648 0.142857142857143 0.000818330605564648 0.142857142857143 0.00818330605564648 0.142857142857143 0.0824402308326463 0.142857142857143 0.897666068222621]); +bnet.CPD{i+ss} = tabular_CPD(bnet, i+ss, [0.142857142857143 0.897666068222621 0.142857142857143 0.0824402308326463 0.142857142857143 0.00818330605564648 0.142857142857143 0.000818330605564648 0.142857142857143 0.000818330605564648 0.142857142857143 0.000824402308326463 0.142857142857143 0.000897666068222621 0.142857142857143 0.0897666068222621 0.142857142857143 0.824402308326463 0.142857142857143 0.0818330605564648 0.142857142857143 0.00818330605564648 0.142857142857143 0.000818330605564648 0.142857142857143 0.000824402308326463 0.142857142857143 0.000897666068222621 0.142857142857143 0.00897666068222621 0.142857142857143 0.0824402308326463 0.142857142857143 0.818330605564648 0.142857142857143 0.0818330605564648 0.142857142857143 0.00818330605564648 0.142857142857143 0.000824402308326463 0.142857142857143 0.000897666068222621 0.142857142857143 0.000897666068222621 0.142857142857143 0.00824402308326463 0.142857142857143 0.0818330605564648 0.142857142857143 0.818330605564648 0.142857142857143 0.0818330605564648 0.142857142857143 0.00824402308326463 0.142857142857143 0.000897666068222621 0.142857142857143 0.000897666068222621 0.142857142857143 0.000824402308326463 0.142857142857143 0.00818330605564648 0.142857142857143 0.0818330605564648 0.142857142857143 0.818330605564648 0.142857142857143 0.0824402308326463 0.142857142857143 0.00897666068222621 0.142857142857143 0.000897666068222621 0.142857142857143 0.000824402308326463 0.142857142857143 0.000818330605564648 0.142857142857143 0.00818330605564648 0.142857142857143 0.0818330605564648 0.142857142857143 0.824402308326463 0.142857142857143 0.0897666068222621 0.142857142857143 0.000897666068222621 0.142857142857143 0.000824402308326463 0.142857142857143 0.000818330605564648 0.142857142857143 0.000818330605564648 0.142857142857143 0.00818330605564648 0.142857142857143 0.0824402308326463 0.142857142857143 0.897666068222621]); + +i = stringmatch('YdotSens', names); +%bnet.CPD{i} = tabular_CPD(bnet, i, [0.0909090909090909 0.894454382826476 0.0909090909090909 0.0821692686935086 0.0909090909090909 0.00815660685154975 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000821692686935086 0.0909090909090909 0.000894454382826476 0.0909090909090909 0.0894454382826476 0.0909090909090909 0.821692686935086 0.0909090909090909 0.0815660685154976 0.0909090909090909 0.00815660685154975 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000821692686935086 0.0909090909090909 0.000894454382826476 0.0909090909090909 0.00894454382826476 0.0909090909090909 0.0821692686935086 0.0909090909090909 0.815660685154976 0.0909090909090909 0.0815660685154976 0.0909090909090909 0.00815660685154975 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000821692686935086 0.0909090909090909 0.000894454382826476 0.0909090909090909 0.000894454382826476 0.0909090909090909 0.00821692686935086 0.0909090909090909 0.0815660685154976 0.0909090909090909 0.815660685154976 0.0909090909090909 0.0815660685154976 0.0909090909090909 0.00815660685154975 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000821692686935086 0.0909090909090909 0.000894454382826476 0.0909090909090909 0.000894454382826476 0.0909090909090909 0.000821692686935086 0.0909090909090909 0.00815660685154975 0.0909090909090909 0.0815660685154976 0.0909090909090909 0.815660685154976 0.0909090909090909 0.0815660685154976 0.0909090909090909 0.00815660685154975 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000821692686935086 0.0909090909090909 0.000894454382826476 0.0909090909090909 0.000894454382826476 0.0909090909090909 0.000821692686935086 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.00815660685154975 0.0909090909090909 0.0815660685154976 0.0909090909090909 0.815660685154976 0.0909090909090909 0.0815660685154976 0.0909090909090909 0.00815660685154975 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000821692686935086 0.0909090909090909 0.000894454382826476 0.0909090909090909 0.000894454382826476 0.0909090909090909 0.000821692686935086 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.00815660685154975 0.0909090909090909 0.0815660685154976 0.0909090909090909 0.815660685154976 0.0909090909090909 0.0815660685154976 0.0909090909090909 0.00815660685154975 0.0909090909090909 0.000821692686935086 0.0909090909090909 0.000894454382826476 0.0909090909090909 0.000894454382826476 0.0909090909090909 0.000821692686935086 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.00815660685154975 0.0909090909090909 0.0815660685154976 0.0909090909090909 0.815660685154976 0.0909090909090909 0.0815660685154976 0.0909090909090909 0.00821692686935086 0.0909090909090909 0.000894454382826476 0.0909090909090909 0.000894454382826476 0.0909090909090909 0.000821692686935086 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.00815660685154975 0.0909090909090909 0.0815660685154976 0.0909090909090909 0.815660685154976 0.0909090909090909 0.0821692686935086 0.0909090909090909 0.00894454382826476 0.0909090909090909 0.000894454382826476 0.0909090909090909 0.000821692686935086 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.00815660685154975 0.0909090909090909 0.0815660685154976 0.0909090909090909 0.821692686935086 0.0909090909090909 0.0894454382826476 0.0909090909090909 0.000894454382826476 0.0909090909090909 0.000821692686935086 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.00815660685154975 0.0909090909090909 0.0821692686935086 0.0909090909090909 0.894454382826476]); +bnet.CPD{i+ss} = tabular_CPD(bnet, i+ss, [0.0909090909090909 0.894454382826476 0.0909090909090909 0.0821692686935086 0.0909090909090909 0.00815660685154975 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000821692686935086 0.0909090909090909 0.000894454382826476 0.0909090909090909 0.0894454382826476 0.0909090909090909 0.821692686935086 0.0909090909090909 0.0815660685154976 0.0909090909090909 0.00815660685154975 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000821692686935086 0.0909090909090909 0.000894454382826476 0.0909090909090909 0.00894454382826476 0.0909090909090909 0.0821692686935086 0.0909090909090909 0.815660685154976 0.0909090909090909 0.0815660685154976 0.0909090909090909 0.00815660685154975 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000821692686935086 0.0909090909090909 0.000894454382826476 0.0909090909090909 0.000894454382826476 0.0909090909090909 0.00821692686935086 0.0909090909090909 0.0815660685154976 0.0909090909090909 0.815660685154976 0.0909090909090909 0.0815660685154976 0.0909090909090909 0.00815660685154975 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000821692686935086 0.0909090909090909 0.000894454382826476 0.0909090909090909 0.000894454382826476 0.0909090909090909 0.000821692686935086 0.0909090909090909 0.00815660685154975 0.0909090909090909 0.0815660685154976 0.0909090909090909 0.815660685154976 0.0909090909090909 0.0815660685154976 0.0909090909090909 0.00815660685154975 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000821692686935086 0.0909090909090909 0.000894454382826476 0.0909090909090909 0.000894454382826476 0.0909090909090909 0.000821692686935086 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.00815660685154975 0.0909090909090909 0.0815660685154976 0.0909090909090909 0.815660685154976 0.0909090909090909 0.0815660685154976 0.0909090909090909 0.00815660685154975 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000821692686935086 0.0909090909090909 0.000894454382826476 0.0909090909090909 0.000894454382826476 0.0909090909090909 0.000821692686935086 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.00815660685154975 0.0909090909090909 0.0815660685154976 0.0909090909090909 0.815660685154976 0.0909090909090909 0.0815660685154976 0.0909090909090909 0.00815660685154975 0.0909090909090909 0.000821692686935086 0.0909090909090909 0.000894454382826476 0.0909090909090909 0.000894454382826476 0.0909090909090909 0.000821692686935086 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.00815660685154975 0.0909090909090909 0.0815660685154976 0.0909090909090909 0.815660685154976 0.0909090909090909 0.0815660685154976 0.0909090909090909 0.00821692686935086 0.0909090909090909 0.000894454382826476 0.0909090909090909 0.000894454382826476 0.0909090909090909 0.000821692686935086 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.00815660685154975 0.0909090909090909 0.0815660685154976 0.0909090909090909 0.815660685154976 0.0909090909090909 0.0821692686935086 0.0909090909090909 0.00894454382826476 0.0909090909090909 0.000894454382826476 0.0909090909090909 0.000821692686935086 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.00815660685154975 0.0909090909090909 0.0815660685154976 0.0909090909090909 0.821692686935086 0.0909090909090909 0.0894454382826476 0.0909090909090909 0.000894454382826476 0.0909090909090909 0.000821692686935086 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.000815660685154976 0.0909090909090909 0.00815660685154975 0.0909090909090909 0.0821692686935086 0.0909090909090909 0.894454382826476]); + +i = stringmatch('FYdotDiffSens', names); +%bnet.CPD{i} = tabular_CPD(bnet, i, [0.53191206429753 0.00806445109313635 0.0053191206429753 9.9930048965724e-005 0.265956032148765 0.00806445109313635 0.0053191206429753 9.9930048965724e-005 0.132978016074383 0.0806445109313634 0.0053191206429753 9.9930048965724e-005 0.053191206429753 0.806445109313635 0.053191206429753 9.9930048965724e-005 0.0053191206429753 0.0806445109313634 0.132978016074383 9.9930048965724e-005 0.0053191206429753 0.00806445109313635 0.265956032148765 9.9930048965724e-005 0.0053191206429753 0.00806445109313635 0.53191206429753 9.9930048965724e-005 5.3191206429753e-006 8.06445109313635e-006 5.3191206429753e-006 0.99930048965724]); +bnet.CPD{i+ss} = tabular_CPD(bnet, i+ss, [0.53191206429753 0.00806445109313635 0.0053191206429753 9.9930048965724e-005 0.265956032148765 0.00806445109313635 0.0053191206429753 9.9930048965724e-005 0.132978016074383 0.0806445109313634 0.0053191206429753 9.9930048965724e-005 0.053191206429753 0.806445109313635 0.053191206429753 9.9930048965724e-005 0.0053191206429753 0.0806445109313634 0.132978016074383 9.9930048965724e-005 0.0053191206429753 0.00806445109313635 0.265956032148765 9.9930048965724e-005 0.0053191206429753 0.00806445109313635 0.53191206429753 9.9930048965724e-005 5.3191206429753e-006 8.06445109313635e-006 5.3191206429753e-006 0.99930048965724]); + +i = stringmatch('FclrSens', names); +%bnet.CPD{i} = tabular_CPD(bnet, i, [0.472366556447804 0.0044762757385855 6.20655412115194e-005 0.472366556447804 0.044762757385855 6.20655412115194e-005 0.0472366556447804 0.44762757385855 0.000620655412115194 0.000472366556447804 0.44762757385855 0.00620655412115194 0.000472366556447804 0.044762757385855 0.0620655412115194 0.000472366556447804 0.0044762757385855 0.0620655412115194 0.000472366556447804 0.00044762757385855 0.0620655412115194 0.000472366556447804 0.00044762757385855 0.0620655412115194 0.000472366556447804 0.00044762757385855 0.0620655412115194 0.000472366556447804 0.00044762757385855 0.0620655412115194 0.000472366556447804 0.00044762757385855 0.0620655412115194 0.000472366556447804 0.00044762757385855 0.0620655412115194 0.000472366556447804 0.00044762757385855 0.0620655412115194 0.000472366556447804 0.00044762757385855 0.0620655412115194 0.000472366556447804 0.00044762757385855 0.0620655412115194 0.000472366556447804 0.00044762757385855 0.0620655412115194 0.000472366556447804 0.00044762757385855 0.0620655412115194 0.000472366556447804 0.00044762757385855 0.0620655412115194 0.000472366556447804 0.00044762757385855 0.0620655412115194 0.000472366556447804 0.00044762757385855 0.0620655412115194]); +bnet.CPD{i+ss} = tabular_CPD(bnet, i+ss, [0.472366556447804 0.0044762757385855 6.20655412115194e-005 0.472366556447804 0.044762757385855 6.20655412115194e-005 0.0472366556447804 0.44762757385855 0.000620655412115194 0.000472366556447804 0.44762757385855 0.00620655412115194 0.000472366556447804 0.044762757385855 0.0620655412115194 0.000472366556447804 0.0044762757385855 0.0620655412115194 0.000472366556447804 0.00044762757385855 0.0620655412115194 0.000472366556447804 0.00044762757385855 0.0620655412115194 0.000472366556447804 0.00044762757385855 0.0620655412115194 0.000472366556447804 0.00044762757385855 0.0620655412115194 0.000472366556447804 0.00044762757385855 0.0620655412115194 0.000472366556447804 0.00044762757385855 0.0620655412115194 0.000472366556447804 0.00044762757385855 0.0620655412115194 0.000472366556447804 0.00044762757385855 0.0620655412115194 0.000472366556447804 0.00044762757385855 0.0620655412115194 0.000472366556447804 0.00044762757385855 0.0620655412115194 0.000472366556447804 0.00044762757385855 0.0620655412115194 0.000472366556447804 0.00044762757385855 0.0620655412115194 0.000472366556447804 0.00044762757385855 0.0620655412115194 0.000472366556447804 0.00044762757385855 0.0620655412115194]); + +i = stringmatch('BXdotSens', names); +%bnet.CPD{i} = tabular_CPD(bnet, i, [0.689655172413793 0.172413793103448 0.0546448087431694 0.00546448087431694 0.00546448087431694 0.00546448087431694 0.00574712643678161 0.00689655172413793 0.206896551724138 0.574712643678161 0.163934426229508 0.0546448087431694 0.00546448087431694 0.00546448087431694 0.00574712643678161 0.00689655172413793 0.0689655172413793 0.172413793103448 0.546448087431694 0.163934426229508 0.0546448087431694 0.00546448087431694 0.00574712643678161 0.00689655172413793 0.00689655172413793 0.0574712643678161 0.163934426229508 0.546448087431694 0.163934426229508 0.0546448087431694 0.00574712643678161 0.00689655172413793 0.00689655172413793 0.00574712643678161 0.0546448087431694 0.163934426229508 0.546448087431694 0.163934426229508 0.0574712643678161 0.00689655172413793 0.00689655172413793 0.00574712643678161 0.00546448087431694 0.0546448087431694 0.163934426229508 0.546448087431694 0.172413793103448 0.0689655172413793 0.00689655172413793 0.00574712643678161 0.00546448087431694 0.00546448087431694 0.0546448087431694 0.163934426229508 0.574712643678161 0.206896551724138 0.00689655172413793 0.00574712643678161 0.00546448087431694 0.00546448087431694 0.00546448087431694 0.0546448087431694 0.172413793103448 0.689655172413793]); +bnet.CPD{i+ss} = tabular_CPD(bnet, i+ss, [0.689655172413793 0.172413793103448 0.0546448087431694 0.00546448087431694 0.00546448087431694 0.00546448087431694 0.00574712643678161 0.00689655172413793 0.206896551724138 0.574712643678161 0.163934426229508 0.0546448087431694 0.00546448087431694 0.00546448087431694 0.00574712643678161 0.00689655172413793 0.0689655172413793 0.172413793103448 0.546448087431694 0.163934426229508 0.0546448087431694 0.00546448087431694 0.00574712643678161 0.00689655172413793 0.00689655172413793 0.0574712643678161 0.163934426229508 0.546448087431694 0.163934426229508 0.0546448087431694 0.00574712643678161 0.00689655172413793 0.00689655172413793 0.00574712643678161 0.0546448087431694 0.163934426229508 0.546448087431694 0.163934426229508 0.0574712643678161 0.00689655172413793 0.00689655172413793 0.00574712643678161 0.00546448087431694 0.0546448087431694 0.163934426229508 0.546448087431694 0.172413793103448 0.0689655172413793 0.00689655172413793 0.00574712643678161 0.00546448087431694 0.00546448087431694 0.0546448087431694 0.163934426229508 0.574712643678161 0.206896551724138 0.00689655172413793 0.00574712643678161 0.00546448087431694 0.00546448087431694 0.00546448087431694 0.0546448087431694 0.172413793103448 0.689655172413793]); + +i = stringmatch('BclrSens', names); +%bnet.CPD{i} = tabular_CPD(bnet, i, [0.472366556447804 0.0044762757385855 6.20655412115194e-005 0.472366556447804 0.044762757385855 6.20655412115194e-005 0.0472366556447804 0.44762757385855 0.000620655412115194 0.000472366556447804 0.44762757385855 0.00620655412115194 0.000472366556447804 0.044762757385855 0.0620655412115194 0.000472366556447804 0.0044762757385855 0.0620655412115194 0.000472366556447804 0.00044762757385855 0.0620655412115194 0.000472366556447804 0.00044762757385855 0.0620655412115194 0.000472366556447804 0.00044762757385855 0.0620655412115194 0.000472366556447804 0.00044762757385855 0.0620655412115194 0.000472366556447804 0.00044762757385855 0.0620655412115194 0.000472366556447804 0.00044762757385855 0.0620655412115194 0.000472366556447804 0.00044762757385855 0.0620655412115194 0.000472366556447804 0.00044762757385855 0.0620655412115194 0.000472366556447804 0.00044762757385855 0.0620655412115194 0.000472366556447804 0.00044762757385855 0.0620655412115194 0.000472366556447804 0.00044762757385855 0.0620655412115194 0.000472366556447804 0.00044762757385855 0.0620655412115194 0.000472366556447804 0.00044762757385855 0.0620655412115194 0.000472366556447804 0.00044762757385855 0.0620655412115194]); +bnet.CPD{i+ss} = tabular_CPD(bnet, i+ss, [0.472366556447804 0.0044762757385855 6.20655412115194e-005 0.472366556447804 0.044762757385855 6.20655412115194e-005 0.0472366556447804 0.44762757385855 0.000620655412115194 0.000472366556447804 0.44762757385855 0.00620655412115194 0.000472366556447804 0.044762757385855 0.0620655412115194 0.000472366556447804 0.0044762757385855 0.0620655412115194 0.000472366556447804 0.00044762757385855 0.0620655412115194 0.000472366556447804 0.00044762757385855 0.0620655412115194 0.000472366556447804 0.00044762757385855 0.0620655412115194 0.000472366556447804 0.00044762757385855 0.0620655412115194 0.000472366556447804 0.00044762757385855 0.0620655412115194 0.000472366556447804 0.00044762757385855 0.0620655412115194 0.000472366556447804 0.00044762757385855 0.0620655412115194 0.000472366556447804 0.00044762757385855 0.0620655412115194 0.000472366556447804 0.00044762757385855 0.0620655412115194 0.000472366556447804 0.00044762757385855 0.0620655412115194 0.000472366556447804 0.00044762757385855 0.0620655412115194 0.000472366556447804 0.00044762757385855 0.0620655412115194 0.000472366556447804 0.00044762757385855 0.0620655412115194 0.000472366556447804 0.00044762757385855 0.0620655412115194]); + +i = stringmatch('BYdotDiffSens', names); +%bnet.CPD{i} = tabular_CPD(bnet, i, [0.53191206429753 0.00806445109313635 0.0053191206429753 9.9930048965724e-005 0.265956032148765 0.00806445109313635 0.0053191206429753 9.9930048965724e-005 0.132978016074383 0.0806445109313634 0.0053191206429753 9.9930048965724e-005 0.053191206429753 0.806445109313635 0.053191206429753 9.9930048965724e-005 0.0053191206429753 0.0806445109313634 0.132978016074383 9.9930048965724e-005 0.0053191206429753 0.00806445109313635 0.265956032148765 9.9930048965724e-005 0.0053191206429753 0.00806445109313635 0.53191206429753 9.9930048965724e-005 5.3191206429753e-006 8.06445109313635e-006 5.3191206429753e-006 0.99930048965724]); +bnet.CPD{i+ss} = tabular_CPD(bnet, i+ss, [0.53191206429753 0.00806445109313635 0.0053191206429753 9.9930048965724e-005 0.265956032148765 0.00806445109313635 0.0053191206429753 9.9930048965724e-005 0.132978016074383 0.0806445109313634 0.0053191206429753 9.9930048965724e-005 0.053191206429753 0.806445109313635 0.053191206429753 9.9930048965724e-005 0.0053191206429753 0.0806445109313634 0.132978016074383 9.9930048965724e-005 0.0053191206429753 0.00806445109313635 0.265956032148765 9.9930048965724e-005 0.0053191206429753 0.00806445109313635 0.53191206429753 9.9930048965724e-005 5.3191206429753e-006 8.06445109313635e-006 5.3191206429753e-006 0.99930048965724]); +%BIF2BNT added a bunch of zeros at the end of this cpd. Hopefully the only occurence of this bug! 0 0 0 0 0 0 0 0]); \ No newline at end of file diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/mk_orig_water_dbn.m b/sourcecodes/bnt-master/BNT/examples/dynamic/mk_orig_water_dbn.m new file mode 100644 index 00000000..67a4e24a --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/mk_orig_water_dbn.m @@ -0,0 +1,121 @@ +function dbn = mk_orig_water_dbn +% Converted by Frank Hutter: +% Provided in Phrog format by Xavier Boyen. +% Manually converted into BIF. +% Converted to BNT from BIF by Web-based bif2bnt (2004-01-30T05:28:10) +% Manually converted into function creating DBN. +% Manually changed the node numbering s.t. A-B-C-D-E-F-G-H from the BK paper correspond to 1-2-3-4-5-6-7-8 + +node = struct('C_NI_12_ANT', 1, ... + 'CKNI_12_ANT', 2, ... + 'CBODD_12_ANT', 3, ... + 'CNOD_12_ANT', 4, ... + 'CBODN_12_ANT', 5, ... + 'CNON_12_ANT', 6, ... + 'CKND_12_ANT', 7, ... + 'CKNN_12_ANT', 8, ... + 'C_NI_12_OBS', 9, ... + 'CKNI_12_OBS', 10, ... + 'CBODD_12_OBS', 11, ... + 'CNOD_12_OBS', 12, ... + 'CBODN_12_OBS', 13, ... + 'CNON_12_OBS', 14, ... + 'CKND_12_OBS', 15, ... + 'CKNN_12_OBS', 16, ... + 'C_NI_12_ULT', 17, ... + 'CKNI_12_ULT', 18, ... + 'CBODD_12_ULT', 19, ... + 'CNOD_12_ULT', 20, ... + 'CBODN_12_ULT', 21, ... + 'CNON_12_ULT', 22, ... + 'CKND_12_ULT', 23, ... + 'CKNN_12_ULT', 24); + +adjacency = zeros(24); +adjacency([node.C_NI_12_ANT], node.C_NI_12_OBS) = 1; +adjacency([node.CKNI_12_ANT], node.CKNI_12_OBS) = 1; +adjacency([node.CBODD_12_ANT], node.CBODD_12_OBS) = 1; +adjacency([node.CKND_12_ANT], node.CKND_12_OBS) = 1; +adjacency([node.CNOD_12_ANT], node.CNOD_12_OBS) = 1; +adjacency([node.CBODN_12_ANT], node.CBODN_12_OBS) = 1; +adjacency([node.CKNN_12_ANT], node.CKNN_12_OBS) = 1; +adjacency([node.CNON_12_ANT], node.CNON_12_OBS) = 1; +adjacency([node.C_NI_12_ANT], node.C_NI_12_ULT) = 1; +adjacency([node.CKNI_12_ANT], node.CKNI_12_ULT) = 1; +adjacency([node.CBODN_12_ANT node.CNOD_12_ANT node.CBODD_12_ANT node.CKNI_12_ANT node.C_NI_12_ANT], node.CBODD_12_ULT) = 1; +adjacency([node.CKNN_12_ANT node.CKND_12_ANT node.CKNI_12_ANT], node.CKND_12_ULT) = 1; +adjacency([node.CNON_12_ANT node.CNOD_12_ANT node.CBODD_12_ANT], node.CNOD_12_ULT) = 1; +adjacency([node.CNON_12_ANT node.CBODN_12_ANT node.CBODD_12_ANT], node.CBODN_12_ULT) = 1; +adjacency([node.CKNN_12_ANT node.CKND_12_ANT], node.CKNN_12_ULT) = 1; +adjacency([node.CNON_12_ANT node.CKNN_12_ANT node.CBODN_12_ANT node.CNOD_12_ANT], node.CNON_12_ULT) = 1; + +ss = 16; +dnodes = 1:ss; +ant = 1:8; +onodes = 9:16; +ult = 17:24; +intra = adjacency(1:ss, 1:ss); +inter_real = adjacency(ant, ult); +inter = zeros(ss); +inter(ant,1:length(ult)) = inter_real; + +eclass1 = 1:16; +eclass2 = [17:24 9:16]; + +value = {{'zz3num'; 'zz4num'; 'zz5num'; 'zz6num'}, ... + {'zz20mgl'; 'zz30mgl'; 'zz40mgl'}, ... + {'zz15mgl'; 'zz20mgl'; 'zz25mgl'; 'zz30mgl'}, ... + {'zz05mgl'; 'zz1mgl'; 'zz2mgl'; 'zz4mgl'}, ... + {'zz5mgl'; 'zz10mgl'; 'zz15mgl'; 'zz20mgl'}, ... + {'zz2mgl'; 'zz4mgl'; 'zz6mgl'; 'zz10mgl'}, ... + {'zz2mgl'; 'zz4mgl'; 'zz6mgl'}, ... + {'zz05mgl'; 'zz1mgl'; 'zz2mgl'}, ... + {'zz3num'; 'zz4num'; 'zz5num'; 'zz6num'}, ... + {'zz20mgl'; 'zz30mgl'; 'zz40mgl'}, ... + {'zz15mgl'; 'zz20mgl'; 'zz25mgl'; 'zz30mgl'}, ... + {'zz05mgl'; 'zz1mgl'; 'zz2mgl'; 'zz4mgl'}, ... + {'zz5mgl'; 'zz10mgl'; 'zz15mgl'; 'zz20mgl'}, ... + {'zz2mgl'; 'zz4mgl'; 'zz6mgl'; 'zz10mgl'}, ... + {'zz2mgl'; 'zz4mgl'; 'zz6mgl'}, ... + {'zz05mgl'; 'zz1mgl'; 'zz2mgl'}, ... + {'zz3num'; 'zz4num'; 'zz5num'; 'zz6num'}, ... + {'zz20mgl'; 'zz30mgl'; 'zz40mgl'}, ... + {'zz15mgl'; 'zz20mgl'; 'zz25mgl'; 'zz30mgl'}, ... + {'zz05mgl'; 'zz1mgl'; 'zz2mgl'; 'zz4mgl'}, ... + {'zz5mgl'; 'zz10mgl'; 'zz15mgl'; 'zz20mgl'}, ... + {'zz2mgl'; 'zz4mgl'; 'zz6mgl'; 'zz10mgl'}, ... + {'zz2mgl'; 'zz4mgl'; 'zz6mgl'}, ... + {'zz05mgl'; 'zz1mgl'; 'zz2mgl'}}; + +ns = zeros(1,24); +for i=1:24 + ns(i) = length(value{i}); +end + +dbn = mk_dbn(intra, inter, ns, 'discrete', dnodes, 'eclass1', eclass1, 'eclass2', eclass2, ... + 'observed', onodes); + +dbn.CPD{node.C_NI_12_ANT} = tabular_CPD(dbn, node.C_NI_12_ANT, 1/ns(1) * ones(1,ns(1))); +dbn.CPD{node.CKNI_12_ANT} = tabular_CPD(dbn, node.CKNI_12_ANT, 1/ns(2) * ones(1,ns(2))); +dbn.CPD{node.CBODD_12_ANT} = tabular_CPD(dbn, node.CBODD_12_ANT, 1/ns(3) * ones(1,ns(3))); +dbn.CPD{node.CNOD_12_ANT} = tabular_CPD(dbn, node.CNOD_12_ANT, 1/ns(4) * ones(1,ns(4))); +dbn.CPD{node.CBODN_12_ANT} = tabular_CPD(dbn, node.CBODN_12_ANT, 1/ns(5) * ones(1,ns(5))); +dbn.CPD{node.CNON_12_ANT} = tabular_CPD(dbn, node.CNON_12_ANT, 1/ns(6) * ones(1,ns(6))); +dbn.CPD{node.CKND_12_ANT} = tabular_CPD(dbn, node.CKND_12_ANT, 1/ns(7) * ones(1,ns(7))); +dbn.CPD{node.CKNN_12_ANT} = tabular_CPD(dbn, node.CKNN_12_ANT, 1/ns(8) * ones(1,ns(8))); +dbn.CPD{node.C_NI_12_OBS} = tabular_CPD(dbn, node.C_NI_12_OBS, [0.7 0.1 0.1 0.1 0.1 0.7 0.1 0.1 0.1 0.1 0.7 0.1 0.1 0.1 0.1 0.7]); +dbn.CPD{node.CKNI_12_OBS} = tabular_CPD(dbn, node.CKNI_12_OBS, [0.8 0.1 0.1 0.1 0.8 0.1 0.1 0.1 0.8]); +dbn.CPD{node.CBODD_12_OBS} = tabular_CPD(dbn, node.CBODD_12_OBS, [0.7 0.1 0.1 0.1 0.1 0.7 0.1 0.1 0.1 0.1 0.7 0.1 0.1 0.1 0.1 0.7]); +dbn.CPD{node.CKND_12_OBS} = tabular_CPD(dbn, node.CKND_12_OBS, [0.8 0.1 0.1 0.1 0.8 0.1 0.1 0.1 0.8]); +dbn.CPD{node.CNOD_12_OBS} = tabular_CPD(dbn, node.CNOD_12_OBS, [0.7 0.1 0.1 0.1 0.1 0.7 0.1 0.1 0.1 0.1 0.7 0.1 0.1 0.1 0.1 0.7]); +dbn.CPD{node.CBODN_12_OBS} = tabular_CPD(dbn, node.CBODN_12_OBS, [0.7 0.1 0.1 0.1 0.1 0.7 0.1 0.1 0.1 0.1 0.7 0.1 0.1 0.1 0.1 0.7]); +dbn.CPD{node.CKNN_12_OBS} = tabular_CPD(dbn, node.CKNN_12_OBS, [0.8 0.1 0.1 0.1 0.8 0.1 0.1 0.1 0.8]); +dbn.CPD{node.CNON_12_OBS} = tabular_CPD(dbn, node.CNON_12_OBS, [0.7 0.1 0.1 0.1 0.1 0.7 0.1 0.1 0.1 0.1 0.7 0.1 0.1 0.1 0.1 0.7]); +dbn.CPD{node.C_NI_12_ULT} = tabular_CPD(dbn, node.C_NI_12_ULT, [0.5 0.2 0.1 0 0.4 0.55 0.3 0.15 0.1 0.2 0.5 0.25 0 0.05 0.1 0.6]); +dbn.CPD{node.CKNI_12_ULT} = tabular_CPD(dbn, node.CKNI_12_ULT, [0.48 0.2 0.04 0.48 0.6 0.48 0.04 0.2 0.48]); +dbn.CPD{node.CBODD_12_ULT} = tabular_CPD(dbn, node.CBODD_12_ULT, [1 1 0.9791 0.9473 0.9949 0.9473 0.8997 0.8521 0.9473 0.8838 0.8203 0.7568 0.0903 0.0585 0.0268 0 0.0426 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0.9951 0.9634 1 0.9634 0.9158 0.8681 0.9634 0.8999 0.8364 0.7729 0.109 0.0773 0.0455 0.0138 0.0614 0.0138 0 0 0.0138 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0.9762 1 0.9762 0.9286 0.881 0.9762 0.9127 0.8493 0.7858 0.124 0.0923 0.0605 0.0288 0.0764 0.0288 0 0 0.0288 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0.9848 1 0.9848 0.9372 0.8896 0.9848 0.9213 0.8578 0.7943 0.134 0.1023 0.0705 0.0388 0.0864 0.0388 0 0 0.0388 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0.9791 0.9473 0.9156 0.9632 0.9156 0.8679 0.8203 0.9156 0.8521 0.7886 0.7251 0.0585 0.0268 0 0 0.0109 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0.9951 0.9634 0.9316 0.9793 0.9316 0.884 0.8364 0.9316 0.8681 0.8046 0.7412 0.0773 0.0455 0.0138 0 0.0296 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0.9762 0.9445 0.9921 0.9445 0.8969 0.8493 0.9445 0.881 0.8175 0.754 0.0923 0.0605 0.0288 0 0.0446 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0.9848 0.9531 1 0.9531 0.9054 0.8578 0.9531 0.8896 0.8261 0.7626 0.1023 0.0705 0.0388 0.007 0.0546 0.007 0 0 0.007 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.9791 0.9473 0.9156 0.8838 0.9314 0.8838 0.8362 0.7886 0.8838 0.8203 0.7568 0.6933 0.0268 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.9951 0.9634 0.9316 0.8999 0.9475 0.8999 0.8523 0.8046 0.8999 0.8364 0.7729 0.7094 0.0455 0.0138 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0.9762 0.9445 0.9127 0.9604 0.9127 0.8651 0.8175 0.9127 0.8493 0.7858 0.7223 0.0605 0.0288 0 0 0.0129 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0.9848 0.9531 0.9213 0.9689 0.9213 0.8737 0.8261 0.9213 0.8578 0.7943 0.7308 0.0705 0.0388 0.007 0 0.0229 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.9473 0.9156 0.8838 0.8521 0.8997 0.8521 0.8045 0.7568 0.8521 0.7886 0.7251 0.6616 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.9634 0.9316 0.8999 0.8681 0.9158 0.8681 0.8205 0.7729 0.8681 0.8046 0.7412 0.6777 0.0138 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.9762 0.9445 0.9127 0.881 0.9286 0.881 0.8334 0.7858 0.881 0.8175 0.754 0.6905 0.0288 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.9848 0.9531 0.9213 0.8896 0.9372 0.8896 0.842 0.7943 0.8896 0.8261 0.7626 0.6991 0.0388 0.007 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0209 0.0527 0.0051 0.0527 0.1003 0.1479 0.0527 0.1162 0.1797 0.2432 0.9097 0.9415 0.9732 0.995 0.9574 0.995 0.9474 0.8998 0.995 0.9315 0.868 0.8045 0.1362 0.1045 0.0727 0.041 0.0886 0.041 0 0 0.041 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0049 0.0366 0 0.0366 0.0842 0.1319 0.0366 0.1001 0.1636 0.2271 0.891 0.9227 0.9545 0.9862 0.9386 0.9862 0.9662 0.9185 0.9862 0.9503 0.8868 0.8233 0.157 0.1253 0.0935 0.0618 0.1094 0.0618 0.0142 0 0.0618 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0238 0 0.0238 0.0714 0.119 0.0238 0.0873 0.1507 0.2142 0.876 0.9077 0.9395 0.9712 0.9236 0.9712 0.9812 0.9335 0.9712 0.9653 0.9018 0.8383 0.1737 0.142 0.1102 0.0785 0.1261 0.0785 0.0308 0 0.0785 0.015 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0152 0 0.0152 0.0628 0.1104 0.0152 0.0787 0.1422 0.2057 0.866 0.8977 0.9295 0.9612 0.9136 0.9612 0.9912 0.9435 0.9612 0.9753 0.9118 0.8483 0.1848 0.1531 0.1213 0.0896 0.1372 0.0896 0.042 0 0.0896 0.0261 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0209 0.0527 0.0844 0.0368 0.0844 0.1321 0.1797 0.0844 0.1479 0.2114 0.2749 0.9415 0.9732 0.995 0.9633 0.9891 0.9633 0.9157 0.868 0.9633 0.8998 0.8363 0.7728 0.1045 0.0727 0.041 0.0092 0.0568 0.0092 0 0 0.0092 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0049 0.0366 0.0684 0.0207 0.0684 0.116 0.1636 0.0684 0.1319 0.1954 0.2588 0.9227 0.9545 0.9862 0.982 0.9704 0.982 0.9344 0.8868 0.982 0.9185 0.855 0.7916 0.1253 0.0935 0.0618 0.03 0.0777 0.03 0 0 0.03 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0238 0.0555 0.0079 0.0555 0.1031 0.1507 0.0555 0.119 0.1825 0.246 0.9077 0.9395 0.9712 0.997 0.9554 0.997 0.9494 0.9018 0.997 0.9335 0.87 0.8066 0.142 0.1102 0.0785 0.0467 0.0943 0.0467 0 0 0.0467 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0152 0.0469 0 0.0469 0.0946 0.1422 0.0469 0.1104 0.1739 0.2374 0.8977 0.9295 0.9612 0.993 0.9454 0.993 0.9594 0.9118 0.993 0.9435 0.88 0.8166 0.1531 0.1213 0.0896 0.0578 0.1054 0.0578 0.0102 0 0.0578 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0209 0.0527 0.0844 0.1162 0.0686 0.1162 0.1638 0.2114 0.1162 0.1797 0.2432 0.3067 0.9732 0.995 0.9633 0.9315 0.9792 0.9315 0.8839 0.8363 0.9315 0.868 0.8045 0.7411 0.0727 0.041 0.0092 0 0.0251 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0049 0.0366 0.0684 0.1001 0.0525 0.1001 0.1477 0.1954 0.1001 0.1636 0.2271 0.2906 0.9545 0.9862 0.982 0.9503 0.9979 0.9503 0.9027 0.855 0.9503 0.8868 0.8233 0.7598 0.0935 0.0618 0.03 0 0.0459 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0238 0.0555 0.0873 0.0396 0.0873 0.1349 0.1825 0.0873 0.1507 0.2142 0.2777 0.9395 0.9712 0.997 0.9653 0.9871 0.9653 0.9177 0.87 0.9653 0.9018 0.8383 0.7748 0.1102 0.0785 0.0467 0.015 0.0626 0.015 0 0 0.015 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0152 0.0469 0.0787 0.0311 0.0787 0.1263 0.1739 0.0787 0.1422 0.2057 0.2692 0.9295 0.9612 0.993 0.9753 0.9771 0.9753 0.9277 0.88 0.9753 0.9118 0.8483 0.7848 0.1213 0.0896 0.0578 0.0261 0.0737 0.0261 0 0 0.0261 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0527 0.0844 0.1162 0.1479 0.1003 0.1479 0.1955 0.2432 0.1479 0.2114 0.2749 0.3384 0.995 0.9633 0.9315 0.8998 0.9474 0.8998 0.8522 0.8045 0.8998 0.8363 0.7728 0.7093 0.041 0.0092 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0366 0.0684 0.1001 0.1319 0.0842 0.1319 0.1795 0.2271 0.1319 0.1954 0.2588 0.3223 0.9862 0.982 0.9503 0.9185 0.9662 0.9185 0.8709 0.8233 0.9185 0.855 0.7916 0.7281 0.0618 0.03 0 0 0.0142 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0238 0.0555 0.0873 0.119 0.0714 0.119 0.1666 0.2142 0.119 0.1825 0.246 0.3095 0.9712 0.997 0.9653 0.9335 0.9812 0.9335 0.8859 0.8383 0.9335 0.87 0.8066 0.7431 0.0785 0.0467 0.015 0 0.0308 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0152 0.0469 0.0787 0.1104 0.0628 0.1104 0.158 0.2057 0.1104 0.1739 0.2374 0.3009 0.9612 0.993 0.9753 0.9435 0.9912 0.9435 0.8959 0.8483 0.9435 0.88 0.8166 0.7531 0.0896 0.0578 0.0261 0 0.042 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.005 0 0.005 0.0526 0.1002 0.005 0.0685 0.132 0.1955 0.8638 0.8955 0.9273 0.959 0.9114 0.959 0.9933 0.9457 0.959 0.9775 0.914 0.8505 0.1809 0.1491 0.1174 0.0856 0.1333 0.0856 0.038 0 0.0856 0.0221 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0338 0.0815 0 0.0497 0.1132 0.1767 0.843 0.8747 0.9065 0.9382 0.8906 0.9382 0.9858 0.9666 0.9382 0.9983 0.9348 0.8713 0.2034 0.1716 0.1399 0.1081 0.1558 0.1081 0.0605 0.0129 0.1081 0.0446 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0188 0.0665 0 0.0347 0.0982 0.1617 0.8263 0.858 0.8898 0.9215 0.8739 0.9215 0.9692 0.9832 0.9215 0.985 0.9515 0.888 0.2214 0.1896 0.1579 0.1261 0.1738 0.1261 0.0785 0.0309 0.1261 0.0626 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0088 0.0565 0 0.0247 0.0882 0.1517 0.8152 0.8469 0.8787 0.9104 0.8628 0.9104 0.958 0.9943 0.9104 0.9739 0.9626 0.8991 0.2334 0.2016 0.1699 0.1381 0.1858 0.1381 0.0905 0.0429 0.1381 0.0746 0.0112 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.005 0.0367 0 0.0367 0.0843 0.132 0.0367 0.1002 0.1637 0.2272 0.8955 0.9273 0.959 0.9908 0.9432 0.9908 0.9616 0.914 0.9908 0.9457 0.8822 0.8187 0.1491 0.1174 0.0856 0.0539 0.1015 0.0539 0.0063 0 0.0539 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.018 0 0.018 0.0656 0.1132 0.018 0.0815 0.145 0.2084 0.8747 0.9065 0.9382 0.97 0.9223 0.97 0.9824 0.9348 0.97 0.9666 0.9031 0.8396 0.1716 0.1399 0.1081 0.0764 0.124 0.0764 0.0288 0 0.0764 0.0129 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.003 0 0.003 0.0506 0.0982 0.003 0.0665 0.13 0.1934 0.858 0.8898 0.9215 0.9533 0.9057 0.9533 0.9991 0.9515 0.9533 0.9832 0.9197 0.8562 0.1896 0.1579 0.1261 0.0944 0.142 0.0944 0.0468 0 0.0944 0.0309 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0406 0.0882 0 0.0565 0.12 0.1834 0.8469 0.8787 0.9104 0.9422 0.8946 0.9422 0.9898 0.9626 0.9422 0.9943 0.9308 0.8673 0.2016 0.1699 0.1381 0.1064 0.154 0.1064 0.0588 0.0112 0.1064 0.0429 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.005 0.0367 0.0685 0.0208 0.0685 0.1161 0.1637 0.0685 0.132 0.1955 0.2589 0.9273 0.959 0.9908 0.9775 0.9749 0.9775 0.9298 0.8822 0.9775 0.914 0.8505 0.787 0.1174 0.0856 0.0539 0.0221 0.0698 0.0221 0 0 0.0221 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.018 0.0497 0.0021 0.0497 0.0973 0.145 0.0497 0.1132 0.1767 0.2402 0.9065 0.9382 0.97 0.9983 0.9541 0.9983 0.9507 0.9031 0.9983 0.9348 0.8713 0.8078 0.1399 0.1081 0.0764 0.0446 0.0923 0.0446 0 0 0.0446 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.003 0.0347 0 0.0347 0.0823 0.13 0.0347 0.0982 0.1617 0.2252 0.8898 0.9215 0.9533 0.985 0.9374 0.985 0.9673 0.9197 0.985 0.9515 0.888 0.8245 0.1579 0.1261 0.0944 0.0626 0.1103 0.0626 0.015 0 0.0626 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0247 0 0.0247 0.0723 0.12 0.0247 0.0882 0.1517 0.2152 0.8787 0.9104 0.9422 0.9739 0.9263 0.9739 0.9785 0.9308 0.9739 0.9626 0.8991 0.8356 0.1699 0.1381 0.1064 0.0746 0.1223 0.0746 0.027 0 0.0746 0.0112 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.005 0.0367 0.0685 0.1002 0.0526 0.1002 0.1478 0.1955 0.1002 0.1637 0.2272 0.2907 0.959 0.9908 0.9775 0.9457 0.9933 0.9457 0.8981 0.8505 0.9457 0.8822 0.8187 0.7552 0.0856 0.0539 0.0221 0 0.038 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.018 0.0497 0.0815 0.0338 0.0815 0.1291 0.1767 0.0815 0.145 0.2084 0.2719 0.9382 0.97 0.9983 0.9666 0.9858 0.9666 0.9189 0.8713 0.9666 0.9031 0.8396 0.7761 0.1081 0.0764 0.0446 0.0129 0.0605 0.0129 0 0 0.0129 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.003 0.0347 0.0665 0.0188 0.0665 0.1141 0.1617 0.0665 0.13 0.1934 0.2569 0.9215 0.9533 0.985 0.9832 0.9692 0.9832 0.9356 0.888 0.9832 0.9197 0.8562 0.7927 0.1261 0.0944 0.0626 0.0309 0.0785 0.0309 0 0 0.0309 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0247 0.0565 0.0088 0.0565 0.1041 0.1517 0.0565 0.12 0.1834 0.2469 0.9104 0.9422 0.9739 0.9943 0.958 0.9943 0.9467 0.8991 0.9943 0.9308 0.8673 0.8039 0.1381 0.1064 0.0746 0.0429 0.0905 0.0429 0 0 0.0429 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0067 0.0543 0 0.0225 0.086 0.1495 0.8191 0.8509 0.8826 0.9144 0.8667 0.9144 0.962 1 0.9144 0.9779 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0334 0 0.0017 0.0652 0.1287 0.7966 0.8284 0.8601 0.8919 0.8442 0.8919 0.9395 0.9871 0.8919 0.9554 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0168 0 0 0.0485 0.112 0.7786 0.8104 0.8421 0.8739 0.8262 0.8739 0.9215 0.9691 0.8739 0.9374 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0057 0 0 0.0374 0.1009 0.7666 0.7984 0.8301 0.8619 0.8142 0.8619 0.9095 0.9571 0.8619 0.9254 0.9888 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0384 0.086 0 0.0543 0.1178 0.1813 0.8509 0.8826 0.9144 0.9461 0.8985 0.9461 0.9937 1 0.9461 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0176 0.0652 0 0.0334 0.0969 0.1604 0.8284 0.8601 0.8919 0.9236 0.876 0.9236 0.9712 1 0.9236 0.9871 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0009 0.0485 0 0.0168 0.0803 0.1438 0.8104 0.8421 0.8739 0.9056 0.858 0.9056 0.9532 1 0.9056 0.9691 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0374 0 0.0057 0.0692 0.1327 0.7984 0.8301 0.8619 0.8936 0.846 0.8936 0.9412 0.9888 0.8936 0.9571 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0225 0 0.0225 0.0702 0.1178 0.0225 0.086 0.1495 0.213 0.8826 0.9144 0.9461 0.9779 0.9302 0.9779 1 1 0.9779 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0017 0 0.0017 0.0493 0.0969 0.0017 0.0652 0.1287 0.1922 0.8601 0.8919 0.9236 0.9554 0.9077 0.9554 1 1 0.9554 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0327 0.0803 0 0.0485 0.112 0.1755 0.8421 0.8739 0.9056 0.9374 0.8897 0.9374 0.985 1 0.9374 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0215 0.0692 0 0.0374 0.1009 0.1644 0.8301 0.8619 0.8936 0.9254 0.8777 0.9254 0.973 1 0.9254 0.9888 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0225 0.0543 0.0067 0.0543 0.1019 0.1495 0.0543 0.1178 0.1813 0.2448 0.9144 0.9461 0.9779 1 0.962 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0017 0.0334 0 0.0334 0.0811 0.1287 0.0334 0.0969 0.1604 0.2239 0.8919 0.9236 0.9554 0.9871 0.9395 0.9871 1 1 0.9871 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0168 0 0.0168 0.0644 0.112 0.0168 0.0803 0.1438 0.2073 0.8739 0.9056 0.9374 0.9691 0.9215 0.9691 1 1 0.9691 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0057 0 0.0057 0.0533 0.1009 0.0057 0.0692 0.1327 0.1961 0.8619 0.8936 0.9254 0.9571 0.9095 0.9571 1 1 0.9571 1 1 1]); +dbn.CPD{node.CKND_12_ULT} = tabular_CPD(dbn, node.CKND_12_ULT, [0.9524 0.9127 0.873 0 0 0 0 0 0 0.9444 0.9048 0.8651 0 0 0 0 0 0 0.9286 0.8889 0.8492 0 0 0 0 0 0 0.0476 0.0873 0.127 0.9921 0.9524 0.9127 0.0317 0 0 0.0556 0.0952 0.1349 0.9841 0.9444 0.9048 0.0238 0 0 0.0714 0.1111 0.1508 0.9683 0.9286 0.8889 0.0079 0 0 0 0 0 0.0079 0.0476 0.0873 0.9683 1 1 0 0 0 0.0159 0.0556 0.0952 0.9762 1 1 0 0 0 0.0317 0.0714 0.1111 0.9921 1 1]); +dbn.CPD{node.CNOD_12_ULT} = tabular_CPD(dbn, node.CNOD_12_ULT, [1 1 1 1 0.3675 0.4905 0.5862 0.6627 0 0 0 0 0 0 0 0 1 1 1 1 0.2405 0.3635 0.4592 0.5358 0 0 0 0 0 0 0 0 0.8893 0.9816 1 1 0.1135 0.2366 0.3322 0.4088 0 0 0 0 0 0 0 0 0.6354 0.7276 0.7994 0.8568 0 0 0.0783 0.1548 0 0 0 0 0 0 0 0 0 0 0 0 0.6325 0.5095 0.4138 0.3373 0.2972 0.3711 0.4285 0.4744 0 0 0 0 0 0 0 0 0.7595 0.6365 0.5408 0.4642 0.2338 0.3076 0.365 0.4109 0 0 0 0 0.1107 0.0184 0 0 0.8865 0.7634 0.6678 0.5912 0.1703 0.2441 0.3015 0.3474 0 0 0 0 0.3646 0.2724 0.2006 0.1432 0.9298 0.9913 0.9217 0.8452 0.0433 0.1171 0.1745 0.2204 0 0 0 0 0 0 0 0 0 0 0 0 0.7028 0.6289 0.5715 0.5256 0.2129 0.2539 0.2858 0.3113 0 0 0 0 0 0 0 0 0.7662 0.6924 0.635 0.5891 0.1812 0.2222 0.2541 0.2796 0 0 0 0 0 0 0 0 0.8297 0.7559 0.6985 0.6526 0.1494 0.1904 0.2223 0.2478 0 0 0 0 0.0702 0.0087 0 0 0.9567 0.8829 0.8255 0.7796 0.0859 0.1269 0.1588 0.1843 0 0 0 0 0 0 0 0 0 0 0 0 0.7871 0.7461 0.7142 0.6887 0 0 0 0 0 0 0 0 0 0 0 0 0.8188 0.7778 0.7459 0.7204 0 0 0 0 0 0 0 0 0 0 0 0 0.8506 0.8096 0.7777 0.7522 0 0 0 0 0 0 0 0 0 0 0 0 0.9141 0.8731 0.8412 0.8157]); +dbn.CPD{node.CBODN_12_ULT} = tabular_CPD(dbn, node.CBODN_12_ULT, [0.9557 0.9067 0.8577 0.8087 0.0406 0 0 0 0 0 0 0 0 0 0 0 0.9561 0.9071 0.8581 0.809 0.0412 0 0 0 0 0 0 0 0 0 0 0 0.9562 0.9072 0.8582 0.8092 0.0414 0 0 0 0 0 0 0 0 0 0 0 0.9564 0.9073 0.8583 0.8093 0.0416 0 0 0 0 0 0 0 0 0 0 0 0.0443 0.0933 0.1423 0.1913 0.9594 0.9916 0.9426 0.8936 0.1152 0.0662 0.0172 0 0 0 0 0 0.0439 0.0929 0.1419 0.191 0.9588 0.9922 0.9432 0.8942 0.116 0.067 0.018 0 0 0 0 0 0.0438 0.0928 0.1418 0.1908 0.9586 0.9924 0.9434 0.8944 0.1163 0.0673 0.0183 0 0 0 0 0 0.0436 0.0927 0.1417 0.1907 0.9584 0.9926 0.9436 0.8946 0.1166 0.0676 0.0185 0 0 0 0 0 0 0 0 0 0 0.0084 0.0574 0.1064 0.8848 0.9338 0.9828 0.9682 0.1835 0.1344 0.0854 0.0364 0 0 0 0 0 0.0078 0.0568 0.1058 0.884 0.933 0.982 0.969 0.1844 0.1354 0.0863 0.0373 0 0 0 0 0 0.0076 0.0566 0.1056 0.8837 0.9327 0.9817 0.9693 0.1847 0.1357 0.0867 0.0377 0 0 0 0 0 0.0074 0.0564 0.1054 0.8834 0.9324 0.9815 0.9695 0.185 0.136 0.087 0.038 0 0 0 0 0 0 0 0 0 0 0 0.0318 0.8165 0.8656 0.9146 0.9636 0 0 0 0 0 0 0 0 0 0 0 0.031 0.8156 0.8646 0.9137 0.9627 0 0 0 0 0 0 0 0 0 0 0 0.0307 0.8153 0.8643 0.9133 0.9623 0 0 0 0 0 0 0 0 0 0 0 0.0305 0.815 0.864 0.913 0.962]); +dbn.CPD{node.CKNN_12_ULT} = tabular_CPD(dbn, node.CKNN_12_ULT, [1 1 0.8234 0.4459 0.2499 0.0538 0 0 0 0 0 0.1766 0.5541 0.7501 0.9462 0.3627 0.2646 0.1666 0 0 0 0 0 0 0.6373 0.7354 0.8334]); +dbn.CPD{node.CNON_12_ULT} = tabular_CPD(dbn, node.CNON_12_ULT, [0.9555 0.9432 0.9187 0.8697 0.9618 0.9495 0.925 0.876 0.9662 0.954 0.9295 0.8804 0.9696 0.9573 0.9328 0.8838 0.9102 0.8979 0.8734 0.8244 0.9164 0.9042 0.8797 0.8306 0.9209 0.9086 0.8841 0.8351 0.9243 0.912 0.8875 0.8385 0.8648 0.8526 0.8281 0.779 0.8711 0.8588 0.8343 0.7853 0.8756 0.8633 0.8388 0.7898 0.8789 0.8667 0.8422 0.7931 0.0056 0 0 0 0.0125 0.0003 0 0 0.0175 0.0052 0 0 0.0212 0.009 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0445 0.0568 0.0813 0.1303 0.0382 0.0505 0.075 0.124 0.0338 0.046 0.0705 0.1196 0.0304 0.0427 0.0672 0.1162 0.0898 0.1021 0.1266 0.1756 0.0836 0.0958 0.1203 0.1694 0.0791 0.0914 0.1159 0.1649 0.0757 0.088 0.1125 0.1615 0.1352 0.1474 0.1719 0.221 0.1289 0.1412 0.1657 0.2147 0.1244 0.1367 0.1612 0.2102 0.1211 0.1333 0.1578 0.2069 0.9944 0.9933 0.9688 0.9198 0.9875 0.9997 0.9758 0.9267 0.9825 0.9948 0.9807 0.9317 0.9788 0.991 0.9845 0.9354 0.9602 0.948 0.9235 0.8744 0.9672 0.9549 0.9304 0.8814 0.9722 0.9599 0.9354 0.8864 0.9759 0.9636 0.9391 0.8901 0.9149 0.9026 0.8781 0.8291 0.9219 0.9096 0.8851 0.8361 0.9268 0.9146 0.8901 0.841 0.9306 0.9183 0.8938 0.8448 0.055 0.0427 0.0182 0 0.0622 0.05 0.0254 0 0.0674 0.0551 0.0306 0 0.0712 0.059 0.0345 0 0.0096 0 0 0 0.0169 0.0046 0 0 0.022 0.0098 0 0 0.0259 0.0137 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0067 0.0312 0.0802 0 0 0.0242 0.0733 0 0 0.0193 0.0683 0 0 0.0155 0.0646 0.0398 0.052 0.0765 0.1256 0.0328 0.0451 0.0696 0.1186 0.0278 0.0401 0.0646 0.1136 0.0241 0.0364 0.0609 0.1099 0.0851 0.0974 0.1219 0.1709 0.0781 0.0904 0.1149 0.1639 0.0732 0.0854 0.1099 0.159 0.0694 0.0817 0.1062 0.1552 0.945 0.9573 0.9818 0.9846 0.9378 0.95 0.9746 0.9882 0.9326 0.9449 0.9694 0.9908 0.9288 0.941 0.9655 0.9927 0.9904 0.9987 0.9864 0.9619 0.9831 0.9954 0.9901 0.9655 0.978 0.9902 0.9926 0.9681 0.9741 0.9863 0.9946 0.9701 0.9822 0.976 0.9638 0.9393 0.9858 0.9796 0.9674 0.9429 0.9884 0.9822 0.97 0.9455 0.9903 0.9842 0.9719 0.9474 0.0767 0.0706 0.0583 0.0338 0.0804 0.0743 0.062 0.0375 0.0831 0.0769 0.0647 0.0402 0.0851 0.0789 0.0667 0.0422 0.054 0.0479 0.0356 0.0111 0.0577 0.0516 0.0394 0.0149 0.0604 0.0543 0.042 0.0175 0.0624 0.0563 0.044 0.0195 0.0313 0.0252 0.013 0 0.0351 0.0289 0.0167 0 0.0377 0.0316 0.0194 0 0.0397 0.0336 0.0214 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0154 0 0 0 0.0118 0 0 0 0.0092 0 0 0 0.0073 0 0.0013 0.0136 0.0381 0 0 0.0099 0.0345 0 0 0.0074 0.0319 0 0 0.0054 0.0299 0.0178 0.024 0.0362 0.0607 0.0142 0.0204 0.0326 0.0571 0.0116 0.0178 0.03 0.0545 0.0097 0.0158 0.0281 0.0526 0.9233 0.9294 0.9417 0.9662 0.9196 0.9257 0.938 0.9625 0.9169 0.9231 0.9353 0.9598 0.9149 0.9211 0.9333 0.9578 0.946 0.9521 0.9644 0.9889 0.9423 0.9484 0.9606 0.9851 0.9396 0.9457 0.958 0.9825 0.9376 0.9437 0.956 0.9805 0.9687 0.9748 0.987 1 0.9649 0.9711 0.9833 1 0.9623 0.9684 0.9806 1 0.9603 0.9664 0.9786 1]); \ No newline at end of file diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/mk_ps_from_clqs.m b/sourcecodes/bnt-master/BNT/examples/dynamic/mk_ps_from_clqs.m new file mode 100644 index 00000000..6e469446 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/mk_ps_from_clqs.m @@ -0,0 +1,44 @@ +function mk_ps_from_clqs(dbn, T, cliques, dir) + +% Draw multiple copies of the DBN, +% and indicate the nodes in each clique by shading the nodes. +% Generate a series of color postscript files, +% or, if dir=[], displays them to the screen and pauses. + +if isempty(dir) + print_to_file = 0; +else + print_to_file = 1; +end + +if print_to_file, cd(dir), end +flip = 1; +clf; +[dummyx, dummyy, h] = draw_dbn(dbn.intra, dbn.inter, flip, T, -1); + +C = length(cliques); + +% nodes = []; +% for i=1:C +% cl = cliques{i}; +% nodes = [nodes cl(:)']; +% end +%nodes = unique(nodes); +ss = length(dbn.intra); +nodes = 1:(ss*T); + +for c=1:C + for i=cliques{c} + set(h(i,2), 'facecolor', 'r'); + end + rest = mysetdiff(nodes, cliques{c}); + for i=rest + set(h(i,2), 'facecolor', 'w'); + end + if print_to_file + print(gcf, '-depsc', sprintf('clq%d.ps', c)) + else + disp(['clique ' num2str(c) ' = ' num2str(cliques{c}) '; hit key for next']) + pause + end +end diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/mk_uffe_dbn.m b/sourcecodes/bnt-master/BNT/examples/dynamic/mk_uffe_dbn.m new file mode 100644 index 00000000..2e881dc1 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/mk_uffe_dbn.m @@ -0,0 +1,14 @@ +function bnet = mk_uffe_dbn() + +% Make the Uffe DBN from fig 3.4 p55 of my thesis + +ss = 4; +intra = zeros(ss,ss); +intra(1,[2 3])=1; +intra(2,3)=1; +intra(3,4)=1; +inter = zeros(ss,ss); +inter(1,1)=1; +inter(4,4)=1; +ns = 2*ones(1,ss); +bnet = mk_dbn(intra, inter, ns); diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/mk_water_dbn.m b/sourcecodes/bnt-master/BNT/examples/dynamic/mk_water_dbn.m new file mode 100644 index 00000000..38cc5124 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/mk_water_dbn.m @@ -0,0 +1,68 @@ +function bnet = mk_water_dbn(discrete_obs, obs_leaves) +% MK_WATER_DBN +% bnet = mk_water_dbn(discrete_obs, obs_leaves) +% +% If discrete_obs = 1 (default), the leaves are binary, else scalar Gaussians +% If obs_leaves = 1, all the leaves are observed, otherwise rnd nodes are observed +% +% This is a model of the biological processes of a water purification plant, developed +% by Finn V. Jensen, Uffe Kjærulff, Kristian G. Olesen, and Jan Pedersen. +% See http://www-nt.cs.berkeley.edu/home/nir/public_html/Repository/water.htm +% See also Boyen and Koller, "Tractable Inference for Complex Stochastic Processes", UAI98 + +if nargin < 1, discrete_obs = 1; end +if nargin < 1, obs_leaves = 1; end + +ss = 12; +intra = zeros(ss); +intra(1,9) = 1; +intra(3,10) = 1; +intra(4,11) = 1; +intra(8,12) = 1; + +inter = zeros(ss); +inter(1, [1 3]) = 1; +inter(2, [2 3 7]) = 1; +inter(3, [3 4 5]) = 1; +inter(4, [3 4 6]) = 1; +inter(5, [3 5 6]) = 1; +inter(6, [4 5 6]) = 1; +inter(7, [7 8]) = 1; +inter(8, [6 7 8]) = 1; + +if obs_leaves + onodes = 9:12; % leaves +else + onodes = [1 5 9:12]; % throw in some other nodes +end +hnodes = 1:8; +if discrete_obs + ns = 2*ones(1 ,ss); + dnodes = 1:ss; +else + ns = [2*ones(1,length(hnodes)) 1*ones(length(onodes))]; + dnodes = hnodes; +end + +eclass1 = 1:12; +eclass2 = [13:20 9:12]; +bnet = mk_dbn(intra, inter, ns, 'discrete', dnodes, 'eclass1', eclass1, 'eclass2', eclass2, ... + 'observed', onodes); +if discrete_obs + for i=1:max(eclass2) + bnet.CPD{i} = tabular_CPD(bnet, i); + end +else + for i=hnodes(:)' + bnet.CPD{i} = tabular_CPD(bnet, i); + end + for i=onodes(:)' + bnet.CPD{i} = gaussian_CPD(bnet, i); + end + for i=hnodes(:)'+ss + bnet.CPD{i} = tabular_CPD(bnet, i); + end +end + + + diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/orig_water1.m b/sourcecodes/bnt-master/BNT/examples/dynamic/orig_water1.m new file mode 100644 index 00000000..7daa533c --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/orig_water1.m @@ -0,0 +1,28 @@ +% Compare the speeds of various inference engines on the water DBN +seed = 0; +rand('state', seed); +randn('state', seed); + +%bnet = mk_water_dbn; +bnet = mk_orig_water_dbn; + +T = 3; +engine = {}; +%engine{end+1} = smoother_engine(jtree_2TBN_inf_engine(bnet)); +%engine{end+1} = smoother_engine(hmm_2TBN_inf_engine(bnet)); +%engine{end+1} = jtree_dbn_inf_engine(bnet); +engine{end+1} = jtree_unrolled_dbn_inf_engine(bnet, T); +engine{end+1} = cbk_inf_engine(bnet, 'clusters', {[1],[2],[3],[4],[5],[6],[7],[8]}); %ff +engine{end+1} = cbk_inf_engine(bnet, 'clusters', {[1 2],[3 4 5 6],[7 8]}); %manually designed marginally independent by BK +engine{end+1} = cbk_inf_engine(bnet, 'clusters', {[1:5], [3:7], [7:8]}); %manually designed conditionally independent by BK +engine{end+1} = cbk_inf_engine(bnet, 'clusters', {[1 3], [2 3 7], [3 5], [3 4 7], [6 7 8]}); %automatically found using TJTs offline +engine{end+1} = cbk_inf_engine(bnet, 'clusters', {[1 3 5], [2 3 5 7], [3 4 7], [4 6 7], [6 7 8]}); %automatically found using TJTs offline +engine{end+1} = cbk_inf_engine(bnet, 'clusters', {[1 3 4 5], [2 3 4 7 8], [4 6 7 8]}); %automatically found using TJTs offline + +% bk_inf_engine yields exactly the same results for the marginally independent cases. +%engine{end+1} = bk_inf_engine(bnet, 'clusters', 'ff'); +%engine{end+1} = bk_inf_engine(bnet, 'clusters', { [1 2], [3 4 5 6], [7 8] }); + + +inf_time = cmp_inference_dbn(bnet, engine, T, 'exact', 1) +learning_time = cmp_learning_dbn(bnet, engine, T, 'exact', 1) \ No newline at end of file diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/reveal1.m b/sourcecodes/bnt-master/BNT/examples/dynamic/reveal1.m new file mode 100644 index 00000000..938f4b46 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/reveal1.m @@ -0,0 +1,73 @@ +% Make a DBN with the following inter-connectivity matrix +% 1 +% / \ +% 2 3 +% \ / +% 4 +% | +% 5 +% where all arcs point down. In addition, there are persistence arcs from each node to itself. +% There are no intra-slice connections. +% Nodes have noisy-or CPDs. +% Node 1 turns on spontaneously due to its leaky source. +% This effect trickles down to the other nodes in the order shown. +% All the other nodes inhibit their leaks. +% None of the nodes inhibit the connection from themselves, so that once they are on, they remain +% on (persistence). +% +% This model was used in the experiments reported in +% - "Learning the structure of DBNs", Friedman, Murphy and Russell, UAI 1998. +% where the structure was learned even in the presence of missing data. +% In that paper, we used the structural EM algorithm. +% Here, we assume full observability and tabular CPDs for the learner, so we can use a much +% simpler learning algorithm. + +ss = 5; + +inter = eye(ss); +inter(1,[2 3]) = 1; +inter(2,4)=1; +inter(3,4)=1; +inter(4,5)=1; + +intra = zeros(ss); +ns = 2*ones(1,ss); + +bnet = mk_dbn(intra, inter, ns); + +% All nodes start out off +for i=1:ss + bnet.CPD{i} = tabular_CPD(bnet, i, [1.0 0.0]'); +end + +% The following params correspond to Fig 4a in the UAI 98 paper +% The first arg is the leak inhibition prob. +% The vector contains the inhib probs from the parents in the previous slice; +% the last element is self, which is never inhibited. +bnet.CPD{1+ss} = noisyor_CPD(bnet, 1+ss, 0.8, 0); +bnet.CPD{2+ss} = noisyor_CPD(bnet, 2+ss, 1, [0.9 0]); +bnet.CPD{3+ss} = noisyor_CPD(bnet, 3+ss, 1, [0.8 0]); +bnet.CPD{4+ss} = noisyor_CPD(bnet, 4+ss, 1, [0.7 0.6 0]); +bnet.CPD{5+ss} = noisyor_CPD(bnet, 5+ss, 1, [0.5 0]); + + +% Generate some training data + +nseqs = 20; +seqs = cell(1,nseqs); +T = 30; +for i=1:nseqs + seqs{i} = sample_dbn(bnet, T); +end + +max_fan_in = 3; % let's cheat a little here + +% computing num. incorrect edges as a fn of the size of the training set +%sz = [5 10 15 20]; +sz = [5 10]; +h = zeros(1, length(sz)); +for i=1:length(sz) + inter2 = learn_struct_dbn_reveal(seqs(1:sz(i)), ns, max_fan_in); + h(i) = sum(abs(inter(:)-inter2(:))); % hamming distance +end +h diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/scg_dbn.m b/sourcecodes/bnt-master/BNT/examples/dynamic/scg_dbn.m new file mode 100644 index 00000000..7281e58c --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/scg_dbn.m @@ -0,0 +1,39 @@ +% Test whether stable conditional Gaussian inference works +% Make a linear dynamical system +% X1 -> X2 +% | | +% v v +% Y1 Y2 + +intra = zeros(2); +intra(1,2) = 1; +inter = zeros(2); +inter(1,1) = 1; +n = 2; + +X = 2; % size of hidden state +Y = 2; % size of observable state + +ns = [X Y]; +bnet = mk_dbn(intra, inter, ns, 'discrete', [], 'observed', 2); + +x0 = rand(X,1); +V0 = eye(X); +C0 = rand(Y,X); +R0 = eye(Y); +A0 = rand(X,X); +Q0 = eye(X); + +bnet.CPD{1} = gaussian_CPD(bnet, 1, 'mean', x0, 'cov', V0); +bnet.CPD{2} = gaussian_CPD(bnet, 2, 'mean', zeros(Y,1), 'cov', R0, 'weights', C0); +bnet.CPD{3} = gaussian_CPD(bnet, 3, 'mean', zeros(X,1), 'cov', Q0, 'weights', A0); + + +T = 5; % fixed length sequences + +engine = {}; +engine{end+1} = kalman_inf_engine(bnet); +engine{end+1} = scg_unrolled_dbn_inf_engine(bnet, T); +engine{end+1} = jtree_unrolled_dbn_inf_engine(bnet, T); + +inf_time = cmp_inference_dbn(bnet, engine, T, 'check_ll', 0); diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/skf_data_assoc_gmux.m b/sourcecodes/bnt-master/BNT/examples/dynamic/skf_data_assoc_gmux.m new file mode 100644 index 00000000..31fdeda9 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/skf_data_assoc_gmux.m @@ -0,0 +1,139 @@ +% We consider a switching Kalman filter of the kind studied +% by Zoubin Ghahramani, i.e., where the switch node determines +% which of the hidden chains we get to observe (data association). +% e.g., for n=2 chains +% +% X1 -> X1 +% | X2 -> X2 +% \ | +% v +% Y +% ^ +% | +% S +% +% Y is a gmux (multiplexer) node, where S switches in one of the parents. +% We differ from Zoubin by not connecting the S nodes over time (which +% doesn't make sense for data association). +% Indeed, we assume the S nodes are always observed. +% +% +% We will track 2 objects (points) moving in the plane, as in BNT/Kalman/tracking_demo. +% We will alternate between observing them. + +nobj = 2; +N = nobj+2; +Xs = 1:nobj; +S = nobj+1; +Y = nobj+2; + +intra = zeros(N,N); +inter = zeros(N,N); +intra([Xs S], Y) =1; +for i=1:nobj + inter(Xs(i), Xs(i))=1; +end + +Xsz = 4; % state space = (x y xdot ydot) +Ysz = 2; +ns = zeros(1,N); +ns(Xs) = Xsz; +ns(Y) = Ysz; +ns(S) = n; + +bnet = mk_dbn(intra, inter, ns, 'discrete', S, 'observed', [S Y]); + +% For each object, we have +% X(t+1) = F X(t) + noise(Q) +% Y(t) = H X(t) + noise(R) +F = [1 0 1 0; 0 1 0 1; 0 0 1 0; 0 0 0 1]; +H = [1 0 0 0; 0 1 0 0]; +Q = 1e-3*eye(Xsz); +%R = 1e-3*eye(Ysz); +R = eye(Ysz); + +% We initialise object 1 moving to the right, and object 2 moving to the left +% (Here, we assume nobj=2) +init_state{1} = [10 10 1 0]'; +init_state{2} = [10 -10 -1 0]'; + +for i=1:nobj + bnet.CPD{Xs(i)} = gaussian_CPD(bnet, Xs(i), 'mean', init_state{i}, 'cov', 1e-4*eye(Xsz)); +end +bnet.CPD{S} = root_CPD(bnet, S); % always observed +bnet.CPD{Y} = gmux_CPD(bnet, Y, 'cov', repmat(R, [1 1 nobj]), 'weights', repmat(H, [1 1 nobj])); +% slice 2 +eclass = bnet.equiv_class; +for i=1:nobj + bnet.CPD{eclass(Xs(i), 2)} = gaussian_CPD(bnet, Xs(i)+N, 'mean', zeros(Xsz,1), 'cov', Q, 'weights', F); +end + +% Observe objects at random +T = 10; +evidence = cell(N, T); +data_assoc = sample_discrete(normalise(ones(1,nobj)), 1, T); +evidence(S,:) = num2cell(data_assoc); +evidence = sample_dbn(bnet, 'evidence', evidence); + +% plot the data +true_state = cell(1,nobj); +for i=1:nobj + true_state{i} = cell2num(evidence(Xs(i), :)); % true_state{i}(:,t) = [x y xdot ydot]' +end +obs_pos = cell2num(evidence(Y,:)); +figure(1) +clf +hold on +styles = {'rx', 'go', 'b+', 'k*'}; +for i=1:nobj + plot(true_state{i}(1,:), true_state{i}(2,:), styles{i}); +end +for t=1:T + text(obs_pos(1,t), obs_pos(2,t), sprintf('%d', t)); +end +hold off +relax_axes(0.1) + + +% Inference +ev = cell(N,T); +ev(bnet.observed,:) = evidence(bnet.observed, :); + +engines = {}; +engines{end+1} = jtree_dbn_inf_engine(bnet); +%engines{end+1} = scg_unrolled_dbn_inf_engine(bnet, T); +engines{end+1} = pearl_unrolled_dbn_inf_engine(bnet); +E = length(engines); + +inferred_state = cell(nobj,E); % inferred_state{i,e}(:,t) +for e=1:E + engines{e} = enter_evidence(engines{e}, ev); + for i=1:nobj + inferred_state{i,e} = zeros(4, T); + for t=1:T + m = marginal_nodes(engines{e}, Xs(i), t); + inferred_state{i,e}(:,t) = m.mu; + end + end +end +inferred_state{1,1} +inferred_state{1,2} + +% Plot results +figure(2) +clf +hold on +styles = {'rx', 'go', 'b+', 'k*'}; +nstyles = length(styles); +c = 1; +for e=1:E + for i=1:nobj + plot(inferred_state{i,e}(1,:), inferred_state{i,e}(2,:), styles{mod(c-1,nstyles)+1}); + c = c + 1; + end +end +for t=1:T + text(obs_pos(1,t), obs_pos(2,t), sprintf('%d', t)); +end +hold off +relax_axes(0.1) diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/viterbi1.m b/sourcecodes/bnt-master/BNT/examples/dynamic/viterbi1.m new file mode 100644 index 00000000..6ad07d91 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/viterbi1.m @@ -0,0 +1,47 @@ +% Compute Viterbi path discrete HMM by different methods + +intra = zeros(2); +intra(1,2) = 1; +inter = zeros(2); +inter(1,1) = 1; +n = 2; + +Q = 2; % num hidden states +O = 2; % num observable symbols + +ns = [Q O]; +dnodes = 1:2; +onodes = [2]; +eclass1 = [1 2]; +eclass2 = [3 2]; +bnet = mk_dbn(intra, inter, ns, 'discrete', dnodes, 'eclass1', eclass1, 'eclass2', eclass2, ... + 'observed', onodes); + +for seed=1:10 +rand('state', seed); +prior = normalise(rand(Q,1)); +transmat = mk_stochastic(rand(Q,Q)); +obsmat = mk_stochastic(rand(Q,O)); +bnet.CPD{1} = tabular_CPD(bnet, 1, prior); +bnet.CPD{2} = tabular_CPD(bnet, 2, obsmat); +bnet.CPD{3} = tabular_CPD(bnet, 3, transmat); + + +% Create a sequence +T = 5; +ev = sample_dbn(bnet, T); +evidence = cell(2,T); +evidence(2,:) = ev(2,:); % extract observed component +data = cell2num(ev(2,:)); + +%obslik = mk_dhmm_obs_lik(data, obsmat); +obslik = multinomial_prob(data, obsmat); +path = viterbi_path(prior, transmat, obslik); + +engine = {}; +engine{end+1} = smoother_engine(jtree_2TBN_inf_engine(bnet)); + +mpe = find_mpe(engine{1}, evidence); + +assert(isequal(cell2num(mpe(1,:)), path)) % extract values of hidden nodes +end diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/water1.m b/sourcecodes/bnt-master/BNT/examples/dynamic/water1.m new file mode 100644 index 00000000..7dcf205f --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/water1.m @@ -0,0 +1,20 @@ +% Compare the speeds of various inference engines on the water DBN +seed = 0; +rand('state', seed); +randn('state', seed); + +bnet = mk_water_dbn; + +T = 3; +engine = {}; +engine{end+1} = smoother_engine(jtree_2TBN_inf_engine(bnet)); +engine{end+1} = smoother_engine(hmm_2TBN_inf_engine(bnet)); +engine{end+1} = jtree_dbn_inf_engine(bnet); +engine{end+1} = jtree_unrolled_dbn_inf_engine(bnet, T); + +%engine{end+1} = bk_inf_engine(bnet, 'ff', onodes); +%engine{end+1} = bk_inf_engine(bnet, { [1 2], [3 4 5 6], [7 8] }, onodes); + +inf_time = cmp_inference_dbn(bnet, engine, T) +learning_time = cmp_learning_dbn(bnet, engine, T) + diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/water2.m b/sourcecodes/bnt-master/BNT/examples/dynamic/water2.m new file mode 100644 index 00000000..f7b9a42e --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/dynamic/water2.m @@ -0,0 +1,22 @@ +% Compare the speeds of various inference engines on the water DBN +seed = 0; +rand('state', seed); +randn('state', seed); + +bnet = mk_water_dbn; + +T = 3; + +engine = {}; +engine{end+1} = smoother_engine(jtree_2TBN_inf_engine(bnet)); +engine{end+1} = smoother_engine(jtree_sparse_2TBN_inf_engine(bnet)); +engine{end+1} = smoother_engine(hmm_2TBN_inf_engine(bnet)); +engine{end+1} = jtree_dbn_inf_engine(bnet); +%engine{end+1} = jtree_unrolled_dbn_inf_engine(bnet, T); + +%engine{end+1} = bk_inf_engine(bnet, 'ff', onodes); +%engine{end+1} = bk_inf_engine(bnet, { [1 2], [3 4 5 6], [7 8] }, onodes); + +inf_time = cmp_inference_dbn(bnet, engine, T) +%learning_time = cmp_learning_dbn(bnet, engine, T) + diff --git a/sourcecodes/bnt-master/BNT/examples/limids/CVS/Entries b/sourcecodes/bnt-master/BNT/examples/limids/CVS/Entries new file mode 100644 index 00000000..f7f12046 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/limids/CVS/Entries @@ -0,0 +1,6 @@ +/amnio.m/1.1.1.1/Mon Sep 13 03:21:04 2004// +/asia_dt1.m/1.1.1.1/Mon Jun 7 15:53:54 2004// +/id1.m/1.1.1.1/Wed May 29 15:59:54 2002// +/oil1.m/1.1.1.1/Mon Sep 13 02:27:08 2004// +/pigs1.m/1.1.1.1/Wed May 29 15:59:54 2002// +D diff --git a/sourcecodes/bnt-master/BNT/examples/limids/CVS/Repository b/sourcecodes/bnt-master/BNT/examples/limids/CVS/Repository new file mode 100644 index 00000000..bd5dd5af --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/limids/CVS/Repository @@ -0,0 +1 @@ +FullBNT/BNT/examples/limids diff --git a/sourcecodes/bnt-master/BNT/examples/limids/CVS/Root b/sourcecodes/bnt-master/BNT/examples/limids/CVS/Root new file mode 100644 index 00000000..f3bd14a6 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/limids/CVS/Root @@ -0,0 +1 @@ +:ext:nsaunier@bnt.cvs.sourceforge.net:/cvsroot/bnt diff --git a/sourcecodes/bnt-master/BNT/examples/limids/amnio.m b/sourcecodes/bnt-master/BNT/examples/limids/amnio.m new file mode 100644 index 00000000..fd621b6c --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/limids/amnio.m @@ -0,0 +1,135 @@ + +clear all +B0 = 1; Rtriple = 2; Damnio = 3; +B1 = 4; Ramnio = 5; Dabort = 6; +B2 = 7; U = 8; + +N = 8; +dag = zeros(N,N); +dag(B0, [Rtriple B1 Ramnio]) = 1; +dag(Rtriple, [Damnio Dabort]) = 1; +dag(Damnio, [B1 Ramnio]) = 1; +dag(B1, B2) = 1; +dag(Ramnio, [Dabort U]) = 1; +dag(Dabort, B2) = 1; +dag(B2, U) = 1; + + + +ns = zeros(1,N); +ns(B0) = 2; +ns(B1) = 3; +ns(B2) = 4; +ns(Rtriple) = 2; +ns(Ramnio) = 3; +ns(Damnio) = 2; +ns(Dabort) = 2; +ns(U) = 1; + +limid = mk_limid(dag, ns, 'chance', [B0 B1 B2], ... + 'decision', [Damnio Dabort], 'utility', [U]); + +% states of nature +healthy = 1; downs = 2; miscarry = 3; aborted = 4; +% test results +pos = 1; neg = 2; unk = 3; +% actions +yes = 1; no = 2; + +% Prior probability baby has downs syndrome +tbl = zeros(2,1); +p = 1/1000; % from www.downs-syndrome.org.uk figure +p = 24/10000; % www-personal.umich.edu/~bobwolfe/560/review/Downs.pdf (for women agen 35-40) +tbl(healthy) = 1-p; +tbl(downs) = p; +limid.CPD{B0} = tabular_CPD(limid, B0, tbl); + +% Reliability of triple screen test +% Unreliable sensor +% B0 -> Rtriple +tbl = zeros(2,2); % Rtriple = pos, neg +p = 0.5; % high false positive rate (guess) +tbl(healthy, :) = [p 1-p]; +p = 0.6; % low detection rate (march of dimes figure) +tbl(downs, :) = [p 1-p]; +limid.CPD{Rtriple} = tabular_CPD(limid, Rtriple, tbl); + +limid.CPD{Damnio} = tabular_decision_node(limid, Damnio); + +% Effect of amnio on baby B0,Damnio -> B1 + % 1/200 risk of miscarry +p = 1/200; % (march of dimes figure) +tbl = zeros(2, 2, 3); % B1 = healthy, downs, miscarry +tbl(healthy, no, :) = [1 0 0]; +tbl(downs, no, :) = [0 1 0]; +tbl(healthy, yes, :) = [1-p 0 p]; +tbl(downs, yes, :) = [0 1-p p]; +limid.CPD{B1} = tabular_CPD(limid, B1, tbl); + +% Reliability of amnio B0, Damnio -> Ramnio +% Perfect sensor +tbl = zeros(2,2,3); % Ramnio = pos, neg, unk +tbl(:, no, :) = repmat([0 0 1], 2 ,1); +tbl(healthy, yes, :) = [0 1 0]; +tbl(downs, yes, :) = [1 0 0]; +limid.CPD{Ramnio} = tabular_CPD(limid, Ramnio, tbl); + +limid.CPD{Dabort} = tabular_decision_node(limid, Dabort); + +% Effect of abortion on baby B1, Dabort -> B2 +tbl = zeros(3, 2, 4); % B2 = healthy, downs, miscarry, aborted +tbl(:, yes, :) = repmat([0 0 0 1], 3, 1); +tbl(healthy, no, :) = [1 0 0 0]; +tbl(downs, no, :) = [0 1 0 0]; +tbl(miscarry, no, :) = [0 0 1 0]; +limid.CPD{B2} = tabular_CPD(limid, B2, tbl); + +% Utility U(Ramnio, B2) +tbl = zeros(3, 4); +tbl(:, healthy) = 5000; +tbl(:, downs) = -50000; +tbl(:, miscarry) = -1000; +tbl(:, aborted) = -1000; + +if 0 +%tbl(unk, miscarry) = 0; % this case is impossible +tbl(pos, miscarry) = -1; +tbl(neg, miscarry) = -1000; +if 1 + tbl(unk, aborted) = -100; + tbl(pos, aborted) = -1; + tbl(neg, aborted) = -500; +else % pro-life utility fn + tbl(unk, aborted) = -500000; + tbl(pos, aborted) = -500000; + tbl(neg, aborted) = -500000; +end +end + +limid.CPD{U} = tabular_utility_node(limid, U, tbl); + + + +engine = jtree_limid_inf_engine(limid); +[strategy, MEU] = solve_limid(engine); + +% Rtriple U(Damnio=1=yes) U(Damnio=2=no) +% 1=pos 0 1 +% 2=neg 0 1 +dispcpt(strategy{Damnio}) +if isequal(strategy{Damnio}(1,:), strategy{Damnio}(2,:)) + % Rtriple result irrelevant + doAmnio = argmax(strategy{Damnio}(1,:)) +else + doAmnio = 1; +end + +% Rtriple Ramnio U(Dabort=yes=1) U(Dabort=no=2) +% 1=pos 1=pos 1 0 +% 2=neg 1=pos 1 0 +% 1=pos 2=neg 0 1 +% 2=neg 2=neg 0 1 +% 1=pos 3=unk 0 1 +% 2=neg 3=unk 0 1 +dispcpt(strategy{Dabort}) + diff --git a/sourcecodes/bnt-master/BNT/examples/limids/asia_dt1.m b/sourcecodes/bnt-master/BNT/examples/limids/asia_dt1.m new file mode 100644 index 00000000..de305818 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/limids/asia_dt1.m @@ -0,0 +1,100 @@ +% decision theoretic version of asia network +% Cowell et al, p177 +% We explicitely add the no-forgetting arcs. + +Smoking = 1; +VisitToAsia = 2; +Bronchitis = 3; +LungCancer = 4; +TB = 5; +Do_Xray = 6; +TBorCancer = 7; +Util_Xray = 8; +Dys = 9; +posXray = 10; +Do_Hosp = 11; +Util_Hosp = 12; + +n = 12; +dag = zeros(n); +dag(Smoking, [Bronchitis LungCancer]) = 1; +dag(VisitToAsia, [TB Do_Xray Do_Hosp]) = 1; +dag(Bronchitis, Dys) = 1; +dag(LungCancer, [Util_Hosp TBorCancer]) = 1; +dag(TB, [Util_Hosp TBorCancer Util_Xray]) = 1; +dag(Do_Xray, [posXray Util_Xray Do_Hosp]) = 1; +dag(TBorCancer, [Dys posXray]) = 1; +dag(Dys, Do_Hosp) = 1; +dag(posXray, Do_Hosp) = 1; +dag(Do_Hosp, Util_Hosp) = 1; + +dnodes = [Do_Xray Do_Hosp]; +unodes = [Util_Xray Util_Hosp]; +cnodes = mysetdiff(1:n, [dnodes unodes]); % chance nodes +ns = 2*ones(1,n); +ns(unodes) = 1; +limid = mk_limid(dag, ns, 'chance', cnodes, 'decision', dnodes, 'utility', unodes); + +% 1 = yes, 2 = no +limid.CPD{VisitToAsia} = tabular_CPD(limid, VisitToAsia, [0.01 0.99]); +limid.CPD{Bronchitis} = tabular_CPD(limid, Bronchitis, [0.6 0.3 0.4 0.7]); +limid.CPD{Dys} = tabular_CPD(limid, Dys, [0.9 0.7 0.8 0.1 0.1 0.3 0.2 0.9]); +limid.CPD{TBorCancer} = tabular_CPD(limid, TBorCancer, [1 1 1 0 0 0 0 1]); + +limid.CPD{LungCancer} = tabular_CPD(limid, LungCancer, [0.1 0.01 0.9 0.99]); +limid.CPD{Smoking} = tabular_CPD(limid, Smoking, [0.5 0.5]); +limid.CPD{TB} = tabular_CPD(limid, TB, [0.05 0.01 0.95 0.99]); +limid.CPD{posXray} = tabular_CPD(limid, posXray, [0.98 0.5 0.05 0.5 0.02 0.5 0.95 0.5]); + +limid.CPD{Util_Hosp} = tabular_utility_node(limid, Util_Hosp, [180 120 160 15 2 4 0 40]); +limid.CPD{Util_Xray} = tabular_utility_node(limid, Util_Xray, [0 1 10 10]); + +for i=dnodes(:)' + limid.CPD{i} = tabular_decision_node(limid, i); +end + +engines = {}; +engines{end+1} = global_joint_inf_engine(limid); +engines{end+1} = jtree_limid_inf_engine(limid); +%engines{end+1} = belprop_inf_engine(limid); + +exact = [1 2]; +%approx = 3; +approx = []; + + +NE = length(engines); +MEU = zeros(1, NE); +niter = zeros(1, NE); +strategy = cell(1, NE); + +tol = 1e-2; +for e=1:length(engines) + [strategy{e}, MEU(e), niter(e)] = solve_limid(engines{e}); +end + +for e=exact(:)' + assert(approxeq(MEU(e), 47.49, tol)) + assert(isequal(strategy{e}{Do_Xray}(:)', [1 0 0 1])) + + % Check the hosptialize strategy is correct (p180) + % We assume the patient has not been to Asia and therefore did not have an Xray. + % In this case it is optimal not to hospitalize regardless of whether the patient has + % dyspnoea or not (and of course regardless of the value of pos_xray). + asia = 2; + do_xray = 2; + for dys = 1:2 + for pos_xray = 1:2 + assert(argmax(squeeze(strategy{e}{Do_Hosp}(asia, do_xray, dys, pos_xray, :))) == 2) + end + end +end + + +for e=approx(:)' + approxeq(strategy{exact(1)}{Do_Xray}, strategy{e}{Do_Xray}) + approxeq(strategy{exact(1)}{Do_Hosp}, strategy{e}{Do_Hosp}) +end + + + diff --git a/sourcecodes/bnt-master/BNT/examples/limids/id1.m b/sourcecodes/bnt-master/BNT/examples/limids/id1.m new file mode 100644 index 00000000..ddacd67a --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/limids/id1.m @@ -0,0 +1,50 @@ +% influence diagram with no loops +% +% rv dec +% \ / +% utility + +N = 3; +dag = zeros(N); +X = 1; D = 2; U = 3; +dag([X D], U)=1; + +ns = zeros(1,N); +ns(X) = 2; ns(D) = 2; ns(U) = 1; + +limid = mk_limid(dag, ns, 'chance', X, 'decision', D, 'utility', U); + +% use random params +limid.CPD{X} = tabular_CPD(limid, X); +limid.CPD{D} = tabular_decision_node(limid, D); +limid.CPD{U} = tabular_utility_node(limid, U); + +%fname = '/home/cs/murphyk/matlab/Misc/loopybel.txt'; +global BNT_HOME +fname = sprintf('%s/loopybel.txt', BNT_HOME); + +engines = {}; +engines{end+1} = global_joint_inf_engine(limid); +engines{end+1} = jtree_limid_inf_engine(limid); +%engines{end+1} = belprop_inf_engine(limid, 'max_iter', 2*N, 'filename', fname); +engines{end+1} = belprop_inf_engine(limid, 'max_iter', 2*N); + +exact = [1 2]; +approx = 3; + +E = length(engines); +strategy = cell(1, E); +MEU = zeros(1, E); +for e=1:E + [strategy{e}, MEU(e)] = solve_limid(engines{e}); + MEU +end +MEU + +for e=exact(:)' + assert(approxeq(strategy{exact(1)}{D}, strategy{e}{D})) +end + +for e=approx(:)' + approxeq(strategy{exact(1)}{D}, strategy{e}{D}) +end diff --git a/sourcecodes/bnt-master/BNT/examples/limids/oil1.m b/sourcecodes/bnt-master/BNT/examples/limids/oil1.m new file mode 100644 index 00000000..192acc9d --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/limids/oil1.m @@ -0,0 +1,91 @@ +% oil wildcatter influence diagram in Cowell et al p172 + +% T = test for oil? +% UT = utility (negative cost) of testing +% O = amount of oil = Dry, Wet or Soaking +% R = results of test = NoStrucure, OpenStructure, ClosedStructure or NoResult +% D = drill? +% UD = utility of drilling + +% Decision sequence = T R D O + +T = 1; UT = 2; O = 3; R = 4; D = 5; UD = 6; +N = 6; +dag = zeros(N); +dag(T, [UT R D]) = 1; +dag(O, [R UD]) = 1; +dag(R, D) = 1; +dag(D, UD) = 1; + +ns = zeros(1,N); +ns(O) = 3; ns(R) = 4; ns(T) = 2; ns(D) = 2; ns(UT) = 1; ns(UD) = 1; + +limid = mk_limid(dag, ns, 'chance', [O R], 'decision', [T D], 'utility', [UT UD]); + +limid.CPD{O} = tabular_CPD(limid, O, [0.5 0.3 0.2]); +tbl = [0.6 0 0.3 0 0.1 0 0.3 0 0.4 0 0.4 0 0.1 0 0.3 0 0.5 0 0 1 0 1 0 1]; +limid.CPD{R} = tabular_CPD(limid, R, tbl); + +limid.CPD{UT} = tabular_utility_node(limid, UT, [-10 0]); +limid.CPD{UD} = tabular_utility_node(limid, UD, [-70 50 200 0 0 0]); + +if 1 + % start with uniform policies + limid.CPD{T} = tabular_decision_node(limid, T); + limid.CPD{D} = tabular_decision_node(limid, D); +else + % hard code optimal policies + limid.CPD{T} = tabular_decision_node(limid, T, [1.0 0.0]); + a = 0.5; b = 1-a; % arbitrary value + tbl = myreshape([0 a 1 a 1 a a a 1 b 0 b 0 b b b], ns([T R D])); + limid.CPD{D} = tabular_decision_node(limid, D, tbl); +end + +%fname = '/home/cs/murphyk/matlab/Misc/loopybel.txt'; + +engines = {}; +engines{end+1} = global_joint_inf_engine(limid); +engines{end+1} = jtree_limid_inf_engine(limid); +%engines{end+1} = belprop_inf_engine(limid, 'max_iter', 3*N, 'filename', fname); + +exact = [1 2]; +%approx = 3; +approx = []; + +E = length(engines); +strategy = cell(1, E); +MEU = zeros(1, E); +for e=1:E + [strategy{e}, MEU(e)] = solve_limid(engines{e}); + MEU +end +MEU + +for e=exact(:)' + assert(approxeq(MEU(e), 22.5)) + % U(T=yes) U(T=no) + % 1 0 + assert(argmax(strategy{e}{T}) == 1); % test = yes + t = 1; % test = yes + % strategy{D} T R U(D=yes=1) U(D=no=2) + % 1=yes 1=noS 0 1 Don't drill + % 2=no 1=noS 1 0 + % 1=yes 2=opS 1 0 + % 2=no 2=opS 1 0 + % 1=yes 3=clS 1 0 + % 2=no 3=clS 1 0 + % 1=yes 4=unk 1 0 + % 2=no 4=unk 1 0 + + for r=[2 3] % OpS, ClS + assert(argmax(squeeze(strategy{e}{D}(t,r,:))) == 1); % drill = yes + end + r = 1; % noS + assert(argmax(squeeze(strategy{e}{D}(t,r,:))) == 2); % drill = no +end + + +for e=approx(:)' + approxeq(strategy{exact(1)}{T}, strategy{e}{T}) + approxeq(strategy{exact(1)}{D}, strategy{e}{D}) +end diff --git a/sourcecodes/bnt-master/BNT/examples/limids/pigs1.m b/sourcecodes/bnt-master/BNT/examples/limids/pigs1.m new file mode 100644 index 00000000..6a56ad04 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/limids/pigs1.m @@ -0,0 +1,153 @@ +% pigs model from Lauritzen and Nilsson, 2001 + +seed = 0; +rand('state', seed); +randn('state', seed); + +% we number nodes down and to the right +h = [1 5 9 13]; +t = [2 6 10]; +d = [3 7 11]; +u = [4 8 12 14]; + +N = 14; +dag = zeros(N); + +% causal arcs +for i=1:3 + dag(h(i), [t(i) h(i+1)]) = 1; + dag(d(i), [u(i) h(i+1)]) = 1; +end +dag(h(4), u(4)) = 1; + +% information arcs +fig = 2; +switch fig + case 0, + % no info arcs + case 1, + % no-forgetting policy (figure 1) + for i=1:3 + dag(t(i), d(i:3)) = 1; + end + case 2, + % reactive policy (figure 2) + for i=1:3 + dag(t(i), d(i)) = 1; + end + case 7, + % omniscient policy (figure 7: di has access to hidden state h(i-1)) + dag(t(1), d(1)) = 1; + for i=2:3 + %dag([h(i-1) t(i-1) d(i-1)], d(i)) = 1; + dag([h(i-1) d(i-1)], d(i)) = 1; % t(i-1) is redundant given h(i-1) + end +end + + +ns = 2*ones(1,N); +ns(u) = 1; + +% parameter tying +params = ones(1,N); +uparam = 1; +final_uparam = 2; +tparam = 3; +h1_param = 4; +hparam = 5; +dparams = 6:8; + +params(u(1:3)) = uparam; +params(u(4)) = final_uparam; +params(t) = tparam; +params(h(1)) = h1_param; +params(h(2:end)) = hparam; +params(d) = dparams; + +limid = mk_limid(dag, ns, 'chance', [h t], 'decision', d, 'utility', u, 'equiv_class', params); + +% h = 1 means healthy, h = 2 means diseased +% d = 1 means don't treat, d = 2 means treat +% t = 1 means test shows healthy, t = 2 means test shows diseased + +if 0 + % use random params + limid.CPD{final_uparam} = tabular_utility_node(limid, u(4)); + limid.CPD{uparam} = tabular_utility_node(limid, u(1)); + limid.CPD{tparam} = tabular_CPD(limid, t(1)); + limid.CPD{h1_param} = tabular_CPD(limid, h(1)); + limid.CPD{hparam} = tabular_CPD(limid, h(2)); +else + limid.CPD{final_uparam} = tabular_utility_node(limid, u(4), [1000 300]); + limid.CPD{uparam} = tabular_utility_node(limid, u(1), [0 -100]); % costs have negative utility! + + % h P(t=1) P(t=2) + % 1 0.9 0.1 + % 2 0.2 0.8 + limid.CPD{tparam} = tabular_CPD(limid, t(1), [0.9 0.2 0.1 0.8]); + + % P(h1) + limid.CPD{h1_param} = tabular_CPD(limid, h(1), [0.9 0.1]); + + % hi di P(hj=1) P(hj=2), j = i+1, i=1:3 + % 1 1 0.8 0.2 + % 2 1 0.1 0.9 + % 1 2 0.9 0.1 + % 2 2 0.5 0.5 + limid.CPD{hparam} = tabular_CPD(limid, h(2), [0.8 0.1 0.9 0.5 0.2 0.9 0.1 0.5]); +end + +% Decision nodes get assigned uniform policies by default +for i=1:3 + limid.CPD{dparams(i)} = tabular_decision_node(limid, d(i)); +end + + +fname = '/home/cs/murphyk/matlab/Misc/loopybel.txt'; + +engines = {}; +engines{end+1} = global_joint_inf_engine(limid); +engines{end+1} = jtree_limid_inf_engine(limid); +%engines{end+1} = belprop_inf_engine(limid, 'max_iter', 1*N, 'filename', fname, 'tol', 1e-3); + +exact = [1 2]; +%approx = 3; +approx = []; + +max_iter = 1; +order = d(end:-1:1); +%order = d(1:end); + +NE = length(engines); +MEU = zeros(1, NE); +niter = zeros(1, NE); +strategy = cell(1, NE); +for e=1:NE + [strategy{e}, MEU(e), niter(e)] = solve_limid(engines{e}, 'max_iter', max_iter, 'order', order); +end +MEU + +% check results match those in the paper (p. 22) +direct_policy = eye(2); % treat iff test is positive +never_policy = [1 0; 1 0]; % never treat +tol = 1e-0; % results in paper are reported to 0dp +for e=exact(:)' + switch fig + case 2, % reactive policy + assert(approxeq(MEU(e), 727, tol)); + assert(approxeq(strategy{e}{d(1)}(:), never_policy(:))) + assert(approxeq(strategy{e}{d(2)}(:), direct_policy(:))) + assert(approxeq(strategy{e}{d(3)}(:), direct_policy(:))) + case 1, assert(approxeq(MEU(e), 729, tol)); + case 7, assert(approxeq(MEU(e), 732, tol)); + end +end + + +for e=approx(:)' + for i=1:3 + approxeq(strategy{exact(1)}{d(i)}, strategy{e}{d(i)}) + dispcpt(strategy{e}{d(i)}) + end +end + diff --git a/sourcecodes/bnt-master/BNT/examples/static/Belprop/CVS/Entries b/sourcecodes/bnt-master/BNT/examples/static/Belprop/CVS/Entries new file mode 100644 index 00000000..6550caa0 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/Belprop/CVS/Entries @@ -0,0 +1,11 @@ +/belprop_loop1_discrete.m/1.1.1.1/Wed May 29 15:59:54 2002// +/belprop_loop1_gauss.m/1.1.1.1/Wed May 29 15:59:54 2002// +/belprop_loopy_cg.m/1.1.1.1/Wed May 29 15:59:54 2002// +/belprop_loopy_discrete.m/1.1.1.1/Wed May 29 15:59:54 2002// +/belprop_loopy_gauss.m/1.1.1.1/Wed May 29 15:59:54 2002// +/belprop_polytree_cg.m/1.1.1.1/Wed May 29 15:59:54 2002// +/belprop_polytree_discrete.m/1.1.1.1/Tue Oct 1 18:21:26 2002// +/belprop_polytree_gauss.m/1.1.1.1/Wed May 29 15:59:54 2002// +/bp1.m/1.1.1.1/Wed May 29 15:59:54 2002// +/gmux1.m/1.1.1.1/Wed May 29 15:59:54 2002// +D diff --git a/sourcecodes/bnt-master/BNT/examples/static/Belprop/CVS/Repository b/sourcecodes/bnt-master/BNT/examples/static/Belprop/CVS/Repository new file mode 100644 index 00000000..f3d573bd --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/Belprop/CVS/Repository @@ -0,0 +1 @@ +FullBNT/BNT/examples/static/Belprop diff --git a/sourcecodes/bnt-master/BNT/examples/static/Belprop/CVS/Root b/sourcecodes/bnt-master/BNT/examples/static/Belprop/CVS/Root new file mode 100644 index 00000000..f3bd14a6 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/Belprop/CVS/Root @@ -0,0 +1 @@ +:ext:nsaunier@bnt.cvs.sourceforge.net:/cvsroot/bnt diff --git a/sourcecodes/bnt-master/BNT/examples/static/Belprop/belprop_loop1_discrete.m b/sourcecodes/bnt-master/BNT/examples/static/Belprop/belprop_loop1_discrete.m new file mode 100644 index 00000000..20faed5e --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/Belprop/belprop_loop1_discrete.m @@ -0,0 +1,26 @@ +% Compare different loopy belief propagation algorithms on a graph with a single loop. +% LBP should give exact results if it converges. + +N = 4; +dag = zeros(N,N); +C = 1; S = 2; R = 3; W = 4; +dag(C,[R S]) = 1; +dag(R,W) = 1; +dag(S,W)=1; +ns = 2*ones(1,N); +bnet = mk_bnet(dag, ns); +for i=1:N + bnet.CPD{i} = tabular_CPD(bnet, i); +end + +engines = {}; +engines{end+1} = jtree_inf_engine(bnet); +engines{end+1} = pearl_inf_engine(bnet, 'protocol', 'parallel'); +engines{end+1} = belprop_fg_inf_engine(bnet_to_fgraph(bnet)); +engines{end+1} = belprop_inf_engine(bnet, 'protocol', 'parallel'); + +% belprop_fg does not support marginal_family +% belprop_fg and belprop do not support loglik even on discrete +[time, engines] = cmp_inference_static(bnet, engines, 'maximize', 0, 'exact', 1, 'observed', 2, ... + 'check_ll', 0, 'singletons_only', 1, 'check_converged', 2:4); + diff --git a/sourcecodes/bnt-master/BNT/examples/static/Belprop/belprop_loop1_gauss.m b/sourcecodes/bnt-master/BNT/examples/static/Belprop/belprop_loop1_gauss.m new file mode 100644 index 00000000..e547e44a --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/Belprop/belprop_loop1_gauss.m @@ -0,0 +1,25 @@ +% Compare different loopy belief propagation algorithms on a graph with a single loop. +% LBP should give exact results if it converges. + +N = 4; +dag = zeros(N,N); +C = 1; S = 2; R = 3; W = 4; +dag(C,[R S]) = 1; +dag(R,W) = 1; +dag(S,W)=1; +ns = 2*ones(1,N); +bnet = mk_bnet(dag, ns, 'discrete', []); +for i=1:N + bnet.CPD{i} = gaussian_CPD(bnet, i); +end + +engines = {}; +engines{end+1} = jtree_inf_engine(bnet); +engines{end+1} = pearl_inf_engine(bnet, 'protocol', 'parallel', 'max_iter', 20); +%engines{end+1} = pearl_inf_engine(bnet, 'protocol', 'parallel', 'max_iter', 20, 'filename', ... +% '/home/eecs/murphyk/matlab/gausspearl.txt', 'tol', 1e-5); + +% pearl gaussian does not compute loglik +[time, engines] = cmp_inference_static(bnet, engines, 'maximize', 0, 'exact', 1, 'observed', [2], ... + 'check_ll', 0, 'singletons_only', 0, 'check_converged', [2]); + diff --git a/sourcecodes/bnt-master/BNT/examples/static/Belprop/belprop_loopy_cg.m b/sourcecodes/bnt-master/BNT/examples/static/Belprop/belprop_loopy_cg.m new file mode 100644 index 00000000..01f36b03 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/Belprop/belprop_loopy_cg.m @@ -0,0 +1,22 @@ +% Same as cg1, except we assume all discretes are observed, +% and use loopy for approximate inference. + +ns = 2*ones(1,9); +F = 1; W = 2; E = 3; B = 4; C = 5; D = 6; Min = 7; Mout = 8; L = 9; +n = 9; +dnodes = [B F W]; +cnodes = mysetdiff(1:n, dnodes); + +%bnet = mk_incinerator_bnet(ns); +bnet = mk_incinerator_bnet; + +bnet.observed = [dnodes E]; + +engines = {}; +engines{end+1} = jtree_inf_engine(bnet); +engines{end+1} = pearl_inf_engine(bnet, 'protocol', 'parallel'); +nengines = length(engines); + + +[time, engines] = cmp_inference_static(bnet, engines, 'maximize', 0, 'check_ll', 0, ... + 'singletons_only', 0, 'exact', 1, 'check_converged', 2); diff --git a/sourcecodes/bnt-master/BNT/examples/static/Belprop/belprop_loopy_discrete.m b/sourcecodes/bnt-master/BNT/examples/static/Belprop/belprop_loopy_discrete.m new file mode 100644 index 00000000..c46e6d02 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/Belprop/belprop_loopy_discrete.m @@ -0,0 +1,13 @@ +% Compare different loopy belief propagation algorithms on a graph with many loops + +bnet = mk_asia_bnet('orig'); + +engines = {}; +engines{end+1} = jtree_inf_engine(bnet); +engines{end+1} = pearl_inf_engine(bnet, 'protocol', 'parallel'); +engines{end+1} = belprop_fg_inf_engine(bnet_to_fgraph(bnet)); +engines{end+1} = belprop_inf_engine(bnet, 'protocol', 'parallel'); + +[time, engines] = cmp_inference_static(bnet, engines, 'maximize', 0, 'exact', 1, 'observed', [1 3 5], ... + 'check_ll', 0, 'singletons_only', 1, 'check_converged', 2:4); + diff --git a/sourcecodes/bnt-master/BNT/examples/static/Belprop/belprop_loopy_gauss.m b/sourcecodes/bnt-master/BNT/examples/static/Belprop/belprop_loopy_gauss.m new file mode 100644 index 00000000..a9924aed --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/Belprop/belprop_loopy_gauss.m @@ -0,0 +1,12 @@ +% Compare different loopy belief propagation algorithms on a graph with many loops +% If LBP converges, the means should be exact + +bnet = mk_asia_bnet('gauss'); + +engines = {}; +engines{end+1} = jtree_inf_engine(bnet); +engines{end+1} = pearl_inf_engine(bnet, 'protocol', 'parallel'); + +[time, engines] = cmp_inference_static(bnet, engines, 'maximize', 0, 'exact', 1, 'observed', [1 3 5], ... + 'check_ll', 0, 'singletons_only', 0, 'check_converged', 2); + diff --git a/sourcecodes/bnt-master/BNT/examples/static/Belprop/belprop_polytree_cg.m b/sourcecodes/bnt-master/BNT/examples/static/Belprop/belprop_polytree_cg.m new file mode 100644 index 00000000..70aa03da --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/Belprop/belprop_polytree_cg.m @@ -0,0 +1,33 @@ +% Inference on a conditional Gaussian model + +% Make the following polytree, where all arcs point down + +% 1 2 +% \ / +% 3 +% / \ +% 4 5 + +N = 5; +dag = zeros(N,N); +dag(1,3) = 1; +dag(2,3) = 1; +dag(3, [4 5]) = 1; + +ns = [2 1 2 1 2]; + +dnodes = 1; +%onodes = [1 5]; +bnet = mk_bnet(dag, ns, 'discrete', dnodes, 'observed', dnodes); + +bnet.CPD{1} = tabular_CPD(bnet, 1); +for i=2:N + bnet.CPD{i} = gaussian_CPD(bnet, i); +end + +engine = {}; +engine{end+1} = jtree_inf_engine(bnet); +engine{end+1} = pearl_inf_engine(bnet, 'protocol', 'parallel'); + +[time, engine] = cmp_inference_static(bnet, engine, 'maximize', 0, 'check_ll', 0, ... + 'singletons_only', 0, 'observed', [1 3]); diff --git a/sourcecodes/bnt-master/BNT/examples/static/Belprop/belprop_polytree_discrete.m b/sourcecodes/bnt-master/BNT/examples/static/Belprop/belprop_polytree_discrete.m new file mode 100644 index 00000000..d8a3a229 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/Belprop/belprop_polytree_discrete.m @@ -0,0 +1,38 @@ +% Make the following polytree, where all arcs point down + +% 1 2 +% \ / +% 3 +% / \ +% 4 5 + +N = 5; +dag = zeros(N,N); +dag(1,3) = 1; +dag(2,3) = 1; +dag(3, [4 5]) = 1; + +ns = 2*ones(1,N); % binary nodes + +onodes = [1 5]; + +bnet = mk_bnet(dag, ns, 'observed', onodes); + +if 0 +seed = 0; +rand('state', seed); +randn('state', seed); +end + +for i=1:N + %bnet.CPD{i} = tabular_CPD(bnet, i); + bnet.CPD{i} = noisyor_CPD(bnet, i); +end + +engine = {}; +engine{end+1} = jtree_inf_engine(bnet); +engine{end+1} = pearl_inf_engine(bnet, 'protocol', 'tree'); +engine{end+1} = pearl_inf_engine(bnet, 'protocol', 'parallel'); + +[err, time] = cmp_inference_static(bnet, engine, 'maximize', 0, 'check_ll', 1); + diff --git a/sourcecodes/bnt-master/BNT/examples/static/Belprop/belprop_polytree_gauss.m b/sourcecodes/bnt-master/BNT/examples/static/Belprop/belprop_polytree_gauss.m new file mode 100644 index 00000000..1823c107 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/Belprop/belprop_polytree_gauss.m @@ -0,0 +1,135 @@ +% Do the example from Satnam Alag's PhD thesis, UCB ME dept 1996 p46 + +% Make the following polytree, where all arcs point down + +% 1 2 +% \ / +% 3 +% / \ +% 4 5 + +N = 5; +dag = zeros(N,N); +dag(1,3) = 1; +dag(2,3) = 1; +dag(3, [4 5]) = 1; + +ns = [2 1 2 1 2]; + +bnet = mk_bnet(dag, ns, 'discrete', []); + +bnet.CPD{1} = gaussian_CPD(bnet, 1, 'mean', [1 0]', 'cov', [4 1; 1 4]); +bnet.CPD{2} = gaussian_CPD(bnet, 2, 'mean', 1, 'cov', 1); +B1 = [1 2; 1 0]; B2 = [2 1]'; +bnet.CPD{3} = gaussian_CPD(bnet, 3, 'mean', [0 0]', 'cov', [2 1; 1 1], ... + 'weights', [B1 B2]); +H1 = [1 1]; +bnet.CPD{4} = gaussian_CPD(bnet, 4, 'mean', 0, 'cov', 1, 'weights', H1); +H2 = [1 0; 1 1]; +bnet.CPD{5} = gaussian_CPD(bnet, 5, 'mean', [0 0]', 'cov', eye(2), 'weights', H2); + +engine = {}; +engine{end+1} = jtree_inf_engine(bnet); +engine{end+1} = pearl_inf_engine(bnet, 'protocol', 'tree'); +engine{end+1} = pearl_inf_engine(bnet, 'protocol', 'parallel'); +E = length(engine); + +if 1 +% no evidence +evidence = cell(1,N); +ll = zeros(1,E); +for e=1:E + [engine{e}, ll(e)] = enter_evidence(engine{e}, evidence); + add_ev = 1; + m = marginal_nodes(engine{e}, 3, add_ev); + assert(approxeq(m.mu, [3 2]')) + assert(approxeq(m.Sigma, [30 9; 9 6])) + + m = marginal_nodes(engine{e}, 4, add_ev); + assert(approxeq(m.mu, 5)) + assert(approxeq(m.Sigma, 55)) + + m = marginal_nodes(engine{e}, 5, add_ev); + assert(approxeq(m.mu, [3 5]')) + assert(approxeq(m.Sigma, [31 39; 39 55])) +end +end + +if 1 +% evidence on leaf 5 +evidence = cell(1,N); +evidence{5} = [5 5]'; +for e=1:E + [engine{e}, ll(e)] = enter_evidence(engine{e}, evidence); + add_ev = 1; + m = marginal_nodes(engine{e}, 3, add_ev); + assert(approxeq(m.mu, [4.4022 1.0217]')) + assert(approxeq(m.Sigma, [0.7011 -0.4891; -0.4891 1.1087])) + + m = marginal_nodes(engine{e}, 4, add_ev); + assert(approxeq(m.mu, 5.4239)) + assert(approxeq(m.Sigma, 1.8315)) + + m = marginal_nodes(engine{e}, 1, add_ev); + assert(approxeq(m.mu, [0.3478 1.1413]')) + assert(approxeq(m.Sigma, [1.8261 -0.1957; -0.1957 1.0924])) + + m = marginal_nodes(engine{e}, 2, add_ev); + assert(approxeq(m.mu, 0.9239)) + assert(approxeq(m.Sigma, 0.8315)) + + m = marginal_nodes(engine{e}, 5, add_ev); + assert(approxeq(m.mu, evidence{5})) + assert(approxeq(m.Sigma, zeros(2))) +end +end + +if 1 +% evidence on leaf 4 (non-info-state version is uninvertible) +evidence = cell(1,N); +evidence{4} = 10; +for e=1:E + [engine{e}, ll(e)] = enter_evidence(engine{e}, evidence); + add_ev = 1; + m = marginal_nodes(engine{e}, 3, add_ev); + assert(approxeq(m.mu, [6.5455 3.3636]')) + assert(approxeq(m.Sigma, [2.3455 -1.6364; -1.6364 1.9091])) + + m = marginal_nodes(engine{e}, 5, add_ev); + assert(approxeq(m.mu, [6.5455 9.9091]')) + assert(approxeq(m.Sigma, [3.3455 0.7091; 0.7091 1.9818])) + + m = marginal_nodes(engine{e}, 1, add_ev); + assert(approxeq(m.mu, [1.9091 0.9091]')) + assert(approxeq(m.Sigma, [2.1818 -0.8182; -0.8182 2.1818])) + + m = marginal_nodes(engine{e}, 2, add_ev); + assert(approxeq(m.mu, 1.2727)) + assert(approxeq(m.Sigma, 0.8364)) +end +end + + +if 1 +% evidence on leaves 4,5 and root 2 +evidence = cell(1,N); +evidence{2} = 0; +evidence{4} = 10; +evidence{5} = [5 5]'; +for e=1:E + [engine{e}, ll(e)] = enter_evidence(engine{e}, evidence); + add_ev = 1; + m = marginal_nodes(engine{e}, 3, add_ev); + assert(approxeq(m.mu, [4.9964 2.4444]')); + assert(approxeq(m.Sigma, [0.6738 -0.5556; -0.5556 0.8889])); + + m = marginal_nodes(engine{e}, 1, add_ev); + assert(approxeq(m.mu, [2.2043 1.2151]')); + assert(approxeq(m.Sigma, [1.2903 -0.4839; -0.4839 0.8065])); +end +end + +if 1 + [time, engine] = cmp_inference_static(bnet, engine, 'maximize', 0, 'check_ll', 0, ... + 'singletons_only', 0, 'observed', [1 3 5]); +end diff --git a/sourcecodes/bnt-master/BNT/examples/static/Belprop/bp1.m b/sourcecodes/bnt-master/BNT/examples/static/Belprop/bp1.m new file mode 100644 index 00000000..93cba443 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/Belprop/bp1.m @@ -0,0 +1,24 @@ +% Compare different loopy belief propagation algorithms on a graph with a single loop. +% LBP should give exact results if it converges. + +seed = 0; +rand('state', seed); +randn('state', seed); + +N = 2; +dag = zeros(N,N); +dag(1,2)=1; +ns = ones(1,N); +bnet = mk_bnet(dag, ns, 'discrete', []); +for i=1:N + %bnet.CPD{i} = gaussian_CPD(bnet, i, 'mean', 0); + bnet.CPD{i} = gaussian_CPD(bnet, i); +end + +engines = {}; +engines{end+1} = jtree_inf_engine(bnet); +engines{end+1} = pearl_inf_engine(bnet, 'protocol', 'tree'); + +[time, engines] = cmp_inference_static(bnet, engines, 'maximize', 0, 'exact', 1:2, 'observed', [2], ... + 'check_ll', 0, 'singletons_only', 1, 'check_converged', []); + diff --git a/sourcecodes/bnt-master/BNT/examples/static/Belprop/gmux1.m b/sourcecodes/bnt-master/BNT/examples/static/Belprop/gmux1.m new file mode 100644 index 00000000..21846b68 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/Belprop/gmux1.m @@ -0,0 +1,90 @@ +% Test gmux. +% The following model, where Y is a gmux node, +% and M is set to 1, should be equivalent to X1 -> Y +% +% X1 Xn M +% \ | / +% Y + +n = 3; +N = n+2; +Xs = 1:n; +M = n+1; +Y = n+2; +dag = zeros(N,N); +dag([Xs M], Y)=1; + +dnodes = M; +ns = zeros(1, N); +sz = 2; +ns(Xs) = sz; +ns(M) = n; +ns(Y) = sz; + +bnet = mk_bnet(dag, ns, 'discrete', M, 'observed', [M Y]); + +psz = ns(Xs(1)); +selfsz = ns(Y); + +W = randn(selfsz, psz); +mu = randn(selfsz, 1); +Sigma = eye(selfsz, selfsz); + +bnet.CPD{M} = root_CPD(bnet, M); +for i=Xs(:)' + bnet.CPD{i} = gaussian_CPD(bnet, i, 'mean', zeros(psz, 1), 'cov', eye(psz, psz)); +end +bnet.CPD{Y} = gmux_CPD(bnet, Y, 'mean', mu, 'weights', W, 'cov', Sigma); + +evidence = cell(1,N); +yval = randn(selfsz, 1); +evidence{Y} = yval; +m = 2; +%notm = not(m-1)+1; % only valid for n=2 +notm = mysetdiff(1:n, m); +evidence{M} = m; + +engines = {}; +engines{end+1} = jtree_inf_engine(bnet); +engines{end+1} = pearl_inf_engine(bnet, 'protocol', 'parallel'); + +for e=1:length(engines) + engines{e} = enter_evidence(engines{e}, evidence); + mXm{e} = marginal_nodes(engines{e}, Xs(m)); + + % Since M=m, only Xm was updated. + % Hence the posterior on Xnotm should equal the prior. + for i=notm(:)' + mXnotm = marginal_nodes(engines{e}, Xs(i)); + assert(approxeq(mXnotm.mu, zeros(psz,1))) + assert(approxeq(mXnotm.Sigma, eye(psz, psz))) + end +end + +% Check that all engines give the same posterior +for e=2:length(engines) + assert(approxeq(mXm{e}.mu, mXm{1}.mu)) + assert(approxeq(mXm{e}.Sigma, mXm{1}.Sigma)) +end + + +% Compute the correct posterior by building Xm -> Y + +N = 2; +dag = zeros(N,N); +dag(1, 2)=1; +ns = [psz selfsz]; +bnet = mk_bnet(dag, ns, 'discrete', [], 'observed', 2); + +bnet.CPD{1} = gaussian_CPD(bnet, 1, 'mean', zeros(psz, 1), 'cov', eye(psz, psz)); +bnet.CPD{2} = gaussian_CPD(bnet, 2, 'mean', mu, 'cov', Sigma, 'weights', W); + +jengine = jtree_inf_engine(bnet); +evidence = {[], yval}; +jengine = enter_evidence(jengine, evidence); % apply Bayes rule to invert the arc +mX = marginal_nodes(jengine, 1); + +for e=1:length(engines) + assert(approxeq(mX.mu, mXm{e}.mu)) + assert(approxeq(mX.Sigma, mXm{e}.Sigma)) +end diff --git a/sourcecodes/bnt-master/BNT/examples/static/Brutti/Belief_IOhmm.m b/sourcecodes/bnt-master/BNT/examples/static/Brutti/Belief_IOhmm.m new file mode 100644 index 00000000..43c6c802 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/Brutti/Belief_IOhmm.m @@ -0,0 +1,49 @@ +% Sigmoid Belief IOHMM +% Here is the model +% +% X \ X \ +% | | | | +% Q-|->Q-|-> ... +% | / | / +% Y Y +% +clear all; +clc; +rand('state',0); randn('state',0); +X = 1; Q = 2; Y = 3; +% intra time-slice graph +intra=zeros(3); +intra(X,[Q Y])=1; +intra(Q,Y)=1; +% inter time-slice graph +inter=zeros(3); +inter(Q,Q)=1; + +ns = [1 3 1]; +dnodes = [2]; +eclass1 = [1 2 3]; +eclass2 = [1 4 3]; +bnet = mk_dbn(intra, inter, ns, dnodes, eclass1, eclass2); +bnet.CPD{1} = root_CPD(bnet, 1); +% ========================================================== +bnet.CPD{2} = softmax_CPD(bnet, 2); +bnet.CPD{4} = softmax_CPD(bnet, 5, 'discrete', [2]); +% ========================================================== +bnet.CPD{3} = gaussian_CPD(bnet, 3); + +% make some data +T=20; +cases = cell(3, T); +cases(1,:)=num2cell(round(rand(1,T)*2)+1); +%cases(2,:)=num2cell(round(rand(1,T))+1); +cases(3,:)=num2cell(rand(1,T)); + +engine = bk_inf_engine(bnet, 'exact', [1 2 3]); + +% log lik before learning +[engine, loglik] = enter_evidence(engine, cases); + +% do learning +ev=cell(1,1); +ev{1}=cases; +[bnet2, LL2] = learn_params_dbn_em(engine, ev, 3); \ No newline at end of file diff --git a/sourcecodes/bnt-master/BNT/examples/static/Brutti/Belief_hmdt.m b/sourcecodes/bnt-master/BNT/examples/static/Brutti/Belief_hmdt.m new file mode 100644 index 00000000..88c4ae00 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/Brutti/Belief_hmdt.m @@ -0,0 +1,48 @@ +% Sigmoid Belief Hidden Markov Decision Tree (Jordan/Gharhamani 1996) +% +clear all; +%clc; +rand('state',0); randn('state',0); +X = 1; Q1 = 2; Q2 = 3; Y = 4; +% intra time-slice graph +intra=zeros(4); +intra(X,[Q1 Q2 Y])=1; +intra(Q1,[Q2 Y])=1; +intra(Q2, Y)=1; +% inter time-slice graph +inter=zeros(4); +inter(Q1,Q1)=1; +inter(Q2,Q2)=1; + +ns = [1 2 3 1]; +dnodes = [2 3]; +eclass1 = [1 2 3 4]; +eclass2 = [1 5 6 4]; +bnet = mk_dbn(intra, inter, ns, dnodes, eclass1, eclass2); + +bnet.CPD{1} = root_CPD(bnet, 1); +% ========================================= +bnet.CPD{2} = softmax_CPD(bnet, 2); +bnet.CPD{3} = softmax_CPD(bnet, 3, 'discrete', [2]); +bnet.CPD{5} = softmax_CPD(bnet, 6); +bnet.CPD{6} = softmax_CPD(bnet, 7, 'discrete', [3 6]); +% ========================================= +bnet.CPD{4} = gaussian_CPD(bnet, 4); + +% make some data +T=20; +cases = cell(4, T); +cases(1,:)=num2cell(round(rand(1,T)*2)+1); +%cases(2,:)=num2cell(round(rand(1,T))+1); +%cases(3,:)=num2cell(round(rand(1,T)*2)+1); +cases(4,:)=num2cell(rand(1,T)); + +engine = bk_inf_engine(bnet, 'exact', [1 2 3 4]); + +% log lik before learning +[engine, loglik] = enter_evidence(engine, cases); + +% do learning +ev=cell(1,1); +ev{1}=cases; +[bnet2, LL2] = learn_params_dbn_em(engine, ev, 10); \ No newline at end of file diff --git a/sourcecodes/bnt-master/BNT/examples/static/Brutti/Belief_hme.m b/sourcecodes/bnt-master/BNT/examples/static/Brutti/Belief_hme.m new file mode 100644 index 00000000..0b298d48 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/Brutti/Belief_hme.m @@ -0,0 +1,37 @@ +% Sigmoid Belief Hierarchical Mixtures of Experts + +clear all +clc +X = 1; +Q1 = 2; +Q2 = 3; +Y = 4; +dag = zeros(4,4); +dag(X,[Q1 Q2 Y]) = 1; +dag(Q1, [Q2 Y]) = 1; +dag(Q2,Y)=1; +ns = [1 3 4 3]; +dnodes = [2 3 4]; +onodes=[1 2 3 4]; +bnet = mk_bnet(dag,ns, dnodes); + +rand('state',0); randn('state',0); + +bnet.CPD{1} = root_CPD(bnet, 1); +bnet.CPD{2} = softmax_CPD(bnet, 2, 'max_iter', 3); +bnet.CPD{3} = softmax_CPD(bnet, 3, 'discrete', [2], 'max_iter', 3); +bnet.CPD{4} = softmax_CPD(bnet, 4, 'discrete', [2 3], 'max_iter', 3); + +T=5; +cases = cell(4, T); +cases(1,:)=num2cell(rand(1,T)); +%cases(2,:)=num2cell(round(rand(1,T)*2)+1); +%cases(3,:)=num2cell(round(rand(1,T)*3)+1); +cases(4,:)=num2cell(round(rand(1,T)*2)+1); + +engine = jtree_inf_engine(bnet, onodes); + +[engine, loglik] = enter_evidence(engine, cases); + +disp('learning-------------------------------------------') +[bnet2, LL2] = learn_params_em(engine, cases, 4); \ No newline at end of file diff --git a/sourcecodes/bnt-master/BNT/examples/static/Brutti/CVS/Entries b/sourcecodes/bnt-master/BNT/examples/static/Brutti/CVS/Entries new file mode 100644 index 00000000..bf214c0a --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/Brutti/CVS/Entries @@ -0,0 +1,5 @@ +/Belief_IOhmm.m/1.1.1.1/Wed May 29 15:59:54 2002// +/Belief_hmdt.m/1.1.1.1/Wed May 29 15:59:54 2002// +/Belief_hme.m/1.1.1.1/Wed May 29 15:59:54 2002// +/Sigmoid_Belief.m/1.1.1.1/Wed May 29 15:59:54 2002// +D diff --git a/sourcecodes/bnt-master/BNT/examples/static/Brutti/CVS/Repository b/sourcecodes/bnt-master/BNT/examples/static/Brutti/CVS/Repository new file mode 100644 index 00000000..52d4e2ed --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/Brutti/CVS/Repository @@ -0,0 +1 @@ +FullBNT/BNT/examples/static/Brutti diff --git a/sourcecodes/bnt-master/BNT/examples/static/Brutti/CVS/Root b/sourcecodes/bnt-master/BNT/examples/static/Brutti/CVS/Root new file mode 100644 index 00000000..f3bd14a6 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/Brutti/CVS/Root @@ -0,0 +1 @@ +:ext:nsaunier@bnt.cvs.sourceforge.net:/cvsroot/bnt diff --git a/sourcecodes/bnt-master/BNT/examples/static/Brutti/Sigmoid_Belief.m b/sourcecodes/bnt-master/BNT/examples/static/Brutti/Sigmoid_Belief.m new file mode 100644 index 00000000..1a6ecc35 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/Brutti/Sigmoid_Belief.m @@ -0,0 +1,49 @@ +% Sigmoid Belief Net + +clear all +clc +dum1 = 1; +dum2 = 2; +dum3 = 3; +Q1 = 4; +Q2 = 5; +Y = 6; +dag = zeros(6,6); +dag(dum1,[Q1 Y]) = 1; +dag(dum2, Q2)=1; +dag(dum3, [Q1 Q2])=1; +dag(Q1,[Q2 Y]) = 1; +dag(Q2, Y)=1; + +ns = [2 2 3 3 4 3]; +dnodes = [1:6]; +bnet = mk_bnet(dag,ns, dnodes); + +rand('state',0); randn('state',0); +n_iter=10; +clamped=0; + +bnet.CPD{1} = tabular_CPD(bnet, 1); +bnet.CPD{2} = tabular_CPD(bnet, 2); +bnet.CPD{3} = tabular_CPD(bnet, 3); +% CPD = dsoftmax_CPD(bnet, self, dummy_pars, w, b, clamped, max_iter, verbose, wthresh,... +% llthresh, approx_hess) +bnet.CPD{4} = softmax_CPD(bnet, 4, 'discrete', [1 3]); +bnet.CPD{5} = softmax_CPD(bnet, 5, 'discrete', [2 3]); +bnet.CPD{6} = softmax_CPD(bnet, 6, 'discrete', [1 4]); + +T=5; +cases = cell(6, T); +cases(1,:)=num2cell(round(rand(1,T)*1)+1); +%cases(2,:)=num2cell(round(rand(1,T)*1)+1); +cases(3,:)=num2cell(round(rand(1,T)*2)+1); +cases(4,:)=num2cell(round(rand(1,T)*2)+1); +%cases(5,:)=num2cell(round(rand(1,T)*3)+1); +cases(6,:)=num2cell(round(rand(1,T)*2)+1); + +engine = jtree_inf_engine(bnet); + +[engine, loglik] = enter_evidence(engine, cases); + +disp('learning-------------------------------------------') +[bnet2, LL2, eng2] = learn_params_em(engine, cases, n_iter); \ No newline at end of file diff --git a/sourcecodes/bnt-master/BNT/examples/static/CVS/Entries b/sourcecodes/bnt-master/BNT/examples/static/CVS/Entries new file mode 100644 index 00000000..f27bc17d --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/CVS/Entries @@ -0,0 +1,30 @@ +/brainy.m/1.1.1.1/Sun Feb 22 19:43:32 2004// +/burglar-alarm-net.lisp.txt/1.1.1.1/Thu Mar 4 22:27:48 2004// +/burglary.m/1.1.1.1/Thu Mar 4 22:34:14 2004// +/cg1.m/1.1.1.1/Wed May 29 15:59:54 2002// +/cg2.m/1.1.1.1/Wed May 29 15:59:54 2002// +/cmp_inference_static.m/1.2/Sat Sep 17 16:59:57 2005// +/discrete1.m/1.1.1.1/Mon Jun 7 19:45:06 2004// +/discrete2.m/1.1.1.1/Wed May 29 15:59:54 2002// +/discrete3.m/1.1.1.1/Wed May 29 15:59:54 2002// +/fa1.m/1.1.1.1/Wed May 29 15:59:54 2002// +/gaussian1.m/1.1.1.1/Wed May 29 15:59:54 2002// +/gaussian2.m/1.1.1.1/Thu Jun 10 01:31:02 2004// +/gibbs_test1.m/1.1.1.1/Wed May 29 15:59:54 2002// +/learn1.m/1.1.1.1/Sat Feb 28 17:25:40 2004// +/lw1.m/1.1.1.1/Wed May 29 15:59:54 2002// +/mfa1.m/1.1.1.1/Wed May 29 15:59:54 2002// +/mixexp1.m/1.1.1.1/Wed May 29 15:59:54 2002// +/mixexp2.m/1.1.1.1/Wed May 29 15:59:54 2002// +/mixexp3.m/1.1.1.1/Wed May 29 15:59:54 2002// +/mog1.m/1.1.1.1/Wed May 29 15:59:54 2002// +/mpe1.m/1.1.1.1/Wed Jun 19 22:08:58 2002// +/mpe2.m/1.1.1.1/Wed Jun 19 22:09:08 2002// +/nodeorderExample.m/1.1.1.1/Thu Jun 10 01:42:04 2004// +/qmr1.m/1.1.1.1/Wed May 29 15:59:54 2002// +/qmr2.m/1.1.1.1/Thu Nov 14 01:01:46 2002// +/sample1.m/1.1.1.1/Wed May 29 15:59:54 2002// +/softev1.m/1.1.1.1/Wed Jun 19 23:59:18 2002// +/softmax1.m/1.1.1.1/Wed May 29 15:59:54 2002// +/sprinkler1.m/1.1.1.1/Sun Sep 12 21:01:38 2004// +D diff --git a/sourcecodes/bnt-master/BNT/examples/static/CVS/Entries.Log b/sourcecodes/bnt-master/BNT/examples/static/CVS/Entries.Log new file mode 100644 index 00000000..d4b2cb30 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/CVS/Entries.Log @@ -0,0 +1,10 @@ +A D/Belprop//// +A D/Brutti//// +A D/HME//// +A D/Misc//// +A D/Models//// +A D/SCG//// +A D/StructLearn//// +A D/Zoubin//// +A D/dtree//// +A D/fgraph//// diff --git a/sourcecodes/bnt-master/BNT/examples/static/CVS/Repository b/sourcecodes/bnt-master/BNT/examples/static/CVS/Repository new file mode 100644 index 00000000..f43b2803 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/CVS/Repository @@ -0,0 +1 @@ +FullBNT/BNT/examples/static diff --git a/sourcecodes/bnt-master/BNT/examples/static/CVS/Root b/sourcecodes/bnt-master/BNT/examples/static/CVS/Root new file mode 100644 index 00000000..f3bd14a6 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/CVS/Root @@ -0,0 +1 @@ +:ext:nsaunier@bnt.cvs.sourceforge.net:/cvsroot/bnt diff --git a/sourcecodes/bnt-master/BNT/examples/static/HME/CVS/Entries b/sourcecodes/bnt-master/BNT/examples/static/HME/CVS/Entries new file mode 100644 index 00000000..b27a9df8 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/HME/CVS/Entries @@ -0,0 +1,14 @@ +/HMEforMatlab.jpg/1.1.1.1/Wed May 29 15:59:54 2002// +/README/1.1.1.1/Wed May 29 15:59:54 2002// +/fhme.m/1.1.1.1/Wed May 29 15:59:54 2002// +/gen_data.m/1.1.1.1/Wed May 29 15:59:54 2002// +/hme_class_plot.m/1.1.1.1/Wed May 29 15:59:54 2002// +/hme_reg_plot.m/1.1.1.1/Wed May 29 15:59:54 2002// +/hme_topobuilder.m/1.1.1.1/Wed May 29 15:59:54 2002// +/hmemenu.m/1.1.1.1/Thu Feb 12 12:57:28 2004// +/test_data_class.mat/1.1.1.1/Wed May 29 15:59:54 2002// +/test_data_class2.mat/1.1.1.1/Wed May 29 15:59:54 2002// +/test_data_reg.mat/1.1.1.1/Wed May 29 15:59:54 2002// +/train_data_class.mat/1.1.1.1/Wed May 29 15:59:54 2002// +/train_data_reg.mat/1.1.1.1/Wed May 29 15:59:54 2002// +D diff --git a/sourcecodes/bnt-master/BNT/examples/static/HME/CVS/Repository b/sourcecodes/bnt-master/BNT/examples/static/HME/CVS/Repository new file mode 100644 index 00000000..2ac6a351 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/HME/CVS/Repository @@ -0,0 +1 @@ +FullBNT/BNT/examples/static/HME diff --git a/sourcecodes/bnt-master/BNT/examples/static/HME/CVS/Root b/sourcecodes/bnt-master/BNT/examples/static/HME/CVS/Root new file mode 100644 index 00000000..f3bd14a6 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/HME/CVS/Root @@ -0,0 +1 @@ +:ext:nsaunier@bnt.cvs.sourceforge.net:/cvsroot/bnt diff --git a/sourcecodes/bnt-master/BNT/examples/static/HME/HMEforMatlab.jpg b/sourcecodes/bnt-master/BNT/examples/static/HME/HMEforMatlab.jpg new file mode 100644 index 00000000..16682678 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/HME/HMEforMatlab.jpg Binary files differdiff --git a/sourcecodes/bnt-master/BNT/examples/static/HME/README b/sourcecodes/bnt-master/BNT/examples/static/HME/README new file mode 100644 index 00000000..4c794975 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/HME/README @@ -0,0 +1,2 @@ +This directory contains code for hierarchical mixture of experts, +written by Pierpaolo Brutti (May 2001). Run the file hmemenu to get started. diff --git a/sourcecodes/bnt-master/BNT/examples/static/HME/fhme.m b/sourcecodes/bnt-master/BNT/examples/static/HME/fhme.m new file mode 100644 index 00000000..6aeb7196 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/HME/fhme.m @@ -0,0 +1,109 @@ +function risultati = fhme(net, nodes_info, data, n) +%HMEFWD Forward propagation through an HME model +% +% Each row of the (n x class_num) matrix 'risultati' containes the estimated class posterior prob. +% +% ---------------------------------------------------------------------------------------------------- +% -> pierpaolo_b@hotmail.com or -> pampo@interfree.it +% ---------------------------------------------------------------------------------------------------- +% +ns=net.node_sizes; +if nargin==3 + ndata=n; +else + ndata=size(data, 1); +end +altezza=size(ns,2); +coeff=cell(altezza-1,1); +for m=1:ndata + %- i=2 -------------------------------------------------------------------------------------- + s=struct(net.CPD{2}); + if nodes_info(1,2)==0, + mu=[]; W=[]; predict=[]; + mu=s.mean(:,:); + W=s.weights(:,:,:); + predict=mu(:,:)+W(:,:,:)*data(m,:)'; + coeff{1,1}=predict'; + elseif nodes_info(1,2)==1, + coeff{1,1}=fglm(s.glim{1}, data(m,:)); + else, + coeff{1,1}=fmlp(s.mlp{1}, data(m,:)); + end + %---------------------------------------------------------------------------------------------- + if altezza>3, + for i=3:altezza-1, + s=[]; f=[]; dpsz=[]; + f=family(net.dag,i); f=f(2:end-1); dpsz=prod(ns(f)); + s=struct(net.CPD{i}); + for j=1:dpsz, + if nodes_info(1,i)==1, + coeff{i-1,1}(j,:)=coeff{i-2,1}(1,j)*fglm(s.glim{j}, data(m,:)); + else + coeff{i-1,1}(j,:)=coeff{i-2,1}(1,j)*fmlp(s.mlp{j}, data(m,:)); + end + end + app=cat(2, coeff{i-1,1}(:)); coeff{i-1,1}=app'; clear app; + end + end + %- i=altezza ---------------------------------------------------------------------------------- + if altezza>2, + i=altezza; + s=[]; f=[]; dpsz=[]; + f=family(net.dag,i); f=f(2:end-1); dpsz=prod(ns(f)); + s=struct(net.CPD{i}); + if nodes_info(1,i)==0, + mu=[]; W=[]; + mu=s.mean(:,:); + W=s.weights(:,:,:); + end + for j=1:dpsz, + if nodes_info(1,i)==0, + predict=[]; + predict=mu(:,j)+W(:,:,j)*data(m,:)'; + coeff{i-1,1}(j,:)=coeff{i-2,1}(1,j)*predict'; + elseif nodes_info(1,i)==1, + coeff{i-1,1}(j,:)=coeff{i-2,1}(1,j)*fglm(s.glim{j}, data(m,:)); + else + coeff{i-1,1}(j,:)=coeff{i-2,1}(1,j)*fmlp(s.mlp{j}, data(m,:)); + end + end + end + %---------------------------------------------------------------------------------------------- + risultati(m,:)=sum(coeff{altezza-1,1},1); + clear coeff; coeff=cell(altezza-1,1); +end +return + +%------------------------------------------------------------------- + +function [y, a] = fglm(net, x) +%GLMFWD Forward propagation through 1-layer net->GLM statistical model + +ndata = size(x, 1); + +a = x*net.w1 + ones(ndata, 1)*net.b1; + +nout = size(a,2); +% Ensure that sum(exp(a), 2) does not overflow +maxcut = log(realmax) - log(nout); +% Ensure that exp(a) > 0 +mincut = log(realmin); +a = min(a, maxcut); +a = max(a, mincut); +temp = exp(a); +y = temp./(sum(temp, 2)*ones(1,nout)); + +%------------------------------------------------------------------- + +function [y, z, a] = fmlp(net, x) +%MLPFWD Forward propagation through 2-layer network. + +ndata = size(x, 1); + +z = tanh(x*net.w1 + ones(ndata, 1)*net.b1); +a = z*net.w2 + ones(ndata, 1)*net.b2; +temp = exp(a); +nout = size(a,2); +y = temp./(sum(temp,2)*ones(1,nout)); + +%------------------------------------------------------------------- diff --git a/sourcecodes/bnt-master/BNT/examples/static/HME/gen_data.m b/sourcecodes/bnt-master/BNT/examples/static/HME/gen_data.m new file mode 100644 index 00000000..c37f9d28 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/HME/gen_data.m @@ -0,0 +1,52 @@ +function [data, ndata1, ndata2, targets]=gen_data(ndata, seed) +% Generate data from three classes in 2d +% Setting 'seed' for reproducible results +% OUTPUT +% data : data set +% ndata1, ndata2: separator + +if nargin<1, + error('Missing data size'); +end + +input_dim = 2; +num_classes = 3; + +if nargin==2, + % Fix seeds for reproducible results + randn('state', seed); + rand('state', seed); +end + +% Generate mixture of three Gaussians in two dimensional space +data = randn(ndata, input_dim); +targets = zeros(ndata, 3); + +% Priors for the clusters +prior(1) = 0.4; +prior(2) = 0.3; +prior(3) = 0.3; + +% Cluster centres +c = [2.0, 2.0; 0.0, 0.0; 1, -1]; + +ndata1 = round(prior(1)*ndata); +ndata2 = round((prior(1) + prior(2))*ndata); +% Put first cluster at (2, 2) +data(1:ndata1, 1) = data(1:ndata1, 1) * 0.5 + c(1,1); +data(1:ndata1, 2) = data(1:ndata1, 2) * 0.5 + c(1,2); +targets(1:ndata1, 1) = 1; + +% Leave second cluster at (0,0) +data((ndata1 + 1):ndata2, :) = data((ndata1 + 1):ndata2, :); +targets((ndata1+1):ndata2, 2) = 1; + +data((ndata2+1):ndata, 1) = data((ndata2+1):ndata,1) *0.6 + c(3, 1); +data((ndata2+1):ndata, 2) = data((ndata2+1):ndata,2) *0.6 + c(3, 2); +targets((ndata2+1):ndata, 3) = 1; + +if 0 + ndata = 1; + data = x; + targets = [1 0 0]; +end diff --git a/sourcecodes/bnt-master/BNT/examples/static/HME/hme_class_plot.m b/sourcecodes/bnt-master/BNT/examples/static/HME/hme_class_plot.m new file mode 100644 index 00000000..de60c2ee --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/HME/hme_class_plot.m @@ -0,0 +1,142 @@ +function fh=hme_class_plot(net, nodes_info, train_data, test_data) +% +% Use this function ONLY when the input dimension is 2 +% and the problem is a classification one. +% We assume that each row of 'train_data' & 'test_data' is an example. +% +%------Line Spec------------------------------------------------------------------------ +% +% LineWidth - specifies the width (in points) of the line +% MarkerEdgeColor - specifies the color of the marker or the edge color +% forfilled markers (circle, square, diamond, pentagram, hexagram, and the +% four triangles). +% MarkerFaceColor - specifies the color of the face of filled markers. +% MarkerSize - specifies the size of the marker in points. +% +% Example +% ------- +% plot(t,sin(2*t),'-mo',... +% 'LineWidth',2,... +% 'MarkerEdgeColor','k',... % 'k'=black +% 'MarkerFaceColor',[.49 1 .63],... % RGB color +% 'MarkerSize',12) +%---------------------------------------------------------------------------------------- + +class_num=nodes_info(2,end); +mn_x = round(min(train_data(:,1))); mx_x = round(max(train_data(:,1))); +mn_y = round(min(train_data(:,2))); mx_y = round(max(train_data(:,2))); +if nargin==4, + mn_x = round(min([train_data(:,1); test_data(:,1)])); + mx_x = round(max([train_data(:,1); test_data(:,1)])); + mn_y = round(min([train_data(:,2); test_data(:,2)])); + mx_y = round(max([train_data(:,1); test_data(:,2)])); +end +x = mn_x(1)-1:0.2:mx_x(1)+1; +y = mn_y(1)-1:0.2:mx_y(1)+1; +[X, Y] = meshgrid(x,y); +X = X(:); +Y = Y(:); +num_g=size(X,1); +griglia = [X Y]; +rand('state',1); +if class_num<=6, + colors=['r'; 'g'; 'b'; 'c'; 'm'; 'y']; +else + colors=rand(class_num, 3); % each row is an RGB color +end +fh = figure('Name','Data & decision boundaries', 'MenuBar', 'none', 'NumberTitle', 'off'); +ms=5; % Marker Size +if nargin==4, +% ms=4; % Marker Size + subplot(1,2,1); +end +% Plot of train_set ------------------------------------------------------------------------- +axis([mn_x-1 mx_x+1 mn_y-1 mx_y+1]); +set(gca, 'Box', 'on'); +c_max_train = max(train_data(:,3)); +hold on +for m=1:c_max_train, + app_x=train_data(:,1); + app_y=train_data(:,2); + thisX=app_x(train_data(:,3)==m); + thisY=app_y(train_data(:,3)==m); + if class_num<=6, + str_col=[]; + str_col=['o', colors(m,:)]; + plot(thisX, thisY, str_col, 'MarkerSize', ms); + else + plot(thisX, thisY, 'o',... + 'LineWidth', 1,... + 'MarkerEdgeColor', colors(m,:), 'MarkerSize', ms) + end +end +%---hmefwd_generale(net,data,ndata)----------------------------------------------------------- +Z=fhme(net, nodes_info, griglia, num_g); % forward propagation trougth the HME +%--------------------------------------------------------------------------------------------- +[foo , class] = max(Z'); % 0/1 loss function => we assume that the true class is the one with the + % maximum posterior prob. +class = class'; +for m = 1:class_num, + thisX=[]; thisY=[]; + thisX = X(class == m); + thisY = Y(class == m); + if class_num<=6, + str_col=[]; + str_col=['d', colors(m,:)]; + h=plot(thisX, thisY, str_col); + else + h = plot(thisX, thisY, 'd',... + 'MarkerEdgeColor',colors(m,:),... + 'MarkerFaceColor','w'); + end + set(h, 'MarkerSize', 4); +end +title('Training set and Decision Boundaries (0/1 loss)') +hold off + +% Plot of test_set -------------------------------------------------------------------------- +if nargin==4, + subplot(1,2,2); + axis([mn_x-1 mx_x+1 mn_y-1 mx_y+1]); + set(gca, 'Box', 'on'); + hold on + if size(test_data,2)==3, % we know the classification of the test set examples + c_max_test = max(test_data(:,3)); + for m=1:c_max_test, + app_x=test_data(:,1); + app_y=test_data(:,2); + thisX=app_x(test_data(:,3)==m); + thisY=app_y(test_data(:,3)==m); + if class_num<=6, + str_col=[]; + str_col=['o', colors(m,:)]; + plot(thisX, thisY, str_col, 'MarkerSize', ms); + else + plot(thisX, thisY, 'o',... + 'LineWidth', 1,... + 'MarkerEdgeColor', colors(m,:),... + 'MarkerSize',ms); + end + end + else + plot(test_data(:,1), test_data(:,2), 'ko',... + 'MarkerSize', ms); + end + for m = 1:class_num, + thisX=[]; thisY=[]; + thisX = X(class == m); + thisY = Y(class == m); + if class_num<=6, + str_col=[]; + str_col=['d', colors(m,:)]; + h=plot(thisX, thisY, str_col); + else + h = plot(thisX, thisY, 'd',... + 'MarkerEdgeColor', colors(m,:),... + 'MarkerFaceColor','w'); + end + set(h, 'MarkerSize', 4); + end + title('Test set and Decision Boundaries (0/1 loss)') + hold off +end \ No newline at end of file diff --git a/sourcecodes/bnt-master/BNT/examples/static/HME/hme_reg_plot.m b/sourcecodes/bnt-master/BNT/examples/static/HME/hme_reg_plot.m new file mode 100644 index 00000000..510e96c2 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/HME/hme_reg_plot.m @@ -0,0 +1,43 @@ +function fh=hme_reg_plot(net, nodes_info, train_data, test_data) +% +% Use this function ONLY when the input dimension is 1 +% and the problem is a regression one. +% We assume that each row of 'train_data' & 'test_data' is an example. +% +% ---------------------------------------------------------------------------------------------------- +% -> pierpaolo_b@hotmail.com or -> pampo@interfree.it +% ---------------------------------------------------------------------------------------------------- + +fh=figure('Name','HME based regression', 'MenuBar', 'none', 'NumberTitle', 'off'); + +mn_x_train = round(min(train_data(:,1))); +mx_x_train = round(max(train_data(:,1))); +x_train = mn_x_train(1):0.01:mx_x_train(1); +Z_train=fhme(net, nodes_info, x_train',size(x_train,2)); % forward propagation trougth the HME + +if nargin==4, + subplot(2,1,1); + mn_x_test = round(min(test_data(:,1))); + mx_x_test = round(max(test_data(:,1))); + x_test = mn_x_test(1):0.01:mx_x_test(1); + Z_test=fhme(net, nodes_info, x_test',size(x_test,2)); % forward propagation trougth the HME +end + +hold on; +set(gca, 'Box', 'on'); +plot(x_train', Z_train, 'r'); +plot(train_data(:,1),train_data(:,2),'+k'); +title('Training set and prediction'); +hold off + +if nargin==4, + subplot(2,1,2); + hold on; + set(gca, 'Box', 'on'); + plot(x_train', Z_train, 'r'); + if size(test_data,2)==2, + plot(test_data(:,1),test_data(:,2),'+k'); + end + title('Test set and prediction'); + hold off +end \ No newline at end of file diff --git a/sourcecodes/bnt-master/BNT/examples/static/HME/hme_topobuilder.m b/sourcecodes/bnt-master/BNT/examples/static/HME/hme_topobuilder.m new file mode 100644 index 00000000..0893bc38 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/HME/hme_topobuilder.m @@ -0,0 +1,47 @@ +function [bnet, onodes]=hme_topobuilder(nodes_info); +% +% HME topology builder +% +% ---------------------------------------------------------------------------------------------------- +% -> pierpaolo_b@hotmail.com or -> pampo@interfree.it +% ---------------------------------------------------------------------------------------------------- + +nodes_num=size(nodes_info,2); +dag = zeros(nodes_num); +list=[1:nodes_num]; +for i=1:(nodes_num-1) + app=[]; + app=list((i+1):end); + dag(i,app) = 1; +end +onodes = [1 nodes_num]; +dnodes = list(2:end-1); +if nodes_info(1,end)>0, + dnodes=[dnodes nodes_num]; +end +ns = nodes_info(2,:); + +bnet = mk_bnet(dag, ns, dnodes); +clamped = 0; + +bnet.CPD{1} = root_CPD(bnet, 1); + +rand('state', 50); +randn('state', 50); + +for i=2:nodes_num, + if (nodes_info(1,i)==0)&(nodes_info(4,i)==1), + bnet.CPD{i} = gaussian_CPD(bnet, i, [], [], [], 'full'); + elseif (nodes_info(1,i)==0)&(nodes_info(4,i)==2), + bnet.CPD{i} = gaussian_CPD(bnet, i, [], [], [], 'diag'); + elseif (nodes_info(1,i)==0)&(nodes_info(4,i)==3), + bnet.CPD{i} = gaussian_CPD(bnet, i, [], [], [], 'full', 'tied'); + elseif (nodes_info(1,i)==0)&(nodes_info(4,i)==4), + bnet.CPD{i} = gaussian_CPD(bnet, i, [], [], [], 'diag', 'tied'); + elseif nodes_info(1,i)==1, + %bnet.CPD{i} = dsoftmax_CPD(bnet, i, [], [], clamped, nodes_info(4,i)); + bnet.CPD{i} = softmax_CPD(bnet, i, 'clamped', clamped, 'max_iter', nodes_info(4,i)); + else + bnet.CPD{i} = mlp_CPD(bnet, i, nodes_info(3,i), [], [], [], [], clamped, nodes_info(4,i)); + end +end diff --git a/sourcecodes/bnt-master/BNT/examples/static/HME/hmemenu.m b/sourcecodes/bnt-master/BNT/examples/static/HME/hmemenu.m new file mode 100644 index 00000000..64762b8e --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/HME/hmemenu.m @@ -0,0 +1,552 @@ +% dataset -> (1=>user data) or (2=>toy example) +% type -> (1=> Regression model) or (2=>Classification model) +% num_glevel -> number of hidden nodes in the net (gating levels) +% num_exp -> number of experts in the net +% branch_fact -> dimension of the hidden nodes in the net +% cov_dim -> root node dimension +% res_dim -> output node dimension +% nodes_info -> 4 x num_glevel+2 matrix that contain all the info about the nodes: +% nodes_info(1,:) = nodes type: (0=>gaussian)or(1=>softmax)or(2=>mlp) +% nodes_info(2,:) = nodes size: [cov_dim num_glevel x branch_fact res_dim] +% nodes_info(3,:) = hidden units number (for mlp nodes) +% |- optimizer iteration number (for softmax & mlp CPD) +% nodes_info(4,:) =|- covariance type (for gaussian CPD)-> +% | (1=>Full)or(2=>Diagonal)or(3=>Full&Tied)or(4=>Diagonal&Tied) +% fh1 -> Figure: data & decizion boundaries; fh2 -> confusion matrix; fh3 -> LL trace +% test_data -> test data matrix +% train_data -> training data matrix +% ntrain -> size(train_data,2) +% ntest -> size(test_data,2) +% cases -> (cell array) training data formatted for the learning engine +% bnet -> bayesian net before learning +% bnet2 -> bayesian net after learning +% ll -> log-likelihood before learning +% LL2 -> log-likelihood trace +% onodes -> obs nodes in bnet & bnet2 +% max_em_iter -> maximum number of interations of the EM algorithm +% train_result -> prediction on the training set (as test_result) +% +% IMPORTANT: CHECK the loading path (lines 64 & 364) +% ---------------------------------------------------------------------------------------------------- +% -> pierpaolo_b@hotmail.com or -> pampo@interfree.it +% ---------------------------------------------------------------------------------------------------- + +error('this no longer works with the latest version of BNT') + +clear all; +clc; +disp('---------------------------------------------------'); +disp(' Hierarchical Mixtures of Experts models builder '); +disp('---------------------------------------------------'); +disp(' ') +disp(' Using this script you can build both an HME model') +disp('as in [Wat94] and [Jor94] i.e. with ''softmax'' gating') +disp('nodes and ''gaussian'' ( for regression ) or ''softmax''') +disp('( for classification ) expert node, and its variants') +disp('called ''gated nets'' where we use ''mlp'' models in') +disp('place of a number of ''softmax'' ones [Mor98], [Wei95].') +disp(' You can decide to train and test the model on your') +disp('datasets or to evaluate its performance on a toy') +disp('example.') +disp(' ') +disp('Reference') +disp('[Mor98] P. Moerland (1998):') +disp(' Localized mixtures of experts. (http://www.idiap.ch/~perry/)') +disp('[Jor94] M.I. Jordan, R.A. Jacobs (1994):') +disp(' HME and the EM algorithm. (http://www.cs.berkeley.edu/~jordan/)') +disp('[Wat94] S.R. Waterhouse, A.J. Robinson (1994):') +disp(' Classification using HME. (http://www.oigeeza.com/steve/)') +disp('[Wei95] A.S. Weigend, M. Mangeas (1995):') +disp(' Nonlinear gated experts for time series.') +disp(' ') + +if 0 +disp('(See the figure)') +pause(5); +%%%%%WARNING!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +im_path=which('HMEforMatlab.jpg'); +fig=imread(im_path, 'jpg'); +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +figure('Units','pixels','MenuBar','none','NumberTitle','off', 'Name', 'HME model'); +image(fig); +axis image; +axis off; +clear fig; +set(gca,'Position',[0 0 1 1]) +disp('(Press any key to continue)') +pause +end + +clc +disp('---------------------------------------------------'); +disp(' Specify the Architecture '); +disp('---------------------------------------------------'); +disp(' '); +disp('What kind of model do you need?') +disp(' ') +disp('1) Regression ') +disp('2) Classification') +disp(' ') +type=input('1 or 2?: '); +if (isempty(type)|(~ismember(type,[1 2]))), error('Invalid value'); end +clc +disp('----------------------------------------------------'); +disp(' Specify the Architecture '); +disp('----------------------------------------------------'); +disp(' ') +disp('Now you have to set the number of experts and gating') +disp('levels in the net. This script builds only balanced') +disp('hierarchy with the same branching factor (>1)at each') +disp('(gating) level. So remember that: ') +disp(' ') +disp(' num_exp = branch_fact^num_glevel ') +disp(' ') +disp('with branch_fact >=2.') +disp('You can also set to zeros the number of gating level') +disp('in order to obtain a classical GLM model. ') +disp(' ') +disp('----------------------------------------------------'); +disp(' ') +num_glevel=input('Insert the number of gating levels {0,...,20}: '); +if (isempty(num_glevel)|(~ismember(num_glevel,[0:20]))), error('Invalid value'); end +nodes_info=zeros(4,num_glevel+2); +if num_glevel>0, %------------------------------------------------------------------------------------ + for i=2:num_glevel+1, + clc + disp('----------------------------------------------------'); + disp(' Specify the Architecture '); + disp('----------------------------------------------------'); + disp(' ') + disp(['-> Gating network ', num2str(i-1), ' is a: ']) + disp(' ') + disp(' 1) Softmax model'); + disp(' 2) Two layer perceptron model') + disp(' ') + nodes_info(1,i)=input('1 or 2?: '); + if (isempty(nodes_info(1,i))|(~ismember(nodes_info(1,i),[1 2]))), error('Invalid value'); end + disp(' ') + if nodes_info(1,i)==2, + nodes_info(3,i)=input('Insert the number of units in the hidden layer: '); + if (isempty(nodes_info(3,i))|(floor(nodes_info(3,i))~=nodes_info(3,i))|(nodes_info(3,i)<=0)), + error(['Invalid value: ', num2str(nodes_info(3,i)), ' is not a positive integer!']); + end + disp(' ') + end + nodes_info(4,i)=input('Insert the optimizer iteration number: '); + if (isempty(nodes_info(4,i))|(floor(nodes_info(4,i))~=nodes_info(4,i))|(nodes_info(4,i)<=0)), + error(['Invalid value: ', num2str(nodes_info(4,i)), ' is not a positive integer!']); + end + end + clc + disp('---------------------------------------------------------'); + disp(' Specify the Architecture '); + disp('---------------------------------------------------------'); + disp(' ') + disp('Now you have to set the number of experts in the network'); + disp('The value will be adjusted in order to obtain a hierarchy'); + disp('as said above.') + disp(' '); + num_exp=input(['Insert the approximative number of experts (>=', num2str(2^num_glevel), '): ']); + if (isempty(num_exp)|(num_exp<=0)|(num_exp<2^num_glevel)), + error('Invalid value'); + end + app1=0; base=2; + while app1<num_exp, + app1=base^num_glevel; + base=base+1; + end + app2=(base-2)^num_glevel; + branch_fact=base-1; + if app2>=(2^num_glevel)&(abs(app2-num_exp)<abs(app1-num_exp)), + branch_fact=base-2; + end + clear app1 app2 base; + disp(' ') + disp(['The effective number of experts in the net is: ', num2str(branch_fact^num_glevel), '.']) + disp(' '); +else + clc + disp('---------------------------------------------------------'); + disp(' Specify the Architecture (GLM model) '); + disp('---------------------------------------------------------'); + disp(' ') +end % END of: if num_glevel>0------------------------------------------------------------------------- + +if type==2, + disp(['-> Expert node is a: ']) + disp(' ') + disp(' 1) Softmax model'); + disp(' 2) Two layer perceptron model') + disp(' ') + nodes_info(1,end)=input('1 or 2?: '); + if (isempty(nodes_info(1,end))|(~ismember(nodes_info(1,end),[1 2]))), + error('Invalid value'); + end + disp(' ') + if nodes_info(1,end)==2, + nodes_info(3,end)=input('Insert the number of units in the hidden layer: '); + if (isempty(nodes_info(3,end))|(floor(nodes_info(3,end))~=nodes_info(3,end))|(nodes_info(3,end)<=0)), + error(['Invalid value: ', num2str(nodes_info(3,end)), ' is not a positive integer!']); + end + disp(' ') + end + nodes_info(4,end)=input('Insert the optimizer iteration number: '); + if (isempty(nodes_info(4,end))|(floor(nodes_info(4,end))~=nodes_info(4,end))|(nodes_info(4,end)<=0)), + error(['Invalid value: ', num2str(nodes_info(4,end)), ' is not a positive integer!']); + end +elseif type==1, + disp('What kind of covariance matrix structure do you want?') + disp(' ') + disp(' 1) Full'); + disp(' 2) Diagonal') + disp(' 3) Full & Tied'); + disp(' 4) Diagonal & Tied') + + disp(' ') + nodes_info(4,end)=input('1, 2, 3 or 4?: '); + if (isempty(nodes_info(4,end))|(~ismember(nodes_info(4,end),[1 2 3 4]))), + error('Invalid value'); + end +end +clc +disp('----------------------------------------------------'); +disp(' Specify the Input '); +disp('----------------------------------------------------'); +disp(' ') +disp('Do you want to...') +disp(' ') +disp('1) ...use your own dataset?') +disp('2) ...apply the model on a toy example?') +disp(' ') +dataset=input('1 or 2?: '); +if (isempty(dataset)|(~ismember(dataset,[1 2]))), error('Invalid value'); end +if dataset==1, + if type==1, + clc + disp('-------------------------------------------------------'); + disp(' Specify the Input - Regression problem '); + disp('-------------------------------------------------------'); + disp(' ') + disp('Be sure that each row of your data matrix is an example'); + disp('with the covariate values that precede the respond ones') + disp(' ') + disp('-------------------------------------------------------'); + disp(' ') + cov_dim=input('Insert the covariate space dimension: '); + if (isempty(cov_dim)|(floor(cov_dim)~=cov_dim)|(cov_dim<=0)), + error(['Invalid value: ', num2str(cov_dim), ' is not a positive integer!']); + end + disp(' ') + res_dim=input('Insert the dimension of the respond variable: '); + if (isempty(res_dim)|(floor(res_dim)~=res_dim)|(res_dim<=0)), + error(['Invalid value: ', num2str(res_dim), ' is not a positive integer!']); + end + disp(' '); + elseif type==2 + clc + disp('-------------------------------------------------------'); + disp(' Specify the Input - Classification problem '); + disp('-------------------------------------------------------'); + disp(' ') + disp('Be sure that each row of your data matrix is an example'); + disp('with the covariate values that precede the class labels'); + disp('(integer value >=1). '); + disp(' ') + disp('-------------------------------------------------------'); + disp(' ') + cov_dim=input('Insert the covariate space dimension: '); + if (isempty(cov_dim)|(floor(cov_dim)~=cov_dim)|(cov_dim<=0)), + error(['Invalid value: ', num2str(cov_dim), ' is not a positive integer!']); + end + disp(' ') + res_dim=input('Insert the number of classes: '); + if (isempty(res_dim)|(floor(res_dim)~=res_dim)|(res_dim<=0)), + error(['Invalid value: ', num2str(res_dim), ' is not a positive integer!']); + end + disp(' ') + end + % ------------------------------------------------------------------------------------------------ + % Loading training data -------------------------------------------------------------------------- + % ------------------------------------------------------------------------------------------------ + train_path=input('Insert the complete (with extension) path of the training data file:\n >> ','s'); + if isempty(train_path), error('You must specify a data set for training!'); end + if ~isempty(findstr('.mat',train_path)), + ap=load(train_path); app=fieldnames(ap); train_data=eval(['ap.', app{1,1}]); + clear ap app; + elseif ~isempty(findstr('.txt',train_path)), + train_data=load(train_path, '-ascii'); + else + error('Invalid data format: not a .mat or a .txt file') + end + if (size(train_data,2)~=cov_dim+res_dim)&(type==1), + error(['Invalid data matrix size: ', num2str(size(train_data,2)), ' columns rather than ',... + num2str(cov_dim+res_dim),'!']); + elseif (size(train_data,2)~=cov_dim+1)&(type==2), + error(['Invalid data matrix size: ', num2str(size(train_data,2)), ' columns rather than ',... + num2str(cov_dim+1),'!']); + elseif (~isempty(find(ismember(intersect([train_data(:,end)' 1:res_dim],... + train_data(:,end)'),[1:res_dim])==0)))&(type==2), + error('Invalid class label'); + end + ntrain=size(train_data,1); + train_d=train_data(:,1:cov_dim); + if type==2, + train_t=zeros(ntrain, res_dim); + for m=1:res_dim, + train_t((find(train_data(:,end)==m))',m)=1; + end + else + train_t=train_data(:,cov_dim+1:end); + end + disp(' ') + % ------------------------------------------------------------------------------------------------ + % Loading test data ------------------------------------------------------------------------------ + % ------------------------------------------------------------------------------------------------ + disp('(If you don''t want to specify a test-set press ''return'' only)'); + test_path=input('Insert the complete (with extension) path of the test data file:\n >> ','s'); + if ~isempty(test_path), + if ~isempty(findstr('.mat',test_path)), + ap=load(test_path); app=fieldnames(ap); test_data=eval(['ap.', app{1,1}]); + clear ap app; + elseif ~isempty(findstr('.txt',test_path)), + test_data=load(test_path, '-ascii'); + else + error('Invalid data format: not a .mat or a .txt file') + end + if (size(test_data,2)~=cov_dim)&(size(test_data,2)~=cov_dim+res_dim)&(type==1), + error(['Invalid data matrix size: ', num2str(size(test_data,2)), ' columns rather than ',... + num2str(cov_dim+res_dim), ' or ', num2str(cov_dim), '!']); + elseif (size(test_data,2)~=cov_dim)&(size(test_data,2)~=cov_dim+1)&(type==2), + error(['Invalid data matrix size: ', num2str(size(test_data,2)), ' columns rather than ',... + num2str(cov_dim+1), ' or ', num2str(cov_dim), '!']); + elseif (~isempty(find(ismember(intersect([test_data(:,end)' 1:res_dim],... + test_data(:,end)'),[1:res_dim])==0)))&(type==2)&(size(test_data,2)==cov_dim+1), + error('Invalid class label'); + end + ntest=size(test_data,1); + test_d=test_data(:,1:cov_dim); + if (type==2)&(size(test_data,2)>cov_dim), + test_t=zeros(ntest, res_dim); + for m=1:res_dim, + test_t((find(test_data(:,end)==m))',m)=1; + end + elseif (type==1)&(size(test_data,2)>cov_dim), + test_t=test_data(:,cov_dim+1:end); + end + disp(' '); + end +else + clc + disp('----------------------------------------------------'); + disp(' Specify the Input '); + disp('----------------------------------------------------'); + disp(' ') + ntrain = input('Insert the number of examples in training (<500): '); + if (isempty(ntrain)|(floor(ntrain)~=ntrain)|(ntrain<=0)|(ntrain>500)), + error(['Invalid value: ', num2str(ntrain), ' is not a positive integer <500!']); + end + disp(' ') + test_path='toy'; + ntest = input('Insert the number of examples in test (<500): '); + if (isempty(ntest)|(floor(ntest)~=ntest)|(ntest<=0)|(ntest>500)), + error(['Invalid value: ', num2str(ntest), ' is not a positive integer <500!']); + end + + if type==2, + cov_dim=2; + res_dim=3; + seed = 42; + [train_d, ntrain1, ntrain2, train_t]=gen_data(ntrain, seed); + for m=1:ntrain + q=[]; q = find(train_t(m,:)==1); + train_data(m,:)=[train_d(m,:) q]; + end + [test_d, ntest1, ntest2, test_t]=gen_data(ntest); + for m=1:ntest + q=[]; q = find(test_t(m,:)==1); + test_data(m,:)=[test_d(m,:) q]; + end + else + cov_dim=1; + res_dim=1; + global HOME + %%%%%WARNING!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + load([HOME '/examples/static/Misc/mixexp_data.txt'], '-ascii'); + %%%%%WARNING!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + train_data = mixexp_data(1:ntrain, :); + train_d=train_data(:,1:cov_dim); train_t=train_data(:,cov_dim+1:end); + test_data = mixexp_data(ntrain+1:ntrain+ntest, :); + test_d=test_data(:,1:cov_dim); + if size(test_data,2)>cov_dim, + test_t=test_data(:,cov_dim+1:end); + end + end +end +% Set the nodes dimension----------------------------------- +if num_glevel>0, + nodes_info(2,2:num_glevel+1)=branch_fact; +end +nodes_info(2,1)=cov_dim; nodes_info(2,end)=res_dim; +%----------------------------------------------------------- +% Prepare the training data for the learning engine--------- +%----------------------------------------------------------- +cases = cell(size(nodes_info,2), ntrain); +for m=1:ntrain, + cases{1,m}=train_data(m,1:cov_dim)'; + cases{end,m}=train_data(m,cov_dim+1:end)'; +end +%----------------------------------------------------------------------------------------------------- +[bnet onodes]=hme_topobuilder(nodes_info); +engine = jtree_inf_engine(bnet, onodes); +clc +disp('---------------------------------------------------------------------'); +disp(' L E A R N I N G '); +disp('---------------------------------------------------------------------'); +disp(' ') +ll = 0; +for l=1:ntrain + scritta=['example number: ', int2str(l),'---------------------------------------------']; + disp(scritta); + ev = cases(:,l); + [engine, loglik] = enter_evidence(engine, ev); + ll = ll + loglik; +end +disp(' ') +disp(['Log-likelihood before learning: ', num2str(ll)]); +disp(' ') +disp('(Press any key to continue)'); +pause +%----------------------------------------------------------- +clc +disp('---------------------------------------------------------------------'); +disp(' L E A R N I N G '); +disp('---------------------------------------------------------------------'); +disp(' ') +max_em_iter=input('Insert the maximum number of the EM algorithm iterations: '); +if (isempty(max_em_iter)|(floor(max_em_iter)~=max_em_iter)|(max_em_iter<=1)), + error(['Invalid value: ', num2str(ntest), ' is not a positive integer >1!']); +end +disp(' ') +disp(['Log-likelihood before learning: ', num2str(ll)]); +disp(' ') + +[bnet2, LL2] = learn_params_em(engine, cases, max_em_iter); +disp(' ') +fprintf('HME: loglik before learning %f, after %d iters %f\n', ll, length(LL2), LL2(end)); +disp(' ') +disp('(Press any key to continue)'); +pause +%----------------------------------------------------------------------------------- +% Classification problem: plot data & decision boundaries if the input data size = 2 +% Regression problem: plot data & prediction if the input data size = 1 +%----------------------------------------------------------------------------------- +if (type==2)&(nodes_info(2,1)==2)&(~isempty(test_path)), + fh1=hme_class_plot(bnet2, nodes_info, train_data, test_data); + disp(' '); + disp('(See the figure)'); +elseif (type==2)&(nodes_info(2,1)==2)&(isempty(test_path)), + fh1=hme_class_plot(bnet2, nodes_info, train_data); + disp(' '); + disp('(See the figure)'); +elseif (type==1)&(nodes_info(2,1)==1)&(~isempty(test_path)), + fh1=hme_reg_plot(bnet2, nodes_info, train_data, test_data); + disp(' '); + disp('(See the figure)'); +elseif (type==1)&(nodes_info(2,1)==1)&(isempty(test_path)), + fh1=hme_reg_plot(bnet2, nodes_info, train_data); + disp(' ') + disp('(See the figure)'); +end +%----------------------------------------------------------------------------------- +% Classification problem: plot confusion matrix +%----------------------------------------------------------------------------------- +if (type==2) + ztrain=fhme(bnet2, nodes_info, train_d, size(train_d,1)); + [Htrain, trainRate]=confmat(ztrain, train_t); % CM on the training set + fh2=figure('Name','Confusion matrix', 'MenuBar', 'none', 'NumberTitle', 'off'); + if (~isempty(test_path))&(size(test_data,2)>cov_dim), + ztest=fhme(bnet2, nodes_info, test_d, size(test_d,1)); + [Htest, testRate]=confmat(ztest, test_t); % CM on the test set + subplot(1,2,1); + end + plotmat(Htrain,'b','k',12) + tick=[0.5:1:(0.5+nodes_info(2,end)-1)]; + set(gca,'XTick',tick) + set(gca,'YTick',tick) + grid('off') + ylabel('True') + xlabel('Prediction') + title(['Confusion Matrix: training set (' num2str(trainRate(1)) '%)']) + if (~isempty(test_path))&(size(test_data,2)>cov_dim), + subplot(1,2,2) + plotmat(Htest,'b','k',12) + set(gca,'XTick',tick) + set(gca,'YTick',tick) + grid('off') + ylabel('True') + xlabel('Prediction') + title(['Confusion Matrix: test set (' num2str(testRate(1)) '%)']) + end + disp(' ') + disp('(Press any key to continue)'); + pause +end +%----------------------------------------------------------------------------------- +% Regression & Classification problem: calculate the predictions & plot the LL trace +%----------------------------------------------------------------------------------- +train_result=fhme(bnet2,nodes_info,train_d,size(train_d,1)); +if ~isempty(test_path), + test_result=fhme(bnet2,nodes_info,test_d,size(test_d,1)); +end +fh3=figure('Name','Log-likelihood trace', 'MenuBar', 'none', 'NumberTitle', 'off') +plot(LL2,'-ro',... + 'MarkerEdgeColor','k',... + 'MarkerFaceColor',[1 1 0],... + 'MarkerSize',4) +title('Log-likelihood trace') +%----------------------------------------------------------------------------------- +% Regression & Classification problem: save the predictions +%----------------------------------------------------------------------------------- +clc +disp('------------------------------------------------------------------'); +disp(' Save the results '); +disp('------------------------------------------------------------------'); +disp(' ') +%----------------------------------------------------------------------------------- +save_quest_m=input('Do you want to save the HME model (Y/N)? [Y default]: ', 's'); +if isempty(save_quest_m), + save_quest_m='Y'; +end +if ~findstr(save_quest_m, ['Y', 'N']), error('Invalid input'); end +if save_quest_m=='Y', + disp(' '); + m_save=input('Insert the complete path for save the HME model (.mat):\n >> ', 's'); + if isempty(m_save), error('You must specify a path!'); end + save(m_save, 'bnet2'); +end +%----------------------------------------------------------------------------------- +disp(' ') +save_quest=input('Do you want to save the HME predictions (Y/N)? [Y default]: ', 's'); +disp(' ') +if isempty(save_quest), + save_quest='Y'; +end +if ~findstr(save_quest, ['Y', 'N']), error('Invalid input'); end +if save_quest=='Y', + tr_save=input('Insert the complete path for save the training data prediction (.mat):\n >> ', 's'); + if isempty(tr_save), error('You must specify a path!'); end + save(tr_save, 'train_result'); + if ~isempty(test_path), + disp(' ') + te_save=input('Insert the complete path for save the test data prediction (.mat):\n >> ', 's'); + if isempty(te_save), error('You must specify a path!'); end + save(te_save, 'test_result'); + end +end +clc +disp('----------------------------------------------------'); +disp(' B Y E ! '); +disp('----------------------------------------------------'); +pause(2) +%clear +clc diff --git a/sourcecodes/bnt-master/BNT/examples/static/HME/test_data_class.mat b/sourcecodes/bnt-master/BNT/examples/static/HME/test_data_class.mat new file mode 100644 index 00000000..7340c99e --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/HME/test_data_class.mat Binary files differdiff --git a/sourcecodes/bnt-master/BNT/examples/static/HME/test_data_class2.mat b/sourcecodes/bnt-master/BNT/examples/static/HME/test_data_class2.mat new file mode 100644 index 00000000..33b9aa43 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/HME/test_data_class2.mat Binary files differdiff --git a/sourcecodes/bnt-master/BNT/examples/static/HME/test_data_reg.mat b/sourcecodes/bnt-master/BNT/examples/static/HME/test_data_reg.mat new file mode 100644 index 00000000..daffe218 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/HME/test_data_reg.mat Binary files differdiff --git a/sourcecodes/bnt-master/BNT/examples/static/HME/train_data_class.mat b/sourcecodes/bnt-master/BNT/examples/static/HME/train_data_class.mat new file mode 100644 index 00000000..f181a433 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/HME/train_data_class.mat Binary files differdiff --git a/sourcecodes/bnt-master/BNT/examples/static/HME/train_data_reg.mat b/sourcecodes/bnt-master/BNT/examples/static/HME/train_data_reg.mat new file mode 100644 index 00000000..a0a2be2c --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/HME/train_data_reg.mat Binary files differdiff --git a/sourcecodes/bnt-master/BNT/examples/static/Misc/CVS/Entries b/sourcecodes/bnt-master/BNT/examples/static/Misc/CVS/Entries new file mode 100644 index 00000000..9e60b950 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/Misc/CVS/Entries @@ -0,0 +1,5 @@ +/mixexp_data.txt/1.1.1.1/Wed May 29 15:59:54 2002// +/mixexp_graddesc.m/1.1.1.1/Wed May 29 15:59:54 2002// +/mixexp_plot.m/1.1.1.1/Wed May 29 15:59:54 2002// +/sprinkler.bif/1.1.1.1/Wed May 29 15:59:54 2002// +D diff --git a/sourcecodes/bnt-master/BNT/examples/static/Misc/CVS/Repository b/sourcecodes/bnt-master/BNT/examples/static/Misc/CVS/Repository new file mode 100644 index 00000000..cd252fd6 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/Misc/CVS/Repository @@ -0,0 +1 @@ +FullBNT/BNT/examples/static/Misc diff --git a/sourcecodes/bnt-master/BNT/examples/static/Misc/CVS/Root b/sourcecodes/bnt-master/BNT/examples/static/Misc/CVS/Root new file mode 100644 index 00000000..f3bd14a6 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/Misc/CVS/Root @@ -0,0 +1 @@ +:ext:nsaunier@bnt.cvs.sourceforge.net:/cvsroot/bnt diff --git a/sourcecodes/bnt-master/BNT/examples/static/Misc/mixexp_data.txt b/sourcecodes/bnt-master/BNT/examples/static/Misc/mixexp_data.txt new file mode 100644 index 00000000..9bff9448 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/Misc/mixexp_data.txt @@ -0,0 +1,1000 @@ + 0.68 0.65 + -0.31 -0.24 + 0.85 0.94 + -0.34 -0.31 + -0.58 0.62 + -0.04 -0.11 + -0.82 0.61 + -0.38 0.16 + 0.71 0.75 + 0.80 0.95 + 0.66 0.64 + -0.11 -0.03 + -0.16 0.11 + -0.47 0.75 + 0.09 0.43 + 0.04 -0.36 + -0.80 0.77 + -0.63 0.79 + -0.13 0.46 + 0.49 0.78 + 0.93 1.12 + 0.08 -0.35 + 0.27 -0.19 + -0.66 0.56 + 0.98 1.02 + 0.13 -0.16 + -0.21 0.10 + 0.41 0.29 + 0.18 0.34 + -0.46 0.21 + 0.03 -0.24 + 0.96 1.42 + 0.96 1.45 + 0.93 1.12 + 0.08 -0.21 + -0.49 0.39 + 0.66 0.21 + -0.70 0.45 + -0.81 0.57 + -0.17 0.10 + -0.22 0.03 + 0.24 0.32 + -0.07 0.31 + -0.47 0.58 + 0.41 0.28 + -0.16 0.03 + 0.97 0.79 + -0.07 -0.09 + 0.22 0.32 + -0.71 0.73 + 0.51 0.22 + 0.21 0.57 + 0.64 0.58 + -0.65 0.74 + 0.05 0.55 + 0.41 0.67 + -0.96 1.36 + -0.35 0.03 + -0.38 -0.15 + 0.61 0.71 + -0.93 0.75 + 0.98 0.99 + 0.58 0.61 + 0.53 0.62 + -0.24 -0.04 + -0.84 1.07 + -0.35 0.42 + -0.12 0.15 + -0.38 0.18 + -0.87 1.15 + 0.38 0.38 + -0.58 0.57 + 0.42 0.48 + 0.64 0.59 + -0.52 0.54 + 0.56 0.54 + -0.22 0.21 + -0.64 0.94 + 0.92 0.99 + 0.73 0.31 + -0.04 0.39 + -0.42 0.40 + -0.73 1.08 + -0.67 0.51 + -0.51 -0.11 + -0.71 0.81 + -0.44 0.32 + 0.34 0.15 + 0.45 0.27 + 0.83 0.57 + -0.91 1.07 + -0.27 0.46 + -0.51 0.23 + -0.90 0.66 + 0.09 0.03 + 0.05 -0.16 + -0.87 0.45 + -0.43 0.61 + -0.00 0.16 + 0.48 0.49 + -0.84 1.06 + -0.45 0.34 + -0.25 0.30 + -0.91 1.03 + -0.82 0.88 + -0.45 0.23 + 0.77 0.59 + -0.40 0.30 + 0.45 0.28 + -0.56 0.17 + -0.81 1.00 + 0.68 0.81 + 0.18 0.03 + 0.64 1.01 + 0.63 0.61 + -0.16 0.48 + 0.66 0.92 + -0.22 0.14 + -0.49 0.36 + -0.66 0.92 + 0.77 0.91 + -0.10 -0.28 + -0.42 0.22 + 0.96 1.23 + 0.85 0.72 + 0.87 0.71 + 0.86 0.85 + -0.53 0.72 + -0.60 0.54 + 0.43 0.65 + -0.37 0.29 + -0.84 0.72 + -0.46 0.35 + 0.11 -0.39 + 0.72 0.97 + 0.92 0.89 + -0.32 0.29 + 0.27 0.05 + 0.45 0.44 + -0.25 0.34 + -0.45 1.16 + -0.52 0.85 + -0.78 0.90 + 0.03 -0.11 + -0.88 0.33 + -0.56 1.02 + 0.83 0.54 + -0.01 0.14 + 0.86 1.05 + -0.53 0.21 + -0.60 0.10 + 0.89 1.15 + 0.65 0.61 + -0.04 0.29 + 0.72 0.51 + -0.32 0.40 + -0.75 0.69 + 0.47 0.48 + 0.49 0.69 + 0.55 0.59 + -0.20 -0.15 + -0.52 0.36 + -0.61 1.07 + -0.02 -0.02 + -0.67 0.63 + 0.42 0.46 + -0.20 0.66 + 0.84 0.27 + 0.05 -0.12 + 0.96 0.51 + -0.71 0.72 + 0.04 -0.16 + -0.68 0.91 + -0.38 0.26 + 0.11 0.18 + 0.40 0.32 + -0.75 0.59 + -0.43 0.54 + 0.96 1.53 + -0.20 0.31 + -0.14 -0.48 + 0.84 0.50 + 0.12 -0.28 + 0.66 1.07 + -0.15 0.19 + 0.28 0.48 + 0.78 0.54 + -0.75 0.73 + 0.37 0.26 + -0.67 0.39 + -0.71 0.31 + -0.96 0.82 + 0.68 0.75 + -0.36 0.56 + 0.59 0.44 + 0.68 0.23 + 0.10 -0.04 + 0.13 0.43 + 0.28 0.52 + -0.83 0.55 + -0.41 0.38 + -0.21 -0.00 + 0.08 0.23 + 0.04 -0.02 + -0.11 0.14 + 0.40 0.34 + 0.22 0.43 + 0.30 0.19 + -0.85 0.83 + 0.13 -0.12 + -0.55 0.49 + 0.15 0.09 + 0.48 0.15 + -0.19 0.24 + -0.39 -0.11 + -0.79 0.50 + -0.53 0.73 + 0.07 -0.17 + 0.51 0.75 + 0.84 0.78 + 0.70 0.39 + 0.94 0.55 + -0.73 1.00 + 0.95 1.10 + -0.44 0.55 + -0.50 0.54 + -0.34 0.00 + 0.31 0.61 + 0.02 0.14 + 0.85 0.57 + 0.74 0.58 + -0.05 -0.14 + -0.94 0.80 + -0.59 0.18 + 0.28 -0.32 + 0.98 0.88 + 0.64 0.20 + -0.20 0.13 + 0.58 1.10 + 0.08 -0.05 + -0.39 0.66 + 0.22 0.36 + 0.07 -0.08 + -0.87 1.11 + -0.05 0.14 + 0.91 0.91 + 0.87 1.19 + -0.99 1.14 + 0.55 1.30 + -0.90 0.81 + 0.49 0.42 + 0.11 0.30 + -0.90 0.64 + -0.38 0.52 + 0.38 0.61 + -0.67 0.58 + -0.05 0.25 + 0.36 0.85 + 0.84 0.51 + 0.04 0.08 + 0.05 0.08 + -0.20 0.22 + -0.98 0.74 + 0.14 0.29 + 0.93 1.22 + 0.19 0.41 + -0.85 1.16 + 0.44 0.84 + 0.34 -0.04 + -0.33 0.16 + 0.90 0.73 + 0.58 0.39 + -0.97 0.70 + -0.18 0.09 + -1.00 0.83 + -0.75 1.07 + 0.98 1.01 + -0.94 1.11 + 0.46 1.00 + 0.64 0.38 + -0.21 -0.10 + -0.18 0.04 + -0.39 0.61 + -0.85 0.46 + -0.94 1.08 + 0.49 0.26 + -0.94 0.87 + 0.08 0.19 + -0.05 0.45 + -0.98 0.94 + 0.75 0.86 + 0.67 0.83 + 0.62 0.21 + -0.29 -0.33 + 0.64 0.42 + -0.32 0.48 + 0.39 0.34 + -0.88 1.29 + -0.03 -0.23 + 0.77 0.68 + 0.15 0.18 + -0.99 1.20 + -0.38 0.25 + -0.58 0.45 + 0.08 0.00 + 0.30 0.29 + -0.78 0.61 + 0.85 0.55 + -0.79 0.57 + 0.75 0.40 + -0.13 -0.03 + -0.34 -0.00 + -0.27 -0.03 + 0.76 1.15 + 0.12 0.66 + 0.91 1.48 + 0.10 -0.07 + -0.17 -0.18 + -0.65 0.98 + -0.54 0.18 + 0.02 0.07 + 0.15 0.43 + -0.82 0.94 + 0.73 0.40 + -0.47 0.41 + -0.32 0.59 + -0.33 0.41 + 0.66 0.70 + 0.88 0.79 + 0.24 0.29 + -0.89 1.25 + 0.83 0.84 + 0.35 0.74 + 0.16 0.32 + -0.70 0.83 + 0.71 0.58 + 0.17 -0.38 + -0.83 0.77 + 0.47 0.47 + 0.28 0.51 + 0.18 0.54 + -0.86 0.79 + -0.36 -0.00 + 0.22 -0.04 + -0.07 0.57 + 0.49 0.31 + -0.96 0.58 + -0.85 0.80 + 0.92 0.93 + -0.96 0.94 + 0.19 -0.39 + -0.33 0.07 + -0.71 0.67 + 0.30 -0.22 + -0.29 0.67 + -0.18 -0.17 + 0.68 0.52 + 0.57 0.39 + -0.17 0.41 + -0.08 0.06 + 0.89 0.86 + 0.99 0.88 + -0.69 0.71 + 0.09 0.42 + -0.95 1.04 + 0.71 0.57 + 0.67 0.95 + 0.24 0.13 + -0.34 0.22 + -0.00 -0.00 + -0.05 0.71 + 0.93 1.59 + -0.29 0.32 + 0.27 0.50 + -0.90 0.91 + 0.17 0.17 + 0.23 0.16 + 0.19 -0.16 + 0.93 0.50 + 0.11 -0.39 + 0.96 1.11 + 0.64 1.04 + -0.75 0.89 + -0.55 0.11 + 0.20 0.12 + -0.10 -0.50 + 0.40 0.75 + 0.91 0.84 + -0.60 0.31 + 0.20 0.38 + -0.85 0.87 + 0.91 1.19 + 0.27 0.42 + 0.18 -0.01 + -0.47 0.33 + 0.02 -0.30 + 0.84 1.08 + 0.75 1.10 + 0.06 -0.20 + -0.32 0.31 + -0.03 -0.19 + 0.40 0.07 + -0.33 0.20 + 0.50 0.81 + 0.57 1.11 + -0.70 0.96 + 0.09 -0.01 + -0.99 0.86 + 0.67 0.68 + 0.15 0.04 + 0.29 0.58 + 0.03 -0.25 + -0.59 0.75 + -0.98 0.48 + -0.33 0.25 + -0.10 0.51 + -0.78 0.67 + -0.89 0.84 + -0.23 0.13 + -0.55 0.20 + 0.61 0.79 + -0.34 0.31 + 0.90 0.79 + 0.15 0.60 + -0.48 0.04 + -0.89 1.15 + -0.92 1.34 + 0.91 0.87 + -0.34 0.12 + 0.08 0.51 + 0.82 0.58 + -0.66 0.72 + 0.02 -0.23 + -0.05 0.51 + 0.39 0.44 + -0.64 0.79 + 0.22 -0.30 + -0.13 0.56 + -0.03 0.14 + 0.67 0.68 + -0.02 0.05 + -0.35 0.22 + -0.50 0.57 + -0.71 0.63 + 0.70 0.73 + -0.52 1.35 + 0.51 0.38 + -0.67 0.48 + -0.34 0.56 + -0.45 0.32 + 0.82 0.73 + -0.32 -0.31 + -0.06 0.23 + -0.33 0.00 + -0.28 0.39 + 0.98 1.13 + 0.11 0.08 + -0.01 -0.05 + 0.36 0.13 + -0.35 0.47 + 0.84 1.12 + -0.45 0.14 + -0.48 0.03 + 0.01 -0.27 + 0.04 -0.14 + 0.37 0.78 + 0.58 -0.14 + 0.56 0.50 + 0.71 0.63 + -0.79 0.64 + -0.35 0.16 + 0.16 -0.10 + 0.73 1.05 + -0.11 0.23 + -0.76 0.56 + 0.88 0.71 + 0.43 0.26 + -0.33 0.51 + -0.55 0.53 + -0.17 0.23 + -0.16 -0.32 + 0.53 0.29 + 0.99 1.28 + 0.30 0.24 + -0.29 0.54 + 0.23 0.34 + 0.19 -0.00 + 0.71 1.04 + -0.35 0.39 + 0.54 0.73 + -0.25 0.03 + -0.16 0.12 + 0.43 0.51 + 0.07 0.12 + 0.58 -0.60 + -0.59 0.36 + -0.68 0.89 + -0.24 0.08 + 0.52 0.36 + 0.23 0.14 + 0.26 0.48 + 0.50 0.68 + 1.00 0.86 + 0.25 -0.21 + -0.37 0.24 + 0.13 -0.40 + 0.86 0.78 + 0.81 1.03 + 0.52 0.57 + 0.79 0.79 + -0.54 0.99 + 0.19 -0.14 + 0.09 -0.06 + -0.73 0.52 + -0.70 0.35 + -0.16 0.47 + -0.10 0.25 + -0.19 0.08 + -0.41 -0.05 + -0.71 1.07 + 0.19 -0.01 + 0.94 1.21 + 0.23 0.05 + -0.25 0.25 + -0.03 -0.48 + -0.10 0.12 + -0.59 0.73 + 0.66 1.12 + 0.36 0.29 + -0.18 0.17 + 0.99 1.20 + 0.22 -0.22 + -0.82 0.99 + -0.54 0.93 + -0.73 0.74 + 0.34 0.63 + 0.66 0.75 + -0.72 0.68 + 0.73 0.87 + 0.72 0.60 + -0.36 0.59 + -0.01 -0.03 + -0.20 0.33 + -0.22 -0.30 + 0.90 1.11 + 0.37 0.13 + -0.92 0.69 + -0.93 1.00 + 0.84 1.16 + -0.70 0.99 + -0.51 0.50 + -0.78 1.09 + -0.65 1.21 + -0.85 0.68 + 0.42 0.37 + 0.65 0.37 + -0.25 0.13 + -0.61 0.40 + -0.54 0.29 + 0.98 1.38 + -0.16 0.43 + 0.66 0.41 + -0.27 0.25 + 0.40 0.76 + 0.97 1.05 + 0.81 1.14 + -0.67 0.26 + -0.39 0.49 + 0.57 0.71 + 0.84 0.65 + 0.42 0.81 + -0.73 0.85 + 0.54 -0.01 + 0.82 0.47 + -0.20 0.51 + 0.15 0.16 + 0.79 1.09 + -0.22 0.19 + -0.78 0.89 + -0.51 0.16 + 0.69 0.56 + -0.87 0.79 + -0.95 1.23 + 0.02 -0.14 + -0.47 0.50 + 0.82 0.83 + -0.25 0.08 + -0.14 0.42 + 0.20 0.55 + 0.97 1.04 + 0.30 0.44 + -0.66 0.85 + -0.02 -0.34 + 0.02 0.34 + 0.27 -0.23 + 0.51 0.26 + -0.00 -0.15 + 0.70 0.80 + -0.74 0.83 + 0.05 -0.09 + -0.97 0.64 + 0.98 1.12 + 0.92 0.76 + -0.43 0.58 + 0.74 0.87 + 0.42 0.74 + -0.00 -0.27 + -0.78 0.59 + -0.15 0.24 + 0.19 -0.09 + -0.57 0.40 + -0.45 0.43 + -0.46 0.39 + -0.63 0.83 + -0.50 0.32 + 0.46 0.59 + -0.96 0.81 + -0.69 0.45 + -0.51 0.93 + 0.35 0.33 + -0.17 0.13 + -0.70 0.54 + -0.17 0.07 + 0.32 0.59 + -0.01 -0.17 + -0.77 0.55 + -0.35 0.25 + -0.19 -0.20 + -0.08 0.16 + 0.77 0.60 + 0.52 1.07 + 0.41 0.62 + 0.09 -0.35 + 0.24 0.74 + -0.64 0.65 + 0.96 0.80 + 0.28 0.09 + 0.51 0.54 + -0.79 0.77 + -0.73 0.96 + -0.57 0.59 + -0.75 0.78 + -0.44 0.13 + 0.61 0.40 + -0.24 0.13 + 0.64 1.05 + -0.48 0.04 + -0.87 0.61 + -0.34 0.23 + 0.23 0.17 + -0.67 0.58 + 0.33 -0.01 + -0.86 1.48 + 0.71 0.97 + 0.16 -0.33 + -0.49 0.46 + 0.21 0.66 + 0.46 0.58 + 0.99 1.08 + -0.36 0.49 + 0.09 -0.12 + -0.10 -0.35 + 0.33 0.52 + -0.97 1.56 + -0.53 0.17 + 0.31 0.38 + 0.71 0.85 + -0.56 0.81 + 0.74 0.77 + 0.48 0.65 + 0.65 1.18 + 0.70 0.73 + -0.97 0.62 + 0.75 0.32 + 0.76 0.57 + 0.32 0.49 + 0.46 0.62 + 0.95 1.14 + 0.84 0.74 + -0.78 0.37 + 0.19 0.44 + 0.39 0.44 + 0.16 0.04 + -0.88 1.11 + -0.62 0.78 + -0.20 0.06 + -0.73 0.87 + 0.49 0.56 + -0.68 0.67 + 0.31 -0.22 + 0.99 0.74 + 0.50 0.35 + -0.46 0.75 + -0.50 0.53 + -0.02 0.03 + -0.42 0.23 + 0.91 0.59 + -0.12 0.03 + -0.57 0.73 + 0.67 0.50 + -0.52 0.32 + -0.35 0.15 + -0.07 0.01 + -0.02 0.22 + 0.23 0.29 + -0.10 -0.44 + -0.22 0.24 + -0.34 -0.07 + 0.43 0.51 + 0.94 1.08 + -0.80 1.10 + 0.31 0.21 + 0.82 0.78 + -0.19 0.14 + -0.07 0.07 + -0.86 0.83 + 0.97 0.99 + -0.18 0.11 + -0.49 0.18 + -0.88 0.69 + -0.13 -0.01 + -0.29 0.19 + 0.58 0.37 + -0.64 0.28 + -0.49 0.44 + -0.85 0.72 + 0.73 0.74 + 0.43 0.32 + -0.36 0.44 + 0.30 0.08 + -0.58 0.28 + -0.40 0.40 + -0.69 0.99 + -0.54 0.30 + -0.04 0.03 + 0.13 0.33 + 0.10 0.02 + 0.20 0.42 + 0.97 0.93 + -0.98 1.33 + -0.60 0.05 + -0.11 -0.23 + -0.82 0.75 + -0.35 0.47 + 0.14 0.11 + 0.87 1.13 + -0.34 0.66 + -0.84 1.18 + -0.82 1.03 + -0.53 0.61 + 0.60 0.40 + 0.90 1.08 + -0.56 0.73 + 0.89 0.47 + -0.72 0.70 + 0.15 0.24 + 0.95 1.05 + 0.63 0.36 + -0.76 0.64 + 0.38 0.53 + 0.55 0.62 + 0.42 0.35 + -0.91 0.88 + -0.93 0.94 + -0.64 0.18 + -0.99 1.08 + -0.71 1.04 + 0.64 0.13 + -0.48 0.51 + -0.10 -0.10 + 0.67 0.86 + -0.83 0.69 + -0.25 0.18 + -0.07 0.17 + 0.13 0.11 + 0.77 1.05 + 0.01 0.34 + -0.12 0.02 + 0.50 0.62 + -0.07 -0.19 + 0.76 1.08 + -0.68 0.23 + 0.18 0.01 + -0.55 1.31 + 0.68 0.83 + -0.08 0.12 + 0.31 0.53 + 0.35 0.29 + -0.61 0.51 + -0.18 0.25 + 0.50 0.58 + -0.36 0.33 + 0.46 -0.02 + 0.72 0.96 + -0.56 0.41 + 0.73 0.96 + -0.14 -0.15 + 0.08 0.08 + 0.76 0.62 + 0.15 -0.25 + 0.23 0.13 + -0.12 0.11 + 0.12 -0.57 + -0.24 0.44 + -0.63 0.67 + -0.44 0.31 + 0.84 0.99 + -0.74 0.56 + -0.74 0.51 + 0.25 0.20 + 0.76 0.88 + 0.44 0.49 + 0.32 0.42 + -0.44 0.87 + 0.33 0.62 + -0.76 1.23 + 0.74 1.40 + 0.81 0.39 + -0.40 0.23 + 0.16 0.15 + -0.54 0.92 + -0.44 0.64 + 0.85 1.25 + 0.27 0.41 + -0.94 0.76 + 0.65 0.56 + 0.87 0.82 + -0.04 -0.10 + -0.43 0.35 + -0.78 0.77 + -0.80 0.54 + -0.04 0.23 + 0.21 0.30 + 0.71 0.64 + 0.51 0.43 + -0.38 0.33 + -0.32 0.37 + 0.77 0.95 + -0.91 0.89 + 0.79 0.70 + -0.94 0.78 + -0.05 -0.18 + 0.85 0.98 + -0.33 0.61 + -0.51 0.82 + -0.63 0.47 + -0.77 0.40 + -0.56 0.89 + 0.67 0.68 + -0.87 1.17 + -0.25 0.43 + 0.17 0.44 + -0.13 0.20 + -0.01 -0.14 + 0.87 -0.02 + 0.22 0.05 + -0.77 0.75 + -0.73 0.38 + 0.68 0.53 + -0.69 0.55 + -0.17 0.28 + -0.42 0.40 + -0.53 1.08 + -0.46 0.66 + 0.89 0.73 + -0.15 -0.02 + 0.30 0.44 + 0.42 0.43 + 0.68 0.87 + -0.66 0.84 + -0.18 -0.03 + -0.86 0.96 + -0.93 1.08 + -0.34 0.05 + 0.42 0.40 + -0.36 0.34 + -0.44 -0.30 + 0.80 0.66 + -0.01 0.24 + -0.40 -0.03 + -0.47 0.46 + -0.15 0.07 + -0.41 -0.54 + 0.25 -0.16 + 0.86 0.92 + 0.35 0.51 + 0.90 1.17 + -0.82 0.74 + -0.90 1.10 + 0.88 1.01 + 0.95 0.89 + 0.01 0.23 + 0.54 0.70 + -0.37 0.33 + -0.12 -0.71 + -0.47 0.88 + 0.24 0.47 + -0.17 0.35 + 0.68 0.56 + -0.93 0.82 + 0.60 0.86 + 0.49 0.86 + 0.78 0.81 + -0.01 0.26 + -0.96 1.34 + -0.71 0.96 + 0.17 0.37 + -0.94 0.53 + -0.42 0.77 + 0.12 -0.05 + 0.84 0.87 + -0.57 0.48 + -0.54 0.51 + -0.98 0.56 + -0.07 -0.16 + -0.49 0.47 + -0.08 0.28 + -0.59 0.89 + 0.32 0.11 + -0.59 0.63 + 0.08 0.34 + 0.24 0.21 + -0.35 0.52 + -0.18 -0.07 + 0.70 0.56 + -0.05 0.24 + -0.13 -0.36 + 0.24 0.14 + 0.54 0.55 + 0.17 -0.32 + -0.36 0.61 + -0.64 0.70 + 0.92 1.14 + 0.35 0.29 + -0.47 0.70 + -0.96 0.97 + 0.79 0.53 + 0.54 0.32 + -0.19 0.41 + -0.41 0.57 + 0.77 0.79 + -0.53 0.53 + -0.90 0.85 + 0.54 0.38 + -0.42 0.77 + 0.02 0.19 + -0.68 0.34 + -0.87 0.48 + -0.37 0.60 + -0.46 0.47 + 0.08 -0.01 + 0.51 0.64 + -0.53 0.06 + 0.87 0.82 + -0.25 0.41 + -0.46 -0.06 + 0.33 0.36 + -0.05 0.01 + -1.00 0.66 + -0.42 0.10 + 0.08 0.53 + -0.98 0.72 + 0.24 0.41 + 0.18 0.36 + -0.24 0.62 + 0.56 0.35 + 0.39 0.18 + 0.76 0.27 + 1.00 0.68 + -0.52 0.39 + -0.66 0.75 + 0.53 0.67 + 0.77 0.62 + 0.84 0.70 + 0.07 -0.28 + -0.67 0.86 + -0.75 0.92 + 0.42 0.48 + -0.32 0.59 + 0.39 0.43 + 0.79 1.31 + 0.34 0.43 + 0.48 0.86 + -0.50 0.81 + 0.94 1.67 + 0.66 0.02 + -0.28 0.02 + 0.89 1.28 + -0.74 1.16 + 0.81 0.75 + 0.96 0.12 + -0.63 0.53 + -0.86 1.20 + 0.61 0.56 + 0.53 0.95 + 0.20 0.50 + -0.07 -0.15 + 0.28 -0.11 + -0.23 0.47 + 0.02 0.29 diff --git a/sourcecodes/bnt-master/BNT/examples/static/Misc/mixexp_graddesc.m b/sourcecodes/bnt-master/BNT/examples/static/Misc/mixexp_graddesc.m new file mode 100644 index 00000000..534d9e58 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/Misc/mixexp_graddesc.m @@ -0,0 +1,51 @@ + +%%%%%%%%%% + +function [theta, eta] = mixture_of_experts(q, data, num_iter, theta, eta) +% MIXTURE_OF_EXPERTS Fit a piecewise linear regression model using stochastic gradient descent. +% [theta, eta] = mixture_of_experts(q, data, num_iter) +% +% Inputs: +% q = number of pieces (experts) +% data(l,:) = input example l +% +% Outputs: +% theta(i,:) = regression vector for expert i +% eta(i,:) = softmax (gating) params for expert i + +[num_cases dim] = size(data); +data = [ones(num_cases,1) data]; % prepend with offset +mu = 0.5; % step size +sigma = 1; % variance of noise + +if nargin < 4 + theta = 0.1*rand(q, dim); + eta = 0.1*rand(q, dim); +end + +for t=1:num_iter + for iter=1:num_cases + x = data(iter, 1:dim); + ystar = data(iter, dim+1); % target + % yhat(i) = E[y | Q=i, x] = prediction of i'th expert + yhat = theta * x'; + % gate_prior(i,:) = Pr(Q=i | x) + gate_prior = exp(eta * x'); + gate_prior = gate_prior / sum(gate_prior); + % lik(i) = Pr(y | Q=i, x) + lik = (1/(sqrt(2*pi)*sigma)) * exp(-(0.5/sigma^2) * ((ystar - yhat) .* (ystar - yhat))); + % gate_posterior(i,:) = Pr(Q=i | x, y) + gate_posterior = gate_prior .* lik; + gate_posterior = gate_posterior / sum(gate_posterior); + % Update + eta = eta + mu*(gate_posterior - gate_prior)*x; + theta = theta + mu*(gate_posterior .* (ystar - yhat))*x; + end + + if mod(t,100)==0 + fprintf(1, 'iter %d\n', t); + end + +end +fprintf(1, '\n'); + diff --git a/sourcecodes/bnt-master/BNT/examples/static/Misc/mixexp_plot.m b/sourcecodes/bnt-master/BNT/examples/static/Misc/mixexp_plot.m new file mode 100644 index 00000000..bb2a2fec --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/Misc/mixexp_plot.m @@ -0,0 +1,49 @@ +function plot_mixexp(theta, eta, data) +% PLOT_MIXEXP Plot the results for a piecewise linear regression model +% plot_mixexp(theta, eta, data) +% +% data(l,:) = [x y] for example l +% theta(i,:) = regression vector for expert i +% eta(i,:) = softmax (gating) params for expert i + +numexp = size(theta, 1); + +mn = min(data); +mx = max(data); +xa = mn(1):0.01:mx(1); +x = [ones(length(xa),1) xa']; +% pr(i,l) = posterior probability of expert i on example l +pr = exp(eta * x'); +pr = pr ./ (ones(numexp,1) * sum(pr)); +% y(i,l) = prediction of expert i for example l +y = theta * x'; +% yg(l) = weighted prediction for example l +yg = sum(y .* pr)'; + +subplot(3,2,1); +plot(xa, y(1,:)); +title('expert 1'); + +subplot(3,2,2); +plot(xa, y(2,:)); +title('expert 2'); + +subplot(3,2,3); +plot(xa, pr(1,:)); +title('gating 1'); + +subplot(3,2,4); +plot(xa, pr(2,:)); +title('gating 2'); + +subplot(3,2,5); +plot(xa, yg); +axis([-1 1 -1 2]) +title('prediction'); + +subplot(3,2,6); +title('data'); +hold on +plot(data(:,1), data(:,2), '+'); +hold off + diff --git a/sourcecodes/bnt-master/BNT/examples/static/Misc/sprinkler.bif b/sourcecodes/bnt-master/BNT/examples/static/Misc/sprinkler.bif new file mode 100644 index 00000000..8925e79c --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/Misc/sprinkler.bif @@ -0,0 +1,18 @@ +network Grass + {} +variable Cloudy + { type discrete[2] {false true}; } +variable Sprinkler + { type discrete[2] {false true}; } +variable Rain + { type discrete[2] {false true}; } +variable WetGrass + { type discrete[2] {false true}; } +probability (Cloudy) + { table 0.5 0.5; } +probability (Sprinkler | Cloudy) + { table 0.5 0.9 0.5 0.1; } +probability (Rain | Cloudy) + { table 0.8 0.2 0.2 0.8; } +probability (WetGrass | Rain Sprinkler) + { table 1.0 0.1 0.1 0.01 0.0 0.9 0.9 0.99; } diff --git a/sourcecodes/bnt-master/BNT/examples/static/Models/CVS/Entries b/sourcecodes/bnt-master/BNT/examples/static/Models/CVS/Entries new file mode 100644 index 00000000..398be87c --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/Models/CVS/Entries @@ -0,0 +1,12 @@ +/mk_alarm_bnet.m/1.1.1.1/Sun Nov 3 16:44:14 2002// +/mk_asia_bnet.m/1.1.1.1/Wed Mar 26 00:06:42 2003// +/mk_cancer_bnet.m/1.1.1.1/Wed May 29 15:59:54 2002// +/mk_car_bnet.m/1.1.1.1/Wed May 29 15:59:54 2002// +/mk_hmm_bnet.m/1.1.1.1/Thu Jan 15 01:06:12 2004// +/mk_ideker_bnet.m/1.1.1.1/Wed May 29 15:59:54 2002// +/mk_incinerator_bnet.m/1.1.1.1/Wed May 29 15:59:54 2002// +/mk_markov_chain_bnet.m/1.1.1.1/Wed May 29 15:59:54 2002// +/mk_minimal_qmr_bnet.m/1.1.1.1/Wed May 29 15:59:54 2002// +/mk_qmr_bnet.m/1.1.1.1/Wed May 29 15:59:54 2002// +/mk_vstruct_bnet.m/1.1.1.1/Wed May 29 15:59:54 2002// +D/Old//// diff --git a/sourcecodes/bnt-master/BNT/examples/static/Models/CVS/Repository b/sourcecodes/bnt-master/BNT/examples/static/Models/CVS/Repository new file mode 100644 index 00000000..2218a7f5 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/Models/CVS/Repository @@ -0,0 +1 @@ +FullBNT/BNT/examples/static/Models diff --git a/sourcecodes/bnt-master/BNT/examples/static/Models/CVS/Root b/sourcecodes/bnt-master/BNT/examples/static/Models/CVS/Root new file mode 100644 index 00000000..f3bd14a6 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/Models/CVS/Root @@ -0,0 +1 @@ +:ext:nsaunier@bnt.cvs.sourceforge.net:/cvsroot/bnt diff --git a/sourcecodes/bnt-master/BNT/examples/static/Models/Old/CVS/Entries b/sourcecodes/bnt-master/BNT/examples/static/Models/Old/CVS/Entries new file mode 100644 index 00000000..c7e92b5c --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/Models/Old/CVS/Entries @@ -0,0 +1,2 @@ +/mk_hmm_bnet.m/1.1.1.1/Wed May 29 15:59:54 2002// +D diff --git a/sourcecodes/bnt-master/BNT/examples/static/Models/Old/CVS/Repository b/sourcecodes/bnt-master/BNT/examples/static/Models/Old/CVS/Repository new file mode 100644 index 00000000..fdee291b --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/Models/Old/CVS/Repository @@ -0,0 +1 @@ +FullBNT/BNT/examples/static/Models/Old diff --git a/sourcecodes/bnt-master/BNT/examples/static/Models/Old/CVS/Root b/sourcecodes/bnt-master/BNT/examples/static/Models/Old/CVS/Root new file mode 100644 index 00000000..f3bd14a6 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/Models/Old/CVS/Root @@ -0,0 +1 @@ +:ext:nsaunier@bnt.cvs.sourceforge.net:/cvsroot/bnt diff --git a/sourcecodes/bnt-master/BNT/examples/static/Models/Old/mk_hmm_bnet.m b/sourcecodes/bnt-master/BNT/examples/static/Models/Old/mk_hmm_bnet.m new file mode 100644 index 00000000..1179c6d8 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/Models/Old/mk_hmm_bnet.m @@ -0,0 +1,58 @@ +function [bnet, onodes] = mk_hmm_bnet(T, Q, O, cts_obs, param_tying) +% MK_HMM_BNET Make a (static( bnet to represent a hidden Markov model +% [bnet, onodes] = mk_hmm_bnet(T, Q, O, cts_obs, param_tying) +% +% T = num time slices +% Q = num hidden states +% O = size of the observed node (num discrete values or length of vector) +% cts_obs - 1 means the observed node is a continuous-valued vector, 0 means it's discrete +% param_tying - 1 means we create 3 CPDs, 0 means we create 1 CPD per node + +N = 2*T; +dag = zeros(N); +for i=1:T-1 + dag(i,i+1)=1; +end +onodes = T+1:N; +for i=1:T + dag(i, onodes(i)) = 1; +end + +if cts_obs + dnodes = 1:T; +else + dnodes = 1:N; +end +ns = [Q*ones(1,T) O*ones(1,T)]; + +if param_tying + eclass = [1 2*ones(1,T-1) 3*ones(1,T)]; +else + eclass = 1:N; +end + +bnet = mk_bnet(dag, ns, dnodes, eclass); + +hnodes = mysetdiff(1:N, onodes); +if ~param_tying + for i=hnodes(:)' + bnet.CPD{i} = tabular_CPD(bnet, i); + end + if cts_obs + for i=onodes(:)' + bnet.CPD{i} = gaussian_CPD(bnet, i); + end + else + for i=onodes(:)' + bnet.CPD{i} = tabular_CPD(bnet, i); + end + end +else + bnet.CPD{1} = tabular_CPD(bnet, 1); + bnet.CPD{2} = tabular_CPD(bnet, 2); + if cts_obs + bnet.CPD{3} = gaussian_CPD(bnet, 3); + else + bnet.CPD{3} = tabular_CPD(bnet, 3); + end +end diff --git a/sourcecodes/bnt-master/BNT/examples/static/Models/mk_alarm_bnet.m b/sourcecodes/bnt-master/BNT/examples/static/Models/mk_alarm_bnet.m new file mode 100644 index 00000000..5705909b --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/Models/mk_alarm_bnet.m @@ -0,0 +1,118 @@ +function bnet = mk_alarm_bnet() + +% Written by Qian Diao <qian.diao@intel.com> on 11 Dec 01 + +N = 37; +dag = zeros(N,N); +dag(21,23) = 1 ; +dag(21,24) = 1 ; +dag(1,24) = 1 ; +dag(1,23) = 1 ; +dag(2,26) = 1 ; +dag(2,25) = 1 ; +dag(2,24) = 1 ; +dag(2,13) = 1 ; +dag(2,23) = 1 ; +dag(13,30) = 1 ; +dag(30,31) = 1 ; +dag(3,14) = 1 ; +dag(3,19) = 1 ; +dag(4,36) = 1 ; +dag(14,35) = 1 ; +dag(32,33) = 1 ; +dag(32,35) = 1 ; +dag(32,34) = 1 ; +dag(32,36) = 1 ; +dag(15,21) = 1 ; +dag(5,31) = 1 ; +dag(27,30) = 1 ; +dag(28,31) = 1 ; +dag(28,29) = 1 ; +dag(26,28) = 1 ; +dag(26,27) = 1 ; +dag(16,31) = 1 ; +dag(16,37) = 1 ; +dag(23,26) = 1 ; +dag(23,29) = 1 ; +dag(23,25) = 1 ; +dag(6,15) = 1 ; +dag(7,27) = 1 ; +dag(8,21) = 1 ; +dag(19,20) = 1 ; +dag(19,22) = 1 ; +dag(31,32) = 1 ; +dag(9,14) = 1 ; +dag(9,17) = 1 ; +dag(9,19) = 1 ; +dag(10,33) = 1 ; +dag(10,34) = 1 ; +dag(11,16) = 1 ; +dag(12,13) = 1 ; +dag(12,18) = 1 ; +dag(35,37) = 1 ; + +node_sizes = 2*ones(1,N); +node_sizes(2) = 3; +node_sizes(6) = 3; +node_sizes(14) = 3; +node_sizes(15) = 4; +node_sizes(16) = 3; +node_sizes(18) = 3; +node_sizes(19) = 3; +node_sizes(20) = 3; +node_sizes(21) = 4; +node_sizes(22) = 3; +node_sizes(23) = 4; +node_sizes(24) = 4; +node_sizes(25) = 4; +node_sizes(26) = 4; +node_sizes(27) = 3; +node_sizes(28) = 3; +node_sizes(29) = 4; +node_sizes(30) = 3; +node_sizes(32) = 3; +node_sizes(33) = 3; +node_sizes(34) = 3; +node_sizes(35) = 3; +node_sizes(36) = 3; +node_sizes(37) = 3; + +bnet = mk_bnet(dag, node_sizes); + +bnet.CPD{1} = tabular_CPD(bnet, 1,[0.96 0.04 ]); +bnet.CPD{2} = tabular_CPD(bnet, 2,[0.92 0.03 0.05 ]); +bnet.CPD{3} = tabular_CPD(bnet, 3,[0.8 0.2 ]); +bnet.CPD{4} = tabular_CPD(bnet, 4,[0.95 0.05 ]); +bnet.CPD{5} = tabular_CPD(bnet, 5,[0.8 0.2 ]); +bnet.CPD{6} = tabular_CPD(bnet, 6,[0.01 0.98 0.01 ]); +bnet.CPD{7} = tabular_CPD(bnet, 7,[0.01 0.99 ]); +bnet.CPD{8} = tabular_CPD(bnet, 8,[0.95 0.05 ]); +bnet.CPD{9} = tabular_CPD(bnet, 9,[0.95 0.05 ]); +bnet.CPD{10} = tabular_CPD(bnet, 10,[0.9 0.1 ]); +bnet.CPD{11} = tabular_CPD(bnet, 11,[0.99 0.01 ]); +bnet.CPD{12} = tabular_CPD(bnet, 12,[0.99 0.01 ]); +bnet.CPD{13} = tabular_CPD(bnet, 13,[0.95 0.95 0.05 0.1 0.1 0.01 0.05 0.05 0.95 0.9 0.9 0.99 ]); +bnet.CPD{14} = tabular_CPD(bnet, 14,[0.05 0.95 0.5 0.98 0.9 0.04 0.49 0.01 0.05 0.01 0.01 0.01 ]); +bnet.CPD{15} = tabular_CPD(bnet, 15,[0.01 0.01 0.01 0.97 0.01 0.01 0.01 0.97 0.01 0.01 0.01 0.97 ]); +bnet.CPD{16} = tabular_CPD(bnet, 16,[0.3 0.98 0.4 0.01 0.3 0.01 ]); +bnet.CPD{17} = tabular_CPD(bnet, 17,[0.99 0.1 0.01 0.9 ]); +bnet.CPD{18} = tabular_CPD(bnet, 18,[0.05 0.01 0.9 0.19 0.05 0.8 ]); +bnet.CPD{19} = tabular_CPD(bnet, 19,[0.05 0.98 0.01 0.95 0.9 0.01 0.09 0.04 0.05 0.01 0.9 0.01 ]); +bnet.CPD{20} = tabular_CPD(bnet, 20,[0.95 0.04 0.01 0.04 0.95 0.29 0.01 0.01 0.7 ]); +bnet.CPD{21} = tabular_CPD(bnet, 21,[0.97 0.97 0.01 0.97 0.01 0.97 0.01 0.97 0.01 0.01 0.97 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.97 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.97 0.01 ]); +bnet.CPD{22} = tabular_CPD(bnet, 22,[0.95 0.04 0.01 0.04 0.95 0.04 0.01 0.01 0.95 ]); +bnet.CPD{23} = tabular_CPD(bnet, 23,[0.97 0.97 0.97 0.97 0.97 0.97 0.01 0.95 0.97 0.97 0.01 0.95 0.01 0.4 0.97 0.97 0.01 0.5 0.01 0.3 0.97 0.97 0.01 0.3 0.01 0.01 0.01 0.01 0.01 0.01 0.97 0.03 0.01 0.01 0.97 0.03 0.01 0.58 0.01 0.01 0.01 0.48 0.01 0.68 0.01 0.01 0.01 0.68 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.97 0.01 0.01 0.01 0.97 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.97 0.01 0.01 0.01 0.97 0.01 ]); +bnet.CPD{24} = tabular_CPD(bnet, 24,[0.97 0.97 0.97 0.97 0.97 0.97 0.01 0.01 0.4 0.1 0.01 0.01 0.01 0.01 0.2 0.05 0.01 0.01 0.01 0.01 0.2 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.97 0.49 0.58 0.84 0.9 0.29 0.01 0.01 0.75 0.25 0.01 0.01 0.01 0.01 0.7 0.15 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.3 0.01 0.05 0.08 0.3 0.97 0.08 0.04 0.25 0.38 0.08 0.01 0.01 0.09 0.25 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.2 0.01 0.01 0.01 0.4 0.01 0.9 0.01 0.45 0.6 0.9 0.97 0.97 0.01 0.59 0.97 0.97 ]); +bnet.CPD{25} = tabular_CPD(bnet, 25,[0.97 0.97 0.97 0.01 0.6 0.01 0.01 0.5 0.01 0.01 0.5 0.01 0.01 0.01 0.01 0.97 0.38 0.97 0.01 0.48 0.01 0.01 0.48 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.97 0.01 0.97 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.97 0.01 0.97 ]); +bnet.CPD{26} = tabular_CPD(bnet, 26,[0.97 0.97 0.97 0.01 0.01 0.03 0.01 0.01 0.01 0.97 0.01 0.01 0.01 0.01 0.01 0.97 0.97 0.95 0.01 0.01 0.94 0.01 0.01 0.88 0.01 0.01 0.01 0.01 0.01 0.01 0.97 0.97 0.04 0.01 0.01 0.1 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.97 0.97 0.01 ]); +bnet.CPD{27} = tabular_CPD(bnet, 27,[0.98 0.98 0.98 0.98 0.95 0.01 0.95 0.01 0.01 0.01 0.01 0.01 0.04 0.95 0.04 0.01 0.01 0.01 0.01 0.01 0.01 0.04 0.01 0.98 ]); +bnet.CPD{28} = tabular_CPD(bnet, 28,[0.01 0.01 0.04 0.9 0.01 0.01 0.92 0.09 0.98 0.98 0.04 0.01 ]); +bnet.CPD{29} = tabular_CPD(bnet, 29,[0.97 0.01 0.01 0.01 0.97 0.01 0.01 0.01 0.97 0.01 0.01 0.01 0.01 0.97 0.97 0.97 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.97 0.97 0.97 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.97 0.97 0.43 ]); +bnet.CPD{30} = tabular_CPD(bnet, 30,[0.98 0.98 0.01 0.98 0.01 0.69 0.01 0.01 0.98 0.01 0.01 0.3 0.01 0.01 0.01 0.01 0.98 0.01 ]); +bnet.CPD{31} = tabular_CPD(bnet, 31,[0.05 0.01 0.05 0.01 0.05 0.01 0.05 0.01 0.05 0.01 0.05 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.1 0.01 0.95 0.01 0.95 0.05 0.1 0.01 0.95 0.01 0.95 0.05 0.1 0.01 0.3 0.01 0.3 0.01 0.95 0.01 0.99 0.05 0.95 0.05 0.95 0.01 0.99 0.05 0.99 0.05 0.3 0.01 0.99 0.01 0.3 0.01 0.95 0.99 0.95 0.99 0.95 0.99 0.95 0.99 0.95 0.99 0.95 0.99 0.99 0.99 0.99 0.99 0.99 0.99 0.9 0.99 0.05 0.99 0.05 0.95 0.9 0.99 0.05 0.99 0.05 0.95 0.9 0.99 0.7 0.99 0.7 0.99 0.05 0.99 0.00999999 0.95 0.05 0.95 0.05 0.99 0.01 0.95 0.01 0.95 0.7 0.99 0.01 0.99 0.7 0.99 ]); +bnet.CPD{32} = tabular_CPD(bnet, 32,[0.1 0.01 0.89 0.09 0.01 0.9 ]); +bnet.CPD{33} = tabular_CPD(bnet, 33,[0.98 0.33333334 0.01 0.33333334 0.01 0.33333334 0.01 0.33333334 0.98 0.33333334 0.01 0.33333334 0.01 0.33333334 0.01 0.33333334 0.98 0.33333334 ]); +bnet.CPD{34} = tabular_CPD(bnet, 34,[0.98 0.33333334 0.01 0.33333334 0.01 0.33333334 0.01 0.33333334 0.98 0.33333334 0.01 0.33333334 0.01 0.33333334 0.01 0.33333334 0.98 0.33333334 ]); +bnet.CPD{35} = tabular_CPD(bnet, 35,[0.98 0.95 0.3 0.95 0.04 0.01 0.8 0.01 0.01 0.01 0.04 0.69 0.04 0.95 0.3 0.19 0.04 0.01 0.01 0.01 0.01 0.01 0.01 0.69 0.01 0.95 0.98 ]); +bnet.CPD{36} = tabular_CPD(bnet, 36,[0.98 0.98 0.01 0.4 0.01 0.3 0.01 0.01 0.98 0.59 0.01 0.4 0.01 0.01 0.01 0.01 0.98 0.3 ]); +bnet.CPD{37} = tabular_CPD(bnet, 37,[0.98 0.98 0.3 0.98 0.1 0.05 0.9 0.05 0.01 0.01 0.01 0.6 0.01 0.85 0.4 0.09 0.2 0.09 0.01 0.01 0.1 0.01 0.05 0.55 0.01 0.75 0.9 ]); diff --git a/sourcecodes/bnt-master/BNT/examples/static/Models/mk_asia_bnet.m b/sourcecodes/bnt-master/BNT/examples/static/Models/mk_asia_bnet.m new file mode 100644 index 00000000..fce24c3a --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/Models/mk_asia_bnet.m @@ -0,0 +1,76 @@ +function bnet = mk_asia_bnet(CPD_type, p, arity) +% MK_ASIA_BNET Make the 'Asia' bayes net. +% +% BNET = MK_ASIA_BNET uses the parameters specified on p21 of Cowell et al, +% "Probabilistic networks and expert systems", Springer Verlag 1999. +% +% BNET = MK_ASIA_BNET('cpt', p) uses random parameters drawn from a Dirichlet(p,p,...) +% distribution. If p << 1, this is nearly deterministic; if p >> 1, this is nearly uniform. +% +% BNET = MK_ASIA_BNET('bool') makes each CPT a random boolean function. +% +% BNET = MK_ASIA_BNET('gauss') makes each CPT a random linear Gaussian distribution. +% +% BNET = MK_ASIA_BNET('orig') is the same as MK_ASIA_BNET. +% +% BNET = MK_ASIA_BNET('cpt', p, arity) can specify non-binary nodes. + + +if nargin == 0, CPD_type = 'orig'; end +if nargin < 3, arity = 2; end + +Smoking = 1; +Bronchitis = 2; +LungCancer = 3; +VisitToAsia = 4; +TB = 5; +TBorCancer = 6; +Dys = 7; +Xray = 8; + +n = 8; +dag = zeros(n); +dag(Smoking, [Bronchitis LungCancer]) = 1; +dag(Bronchitis, Dys) = 1; +dag(LungCancer, TBorCancer) = 1; +dag(VisitToAsia, TB) = 1; +dag(TB, TBorCancer) = 1; +dag(TBorCancer, [Dys Xray]) = 1; + +ns = arity*ones(1,n); +if strcmp(CPD_type, 'gauss') + dnodes = []; +else + dnodes = 1:n; +end +bnet = mk_bnet(dag, ns, 'discrete', dnodes); + +switch CPD_type + case 'orig', + % true is 2, false is 1 + bnet.CPD{VisitToAsia} = tabular_CPD(bnet, VisitToAsia, [0.99 0.01]); + bnet.CPD{Bronchitis} = tabular_CPD(bnet, Bronchitis, [0.7 0.4 0.3 0.6]); + % minka: bug fix + bnet.CPD{Dys} = tabular_CPD(bnet, Dys, [0.9 0.2 0.3 0.1 0.1 0.8 0.7 0.9]); + bnet.CPD{TBorCancer} = tabular_CPD(bnet, TBorCancer, [1 0 0 0 0 1 1 1]); + % minka: bug fix + bnet.CPD{LungCancer} = tabular_CPD(bnet, LungCancer, [0.99 0.9 0.01 0.1]); + bnet.CPD{Smoking} = tabular_CPD(bnet, Smoking, [0.5 0.5]); + bnet.CPD{TB} = tabular_CPD(bnet, TB, [0.99 0.95 0.01 0.05]); + bnet.CPD{Xray} = tabular_CPD(bnet, Xray, [0.95 0.02 0.05 0.98]); + case 'bool', + for i=1:n + bnet.CPD{i} = boolean_CPD(bnet, i, 'rnd'); + end + case 'gauss', + for i=1:n + bnet.CPD{i} = gaussian_CPD(bnet, i, 'cov', 1*eye(ns(i))); + end + case 'cpt', + for i=1:n + bnet.CPD{i} = tabular_CPD(bnet, i, p); + end +end + + + diff --git a/sourcecodes/bnt-master/BNT/examples/static/Models/mk_cancer_bnet.m b/sourcecodes/bnt-master/BNT/examples/static/Models/mk_cancer_bnet.m new file mode 100644 index 00000000..c54cbfad --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/Models/mk_cancer_bnet.m @@ -0,0 +1,61 @@ +function bnet = mk_cancer_bnet(CPD_type, p) +% MK_CANCER_BNET Make the 'Cancer' Bayes net. +% +% BNET = MK_CANCER_BNET uses the noisy-or parameters specified in Fig 4a of the UAI98 paper by +% Friedman, Murphy and Russell, "Learning the Structure of DPNs", p145. +% +% BNET = MK_CANCER_BNET('noisyor', p) makes each CPD a noisy-or, with probability p of +% suppression for each parent; leaks are turned off. +% +% BNET = MK_CANCER_BNET('cpt', p) uses random CPT parameters drawn from a Dirichlet(p,p,...) +% distribution. If p << 1, this is near deterministic; if p >> 1, this is near 1/k. +% p defaults to 1.0 (uniform distribution). +% +% BNET = MK_CANCER_BNET('bool') makes each CPT a random boolean function. +% +% In all cases, the root is set to a uniform distribution. + +if nargin == 0 + rnd = 0; +else + rnd = 1; +end + +n = 5; +dag = zeros(n); +dag(1,[2 3]) = 1; +dag(2,4) = 1; +dag(3,4) = 1; +dag(4,5) = 1; + +ns = 2*ones(1,n); +bnet = mk_bnet(dag, ns); + +if ~rnd + bnet.CPD{1} = tabular_CPD(bnet, 1, [0.5 0.5]); + bnet.CPD{2} = noisyor_CPD(bnet, 2, 1.0, 1-0.9); + bnet.CPD{3} = noisyor_CPD(bnet, 3, 1.0, 1-0.2); + bnet.CPD{4} = noisyor_CPD(bnet, 4, 1.0, 1-[0.7 0.6]); + bnet.CPD{5} = noisyor_CPD(bnet, 5, 1.0, 1-0.5); +else + switch CPD_type + case 'noisyor', + for i=1:n + ps = parents(dag, i); + bnet.CPD{i} = noisyor_CPD(bnet, i, 1.0, p*ones(1,length(ps))); + end + case 'bool', + for i=1:n + bnet.CPD{i} = boolean_CPD(bnet, i, 'rnd'); + end + case 'cpt', + for i=1:n + bnet.CPD{i} = tabular_CPD(bnet, i, p); + end + otherwise + error(['bad CPD type ' CPD_type]); + end +end + + + diff --git a/sourcecodes/bnt-master/BNT/examples/static/Models/mk_car_bnet.m b/sourcecodes/bnt-master/BNT/examples/static/Models/mk_car_bnet.m new file mode 100644 index 00000000..c9a27c9c --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/Models/mk_car_bnet.m @@ -0,0 +1,39 @@ +function bnet = mk_car_bnet() +% MK_CAR_BNET Make the car trouble-shooter bayes net. +% +% This network is from p13 of "Troubleshooting under uncertainty", Heckerman, Breese and +% Rommelse, Microsoft Research Tech Report 1994. + + +BatteryAge = 1; +Battery = 2; +Starter = 3; +Lights = 4; +TurnsOver = 5; +FuelPump = 6; +FuelLine = 7; +FuelSubsys =8; +Fuel = 9; +Spark = 10; +Starts = 11; +Gauge = 12; + +n = 12; +dag = zeros(n); +dag(1,2) = 1; +dag(2,[4 5])=1; +dag(3,5) = 1; +dag(6,8) = 1; +dag(7,8) = 1; +dag(8,11) = 1; +dag(9,12) = 1; +dag(10,11) = 1; + +arity = 2; +ns = arity*ones(1,n); +bnet = mk_bnet(dag, ns); +for i=1:n + bnet.CPD{i} = tabular_CPD(bnet, i); +end + + diff --git a/sourcecodes/bnt-master/BNT/examples/static/Models/mk_hmm_bnet.m b/sourcecodes/bnt-master/BNT/examples/static/Models/mk_hmm_bnet.m new file mode 100644 index 00000000..6e2dbfba --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/Models/mk_hmm_bnet.m @@ -0,0 +1,67 @@ +function bnet = mk_hmm_bnet(T, Q, O, cts_obs, param_tying) +% MK_HMM_BNET Make a (static) bnet to represent a hidden Markov model +% bnet = mk_hmm_bnet(T, Q, O, cts_obs, param_tying) +% +% T = num time slices +% Q = num hidden states +% O = size of the observed node (num discrete values or length of vector) +% cts_obs - 1 means the observed node is a continuous-valued vector, 0 means it's discrete +% param_tying - 1 means we create 3 CPDs, 0 means we create 1 CPD per node + +N = 2*T; +dag = zeros(N); +%hnodes = 1:2:2*T; +hnodes = 1:T; +for i=1:T-1 + dag(hnodes(i), hnodes(i+1))=1; +end +%onodes = 2:2:2*T; +onodes = T+1:2*T; +for i=1:T + dag(hnodes(i), onodes(i)) = 1; +end + +if cts_obs + dnodes = hnodes; +else + dnodes = 1:N; +end +ns = ones(1,N); +ns(hnodes) = Q; +ns(onodes) = O; + +if param_tying + H1class = 1; Hclass = 2; Oclass = 3; + eclass = ones(1,N); + eclass(hnodes(2:end)) = Hclass; + eclass(hnodes(1)) = H1class; + eclass(onodes) = Oclass; +else + eclass = 1:N; +end + +bnet = mk_bnet(dag, ns, 'observed', onodes, 'discrete', dnodes, 'equiv_class', eclass); + +hnodes = mysetdiff(1:N, onodes); +if ~param_tying + for i=hnodes(:)' + bnet.CPD{i} = tabular_CPD(bnet, i); + end + if cts_obs + for i=onodes(:)' + bnet.CPD{i} = gaussian_CPD(bnet, i); + end + else + for i=onodes(:)' + bnet.CPD{i} = tabular_CPD(bnet, i); + end + end +else + bnet.CPD{H1class} = tabular_CPD(bnet, hnodes(1)); % prior + bnet.CPD{Hclass} = tabular_CPD(bnet, hnodes(2)); % transition matrix + if cts_obs + bnet.CPD{Oclass} = gaussian_CPD(bnet, onodes(1)); + else + bnet.CPD{Oclass} = tabular_CPD(bnet, onodes(1)); + end +end diff --git a/sourcecodes/bnt-master/BNT/examples/static/Models/mk_ideker_bnet.m b/sourcecodes/bnt-master/BNT/examples/static/Models/mk_ideker_bnet.m new file mode 100644 index 00000000..67f95bae --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/Models/mk_ideker_bnet.m @@ -0,0 +1,52 @@ +function bnet = mk_ideker_bnet(CPD_type, p) +% MK_IDEKER_BNET Make the Bayes net in the PSB'00 paper by Ideker, Thorsson and Karp. +% +% BNET = MK_IDEKER_BNET uses the boolean functions specified in the paper +% "Discovery of regulatory interactions through perturbation: inference and experimental design", +% Pacific Symp. on Biocomputing, 2000. +% +% BNET = MK_IDEKER_BNET('root') uses the above boolean functions, but puts a uniform +% distribution on the root nodes. +% +% BNET = MK_IDEKER_BNET('cpt', p) uses random parameters drawn from a Dirichlet(p,p,...) +% distribution. If p << 1, this is nearly deterministic; if p >> 1, this is nearly uniform. +% +% BNET = MK_IDEKER_BNET('bool') makes each CPT a random boolean function. +% +% BNET = MK_IDEKER_BNET('orig') is the same as MK_IDEKER_BNET. + + +if nargin == 0 + CPD_type = 'orig'; +end + +n = 4; +dag = zeros(n); +dag(1,3)=1; +dag(2,[3 4])=1; +dag(3,4)=1; +ns = 2*ones(1,n); +bnet = mk_bnet(dag, ns); + +switch CPD_type + case 'orig', + bnet.CPD{1} = tabular_CPD(bnet, 1, [0 1]); + bnet.CPD{2} = tabular_CPD(bnet, 2, [0 1]); + bnet.CPD{3} = boolean_CPD(bnet, 3, 'inline', inline('x(1) & x(2)')); + bnet.CPD{4} = boolean_CPD(bnet, 4, 'inline', inline('x(1) & ~x(2)')); + case 'root', + bnet.CPD{1} = tabular_CPD(bnet, 1, [0.5 0.5]); + bnet.CPD{2} = tabular_CPD(bnet, 2, [0.5 0.5]); + bnet.CPD{3} = boolean_CPD(bnet, 3, 'inline', inline('x(1) & x(2)')); + bnet.CPD{4} = boolean_CPD(bnet, 4, 'inline', inline('x(1) & ~x(2)')); + case 'bool', + for i=1:n + bnet.CPD{i} = boolean_CPD(bnet, i, 'rnd'); + end + case 'cpt', + for i=1:n + bnet.CPD{i} = tabular_CPD(bnet, i, p); + end + otherwise, + error(['unknown type ' CPD_type]); +end diff --git a/sourcecodes/bnt-master/BNT/examples/static/Models/mk_incinerator_bnet.m b/sourcecodes/bnt-master/BNT/examples/static/Models/mk_incinerator_bnet.m new file mode 100644 index 00000000..1583ad17 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/Models/mk_incinerator_bnet.m @@ -0,0 +1,61 @@ +function bnet = mk_incinerator_bnet(ns) +% MK_INCINERATOR_BNET The waste incinerator emissions example from Cowell et al p145 +% function bnet = mk_incinerator_bnet(ns) +% +% If ns is omitted, we use the scalars and binary nodes and the original params. +% Otherwise, we use random params of the desired size. +% +% Lauritzen, "Propogation of Probabilities, Means and Variances in Mixed Graphical Association Models", +% JASA 87(420): 1098--1108 +% This example is reprinted on p145 of "Probabilistic Networks and Expert Systems", +% Cowell, Dawid, Lauritzen and Spiegelhalter, 1999, Springer. +% For a picture, see http://www.cs.berkeley.edu/~murphyk/Bayes/usage.html#cg_model + +% node numbers +F = 1; W = 2; E = 3; B = 4; C = 5; D = 6; Min = 7; Mout = 8; L = 9; +names = {'F', 'W', 'E', 'B', 'C', 'D', 'Min', 'Mout', 'L'}; +n = 9; +dnodes = [F W B]; +cnodes = mysetdiff(1:n, dnodes); + +% node sizes - all cts nodes are scalar, all discrete nodes are binary +if nargin < 1 + ns = ones(1, n); + ns(dnodes) = 2; + rnd = 0; +else + rnd = 1; +end + +% topology (p 1099, fig 1) +dag = zeros(n); +dag(F,E)=1; +dag(W,[E Min D]) = 1; +dag(E,D)=1; +dag(B,[C D])=1; +dag(D,[L Mout])=1; +dag(Min,Mout)=1; + +% params (p 1102) +bnet = mk_bnet(dag, ns, 'discrete', dnodes, 'names', names); + +if rnd + for i=dnodes(:)' + bnet.CPD{i} = tabular_CPD(bnet, i); + end + for i=cnodes(:)' + bnet.CPD{i} = gaussian_CPD(bnet, i); + end +else + bnet.CPD{B} = tabular_CPD(bnet, B, 'CPT', [0.85 0.15]); % 1=stable, 2=unstable + bnet.CPD{F} = tabular_CPD(bnet, F, 'CPT', [0.95 0.05]); % 1=intact, 2=defect + bnet.CPD{W} = tabular_CPD(bnet, W, 'CPT', [2/7 5/7]); % 1=industrial, 2=household + bnet.CPD{E} = gaussian_CPD(bnet, E, 'mean', [-3.9 -0.4 -3.2 -0.5], ... + 'cov', [0.00002 0.0001 0.00002 0.0001]); + bnet.CPD{D} = gaussian_CPD(bnet, D, 'mean', [6.5 6.0 7.5 7.0], ... + 'cov', [0.03 0.04 0.1 0.1], 'weights', [1 1 1 1]); + bnet.CPD{C} = gaussian_CPD(bnet, C, 'mean', [-2 -1], 'cov', [0.1 0.3]); + bnet.CPD{L} = gaussian_CPD(bnet, L, 'mean', 3, 'cov', 0.25, 'weights', -0.5); + bnet.CPD{Min} = gaussian_CPD(bnet, Min, 'mean', [0.5 -0.5], 'cov', [0.01 0.005]); + bnet.CPD{Mout} = gaussian_CPD(bnet, Mout, 'mean', 0, 'cov', 0.002, 'weights', [1 1]); +end diff --git a/sourcecodes/bnt-master/BNT/examples/static/Models/mk_markov_chain_bnet.m b/sourcecodes/bnt-master/BNT/examples/static/Models/mk_markov_chain_bnet.m new file mode 100644 index 00000000..a911ece5 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/Models/mk_markov_chain_bnet.m @@ -0,0 +1,9 @@ +function bnet = mk_markov_chain_bnet(N, Q) + +dag = zeros(N); +dag(1,2)=1; dag(2,3)=1; +ns = Q*ones(1,N); +bnet = mk_bnet(dag, ns); +for i=1:N + bnet.CPD{i} = tabular_CPD(bnet, i); +end diff --git a/sourcecodes/bnt-master/BNT/examples/static/Models/mk_minimal_qmr_bnet.m b/sourcecodes/bnt-master/BNT/examples/static/Models/mk_minimal_qmr_bnet.m new file mode 100644 index 00000000..99ad6e24 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/Models/mk_minimal_qmr_bnet.m @@ -0,0 +1,82 @@ +function [bnet, vals] = mk_minimal_qmr_bnet(G, inhibit, leak, prior, pos, neg, pos_only) +% MK_MINIMAL_QMR_BNET Make a QMR model which only contains the observed findings +% [bnet, vals] = mk_minimal_qmr_bnet(G, inhibit, prior, leak, pos, neg) +% +% Input: +% G(i,j) = 1 iff there is an arc from disease i to finding j +% inhibit(i,j) = inhibition probability on i->j arc +% leak(j) = inhibition prob. on leak->j arc +% prior(i) = prob. disease i is on +% pos = list of leaves that have positive observations +% neg = list of leaves that have negative observations +% pos_only = 1 means only include positively observed leaves in the model - the negative +% ones are absorbed into the prior terms +% +% Output: +% bnet +% vals is their value + +if pos_only + obs = pos; +else + obs = myunion(pos, neg); +end +Nfindings = length(obs); +[Ndiseases maxNfindings] = size(inhibit); +N = Ndiseases + Nfindings; +finding_node = Ndiseases+1:N; + +% j = finding_node(i) means the i'th finding node is the j'th node in the bnet +% k = obs(i) means the i'th observed (positive) finding is the k'th finding overall +% If all findings are observed, and posonly = 0, we have i = obs(i) for all i. + +%dag = sparse(N, N); +dag = zeros(N, N); +dag(1:Ndiseases, Ndiseases+1:N) = G(:,obs); + +ns = 2*ones(1,N); +bnet = mk_bnet(dag, ns, 'observed', finding_node); + +CPT = cell(1, Ndiseases); +for d=1:Ndiseases + CPT{d} = [1-prior(d) prior(d)]; +end + +if pos_only + % Fold in the negative evidence into the prior + for i=1:length(neg) + n = neg(i); + ps = parents(G,n); + for pi=1:length(ps) + p = ps(pi); + q = inhibit(p,n); + CPT{p} = CPT{p} .* [1 q]; + end + % Arbitrarily attach the leak term to the first parent + p = ps(1); + q = leak(n); + CPT{p} = CPT{p} .* [q q]; + end +end + +for d=1:Ndiseases + bnet.CPD{d} = tabular_CPD(bnet, d, CPT{d}'); +end + +for i=1:Nfindings + fnode = finding_node(i); + fid = obs(i); + ps = parents(G, fid); + bnet.CPD{fnode} = noisyor_CPD(bnet, fnode, leak(fid), inhibit(ps, fid)); +end + +obs_nodes = finding_node; +vals = sparse(1, maxNfindings); +vals(pos) = 2; +vals(neg) = 1; +vals = full(vals(obs)); + + + + + diff --git a/sourcecodes/bnt-master/BNT/examples/static/Models/mk_qmr_bnet.m b/sourcecodes/bnt-master/BNT/examples/static/Models/mk_qmr_bnet.m new file mode 100644 index 00000000..1532ae4c --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/Models/mk_qmr_bnet.m @@ -0,0 +1,41 @@ +function bnet = mk_qmr_bnet(G, inhibit, leak, prior, tabular_findings, onodes) +% MK_QMR_BNET Make a QMR model +% bnet = mk_qmr_bnet(G, inhibit, leak, prior) +% +% G(i,j) = 1 iff there is an arc from disease i to finding j +% inhibit(i,j) = inhibition probability on i->j arc +% leak(j) = inhibition prob. on leak->j arc +% prior(i) = prob. disease i is on +% tabular_findings = 1 means multinomial leaves (ignores leak/inhibit params) +% = 0 means noisy-OR leaves (default = 0) + +if nargin < 5, tabular_findings = 0; end + +[Ndiseases Nfindings] = size(inhibit); +N = Ndiseases + Nfindings; +finding_node = Ndiseases+1:N; +ns = 2*ones(1,N); +dag = zeros(N,N); +dag(1:Ndiseases, finding_node) = G; +if nargin < 6, onodes = finding_node; end +bnet = mk_bnet(dag, ns, 'observed', onodes); + +for d=1:Ndiseases + CPT = [1-prior(d) prior(d)]; + bnet.CPD{d} = tabular_CPD(bnet, d, CPT'); +end + +for i=1:Nfindings + fnode = finding_node(i); + ps = parents(G, i); + if tabular_findings + bnet.CPD{fnode} = tabular_CPD(bnet, fnode); + else + bnet.CPD{fnode} = noisyor_CPD(bnet, fnode, leak(i), inhibit(ps, i)); + end +end + + + + + diff --git a/sourcecodes/bnt-master/BNT/examples/static/Models/mk_vstruct_bnet.m b/sourcecodes/bnt-master/BNT/examples/static/Models/mk_vstruct_bnet.m new file mode 100644 index 00000000..a37a4548 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/Models/mk_vstruct_bnet.m @@ -0,0 +1,16 @@ +function oracle = mk_vstruct_bnet() +% MK_VSTRUCT_BNET Make a simple V-structured 3-node noisy-AND Bayes net +% oracle = mk_vstruct_bnet() + +N = 3; +dag = zeros(N); +A = 1; B = 2; C = 3; +dag(A,C)=1; +dag(B,C)=1; +ns = 2*ones(1,N); + +oracle = mk_bnet(dag, ns); +oracle.CPD{1} = tabular_CPD(oracle, 1, [0.5 0.5]); +oracle.CPD{2} = tabular_CPD(oracle, 2, [0.5 0.5]); +pnoise = 0.1; % degree of noise +oracle.CPD{3} = boolean_CPD(oracle, 3, 'named', 'all', pnoise); diff --git a/sourcecodes/bnt-master/BNT/examples/static/SCG/CVS/Entries b/sourcecodes/bnt-master/BNT/examples/static/SCG/CVS/Entries new file mode 100644 index 00000000..8a722650 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/SCG/CVS/Entries @@ -0,0 +1,6 @@ +/scg1.m/1.1.1.1/Wed May 29 15:59:54 2002// +/scg2.m/1.1.1.1/Wed May 29 15:59:54 2002// +/scg3.m/1.1.1.1/Wed May 29 15:59:54 2002// +/scg_3node.m/1.1.1.1/Wed May 29 15:59:54 2002// +/scg_unstable.m/1.1.1.1/Wed May 29 15:59:54 2002// +D diff --git a/sourcecodes/bnt-master/BNT/examples/static/SCG/CVS/Repository b/sourcecodes/bnt-master/BNT/examples/static/SCG/CVS/Repository new file mode 100644 index 00000000..1b551eef --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/SCG/CVS/Repository @@ -0,0 +1 @@ +FullBNT/BNT/examples/static/SCG diff --git a/sourcecodes/bnt-master/BNT/examples/static/SCG/CVS/Root b/sourcecodes/bnt-master/BNT/examples/static/SCG/CVS/Root new file mode 100644 index 00000000..f3bd14a6 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/SCG/CVS/Root @@ -0,0 +1 @@ +:ext:nsaunier@bnt.cvs.sourceforge.net:/cvsroot/bnt diff --git a/sourcecodes/bnt-master/BNT/examples/static/SCG/scg1.m b/sourcecodes/bnt-master/BNT/examples/static/SCG/scg1.m new file mode 100644 index 00000000..f504c1fe --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/SCG/scg1.m @@ -0,0 +1,77 @@ +% Same as cg1, except we call stab_cond_gauss_inf_engine + +bnet = mk_incinerator_bnet; + +engines = {}; +engines{end+1} = stab_cond_gauss_inf_engine(bnet); +engines{end+1} = jtree_inf_engine(bnet); +engines{end+1} = cond_gauss_inf_engine(bnet); +nengines = length(engines); + +F = 1; W = 2; E = 3; B = 4; C = 5; D = 6; Min = 7; Mout = 8; L = 9; +n = 9; +dnodes = [B F W]; +cnodes = mysetdiff(1:n, dnodes); + +evidence = cell(1,n); % no evidence +ll = zeros(1, nengines); +for e=1:nengines + [engines{e}, ll(e)] = enter_evidence(engines{e}, evidence); +end +%assert(approxeq(ll(1), ll))) +ll + +% Compare to the results in table on p1107. +% These results are printed to 3dp in Cowell p150 + +mu = zeros(1,n); +sigma = zeros(1,n); +dprob = zeros(1,n); +addev = 1; +tol = 1e-2; +for e=1:nengines + for i=cnodes(:)' + m = marginal_nodes(engines{e}, i, addev); + mu(i) = m.mu; + sigma(i) = sqrt(m.Sigma); + end + for i=dnodes(:)' + m = marginal_nodes(engines{e}, i, addev); + dprob(i) = m.T(1); + end + assert(approxeq(mu([E D C L Min Mout]), [-3.25 3.04 -1.85 1.48 -0.214 2.83], tol)) + assert(approxeq(sigma([E D C L Min Mout]), [0.709 0.770 0.507 0.631 0.459 0.860], tol)) + assert(approxeq(dprob([B F W]), [0.85 0.95 0.29], tol)) + %m = marginal_nodes(engines{e}, bnet.names('E'), addev); + %assert(approxeq(m.mu, -3.25, tol)) + %assert(approxeq(sqrt(m.Sigma), 0.709, tol)) +end + +% Add evidence (p 1105, top right) +evidence = cell(1,n); +evidence{W} = 1; % industrial +evidence{L} = 1.1; +evidence{C} = -0.9; + +ll = zeros(1, nengines); +for e=1:nengines + [engines{e}, ll(e)] = enter_evidence(engines{e}, evidence); +end +%assert(all(approxeq(ll(1), ll))) +ll + +for e=1:nengines + for i=cnodes(:)' + m = marginal_nodes(engines{e}, i, addev); + mu(i) = m.mu; + sigma(i) = sqrt(m.Sigma); + end + for i=dnodes(:)' + m = marginal_nodes(engines{e}, i, addev); + dprob(i) = m.T(1); + end + assert(approxeq(mu([E D C L Min Mout]), [-3.90 3.61 -0.9 1.1 0.5 4.11], tol)) + assert(approxeq(sigma([E D C L Min Mout]), [0.076 0.326 0 0 0.1 0.344], tol)) + assert(approxeq(dprob([B F W]), [0.0122 0.9995 1], tol)) +end + diff --git a/sourcecodes/bnt-master/BNT/examples/static/SCG/scg2.m b/sourcecodes/bnt-master/BNT/examples/static/SCG/scg2.m new file mode 100644 index 00000000..a9779248 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/SCG/scg2.m @@ -0,0 +1,12 @@ +% Same as cg2, except we call stab_cond_gauss_inf_engine + +ns = 2*ones(1,9); +bnet = mk_incinerator_bnet(ns); + +engines = {}; +engines{end+1} = stab_cond_gauss_inf_engine(bnet); +engines{end+1} = jtree_inf_engine(bnet); +engines{end+1} = cond_gauss_inf_engine(bnet); +nengines = length(engines); + +[err, time] = cmp_inference_static(bnet, engines, 'singletons_only', 1); diff --git a/sourcecodes/bnt-master/BNT/examples/static/SCG/scg3.m b/sourcecodes/bnt-master/BNT/examples/static/SCG/scg3.m new file mode 100644 index 00000000..cc35b0a5 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/SCG/scg3.m @@ -0,0 +1,42 @@ +% Compare various inference engines on the following network (from Jensen (1996) p84 fig 4.17) +% 1 +% / | \ +% 2 3 4 +% | | | +% 5 6 7 +% \/ \/ +% 8 9 +% where all arcs point downwards + +N = 9; +dag = zeros(N,N); +dag(1,2)=1; dag(1,3)=1; dag(1,4)=1; +dag(2,5)=1; dag(3,6)=1; dag(4,7)=1; +dag(5,8)=1; dag(6,8)=1; dag(6,9)=1; dag(7,9) = 1; + +gauss = 1; +if gauss + ns = ones(1,N); % scalar nodes + ns(1) = 2; + ns(9) = 3; + dnodes = []; +else + ns = 2*ones(1,N); % binary nodes + dnodes = 1:N; +end + +bnet = mk_bnet(dag, ns, 'discrete', dnodes); +% use random params +for i=1:N + if gauss + bnet.CPD{i} = gaussian_CPD(bnet, i); + else + bnet.CPD{i} = tabular_CPD(bnet, i); + end +end + +engines = {}; +engines{1} = jtree_inf_engine(bnet); +engines{2} = stab_cond_gauss_inf_engine(bnet); + +[err, time] = cmp_inference_static(bnet, engines); diff --git a/sourcecodes/bnt-master/BNT/examples/static/SCG/scg_3node.m b/sourcecodes/bnt-master/BNT/examples/static/SCG/scg_3node.m new file mode 100644 index 00000000..5f75946a --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/SCG/scg_3node.m @@ -0,0 +1,52 @@ +% This example is from Page.143 of "Probabilistic Networks and Expert Systems", +% Cowell, Dawid, Lauritzen and Spiegelhalter, 1999, Springer. + +X = 1; Y = 2; Z = 3; +n = 3; + +dag = zeros(n); +dag(X, Y)=1; +dag(Y, Z)=1; + +ns = ones(1, n); +dnodes = []; + +bnet = mk_bnet(dag, ns, dnodes); +bnet.CPD{X} = gaussian_CPD(bnet, X, 'mean', 0, 'cov', 1); +bnet.CPD{Y} = gaussian_CPD(bnet, Y, 'mean', 0, 'cov', 1, 'weights', 1); +bnet.CPD{Z} = gaussian_CPD(bnet, Z, 'mean', 0, 'cov', 1, 'weights', 1); + +engines = {}; +engines{end+1} = jtree_inf_engine(bnet); +engines{end+1} = stab_cond_gauss_inf_engine(bnet); +nengines = length(engines); + +evidence = cell(1,n); +evidence{Y} = 1.5; + +for e=1:nengines + engines{e} = enter_evidence(engines{e}, evidence); + margX = marginal_nodes(engines{e}, X); + assert(approxeq(margX.mu, 0.75)) + assert(approxeq(margX.Sigma, 0.5)) + + margZ = marginal_nodes(engines{e}, Z); + assert(approxeq(margZ.mu, 1.5)) + assert(approxeq(margZ.Sigma, 1)) +end + + +evidence = cell(1,n); +evidence{Z} = 1.5; + +for e=1:nengines + engines{e} = enter_evidence(engines{e}, evidence); + margX = marginal_nodes(engines{e}, X); + assert(approxeq(margX.mu, 1/2)) + assert(approxeq(margX.Sigma, 2/3)) + + margY = marginal_nodes(engines{e}, Y); + assert(approxeq(margY.mu, 1)) + assert(approxeq(margY.Sigma, 2/3)) +end + diff --git a/sourcecodes/bnt-master/BNT/examples/static/SCG/scg_unstable.m b/sourcecodes/bnt-master/BNT/examples/static/SCG/scg_unstable.m new file mode 100644 index 00000000..6617bfa9 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/SCG/scg_unstable.m @@ -0,0 +1,91 @@ +function scg_unstable() + +% the objective of this script is to test if the stable conditonal gaussian +% inference can handle the numerical instability problem described on +% page.151 of 'Probabilistic networks and expert system' by Cowell, Dawid, Lauritzen and +% Spiegelhalter, 1999. + +A = 1; Y = 2; +n = 2; + +ns = ones(1, n); +dnodes = [A]; +cnodes = Y; +ns = [2 1]; + +dag = zeros(n); +dag(A, Y) = 1; + +bnet = mk_bnet(dag, ns, dnodes); + +bnet.CPD{A} = tabular_CPD(bnet, A, [0.5 0.5]'); +bnet.CPD{Y} = gaussian_CPD(bnet, Y, 'mean', [0 1], 'cov', [1e-5 1e-6]); + +evidence = cell(1, n); + +pot_type = 'cg'; +potYgivenA = convert_to_pot(bnet.CPD{Y}, pot_type, [A Y], evidence); +potA = convert_to_pot(bnet.CPD{A}, pot_type, A, evidence); +potYandA = multiply_by_pot(potYgivenA, potA); +potA2 = marginalize_pot(potYandA, A); + +thresh = 1; % 0dp + +[g,h,K] = extract_can(potA); +assert(approxeq(g(:)', [-0.693147 -0.693147], thresh)) + + +[g,h,K] = extract_can(potYgivenA); +assert(approxeq(g(:)', [4.83752 -499994], thresh)) +assert(approxeq(h(:)', [0 1e6])) +assert(approxeq(K(:)', [1e5 1e6])) + +[g,h,K] = extract_can(potYandA); +assert(approxeq(g(:)', [4.14437 -499995], thresh)) +assert(approxeq(h(:)', [0 1e6])) +assert(approxeq(K(:)', [1e5 1e6])) + + +[g,h,K] = extract_can(potA2); +%assert(approxeq(g(:)', [-0.69315 -1])) +g +assert(approxeq(g(:)', [-0.69315 -0.69315])) + + + +if 0 +pot_type = 'scg'; +spotYgivenA = convert_to_pot(bnet.CPD{Y}, pot_type, [A Y], evidence); +spotA = convert_to_pot(bnet.CPD{A}, pot_type, A, evidence); +spotYandA = direct_combine_pots(spotYgivenA, spotA); +spotA2 = marginalize_pot(spotYandA, A); + +spotA=struct(spotA); +spotA2=struct(spotA2); +for i=1:2 + assert(approxeq(spotA2.scgpotc{i}.p, spotA.scgpotc{i}.p)) + assert(approxeq(spotA2.scgpotc{i}.A, spotA.scgpotc{i}.A)) + assert(approxeq(spotA2.scgpotc{i}.B, spotA.scgpotc{i}.B)) + assert(approxeq(spotA2.scgpotc{i}.C, spotA.scgpotc{i}.C)) +end + +end + + +%%%%%%%%%%% + +function [g,h,K] = extract_can(pot) + +pot = struct(pot); +D = length(pot.can); +g = zeros(1, D); +h = zeros(1, D); +K = zeros(1, D); +for i=1:D + S = struct(pot.can{i}); + g(i) = S.g; + if length(S.h) > 0 + h(i) = S.h; + K(i) = S.K; + end +end diff --git a/sourcecodes/bnt-master/BNT/examples/static/StructLearn/CVS/Entries b/sourcecodes/bnt-master/BNT/examples/static/StructLearn/CVS/Entries new file mode 100644 index 00000000..0586b211 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/StructLearn/CVS/Entries @@ -0,0 +1,9 @@ +/bic1.m/1.1.1.1/Wed May 29 15:59:54 2002// +/cooper_yoo.m/1.1.1.1/Wed May 29 15:59:54 2002// +/k2demo1.m/1.1.1.1/Wed May 29 15:59:54 2002// +/mcmc1.m/1.1.1.1/Wed May 29 15:59:54 2002// +/model_select1.m/1.1.1.1/Sat Nov 6 20:55:18 2004// +/model_select2.m/1.1.1.1/Sat Nov 6 21:52:42 2004// +/pc1.m/1.1.1.1/Wed May 29 15:59:54 2002// +/pc2.m/1.1.1.1/Wed May 29 15:59:54 2002// +D diff --git a/sourcecodes/bnt-master/BNT/examples/static/StructLearn/CVS/Repository b/sourcecodes/bnt-master/BNT/examples/static/StructLearn/CVS/Repository new file mode 100644 index 00000000..5b40c54c --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/StructLearn/CVS/Repository @@ -0,0 +1 @@ +FullBNT/BNT/examples/static/StructLearn diff --git a/sourcecodes/bnt-master/BNT/examples/static/StructLearn/CVS/Root b/sourcecodes/bnt-master/BNT/examples/static/StructLearn/CVS/Root new file mode 100644 index 00000000..f3bd14a6 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/StructLearn/CVS/Root @@ -0,0 +1 @@ +:ext:nsaunier@bnt.cvs.sourceforge.net:/cvsroot/bnt diff --git a/sourcecodes/bnt-master/BNT/examples/static/StructLearn/bic1.m b/sourcecodes/bnt-master/BNT/examples/static/StructLearn/bic1.m new file mode 100644 index 00000000..22473564 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/StructLearn/bic1.m @@ -0,0 +1,79 @@ +% compare BIC and Bayesian score + +N = 4; +dag = zeros(N,N); +%C = 1; S = 2; R = 3; W = 4; % topological order +C = 4; S = 2; R = 3; W = 1; % arbitrary order +dag(C,[R S]) = 1; +dag(R,W) = 1; +dag(S,W)=1; + + +false = 1; true = 2; +ns = 2*ones(1,N); % binary nodes +bnet = mk_bnet(dag, ns); +bnet.CPD{C} = tabular_CPD(bnet, C, 'CPT', [0.5 0.5]); +bnet.CPD{R} = tabular_CPD(bnet, R, 'CPT', [0.8 0.2 0.2 0.8]); +bnet.CPD{S} = tabular_CPD(bnet, S, 'CPT', [0.5 0.9 0.5 0.1]); +bnet.CPD{W} = tabular_CPD(bnet, W, 'CPT', [1 0.1 0.1 0.01 0 0.9 0.9 0.99]); + + +seed = 0; +rand('state', seed); +randn('state', seed); +ncases = 1000; +data = cell(N, ncases); +for m=1:ncases + data(:,m) = sample_bnet(bnet); +end + +priors = [0.1 1 10]; +P = length(priors); +params = cell(1,P); +for p=1:P + params{p} = cell(1,N); + for i=1:N + %params{p}{i} = {'prior', priors(p)}; + params{p}{i} = {'prior_type', 'dirichlet', 'dirichlet_weight', priors(p)}; + end +end + +%sz = 1000:1000:10000; +sz = 10:10:100; +S = length(sz); +bic_score = zeros(S, 1); +bayes_score = zeros(S, P); +for i=1:S + bic_score(i) = score_dags(data(:,1:sz(i)), ns, {dag}, 'scoring_fn', 'bic', 'params', []); +end +diff = zeros(S,P); +for p=1:P + for i=1:S + bayes_score(i,p) = score_dags(data(:,1:sz(i)), ns, {dag}, 'params', params{p}); + end +end + +for p=1:P + for i=1:S + diff(i,p) = bayes_score(i,p)/ bic_score(i); + %diff(i,p) = abs(bayes_score(i,p) - bic_score(i)); + end +end + +if 0 +plot(sz, diff(:,1), 'g--*', sz, diff(:,2), 'b-.+', sz, diff(:,3), 'k:s'); +title('Relative BIC error vs. size of data set') +legend('BDeu 0.1', 'BDeu 1', 'Bdeu 10', 2) +end + +if 0 +plot(sz, bic_score, 'r-o', sz, bayes_score(:,1), 'g--*', sz, bayes_score(:,2), 'b-.+', sz, bayes_score(:,3), 'k:s'); +legend('bic', 'BDeu 0.01', 'BDeu 1', 'Bdeu 100') +ylabel('score') +title('score vs. size of data set') +end + +%xlabel('num. data cases') + +%previewfig(gcf, 'format', 'png', 'height', 2, 'color', 'rgb') +%exportfig(gcf, '/home/cs/murphyk/public_html/Bayes/Figures/bic.png', 'format', 'png', 'height', 2, 'color', 'rgb') diff --git a/sourcecodes/bnt-master/BNT/examples/static/StructLearn/cooper_yoo.m b/sourcecodes/bnt-master/BNT/examples/static/StructLearn/cooper_yoo.m new file mode 100644 index 00000000..97ceb44a --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/StructLearn/cooper_yoo.m @@ -0,0 +1,65 @@ +% Do the example in Cooper and Yoo, "Causal discovery from a mixture of experimental and +% observational data", UAI 99, p120 + +N = 2; +dag = zeros(N); +A = 1; B = 2; +dag(A,B) = 1; +ns = 2*ones(1,N); + +bnet0 = mk_bnet(dag, ns); +%bnet0.CPD{A} = tabular_CPD(bnet0, A, 'unif', 1); +bnet0.CPD{A} = tabular_CPD(bnet0, A, 'CPT', 'unif', 'prior_type', 'dirichlet'); +bnet0.CPD{B} = tabular_CPD(bnet0, B, 'CPT', 'unif', 'prior_type', 'dirichlet'); + +samples = [2 2; + 2 1; + 2 2; + 1 1; + 1 2; + 2 2; + 1 1; + 2 2; + 1 2; + 2 1; + 1 1]; + +clamped = [0 0; + 0 0; + 0 0; + 0 0; + 0 0; + 1 0; + 1 0; + 0 1; + 0 1; + 0 1; + 0 1]; + +nsamples = size(samples, 1); + +% sequential version +LL = 0; +bnet = bnet0; +for l=1:nsamples + ev = num2cell(samples(l,:)'); + manip = find(clamped(l,:)'); + LL = LL + log_marg_lik_complete(bnet, ev, manip); + bnet = bayes_update_params(bnet, ev, manip); +end +assert(approxeq(exp(LL), 5.97e-7)) % compare with result from UAI paper + + +% batch version +cases = num2cell(samples'); +LL2 = log_marg_lik_complete(bnet0, cases, clamped'); +bnet2 = bayes_update_params(bnet0, cases, clamped'); + +assert(approxeq(LL, LL2)) + +for j=1:N + s1 = struct(bnet.CPD{j}); % violate object privacy + s2 = struct(bnet2.CPD{j}); + assert(approxeq(s1.CPT, s2.CPT)) +end + diff --git a/sourcecodes/bnt-master/BNT/examples/static/StructLearn/k2demo1.m b/sourcecodes/bnt-master/BNT/examples/static/StructLearn/k2demo1.m new file mode 100644 index 00000000..a6288286 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/StructLearn/k2demo1.m @@ -0,0 +1,45 @@ +N = 4; +dag = zeros(N,N); +%C = 1; S = 2; R = 3; W = 4; +C = 4; S = 2; R = 3; W = 1; % arbitrary order +dag(C,[R S]) = 1; +dag(R,W) = 1; +dag(S,W)=1; + +false = 1; true = 2; +ns = 2*ones(1,N); % binary nodes + +bnet = mk_bnet(dag, ns); +bnet.CPD{C} = tabular_CPD(bnet, C, [0.5 0.5]); +bnet.CPD{R} = tabular_CPD(bnet, R, [0.8 0.2 0.2 0.8]); +bnet.CPD{S} = tabular_CPD(bnet, S, [0.5 0.9 0.5 0.1]); +bnet.CPD{W} = tabular_CPD(bnet, W, [1 0.1 0.1 0.01 0 0.9 0.9 0.99]); + +seed = 0; +rand('state', seed); +randn('state', seed); +ncases = 100; +data = zeros(N, ncases); +for m=1:ncases + data(:,m) = cell2num(sample_bnet(bnet)); +end + +order = [C S R W]; +max_fan_in = 2; + +%dag2 = learn_struct_K2(data, ns, order, 'max_fan_in', max_fan_in, 'verbose', 'yes'); + +sz = 5:5:50; +for i=1:length(sz) + dag2 = learn_struct_K2(data(:,1:sz(i)), ns, order, 'max_fan_in', max_fan_in); + correct(i) = isequal(dag, dag2); +end +correct + +for i=1:length(sz) + dag3 = learn_struct_K2(data(:,1:sz(i)), ns, order, 'max_fan_in', max_fan_in, 'scoring_fn', 'bic', 'params', []); + correct(i) = isequal(dag, dag3); +end +correct + + diff --git a/sourcecodes/bnt-master/BNT/examples/static/StructLearn/mcmc1.m b/sourcecodes/bnt-master/BNT/examples/static/StructLearn/mcmc1.m new file mode 100644 index 00000000..241d0686 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/StructLearn/mcmc1.m @@ -0,0 +1,35 @@ +% We compare MCMC structure learning with exhaustive enumeration of all dags. + +N = 3; +%N = 4; +dag = mk_rnd_dag(N); +ns = 2*ones(1,N); +bnet = mk_bnet(dag, ns); +for i=1:N + bnet.CPD{i} = tabular_CPD(bnet, i); +end + +ncases = 100; +data = zeros(N, ncases); +for m=1:ncases + data(:,m) = cell2num(sample_bnet(bnet)); +end + +dags = mk_all_dags(N); +score = score_dags(data, ns, dags); +post = normalise(exp(score)); + +[sampled_graphs, accept_ratio] = learn_struct_mcmc(data, ns, 'nsamples', 100, 'burnin', 10); +mcmc_post = mcmc_sample_to_hist(sampled_graphs, dags); + +if 0 + subplot(2,1,1) + bar(post) + subplot(2,1,2) + bar(mcmc_post) + print(gcf, '-djpeg', '/home/cs/murphyk/public_html/Bayes/Figures/mcmc_post.jpg') + + clf + plot(accept_ratio) + print(gcf, '-djpeg', '/home/cs/murphyk/public_html/Bayes/Figures/mcmc_accept.jpg') +end diff --git a/sourcecodes/bnt-master/BNT/examples/static/StructLearn/model_select1.m b/sourcecodes/bnt-master/BNT/examples/static/StructLearn/model_select1.m new file mode 100644 index 00000000..c79131c3 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/StructLearn/model_select1.m @@ -0,0 +1,121 @@ +% Bayesian model selection demo. + +% We generate data from the model A->B +% and compute the posterior prob of all 3 dags on 2 nodes: +% (1) A B, (2) A <- B , (3) A -> B +% Models 2 and 3 are Markov equivalent, and therefore indistinguishable from +% observational data alone. +% Using the "difficult" params, the true model only gets a higher posterior after 2000 trials! +% However, using the noisy NOT gate, the true model wins after 12 trials. + +% ground truth +N = 2; +dag = zeros(N); +A = 1; B = 2; +dag(A,B) = 1; + +difficult = 0; +if difficult + ntrials = 2000; + ns = 3*ones(1,N); + true_bnet = mk_bnet(dag, ns); + rand('state', 0); + temp = 5; + for i=1:N + %true_bnet.CPD{i} = tabular_CPD(true_bnet, i, temp); + true_bnet.CPD{i} = tabular_CPD(true_bnet, i); + end +else + ntrials = 25; + ns = 2*ones(1,N); + true_bnet = mk_bnet(dag, ns); + true_bnet.CPD{1} = tabular_CPD(true_bnet, 1, [0.5 0.5]); + pfail = 0.1; + psucc = 1-pfail; + true_bnet.CPD{2} = tabular_CPD(true_bnet, 2, [pfail psucc; psucc pfail]); % NOT gate +end + +G = mk_all_dags(N); +nhyp = length(G); +hyp_bnet = cell(1, nhyp); +for h=1:nhyp + hyp_bnet{h} = mk_bnet(G{h}, ns); + for i=1:N + % We must set the CPTs to the mean of the prior for sequential log_marg_lik to be correct + % The BDeu prior is score equivalent, so models 2,3 will be indistinguishable. + % The uniform Dirichlet prior is not score equivalent... + fam = family(G{h}, i); + hyp_bnet{h}.CPD{i}= tabular_CPD(hyp_bnet{h}, i, 'prior_type', 'dirichlet', ... + 'CPT', 'unif'); + end +end +prior = normalise(ones(1, nhyp)); + +% save results before doing sequential updating +init_hyp_bnet = hyp_bnet; +init_prior = prior; + + +rand('state', 0); +hyp_w = zeros(ntrials+1, nhyp); +hyp_w(1,:) = prior(:)'; + +data = zeros(N, ntrials); + +% First we compute the posteriors sequentially + +LL = zeros(1, nhyp); +ll = zeros(1, nhyp); +for t=1:ntrials + ev = cell2num(sample_bnet(true_bnet)); + data(:,t) = ev; + for i=1:nhyp + ll(i) = log_marg_lik_complete(hyp_bnet{i}, ev); + hyp_bnet{i} = bayes_update_params(hyp_bnet{i}, ev); + end + prior = normalise(prior .* exp(ll)); + LL = LL + ll; + hyp_w(t+1,:) = prior; +end + +% Plot posterior model probabilities +% Red = model 1 (no arcs), blue/green = models 2/3 (1 arc) +% Blue = model 2 (2->1) +% Green = model 3 (1->2, "ground truth") + +if 1 + figure; +m = size(hyp_w, 1); +h=plot(1:m, hyp_w(:,1), 'r-', 1:m, hyp_w(:,2), 'b-.', 1:m, hyp_w(:,3), 'g:'); +axis([0 m 0 1]) +title('model posterior vs. time') +%previewfig(gcf, 'format', 'png', 'height', 2, 'color', 'rgb') +%exportfig(gcf, '/home/cs/murphyk/public_html/Bayes/Figures/model_select.png',... +%'format', 'png', 'height', 2, 'color', 'rgb') +drawnow +end + + +% Now check that batch updating gives same result +hyp_bnet2 = init_hyp_bnet; +prior2 = init_prior; + +cases = num2cell(data); +LL2 = zeros(1, nhyp); +for i=1:nhyp + LL2(i) = log_marg_lik_complete(hyp_bnet2{i}, cases); + hyp_bnet2{i} = bayes_update_params(hyp_bnet2{i}, cases); +end + + +assert(approxeq(LL, LL2)) +LL + +for i=1:nhyp + for j=1:N + s1 = struct(hyp_bnet{i}.CPD{j}); + s2 = struct(hyp_bnet2{i}.CPD{j}); + assert(approxeq(s1.CPT, s2.CPT)) + end +end + diff --git a/sourcecodes/bnt-master/BNT/examples/static/StructLearn/model_select2.m b/sourcecodes/bnt-master/BNT/examples/static/StructLearn/model_select2.m new file mode 100644 index 00000000..d34c75c4 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/StructLearn/model_select2.m @@ -0,0 +1,83 @@ +% Online Bayesian model selection demo. + +% We generate data from the model A->B +% and compute the posterior prob of all 3 dags on 2 nodes: +% (1) A B, (2) A <- B , (3) A -> B +% Models 2 and 3 are Markov equivalent, and therefore indistinguishable from +% observational data alone. + +% We control the dependence of B on A by setting +% P(B|A) = 0.5 - epislon and vary epsilon +% as in Koller & Friedman book p512 + +% ground truth +N = 2; +dag = zeros(N); +A = 1; B = 2; +dag(A,B) = 1; + +ntrials = 100; +ns = 2*ones(1,N); +true_bnet = mk_bnet(dag, ns); +true_bnet.CPD{1} = tabular_CPD(true_bnet, 1, [0.5 0.5]); + +% hypothesis space +G = mk_all_dags(N); +nhyp = length(G); +hyp_bnet = cell(1, nhyp); +for h=1:nhyp + hyp_bnet{h} = mk_bnet(G{h}, ns); + for i=1:N + % We must set the CPTs to the mean of the prior for sequential log_marg_lik to be correct + % The BDeu prior is score equivalent, so models 2,3 will be indistinguishable. + % The uniform Dirichlet prior is not score equivalent... + fam = family(G{h}, i); + hyp_bnet{h}.CPD{i}= tabular_CPD(hyp_bnet{h}, i, 'prior_type', 'dirichlet', ... + 'CPT', 'unif'); + end +end + +clf +seeds = 1:3; +expt = 1; +for seedi=1:length(seeds) + seed = seeds(seedi); + rand('state', seed); + randn('state', seed); + + es = [0.05 0.1 0.15 0.2]; + for ei=1:length(es) + e = es(ei); + true_bnet.CPD{2} = tabular_CPD(true_bnet, 2, [0.5+e 0.5-e; 0.5-e 0.5+e]); + + prior = normalise(ones(1, nhyp)); + hyp_w = zeros(ntrials+1, nhyp); + hyp_w(1,:) = prior(:)'; + LL = zeros(1, nhyp); + ll = zeros(1, nhyp); + for t=1:ntrials + ev = cell2num(sample_bnet(true_bnet)); + for i=1:nhyp + ll(i) = log_marg_lik_complete(hyp_bnet{i}, ev); + hyp_bnet{i} = bayes_update_params(hyp_bnet{i}, ev); + end + prior = normalise(prior .* exp(ll)); + LL = LL + ll; + hyp_w(t+1,:) = prior; + end + + % Plot posterior model probabilities + % Red = model 1 (no arcs), blue/green = models 2/3 (1 arc) + % Blue = model 2 (2->1) + % Green = model 3 (1->2, "ground truth") + + subplot2(length(seeds), length(es), seedi, ei); + m = size(hyp_w,1); + h=plot(1:m, hyp_w(:,1), 'r-', 1:m, hyp_w(:,2), 'b-.', 1:m, hyp_w(:,3), 'g:'); + axis([0 m 0 1]) + %title('model posterior vs. time') + title(sprintf('e=%3.2f, seed=%d', e, seed)); + drawnow + expt = expt + 1; + end +end diff --git a/sourcecodes/bnt-master/BNT/examples/static/StructLearn/pc1.m b/sourcecodes/bnt-master/BNT/examples/static/StructLearn/pc1.m new file mode 100644 index 00000000..a6fbb9bd --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/StructLearn/pc1.m @@ -0,0 +1,30 @@ +% SGS p118 +% Try learning the structure using an oracle for the cond indep tests + +n = 5; + +A = 1; B = 2; C = 3; D = 4; E = 5; + +G = zeros(n); +G(A,B)=1; +G(B,[C D]) = 1; +G(C,E)=1; +G(D,E)=1; + +k = 2; + +pdag = learn_struct_pdag_pc('dsep', n, k, G) + + + + +if 0 +N = 4; +dag = zeros(N,N); +C = 1; S = 2; R = 3; W = 4; +dag(C,[R S]) = 1; +dag(R,W) = 1; +dag(S,W)=1; + +pdag = learn_struct_pdag_pc('dsep', N, 2, dag) +end diff --git a/sourcecodes/bnt-master/BNT/examples/static/StructLearn/pc2.m b/sourcecodes/bnt-master/BNT/examples/static/StructLearn/pc2.m new file mode 100644 index 00000000..7b7e2066 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/StructLearn/pc2.m @@ -0,0 +1,21 @@ +% SGS p141 (female orgasm data set) + +C = eye(7,7); +C(2,1:1) = [-0.132]; +C(3,1:2) = [0.009 -0.136]; +C(4,1:3) = [0.22 -0.166 0.403]; +C(5,1:4) = [-0.008 0.008 0.598 0.282]; +C(6,1:5) = [0.119 -0.076 0.264 0.514 0.176]; +C(7,1:6) = [0.118 -0.137 0.368 0.414 0.336 0.338]; + +n = 7; +for i=1:n + for j=i+1:n + C(i,j)=C(j,i); + end +end + +max_fan_in = 4; +nsamples = 281; +alpha = 0.05; +pdag = learn_struct_pdag_pc('cond_indep_fisher_z', n, max_fan_in, C, nsamples, alpha) diff --git a/sourcecodes/bnt-master/BNT/examples/static/Zoubin/CVS/Entries b/sourcecodes/bnt-master/BNT/examples/static/Zoubin/CVS/Entries new file mode 100644 index 00000000..31efa304 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/Zoubin/CVS/Entries @@ -0,0 +1,10 @@ +/README/1.1.1.1/Wed May 29 15:59:54 2002// +/csum.m/1.1.1.1/Wed May 29 15:59:54 2002// +/ffa.m/1.1.1.1/Wed May 29 15:59:54 2002// +/mfa.m/1.1.1.1/Wed May 29 15:59:54 2002// +/mfa_cl.m/1.1.1.1/Wed May 29 15:59:54 2002// +/mfademo.m/1.1.1.1/Wed May 29 15:59:54 2002// +/rdiv.m/1.1.1.1/Wed May 29 15:59:54 2002// +/rprod.m/1.1.1.1/Wed May 29 15:59:54 2002// +/rsum.m/1.1.1.1/Wed May 29 15:59:54 2002// +D diff --git a/sourcecodes/bnt-master/BNT/examples/static/Zoubin/CVS/Repository b/sourcecodes/bnt-master/BNT/examples/static/Zoubin/CVS/Repository new file mode 100644 index 00000000..15fbcd8e --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/Zoubin/CVS/Repository @@ -0,0 +1 @@ +FullBNT/BNT/examples/static/Zoubin diff --git a/sourcecodes/bnt-master/BNT/examples/static/Zoubin/CVS/Root b/sourcecodes/bnt-master/BNT/examples/static/Zoubin/CVS/Root new file mode 100644 index 00000000..f3bd14a6 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/Zoubin/CVS/Root @@ -0,0 +1 @@ +:ext:nsaunier@bnt.cvs.sourceforge.net:/cvsroot/bnt diff --git a/sourcecodes/bnt-master/BNT/examples/static/Zoubin/README b/sourcecodes/bnt-master/BNT/examples/static/Zoubin/README new file mode 100644 index 00000000..0fe8214b --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/Zoubin/README @@ -0,0 +1,61 @@ +This software was downloaded from + http://www.gatsby.ucl.ac.uk/~zoubin/software.html +with permission of the author. + + +This software was written by + +Zoubin Ghahramani +Dept of Computer Science +University of Toronto +zoubin@cs.toronto.edu + +This software is written in Matlab 4.2c and should run on all platforms +supporting this version of Matlab. Matlab is a commercial software +package available from The MathWorks (http://www.mathworks.com/). + +This software is meant for free non-commercial use and distribution. See the +copyright notice at the bottom of this page. + +If you use it, please refer to the accompanying technical report: + +Ghahramani, Z. and Hinton, G.E. (1996) The EM Algorithm for Mixtures +of Factor Analyzers. University of Toronto Technical Report CRG-TR-96-1. +Available at ftp://ftp.cs.toronto.edu/pub/zoubin/tr-96-1.ps.gz + +If you find bugs, or would like to see if I've implemented any +extensions, please send me email at zoubin@cs.toronto.edu. The +software is provided "as is", and I cannot guarantee I will be able +to fix all problems or answer all inquiries. + +See mfademo.m for a demo. + +Hope you find it useful. Please send me email if you find it useful +and I will put you on a mailing list announcing releases of other +statistical machine learning software in Matlab. + + +---------------------------------------------------------------------- + Copyright (c) 1996 by Zoubin Ghahramani + Toronto, Ontario, Canada. + All Rights Reserved + +Permission to use, copy, modify, and distribute this software and its +documentation for non-commercial purposes only is hereby granted +without fee, provided that the above copyright notice appears in all +copies and that both the copyright notice and this permission notice +appear in supporting documentation, and that my name not be used in +advertising or publicity pertaining to distribution of the software +without specific, written prior permission. I make no representations +about the suitability of this software for any purpose. It is provided +"as is" without express or implied warranty. + +I disclaim all warranties with regard to this software, including all +implied warranties of merchantability and fitness. In no event shall I +be liable for any special, indirect or consequential damages or any +damages whatsoever resulting from loss of use, data or profits, +whether in an action of contract, negligence or other tortious action, +arising out of or in connection with the use or performance of this +software. + +Zoubin Ghahramani Dec 17, 1996 diff --git a/sourcecodes/bnt-master/BNT/examples/static/Zoubin/csum.m b/sourcecodes/bnt-master/BNT/examples/static/Zoubin/csum.m new file mode 100644 index 00000000..2fba6ca5 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/Zoubin/csum.m @@ -0,0 +1,11 @@ +% column sum +% function Z=csum(X) + +function Z=csum(X) + +N=length(X(:,1)); +if (N>1) + Z=sum(X); +else + Z=X; +end; \ No newline at end of file diff --git a/sourcecodes/bnt-master/BNT/examples/static/Zoubin/ffa.m b/sourcecodes/bnt-master/BNT/examples/static/Zoubin/ffa.m new file mode 100644 index 00000000..e4caa3e0 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/Zoubin/ffa.m @@ -0,0 +1,75 @@ +% function [L,Ph,LL]=ffa(X,K,cyc,tol); +% +% Fast Maximum Likelihood Factor Analysis using EM +% +% X - data matrix +% K - number of factors +% cyc - maximum number of cycles of EM (default 100) +% tol - termination tolerance (prop change in likelihood) (default 0.0001) +% +% L - factor loadings +% Ph - diagonal uniquenesses matrix +% LL - log likelihood curve +% +% Iterates until a proportional change < tol in the log likelihood +% or cyc steps of EM +% + +function [L,Ph,LL]=ffa(X,K,cyc,tol); + +if nargin<4 tol=0.0001; end; +if nargin<3 cyc=100; end; + +N=length(X(:,1)); +D=length(X(1,:)); +tiny=exp(-700); + +X=X-ones(N,1)*mean(X); +XX=X'*X/N; +diagXX=diag(XX); + +randn('seed', 0); +cX=cov(X); +scale=det(cX)^(1/D); +L=randn(D,K)*sqrt(scale/K); +Ph=diag(cX); + +I=eye(K); + +lik=0; LL=[]; + +const=-D/2*log(2*pi); + + +for i=1:cyc; + + %%%% E Step %%%% + Phd=diag(1./Ph); + LP=Phd*L; + MM=Phd-LP*inv(I+L'*LP)*LP'; + dM=sqrt(det(MM)); + beta=L'*MM; + XXbeta=XX*beta'; + EZZ=I-beta*L +beta*XXbeta; + + %%%% Compute log likelihood %%%% + + oldlik=lik; + lik=N*const+N*log(dM)-0.5*N*sum(diag(MM*XX)); + fprintf('cycle %i lik %g \n',i,lik); + LL=[LL lik]; + + %%%% M Step %%%% + + L=XXbeta*inv(EZZ); + Ph=diagXX-diag(L*XXbeta'); + + if (i<=2) + likbase=lik; + elseif (lik<oldlik) + disp('VIOLATION'); + elseif ((lik-likbase)<(1+tol)*(oldlik-likbase)||~isfinite(lik)) + break; + end; + +end diff --git a/sourcecodes/bnt-master/BNT/examples/static/Zoubin/mfa.m b/sourcecodes/bnt-master/BNT/examples/static/Zoubin/mfa.m new file mode 100644 index 00000000..2060e331 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/Zoubin/mfa.m @@ -0,0 +1,153 @@ +% function [Lh,Ph,Mu,Pi,LL]=mfa(X,M,K,cyc,tol); +% +% Maximum Likelihood Mixture of Factor Analysis using EM +% +% X - data matrix +% M - number of mixtures (default 1) +% K - number of factors in each mixture (default 2) +% cyc - maximum number of cycles of EM (default 100) +% tol - termination tolerance (prop change in likelihood) (default 0.0001) +% +% Lh - factor loadings +% Ph - diagonal uniquenesses matrix +% Mu - mean vectors +% Pi - priors +% LL - log likelihood curve +% +% Iterates until a proportional change < tol in the log likelihood +% or cyc steps of EM + +function [Lh, Ph, Mu, Pi, LL] = mfa(X,M,K,cyc,tol) + +if nargin<5 tol=0.0001; end; +if nargin<4 cyc=100; end; +if nargin<3 K=2; end; +if nargin<2 M=1; end; + +N=length(X(:,1)); +D=length(X(1,:)); +tiny=exp(-700); + +%rand('state',0); + +fprintf('\n'); + +if (M==1) + [Lh,Ph,LL]=ffa(X,K,cyc,tol); + Mu=mean(X); + Pi=1; +else + if N==1 + mX = X; + else + mX=mean(X); + end + cX=cov(X); + scale=det(cX)^(1/D); + randn('state',0); + Lh=randn(D*M,K)*sqrt(scale/K); + Ph=diag(cX)+tiny; + Pi=ones(M,1)/M; + %randn('state',0); + Mu=randn(M,D)*sqrtm(cX)+ones(M,1)*mX; + oldMu=Mu; + I=eye(K); + + lik=0; + LL=[]; + + H=zeros(N,M); % E(w|x) + EZ=zeros(N*M,K); + EZZ=zeros(K*M,K); + XX=zeros(D*M,D); + s=zeros(M,1); + const=(2*pi)^(-D/2); + %%%%%%%%%%%%%%%%%%%% + for i=1:cyc; + + %%%% E Step %%%% + + Phi=1./Ph; + Phid=diag(Phi); + for k=1:M + Lht=Lh((k-1)*D+1:k*D,:); + LP=Phid*Lht; + MM=Phid-LP*inv(I+Lht'*LP)*LP'; + dM=sqrt(det(MM)); + Xk=(X-ones(N,1)*Mu(k,:)); + XM=Xk*MM; + H(:,k)=const*Pi(k)*dM*exp(-0.5*rsum(XM.*Xk)); + EZ((k-1)*N+1:k*N,:)=XM*Lht; + end; + + Hsum=rsum(H); + oldlik=lik; + lik=sum(log(Hsum+(Hsum==0)*exp(-744))); + + Hzero=(Hsum==0); Nz=sum(Hzero); + H(Hzero,:)=tiny*ones(Nz,M)/M; + Hsum(Hzero)=tiny*ones(Nz,1); + + H=rdiv(H,Hsum); + s=csum(H); + s=s+(s==0)*tiny; + s2=sum(s)+tiny; + + for k=1:M + kD=(k-1)*D+1:k*D; + Lht=Lh(kD,:); + LP=Phid*Lht; + MM=Phid-LP*inv(I+Lht'*LP)*LP'; + Xk=(X-ones(N,1)*Mu(k,:)); + XX(kD,:)=rprod(Xk,H(:,k))'*Xk/s(k); + beta=Lht'*MM; + EZZ((k-1)*K+1:k*K,:)=I-beta*Lht +beta*XX(kD,:)*beta'; + end; + + %%%% log likelihood %%%% + + LL=[LL lik]; + fprintf('cycle %g \tlog likelihood %g ',i,lik); + + if (i<=2) + likbase=lik; + elseif (lik<oldlik) + fprintf(' violation'); + elseif ((lik-likbase)<(1 + tol)*(oldlik-likbase)||~isfinite(lik)) + break; + end; + + fprintf('\n'); + + %%%% M Step %%%% + + % means and covariance structure + + Ph=zeros(D,1); + for k=1:M + kD=(k-1)*D+1:k*D; + kK=(k-1)*K+1:k*K; + kN=(k-1)*N+1:k*N; + + T0=rprod(X,H(:,k)); + T1=T0'*[EZ(kN,:) ones(N,1)]; + XH=EZ(kN,:)'*H(:,k); + T2=inv([s(k)*EZZ(kK,:) XH; XH' s(k)]); + T3=T1*T2; + Lh(kD,:)=T3(:,1:K); + Mu(k,:)=T3(:,K+1)'; + T4=diag(T0'*X-T3*T1')/s2; + Ph=Ph+T4.*(T4>0); + end; + + Phmin=exp(-700); + Ph=Ph.*(Ph>Phmin)+(Ph<=Phmin)*Phmin; % to avoid zero variances + + % priors + Pi=s'/s2; + + end; + fprintf('\n'); +end; + + diff --git a/sourcecodes/bnt-master/BNT/examples/static/Zoubin/mfa_cl.m b/sourcecodes/bnt-master/BNT/examples/static/Zoubin/mfa_cl.m new file mode 100644 index 00000000..b90bab18 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/Zoubin/mfa_cl.m @@ -0,0 +1,54 @@ +% function [lik, likv]=mfa_cl(X,Lh,Ph,Mu,Pi); +% +% Calculates log likelihoods of a data set under a mixture of factor +% analysis model. +% +% X - data matrix +% Lh - factor loadings +% Ph - diagonal uniquenesses matrix +% Mu - mean vectors +% Pi - priors +% +% lik - log likelihood of X +% likv - vector of log likelihoods +% +% If 0 or 1 output arguments requested, lik is returned. If 2 output +% arguments requested, [lik likv] is returned. + +function [lik, likv]=mfa_cl(X,Lh,Ph,Mu,Pi); + +N=length(X(:,1)); +D=length(X(1,:)); +K=length(Lh(1,:)); +M=length(Pi); + +if (abs(sum(Pi)-1) > 1e-6) + disp('ERROR: Pi should sum to 1'); + return; +elseif ((size(Lh) ~= [D*M K]) | (size(Ph) ~= [D 1]) | (size(Mu) ~= [M D]) ... + | (size(Pi) ~= [M 1] & size(Pi) ~= [1 M])) + disp('ERROR in input matrix sizes'); + return; +end; + +tiny=exp(-744); +const=(2*pi)^(-D/2); + +I=eye(K); +Phi=1./Ph; +Phid=diag(Phi); +for k=1:M + Lht=Lh((k-1)*D+1:k*D,:); + LP=Phid*Lht; + MM=Phid-LP*inv(I+Lht'*LP)*LP'; + dM=sqrt(det(MM)); + Xk=(X-ones(N,1)*Mu(k,:)); + XM=Xk*MM; + H(:,k)=const*Pi(k)*dM*exp(-0.5*sum((XM.*Xk)'))'; +end; + +Hsum=rsum(H); + +likv=log(Hsum+(Hsum==0)*tiny); +lik=sum(likv); + diff --git a/sourcecodes/bnt-master/BNT/examples/static/Zoubin/mfademo.m b/sourcecodes/bnt-master/BNT/examples/static/Zoubin/mfademo.m new file mode 100644 index 00000000..508d840b --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/Zoubin/mfademo.m @@ -0,0 +1,81 @@ +echo on; + +clc; + +% This is a very basic demo of the mixture of factor analyzer software +% written in Matlab by Zoubin Ghahramani +% Dept of Computer Science +% University of Toronto + +pause; % Hit any key to continue + +% To demonstrate the software we generate a sample data set +% from a mixture of two Gaussians + +pause; % Hit any key to continue + +X1=randn(300,5); % zero mean 5 dim Gaussian data +X2=randn(200,5)+2; % 5 dim Gaussian data with mean [1 1 1 1 1] +X=[X1;X2]; % total 500 data points from mixture + +% Fitting the model is very easy. For example to fit a mixture of 2 +% factor analyzers with three factors each... + +pause; % Hit any key to continue + + +[Lh,Ph,Mu,Pi,LL]=mfa(X,2,3); + +% Lh, Ph, Mu, and Pi are the factor loadings, observervation +% variances, observation means for each mixture, and mixing +% proportions. LL is the vector of log likelihoods (the learning +% curve). For more information type: help mfa + +% to plot the learning curve (log likelihood at each step of EM)... + +pause; % Hit any key to continue + +plot(LL); + +% you get a more informative picture of convergence by looking at the +% log of the first difference of the log likelihoods... + +pause; % Hit any key to continue + +semilogy(diff(LL)); + +% you can look at some of the parameters of the fitted model... + +pause; % Hit any key to continue + +Mu + +Pi + +% ...to see whether they make any sense given that me know how the +% data was generated. + +% you can also evaluate the log likelihood of another data set under +% the model we have just fitted using the mfa_cl (for Calculate +% Likelihood) function. For example, here we generate a test from the +% same distribution. + + +X1=randn(300,5); +X2=randn(200,5)+2; +Xtest=[X1; X2]; + +pause; % Hit any key to continue + +mfa_cl(Xtest,Lh,Ph,Mu,Pi) + +% we should expect the log likelihood of the test set to be lower than +% that of the training set. + +% finally, we can also fit a regular factor analyzer using the ffa +% function (Fast Factor Analysis)... + +pause; % Hit any key to continue + +[L,Ph,LL]=ffa(X,3); + diff --git a/sourcecodes/bnt-master/BNT/examples/static/Zoubin/rdiv.m b/sourcecodes/bnt-master/BNT/examples/static/Zoubin/rdiv.m new file mode 100644 index 00000000..3128061e --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/Zoubin/rdiv.m @@ -0,0 +1,25 @@ +% function Z=rdiv(X,Y) +% +% row division: Z = X / Y row-wise +% Y must have one column + +function Z=rdiv(X,Y) + +[N M]=size(X); +[K L]=size(Y); +if(N ~= K | L ~=1) + disp('Error in RDIV'); + return; +end + +Z=zeros(N,M); + +if M<N, + for m=1:M + Z(:,m)=X(:,m)./Y; + end +else + for n=1:N + Z(n,:)=X(n,:)/Y(n); + end; +end; \ No newline at end of file diff --git a/sourcecodes/bnt-master/BNT/examples/static/Zoubin/rprod.m b/sourcecodes/bnt-master/BNT/examples/static/Zoubin/rprod.m new file mode 100644 index 00000000..95d3565d --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/Zoubin/rprod.m @@ -0,0 +1,15 @@ +% row product +% function Z=rprod(X,Y) + +function Z=rprod(X,Y) + +if(length(X(:,1)) ~= length(Y(:,1)) | length(Y(1,:)) ~=1) + disp('Error in RPROD'); + return; +end + +Z=zeros(size(X)); + +for i=1:length(X(1,:)) + Z(:,i)=X(:,i).*Y; +end diff --git a/sourcecodes/bnt-master/BNT/examples/static/Zoubin/rsum.m b/sourcecodes/bnt-master/BNT/examples/static/Zoubin/rsum.m new file mode 100644 index 00000000..0af53fde --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/Zoubin/rsum.m @@ -0,0 +1,20 @@ +% row sum +% function Z=rsum(X) + +function Z=rsum(X) + +[N M]=size(X); + +Z=zeros(N,1); + +if M==1, + Z=X; +elseif M<2*N, + for m=1:M, + Z=Z+X(:,m); + end; +else + for n=1:N + Z(n)=sum(X(n,:)); + end; +end diff --git a/sourcecodes/bnt-master/BNT/examples/static/brainy.m b/sourcecodes/bnt-master/BNT/examples/static/brainy.m new file mode 100644 index 00000000..cf9a1623 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/brainy.m @@ -0,0 +1,44 @@ +% Example of explaining away from +% http://www.ai.mit.edu/~murphyk/Bayes/bnintro.html#explainaway +% +% Suppose you have to be brainy or smart to get into college. +% B S P(C=1) P(C=2) 1=false 2=true +% 1 1 1.0 0.0 +% 2 1 0.0 1.0 +% 1 2 0.0 1.0 +% 2 2 0.0 1.0 +% +% +% If we observe that you are in college, you must be either brainy or sporty or both. +% If we observre you are in college and sporty, it is less likely you are brainy, +% since brainy-ness and sporty-ness compete as causal explanations of the effect. + +% B S +% \/ +% C + +B = 1; S = 2; C = 3; +dag = zeros(3,3); +dag([B S], C)=1; +ns = 2*ones(1,3); +bnet = mk_bnet(dag, ns); +bnet.CPD{B} = tabular_CPD(bnet, B, 'CPT', [0.5 0.5]'); +bnet.CPD{S} = tabular_CPD(bnet, S, 'CPT', [0.5 0.5]'); +CPT = zeros(2,2,2); +CPT(1,1,:) = [1 0]; +CPT(2,1,:) = [0 1]; +CPT(1,2,:) = [0 1]; +CPT(2,2,:) = [0 1]; +bnet.CPD{C} = tabular_CPD(bnet, C, 'CPT', CPT); + +engine = jtree_inf_engine(bnet); +ev = cell(1,3); +ev{C} = 2; +engine = enter_evidence(engine, ev); +m = marginal_nodes(engine, B); +fprintf('P(B=true|C=true) = %5.3f\n', m.T(2)) % 0.67 + +ev{S} = 2; +engine = enter_evidence(engine, ev); +m = marginal_nodes(engine, B); +fprintf('P(B=true|C=true,S=true) = %5.3f\n', m.T(2)) % 0.5 = unconditional baseline P(B=true) diff --git a/sourcecodes/bnt-master/BNT/examples/static/burglar-alarm-net.lisp.txt b/sourcecodes/bnt-master/BNT/examples/static/burglar-alarm-net.lisp.txt new file mode 100644 index 00000000..a48f48fb --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/burglar-alarm-net.lisp.txt @@ -0,0 +1,45 @@ +#| +The following code represents the burglar alarm Bayes network from +Chapter 14 of Russell & Norvig, 2nd Edition. This network representation +is used in the corresponding Bayes net code found in this directory. + +The conditional probability tables consist of the values listed here +(along with the probabilities of the corresponding complementary events): + +P(Burglary = true) = 0.001 (=> P(Burglary = false) = 0.999) +P(Earthquake = true) = 0.002 (=> P(Earthquake = false) = 0.998) + +P(Alarm = true | Burglary = true, Earthquake = true) = 0.95 +P(Alarm = true | Burglary = true, Earthquake = false) = 0.94 +P(Alarm = true | Burglary = false, Earthquake = true) = 0.29 +P(Alarm = true | Burglary = false, Earthquake = false) = 0.001 + +P(JohnCalls = true | Alarm = true) = 0.90 +P(JohnCalls = true | Alarm = false) = 0.05 + +P(MaryCalls = true | Alarm = true) = 0.70 +P(MaryCalls = true | Alarm = false) = 0.01 +|# + +(setf *burglar-alarm-net* + '((MaryCalls (true false) + (Alarm) + ((true) 0.70 0.30) + ((false) 0.01 0.99)) + (JohnCalls (true false) + (Alarm) + ((true) 0.90 0.10) + ((false) 0.05 0.95)) + (Alarm (true false) + (Burglary Earthquake) + ((true true) 0.95 0.05) + ((true false) 0.94 0.06) + ((false true) 0.29 0.71) + ((false false) 0.001 0.999)) + (Burglary (true false) + () + (0.001 0.999)) + (Earthquake (true false) + () + (0.002 0.998)) + )) diff --git a/sourcecodes/bnt-master/BNT/examples/static/burglary.m b/sourcecodes/bnt-master/BNT/examples/static/burglary.m new file mode 100644 index 00000000..c93e144e --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/burglary.m @@ -0,0 +1,44 @@ +% Burglar alarm example + +N = 5; +dag = zeros(N,N); +E = 1; B = 2; R = 3; A = 4; C = 5; +dag(E,[R A]) = 1; +dag(B,A) = 1; +dag(A,C)=1; + +% true = state 1, false = state 2 +ns = 2*ones(1,N); % binary nodes +bnet = mk_bnet(dag, ns); + +bnet.CPD{E} = tabular_CPD(bnet, E, [0.1 0.9]); +bnet.CPD{B} = tabular_CPD(bnet, B, [0.01 0.99]); +%bnet.CPD{R} = tabular_CPD(bnet, R, [0.65 0.00001 0.35 0.99999]); +bnet.CPD{R} = tabular_CPD(bnet, R, [0.65 0.01 0.35 0.99]); +bnet.CPD{A} = tabular_CPD(bnet, A, [0.95 0.8 0.3 0.001 0.05 0.2 0.7 0.999]); +bnet.CPD{C} = tabular_CPD(bnet, C, [0.7 0.05 0.3 0.95]); + + +engine = jtree_inf_engine(bnet); +ev = cell(1,N); +ev{C} = 1; +engine = enter_evidence(engine, ev); +mE = marginal_nodes(engine, E); +mB = marginal_nodes(engine, B); +fprintf('P(E|c)=%5.3f, P(B|c)=%5.3f\n', mE.T(1), mB.T(1)) + +ev{C} = 1; +ev{R} = 1; +engine = enter_evidence(engine, ev); +mE = marginal_nodes(engine, E); +mB = marginal_nodes(engine, B); +fprintf('P(E|c,r)=%5.3f, P(B|c,r)=%5.3f\n', mE.T(1), mB.T(1)) + + +if 0 +nsamples = 100; +samples = zeros(nsamples, 5); +for i=1:nsamples + samples(i,:) = cell2num(sample_bnet(bnet))'; +end +end diff --git a/sourcecodes/bnt-master/BNT/examples/static/cg1.m b/sourcecodes/bnt-master/BNT/examples/static/cg1.m new file mode 100644 index 00000000..00821cc2 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/cg1.m @@ -0,0 +1,86 @@ +% Conditional Gaussian network +% The waste incinerator emissions example from Lauritzen (1992), +% "Propogation of Probabilities, Means and Variances in Mixed Graphical Association Models", +% JASA 87(420): 1098--1108 +% +% This example is reprinted on p145 of "Probabilistic Networks and Expert Systems", +% Cowell, Dawid, Lauritzen and Spiegelhalter, 1999, Springer. +% +% For a picture, see http://www.cs.berkeley.edu/~murphyk/Bayes/usage.html#cg_model + +ns = 2*ones(1,9); +%bnet = mk_incinerator_bnet(ns); +bnet = mk_incinerator_bnet; + +engines = {}; +%engines{end+1} = stab_cond_gauss_inf_engine(bnet); +engines{end+1} = jtree_inf_engine(bnet); +engines{end+1} = cond_gauss_inf_engine(bnet); +nengines = length(engines); + +F = 1; W = 2; E = 3; B = 4; C = 5; D = 6; Min = 7; Mout = 8; L = 9; +n = 9; +dnodes = [B F W]; +cnodes = mysetdiff(1:n, dnodes); + +evidence = cell(1,n); % no evidence +ll = zeros(1, nengines); +for e=1:nengines + [engines{e}, ll(e)] = enter_evidence(engines{e}, evidence); +end +%assert(approxeq(ll(1), ll))) +ll + +% Compare to the results in table on p1107. +% These results are printed to 3dp in Cowell p150 + +mu = zeros(1,n); +sigma = zeros(1,n); +dprob = zeros(1,n); +addev = 1; +tol = 1e-2; +for e=1:nengines + for i=cnodes(:)' + m = marginal_nodes(engines{e}, i, addev); + mu(i) = m.mu; + sigma(i) = sqrt(m.Sigma); + end + for i=dnodes(:)' + m = marginal_nodes(engines{e}, i, addev); + dprob(i) = m.T(1); + end + assert(approxeq(mu([E D C L Min Mout]), [-3.25 3.04 -1.85 1.48 -0.214 2.83], tol)) + assert(approxeq(sigma([E D C L Min Mout]), [0.709 0.770 0.507 0.631 0.459 0.860], tol)) + assert(approxeq(dprob([B F W]), [0.85 0.95 0.29], tol)) + %m = marginal_nodes(engines{e}, bnet.names('E'), addev); + %assert(approxeq(m.mu, -3.25, tol)) + %assert(approxeq(sqrt(m.Sigma), 0.709, tol)) +end + +% Add evidence (p 1105, top right) +evidence = cell(1,n); +evidence{W} = 1; % industrial +evidence{L} = 1.1; +evidence{C} = -0.9; + +ll = zeros(1, nengines); +for e=1:nengines + [engines{e}, ll(e)] = enter_evidence(engines{e}, evidence); +end +assert(all(approxeq(ll(1), ll))) + +for e=1:nengines + for i=cnodes(:)' + m = marginal_nodes(engines{e}, i, addev); + mu(i) = m.mu; + sigma(i) = sqrt(m.Sigma); + end + for i=dnodes(:)' + m = marginal_nodes(engines{e}, i, addev); + dprob(i) = m.T(1); + end + assert(approxeq(mu([E D C L Min Mout]), [-3.90 3.61 -0.9 1.1 0.5 4.11], tol)) + assert(approxeq(sigma([E D C L Min Mout]), [0.076 0.326 0 0 0.1 0.344], tol)) + assert(approxeq(dprob([B F W]), [0.0122 0.9995 1], tol)) +end + diff --git a/sourcecodes/bnt-master/BNT/examples/static/cg2.m b/sourcecodes/bnt-master/BNT/examples/static/cg2.m new file mode 100644 index 00000000..9d70bff2 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/cg2.m @@ -0,0 +1,11 @@ +% Conditional Gaussian network with vector-valued nodes and random params + +ns = 2*ones(1,9); +bnet = mk_incinerator_bnet(ns); + +engines = {}; +%engines{end+1} = stab_cond_gauss_inf_engine(bnet); +engines{end+1} = jtree_inf_engine(bnet); +engines{end+1} = cond_gauss_inf_engine(bnet); + +[err, time] = cmp_inference_static(bnet, engines, 'singletons_only', 1); diff --git a/sourcecodes/bnt-master/BNT/examples/static/cmp_inference_static.m b/sourcecodes/bnt-master/BNT/examples/static/cmp_inference_static.m new file mode 100644 index 00000000..4eeb22d8 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/cmp_inference_static.m @@ -0,0 +1,114 @@ +function [time, engine] = cmp_inference_static(bnet, engine, varargin) +% CMP_INFERENCE Compare several inference engines on a BN +% function [time, engine] = cmp_inference_static(bnet, engine, ...) +% +% engine{i} is the i'th inference engine. +% time(e) = elapsed time for doing inference with engine e +% +% The list below gives optional arguments [default value in brackets]. +% +% exact - specifies which engines do exact inference [ 1:length(engine) ] +% singletons_only - if 1, we only call marginal_nodes, else this and marginal_family [0] +% maximize - 1 means we do max-propagation, 0 means sum-propagation [0] +% check_ll - 1 means we check that the log-likelihoods are correct [1] +% observed - list of the observed ndoes [ bnet.observed ] +% check_converged - list of loopy engines that should be checked for convergence [ [] ] +% If an engine has converged, it is added to the exact list. + + +% set default params +exact = 1:length(engine); +singletons_only = 0; +maximize = 0; +check_ll = 1; +observed = bnet.observed; +check_converged = []; + +args = varargin; +nargs = length(args); +for i=1:2:nargs + switch args{i}, + case 'exact', exact = args{i+1}; + case 'singletons_only', singletons_only = args{i+1}; + case 'maximize', maximize = args{i+1}; + case 'check_ll', check_ll = args{i+1}; + case 'observed', observed = args{i+1}; + case 'check_converged', check_converged = args{i+1}; + otherwise, + error(['unrecognized argument ' args{i}]) + end +end + +E = length(engine); +ref = exact(1); % reference + +N = length(bnet.dag); +ev = sample_bnet(bnet); +evidence = cell(1,N); +evidence(observed) = ev(observed); +%celldisp(evidence(observed)) + +for i=1:E + tic; + if check_ll + [engine{i}, ll(i)] = enter_evidence(engine{i}, evidence, 'maximize', maximize); + else + engine{i} = enter_evidence(engine{i}, evidence, 'maximize', maximize); + end + time(i)=toc; +end + +for i=check_converged(:)' + niter = loopy_converged(engine{i}); + if niter > 0 + fprintf('loopy engine %d converged in %d iterations\n', i, niter); +% exact = myunion(exact, i); + else + fprintf('loopy engine %d has not converged\n', i); + end +end + +cmp = exact(2:end); +if check_ll + for i=cmp(:)' + assert(approxeq(ll(ref), ll(i))); + end +end + +hnodes = mysetdiff(1:N, observed); + +if ~singletons_only + get_marginals(engine, hnodes, exact, 0); +end +get_marginals(engine, hnodes, exact, 1); + +%%%%%%%%%% + +function get_marginals(engine, hnodes, exact, singletons) + +bnet = bnet_from_engine(engine{1}); +N = length(bnet.dag); +cnodes_bitv = zeros(1,N); +cnodes_bitv(bnet.cnodes) = 1; +ref = exact(1); % reference +cmp = exact(2:end); +E = length(engine); + +for n=hnodes(:)' + for e=1:E + if singletons + m{e} = marginal_nodes(engine{e}, n); + else + m{e} = marginal_family(engine{e}, n); + end + end + for e=cmp(:)' + if cnodes_bitv(n) + assert(approxeq(m{ref}.mu, m{e}.mu)) + assert(approxeq(m{ref}.Sigma, m{e}.Sigma)) + else + assert(approxeq(m{ref}.T, m{e}.T)) + end + assert(isequal(m{e}.domain, m{ref}.domain)); + end +end diff --git a/sourcecodes/bnt-master/BNT/examples/static/discrete1.m b/sourcecodes/bnt-master/BNT/examples/static/discrete1.m new file mode 100644 index 00000000..37761f37 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/discrete1.m @@ -0,0 +1,40 @@ +% Compare various inference engines on the following network (from Jensen (1996) p84 fig 4.17) +% 1 +% / | \ +% 2 3 4 +% | | | +% 5 6 7 +% \/ \/ +% 8 9 +% where all arcs point downwards + +N = 9; +dag = zeros(N,N); +dag(1,2)=1; dag(1,3)=1; dag(1,4)=1; +dag(2,5)=1; dag(3,6)=1; dag(4,7)=1; +dag(5,8)=1; dag(6,8)=1; dag(6,9)=1; dag(7,9) = 1; + +dnodes = 1:N; +false = 1; true = 2; +ns = 2*ones(1,N); % binary nodes + +onodes = [2 7]; +bnet = mk_bnet(dag, ns, 'observed', onodes); +% use random params +for i=1:N + bnet.CPD{i} = tabular_CPD(bnet, i); +end + +query = [3]; +engine = {}; +engine{end+1} = jtree_inf_engine(bnet); +engine{end+1} = var_elim_inf_engine(bnet); +%engine{end+1} = global_joint_inf_engine(bnet); +% global joint is designed for limids because does not normalize + +%engine{end+1} = enumerative_inf_engine(bnet); +%engine{end+1} = jtree_onepass_inf_engine(bnet, query, onodes); + +maximize = 0; % jtree_ndx crashes on max-prop +[err, time] = cmp_inference_static(bnet, engine, 'maximize', maximize); + diff --git a/sourcecodes/bnt-master/BNT/examples/static/discrete2.m b/sourcecodes/bnt-master/BNT/examples/static/discrete2.m new file mode 100644 index 00000000..c7ca5f41 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/discrete2.m @@ -0,0 +1,46 @@ +% Compare various inference engines on the following network (from Jensen (1996) p84 fig 4.17) +% 1 +% / | \ +% 2 3 4 +% | | | +% 5 6 7 +% \/ \/ +% 8 9 +% where all arcs point downwards +seed = 0; +rand('state', seed); +randn('state', seed); + +N = 9; +dag = zeros(N,N); +dag(1,2)=1; dag(1,3)=1; dag(1,4)=1; +dag(2,5)=1; dag(3,6)=1; dag(4,7)=1; +dag(5,8)=1; dag(6,8)=1; dag(6,9)=1; dag(7,9) = 1; + +dnodes = 1:N; +false = 1; true = 2; +ns = 2*ones(1,N); % binary nodes + +onodes = [2 4]; +bnet = mk_bnet(dag, ns, 'observed', onodes); +% use random params +for i=1:N + bnet.CPD{i} = tabular_CPD(bnet, i); +end + +%USEC = exist('@jtree_C_inf_engine/collect_evidence','file'); +query = [3]; +engine = {}; +engine{end+1} = jtree_inf_engine(bnet); +engine{end+1} = jtree_sparse_inf_engine(bnet); +%engine{end+1} = jtree_ndx_inf_engine(bnet, 'ndx_type', 'SD'); +%engine{end+1} = jtree_ndx_inf_engine(bnet, 'ndx_type', 'B'); +%engine{end+1} = jtree_ndx_inf_engine(bnet, 'ndx_type', 'D'); +%if USEC, engine{end+1} = jtree_C_inf_engine(bnet); end +%engine{end+1} = var_elim_inf_engine(bnet); +%engine{end+1} = enumerative_inf_engine(bnet); +%engine{end+1} = jtree_onepass_inf_engine(bnet, query, onodes); + +maximize = 0; % jtree_ndx crashes on max-prop +[err, time] = cmp_inference_static(bnet, engine, 'maximize', maximize); + diff --git a/sourcecodes/bnt-master/BNT/examples/static/discrete3.m b/sourcecodes/bnt-master/BNT/examples/static/discrete3.m new file mode 100644 index 00000000..70164e86 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/discrete3.m @@ -0,0 +1,43 @@ +% Compare various inference engines on the following network (from Jensen (1996) p84 fig 4.17) +% 1 +% / | \ +% 2 3 4 +% | | | +% 5 6 7 +% \/ \/ +% 8 9 +% where all arcs point downwards + +N = 9; +dag = zeros(N,N); +dag(1,2)=1; dag(1,3)=1; dag(1,4)=1; +dag(2,5)=1; dag(3,6)=1; dag(4,7)=1; +dag(5,8)=1; dag(6,8)=1; dag(6,9)=1; dag(7,9) = 1; + +dnodes = 1:N; +false = 1; true = 2; +ns = 2*ones(1,N); % binary nodes + +onodes = [1]; +evidence = cell(1,N); +evidence(onodes) = num2cell(1); +bnet = mk_bnet(dag, ns, 'observed', onodes); +% use random params +%for i=1:N +% bnet.CPD{i} = tabular_CPD(bnet, i); +%end +bnet.CPD{1} = tabular_CPD(bnet, 1, 'sparse', 1, 'CPT', [0.8, 0.2]); +bnet.CPD{2} = tabular_CPD(bnet, 2, 'sparse', 1, 'CPT', [1 0 0 1]); +bnet.CPD{3} = tabular_CPD(bnet, 3, 'sparse', 1, 'CPT', [0 1 1 0]); +bnet.CPD{4} = tabular_CPD(bnet, 4, 'sparse', 1, 'CPT', [1 1 0 0]); +bnet.CPD{5} = tabular_CPD(bnet, 5, 'sparse', 1, 'CPT', [0 0 1 1]); +bnet.CPD{6} = tabular_CPD(bnet, 6, 'sparse', 1, 'CPT', [1 0 0 1]); +bnet.CPD{7} = tabular_CPD(bnet, 7, 'sparse', 1, 'CPT', [0 1 1 0]); +bnet.CPD{8} = tabular_CPD(bnet, 8, 'sparse', 1, 'CPT', [1 1 0 0 0 0 1 1]); +bnet.CPD{9} = tabular_CPD(bnet, 9, 'sparse', 1, 'CPT', [0 1 0 1 1 0 1 0]); + +engine = jtree_sparse_inf_engine(bnet); +tic +[engine, ll] = enter_evidence(engine, evidence); +toc + diff --git a/sourcecodes/bnt-master/BNT/examples/static/dtree/CVS/Entries b/sourcecodes/bnt-master/BNT/examples/static/dtree/CVS/Entries new file mode 100644 index 00000000..81dc9c31 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/dtree/CVS/Entries @@ -0,0 +1,6 @@ +/test_housing.m/1.1.1.1/Wed May 29 15:59:54 2002// +/test_restaurants.m/1.1.1.1/Wed May 29 15:59:54 2002// +/test_zoo1.m/1.1.1.1/Wed May 29 15:59:54 2002// +/tmp.dot/1.1.1.1/Wed May 29 15:59:54 2002// +/transform_data_into_bnt_format.m/1.1.1.1/Wed May 29 15:59:54 2002// +D diff --git a/sourcecodes/bnt-master/BNT/examples/static/dtree/CVS/Repository b/sourcecodes/bnt-master/BNT/examples/static/dtree/CVS/Repository new file mode 100644 index 00000000..f45a3265 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/dtree/CVS/Repository @@ -0,0 +1 @@ +FullBNT/BNT/examples/static/dtree diff --git a/sourcecodes/bnt-master/BNT/examples/static/dtree/CVS/Root b/sourcecodes/bnt-master/BNT/examples/static/dtree/CVS/Root new file mode 100644 index 00000000..f3bd14a6 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/dtree/CVS/Root @@ -0,0 +1 @@ +:ext:nsaunier@bnt.cvs.sourceforge.net:/cvsroot/bnt diff --git a/sourcecodes/bnt-master/BNT/examples/static/dtree/test_housing.m b/sourcecodes/bnt-master/BNT/examples/static/dtree/test_housing.m new file mode 100644 index 00000000..40184b47 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/dtree/test_housing.m @@ -0,0 +1,87 @@ +% Here the training data is adapted from UCI ML repository, 'housing' data +% Input variables: 12 continous, one binary +% Ouput variables: continous +% The testing result trace is in the end of this script, it is same to the graph in page 219 of +% Leo Brieman etc. 1984 book titled "Classification and regression trees". + +dtreeCPD=tree_CPD; + +% load data +fname = fullfile(BNT_HOME, 'examples', 'static', 'uci_data', 'housing', 'housing.data'); +data=load(fname); +data=data'; +data=transform_data_into_bnt_format(data,[1:3,5:14]); + +% learn decision tree from data +ns=1*ones(1,14); +ns(4)=2; +dtreeCPD1=learn_params(dtreeCPD,1:14,data,ns,[1:3,5:14],'stop_cases',5,'min_gain',0.006); + +% evaluate on data +[score,outputs]=evaluate_tree_performance(dtreeCPD1,1:14,data,ns,[1:3,5:14]); +fprintf('Mean square deviation (using regression tree to predict) in old training data %6.3f\n',score); + + +% show decision tree using graphpad +% It should be easy, but still not implemented + + + +% >> test_housing +% Create node 1 split at 6 gain 38.2205 Th 6.939000e+000. Mean 22.5328 Cases 506 +% Create node 2 split at 13 gain 14.4503 Th 1.437000e+001. Mean 19.9337 Cases 430 +% Create node 3 split at 8 gain 4.9809 Th 1.358000e+000. Mean 23.3498 Cases 255 +% Create node 4 split at 1 gain 0.7722 Th 1.023300e+001. Mean 45.5800 Cases 5 +% Create leaf node(samevalue) 5. Mean 50.0000 Std 0.0000 Cases 4 +% Add subtree node 5 to 4. #nodes 5 +% Create leaf node(samevalue) 6. Mean 27.9000 Std 0.0000 Cases 1 +% Add subtree node 6 to 4. #nodes 6 +% Add subtree node 4 to 3. #nodes 6 +% Create node 7 split at 6 gain 2.8497 Th 6.540000e+000. Mean 22.9052 Cases 250 +% Create node 8 split at 13 gain 0.5970 Th 7.560000e+000. Mean 21.6297 Cases 195 +% Create leaf node(nogain) 9. Mean 23.9698 Std 1.7568 Cases 43 +% Add subtree node 9 to 8. #nodes 9 +% Create leaf node(nogain) 10. Mean 20.9678 Std 2.8242 Cases 152 +% Add subtree node 10 to 8. #nodes 10 +% Add subtree node 8 to 7. #nodes 10 +% Create leaf node(nogain) 11. Mean 27.4273 Std 3.4512 Cases 55 +% Add subtree node 11 to 7. #nodes 11 +% Add subtree node 7 to 3. #nodes 11 +% Add subtree node 3 to 2. #nodes 11 +% Create node 12 split at 1 gain 2.2467 Th 6.962150e+000. Mean 14.9560 Cases 175 +% Create node 13 split at 5 gain 0.5172 Th 5.240000e-001. Mean 17.1376 Cases 101 +% Create leaf node(nogain) 14. Mean 20.0208 Std 3.0672 Cases 24 +% Add subtree node 14 to 13. #nodes 14 +% Create leaf node(nogain) 15. Mean 16.2390 Std 2.9746 Cases 77 +% Add subtree node 15 to 13. #nodes 15 +% Add subtree node 13 to 12. #nodes 15 +% Create node 16 split at 5 gain 0.6133 Th 6.050000e-001. Mean 11.9784 Cases 74 +% Create leaf node(nogain) 17. Mean 16.6333 Std 4.5052 Cases 12 +% Add subtree node 17 to 16. #nodes 17 +% Create leaf node(nogain) 18. Mean 11.0774 Std 3.0090 Cases 62 +% Add subtree node 18 to 16. #nodes 18 +% Add subtree node 16 to 12. #nodes 18 +% Add subtree node 12 to 2. #nodes 18 +% Add subtree node 2 to 1. #nodes 18 +% Create node 19 split at 6 gain 6.0493 Th 7.420000e+000. Mean 37.2382 Cases 76 +% Create node 20 split at 1 gain 1.9900 Th 7.367110e+000. Mean 32.1130 Cases 46 +% Create node 21 split at 8 gain 0.6273 Th 1.877300e+000. Mean 33.3488 Cases 43 +% Create leaf node(samevalue) 22. Mean 45.6500 Std 6.1518 Cases 2 +% Add subtree node 22 to 21. #nodes 22 +% Create leaf node(nogain) 23. Mean 32.7488 Std 3.5690 Cases 41 +% Add subtree node 23 to 21. #nodes 23 +% Add subtree node 21 to 20. #nodes 23 +% Create leaf node(samevalue) 24. Mean 14.4000 Std 3.7363 Cases 3 +% Add subtree node 24 to 20. #nodes 24 +% Add subtree node 20 to 19. #nodes 24 +% Create node 25 split at 1 gain 1.1001 Th 2.733970e+000. Mean 45.0967 Cases 30 +% Create leaf node(nogain) 26. Mean 45.8966 Std 4.4005 Cases 29 +% Add subtree node 26 to 25. #nodes 26 +% Create leaf node(samevalue) 27. Mean 21.9000 Std 0.0000 Cases 1 +% Add subtree node 27 to 25. #nodes 27 +% Add subtree node 25 to 19. #nodes 27 +% Add subtree node 19 to 1. #nodes 27 +% Mean square deviation (using regression tree to predict) in old training data 9.405 +% + + diff --git a/sourcecodes/bnt-master/BNT/examples/static/dtree/test_restaurants.m b/sourcecodes/bnt-master/BNT/examples/static/dtree/test_restaurants.m new file mode 100644 index 00000000..9727847a --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/dtree/test_restaurants.m @@ -0,0 +1,98 @@ +% Here the training data is adapted from Russell95 book. See restaurant.names for description. +% (1) Use infomation-gain as the split testing score, we get the the same decision tree as the book Russell 95 (page 537), +% and the Gain(Patrons) is 0.5409, equal to the result in Page 541 of Russell 95. (see below output trace) +% (Note: the dtree in that book has small compilation error, the Type node is from YES of Hungry node, not NO.) +% (2) Use gain-ratio (Quilan 93), the splitting defavorite attribute with more values. (e.g. the Type attribute here) + +dtreeCPD=tree_CPD; + +% load data +fname = fullfile(BNT_HOME, 'examples', 'static', 'uci_data', 'restaurant', 'restaurant.data'); +data=load(fname); +data=data'; + +%make the data be BNT compliant (values for discrete nodes are from 1-n, here n is the node size) + % e.g. if the values are [0 1 6], they must be mapping to [1 2 3] +%data=transform_data(data,'tmp.dat',[]); %here no cts nodes + +% learn decision tree from data +ns=2*ones(1,11); +ns(5:6)=3; +ns(9:10)=4; +dtreeCPD1=learn_params(dtreeCPD,1:11,data,ns,[]); + +% evaluate on data +[score,outputs]=evaluate_tree_performance(dtreeCPD1,1:11,data,ns,[]); +fprintf('Accuracy in training data %6.3f\n',score); + +% show decision tree using graphpad + + + +% --------------------------Output trace: using Information-Gain------------------------------ +% The splits are Patron, Hungry, Type, Fri/Sat +% ********************************* +% Create node 1 split at 5 gain 0.5409 Th 0. Class 1 Cases 12 Error 6 +% Create leaf node(onecla) 2. Class 1 Cases 2 Error 0 +% Add subtree node 2 to 1. #nodes 2 +% Create leaf node(onecla) 3. Class 2 Cases 4 Error 0 +% Add subtree node 3 to 1. #nodes 3 +% Create node 4 split at 4 gain 0.2516 Th 0. Class 1 Cases 6 Error 2 +% Create leaf node(onecla) 5. Class 1 Cases 2 Error 0 +% Add subtree node 5 to 4. #nodes 5 +% Create node 6 split at 9 gain 0.5000 Th 0. Class 1 Cases 4 Error 2 +% Create leaf node(nullset) 7. Father 6 Class 1 +% Create node 8 split at 3 gain 1.0000 Th 0. Class 1 Cases 2 Error 1 +% Create leaf node(onecla) 9. Class 1 Cases 1 Error 0 +% Add subtree node 9 to 8. #nodes 9 +% Create leaf node(onecla) 10. Class 2 Cases 1 Error 0 +% Add subtree node 10 to 8. #nodes 10 +% Add subtree node 8 to 6. #nodes 10 +% Create leaf node(onecla) 11. Class 2 Cases 1 Error 0 +% Add subtree node 11 to 6. #nodes 11 +% Create leaf node(onecla) 12. Class 1 Cases 1 Error 0 +% Add subtree node 12 to 6. #nodes 12 +% Add subtree node 6 to 4. #nodes 12 +% Add subtree node 4 to 1. #nodes 12 +% ******************************** +% +% Note: +% ***Create node 4 split at 4 gain 0.2516 Th 0. Class 1 Cases 6 Error 2 +% This mean we create a new node number 4, it is splitting at the attribute 4, and info-gain is 0.2516, +% "Th 0" means threshhold for splitting continous attribute, "Class 1" means the majority class at node 4 is 1, +% and "Cases 6" means it has 6 cases attached to it, "Error 2" means it has two errors if changing the class lable of +% all the cases in it to the majority class. +% *** Add subtree node 12 to 6. #nodes 12 +% It means we add the child node 12 to node 6. +% *** Create leaf node(onecla) 10. Class 2 Cases 1 Error 0 +% here 'onecla' means all cases in this node belong to one class, so no need to split further. +% 'nullset' means no training cases belong to this node, we use its parent node majority class as its class +% +% +% +% ---------------Output trace: using GainRatio----------------------- +% The splits are Patron, Hungry, Fri/Sat, Price +% +% +% Create node 1 split at 5 gain 0.3707 Th 0. Class 1 Cases 12 Error 6 +% Create leaf node(onecla) 2. Class 1 Cases 2 Error 0 +% Add subtree node 2 to 1. #nodes 2 +% Create leaf node(onecla) 3. Class 2 Cases 4 Error 0 +% Add subtree node 3 to 1. #nodes 3 +% Create node 4 split at 4 gain 0.2740 Th 0. Class 1 Cases 6 Error 2 +% Create leaf node(onecla) 5. Class 1 Cases 2 Error 0 +% Add subtree node 5 to 4. #nodes 5 +% Create node 6 split at 3 gain 0.3837 Th 0. Class 1 Cases 4 Error 2 +% Create leaf node(onecla) 7. Class 1 Cases 1 Error 0 +% Add subtree node 7 to 6. #nodes 7 +% Create node 8 split at 6 gain 1.0000 Th 0. Class 2 Cases 3 Error 1 +% Create leaf node(onecla) 9. Class 2 Cases 2 Error 0 +% Add subtree node 9 to 8. #nodes 9 +% Create leaf node(nullset) 10. Father 8 Class 2 +% Create leaf node(onecla) 11. Class 1 Cases 1 Error 0 +% Add subtree node 11 to 8. #nodes 11 +% Add subtree node 8 to 6. #nodes 11 +% Add subtree node 6 to 4. #nodes 11 +% Add subtree node 4 to 1. #nodes 11 +% +% diff --git a/sourcecodes/bnt-master/BNT/examples/static/dtree/test_zoo1.m b/sourcecodes/bnt-master/BNT/examples/static/dtree/test_zoo1.m new file mode 100644 index 00000000..23c258b3 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/dtree/test_zoo1.m @@ -0,0 +1,21 @@ +% Here the training data is adapted from UCI ML repository, 'zoo' data + +dtreeCPD=tree_CPD; + +% load data +fname = fullfile(BNT_HOME, 'examples', 'static', 'uci_data', 'zoo', 'zoo1.data') +data=load(fname); +data=data'; + +data=transform_data_into_bnt_format(data, []); + +% learn decision tree from data +ns=2*ones(1,17); +ns(13)=6; +ns(17)=7; +dtreeCPD1=learn_params(dtreeCPD,1:17,data,ns,[],'stop_cases',5); % a node with less than 5 cases will not be splitted + +% evaluate on data +[score,outputs]=evaluate_tree_performance(dtreeCPD1,1:17,data,ns,[]); +fprintf('Accuracy in old training data %6.3f\n',score); + diff --git a/sourcecodes/bnt-master/BNT/examples/static/dtree/tmp.dot b/sourcecodes/bnt-master/BNT/examples/static/dtree/tmp.dot new file mode 100644 index 00000000..de359ea7 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/dtree/tmp.dot @@ -0,0 +1,31 @@ +digraph G { +center = 1; +size="4,4"; +n1 [ label = "1 :" ]; +n2 [ label = "2 :" ]; +n3 [ label = "3 :" ]; +n4 [ label = "4 :" ]; +n5 [ label = "5 :" ]; +n6 [ label = "6 :" ]; +n7 [ label = "7 :" ]; +n8 [ label = "8 :" ]; +n9 [ label = "9 :" ]; +n10 [ label = "10 :" ]; +n1 -> n5 [label="1.000"]; +n2 -> n7 [label="0.800"]; +n2 -> n10 [label="0.200"]; +n3 -> n2 [label="1.000"]; +n4 -> n8 [label="1.000"]; +n5 -> n3 [label="0.143"]; +n5 -> n5 [label="0.571"]; +n5 -> n8 [label="0.286"]; +n6 -> n4 [label="1.000"]; +n7 -> n6 [label="0.333"]; +n7 -> n9 [label="0.667"]; +n8 -> n1 [label="0.333"]; +n8 -> n5 [label="0.333"]; +n8 -> n10 [label="0.333"]; +n9 -> n2 [label="1.000"]; +n10 -> n9 [label="1.000"]; + +} \ No newline at end of file diff --git a/sourcecodes/bnt-master/BNT/examples/static/dtree/transform_data_into_bnt_format.m b/sourcecodes/bnt-master/BNT/examples/static/dtree/transform_data_into_bnt_format.m new file mode 100644 index 00000000..92739590 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/dtree/transform_data_into_bnt_format.m @@ -0,0 +1,66 @@ +function [bnt_data, old_values] = transform_data_into_bnt_format(data,cnodes) +% TRANSFORM_DATA_TO_BNT_FORMAT Ensures discrete variables have values 1,2,..,k +% e.g., if the values of a discrete are [0 1 6], they must be mapped to [1 2 3] +% +% data(i,j) is the value for i-th node in j-th case. +% bnt_data(i,j) is the new value. +% old_values{i} are the original values for node i. +% cnodes is the list of all continous nodes, e.g. [3 5] means the 3rd and 5th node is continuous +% +% Author: yimin.zhang@intel.com +% Last updated: Jan. 22, 2002 by Kevin Murphy. + +num_nodes=size(data,1); +num_cases=size(data,2); +old_values=cell(1,num_nodes); + +for i=1:num_nodes + if (myismember(i,cnodes)==1) %cts nodes no need to be transformed + %just copy the data + bnt_data(i,:)=data(i,:); + continue; + end + values = data(i,:); + sort_v = sort(values); + %remove the duplicate values in sort_v + v_set = unique(sort_v); + + %transform the values + for j=1:size(values,2) + index = binary_search(v_set,values(j)); + if (index==-1) + fprintf('value not found in tranforming data to bnt format.\n'); + return; + end + bnt_data(i,j)=index; + end + old_values{i}=v_set; +end + + +%%%%%%%%%%%% + +function index=binary_search(vector, value) +% BI_SEARCH do binary search for value in the vector +% Author: yimin.zhang@intel.com +% Last updated: Jan. 19, 2002 + +begin_index=1; +end_index=size(vector,2); +index=-1; +while (begin_index<=end_index) + mid=floor((begin_index+end_index)/2); + if (isstr(vector(mid))) + % need to write a strcmp to return three result (< = >) + else + if (value==vector(mid)) + index=mid; + return; + elseif (value>vector(mid)) + begin_index=mid+1; + else + end_index=mid-1; + end + end +end +return; diff --git a/sourcecodes/bnt-master/BNT/examples/static/fa1.m b/sourcecodes/bnt-master/BNT/examples/static/fa1.m new file mode 100644 index 00000000..7e131198 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/fa1.m @@ -0,0 +1,57 @@ +% Factor analysis +% Z -> X, Z in R^k, X in R^D, k << D (high dimensional observations explained by small source) +% Z ~ N(0,I), X|Z ~ N(L z, Psi), where Psi is diagonal. +% +% We compare to Zoubin Ghahramani's code. + +state = 0; +rand('seed', state); +randn('seed', state); +max_iter = 3; +k = 2; +D = 4; +N = 10; +X = randn(N, D); + +% Initialize as in Zoubin's ffa (fast factor analysis) +X=X-ones(N,1)*mean(X); +XX=X'*X/N; +diagXX=diag(XX); +cX=cov(X); +scale=det(cX)^(1/D); +randn('seed', 0); % must reset seed here so initial params are identical to mfa +L0=randn(D,k)*sqrt(scale/k); +W0 = L0; +Psi0=diag(cX); + +[L1, Psi1, LL1] = ffa(X,k,max_iter); + + +ns = [k D]; +dag = zeros(2,2); +dag(1,2) = 1; +bnet = mk_bnet(dag, ns, 'discrete', [], 'observed', 2); +bnet.CPD{1} = gaussian_CPD(bnet, 1, 'mean', zeros(k,1), 'cov', eye(k), 'cov_type', 'diag', ... + 'clamp_mean', 1, 'clamp_cov', 1); +bnet.CPD{2} = gaussian_CPD(bnet, 2, 'mean', zeros(D,1), 'cov', diag(Psi0), 'weights', W0, ... + 'cov_type', 'diag', 'cov_prior_weight', 0, 'clamp_mean', 1); + +engine = jtree_inf_engine(bnet); +evidence = cell(2,N); +evidence(2,:) = num2cell(X', 1); + +[bnet2, LL2] = learn_params_em(engine, evidence, max_iter); + +s = struct(bnet2.CPD{2}); +L2 = s.weights; +Psi2 = s.cov; + + + +% Compare to Zoubin's code +assert(approxeq(LL2, LL1)); +assert(approxeq(Psi2, diag(Psi1))); +assert(approxeq(L2, L1)); + + + diff --git a/sourcecodes/bnt-master/BNT/examples/static/fgraph/CVS/Entries b/sourcecodes/bnt-master/BNT/examples/static/fgraph/CVS/Entries new file mode 100644 index 00000000..0ed34e12 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/fgraph/CVS/Entries @@ -0,0 +1,6 @@ +/fg1.m/1.1.1.1/Thu Jun 20 00:03:30 2002// +/fg2.m/1.1.1.1/Wed May 29 15:59:54 2002// +/fg3.m/1.1.1.1/Wed May 29 15:59:54 2002// +/fg_mrf1.m/1.1.1.1/Wed May 29 15:59:54 2002// +/fg_mrf2.m/1.1.1.1/Wed May 29 15:59:54 2002// +D diff --git a/sourcecodes/bnt-master/BNT/examples/static/fgraph/CVS/Repository b/sourcecodes/bnt-master/BNT/examples/static/fgraph/CVS/Repository new file mode 100644 index 00000000..14dfb0d0 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/fgraph/CVS/Repository @@ -0,0 +1 @@ +FullBNT/BNT/examples/static/fgraph diff --git a/sourcecodes/bnt-master/BNT/examples/static/fgraph/CVS/Root b/sourcecodes/bnt-master/BNT/examples/static/fgraph/CVS/Root new file mode 100644 index 00000000..f3bd14a6 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/fgraph/CVS/Root @@ -0,0 +1 @@ +:ext:nsaunier@bnt.cvs.sourceforge.net:/cvsroot/bnt diff --git a/sourcecodes/bnt-master/BNT/examples/static/fgraph/fg1.m b/sourcecodes/bnt-master/BNT/examples/static/fgraph/fg1.m new file mode 100644 index 00000000..0b8adc47 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/fgraph/fg1.m @@ -0,0 +1,98 @@ +% make an unrolled HMM, convert to factor graph, and check that +% loopy propagation on the fgraph gives the exact answers. + +seed = 1; +rand('state', seed); +randn('state', seed); + +T = 3; +Q = 3; +O = 3; +cts_obs = 0; +param_tying = 1; +bnet = mk_hmm_bnet(T, Q, O, cts_obs, param_tying); + +data = sample_bnet(bnet); + +fgraph = bnet_to_fgraph(bnet); +big_bnet = fgraph_to_bnet(fgraph); +% converting factor graph back does not recover the structure of the original bnet + +max_iter = 2*T; + +engine = {}; +engine{1} = jtree_inf_engine(bnet); +engine{2} = belprop_inf_engine(bnet, 'max_iter', max_iter); +engine{3} = belprop_fg_inf_engine(fgraph, 'max_iter', max_iter); +engine{4} = jtree_inf_engine(big_bnet); +nengines = length(engine); + +big_engine = 4; +fgraph_engine = 3; + + +N = 2*T; +evidence = cell(1,N); +onodes = bnet.observed; +evidence(onodes) = data(onodes); +hnodes = mysetdiff(1:N, onodes); + +bigN = length(big_bnet.dag); +big_evidence = cell(1, bigN); +big_evidence(onodes) = data(onodes); +big_evidence(N+1:end) = {1}; % factors are observed to be 1 + +ll = zeros(1, nengines); +for i=1:nengines + if i==big_engine + tic; [engine{i}, ll(i)] = enter_evidence(engine{i}, big_evidence); toc + else + tic; [engine{i}, ll(i)] = enter_evidence(engine{i}, evidence); toc + end +end + +% compare all engines to engine{1} + +% the log likelihood values may be bogus... +for i=2:nengines + %assert(approxeq(ll(1), ll(i))); +end + + +marg = zeros(T, nengines, Q); % marg(t,e,:) +for t=1:T + for e=1:nengines + m = marginal_nodes(engine{e}, t); + marg(t,e,:) = m.T; + end +end +marg + + +m = cell(nengines, T); +for i=1:T + for e=1:nengines + m{e,i} = marginal_nodes(engine{e}, hnodes(i)); + end + for e=2:nengines + assert(approxeq(m{e,i}.T, m{1,i}.T)); + end +end + +mpe = {}; +ll = zeros(1, nengines); +for e=1:nengines + if e==big_engine + mpe{e} = find_mpe(engine{e}, big_evidence); + mpe{e} = mpe{e}(1:N); % chop off dummy nodes + else + mpe{e} = find_mpe(engine{e}, evidence); + end +end + +% fgraph can't compute loglikelihood for software reasons +% jtree on the big_bnet gives the wrong ll +for e=2:nengines + %assert(approxeq(ll(1), ll(e))); + assert(approxeq(cell2num(mpe{1}), cell2num(mpe{e}))) +end diff --git a/sourcecodes/bnt-master/BNT/examples/static/fgraph/fg2.m b/sourcecodes/bnt-master/BNT/examples/static/fgraph/fg2.m new file mode 100644 index 00000000..c982f0c7 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/fgraph/fg2.m @@ -0,0 +1,104 @@ +% make a factor graph corresponding to an HMM, where we absorb the evidence up front, +% and then eliminate the observed nodes. +% Compare this with not absorbing the evidence. + +seed = 1; +rand('state', seed); +randn('state', seed); + +T = 3; +Q = 3; +O = 2; +cts_obs = 0; +param_tying = 1; +bnet = mk_hmm_bnet(T, Q, O, cts_obs, param_tying); +N = 2*T; +onodes = bnet.observed; +hnodes = mysetdiff(1:N, onodes); + +data = sample_bnet(bnet); + +init_factor = bnet.CPD{1}; +obs_factor = bnet.CPD{3}; +edge_factor = bnet.CPD{2}; % trans matrix + +nfactors = T; +nvars = T; % hidden only +G = zeros(nvars, nfactors); +G(1,1) = 1; +for t=1:T-1 + G(t:t+1, t+1)=1; +end + +node_sizes = Q*ones(1,T); + +% We tie params as follows: +% the first hidden node use init_factor (number 1) +% all hidden nodes on the backbone use edge_factor (number 2) +% all observed nodes use the same factor, namely obs_factor + +small_fg = mk_fgraph_given_ev(G, node_sizes, {init_factor, edge_factor}, {obs_factor}, data(onodes), ... + 'equiv_class', [1 2*ones(1,T-1)], 'ev_equiv_class', ones(1,T)); + +small_bnet = fgraph_to_bnet(small_fg); + +% don't pre-process evidence +big_fg = bnet_to_fgraph(bnet); +big_bnet = fgraph_to_bnet(big_fg); + + + +engine = {}; +engine{1} = jtree_inf_engine(bnet); +engine{2} = belprop_fg_inf_engine(small_fg, 'max_iter', 2*T); +engine{3} = jtree_inf_engine(small_bnet); +engine{4} = belprop_fg_inf_engine(big_fg, 'max_iter', 3*T); +engine{5} = jtree_inf_engine(big_bnet); +nengines = length(engine); + + +% on BN, use the original evidence +evidence = cell(1, 2*T); +evidence(onodes) = data(onodes); +tic; [engine{1}, ll(1)] = enter_evidence(engine{1}, evidence); toc + + +% on small_fg, we have already included the evidence +evidence = cell(1,T); +tic; [engine{2}, ll(2)] = enter_evidence(engine{2}, evidence); toc + + +% on small_bnet, we must add evidence to the dummy nodes +V = small_fg.nvars; +dummy = V+1:V+small_fg.nfactors; +N = max(dummy); +evidence = cell(1, N); +evidence(dummy) = {1}; +tic; [engine{3}, ll(3)] = enter_evidence(engine{3}, evidence); toc + + +% on big_fg, use the original evidence +evidence = cell(1, 2*T); +evidence(onodes) = data(onodes); +tic; [engine{4}, ll(4)] = enter_evidence(engine{4}, evidence); toc + + +% on big_bnet, we must add evidence to the dummy nodes +V = big_fg.nvars; +assert(V == 2*T); +dummy = V+1:V+big_fg.nfactors; +N = max(dummy); +evidence = cell(1, N); +evidence(onodes) = data(onodes); +evidence(dummy) = {1}; +tic; [engine{5}, ll(5)] = enter_evidence(engine{5}, evidence); toc + + +marg = zeros(T, nengines, Q); % marg(t,e,:) +for t=1:T + for e=1:nengines + m = marginal_nodes(engine{e}, t); + marg(t,e,:) = m.T; + end +end +marg(:,:,1) diff --git a/sourcecodes/bnt-master/BNT/examples/static/fgraph/fg3.m b/sourcecodes/bnt-master/BNT/examples/static/fgraph/fg3.m new file mode 100644 index 00000000..ec3f28f2 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/fgraph/fg3.m @@ -0,0 +1,83 @@ +% make a factor graph corresponding to an HMM with Gaussian outputs, where we absorb the +% evidence up front + +seed = 1; +rand('state', seed); +randn('state', seed); + +T = 3; +Q = 3; +O = 2; +cts_obs = 1; +param_tying = 1; +bnet = mk_hmm_bnet(T, Q, O, cts_obs, param_tying); +N = 2*T; +onodes = bnet.observed; +hnodes = mysetdiff(1:N, onodes); + +data = sample_bnet(bnet); + +init_factor = bnet.CPD{1}; +obs_factor = bnet.CPD{3}; +edge_factor = bnet.CPD{2}; % trans matrix + +nfactors = T; +nvars = T; % hidden only +G = zeros(nvars, nfactors); +G(1,1) = 1; +for t=1:T-1 + G(t:t+1, t+1)=1; +end + +node_sizes = Q*ones(1,T); + +% We tie params as follows: +% the first hidden node use init_factor (number 1) +% all hidden nodes on the backbone use edge_factor (number 2) +% all observed nodes use the same factor, namely obs_factor + +small_fg = mk_fgraph_given_ev(G, node_sizes, {init_factor, edge_factor}, {obs_factor}, data(onodes), ... + 'equiv_class', [1 2*ones(1,T-1)], 'ev_equiv_class', ones(1,T)); + +small_bnet = fgraph_to_bnet(small_fg); + +% don't pre-process evidence +% big_fg = bnet_to_fgraph(bnet); % can't handle Gaussian node + + +engine = {}; +engine{1} = jtree_inf_engine(bnet); +engine{2} = belprop_fg_inf_engine(small_fg, 'max_iter', 2*T); +engine{3} = jtree_inf_engine(small_bnet); +nengines = length(engine); + + +% on BN, use the original evidence +evidence = cell(1, 2*T); +evidence(onodes) = data(onodes); +tic; [engine{1}, ll(1)] = enter_evidence(engine{1}, evidence); toc + + +% on small_fg, we have already included the evidence +evidence = cell(1,T); +tic; [engine{2}, ll(2)] = enter_evidence(engine{2}, evidence); toc + + +% on small_bnet, we must add evidence to the dummy nodes +V = small_fg.nvars; +dummy = V+1:V+small_fg.nfactors; +N = max(dummy); +evidence = cell(1, N); +evidence(dummy) = {1}; +tic; [engine{3}, ll(3)] = enter_evidence(engine{3}, evidence); toc + + + +marg = zeros(T, nengines, Q); % marg(t,e,:) +for t=1:T + for e=1:nengines + m = marginal_nodes(engine{e}, t); + marg(t,e,:) = m.T; + end +end +marg(:,:,1) diff --git a/sourcecodes/bnt-master/BNT/examples/static/fgraph/fg_mrf1.m b/sourcecodes/bnt-master/BNT/examples/static/fgraph/fg_mrf1.m new file mode 100644 index 00000000..2e204a60 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/fgraph/fg_mrf1.m @@ -0,0 +1,113 @@ +seed = 0; +rand('state', seed); +randn('state', seed); + +nrows = 3; +ncols = 3; +npixels = nrows*ncols; + +% we number pixels in transposed raster scan order (top to bottom, left to right) + +% hidden var +HV = reshape(1:npixels, nrows, ncols); +% observed var +OV = reshape(1:npixels, nrows, ncols) + length(HV(:)); + +% observed factor +OF = reshape(1:npixels, nrows, ncols); +% vertical edge factor VEF(i,j) is the factor for edge HV(i,j) - HV(i+1,j) +VEF = reshape((1:(nrows-1)*ncols), nrows-1, ncols) + length(OF(:)); +% horizontal edge factor HEF(i,j) is the factor for edge HV(i,j) - HV(i,j+1) +HEF = reshape((1:nrows*(ncols-1)), nrows, ncols-1) + length(OF(:)) + length(VEF(:)); + +nvars = length(HV(:))+length(OV(:)); +assert(nvars == 2*npixels); +nfac = length(OF(:)) + length(VEF(:)) + length(HEF(:)); + +K = 2; % number of discrete values for the hidden vars +%O = 1; % each observed pixel is a scalar +O = 2; % each observed pixel is binary + +factors = cell(1,3); + +% hidden states generate observed 0 or 1 plus noise +%factors{2} = cond_gauss1_kernel(K, O, 'mean', [0 1], 'cov', [0.1 0.1]); +pnoise = 0.2; +factors{1} = tabular_kernel([K O], [1-pnoise pnoise; pnoise 1-pnoise]); +ofactor = 1; + +% encourage compatibility between neighboring vertical pixels +factors{2} = tabular_kernel([K K], [0.8 0.2; 0.2 0.8]); +vedge_factor = 2; + +%% no constraint between neighboring horizontal pixels +%factors{3} = tabular_kernel([K K], [0.5 0.5; 0.5 0.5]); + +factors{3} = tabular_kernel([K K], [0.8 0.2; 0.2 0.8]); +hedge_factor = 3; + + + +factor_ndx = zeros(1, 3); +G = zeros(nvars, nfac); +ns = [K*ones(1,length(HV(:))) O*ones(1,length(OV(:)))]; + +N = length(ns); +%cnodes = OV(:); +cnodes = []; +dnodes = 1:N; + +for i=1:nrows + for j=1:ncols + G([HV(i,j), OV(i,j)], OF(i,j)) = 1; + factor_ndx(OF(i,j)) = ofactor; + + if i < nrows + G(HV(i:i+1,j), VEF(i,j)) = 1; + factor_ndx(VEF(i,j)) = vedge_factor; + end + + if j < ncols + G(HV(i,j:j+1), HEF(i,j)) = 1; + factor_ndx(HEF(i,j)) = hedge_factor; + end + + end +end + + +fg = mk_fgraph(G, ns, factors, 'discrete', dnodes, 'equiv_class', factor_ndx); + +if 1 + % make image with vertical stripes + I = zeros(nrows, ncols); + for j=1:2:ncols + I(:,j) = 1; + end +else + % make image with square in middle + I = zeros(nrows, ncols); + I(3:6,3:6) = 1; +end + + +% corrupt image +O = mod(I + (rand(nrows,ncols)> (1-pnoise)), 2); + +maximize = 1; +engine = belprop_fg_inf_engine(fg, 'maximize', maximize, 'max_iter', npixels*5); + +evidence = cell(1, nvars); +onodes = OV(:); +evidence(onodes) = num2cell(O+1); % values must be in range {1,2} + +engine = enter_evidence(engine, evidence); + +for i=1:nrows + for j=1:ncols + m = marginal_nodes(engine, HV(i,j)); + Ihat(i,j) = argmax(m.T)-1; + end +end + +Ihat diff --git a/sourcecodes/bnt-master/BNT/examples/static/fgraph/fg_mrf2.m b/sourcecodes/bnt-master/BNT/examples/static/fgraph/fg_mrf2.m new file mode 100644 index 00000000..1f8981a0 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/fgraph/fg_mrf2.m @@ -0,0 +1,150 @@ +seed = 0; +rand('state', seed); +randn('state', seed); + +nrows = 5; +ncols = 5; +npixels = nrows*ncols; + +% we number pixels in transposed raster scan order (top to bottom, left to right) + +% H(i,j) is the number of the hidden node at (i,j) +H = reshape(1:npixels, nrows, ncols); +% O(i,j) is the number of the obsevred node at (i,j) +O = reshape(1:npixels, nrows, ncols) + length(H(:)); + + +% Make a Bayes net where each hidden pixel generates an observed pixel +% but there are no connections between the hidden pixels. +% We use this just to generate noisy versions of known images. +N = 2*npixels; +dag = zeros(N); +for i=1:nrows + for j=1:ncols + dag(H(i,j), O(i,j)) = 1; + end +end + + +K = 2; % number of discrete values for the hidden vars +ns = ones(N,1); +ns(H(:)) = K; +ns(O(:)) = 1; + + +% make image with vertical stripes +I = zeros(nrows, ncols); +for j=1:2:ncols + I(:,j) = 1; +end + +% each "hidden" node will be instantiated to the pixel in the known image +% each observed node has conditional Gaussian distribution +eclass = ones(1,N); +%eclass(H(:)) = 1; +%eclass(O(:)) = 2; +eclass(H(:)) = 1:npixels; +eclass(O(:)) = npixels+1; +bnet = mk_bnet(dag, ns, 'discrete', H(:), 'equiv_class', eclass); + + +%bnet.CPD{1} = tabular_CPD(bnet, H(1), 'CPT', normalise(ones(1,K))); +for i=1:nrows + for j=1:ncols + bnet.CPD{H(i,j)} = root_CPD(bnet, H(i,j), I(i,j) + 1); + end +end + +% If H(i,j)=1, O(i,j)=+1 plus noise +% If H(i,j)=2, O(i,j)=-1 plus noise +sigma = 0.5; +bnet.CPD{eclass(O(1,1))} = gaussian_CPD(bnet, O(1,1), 'mean', [1 -1], 'cov', reshape(sigma*ones(1,K), [1 1 K])); +ofactor = bnet.CPD{eclass(O(1,1))}; +%ofactor = gaussian_CPD('self', 2, 'dps', 1, 'cps', [], 'sz', [K O], 'mean', [1 -1], 'cov', reshape(sigma*ones(1,K), [1 1 K))); + + +data = sample_bnet(bnet); +img = reshape(data(O(:)), nrows, ncols) + + + + +%%%%%%%%%%%%%%%%%%%%%%%%%%% + +% Now create MRF represented as a factor graph to try and recover the scene + +% VEF(i,j) is the number of the factor for the vertical edge between HV(i,j) - HV(i+1,j) +VEF = reshape((1:(nrows-1)*ncols), nrows-1, ncols); +% HEF(i,j) is the number of the factor for the horizontal edge between HV(i,j) - HV(i,j+1) +HEF = reshape((1:nrows*(ncols-1)), nrows, ncols-1) + length(VEF(:)); + +nvars = npixels; +nfac = length(VEF(:)) + length(HEF(:)); + +G = zeros(nvars, nfac); +N = length(ns); +eclass = zeros(1, nfac); % eclass(i)=j means factor i gets its params from factors{j} +vfactor_ndx = 1; % all vertcial edges get their params from factors{1} +hfactor_ndx = 2; % all vertcial edges get their params from factors{2} +for i=1:nrows + for j=1:ncols + if i < nrows + G(H(i:i+1,j), VEF(i,j)) = 1; + eclass(VEF(i,j)) = vfactor_ndx; + end + if j < ncols + G(H(i,j:j+1), HEF(i,j)) = 1; + eclass(HEF(i,j)) = hfactor_ndx; + end + end +end + + +% "kitten raised in cage" prior - more likely to see continguous vertical lines +vfactor = tabular_kernel([K K], softeye(K, 0.9)); +hfactor = tabular_kernel([K K], softeye(K, 0.5)); +factors = cell(1,2); +factors{vfactor_ndx} = vfactor; +factors{hfactor_ndx} = hfactor; + +ev_eclass = ones(1,N); % every observation factor gets is params from ofactor +ns = K*ones(1,nvars); +%fg = mk_fgraph_given_ev(G, ns, factors, {ofactor}, num2cell(img), 'equiv_class', eclass, 'ev_equiv_class', ev_eclass); +fg = mk_fgraph_given_ev(G, ns, factors, {ofactor}, img, 'equiv_class', eclass, 'ev_equiv_class', ev_eclass); + +bnet2 = fgraph_to_bnet(fg); + +% inference + + +maximize = 1; + +engine = {}; +engine{1} = belprop_fg_inf_engine(fg, 'max_iter', npixels*2); +engine{2} = jtree_inf_engine(bnet2); +nengines = length(engine); + +% on fg, we have already included the evidence +evidence = cell(1,npixels); +tic; [engine{1}, ll(1)] = enter_evidence(engine{1}, evidence, 'maximize', maximize); toc + + +% on bnet2, we must add evidence to the dummy nodes +V = fg.nvars; +dummy = V+1:V+fg.nfactors; +N = max(dummy); +evidence = cell(1, N); +evidence(dummy) = {1}; +tic; [engine{2}, ll(2)] = enter_evidence(engine{2}, evidence); toc + + +Ihat = zeros(nrows, ncols, nengines); +for e=1:nengines + for i=1:nrows + for j=1:ncols + m = marginal_nodes(engine{e}, H(i,j)); + Ihat(i,j,e) = argmax(m.T)-1; + end + end +end +Ihat diff --git a/sourcecodes/bnt-master/BNT/examples/static/gaussian1.m b/sourcecodes/bnt-master/BNT/examples/static/gaussian1.m new file mode 100644 index 00000000..20bb3007 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/gaussian1.m @@ -0,0 +1,34 @@ +% Make the following network (from Jensen (1996) p84 fig 4.17) +% 1 +% / | \ +% 2 3 4 +% | | | +% 5 6 7 +% \/ \/ +% 8 9 +% where all arcs point downwards + + +N = 9; +dag = zeros(N,N); +dag(1,2)=1; dag(1,3)=1; dag(1,4)=1; +dag(2,5)=1; dag(3,6)=1; dag(4,7)=1; +dag(5,8)=1; dag(6,8)=1; dag(6,9)=1; dag(7,9) = 1; + +ns = [5 4 3 2 2 1 2 2 2]; % vector-valued nodes +%ns = ones(1,9); % scalar nodes +dnodes = []; + +bnet = mk_bnet(dag, ns, 'discrete', []); +rand('state', 0); +randn('state', 0); +for i=1:N + bnet.CPD{i} = gaussian_CPD(bnet, i); +end + +clear engine; +engine{1} = gaussian_inf_engine(bnet); +engine{2} = jtree_inf_engine(bnet); + +[err, time] = cmp_inference_static(bnet, engine); + diff --git a/sourcecodes/bnt-master/BNT/examples/static/gaussian2.m b/sourcecodes/bnt-master/BNT/examples/static/gaussian2.m new file mode 100644 index 00000000..157a86cb --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/gaussian2.m @@ -0,0 +1,40 @@ +% Make the following network (from Jensen (1996) p84 fig 4.17) +% 1 +% / | \ +% 2 3 4 +% | | | +% 5 6 7 +% \/ \/ +% 8 9 +% where all arcs point downwards + + +N = 9; +dag = zeros(N,N); +dag(1,2)=1; dag(1,3)=1; dag(1,4)=1; +dag(2,5)=1; dag(3,6)=1; dag(4,7)=1; +dag(5,8)=1; dag(6,8)=1; dag(6,9)=1; dag(7,9) = 1; + +ns = [5 4 3 2 2 1 2 2 2]; % vector-valued nodes +%ns = ones(1,9); % scalar nodes +dnodes = []; + +bnet = mk_bnet(dag, ns, 'discrete', []); +rand('state', 0); +randn('state', 0); +for i=1:N + bnet.CPD{i} = gaussian_CPD(bnet, i); +end + +clear engine; +engine{1} = gaussian_inf_engine(bnet); +engine{2} = jtree_inf_engine(bnet); + +[err, time] = cmp_inference_static(bnet, engine); + +Nsamples = 100; +samples = cell(N, Nsamples); +for s=1:Nsamples + samples(:,s) = sample_bnet(bnet); +end +bnet2 = learn_params(bnet, samples); diff --git a/sourcecodes/bnt-master/BNT/examples/static/gibbs_test1.m b/sourcecodes/bnt-master/BNT/examples/static/gibbs_test1.m new file mode 100644 index 00000000..7c961b3c --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/gibbs_test1.m @@ -0,0 +1,107 @@ +function gibbs_test1() + +disp('gibbs test 1') + +rand('state', 0); +randn('state', 0); + +%[bnet onodes hnodes qnodes] = gibbs_ex_1; +[bnet onodes hnodes qnodes] = gibbs_ex_2; + +je = jtree_inf_engine(bnet); +ge = gibbs_sampling_inf_engine (bnet, 'T', 50, 'burnin', 0, ... + 'order', [2 2 1 2 1]); + +ev = sample_bnet(bnet); + +evidence = cell(length(bnet.dag), 1); +evidence(onodes) = ev(onodes); +[je lj] = enter_evidence(je, evidence); +[ge lg] = enter_evidence(ge, evidence); + + +mj = marginal_nodes(je, qnodes); + +[mg ge] = marginal_nodes (ge, qnodes); +for t = 1:100 + [mg ge] = marginal_nodes (ge, qnodes, 'reset_counts', 0); + diff = mj.T - mg.T; + err(t) = norm (diff(:), 1); +end +clf +plot(err); +%title('error vs num. Gibbs samples') + + +%%%%%%% + +function [bnet, onodes, hnodes, qnodes] = gibbs_ex_1 +% bnet = gibbs_ex_1 +% a simple network to test the gibbs sampling engine +% 1 +% / | \ +% 2 3 4 +% | | | +% 5 6 7 +% \/ \/ +% 8 9 +% where all arcs point downwards + +N = 9; +dag = zeros(N,N); +dag(1,2)=1; dag(1,3)=1; dag(1,4)=1; +dag(2,5)=1; dag(3,6)=1; dag(4,7)=1; +dag(5,8)=1; dag(6,8)=1; dag(6,9)=1; dag(7,9) = 1; + +onodes = 8:9; +hnodes = 1:7; +qnodes = [1 2 6]; +ns = [2 3 4 3 5 2 4 3 2]; + +eclass = [1 2 3 2 4 5 6 7 8]; + +bnet = mk_bnet (dag, ns, 'equiv_class', eclass); + +for i = 1:3 + bnet.CPD{i} = tabular_CPD(bnet, i); +end + +for i = 4:8 + bnet.CPD{i} = tabular_CPD(bnet, i+1); +end + + + +%%%%%%% + +function [bnet, onodes, hnodes, qnodes] = gibbs_ex_2 +% bnet = gibbs_ex_2 +% a very simple network +% +% 1 2 +% \ / +% 3 + +N = 3; +dag = zeros(N,N); +dag(1,3)=1; dag(2,3)=1; + +onodes = 3; +hnodes = 1:2; +qnodes = 1:2; +ns = [2 4 3]; + +eclass = [1 2 3]; + +bnet = mk_bnet (dag, ns, 'equiv_class', eclass); + +for i = 1:3 + bnet.CPD{i} = tabular_CPD(bnet, i); +end + + + + + + + diff --git a/sourcecodes/bnt-master/BNT/examples/static/learn1.m b/sourcecodes/bnt-master/BNT/examples/static/learn1.m new file mode 100644 index 00000000..d2b7522b --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/learn1.m @@ -0,0 +1,86 @@ +% Lawn sprinker example from Russell and Norvig p454 +% See www.cs.berkeley.edu/~murphyk/Bayes/usage.html for details. + +N = 4; +dag = zeros(N,N); +C = 1; S = 2; R = 3; W = 4; +dag(C,[R S]) = 1; +dag(R,W) = 1; +dag(S,W)=1; + +false = 1; true = 2; +ns = 2*ones(1,N); % binary nodes + +bnet = mk_bnet(dag, ns); +bnet.CPD{C} = tabular_CPD(bnet, C, [0.5 0.5]); +bnet.CPD{R} = tabular_CPD(bnet, R, [0.8 0.2 0.2 0.8]); +bnet.CPD{S} = tabular_CPD(bnet, S, [0.5 0.9 0.5 0.1]); +bnet.CPD{W} = tabular_CPD(bnet, W, [1 0.1 0.1 0.01 0 0.9 0.9 0.99]); + +CPT = cell(1,N); +for i=1:N + s=struct(bnet.CPD{i}); % violate object privacy + CPT{i}=s.CPT; +end + +% Generate training data +nsamples = 50; +samples = cell(N, nsamples); +for i=1:nsamples + samples(:,i) = sample_bnet(bnet); +end +data = cell2num(samples); + +% Make a tabula rasa +bnet2 = mk_bnet(dag, ns); +seed = 0; +rand('state', seed); +bnet2.CPD{C} = tabular_CPD(bnet2, C, 'clamped', 1, 'CPT', [0.5 0.5], ... + 'prior_type', 'dirichlet', 'dirichlet_weight', 0); +bnet2.CPD{R} = tabular_CPD(bnet2, R, 'prior_type', 'dirichlet', 'dirichlet_weight', 0); +bnet2.CPD{S} = tabular_CPD(bnet2, S, 'prior_type', 'dirichlet', 'dirichlet_weight', 0); +bnet2.CPD{W} = tabular_CPD(bnet2, W, 'prior_type', 'dirichlet', 'dirichlet_weight', 0); + + +% Find MLEs from fully observed data +bnet4 = learn_params(bnet2, samples); + +% Bayesian updating with 0 prior is equivalent to ML estimation +bnet5 = bayes_update_params(bnet2, samples); + +CPT4 = cell(1,N); +for i=1:N + s=struct(bnet4.CPD{i}); % violate object privacy + CPT4{i}=s.CPT; +end + +CPT5 = cell(1,N); +for i=1:N + s=struct(bnet5.CPD{i}); % violate object privacy + CPT5{i}=s.CPT; + assert(approxeq(CPT5{i}, CPT4{i})) +end + + +if 1 +% Find MLEs from partially observed data + +% hide 50% of the nodes +samplesH = samples; +hide = rand(N, nsamples) > 0.5; +[I,J]=find(hide); +for k=1:length(I) + samplesH{I(k), J(k)} = []; +end + +engine = jtree_inf_engine(bnet2); +max_iter = 5; +[bnet6, LL] = learn_params_em(engine, samplesH, max_iter); + +CPT6 = cell(1,N); +for i=1:N + s=struct(bnet6.CPD{i}); % violate object privacy + CPT6{i}=s.CPT; +end + +end diff --git a/sourcecodes/bnt-master/BNT/examples/static/lw1.m b/sourcecodes/bnt-master/BNT/examples/static/lw1.m new file mode 100644 index 00000000..a6a35577 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/lw1.m @@ -0,0 +1,51 @@ +% Evaluate effectiveness of likelihood weighting on the lawn sprinkler example + +N = 4; +dag = zeros(N,N); +C = 1; R = 2; S = 3; W = 4; +dag(C,[R S]) = 1; +dag(R,W) = 1; +dag(S,W)=1; + +false = 1; true = 2; +ns = 2*ones(1,N); % binary nodes + +bnet = mk_bnet(dag, ns); +bnet.CPD{C} = tabular_CPD(bnet, C, [0.5 0.5]); +bnet.CPD{R} = tabular_CPD(bnet, R, [0.8 0.2 0.2 0.8]); +bnet.CPD{S} = tabular_CPD(bnet, S, [0.5 0.9 0.5 0.1]); +bnet.CPD{W} = tabular_CPD(bnet, W, [1 0.1 0.1 0.01 0 0.9 0.9 0.99]); + + +clear engine; +engine{1} = jtree_inf_engine(bnet); +engine{2} = likelihood_weighting_inf_engine(bnet); + +nengines = length(engine); +m = cell(1, nengines); +ll = zeros(1, nengines); + +evidence = cell(1,N); +%evidence{C} = true; % evidence at the top is the easiest +evidence{W} = true; % evidence at the bottom is the hardets + +query = [R]; + +i=1; +engine{i} = enter_evidence(engine{i}, evidence); +exact_m = marginal_nodes(engine{i}, query); + +i=2; +samples = 100:100:500; +err = zeros(1, length(samples)); +for j=1:length(samples) + nsamples = samples(j); + engine{i} = enter_evidence(engine{i}, evidence, nsamples); + approx_m = marginal_nodes(engine{i}, query); + a1=approxeq(approx_m.T,exact_m.T,1e-1); + a2=approxeq(approx_m.T,exact_m.T,1e-2); + a3=approxeq(approx_m.T,exact_m.T,1e-3); + e = sum(abs(approx_m.T(:) - exact_m.T(:))); + fprintf('%d samples, 1dp %d, 2dp %d, 3dp %d, err %f\n', nsamples, a1, a2, a3, e); + err(j) = e; +end diff --git a/sourcecodes/bnt-master/BNT/examples/static/mfa1.m b/sourcecodes/bnt-master/BNT/examples/static/mfa1.m new file mode 100644 index 00000000..17eb8667 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/mfa1.m @@ -0,0 +1,80 @@ +% Factor analysis +% Z -> X, Z in R^k, X in R^D, k << D (high dimensional observations explained by small source) +% Z ~ N(0,I), X|Z ~ N(L z, Psi), where Psi is diagonal. +% +% Mixtures of FA +% Now X|Z,W=i ~ N(mu(i) + L(i) Z, Psi(i)) +% +% We compare to Zoubin Ghahramani's code. + +randn('state', 0); +max_iter = 3; +M = 2; +k = 3; +D = 5; + +n = 5; +X1 = randn(n, D); +X2 = randn(n, D) + 2; % move the mean to (2,2,2...) +X = [X1; X2]; +N = size(X, 1); + +% initialise as in mfa +tiny=exp(-700); +mX = mean(X); +cX=cov(X); +scale=det(cX)^(1/D); +randn('state',0); % must reset seed here so initial params are identical to mfa +L0=randn(D*M,k)*sqrt(scale/k); +W0 = permute(reshape(L0, [D M k]), [1 3 2]); % use D,K,M +Psi0=diag(cX)+tiny; +Pi0=ones(M,1)/M; +Mu0=randn(M,D)*sqrtm(cX)+ones(M,1)*mX; + +[Lh1, Ph1, Mu1, Pi1, LL1] = mfa(X,M,k,max_iter); +Lh1 = permute(reshape(Lh1, [D M k]), [1 3 2]); % use D,K,M + + +ns = [M k D]; +dag = zeros(3); +dag(1,3) = 1; +dag(2,3) = 1; +dnodes = 1; +onodes = 3; + +bnet = mk_bnet(dag, ns, 'discrete', dnodes, 'observed', onodes); +bnet.CPD{1} = tabular_CPD(bnet, 1, Pi0); + +%bnet.CPD{2} = gaussian_CPD(bnet, 2, zeros(k, 1), eye(k), [], 'diag', 'untied', 'clamp_mean', 'clamp_cov'); + +bnet.CPD{2} = gaussian_CPD(bnet, 2, 'mean', zeros(k, 1), 'cov', eye(k), 'cov_type', 'diag', ... + 'cov_prior_weight', 0, 'clamp_mean', 1, 'clamp_cov', 1); + +%bnet.CPD{3} = gaussian_CPD(bnet, 3, Mu0', repmat(diag(Psi0), [1 1 M]), W0, 'diag', 'tied'); + +bnet.CPD{3} = gaussian_CPD(bnet, 3, 'mean', Mu0', 'cov', repmat(diag(Psi0), [1 1 M]), ... + 'weights', W0, 'cov_type', 'diag', 'cov_prior_weight', 0, 'tied_cov', 1); + +engine = jtree_inf_engine(bnet); +evidence = cell(3, N); +evidence(3,:) = num2cell(X', 1); + +[bnet2, LL2, engine2] = learn_params_em(engine, evidence, max_iter); + +s = struct(bnet2.CPD{1}); +Pi2 = s.CPT(:); +s = struct(bnet2.CPD{3}); +Mu2 = s.mean; +W2 = s.weights; +Sigma2 = s.cov; + + +% Compare to Zoubin's code +assert(approxeq(LL1,LL2)); +for i=1:M + assert(approxeq(W2(:,:,i), Lh1(:,:,i))); + assert(approxeq(Sigma2(:,:,i), diag(Ph1))); + assert(approxeq(Mu2(:,i), Mu1(i,:))); + assert(approxeq(Pi2(:), Pi1(:))); +end + diff --git a/sourcecodes/bnt-master/BNT/examples/static/mixexp1.m b/sourcecodes/bnt-master/BNT/examples/static/mixexp1.m new file mode 100644 index 00000000..ee66610c --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/mixexp1.m @@ -0,0 +1,72 @@ +% Fit a piece-wise linear regression model. +% Here is the model +% +% X \ +% | | +% Q | +% | / +% Y +% +% where all arcs point down. +% We condition everything on X, so X is a root node. Q is a softmax, and Y is a linear Gaussian. +% Q is hidden, X and Y are observed. + +X = 1; +Q = 2; +Y = 3; +dag = zeros(3,3); +dag(X,[Q Y]) = 1; +dag(Q,Y) = 1; +ns = [1 2 1]; % make X and Y scalars, and have 2 experts +dnodes = [2]; +onodes = [1 3]; +bnet = mk_bnet(dag, ns, 'discrete', dnodes, 'observed', onodes); + + +w = [-5 5]; % w(:,i) is the normal vector to the i'th decisions boundary +b = [0 0]; % b(i) is the offset (bias) to the i'th decisions boundary + +mu = [0 0]; +sigma = 1; +Sigma = repmat(sigma*eye(ns(Y)), [ns(Y) ns(Y) ns(Q)]); +W = [-1 1]; +W2 = reshape(W, [ns(Y) ns(X) ns(Q)]); + +bnet.CPD{1} = root_CPD(bnet, 1); +bnet.CPD{2} = softmax_CPD(bnet, 2, w, b); +bnet.CPD{3} = gaussian_CPD(bnet, 3, 'mean', mu, 'cov', Sigma, 'weights', W2); + + + +% Check inference + +x = 0.1; +ystar = 1; + +engine = jtree_inf_engine(bnet); +[engine, loglik] = enter_evidence(engine, {x, [], ystar}); +Qpost = marginal_nodes(engine, 2); + +% eta(i,:) = softmax (gating) params for expert i +eta = [b' w']; + +% theta(i,:) = regression vector for expert i +theta = [mu' W']; + +% yhat(i) = E[y | Q=i, x] = prediction of i'th expert +x1 = [1 x]'; +yhat = theta * x1; + +% gate_prior(i,:) = Pr(Q=i | x) +gate_prior = normalise(exp(eta * x1)); + +% cond_lik(i) = Pr(y | Q=i, x) +cond_lik = (1/(sqrt(2*pi)*sigma)) * exp(-(0.5/sigma^2) * ((ystar - yhat) .* (ystar - yhat))); + +% gate_posterior(i,:) = Pr(Q=i | x, y) +[gate_posterior, lik] = normalise(gate_prior .* cond_lik); + +assert(approxeq(gate_posterior(:), Qpost.T(:))); +assert(approxeq(log(lik), loglik)); + + diff --git a/sourcecodes/bnt-master/BNT/examples/static/mixexp2.m b/sourcecodes/bnt-master/BNT/examples/static/mixexp2.m new file mode 100644 index 00000000..6bcbc646 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/mixexp2.m @@ -0,0 +1,104 @@ +% Fit a piece-wise linear regression model. +% Here is the model +% +% X \ +% | | +% Q | +% | / +% Y +% +% where all arcs point down. +% We condition everything on X, so X is a root node. Q is a softmax, and Y is a linear Gaussian. +% Q is hidden, X and Y are observed. + +X = 1; +Q = 2; +Y = 3; +dag = zeros(3,3); +dag(X,[Q Y]) = 1; +dag(Q,Y) = 1; +ns = [1 2 1]; % make X and Y scalars, and have 2 experts +dnodes = [2]; +onodes = [1 3]; +bnet = mk_bnet(dag, ns, 'discrete', dnodes, 'observed', onodes); + +IRLS_iter = 10; +clamped = 0; + +bnet.CPD{1} = root_CPD(bnet, 1); + +if 0 + % start with good initial params + w = [-5 5]; % w(:,i) is the normal vector to the i'th decisions boundary + b = [0 0]; % b(i) is the offset (bias) to the i'th decisions boundary + + mu = [0 0]; + sigma = 1; + Sigma = repmat(sigma*eye(ns(Y)), [ns(Y) ns(Y) ns(Q)]); + W = [-1 1]; + W2 = reshape(W, [ns(Y) ns(X) ns(Q)]); + + bnet.CPD{2} = softmax_CPD(bnet, 2, w, b, clamped, IRLS_iter); + bnet.CPD{3} = gaussian_CPD(bnet, 3, mu, Sigma, W2); +else + % start with rnd initial params + rand('state', 0); + randn('state', 0); + bnet.CPD{2} = softmax_CPD(bnet, 2, 'clamped', clamped, 'max_iter', IRLS_iter); + bnet.CPD{3} = gaussian_CPD(bnet, 3); +end + + + +load('C:/Users/jziebrth/Documents/data/BNW/BNT/bnt-master-octave/bnt-master/BNT/examples/static/Misc/mixexp_data.txt', '-ascii'); +% Just use 1/10th of the data, to speed things up +data = mixexp_data(1:10:end, :); +%data = mixexp_data; + +%plot(data(:,1), data(:,2), '.') + + +s = struct(bnet.CPD{2}); % violate object privacy +%eta0 = [s.glim.b1; s.glim.w1]'; +eta0 = [s.glim{1}.b1; s.glim{1}.w1]'; +s = struct(bnet.CPD{3}); % violate object privacy +W = reshape(s.weights, [1 2]); +theta0 = [s.mean; W]'; + +%figure(1) +%mixexp_plot(theta0, eta0, data); +%suptitle('before learning') + +ncases = size(data, 1); +cases = cell(3, ncases); +cases([1 3], :) = num2cell(data'); + +engine = jtree_inf_engine(bnet); + +% log lik before learning +ll = 0; +for l=1:ncases + ev = cases(:,l); + [engine, loglik] = enter_evidence(engine, ev); + ll = ll + loglik; +end + +% do learning +max_iter = 5; +[bnet2, LL2] = learn_params_em(engine, cases, max_iter); + +s = struct(bnet2.CPD{2}); +%eta2 = [s.glim.b1; s.glim.w1]'; +eta2 = [s.glim{1}.b1; s.glim{1}.w1]'; +s = struct(bnet2.CPD{3}); +W = reshape(s.weights, [1 2]); +theta2 = [s.mean; W]'; + +%figure(2) +%mixexp_plot(theta2, eta2, data); +%suptitle('after learning') + +fprintf('mixexp2: loglik before learning %f, after %d iters %f\n', ll, length(LL2), LL2(end)); + + + diff --git a/sourcecodes/bnt-master/BNT/examples/static/mixexp3.m b/sourcecodes/bnt-master/BNT/examples/static/mixexp3.m new file mode 100644 index 00000000..a6ce1a4b --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/mixexp3.m @@ -0,0 +1,52 @@ +% Fit a piece-wise linear regression model. +% Here is the model +% +% X \ +% | | +% Q | +% | / +% Y +% +% where all arcs point down. +% We condition everything on X, so X is a root node. Q is a softmax, and Y is a linear Gaussian. +% Q is hidden, X and Y are observed. + +X = 1; +Q = 2; +Y = 3; +dag = zeros(3,3); +dag(X,[Q Y]) = 1; +dag(Q,Y) = 1; +ns = [1 2 1]; % make X and Y scalars, and have 2 experts +dnodes = [2]; +onodes = [1 3]; +bnet = mk_bnet(dag, ns, 'discrete', dnodes, 'observed', onodes); + +IRLS_iter = 10; +clamped = 0; + +bnet.CPD{1} = root_CPD(bnet, 1); + +% start with good initial params +w = [-5 5]; % w(:,i) is the normal vector to the i'th decisions boundary +b = [0 0]; % b(i) is the offset (bias) to the i'th decisions boundary + +mu = [0 0]; +sigma = 1; +Sigma = repmat(sigma*eye(ns(Y)), [ns(Y) ns(Y) ns(Q)]); +W = [-1 1]; +W2 = reshape(W, [ns(Y) ns(X) ns(Q)]); + +bnet.CPD{2} = softmax_CPD(bnet, 2, w, b, clamped, IRLS_iter); +bnet.CPD{3} = gaussian_CPD(bnet, 3, 'mean', mu, 'cov', Sigma, 'weights', W2); + + +engine = jtree_inf_engine(bnet); + +evidence = cell(1,3); +evidence{X} = 0.68; + +engine = enter_evidence(engine, evidence); + +m = marginal_nodes(engine, Y); +m.mu diff --git a/sourcecodes/bnt-master/BNT/examples/static/mog1.m b/sourcecodes/bnt-master/BNT/examples/static/mog1.m new file mode 100644 index 00000000..442f067b --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/mog1.m @@ -0,0 +1,81 @@ +% Fit a mixture of Gaussians using netlab and BNT + +rand('state', 0); +randn('state', 0); + +% Q -> Y +ncenters = 2; dim = 2; +cov_type = 'full'; + +% Generate the data from a mixture of 2 Gaussians +%mu = randn(dim, ncenters); +mu = zeros(dim, ncenters); +mu(:,1) = [-1 -1]'; +mu(:,1) = [1 1]'; +Sigma = repmat(0.1*eye(dim),[1 1 ncenters]); +ndat1 = 8; ndat2 = 8; +%ndat1 = 2; ndat2 = 2; +ndata = ndat1+ndat2; +x1 = gsamp(mu(:,1), Sigma(:,:,1), ndat1); +x2 = gsamp(mu(:,2), Sigma(:,:,2), ndat2); +data = [x1; x2]; +%plot(x1(:,1),x1(:,2),'ro', x2(:,1),x2(:,2),'bx') + +% Fit using netlab +max_iter = 3; +mix = gmm(dim, ncenters, cov_type); +options = foptions; +options(1) = 1; % verbose +options(14) = max_iter; + +% extract initial params +%mix = gmminit(mix, x, options); % Initialize with K-means +mu0 = mix.centres'; +pi0 = mix.priors(:); +Sigma0 = mix.covars; % repmat(eye(dim), [1 1 ncenters]); + +[mix, options] = gmmem(mix, data, options); + +% Final params +ll1 = options(8); +mu1 = mix.centres'; +pi1 = mix.priors(:); +Sigma1 = mix.covars; + + + + +% BNT + +dag = zeros(2); +dag(1,2) = 1; +node_sizes = [ncenters dim]; +discrete_nodes = 1; +onodes = 2; + +bnet = mk_bnet(dag, node_sizes, 'discrete', discrete_nodes, 'observed', onodes); +bnet.CPD{1} = tabular_CPD(bnet, 1, pi0); +bnet.CPD{2} = gaussian_CPD(bnet, 2, 'mean', mu0, 'cov', Sigma0, 'cov_type', cov_type, ... + 'cov_prior_weight', 0); + +engine = jtree_inf_engine(bnet); + +evidence = cell(2, ndata); +evidence(2,:) = num2cell(data', 1); + +[bnet2, LL] = learn_params_em(engine, evidence, max_iter); + +ll2 = LL(end); +s1 = struct(bnet2.CPD{1}); +pi2 = s1.CPT(:); + +s2 = struct(bnet2.CPD{2}); +mu2 = s2.mean; +Sigma2 = s2.cov; + +% assert(approxeq(ll1, ll2)); % gmmem returns the value after the final M step, GMT before +assert(approxeq(mu1, mu2)); +assert(approxeq(Sigma1, Sigma2)) +assert(approxeq(pi1, pi2)) + + diff --git a/sourcecodes/bnt-master/BNT/examples/static/mpe1.m b/sourcecodes/bnt-master/BNT/examples/static/mpe1.m new file mode 100644 index 00000000..2d393c9a --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/mpe1.m @@ -0,0 +1,45 @@ +seed = 1; +rand('state', seed); +randn('state', seed); + +N = 4; +dag = zeros(N,N); +C = 1; S = 2; R = 3; W = 4; +dag(C,[R S]) = 1; +dag(R,W) = 1; +dag(S,W)=1; + +false = 1; true = 2; +ns = 2*ones(1,N); % binary nodes + +bnet = mk_bnet(dag, ns); +if 0 + bnet.CPD{C} = tabular_CPD(bnet, C, [0.5 0.5]); + bnet.CPD{R} = tabular_CPD(bnet, R, [0.8 0.2 0.2 0.8]); + bnet.CPD{S} = tabular_CPD(bnet, S, [0.5 0.9 0.5 0.1]); + bnet.CPD{W} = tabular_CPD(bnet, W, [1 0.1 0.1 0.01 0 0.9 0.9 0.99]); +else + for i=1:N, bnet.CPD{i} = tabular_CPD(bnet, i); end +end + + + +evidence = cell(1,N); +onodes = [1 3]; +data = sample_bnet(bnet); +evidence(onodes) = data(onodes); + +clear engine; +engine{1} = belprop_inf_engine(bnet); +engine{2} = jtree_inf_engine(bnet); +engine{3} = global_joint_inf_engine(bnet); +engine{4} = var_elim_inf_engine(bnet); +E = length(engine); + +clear mpe; +for e=1:E + mpe{e} = find_mpe(engine{e}, evidence); +end +for e=2:E + assert(isequal(mpe{1}, mpe{e})) +end diff --git a/sourcecodes/bnt-master/BNT/examples/static/mpe2.m b/sourcecodes/bnt-master/BNT/examples/static/mpe2.m new file mode 100644 index 00000000..032cc0b1 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/mpe2.m @@ -0,0 +1,53 @@ +% Computing most probable explanation. + +% If you don't break ties consistently, loopy can give wrong mpe +% even though the graph has no cycles, and even though the max-marginals are the same. +% This example was contributed by Wentau Yih <wtyih@yahoo.com> 29 Jan 02. + +% define loop-free graph structure (all edges point down) +% +% Xe1 Xe2 +% | | +% E1 E2 +% \ / +% R1 +% | +% Xr1 + +N = 6; +dag = zeros(N,N); +Xe1 = 1; Xe2 = 2; E1 = 3; E2 = 4; R1 = 5; Xr1 = 6; +dag(Xe1, E1) = 1; +dag(Xe2, E2) = 1; +dag([E1 E2], R1) = 1; +dag(R1, Xr1) = 1; + +node_sizes = [ 1 1 2 2 2 1 ]; + +% create BN + +bnet = mk_bnet(dag, node_sizes, 'observed', [Xe1 Xe2 Xr1]); + +% fill in CPT + +bnet.CPD{Xe1} = tabular_CPD(bnet, Xe1, [1]); +bnet.CPD{Xe2} = tabular_CPD(bnet, Xe2, [1]); +bnet.CPD{E1} = tabular_CPD(bnet, E1, [0.2 0.8]); +bnet.CPD{E2} = tabular_CPD(bnet, E2, [0.3 0.7]); +bnet.CPD{R1} = tabular_CPD(bnet, R1, [1 1 1 0.8 0 0 0 0.2]); +bnet.CPD{Xr1} = tabular_CPD(bnet, Xr1, [0.15 0.85]); + +clear engine; +engine{1} = belprop_inf_engine(bnet); +engine{2} = jtree_inf_engine(bnet); +engine{3} = global_joint_inf_engine(bnet); +engine{4} = var_elim_inf_engine(bnet); + +evidence = cell(1,N); +evidence{Xe1} = 1; evidence{Xe2} = 1; evidence{Xr1} = 1; + +mpe = find_mpe(engine{1}, evidence, 'break_ties', 0) % gives wrong results +mpe = find_mpe(engine{1}, evidence) +for i=2:4 + mpe = find_mpe(engine{i}, evidence) +end diff --git a/sourcecodes/bnt-master/BNT/examples/static/nodeorderExample.m b/sourcecodes/bnt-master/BNT/examples/static/nodeorderExample.m new file mode 100644 index 00000000..165f3210 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/nodeorderExample.m @@ -0,0 +1,68 @@ +% example to illustrate why nodes must be numbered topologically. +% Due to Shinya OHTANI <ohtani@pdp.crl.sony.co.jp> +% 9 June 2004 + +%%%%%%%%% WRONG RESULTS because 2 -> 1 +% should have P(parent|no evidence) = prior = [03. 0.7] + +node = struct('ChildNode', 1, ... + 'ParentNode', 2); + +adjacency = zeros(2); +adjacency([node.ParentNode], node.ChildNode) = 1; + +value = {{'TRUE'; 'FALSE'}, ... + {'TRUE'; 'FALSE'}}; + +bnet = mk_bnet(adjacency, [2 2]); +bnet.CPD{node.ChildNode} = tabular_CPD(bnet, node.ChildNode, [0.2 0.4 0.8 0.6]); +bnet.CPD{node.ParentNode} = tabular_CPD(bnet, node.ParentNode, [0.3 0.7]); + +evidence = cell(1,2); +% evidence{node.ChildNode} = 1; +% evidence{node.ParentNode} = 1; + +engine = jtree_inf_engine(bnet); +[engine, loglik] = enter_evidence(engine, evidence); + + +marg = marginal_nodes(engine, node.ChildNode); +disp(sprintf(' ChildNode : %8.6f %8.6f',marg.T(1),marg.T(2)) ); +marg = marginal_nodes(engine, node.ParentNode); +disp(sprintf(' ParentNode : %8.6f %8.6f',marg.T(1),marg.T(2)) ); + +% +% ChildNode : 0.534483 0.465517 +% ParentNode : 0.155172 0.844828 +% loglik = 0.15 + + + +%%%%%%%%% RIGHT RESULTS because 1 -> 2 + +node = struct('ChildNode', 2, ... + 'ParentNode', 1); + + +adjacency = zeros(2); +adjacency([node.ParentNode], node.ChildNode) = 1; + +value = {{'TRUE'; 'FALSE'}, ... + {'TRUE'; 'FALSE'}}; + +bnet = mk_bnet(adjacency, [2 2]); +bnet.CPD{node.ChildNode} = tabular_CPD(bnet, node.ChildNode, [0.2 0.4 0.8 0.6]); +bnet.CPD{node.ParentNode} = tabular_CPD(bnet, node.ParentNode, [0.3 0.7]); + +evidence = cell(1,2); +% evidence{node.ChildNode} = 1; +% evidence{node.ParentNode} = 1; + +engine = jtree_inf_engine(bnet); +[engine, loglik] = enter_evidence(engine, evidence); + + +marg = marginal_nodes(engine, node.ChildNode); +disp(sprintf(' ChildNode : %8.6f %8.6f',marg.T(1),marg.T(2)) ); +marg = marginal_nodes(engine, node.ParentNode); +disp(sprintf(' ParentNode : %8.6f %8.6f',marg.T(1),marg.T(2)) ); diff --git a/sourcecodes/bnt-master/BNT/examples/static/qmr1.m b/sourcecodes/bnt-master/BNT/examples/static/qmr1.m new file mode 100644 index 00000000..a618fbb6 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/qmr1.m @@ -0,0 +1,112 @@ +% Make a QMR-like network +% This is a bipartite graph, where the top layer contains hidden disease nodes, +% and the bottom later contains observed finding nodes. +% The diseases have Bernoulli CPDs, the findings noisy-or CPDs. +% See quickscore_inf_engine for references. + +pMax = 0.01; +Nfindings = 10; +Ndiseases = 5; +%Nfindings = 20; +%Ndiseases = 10; + +N=Nfindings+Ndiseases; +findings = Ndiseases+1:N; +diseases = 1:Ndiseases; + +G = zeros(Ndiseases, Nfindings); +for i=1:Nfindings + v= rand(1,Ndiseases); + rents = find(v<0.8); + if (length(rents)==0) + rents=ceil(rand(1)*Ndiseases); + end + G(rents,i)=1; +end + +prior = pMax*rand(1,Ndiseases); +leak = 0.5*rand(1,Nfindings); % in real QMR, leak approx exp(-0.02) = 0.98 +%leak = ones(1,Nfindings); % turns off leaks, which makes inference much harder +inhibit = rand(Ndiseases, Nfindings); +inhibit(not(G)) = 1; + + +% first half of findings are +ve, second half -ve +% The very first and last findings are hidden +pos = 2:floor(Nfindings/2); +neg = (pos(end)+1):(Nfindings-1); + +% Make the bnet in the straightforward way +tabular_leaves = 0; +obs_nodes = myunion(pos, neg) + Ndiseases; +big_bnet = mk_qmr_bnet(G, inhibit, leak, prior, tabular_leaves, obs_nodes); +big_evidence = cell(1, N); +big_evidence(findings(pos)) = num2cell(repmat(2, 1, length(pos))); +big_evidence(findings(neg)) = num2cell(repmat(1, 1, length(neg))); + +%clf;draw_layout(big_bnet.dag); +%filename = '../public_html/Bayes/Figures/qmr.rnd.jpg'; +%% 3x3 inches +%set(gcf,'units','inches'); +%set(gcf,'PaperPosition',[0 0 3 3]) +%print(gcf,'-djpeg','-r100',filename); + + +% Marginalize out hidden leaves apriori +positive_leaves_only = 1; +[bnet, vals] = mk_minimal_qmr_bnet(G, inhibit, leak, prior, pos, neg, positive_leaves_only); +obs_nodes = bnet.observed; +evidence = cell(1, Ndiseases + length(obs_nodes)); +evidence(obs_nodes) = num2cell(vals); + + +clear engine; +engine{1} = quickscore_inf_engine(inhibit, leak, prior); +engine{2} = jtree_inf_engine(big_bnet); +engine{3} = jtree_inf_engine(bnet); + +%fname = '/home/cs/murphyk/matlab/Misc/loopybel.txt'; +global BNT_HOME +fname = sprintf('%s/loopybel.txt', BNT_HOME); + + +max_iter = 6; +engine{4} = pearl_inf_engine(bnet, 'protocol', 'parallel', 'max_iter', max_iter); +%engine{5} = belprop_inf_engine(bnet, 'max_iter', max_iter, 'filename', fname); +engine{5} = belprop_inf_engine(bnet, 'max_iter', max_iter); + +E = length(engine); +exact = 1:3; +loopy = [4 5]; + +ll = zeros(1,E); +tic; engine{1} = enter_evidence(engine{1}, pos, neg); toc +tic; [engine{2}, ll(2)] = enter_evidence(engine{2}, big_evidence); toc +tic; [engine{3}, ll(3)] = enter_evidence(engine{3}, evidence); toc +tic; [engine{4}, ll(4), niter(4)] = enter_evidence(engine{4}, evidence); toc +tic; [engine{5}, niter(5)] = enter_evidence(engine{5}, evidence); toc + +ll + +post = zeros(E, Ndiseases); +for e=1:E + for i=diseases(:)' + m = marginal_nodes(engine{e}, i); + post(e, i) = m.T(2); + end +end + +for e=exact(:)' + for i=diseases(:)' + assert(approxeq(post(1, i), post(e, i))); + end +end + +a = zeros(Ndiseases, 2); +for ei=1:length(loopy) + for i=diseases(:)' + a(i,ei) = approxeq(post(1, i), post(loopy(ei), i)); + end +end +disp('is the loopy posterior correct?'); +disp(a) diff --git a/sourcecodes/bnt-master/BNT/examples/static/qmr2.m b/sourcecodes/bnt-master/BNT/examples/static/qmr2.m new file mode 100644 index 00000000..921cf57e --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/qmr2.m @@ -0,0 +1,77 @@ +% Test jtree_compiled on a toy QMR network. + +rand('state', 0); +randn('state', 0); +pMax = 0.01; +Nfindings = 10; +Ndiseases = 5; + +N=Nfindings+Ndiseases; +findings = Ndiseases+1:N; +diseases = 1:Ndiseases; + +G = zeros(Ndiseases, Nfindings); +for i=1:Nfindings + v= rand(1,Ndiseases); + rents = find(v<0.8); + if (length(rents)==0) + rents=ceil(rand(1)*Ndiseases); + end + G(rents,i)=1; +end + +prior = pMax*rand(1,Ndiseases); +leak = 0.5*rand(1,Nfindings); % in real QMR, leak approx exp(-0.02) = 0.98 +%leak = ones(1,Nfindings); % turns off leaks, which makes inference much harder +inhibit = rand(Ndiseases, Nfindings); +inhibit(not(G)) = 1; + +% first half of findings are +ve, second half -ve +% The very first and last findings are hidden +pos = 2:floor(Nfindings/2); +neg = (pos(end)+1):(Nfindings-1); + +big = 1; + +if big + % Make the bnet in the straightforward way + tabular_leaves = 1; + obs_nodes = myunion(pos, neg) + Ndiseases; + bnet = mk_qmr_bnet(G, inhibit, leak, prior, tabular_leaves, obs_nodes); + evidence = cell(1, N); + evidence(findings(pos)) = num2cell(repmat(2, 1, length(pos))); + evidence(findings(neg)) = num2cell(repmat(1, 1, length(neg))); +else + % Marginalize out hidden leaves apriori + positive_leaves_only = 1; + [bnet, vals] = mk_minimal_qmr_bnet(G, inhibit, leak, prior, pos, neg, positive_leaves_only); + obs_nodes = bnet.observed; + evidence = cell(1, Ndiseases + length(obs_nodes)); + evidence(obs_nodes) = num2cell(vals); +end + +engine = {}; +engine{end+1} = jtree_inf_engine(bnet); + +E = length(engine); +exact = 1:E; +ll = zeros(1,E); +for e=1:E + tic; [engine{e}, ll(e)] = enter_evidence(engine{e}, evidence); toc +end + +assert(all(approxeq(ll(exact), ll(exact(1))))) + +post = zeros(E, Ndiseases); +for e=1:E + for i=diseases(:)' + m = marginal_nodes(engine{e}, i); + post(e, i) = m.T(2); + end +end +for e=exact(:)' + for i=diseases(:)' + assert(approxeq(post(1, i), post(e, i))); + end +end + diff --git a/sourcecodes/bnt-master/BNT/examples/static/sample1.m b/sourcecodes/bnt-master/BNT/examples/static/sample1.m new file mode 100644 index 00000000..46dcdb1e --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/sample1.m @@ -0,0 +1,34 @@ +% Check sampling on a mixture of experts model +% +% X \ +% | | +% Q | +% | / +% Y +% +% where all arcs point down. +% We condition everything on X, so X is a root node. Q is a softmax, and Y is a linear Gaussian. +% Q is hidden, X and Y are observed. + +X = 1; +Q = 2; +Y = 3; +dag = zeros(3,3); +dag(X,[Q Y]) = 1; +dag(Q,Y) = 1; +ns = [1 2 2]; +dnodes = [2]; +bnet = mk_bnet(dag, ns, dnodes); + +x = 0.5; +bnet.CPD{1} = root_CPD(bnet, 1, x); +bnet.CPD{2} = softmax_CPD(bnet, 2); +bnet.CPD{3} = gaussian_CPD(bnet, 3); + +data_case = sample_bnet(bnet, 'evidence', {0.8, [], []}) +ll = log_lik_complete(bnet, data_case) + +data_case = sample_bnet(bnet, 'evidence', {-11, [], []}) +ll = log_lik_complete(bnet, data_case) + + diff --git a/sourcecodes/bnt-master/BNT/examples/static/softev1.m b/sourcecodes/bnt-master/BNT/examples/static/softev1.m new file mode 100644 index 00000000..5af7ba22 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/softev1.m @@ -0,0 +1,60 @@ +% Check that adding soft evidence to a hidden node is equivalent to evaluating its leaf CPD. + +% Make an HMM +T = 3; Q = 2; O = 2; cts_obs = 0; param_tying = 0; +bnet = mk_hmm_bnet(T, Q, O, cts_obs, param_tying); +N = 2*T; +onodes = bnet.observed; +hnodes = mysetdiff(1:N, onodes); +for i=1:N + bnet.CPD{i} = tabular_CPD(bnet, i); +end + +ev = sample_bnet(bnet); +evidence = cell(1,N); +evidence(onodes) = ev(onodes); + +engine = jtree_inf_engine(bnet); + +[engine, ll] = enter_evidence(engine, evidence); +query = 1; +m = marginal_nodes(engine, query); + + +% Make a Markov chain with the same backbone +bnet2 = mk_markov_chain_bnet(T, Q); +for i=1:T + S = struct(bnet.CPD{hnodes(i)}); % violate object privacy + bnet2.CPD{i} = tabular_CPD(bnet2, i, S.CPT); +end + +% Evaluate the observed leaves of the HMM +soft_ev = cell(1,T); +for i=1:T + S = struct(bnet.CPD{onodes(i)}); % violate object privacy + dist = S.CPT(:, evidence{onodes(i)}); + soft_ev{i} = dist; +end + +% Use the leaf potentials as soft evidence +engine2 = jtree_inf_engine(bnet2); +[engine2, ll2] = enter_evidence(engine2, cell(1,T), 'soft', soft_ev); +m2 = marginal_nodes(engine2, query); + +assert(approxeq(m2.T, m.T)) +assert(approxeq(ll2, ll)) + + + +% marginal on node 1 without evidence +[engine2, ll2] = enter_evidence(engine2, cell(1,T)); +m2 = marginal_nodes(engine2, 1); + +% add soft evidence +soft_ev=cell(1,T); +soft_ev{1}=[0.7 0.3]; +[engine2, ll2] = enter_evidence(engine2, cell(1,T), 'soft', soft_ev); +m3 = marginal_nodes(engine2, 1); + +assert(approxeq(normalise(m2.T .* [0.7 0.3]'), m3.T)) + diff --git a/sourcecodes/bnt-master/BNT/examples/static/softmax1.m b/sourcecodes/bnt-master/BNT/examples/static/softmax1.m new file mode 100644 index 00000000..06434549 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/softmax1.m @@ -0,0 +1,109 @@ +% Check that softmax works with a simple classification demo. +% Based on netlab's demglm2 +% X -> Q where X is an input node, and Q is a softmax + +rand('state', 0); +randn('state', 0); + +% Check inference + +input_dim = 2; +num_classes = 3; +IRLS_iter = 3; + +net = glm(input_dim, num_classes, 'softmax'); + +dag = zeros(2); +dag(1,2) = 1; +discrete_nodes = [2]; +bnet = mk_bnet(dag, [input_dim num_classes], 'discrete', discrete_nodes, 'observed', 1); +bnet.CPD{1} = root_CPD(bnet, 1); +clamped = 0; +bnet.CPD{2} = softmax_CPD(bnet, 2, net.w1, net.b1, clamped, IRLS_iter); + +engine = jtree_inf_engine(bnet); + +x = rand(1, input_dim); +q = glmfwd(net, x); + +[engine, ll] = enter_evidence(engine, {x, []}); +m = marginal_nodes(engine, 2); +assert(approxeq(m.T(:), q(:))); + + +% Check learning +% We use EM, but in fact there is no hidden data. +% The M step will call IRLS on the softmax node. + +% Generate data from three classes in 2d +input_dim = 2; +num_classes = 3; + +% Fix seeds for reproducible results +randn('state', 42); +rand('state', 42); + +ndata = 10; +% Generate mixture of three Gaussians in two dimensional space +data = randn(ndata, input_dim); +targets = zeros(ndata, 3); + +% Priors for the clusters +prior(1) = 0.4; +prior(2) = 0.3; +prior(3) = 0.3; + +% Cluster centres +c = [2.0, 2.0; 0.0, 0.0; 1, -1]; + +ndata1 = prior(1)*ndata; +ndata2 = (prior(1) + prior(2))*ndata; +% Put first cluster at (2, 2) +data(1:ndata1, 1) = data(1:ndata1, 1) * 0.5 + c(1,1); +data(1:ndata1, 2) = data(1:ndata1, 2) * 0.5 + c(1,2); +targets(1:ndata1, 1) = 1; + +% Leave second cluster at (0,0) +data((ndata1 + 1):ndata2, :) = ... + data((ndata1 + 1):ndata2, :); +targets((ndata1+1):ndata2, 2) = 1; + +data((ndata2+1):ndata, 1) = data((ndata2+1):ndata,1) *0.6 + c(3, 1); +data((ndata2+1):ndata, 2) = data((ndata2+1):ndata,2) *0.6 + c(3, 2); +targets((ndata2+1):ndata, 3) = 1; + + +if 0 + ndata = 1; + data = x; + targets = [1 0 0]; +end + +options = foptions; +options(1) = -1; % verbose +options(14) = IRLS_iter; +[net2, options2] = glmtrain(net, options, data, targets); +net2.ll = options2(8); % type 'help foptions' for details + +cases = cell(2, ndata); +for l=1:ndata + q = find(targets(l,:)==1); + x = data(l,:); + cases{1,l} = x(:); + cases{2,l} = q; +end + +max_iter = 2; % we have complete observability, so 1 iter is enough +[bnet2, ll2] = learn_params_em(engine, cases, max_iter); + +w = get_field(bnet2.CPD{2},'weights'); +b = get_field(bnet2.CPD{2},'offset')'; + +w +net2.w1 + +b +net2.b1 + +% assert(approxeq(net2.ll, ll2)); % glmtrain returns ll after final M step, learn_params before + diff --git a/sourcecodes/bnt-master/BNT/examples/static/sprinkler1.m b/sourcecodes/bnt-master/BNT/examples/static/sprinkler1.m new file mode 100644 index 00000000..f021cf46 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/examples/static/sprinkler1.m @@ -0,0 +1,112 @@ +% Lawn sprinker example from Russell and Norvig p454 +% For a picture, see http://www.cs.berkeley.edu/~murphyk/Bayes/usage.html#basics + +N = 4; +dag = zeros(N,N); +C = 1; S = 2; R = 3; W = 4; +dag(C,[R S]) = 1; +dag(R,W) = 1; +dag(S,W)=1; + +false = 1; true = 2; +ns = 2*ones(1,N); % binary nodes + +%bnet = mk_bnet(dag, ns); +bnet = mk_bnet(dag, ns, 'names', {'cloudy','S','R','W'}, 'discrete', 1:4); +names = bnet.names; +%C = names{'cloudy'}; +bnet.CPD{C} = tabular_CPD(bnet, C, [0.5 0.5]); +bnet.CPD{R} = tabular_CPD(bnet, R, [0.8 0.2 0.2 0.8]); +bnet.CPD{S} = tabular_CPD(bnet, S, [0.5 0.9 0.5 0.1]); +bnet.CPD{W} = tabular_CPD(bnet, W, [1 0.1 0.1 0.01 0 0.9 0.9 0.99]); + + +CPD{C} = reshape([0.5 0.5], 2, 1); +CPD{R} = reshape([0.8 0.2 0.2 0.8], 2, 2); +CPD{S} = reshape([0.5 0.9 0.5 0.1], 2, 2); +CPD{W} = reshape([1 0.1 0.1 0.01 0 0.9 0.9 0.99], 2, 2, 2); +joint = zeros(2,2,2,2); +for c=1:2 + for r=1:2 + for s=1:2 + for w=1:2 + joint(c,s,r,w) = CPD{C}(c) * CPD{S}(c,s) * CPD{R}(c,r) * ... + CPD{W}(s,r,w); + end + end + end +end + +joint2 = repmat(reshape(CPD{C}, [2 1 1 1]), [1 2 2 2]) .* ... + repmat(reshape(CPD{S}, [2 2 1 1]), [1 1 2 2]) .* ... + repmat(reshape(CPD{R}, [2 1 2 1]), [1 2 1 2]) .* ... + repmat(reshape(CPD{W}, [1 2 2 2]), [2 1 1 1]); + +assert(approxeq(joint, joint2)); + + +engine = jtree_inf_engine(bnet); + +evidence = cell(1,N); +evidence{W} = true; + +[engine, ll] = enter_evidence(engine, evidence); + +m = marginal_nodes(engine, S); +p1 = m.T(true) % P(S=true|W=true) = 0.4298 +lik1 = exp(ll); % P(W=true) = 0.6471 +assert(approxeq(p1, 0.4298)); +assert(approxeq(lik1, 0.6471)); + +pSandW = sumv(joint(:,true,:,true), [C R]); % P(S,W) = sum_cr P(CSRW) +pW = sumv(joint(:,:,:,true), [C S R]); +pSgivenW = pSandW / pW; % P(S=t|W=t) = P(S=t,W=t)/P(W=t) +assert(approxeq(pW, lik1)) +assert(approxeq(pSgivenW, p1)) + + +m = marginal_nodes(engine, R); +p2 = m.T(true) % P(R=true|W=true) = 0.7079 + +pRandW = sumv(joint(:,:,true,true), [C S]); % P(R,W) = sum_cr P(CSRW) +pRgivenW = pRandW / pW; % P(R=t|W=t) = P(R=t,W=t)/P(W=t) +assert(approxeq(pRgivenW, p2)) + + +% Add extra evidence that R=true +evidence{R} = true; + +[engine, ll] = enter_evidence(engine, evidence); + +m = marginal_nodes(engine, S); +p3 = m.T(true) % P(S=true|W=true,R=true) = 0.1945 +assert(approxeq(p3, 0.1945)) + + +pSandRandW = sumv(joint(:,true,true,true), [C]); % P(S,R,W) = sum_c P(cSRW) +pRandW = sumv(joint(:,:,true,true), [C S]); % P(R,W) = sum_cs P(cSRW) +pSgivenWR = pSandRandW / pRandW; % P(S=t|W=t,R=t) = P(S=t,R=t,W=t)/P(W=t,R=t) +assert(approxeq(pSgivenWR, p3)) + +% So the sprinkler is less likely to be on if we know that +% it is raining, since the rain can "explain away" the fact +% that the grass is wet. + +lik3 = exp(ll); % P(W=true, R=true) = 0.4581 +% So the combined evidence is less likely (of course) + + + + +% Joint distributions + +evidence = cell(1,N); +[engine, ll] = enter_evidence(engine, evidence); +m = marginal_nodes(engine, [S R W]); + +evidence{R} = 2; +[engine, ll] = enter_evidence(engine, evidence); +m = marginal_nodes(engine, [S R W]); + + + |
