annotate toolboxes/FullBNT-1.0.7/bnt/examples/static/mixexp2.m @ 0:cc4b1211e677 tip

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