score_family.m

来自「麻省理工学院的人工智能工具箱,很珍贵,希望对大家有用!」· M 代码 · 共 47 行

M
47
字号
function score = score_family(j, ps, node_type, scoring_fn, ns, discrete, data, args)
% SCORE_FAMILY_COMPLETE Compute the score of a node and its parents given completely observed data
% score = score_family(j, ps, node_type, scoring_fn, ns, discrete, data, args)
%
% data(i,m) is the value of node i in case m (can be a cell array)
% args is a cell array containing optional arguments passed to the constructor,
% or is [] if none
%
% We create a whole Bayes net which only connects parents to node,
% where node has a CPD of the specified type (with default parameters).
% We then evaluate its score ('bic' or 'bayesian')

% We should use a cache to avoid unnecessary computation.
% In particular, log_marginal_prob_node for tabular CPDs calls gammaln
% and compute_counts, both of which are slow.

[n ncases] = size(data);
dag = zeros(n,n);
if ~isempty(ps), dag(ps, j) = 1; end

bnet = mk_bnet(dag, ns, 'discrete', discrete);
%bnet.CPD{j} = xxx_CPD(bnet, j);
%eval(sprintf('bnet.CPD{j} = %s_CPD(bnet, j);', node_type));
fname = sprintf('%s_CPD', node_type);
%fprintf('score CPD %d\n', j);
if isempty(args)
  bnet.CPD{j} = feval(fname, bnet, j);
else
  bnet.CPD{j} = feval(fname, bnet, j, args{:});
end
switch scoring_fn
 case 'bic',
  fam = [ps j];
  %score = BIC_score_CPD(bnet.CPD{j}, fam, data, ns, bnet.cnodes);
  bnet.CPD{j} = learn_params(bnet.CPD{j}, fam, data, ns, bnet.cnodes);
  L = log_prob_node(bnet.CPD{j}, data(j,:), data(ps,:));
  S = struct(bnet.CPD{j}); % violate object privacy
  score = L - 0.5*S.nparams*log(ncases);
 case 'bayesian', 
  %score = bayesian_score_CPD(bnet.CPD{j}, data(fam, :));
  score = log_marg_prob_node(bnet.CPD{j}, data(j,:), data(ps,:));
  %score = log_prob_node(bnet.CPD{j}, data(j,:), data(ps,:));

 otherwise,
  error(['unrecognized scoring fn ' scoring_fn]);
end

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?