# source:stable/3.9/Ipopt/contrib/MatlabInterface-Old/examples/bayesnet/examplelauritzen.m@1864

Last change on this file since 1864 was 1864, checked in by andreasw, 3 years ago

sync with trunk rev 1862

File size: 3.4 KB
Line
1% This is a small script to compute the Bethe approximation of a small
2% factor graph. The factor graph is generated by taking the Bayes net of
3% Lauritzen and Spiegelhalter (1988) and setting the following random
4% variables as evidence
5%
6%   visit to Asia  = false
7%   positive X-ray = true
8%   smoking        = true
9%
10% For this particular set of evidence, the Bethe approximation is exact
11% because the corresponding factor graph does not have any cycles.
12%
14% computations with probabilities on graphical structures and their
15% application to expert systems." Journal of the Royal Statistical
16% Society, Series B, Vol. 50, No. 2, pp. 157-194.
17%
19% This code is published under the Eclipse Public License.
20%
21% Author: Peter Carbonetto
22%         Dept. of Computer Science
23%         University of British Columbia
24%         May 19, 2007
25
26verbose = true;
27
28% The remaining random variables can either be true or false. They are:
29%   1. tuberculosis
30%   2. lung cancer
31%   3. bronchitis
32%   4. tuberculosis or lung cancer
33%   5. dysponoea
34nv = 5;             % The number of random variables.
35nf = 6;             % The number of factors.
36K  = 2*ones(nv,1);  % The number of possible discrete assignments to the
37                    % random variable at each site.
38C = cell(nf,1);     % The factor neighbourhoods.
39f = cell(nf,1);     % The factors.
40
41% Set up the factors.
42% ------------------
43% This is p(tuberculosis | visit to Asia = false).
44C{1} = 1;
45f{1} = [ 0.01 0.99 ]';
46
47% This is p(lung cancer | smoking = true).
48C{2} = 2;
49f{2} = [ 0.1 0.9 ]';
50
51% This is p(bronchitis | smoking = true).
52C{3} = 3;
53f{3} = [ 0.6 0.4 ]';
54
55% This is p(tuberculosis or lung cancer | tuberculosis, lung cancer).
56C{4}     = [4 1 2];
57p        = zeros(2,2,2);
58p(:,1,1) = [ 0.99 0.01 ]';
59p(:,1,2) = [ 0.99 0.01 ]';
60p(:,2,1) = [ 0.99 0.01 ]';
61p(:,2,2) = [ 0.99 0.01 ]';
62f{4}     = p;
63
64% This is p(positive X-ray = true | tuberculosis or lung cancer).
65C{5} = 4;
66f{5} = [ 0.98 0.5 ]';
67
68% This is p(dyspnoea | tuberculosis or lung cancer, bronchitis).
69C{6}     = [5 4 3];
70p        = zeros(2,2,2);
71p(:,1,1) = [ 0.9 0.1 ]';
72p(:,1,2) = [ 0.7 0.3 ]';
73p(:,2,1) = [ 0.8 0.2 ]';
74p(:,2,2) = [ 0.1 0.9 ]';
75f{6}     = p;
76clear p
77
78% Set up the junction graph.
79% -------------------------
80% These are the large regions of the junction graph.
81Rv{1} = [1 2 4];
82Rf{1} = [1 2 4];
83NR{1} = [1];
84
85Rv{2} = [3 4 5];
86Rf{2} = [3 5 6];
87NR{2} = [1];
88
89% These are the small regions of the junction graph.
90Sv{1} = [4];
91Sf{1} = [];
92NS{1} = [1 2];
93
94nr = length(Rv);  % The number of large regions.
95ns = length(Sv);  % The number of small regions.
96
97% Infer the region marginals.
98% --------------------------
99% Compute the junction graph approximation to the variational free
100% energy. Since the junction graph is a tree in this case (trivially,
101% because there are only two large regions), the junction graph
102% approximation is exact.
103qR = bopt(K,C,f,Rv,Rf,Sv,Sf,NS,verbose);
104
105% Output the marginal probabilities.
106% ---------------------------------
107p = ndsum(qR{1},[2 3]);
108fprintf('Pr(tuberculosis | evidence) = %0.2f \n', p(1));
109
110p = ndsum(qR{1},[1 3]);
111fprintf('Pr(lung cancer | evidence)  = %0.2f \n', p(1));
112
113p = ndsum(qR{2},[2 3]);
114fprintf('Pr(bronchitis | evidence)   = %0.2f \n', p(1));
115
116p = ndsum(qR{2},[1 2]);
117fprintf('Pr(dysponoea | evidence)    = %0.2f \n', p(1));
Note: See TracBrowser for help on using the repository browser.