diff options
Diffstat (limited to 'sourcecodes/bnt-master/BNT/examples/static')
141 files changed, 7502 insertions, 0 deletions
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]); + + + |
