annotate toolboxes/FullBNT-1.0.7/bnt/learning/score_family.m @ 0:cc4b1211e677 tip

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