wolffd@0
|
1 function score = score_dags(data, ns, dags, varargin)
|
wolffd@0
|
2 % SCORE_DAGS Compute the score of one or more DAGs
|
wolffd@0
|
3 % score = score_dags(data, ns, dags, varargin)
|
wolffd@0
|
4 %
|
wolffd@0
|
5 % data{i,m} = value of node i in case m (can be a cell array).
|
wolffd@0
|
6 % node_sizes(i) is the number of size of node i.
|
wolffd@0
|
7 % dags{g} is the g'th dag
|
wolffd@0
|
8 % score(g) is the score of the i'th dag
|
wolffd@0
|
9 %
|
wolffd@0
|
10 % The following optional arguments can be specified in the form of name/value pairs:
|
wolffd@0
|
11 % [default value in brackets]
|
wolffd@0
|
12 %
|
wolffd@0
|
13 % scoring_fn - 'bayesian' or 'bic' [ 'bayesian' ]
|
wolffd@0
|
14 % Currently, only networks with all tabular nodes support Bayesian scoring.
|
wolffd@0
|
15 % type - type{i} is the type of CPD to use for node i, where the type is a string
|
wolffd@0
|
16 % of the form 'tabular', 'noisy_or', 'gaussian', etc. [ all cells contain 'tabular' ]
|
wolffd@0
|
17 % params - params{i} contains optional arguments passed to the CPD constructor for node i,
|
wolffd@0
|
18 % or [] if none. [ all cells contain {'prior', 1}, meaning use uniform Dirichlet priors ]
|
wolffd@0
|
19 % discrete - the list of discrete nodes [ 1:N ]
|
wolffd@0
|
20 % clamped - clamped(i,m) = 1 if node i is clamped in case m [ zeros(N, ncases) ]
|
wolffd@0
|
21 %
|
wolffd@0
|
22 % e.g., score = score_dags(data, ns, mk_all_dags(n), 'scoring_fn', 'bic', 'params', []);
|
wolffd@0
|
23 %
|
wolffd@0
|
24 % If the DAGs have a lot of families in common, we can cache the sufficient statistics,
|
wolffd@0
|
25 % making this potentially more efficient than scoring the DAGs one at a time.
|
wolffd@0
|
26 % (Caching is not currently implemented, however.)
|
wolffd@0
|
27
|
wolffd@0
|
28 [n ncases] = size(data);
|
wolffd@0
|
29
|
wolffd@0
|
30 % set default params
|
wolffd@0
|
31 type = cell(1,n);
|
wolffd@0
|
32 params = cell(1,n);
|
wolffd@0
|
33 for i=1:n
|
wolffd@0
|
34 type{i} = 'tabular';
|
wolffd@0
|
35 params{i} = { 'prior_type', 'dirichlet', 'dirichlet_weight', 1 };
|
wolffd@0
|
36 end
|
wolffd@0
|
37 scoring_fn = 'bayesian';
|
wolffd@0
|
38 discrete = 1:n;
|
wolffd@0
|
39 clamped = zeros(n, ncases);
|
wolffd@0
|
40
|
wolffd@0
|
41 args = varargin;
|
wolffd@0
|
42 nargs = length(args);
|
wolffd@0
|
43 for i=1:2:nargs
|
wolffd@0
|
44 switch args{i},
|
wolffd@0
|
45 case 'scoring_fn', scoring_fn = args{i+1};
|
wolffd@0
|
46 case 'type', type = args{i+1};
|
wolffd@0
|
47 case 'discrete', discrete = args{i+1};
|
wolffd@0
|
48 case 'clamped', clamped = args{i+1};
|
wolffd@0
|
49 case 'params', if isempty(args{i+1}), params = cell(1,n); else params = args{i+1}; end
|
wolffd@0
|
50 end
|
wolffd@0
|
51 end
|
wolffd@0
|
52
|
wolffd@0
|
53 NG = length(dags);
|
wolffd@0
|
54 score = zeros(1, NG);
|
wolffd@0
|
55 for g=1:NG
|
wolffd@0
|
56 dag = dags{g};
|
wolffd@0
|
57 for j=1:n
|
wolffd@0
|
58 u = find(clamped(j,:)==0);
|
wolffd@0
|
59 ps = parents(dag, j);
|
wolffd@0
|
60 score(g) = score(g) + score_family(j, ps, type{j}, scoring_fn, ns, discrete, data(:,u), params{j});
|
wolffd@0
|
61 end
|
wolffd@0
|
62 end
|