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