wolffd@0
|
1 function lam_msg = CPD_to_lambda_msg(CPD, msg_type, n, ps, msg, p, evidence)
|
wolffd@0
|
2 % CPD_TO_LAMBDA_MSG Compute lambda message (gmux)
|
wolffd@0
|
3 % lam_msg = compute_lambda_msg(CPD, msg_type, n, ps, msg, p, evidence)
|
wolffd@0
|
4 % Pearl p183 eq 4.52
|
wolffd@0
|
5
|
wolffd@0
|
6 % Let Y be this node, X1..Xn be the cts parents and M the discrete switch node.
|
wolffd@0
|
7 % e.g., for n=3, M=1
|
wolffd@0
|
8 %
|
wolffd@0
|
9 % X1 X2 X3 M
|
wolffd@0
|
10 % \
|
wolffd@0
|
11 % \
|
wolffd@0
|
12 % Y
|
wolffd@0
|
13 %
|
wolffd@0
|
14 % So the only case in which we send an informative message is if p=1=M.
|
wolffd@0
|
15 % To the other cts parents, we send the "know nothing" message.
|
wolffd@0
|
16
|
wolffd@0
|
17 switch msg_type
|
wolffd@0
|
18 case 'd',
|
wolffd@0
|
19 error('gaussian_CPD can''t create discrete msgs')
|
wolffd@0
|
20 case 'g',
|
wolffd@0
|
21 cps = ps(CPD.cps);
|
wolffd@0
|
22 cpsizes = CPD.sizes(CPD.cps);
|
wolffd@0
|
23 self_size = CPD.sizes(end);
|
wolffd@0
|
24 i = find_equiv_posns(p, cps); % p is n's i'th cts parent
|
wolffd@0
|
25 psz = cpsizes(i);
|
wolffd@0
|
26 dps = ps(CPD.dps);
|
wolffd@0
|
27 M = evidence{dps};
|
wolffd@0
|
28 if isempty(M)
|
wolffd@0
|
29 error('gmux node must have observed discrete parent')
|
wolffd@0
|
30 end
|
wolffd@0
|
31 P = msg{n}.lambda.precision;
|
wolffd@0
|
32 if all(P == 0) | (cps(M) ~= p) % if we know nothing, or are sending to a disconnected parent
|
wolffd@0
|
33 lam_msg.precision = zeros(psz, psz);
|
wolffd@0
|
34 lam_msg.info_state = zeros(psz, 1);
|
wolffd@0
|
35 return;
|
wolffd@0
|
36 end
|
wolffd@0
|
37 % We are sending a message to the only effectively connected parent.
|
wolffd@0
|
38 % There are no other incoming pi messages.
|
wolffd@0
|
39 Bmu = CPD.mean(:,M);
|
wolffd@0
|
40 BSigma = CPD.cov(:,:,M);
|
wolffd@0
|
41 Bi = CPD.weights(:,:,M);
|
wolffd@0
|
42 if (det(P) > 0) | isinf(P)
|
wolffd@0
|
43 if isinf(P) % Y is observed
|
wolffd@0
|
44 Sigma_lambda = zeros(self_size, self_size); % infinite precision => 0 variance
|
wolffd@0
|
45 mu_lambda = msg{n}.lambda.mu; % observed_value;
|
wolffd@0
|
46 else
|
wolffd@0
|
47 Sigma_lambda = inv(P);
|
wolffd@0
|
48 mu_lambda = Sigma_lambda * msg{n}.lambda.info_state;
|
wolffd@0
|
49 end
|
wolffd@0
|
50 C = inv(Sigma_lambda + BSigma);
|
wolffd@0
|
51 lam_msg.precision = Bi' * C * Bi;
|
wolffd@0
|
52 lam_msg.info_state = Bi' * C * (mu_lambda - Bmu);
|
wolffd@0
|
53 else
|
wolffd@0
|
54 % method that uses matrix inversion lemma
|
wolffd@0
|
55 A = inv(P + inv(BSigma));
|
wolffd@0
|
56 C = P - P*A*P;
|
wolffd@0
|
57 lam_msg.precision = Bi' * C * Bi;
|
wolffd@0
|
58 D = eye(self_size) - P*A;
|
wolffd@0
|
59 z = msg{n}.lambda.info_state;
|
wolffd@0
|
60 lam_msg.info_state = Bi' * (D*z - D*P*Bmu);
|
wolffd@0
|
61 end
|
wolffd@0
|
62 end
|