comparison toolboxes/FullBNT-1.0.7/bnt/examples/static/mixexp2.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 % Fit a piece-wise linear regression model.
2 % Here is the model
3 %
4 % X \
5 % | |
6 % Q |
7 % | /
8 % Y
9 %
10 % where all arcs point down.
11 % We condition everything on X, so X is a root node. Q is a softmax, and Y is a linear Gaussian.
12 % Q is hidden, X and Y are observed.
13
14 X = 1;
15 Q = 2;
16 Y = 3;
17 dag = zeros(3,3);
18 dag(X,[Q Y]) = 1;
19 dag(Q,Y) = 1;
20 ns = [1 2 1]; % make X and Y scalars, and have 2 experts
21 dnodes = [2];
22 onodes = [1 3];
23 bnet = mk_bnet(dag, ns, 'discrete', dnodes, 'observed', onodes);
24
25 IRLS_iter = 10;
26 clamped = 0;
27
28 bnet.CPD{1} = root_CPD(bnet, 1);
29
30 if 0
31 % start with good initial params
32 w = [-5 5]; % w(:,i) is the normal vector to the i'th decisions boundary
33 b = [0 0]; % b(i) is the offset (bias) to the i'th decisions boundary
34
35 mu = [0 0];
36 sigma = 1;
37 Sigma = repmat(sigma*eye(ns(Y)), [ns(Y) ns(Y) ns(Q)]);
38 W = [-1 1];
39 W2 = reshape(W, [ns(Y) ns(X) ns(Q)]);
40
41 bnet.CPD{2} = softmax_CPD(bnet, 2, w, b, clamped, IRLS_iter);
42 bnet.CPD{3} = gaussian_CPD(bnet, 3, mu, Sigma, W2);
43 else
44 % start with rnd initial params
45 rand('state', 0);
46 randn('state', 0);
47 bnet.CPD{2} = softmax_CPD(bnet, 2, 'clamped', clamped, 'max_iter', IRLS_iter);
48 bnet.CPD{3} = gaussian_CPD(bnet, 3);
49 end
50
51
52
53 load('/examples/static/Misc/mixexp_data.txt', '-ascii');
54 % Just use 1/10th of the data, to speed things up
55 data = mixexp_data(1:10:end, :);
56 %data = mixexp_data;
57
58 %plot(data(:,1), data(:,2), '.')
59
60
61 s = struct(bnet.CPD{2}); % violate object privacy
62 %eta0 = [s.glim.b1; s.glim.w1]';
63 eta0 = [s.glim{1}.b1; s.glim{1}.w1]';
64 s = struct(bnet.CPD{3}); % violate object privacy
65 W = reshape(s.weights, [1 2]);
66 theta0 = [s.mean; W]';
67
68 %figure(1)
69 %mixexp_plot(theta0, eta0, data);
70 %suptitle('before learning')
71
72 ncases = size(data, 1);
73 cases = cell(3, ncases);
74 cases([1 3], :) = num2cell(data');
75
76 engine = jtree_inf_engine(bnet);
77
78 % log lik before learning
79 ll = 0;
80 for l=1:ncases
81 ev = cases(:,l);
82 [engine, loglik] = enter_evidence(engine, ev);
83 ll = ll + loglik;
84 end
85
86 % do learning
87 max_iter = 5;
88 [bnet2, LL2] = learn_params_em(engine, cases, max_iter);
89
90 s = struct(bnet2.CPD{2});
91 %eta2 = [s.glim.b1; s.glim.w1]';
92 eta2 = [s.glim{1}.b1; s.glim{1}.w1]';
93 s = struct(bnet2.CPD{3});
94 W = reshape(s.weights, [1 2]);
95 theta2 = [s.mean; W]';
96
97 %figure(2)
98 %mixexp_plot(theta2, eta2, data);
99 %suptitle('after learning')
100
101 fprintf('mixexp2: loglik before learning %f, after %d iters %f\n', ll, length(LL2), LL2(end));
102
103
104