annotate toolboxes/FullBNT-1.0.7/bnt/examples/static/StructLearn/k2demo1.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 N = 4;
Daniel@0 2 dag = zeros(N,N);
Daniel@0 3 %C = 1; S = 2; R = 3; W = 4;
Daniel@0 4 C = 4; S = 2; R = 3; W = 1; % arbitrary order
Daniel@0 5 dag(C,[R S]) = 1;
Daniel@0 6 dag(R,W) = 1;
Daniel@0 7 dag(S,W)=1;
Daniel@0 8
Daniel@0 9 false = 1; true = 2;
Daniel@0 10 ns = 2*ones(1,N); % binary nodes
Daniel@0 11
Daniel@0 12 bnet = mk_bnet(dag, ns);
Daniel@0 13 bnet.CPD{C} = tabular_CPD(bnet, C, [0.5 0.5]);
Daniel@0 14 bnet.CPD{R} = tabular_CPD(bnet, R, [0.8 0.2 0.2 0.8]);
Daniel@0 15 bnet.CPD{S} = tabular_CPD(bnet, S, [0.5 0.9 0.5 0.1]);
Daniel@0 16 bnet.CPD{W} = tabular_CPD(bnet, W, [1 0.1 0.1 0.01 0 0.9 0.9 0.99]);
Daniel@0 17
Daniel@0 18 seed = 0;
Daniel@0 19 rand('state', seed);
Daniel@0 20 randn('state', seed);
Daniel@0 21 ncases = 100;
Daniel@0 22 data = zeros(N, ncases);
Daniel@0 23 for m=1:ncases
Daniel@0 24 data(:,m) = cell2num(sample_bnet(bnet));
Daniel@0 25 end
Daniel@0 26
Daniel@0 27 order = [C S R W];
Daniel@0 28 max_fan_in = 2;
Daniel@0 29
Daniel@0 30 %dag2 = learn_struct_K2(data, ns, order, 'max_fan_in', max_fan_in, 'verbose', 'yes');
Daniel@0 31
Daniel@0 32 sz = 5:5:50;
Daniel@0 33 for i=1:length(sz)
Daniel@0 34 dag2 = learn_struct_K2(data(:,1:sz(i)), ns, order, 'max_fan_in', max_fan_in);
Daniel@0 35 correct(i) = isequal(dag, dag2);
Daniel@0 36 end
Daniel@0 37 correct
Daniel@0 38
Daniel@0 39 for i=1:length(sz)
Daniel@0 40 dag3 = learn_struct_K2(data(:,1:sz(i)), ns, order, 'max_fan_in', max_fan_in, 'scoring_fn', 'bic', 'params', []);
Daniel@0 41 correct(i) = isequal(dag, dag3);
Daniel@0 42 end
Daniel@0 43 correct
Daniel@0 44
Daniel@0 45