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