Mercurial > hg > camir-aes2014
comparison toolboxes/FullBNT-1.0.7/bnt/learning/learn_struct_K2.m @ 0:e9a9cd732c1e tip
first hg version after svn
author | wolffd |
---|---|
date | Tue, 10 Feb 2015 15:05:51 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:e9a9cd732c1e |
---|---|
1 function dag = learn_struct_K2(data, ns, order, varargin) | |
2 % LEARN_STRUCT_K2 Greedily learn the best structure compatible with a fixed node ordering | |
3 % best_dag = learn_struct_K2(data, node_sizes, order, ...) | |
4 % | |
5 % data(i,m) = value of node i in case m (can be a cell array). | |
6 % node_sizes(i) is the size of node i. | |
7 % order(i) is the i'th node in the topological ordering. | |
8 % | |
9 % The following optional arguments can be specified in the form of name/value pairs: | |
10 % [default value in brackets] | |
11 % | |
12 % max_fan_in - this the largest number of parents we allow per node [N] | |
13 % scoring_fn - 'bayesian' or 'bic' [ 'bayesian' ] | |
14 % Currently, only networks with all tabular nodes support Bayesian scoring. | |
15 % type - type{i} is the type of CPD to use for node i, where the type is a string | |
16 % of the form 'tabular', 'noisy_or', 'gaussian', etc. [ all cells contain 'tabular' ] | |
17 % params - params{i} contains optional arguments passed to the CPD constructor for node i, | |
18 % or [] if none. [ all cells contain {'prior', 1}, meaning use uniform Dirichlet priors ] | |
19 % discrete - the list of discrete nodes [ 1:N ] | |
20 % clamped - clamped(i,m) = 1 if node i is clamped in case m [ zeros(N, ncases) ] | |
21 % verbose - 'yes' means display output while running [ 'no' ] | |
22 % | |
23 % e.g., dag = learn_struct_K2(data, ns, order, 'scoring_fn', 'bic', 'params', []) | |
24 % | |
25 % To be backwards compatible with BNT2, you can also specify arguments as follows | |
26 % dag = learn_struct_K2(data, node_sizes, order, max_fan_in) | |
27 % | |
28 % This algorithm is described in | |
29 % - Cooper and Herskovits, "A Bayesian method for the induction of probabilistic | |
30 % networks from data", Machine Learning Journal 9:308--347, 1992 | |
31 | |
32 [n ncases] = size(data); | |
33 | |
34 % set default params | |
35 type = cell(1,n); | |
36 params = cell(1,n); | |
37 for i=1:n | |
38 type{i} = 'tabular'; | |
39 %params{i} = { 'prior', 1 }; | |
40 params{i} = { 'prior_type', 'dirichlet', 'dirichlet_weight', 1 }; | |
41 end | |
42 scoring_fn = 'bayesian'; | |
43 discrete = 1:n; | |
44 clamped = zeros(n, ncases); | |
45 | |
46 max_fan_in = n; | |
47 verbose = 0; | |
48 | |
49 args = varargin; | |
50 nargs = length(args); | |
51 if length(args) > 0 | |
52 if isstr(args{1}) | |
53 for i=1:2:nargs | |
54 switch args{i}, | |
55 case 'verbose', verbose = strcmp(args{i+1}, 'yes'); | |
56 case 'max_fan_in', max_fan_in = args{i+1}; | |
57 case 'scoring_fn', scoring_fn = args{i+1}; | |
58 case 'type', type = args{i+1}; | |
59 case 'discrete', discrete = args{i+1}; | |
60 case 'clamped', clamped = args{i+1}; | |
61 case 'params', if isempty(args{i+1}), params = cell(1,n); else params = args{i+1}; end | |
62 end | |
63 end | |
64 else | |
65 max_fan_in = args{1}; | |
66 end | |
67 end | |
68 | |
69 dag = zeros(n,n); | |
70 | |
71 for i=1:n | |
72 ps = []; | |
73 j = order(i); | |
74 u = find(clamped(j,:)==0); | |
75 score = score_family(j, ps, type{j}, scoring_fn, ns, discrete, data(:,u), params{j}); | |
76 if verbose, fprintf('\nnode %d, empty score %6.4f\n', j, score); end | |
77 done = 0; | |
78 while ~done & (length(ps) <= max_fan_in) | |
79 pps = mysetdiff(order(1:i-1), ps); % potential parents | |
80 nps = length(pps); | |
81 pscore = zeros(1, nps); | |
82 for pi=1:nps | |
83 p = pps(pi); | |
84 pscore(pi) = score_family(j, [ps p], type{j}, scoring_fn, ns, discrete, data(:,u), params{j}); | |
85 if verbose, fprintf('considering adding %d to %d, score %6.4f\n', p, j, pscore(pi)); end | |
86 end | |
87 [best_pscore, best_p] = max(pscore); | |
88 best_p = pps(best_p); | |
89 if best_pscore > score | |
90 score = best_pscore; | |
91 ps = [ps best_p]; | |
92 if verbose, fprintf('* adding %d to %d, score %6.4f\n', best_p, j, best_pscore); end | |
93 else | |
94 done = 1; | |
95 end | |
96 end | |
97 if ~isempty(ps) % need this check for matlab 5.2 | |
98 dag(ps, j) = 1; | |
99 end | |
100 end | |
101 | |
102 | |
103 | |
104 |