Daniel@0: % Fit a piece-wise linear regression model. Daniel@0: % Here is the model Daniel@0: % Daniel@0: % X \ Daniel@0: % | | Daniel@0: % Q | Daniel@0: % | / Daniel@0: % Y Daniel@0: % Daniel@0: % where all arcs point down. Daniel@0: % We condition everything on X, so X is a root node. Q is a softmax, and Y is a linear Gaussian. Daniel@0: % Q is hidden, X and Y are observed. Daniel@0: Daniel@0: X = 1; Daniel@0: Q = 2; Daniel@0: Y = 3; Daniel@0: dag = zeros(3,3); Daniel@0: dag(X,[Q Y]) = 1; Daniel@0: dag(Q,Y) = 1; Daniel@0: ns = [1 2 1]; % make X and Y scalars, and have 2 experts Daniel@0: dnodes = [2]; Daniel@0: onodes = [1 3]; Daniel@0: bnet = mk_bnet(dag, ns, 'discrete', dnodes, 'observed', onodes); Daniel@0: Daniel@0: IRLS_iter = 10; Daniel@0: clamped = 0; Daniel@0: Daniel@0: bnet.CPD{1} = root_CPD(bnet, 1); Daniel@0: Daniel@0: if 0 Daniel@0: % start with good initial params Daniel@0: w = [-5 5]; % w(:,i) is the normal vector to the i'th decisions boundary Daniel@0: b = [0 0]; % b(i) is the offset (bias) to the i'th decisions boundary Daniel@0: Daniel@0: mu = [0 0]; Daniel@0: sigma = 1; Daniel@0: Sigma = repmat(sigma*eye(ns(Y)), [ns(Y) ns(Y) ns(Q)]); Daniel@0: W = [-1 1]; Daniel@0: W2 = reshape(W, [ns(Y) ns(X) ns(Q)]); Daniel@0: Daniel@0: bnet.CPD{2} = softmax_CPD(bnet, 2, w, b, clamped, IRLS_iter); Daniel@0: bnet.CPD{3} = gaussian_CPD(bnet, 3, mu, Sigma, W2); Daniel@0: else Daniel@0: % start with rnd initial params Daniel@0: rand('state', 0); Daniel@0: randn('state', 0); Daniel@0: bnet.CPD{2} = softmax_CPD(bnet, 2, 'clamped', clamped, 'max_iter', IRLS_iter); Daniel@0: bnet.CPD{3} = gaussian_CPD(bnet, 3); Daniel@0: end Daniel@0: Daniel@0: Daniel@0: Daniel@0: load('/examples/static/Misc/mixexp_data.txt', '-ascii'); Daniel@0: % Just use 1/10th of the data, to speed things up Daniel@0: data = mixexp_data(1:10:end, :); Daniel@0: %data = mixexp_data; Daniel@0: Daniel@0: %plot(data(:,1), data(:,2), '.') Daniel@0: Daniel@0: Daniel@0: s = struct(bnet.CPD{2}); % violate object privacy Daniel@0: %eta0 = [s.glim.b1; s.glim.w1]'; Daniel@0: eta0 = [s.glim{1}.b1; s.glim{1}.w1]'; Daniel@0: s = struct(bnet.CPD{3}); % violate object privacy Daniel@0: W = reshape(s.weights, [1 2]); Daniel@0: theta0 = [s.mean; W]'; Daniel@0: Daniel@0: %figure(1) Daniel@0: %mixexp_plot(theta0, eta0, data); Daniel@0: %suptitle('before learning') Daniel@0: Daniel@0: ncases = size(data, 1); Daniel@0: cases = cell(3, ncases); Daniel@0: cases([1 3], :) = num2cell(data'); Daniel@0: Daniel@0: engine = jtree_inf_engine(bnet); Daniel@0: Daniel@0: % log lik before learning Daniel@0: ll = 0; Daniel@0: for l=1:ncases Daniel@0: ev = cases(:,l); Daniel@0: [engine, loglik] = enter_evidence(engine, ev); Daniel@0: ll = ll + loglik; Daniel@0: end Daniel@0: Daniel@0: % do learning Daniel@0: max_iter = 5; Daniel@0: [bnet2, LL2] = learn_params_em(engine, cases, max_iter); Daniel@0: Daniel@0: s = struct(bnet2.CPD{2}); Daniel@0: %eta2 = [s.glim.b1; s.glim.w1]'; Daniel@0: eta2 = [s.glim{1}.b1; s.glim{1}.w1]'; Daniel@0: s = struct(bnet2.CPD{3}); Daniel@0: W = reshape(s.weights, [1 2]); Daniel@0: theta2 = [s.mean; W]'; Daniel@0: Daniel@0: %figure(2) Daniel@0: %mixexp_plot(theta2, eta2, data); Daniel@0: %suptitle('after learning') Daniel@0: Daniel@0: fprintf('mixexp2: loglik before learning %f, after %d iters %f\n', ll, length(LL2), LL2(end)); Daniel@0: Daniel@0: Daniel@0: