annotate toolboxes/FullBNT-1.0.7/bnt/examples/static/StructLearn/mcmc1.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 % We compare MCMC structure learning with exhaustive enumeration of all dags.
Daniel@0 2
Daniel@0 3 N = 3;
Daniel@0 4 %N = 4;
Daniel@0 5 dag = mk_rnd_dag(N);
Daniel@0 6 ns = 2*ones(1,N);
Daniel@0 7 bnet = mk_bnet(dag, ns);
Daniel@0 8 for i=1:N
Daniel@0 9 bnet.CPD{i} = tabular_CPD(bnet, i);
Daniel@0 10 end
Daniel@0 11
Daniel@0 12 ncases = 100;
Daniel@0 13 data = zeros(N, ncases);
Daniel@0 14 for m=1:ncases
Daniel@0 15 data(:,m) = cell2num(sample_bnet(bnet));
Daniel@0 16 end
Daniel@0 17
Daniel@0 18 dags = mk_all_dags(N);
Daniel@0 19 score = score_dags(data, ns, dags);
Daniel@0 20 post = normalise(exp(score));
Daniel@0 21
Daniel@0 22 [sampled_graphs, accept_ratio] = learn_struct_mcmc(data, ns, 'nsamples', 100, 'burnin', 10);
Daniel@0 23 mcmc_post = mcmc_sample_to_hist(sampled_graphs, dags);
Daniel@0 24
Daniel@0 25 if 0
Daniel@0 26 subplot(2,1,1)
Daniel@0 27 bar(post)
Daniel@0 28 subplot(2,1,2)
Daniel@0 29 bar(mcmc_post)
Daniel@0 30 print(gcf, '-djpeg', '/home/cs/murphyk/public_html/Bayes/Figures/mcmc_post.jpg')
Daniel@0 31
Daniel@0 32 clf
Daniel@0 33 plot(accept_ratio)
Daniel@0 34 print(gcf, '-djpeg', '/home/cs/murphyk/public_html/Bayes/Figures/mcmc_accept.jpg')
Daniel@0 35 end