annotate _FullBNT/BNT/learning/mcmc_sample_to_hist.m @ 9:4ea6619cb3f5 tip

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