Daniel@0: function mcmc_post = mcmc_sample_to_hist(sampled_graphs, dags) Daniel@0: % MCMC_SAMPLE_TO_HIST Convert a set of sampled dags into a histogram over dags Daniel@0: % hist = mcmc_sample_to_hist(sampled_graphs, dags) Daniel@0: % Daniel@0: % sampled_graphs{m} is the m'th sampled dag Daniel@0: % dags{i} is the i'th dag in the hypothesis space Daniel@0: % hist(i) = Pr(model i | data) Daniel@0: Daniel@0: ndags = length(dags); Daniel@0: nsamples = length(sampled_graphs); Daniel@0: nnodes = length(dags{1}); Daniel@0: % sampled_bitv(m, :) is the m'th sampled graph represented as a vector of n^2 bits, computed Daniel@0: % by stacking the columns of the adjacency matrix vertically. Daniel@0: sampled_bitvs = zeros(nsamples, nnodes*nnodes); Daniel@0: for m=1:nsamples Daniel@0: sampled_bitvs(m, :) = sampled_graphs{m}(:)'; Daniel@0: end Daniel@0: Daniel@0: [ugraphs, I, J] = unique(sampled_bitvs, 'rows'); % each row of ugraphs is a unique bit vector Daniel@0: sampled_indices = subv2ind(2*ones(1,nnodes*nnodes), ugraphs+1); Daniel@0: counts = hist(J, 1:size(ugraphs,1)); % counts(i) = number of times graphs(i,:) occurs in the sample Daniel@0: Daniel@0: mcmc_post = zeros(1, ndags); Daniel@0: for i=1:ndags Daniel@0: bitv = dags{i}(:)'; Daniel@0: % Find the samples that corresponds to this graph by converting the graphs to bitvectors and Daniel@0: % then to integers. Daniel@0: ndx = subv2ind(2*ones(1,nnodes*nnodes), bitv+1); Daniel@0: locn = find(ndx == sampled_indices); Daniel@0: if ~isempty(locn) Daniel@0: mcmc_post(i) = counts(locn); Daniel@0: end Daniel@0: end Daniel@0: mcmc_post = normalise(mcmc_post); Daniel@0: Daniel@0: