annotate toolboxes/FullBNT-1.0.7/bnt/examples/static/StructLearn/bic1.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 % compare BIC and Bayesian score
Daniel@0 2
Daniel@0 3 N = 4;
Daniel@0 4 dag = zeros(N,N);
Daniel@0 5 %C = 1; S = 2; R = 3; W = 4; % topological order
Daniel@0 6 C = 4; S = 2; R = 3; W = 1; % arbitrary order
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
Daniel@0 12 false = 1; true = 2;
Daniel@0 13 ns = 2*ones(1,N); % binary nodes
Daniel@0 14 bnet = mk_bnet(dag, ns);
Daniel@0 15 bnet.CPD{C} = tabular_CPD(bnet, C, 'CPT', [0.5 0.5]);
Daniel@0 16 bnet.CPD{R} = tabular_CPD(bnet, R, 'CPT', [0.8 0.2 0.2 0.8]);
Daniel@0 17 bnet.CPD{S} = tabular_CPD(bnet, S, 'CPT', [0.5 0.9 0.5 0.1]);
Daniel@0 18 bnet.CPD{W} = tabular_CPD(bnet, W, 'CPT', [1 0.1 0.1 0.01 0 0.9 0.9 0.99]);
Daniel@0 19
Daniel@0 20
Daniel@0 21 seed = 0;
Daniel@0 22 rand('state', seed);
Daniel@0 23 randn('state', seed);
Daniel@0 24 ncases = 1000;
Daniel@0 25 data = cell(N, ncases);
Daniel@0 26 for m=1:ncases
Daniel@0 27 data(:,m) = sample_bnet(bnet);
Daniel@0 28 end
Daniel@0 29
Daniel@0 30 priors = [0.1 1 10];
Daniel@0 31 P = length(priors);
Daniel@0 32 params = cell(1,P);
Daniel@0 33 for p=1:P
Daniel@0 34 params{p} = cell(1,N);
Daniel@0 35 for i=1:N
Daniel@0 36 %params{p}{i} = {'prior', priors(p)};
Daniel@0 37 params{p}{i} = {'prior_type', 'dirichlet', 'dirichlet_weight', priors(p)};
Daniel@0 38 end
Daniel@0 39 end
Daniel@0 40
Daniel@0 41 %sz = 1000:1000:10000;
Daniel@0 42 sz = 10:10:100;
Daniel@0 43 S = length(sz);
Daniel@0 44 bic_score = zeros(S, 1);
Daniel@0 45 bayes_score = zeros(S, P);
Daniel@0 46 for i=1:S
Daniel@0 47 bic_score(i) = score_dags(data(:,1:sz(i)), ns, {dag}, 'scoring_fn', 'bic', 'params', []);
Daniel@0 48 end
Daniel@0 49 diff = zeros(S,P);
Daniel@0 50 for p=1:P
Daniel@0 51 for i=1:S
Daniel@0 52 bayes_score(i,p) = score_dags(data(:,1:sz(i)), ns, {dag}, 'params', params{p});
Daniel@0 53 end
Daniel@0 54 end
Daniel@0 55
Daniel@0 56 for p=1:P
Daniel@0 57 for i=1:S
Daniel@0 58 diff(i,p) = bayes_score(i,p)/ bic_score(i);
Daniel@0 59 %diff(i,p) = abs(bayes_score(i,p) - bic_score(i));
Daniel@0 60 end
Daniel@0 61 end
Daniel@0 62
Daniel@0 63 if 0
Daniel@0 64 plot(sz, diff(:,1), 'g--*', sz, diff(:,2), 'b-.+', sz, diff(:,3), 'k:s');
Daniel@0 65 title('Relative BIC error vs. size of data set')
Daniel@0 66 legend('BDeu 0.1', 'BDeu 1', 'Bdeu 10', 2)
Daniel@0 67 end
Daniel@0 68
Daniel@0 69 if 0
Daniel@0 70 plot(sz, bic_score, 'r-o', sz, bayes_score(:,1), 'g--*', sz, bayes_score(:,2), 'b-.+', sz, bayes_score(:,3), 'k:s');
Daniel@0 71 legend('bic', 'BDeu 0.01', 'BDeu 1', 'Bdeu 100')
Daniel@0 72 ylabel('score')
Daniel@0 73 title('score vs. size of data set')
Daniel@0 74 end
Daniel@0 75
Daniel@0 76 %xlabel('num. data cases')
Daniel@0 77
Daniel@0 78 %previewfig(gcf, 'format', 'png', 'height', 2, 'color', 'rgb')
Daniel@0 79 %exportfig(gcf, '/home/cs/murphyk/public_html/Bayes/Figures/bic.png', 'format', 'png', 'height', 2, 'color', 'rgb')