comparison toolboxes/FullBNT-1.0.7/bnt/examples/static/SCG/scg_unstable.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 function scg_unstable()
2
3 % the objective of this script is to test if the stable conditonal gaussian
4 % inference can handle the numerical instability problem described on
5 % page.151 of 'Probabilistic networks and expert system' by Cowell, Dawid, Lauritzen and
6 % Spiegelhalter, 1999.
7
8 A = 1; Y = 2;
9 n = 2;
10
11 ns = ones(1, n);
12 dnodes = [A];
13 cnodes = Y;
14 ns = [2 1];
15
16 dag = zeros(n);
17 dag(A, Y) = 1;
18
19 bnet = mk_bnet(dag, ns, dnodes);
20
21 bnet.CPD{A} = tabular_CPD(bnet, A, [0.5 0.5]');
22 bnet.CPD{Y} = gaussian_CPD(bnet, Y, 'mean', [0 1], 'cov', [1e-5 1e-6]);
23
24 evidence = cell(1, n);
25
26 pot_type = 'cg';
27 potYgivenA = convert_to_pot(bnet.CPD{Y}, pot_type, [A Y], evidence);
28 potA = convert_to_pot(bnet.CPD{A}, pot_type, A, evidence);
29 potYandA = multiply_by_pot(potYgivenA, potA);
30 potA2 = marginalize_pot(potYandA, A);
31
32 thresh = 1; % 0dp
33
34 [g,h,K] = extract_can(potA);
35 assert(approxeq(g(:)', [-0.693147 -0.693147], thresh))
36
37
38 [g,h,K] = extract_can(potYgivenA);
39 assert(approxeq(g(:)', [4.83752 -499994], thresh))
40 assert(approxeq(h(:)', [0 1e6]))
41 assert(approxeq(K(:)', [1e5 1e6]))
42
43 [g,h,K] = extract_can(potYandA);
44 assert(approxeq(g(:)', [4.14437 -499995], thresh))
45 assert(approxeq(h(:)', [0 1e6]))
46 assert(approxeq(K(:)', [1e5 1e6]))
47
48
49 [g,h,K] = extract_can(potA2);
50 %assert(approxeq(g(:)', [-0.69315 -1]))
51 g
52 assert(approxeq(g(:)', [-0.69315 -0.69315]))
53
54
55
56 if 0
57 pot_type = 'scg';
58 spotYgivenA = convert_to_pot(bnet.CPD{Y}, pot_type, [A Y], evidence);
59 spotA = convert_to_pot(bnet.CPD{A}, pot_type, A, evidence);
60 spotYandA = direct_combine_pots(spotYgivenA, spotA);
61 spotA2 = marginalize_pot(spotYandA, A);
62
63 spotA=struct(spotA);
64 spotA2=struct(spotA2);
65 for i=1:2
66 assert(approxeq(spotA2.scgpotc{i}.p, spotA.scgpotc{i}.p))
67 assert(approxeq(spotA2.scgpotc{i}.A, spotA.scgpotc{i}.A))
68 assert(approxeq(spotA2.scgpotc{i}.B, spotA.scgpotc{i}.B))
69 assert(approxeq(spotA2.scgpotc{i}.C, spotA.scgpotc{i}.C))
70 end
71
72 end
73
74
75 %%%%%%%%%%%
76
77 function [g,h,K] = extract_can(pot)
78
79 pot = struct(pot);
80 D = length(pot.can);
81 g = zeros(1, D);
82 h = zeros(1, D);
83 K = zeros(1, D);
84 for i=1:D
85 S = struct(pot.can{i});
86 g(i) = S.g;
87 if length(S.h) > 0
88 h(i) = S.h;
89 K(i) = S.K;
90 end
91 end