annotate toolboxes/FullBNT-1.0.7/KPMstats/cwr_demo.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 % Compare my code with
Daniel@0 2 % http://www.media.mit.edu/physics/publications/books/nmm/files/index.html
Daniel@0 3 %
Daniel@0 4 % cwm.m
Daniel@0 5 % (c) Neil Gershenfeld 9/1/97
Daniel@0 6 % 1D Cluster-Weighted Modeling example
Daniel@0 7 %
Daniel@0 8 clear all
Daniel@0 9 figure;
Daniel@0 10 seed = 0;
Daniel@0 11 rand('state', seed);
Daniel@0 12 randn('state', seed);
Daniel@0 13 x = (-10:10)';
Daniel@0 14 y = (x > 0);
Daniel@0 15 npts = length(x);
Daniel@0 16 plot(x,y,'+')
Daniel@0 17 xlabel('x')
Daniel@0 18 ylabel('y')
Daniel@0 19 nclusters = 4;
Daniel@0 20 nplot = 100;
Daniel@0 21 xplot = 24*(1:nplot)'/nplot - 12;
Daniel@0 22
Daniel@0 23 mux = 20*rand(1,nclusters) - 10;
Daniel@0 24 muy = zeros(1,nclusters);
Daniel@0 25 varx = ones(1,nclusters);
Daniel@0 26 vary = ones(1,nclusters);
Daniel@0 27 pc = 1/nclusters * ones(1,nclusters);
Daniel@0 28 niterations = 5;
Daniel@0 29 eps = 0.01;
Daniel@0 30
Daniel@0 31
Daniel@0 32 I = repmat(eye(1,1), [1 1 nclusters]);
Daniel@0 33 O = repmat(zeros(1,1), [1 1 nclusters]);
Daniel@0 34 X = x(:)';
Daniel@0 35 Y = y(:)';
Daniel@0 36
Daniel@0 37 cwr = cwr_em(X, Y, nclusters, 'muX', mux, 'muY', muy, 'SigmaX', I, ...
Daniel@0 38 'cov_typeX', 'spherical', 'SigmaY', I, 'cov_typeY', 'spherical', ...
Daniel@0 39 'priorC', pc, 'weightsY', O, 'create_init_params', 0, ...
Daniel@0 40 'clamp_weights', 1, 'max_iter', niterations, ...
Daniel@0 41 'cov_priorX', eps*ones(1,1,nclusters), ...
Daniel@0 42 'cov_priorY', eps*ones(1,1,nclusters));
Daniel@0 43
Daniel@0 44
Daniel@0 45 % Gershenfeld's EM code
Daniel@0 46 for step = 1:niterations
Daniel@0 47 pplot = exp(-(kron(xplot,ones(1,nclusters)) ...
Daniel@0 48 - kron(ones(nplot,1),mux)).^2 ...
Daniel@0 49 ./ (2*kron(ones(nplot,1),varx))) ...
Daniel@0 50 ./ sqrt(2*pi*kron(ones(nplot,1),varx)) ...
Daniel@0 51 .* kron(ones(nplot,1),pc);
Daniel@0 52 plot(xplot,pplot,'k');
Daniel@0 53 pause(0);
Daniel@0 54 px = exp(-(kron(x,ones(1,nclusters)) ...
Daniel@0 55 - kron(ones(npts,1),mux)).^2 ...
Daniel@0 56 ./ (2*kron(ones(npts,1),varx))) ...
Daniel@0 57 ./ sqrt(2*pi*kron(ones(npts,1),varx));
Daniel@0 58 py = exp(-(kron(y,ones(1,nclusters)) ...
Daniel@0 59 - kron(ones(npts,1),muy)).^2 ...
Daniel@0 60 ./ (2*kron(ones(npts,1),vary))) ...
Daniel@0 61 ./ sqrt(2*pi*kron(ones(npts,1),vary));
Daniel@0 62 p = px .* py .* kron(ones(npts,1),pc);
Daniel@0 63 pp = p ./ kron(sum(p,2),ones(1,nclusters));
Daniel@0 64 pc = sum(pp)/npts;
Daniel@0 65 yfit = sum(kron(ones(npts,1),muy) .* p,2) ...
Daniel@0 66 ./ sum(p,2);
Daniel@0 67 mux = sum(kron(x,ones(1,nclusters)) .* pp) ...
Daniel@0 68 ./ (npts*pc);
Daniel@0 69 varx = eps + sum((kron(x,ones(1,nclusters)) ...
Daniel@0 70 - kron(ones(npts,1),mux)).^2 .* pp) ...
Daniel@0 71 ./ (npts*pc);
Daniel@0 72 muy = sum(kron(y,ones(1,nclusters)) .* pp) ...
Daniel@0 73 ./ (npts*pc);
Daniel@0 74 vary = eps + sum((kron(y,ones(1,nclusters)) ...
Daniel@0 75 - kron(ones(npts,1),muy)).^2 .* pp) ...
Daniel@0 76 ./ (npts*pc);
Daniel@0 77 end
Daniel@0 78
Daniel@0 79
Daniel@0 80 % Check equal
Daniel@0 81 cwr_pc = cwr.priorC';
Daniel@0 82 assert(approxeq(cwr_pc, pc))
Daniel@0 83 cwr_mux = cwr.muX;
Daniel@0 84 assert(approxeq(mux, cwr_mux))
Daniel@0 85 cwr_SigmaX = squeeze(cwr.SigmaX)';
Daniel@0 86 assert(approxeq(varx, cwr_SigmaX))
Daniel@0 87 cwr_muy = cwr.muY;
Daniel@0 88 assert(approxeq(muy, cwr_muy))
Daniel@0 89 cwr_SigmaY = squeeze(cwr.SigmaY)';
Daniel@0 90 assert(approxeq(vary, cwr_SigmaY))
Daniel@0 91
Daniel@0 92
Daniel@0 93 % Prediction
Daniel@0 94
Daniel@0 95 X = xplot(:)';
Daniel@0 96 [cwr_mu, Sigma, post] = cwr_predict(cwr, X);
Daniel@0 97 cwr_ystd = squeeze(Sigma)';
Daniel@0 98
Daniel@0 99 % pplot(t,c)
Daniel@0 100 pplot = exp(-(kron(xplot,ones(1,nclusters)) ...
Daniel@0 101 - kron(ones(nplot,1),mux)).^2 ...
Daniel@0 102 ./ (2*kron(ones(nplot,1),varx))) ...
Daniel@0 103 ./ sqrt(2*pi*kron(ones(nplot,1),varx)) ...
Daniel@0 104 .* kron(ones(nplot,1),pc);
Daniel@0 105 yplot = sum(kron(ones(nplot,1),muy) .* pplot,2) ...
Daniel@0 106 ./ sum(pplot,2);
Daniel@0 107 ystdplot = sum(kron(ones(nplot,1),(muy.^2+vary)) .* pplot,2) ...
Daniel@0 108 ./ sum(pplot,2) - yplot.^2;
Daniel@0 109
Daniel@0 110
Daniel@0 111 % Check equal
Daniel@0 112 assert(approxeq(yplot(:)', cwr_mu(:)'))
Daniel@0 113 assert(approxeq(ystdplot, cwr_ystd))
Daniel@0 114 assert(approxeq(pplot ./ repmat(sum(pplot,2), 1, nclusters),post') )
Daniel@0 115
Daniel@0 116 plot(xplot,yplot,'k');
Daniel@0 117 hold on
Daniel@0 118 plot(xplot,yplot+ystdplot,'k--');
Daniel@0 119 plot(xplot,yplot-ystdplot,'k--');
Daniel@0 120 plot(x,y,'k+');
Daniel@0 121 axis([-12 12 -1 1.1]);
Daniel@0 122 plot(xplot,.8*pplot/max(max(pplot))-1,'k')
Daniel@0 123 hold off
Daniel@0 124