comparison toolboxes/FullBNT-1.0.7/bnt/CPDs/@gaussian_CPD/CPD_to_lambda_msg.m @ 0:e9a9cd732c1e tip

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