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