wolffd@0: function lam_msg = CPD_to_lambda_msg(CPD, msg_type, n, ps, msg, p, evidence) wolffd@0: % CPD_TO_LAMBDA_MSG Compute lambda message (gaussian) wolffd@0: % lam_msg = compute_lambda_msg(CPD, msg_type, n, ps, msg, p, evidence) wolffd@0: % Pearl p183 eq 4.52 wolffd@0: wolffd@0: switch msg_type wolffd@0: case 'd', wolffd@0: error('gaussian_CPD can''t create discrete msgs') wolffd@0: case 'g', wolffd@0: cps = ps(CPD.cps); wolffd@0: cpsizes = CPD.sizes(CPD.cps); wolffd@0: self_size = CPD.sizes(end); wolffd@0: i = find_equiv_posns(p, cps); % p is n's i'th cts parent wolffd@0: psz = cpsizes(i); wolffd@0: if all(msg{n}.lambda.precision == 0) % no info to send on wolffd@0: lam_msg.precision = zeros(psz, psz); wolffd@0: lam_msg.info_state = zeros(psz, 1); wolffd@0: return; wolffd@0: end wolffd@0: [m, Q, W] = gaussian_CPD_params_given_dps(CPD, [ps n], evidence); wolffd@0: Bmu = m; wolffd@0: BSigma = Q; wolffd@0: for k=1:length(cps) % only get pi msgs from cts parents wolffd@0: pk = cps(k); wolffd@0: if pk ~= p wolffd@0: %bk = block(k, cpsizes); wolffd@0: bk = CPD.cps_block_ndx{k}; wolffd@0: Bk = W(:, bk); wolffd@0: m = msg{n}.pi_from_parent{k}; wolffd@0: BSigma = BSigma + Bk * m.Sigma * Bk'; wolffd@0: Bmu = Bmu + Bk * m.mu; wolffd@0: end wolffd@0: end wolffd@0: % BSigma = Q + sum_{k \neq i} B_k Sigma_k B_k' wolffd@0: %bi = block(i, cpsizes); wolffd@0: bi = CPD.cps_block_ndx{i}; wolffd@0: Bi = W(:,bi); wolffd@0: P = msg{n}.lambda.precision; wolffd@0: if (rcond(P) > 1e-3) | isinf(P) wolffd@0: if isinf(P) % Y is observed wolffd@0: Sigma_lambda = zeros(self_size, self_size); % infinite precision => 0 variance wolffd@0: mu_lambda = msg{n}.lambda.mu; % observed_value; wolffd@0: else wolffd@0: Sigma_lambda = inv(P); wolffd@0: mu_lambda = Sigma_lambda * msg{n}.lambda.info_state; wolffd@0: end wolffd@0: C = inv(Sigma_lambda + BSigma); wolffd@0: lam_msg.precision = Bi' * C * Bi; wolffd@0: lam_msg.info_state = Bi' * C * (mu_lambda - Bmu); wolffd@0: else wolffd@0: % method that uses matrix inversion lemma to avoid inverting P wolffd@0: A = inv(P + inv(BSigma)); wolffd@0: C = P - P*A*P; wolffd@0: lam_msg.precision = Bi' * C * Bi; wolffd@0: D = eye(self_size) - P*A; wolffd@0: z = msg{n}.lambda.info_state; wolffd@0: lam_msg.info_state = Bi' * (D*z - D*P*Bmu); wolffd@0: end wolffd@0: end