annotate _FullBNT/BNT/learning/score_family.m @ 9:4ea6619cb3f5 tip

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