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