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