annotate toolboxes/FullBNT-1.0.7/bnt/learning/mcmc_sample_to_hist.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
rev   line source
wolffd@0 1 function mcmc_post = mcmc_sample_to_hist(sampled_graphs, dags)
wolffd@0 2 % MCMC_SAMPLE_TO_HIST Convert a set of sampled dags into a histogram over dags
wolffd@0 3 % hist = mcmc_sample_to_hist(sampled_graphs, dags)
wolffd@0 4 %
wolffd@0 5 % sampled_graphs{m} is the m'th sampled dag
wolffd@0 6 % dags{i} is the i'th dag in the hypothesis space
wolffd@0 7 % hist(i) = Pr(model i | data)
wolffd@0 8
wolffd@0 9 ndags = length(dags);
wolffd@0 10 nsamples = length(sampled_graphs);
wolffd@0 11 nnodes = length(dags{1});
wolffd@0 12 % sampled_bitv(m, :) is the m'th sampled graph represented as a vector of n^2 bits, computed
wolffd@0 13 % by stacking the columns of the adjacency matrix vertically.
wolffd@0 14 sampled_bitvs = zeros(nsamples, nnodes*nnodes);
wolffd@0 15 for m=1:nsamples
wolffd@0 16 sampled_bitvs(m, :) = sampled_graphs{m}(:)';
wolffd@0 17 end
wolffd@0 18
wolffd@0 19 [ugraphs, I, J] = unique(sampled_bitvs, 'rows'); % each row of ugraphs is a unique bit vector
wolffd@0 20 sampled_indices = subv2ind(2*ones(1,nnodes*nnodes), ugraphs+1);
wolffd@0 21 counts = hist(J, 1:size(ugraphs,1)); % counts(i) = number of times graphs(i,:) occurs in the sample
wolffd@0 22
wolffd@0 23 mcmc_post = zeros(1, ndags);
wolffd@0 24 for i=1:ndags
wolffd@0 25 bitv = dags{i}(:)';
wolffd@0 26 % Find the samples that corresponds to this graph by converting the graphs to bitvectors and
wolffd@0 27 % then to integers.
wolffd@0 28 ndx = subv2ind(2*ones(1,nnodes*nnodes), bitv+1);
wolffd@0 29 locn = find(ndx == sampled_indices);
wolffd@0 30 if ~isempty(locn)
wolffd@0 31 mcmc_post(i) = counts(locn);
wolffd@0 32 end
wolffd@0 33 end
wolffd@0 34 mcmc_post = normalise(mcmc_post);
wolffd@0 35
wolffd@0 36