comparison toolboxes/FullBNT-1.0.7/bnt/learning/score_family.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 score = score_family(j, ps, node_type, scoring_fn, ns, discrete, data, args)
2 % SCORE_FAMILY_COMPLETE Compute the score of a node and its parents given completely observed data
3 % score = score_family(j, ps, node_type, scoring_fn, ns, discrete, data, args)
4 %
5 % data(i,m) is the value of node i in case m (can be a cell array)
6 % args is a cell array containing optional arguments passed to the constructor,
7 % or is [] if none
8 %
9 % We create a whole Bayes net which only connects parents to node,
10 % where node has a CPD of the specified type (with default parameters).
11 % We then evaluate its score ('bic' or 'bayesian')
12
13 % We should use a cache to avoid unnecessary computation.
14 % In particular, log_marginal_prob_node for tabular CPDs calls gammaln
15 % and compute_counts, both of which are slow.
16
17 [n ncases] = size(data);
18 dag = zeros(n,n);
19 % SML added to sort ps b/c mk_bnet, learn_params use sorted ps to make
20 % CPTs
21 % Kevin had: if ~isempty(ps), dag(ps, j) = 1; end
22 if ~isempty(ps), dag(ps, j) = 1;, ps = sort(ps);, end
23
24 bnet = mk_bnet(dag, ns, 'discrete', discrete);
25 %bnet.CPD{j} = xxx_CPD(bnet, j);
26 %eval(sprintf('bnet.CPD{j} = %s_CPD(bnet, j);', node_type));
27 fname = sprintf('%s_CPD', node_type);
28 %fprintf('score CPD %d\n', j);
29 if isempty(args)
30 bnet.CPD{j} = feval(fname, bnet, j);
31 else
32 bnet.CPD{j} = feval(fname, bnet, j, args{:});
33 end
34 switch scoring_fn
35 case 'bic',
36 fam = [ps j];
37 %score = BIC_score_CPD(bnet.CPD{j}, fam, data, ns, bnet.cnodes);
38 %bnet.CPD{j} = learn_params(bnet.CPD{j}, fam, data, ns, bnet.cnodes);
39
40 % SML 03/16/04 had to special case gaussian b/c generic_CPD/learn_params
41 % no longer supported because of simple interface to learn_params
42 % introduced by KPM for tabular nodes below:
43 % KPM 9 June 04 - tabular nodes have changed back!
44 if 1 % (isempty(find(j==discrete)))
45 bnet.CPD{j} = learn_params(bnet.CPD{j}, fam, data, ns, bnet.cnodes);
46 else
47 bnet.CPD{j} = learn_params(bnet.CPD{j}, data(fam, :));
48 end
49 L = log_prob_node(bnet.CPD{j}, data(j,:), data(ps,:));
50 S = struct(bnet.CPD{j}); % violate object privacy
51 score = L - 0.5*S.nparams*log(ncases);
52 case 'bayesian',
53 %score = bayesian_score_CPD(bnet.CPD{j}, data(fam, :));
54 score = log_marg_prob_node(bnet.CPD{j}, data(j,:), data(ps,:));
55 otherwise,
56 error(['unrecognized scoring fn ' scoring_fn]);
57 end