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