comparison toolboxes/FullBNT-1.0.7/bnt/examples/static/nodeorderExample.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 % example to illustrate why nodes must be numbered topologically.
2 % Due to Shinya OHTANI <ohtani@pdp.crl.sony.co.jp>
3 % 9 June 2004
4
5 %%%%%%%%% WRONG RESULTS because 2 -> 1
6 % should have P(parent|no evidence) = prior = [03. 0.7]
7
8 node = struct('ChildNode', 1, ...
9 'ParentNode', 2);
10
11 adjacency = zeros(2);
12 adjacency([node.ParentNode], node.ChildNode) = 1;
13
14 value = {{'TRUE'; 'FALSE'}, ...
15 {'TRUE'; 'FALSE'}};
16
17 bnet = mk_bnet(adjacency, [2 2]);
18 bnet.CPD{node.ChildNode} = tabular_CPD(bnet, node.ChildNode, [0.2 0.4 0.8 0.6]);
19 bnet.CPD{node.ParentNode} = tabular_CPD(bnet, node.ParentNode, [0.3 0.7]);
20
21 evidence = cell(1,2);
22 % evidence{node.ChildNode} = 1;
23 % evidence{node.ParentNode} = 1;
24
25 engine = jtree_inf_engine(bnet);
26 [engine, loglik] = enter_evidence(engine, evidence);
27
28
29 marg = marginal_nodes(engine, node.ChildNode);
30 disp(sprintf(' ChildNode : %8.6f %8.6f',marg.T(1),marg.T(2)) );
31 marg = marginal_nodes(engine, node.ParentNode);
32 disp(sprintf(' ParentNode : %8.6f %8.6f',marg.T(1),marg.T(2)) );
33
34 %
35 % ChildNode : 0.534483 0.465517
36 % ParentNode : 0.155172 0.844828
37 % loglik = 0.15
38
39
40
41 %%%%%%%%% RIGHT RESULTS because 1 -> 2
42
43 node = struct('ChildNode', 2, ...
44 'ParentNode', 1);
45
46
47 adjacency = zeros(2);
48 adjacency([node.ParentNode], node.ChildNode) = 1;
49
50 value = {{'TRUE'; 'FALSE'}, ...
51 {'TRUE'; 'FALSE'}};
52
53 bnet = mk_bnet(adjacency, [2 2]);
54 bnet.CPD{node.ChildNode} = tabular_CPD(bnet, node.ChildNode, [0.2 0.4 0.8 0.6]);
55 bnet.CPD{node.ParentNode} = tabular_CPD(bnet, node.ParentNode, [0.3 0.7]);
56
57 evidence = cell(1,2);
58 % evidence{node.ChildNode} = 1;
59 % evidence{node.ParentNode} = 1;
60
61 engine = jtree_inf_engine(bnet);
62 [engine, loglik] = enter_evidence(engine, evidence);
63
64
65 marg = marginal_nodes(engine, node.ChildNode);
66 disp(sprintf(' ChildNode : %8.6f %8.6f',marg.T(1),marg.T(2)) );
67 marg = marginal_nodes(engine, node.ParentNode);
68 disp(sprintf(' ParentNode : %8.6f %8.6f',marg.T(1),marg.T(2)) );