% regression demo, Barnett 3/6/06, requires stats toolbox % create `fake' data, using noisy linear model... n = 10; x = rand(1,n); ao = 1.5; bo = 0.7; so = 0.3; y = ao*x + bo + normrnd(0, so, size(x)); figure; plot(x, y, '+', 'markersize', 10); xlabel x; ylabel y; title 'data' % likelihood plot for linear model Y = aX + b a = -1:0.1:3; b = -1:0.05:2; [aa,bb] = meshgrid(a, b); L = ones(size(aa)); s = so; % assume noise sigma known for i=1:n %L = L .* exp(-(y(i) - aa*x(i) - bb).^2/(2*s^2)) / s; % normal noise model L = L .* exp(-abs(y(i) - aa*x(i) - bb)/s) / s; % or... exp noise model end figure; contourf(a, b, L); xlabel a; ylabel b; title('L(a,b), noise \sigma known'); where = find(L==max(L(:))); % locate the max in 2-param space aml = aa(where); bml = bb(where); figure; plot(x, y, '+', 'markersize', 10); xlabel x; ylabel y; title 'data' xs = [0 1]; hold on; plot(xs, aml*xs + bml, '-'); % MCMC for a,b,s opt.nits = 2000; % how many iterations. opt.e = 0.5; opt.hess = eye(3); opt.verbosity = 1; X_in = ones(3, 1); % normal noise model, or... [X_chain, P_chain, info] = mcmc_run('nlp_linear_gauss', X_in, opt, x, y); % exp noise model... %[X_chain, P_chain, info] = mcmc_run('nlp_linear_exp', X_in, opt, x, y); figure; plot(X_chain', '-'); % show the Markov chain. % samples in parameter space (marginals onto 2 out of 3 dimensions) ns = 400:opt.nits; figure; plot(X_chain(1,ns), X_chain(2,ns), '.'); xlabel a; ylabel b; figure; plot(X_chain(2,ns), X_chain(3,ns), '.'); xlabel b; ylabel('\sigma'); % samples of model fits (straight lines)... ns = 400:50:opt.nits; nss = numel(ns); as = X_chain(1,ns)';bs = X_chain(2,ns)'; figure; plot(repmat(xs, [nss 1])', repmat(as, [1 2])' + kron(bs, xs)', '-'); hold on; plot(x, y, '+', 'markersize', 10); xlabel x; ylabel y; % make one point an outlier, to test robustness (normal noise fails, exp not) y(find(x==min(x))) = 5.0; figure; plot(x, y, '+', 'markersize', 10); xlabel x; ylabel y; title 'data'