annotate toolboxes/FullBNT-1.0.7/bnt/examples/static/SCG/scg_unstable.m @ 0:cc4b1211e677 tip

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