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