annotate toolboxes/FullBNT-1.0.7/bnt/examples/static/learn1.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 % Lawn sprinker example from Russell and Norvig p454
Daniel@0 2 % See www.cs.berkeley.edu/~murphyk/Bayes/usage.html for details.
Daniel@0 3
Daniel@0 4 N = 4;
Daniel@0 5 dag = zeros(N,N);
Daniel@0 6 C = 1; S = 2; R = 3; W = 4;
Daniel@0 7 dag(C,[R S]) = 1;
Daniel@0 8 dag(R,W) = 1;
Daniel@0 9 dag(S,W)=1;
Daniel@0 10
Daniel@0 11 false = 1; true = 2;
Daniel@0 12 ns = 2*ones(1,N); % binary nodes
Daniel@0 13
Daniel@0 14 bnet = mk_bnet(dag, ns);
Daniel@0 15 bnet.CPD{C} = tabular_CPD(bnet, C, [0.5 0.5]);
Daniel@0 16 bnet.CPD{R} = tabular_CPD(bnet, R, [0.8 0.2 0.2 0.8]);
Daniel@0 17 bnet.CPD{S} = tabular_CPD(bnet, S, [0.5 0.9 0.5 0.1]);
Daniel@0 18 bnet.CPD{W} = tabular_CPD(bnet, W, [1 0.1 0.1 0.01 0 0.9 0.9 0.99]);
Daniel@0 19
Daniel@0 20 CPT = cell(1,N);
Daniel@0 21 for i=1:N
Daniel@0 22 s=struct(bnet.CPD{i}); % violate object privacy
Daniel@0 23 CPT{i}=s.CPT;
Daniel@0 24 end
Daniel@0 25
Daniel@0 26 % Generate training data
Daniel@0 27 nsamples = 50;
Daniel@0 28 samples = cell(N, nsamples);
Daniel@0 29 for i=1:nsamples
Daniel@0 30 samples(:,i) = sample_bnet(bnet);
Daniel@0 31 end
Daniel@0 32 data = cell2num(samples);
Daniel@0 33
Daniel@0 34 % Make a tabula rasa
Daniel@0 35 bnet2 = mk_bnet(dag, ns);
Daniel@0 36 seed = 0;
Daniel@0 37 rand('state', seed);
Daniel@0 38 bnet2.CPD{C} = tabular_CPD(bnet2, C, 'clamped', 1, 'CPT', [0.5 0.5], ...
Daniel@0 39 'prior_type', 'dirichlet', 'dirichlet_weight', 0);
Daniel@0 40 bnet2.CPD{R} = tabular_CPD(bnet2, R, 'prior_type', 'dirichlet', 'dirichlet_weight', 0);
Daniel@0 41 bnet2.CPD{S} = tabular_CPD(bnet2, S, 'prior_type', 'dirichlet', 'dirichlet_weight', 0);
Daniel@0 42 bnet2.CPD{W} = tabular_CPD(bnet2, W, 'prior_type', 'dirichlet', 'dirichlet_weight', 0);
Daniel@0 43
Daniel@0 44
Daniel@0 45 % Find MLEs from fully observed data
Daniel@0 46 bnet4 = learn_params(bnet2, samples);
Daniel@0 47
Daniel@0 48 % Bayesian updating with 0 prior is equivalent to ML estimation
Daniel@0 49 bnet5 = bayes_update_params(bnet2, samples);
Daniel@0 50
Daniel@0 51 CPT4 = cell(1,N);
Daniel@0 52 for i=1:N
Daniel@0 53 s=struct(bnet4.CPD{i}); % violate object privacy
Daniel@0 54 CPT4{i}=s.CPT;
Daniel@0 55 end
Daniel@0 56
Daniel@0 57 CPT5 = cell(1,N);
Daniel@0 58 for i=1:N
Daniel@0 59 s=struct(bnet5.CPD{i}); % violate object privacy
Daniel@0 60 CPT5{i}=s.CPT;
Daniel@0 61 assert(approxeq(CPT5{i}, CPT4{i}))
Daniel@0 62 end
Daniel@0 63
Daniel@0 64
Daniel@0 65 if 1
Daniel@0 66 % Find MLEs from partially observed data
Daniel@0 67
Daniel@0 68 % hide 50% of the nodes
Daniel@0 69 samplesH = samples;
Daniel@0 70 hide = rand(N, nsamples) > 0.5;
Daniel@0 71 [I,J]=find(hide);
Daniel@0 72 for k=1:length(I)
Daniel@0 73 samplesH{I(k), J(k)} = [];
Daniel@0 74 end
Daniel@0 75
Daniel@0 76 engine = jtree_inf_engine(bnet2);
Daniel@0 77 max_iter = 5;
Daniel@0 78 [bnet6, LL] = learn_params_em(engine, samplesH, max_iter);
Daniel@0 79
Daniel@0 80 CPT6 = cell(1,N);
Daniel@0 81 for i=1:N
Daniel@0 82 s=struct(bnet6.CPD{i}); % violate object privacy
Daniel@0 83 CPT6{i}=s.CPT;
Daniel@0 84 end
Daniel@0 85
Daniel@0 86 end