annotate toolboxes/FullBNT-1.0.7/bnt/examples/static/lw1.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
rev   line source
wolffd@0 1 % Evaluate effectiveness of likelihood weighting on the lawn sprinkler example
wolffd@0 2
wolffd@0 3 N = 4;
wolffd@0 4 dag = zeros(N,N);
wolffd@0 5 C = 1; R = 2; S = 3; W = 4;
wolffd@0 6 dag(C,[R S]) = 1;
wolffd@0 7 dag(R,W) = 1;
wolffd@0 8 dag(S,W)=1;
wolffd@0 9
wolffd@0 10 false = 1; true = 2;
wolffd@0 11 ns = 2*ones(1,N); % binary nodes
wolffd@0 12
wolffd@0 13 bnet = mk_bnet(dag, ns);
wolffd@0 14 bnet.CPD{C} = tabular_CPD(bnet, C, [0.5 0.5]);
wolffd@0 15 bnet.CPD{R} = tabular_CPD(bnet, R, [0.8 0.2 0.2 0.8]);
wolffd@0 16 bnet.CPD{S} = tabular_CPD(bnet, S, [0.5 0.9 0.5 0.1]);
wolffd@0 17 bnet.CPD{W} = tabular_CPD(bnet, W, [1 0.1 0.1 0.01 0 0.9 0.9 0.99]);
wolffd@0 18
wolffd@0 19
wolffd@0 20 clear engine;
wolffd@0 21 engine{1} = jtree_inf_engine(bnet);
wolffd@0 22 engine{2} = likelihood_weighting_inf_engine(bnet);
wolffd@0 23
wolffd@0 24 nengines = length(engine);
wolffd@0 25 m = cell(1, nengines);
wolffd@0 26 ll = zeros(1, nengines);
wolffd@0 27
wolffd@0 28 evidence = cell(1,N);
wolffd@0 29 %evidence{C} = true; % evidence at the top is the easiest
wolffd@0 30 evidence{W} = true; % evidence at the bottom is the hardets
wolffd@0 31
wolffd@0 32 query = [R];
wolffd@0 33
wolffd@0 34 i=1;
wolffd@0 35 engine{i} = enter_evidence(engine{i}, evidence);
wolffd@0 36 exact_m = marginal_nodes(engine{i}, query);
wolffd@0 37
wolffd@0 38 i=2;
wolffd@0 39 samples = 100:100:500;
wolffd@0 40 err = zeros(1, length(samples));
wolffd@0 41 for j=1:length(samples)
wolffd@0 42 nsamples = samples(j);
wolffd@0 43 engine{i} = enter_evidence(engine{i}, evidence, nsamples);
wolffd@0 44 approx_m = marginal_nodes(engine{i}, query);
wolffd@0 45 a1=approxeq(approx_m.T,exact_m.T,1e-1);
wolffd@0 46 a2=approxeq(approx_m.T,exact_m.T,1e-2);
wolffd@0 47 a3=approxeq(approx_m.T,exact_m.T,1e-3);
wolffd@0 48 e = sum(abs(approx_m.T(:) - exact_m.T(:)));
wolffd@0 49 fprintf('%d samples, 1dp %d, 2dp %d, 3dp %d, err %f\n', nsamples, a1, a2, a3, e);
wolffd@0 50 err(j) = e;
wolffd@0 51 end