annotate toolboxes/FullBNT-1.0.7/bnt/learning/dirichlet_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 LL = dirichlet_score_family(counts, prior)
Daniel@0 2 % DIRICHLET_SCORE Compute the log marginal likelihood of a single family
Daniel@0 3 % LL = dirichlet_score(counts, prior)
Daniel@0 4 %
Daniel@0 5 % counts(a, b, ..., z) is the number of times parent 1 = a, parent 2 = b, ..., child = z
Daniel@0 6 % prior is an optional multidimensional array of the same shape as counts.
Daniel@0 7 % It defaults to a uniform prior.
Daniel@0 8 %
Daniel@0 9 % We marginalize out the parameters:
Daniel@0 10 % LL = log \int \prod_m P(x(i,m) | x(Pa_i,m), theta_i) P(theta_i) d(theta_i)
Daniel@0 11
Daniel@0 12
Daniel@0 13 % LL = log[ prod_j gamma(alpha_ij)/gamma(alpha_ij + N_ij) *
Daniel@0 14 % prod_k gamma(alpha_ijk + N_ijk)/gamma(alpha_ijk) ]
Daniel@0 15 % Call the prod_k term U and the prod_j term V.
Daniel@0 16 % We reshape all quantities into (j,k) matrices
Daniel@0 17 % This formula was first derived by Cooper and Herskovits, 1992.
Daniel@0 18 % See also "Learning Bayesian Networks", Heckerman, Geiger and Chickering, MLJ 95.
Daniel@0 19
Daniel@0 20 ns = mysize(counts);
Daniel@0 21 ns_ps = ns(1:end-1);
Daniel@0 22 ns_self = ns(end);
Daniel@0 23
Daniel@0 24 if nargin < 2, prior = normalise(myones(ns)); end
Daniel@0 25
Daniel@0 26
Daniel@0 27 if 1
Daniel@0 28 prior = reshape(prior(:), [prod(ns_ps) ns_self]);
Daniel@0 29 counts = reshape(counts, [prod(ns_ps) ns_self]);
Daniel@0 30 %U = prod(gamma(prior + counts) ./ gamma(prior), 2); % mult over k
Daniel@0 31 LU = sum(gammaln(prior + counts) - gammaln(prior), 2);
Daniel@0 32 alpha_ij = sum(prior, 2); % sum over k
Daniel@0 33 N_ij = sum(counts, 2);
Daniel@0 34 %V = gamma(alpha_ij) ./ gamma(alpha_ij + N_ij);
Daniel@0 35 LV = gammaln(alpha_ij) - gammaln(alpha_ij + N_ij);
Daniel@0 36 %L = prod(U .* V);
Daniel@0 37 LL = sum(LU + LV);
Daniel@0 38 else
Daniel@0 39 CPT = mk_stochastic(prior + counts);
Daniel@0 40 LL = sum(log(CPT(:) .* counts(:)));
Daniel@0 41 end
Daniel@0 42