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