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