wolffd@0: % Online Bayesian model selection demo. wolffd@0: wolffd@0: % We generate data from the model A->B wolffd@0: % and compute the posterior prob of all 3 dags on 2 nodes: wolffd@0: % (1) A B, (2) A <- B , (3) A -> B wolffd@0: % Models 2 and 3 are Markov equivalent, and therefore indistinguishable from wolffd@0: % observational data alone. wolffd@0: wolffd@0: % We control the dependence of B on A by setting wolffd@0: % P(B|A) = 0.5 - epislon and vary epsilon wolffd@0: % as in Koller & Friedman book p512 wolffd@0: wolffd@0: % ground truth wolffd@0: N = 2; wolffd@0: dag = zeros(N); wolffd@0: A = 1; B = 2; wolffd@0: dag(A,B) = 1; wolffd@0: wolffd@0: ntrials = 100; wolffd@0: ns = 2*ones(1,N); wolffd@0: true_bnet = mk_bnet(dag, ns); wolffd@0: true_bnet.CPD{1} = tabular_CPD(true_bnet, 1, [0.5 0.5]); wolffd@0: wolffd@0: % hypothesis space wolffd@0: G = mk_all_dags(N); wolffd@0: nhyp = length(G); wolffd@0: hyp_bnet = cell(1, nhyp); wolffd@0: for h=1:nhyp wolffd@0: hyp_bnet{h} = mk_bnet(G{h}, ns); wolffd@0: for i=1:N wolffd@0: % We must set the CPTs to the mean of the prior for sequential log_marg_lik to be correct wolffd@0: % The BDeu prior is score equivalent, so models 2,3 will be indistinguishable. wolffd@0: % The uniform Dirichlet prior is not score equivalent... wolffd@0: fam = family(G{h}, i); wolffd@0: hyp_bnet{h}.CPD{i}= tabular_CPD(hyp_bnet{h}, i, 'prior_type', 'dirichlet', ... wolffd@0: 'CPT', 'unif'); wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: clf wolffd@0: seeds = 1:3; wolffd@0: expt = 1; wolffd@0: for seedi=1:length(seeds) wolffd@0: seed = seeds(seedi); wolffd@0: rand('state', seed); wolffd@0: randn('state', seed); wolffd@0: wolffd@0: es = [0.05 0.1 0.15 0.2]; wolffd@0: for ei=1:length(es) wolffd@0: e = es(ei); wolffd@0: true_bnet.CPD{2} = tabular_CPD(true_bnet, 2, [0.5+e 0.5-e; 0.5-e 0.5+e]); wolffd@0: wolffd@0: prior = normalise(ones(1, nhyp)); wolffd@0: hyp_w = zeros(ntrials+1, nhyp); wolffd@0: hyp_w(1,:) = prior(:)'; wolffd@0: LL = zeros(1, nhyp); wolffd@0: ll = zeros(1, nhyp); wolffd@0: for t=1:ntrials wolffd@0: ev = cell2num(sample_bnet(true_bnet)); wolffd@0: for i=1:nhyp wolffd@0: ll(i) = log_marg_lik_complete(hyp_bnet{i}, ev); wolffd@0: hyp_bnet{i} = bayes_update_params(hyp_bnet{i}, ev); wolffd@0: end wolffd@0: prior = normalise(prior .* exp(ll)); wolffd@0: LL = LL + ll; wolffd@0: hyp_w(t+1,:) = prior; wolffd@0: end wolffd@0: wolffd@0: % Plot posterior model probabilities wolffd@0: % Red = model 1 (no arcs), blue/green = models 2/3 (1 arc) wolffd@0: % Blue = model 2 (2->1) wolffd@0: % Green = model 3 (1->2, "ground truth") wolffd@0: wolffd@0: subplot2(length(seeds), length(es), seedi, ei); wolffd@0: m = size(hyp_w,1); wolffd@0: h=plot(1:m, hyp_w(:,1), 'r-', 1:m, hyp_w(:,2), 'b-.', 1:m, hyp_w(:,3), 'g:'); wolffd@0: axis([0 m 0 1]) wolffd@0: %title('model posterior vs. time') wolffd@0: title(sprintf('e=%3.2f, seed=%d', e, seed)); wolffd@0: drawnow wolffd@0: expt = expt + 1; wolffd@0: end wolffd@0: end