comparison toolboxes/FullBNT-1.0.7/bnt/examples/static/fa1.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 % Factor analysis
2 % Z -> X, Z in R^k, X in R^D, k << D (high dimensional observations explained by small source)
3 % Z ~ N(0,I), X|Z ~ N(L z, Psi), where Psi is diagonal.
4 %
5 % We compare to Zoubin Ghahramani's code.
6
7 state = 0;
8 rand('seed', state);
9 randn('seed', state);
10 max_iter = 3;
11 k = 2;
12 D = 4;
13 N = 10;
14 X = randn(N, D);
15
16 % Initialize as in Zoubin's ffa (fast factor analysis)
17 X=X-ones(N,1)*mean(X);
18 XX=X'*X/N;
19 diagXX=diag(XX);
20 cX=cov(X);
21 scale=det(cX)^(1/D);
22 randn('seed', 0); % must reset seed here so initial params are identical to mfa
23 L0=randn(D,k)*sqrt(scale/k);
24 W0 = L0;
25 Psi0=diag(cX);
26
27 [L1, Psi1, LL1] = ffa(X,k,max_iter);
28
29
30 ns = [k D];
31 dag = zeros(2,2);
32 dag(1,2) = 1;
33 bnet = mk_bnet(dag, ns, 'discrete', [], 'observed', 2);
34 bnet.CPD{1} = gaussian_CPD(bnet, 1, 'mean', zeros(k,1), 'cov', eye(k), 'cov_type', 'diag', ...
35 'clamp_mean', 1, 'clamp_cov', 1);
36 bnet.CPD{2} = gaussian_CPD(bnet, 2, 'mean', zeros(D,1), 'cov', diag(Psi0), 'weights', W0, ...
37 'cov_type', 'diag', 'cov_prior_weight', 0, 'clamp_mean', 1);
38
39 engine = jtree_inf_engine(bnet);
40 evidence = cell(2,N);
41 evidence(2,:) = num2cell(X', 1);
42
43 [bnet2, LL2] = learn_params_em(engine, evidence, max_iter);
44
45 s = struct(bnet2.CPD{2});
46 L2 = s.weights;
47 Psi2 = s.cov;
48
49
50
51 % Compare to Zoubin's code
52 assert(approxeq(LL2, LL1));
53 assert(approxeq(Psi2, diag(Psi1)));
54 assert(approxeq(L2, L1));
55
56
57