annotate _FullBNT/BNT/CPDs/@gaussian_CPD/CPD_to_lambda_msg.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 lam_msg = CPD_to_lambda_msg(CPD, msg_type, n, ps, msg, p, evidence)
matthiasm@8 2 % CPD_TO_LAMBDA_MSG Compute lambda message (gaussian)
matthiasm@8 3 % lam_msg = compute_lambda_msg(CPD, msg_type, n, ps, msg, p, evidence)
matthiasm@8 4 % Pearl p183 eq 4.52
matthiasm@8 5
matthiasm@8 6 switch msg_type
matthiasm@8 7 case 'd',
matthiasm@8 8 error('gaussian_CPD can''t create discrete msgs')
matthiasm@8 9 case 'g',
matthiasm@8 10 cps = ps(CPD.cps);
matthiasm@8 11 cpsizes = CPD.sizes(CPD.cps);
matthiasm@8 12 self_size = CPD.sizes(end);
matthiasm@8 13 i = find_equiv_posns(p, cps); % p is n's i'th cts parent
matthiasm@8 14 psz = cpsizes(i);
matthiasm@8 15 if all(msg{n}.lambda.precision == 0) % no info to send on
matthiasm@8 16 lam_msg.precision = zeros(psz, psz);
matthiasm@8 17 lam_msg.info_state = zeros(psz, 1);
matthiasm@8 18 return;
matthiasm@8 19 end
matthiasm@8 20 [m, Q, W] = gaussian_CPD_params_given_dps(CPD, [ps n], evidence);
matthiasm@8 21 Bmu = m;
matthiasm@8 22 BSigma = Q;
matthiasm@8 23 for k=1:length(cps) % only get pi msgs from cts parents
matthiasm@8 24 pk = cps(k);
matthiasm@8 25 if pk ~= p
matthiasm@8 26 %bk = block(k, cpsizes);
matthiasm@8 27 bk = CPD.cps_block_ndx{k};
matthiasm@8 28 Bk = W(:, bk);
matthiasm@8 29 m = msg{n}.pi_from_parent{k};
matthiasm@8 30 BSigma = BSigma + Bk * m.Sigma * Bk';
matthiasm@8 31 Bmu = Bmu + Bk * m.mu;
matthiasm@8 32 end
matthiasm@8 33 end
matthiasm@8 34 % BSigma = Q + sum_{k \neq i} B_k Sigma_k B_k'
matthiasm@8 35 %bi = block(i, cpsizes);
matthiasm@8 36 bi = CPD.cps_block_ndx{i};
matthiasm@8 37 Bi = W(:,bi);
matthiasm@8 38 P = msg{n}.lambda.precision;
matthiasm@8 39 if (rcond(P) > 1e-3) | isinf(P)
matthiasm@8 40 if isinf(P) % Y is observed
matthiasm@8 41 Sigma_lambda = zeros(self_size, self_size); % infinite precision => 0 variance
matthiasm@8 42 mu_lambda = msg{n}.lambda.mu; % observed_value;
matthiasm@8 43 else
matthiasm@8 44 Sigma_lambda = inv(P);
matthiasm@8 45 mu_lambda = Sigma_lambda * msg{n}.lambda.info_state;
matthiasm@8 46 end
matthiasm@8 47 C = inv(Sigma_lambda + BSigma);
matthiasm@8 48 lam_msg.precision = Bi' * C * Bi;
matthiasm@8 49 lam_msg.info_state = Bi' * C * (mu_lambda - Bmu);
matthiasm@8 50 else
matthiasm@8 51 % method that uses matrix inversion lemma to avoid inverting P
matthiasm@8 52 A = inv(P + inv(BSigma));
matthiasm@8 53 C = P - P*A*P;
matthiasm@8 54 lam_msg.precision = Bi' * C * Bi;
matthiasm@8 55 D = eye(self_size) - P*A;
matthiasm@8 56 z = msg{n}.lambda.info_state;
matthiasm@8 57 lam_msg.info_state = Bi' * (D*z - D*P*Bmu);
matthiasm@8 58 end
matthiasm@8 59 end