wolffd@0: function score = score_family(j, ps, node_type, scoring_fn, ns, discrete, data, args) wolffd@0: % SCORE_FAMILY_COMPLETE Compute the score of a node and its parents given completely observed data wolffd@0: % score = score_family(j, ps, node_type, scoring_fn, ns, discrete, data, args) wolffd@0: % wolffd@0: % data(i,m) is the value of node i in case m (can be a cell array) wolffd@0: % args is a cell array containing optional arguments passed to the constructor, wolffd@0: % or is [] if none wolffd@0: % wolffd@0: % We create a whole Bayes net which only connects parents to node, wolffd@0: % where node has a CPD of the specified type (with default parameters). wolffd@0: % We then evaluate its score ('bic' or 'bayesian') wolffd@0: wolffd@0: % We should use a cache to avoid unnecessary computation. wolffd@0: % In particular, log_marginal_prob_node for tabular CPDs calls gammaln wolffd@0: % and compute_counts, both of which are slow. wolffd@0: wolffd@0: [n ncases] = size(data); wolffd@0: dag = zeros(n,n); wolffd@0: % SML added to sort ps b/c mk_bnet, learn_params use sorted ps to make wolffd@0: % CPTs wolffd@0: % Kevin had: if ~isempty(ps), dag(ps, j) = 1; end wolffd@0: if ~isempty(ps), dag(ps, j) = 1;, ps = sort(ps);, end wolffd@0: wolffd@0: bnet = mk_bnet(dag, ns, 'discrete', discrete); wolffd@0: %bnet.CPD{j} = xxx_CPD(bnet, j); wolffd@0: %eval(sprintf('bnet.CPD{j} = %s_CPD(bnet, j);', node_type)); wolffd@0: fname = sprintf('%s_CPD', node_type); wolffd@0: %fprintf('score CPD %d\n', j); wolffd@0: if isempty(args) wolffd@0: bnet.CPD{j} = feval(fname, bnet, j); wolffd@0: else wolffd@0: bnet.CPD{j} = feval(fname, bnet, j, args{:}); wolffd@0: end wolffd@0: switch scoring_fn wolffd@0: case 'bic', wolffd@0: fam = [ps j]; wolffd@0: %score = BIC_score_CPD(bnet.CPD{j}, fam, data, ns, bnet.cnodes); wolffd@0: %bnet.CPD{j} = learn_params(bnet.CPD{j}, fam, data, ns, bnet.cnodes); wolffd@0: wolffd@0: % SML 03/16/04 had to special case gaussian b/c generic_CPD/learn_params wolffd@0: % no longer supported because of simple interface to learn_params wolffd@0: % introduced by KPM for tabular nodes below: wolffd@0: % KPM 9 June 04 - tabular nodes have changed back! wolffd@0: if 1 % (isempty(find(j==discrete))) wolffd@0: bnet.CPD{j} = learn_params(bnet.CPD{j}, fam, data, ns, bnet.cnodes); wolffd@0: else wolffd@0: bnet.CPD{j} = learn_params(bnet.CPD{j}, data(fam, :)); wolffd@0: end wolffd@0: L = log_prob_node(bnet.CPD{j}, data(j,:), data(ps,:)); wolffd@0: S = struct(bnet.CPD{j}); % violate object privacy wolffd@0: score = L - 0.5*S.nparams*log(ncases); wolffd@0: case 'bayesian', wolffd@0: %score = bayesian_score_CPD(bnet.CPD{j}, data(fam, :)); wolffd@0: score = log_marg_prob_node(bnet.CPD{j}, data(j,:), data(ps,:)); wolffd@0: otherwise, wolffd@0: error(['unrecognized scoring fn ' scoring_fn]); wolffd@0: end