-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathex_rosenbrockbanana.m
65 lines (51 loc) · 1.89 KB
/
ex_rosenbrockbanana.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
%% The MCMC hammer
%
% GWMCMC is an implementation of the Goodman and Weare 2010 Affine
% invariant ensemble Markov Chain Monte Carlo (MCMC) sampler. MCMC sampling
% enables bayesian inference. The problem with many traditional MCMC samplers
% is that they can have slow convergence for badly scaled problems, and that
% it is difficult to optimize the random walk for high-dimensional problems.
% This is where the GW-algorithm really excels as it is affine invariant. It
% can achieve much better convergence on badly scaled problems. It is much
% simpler to get to work straight out of the box, and for that reason it
% truly deserves to be called the MCMC hammer.
%
% See also:
% <http://astrobites.org/2012/02/20/code-you-can-use-the-mcmc-hammer/>
%
%% Rosenbrock: A badly scaled example
%
% A classical difficult low dimensional problem is the rosenbrock density.
% It is defined by the following log-probability function:
%
logPfun=@(m) -(100*(m(2,:)-m(1,:).^2).^2 +(1-m(1,:)).^2)/20;
%lets visualize it:
close all
[X,Y]=meshgrid(-4:.01:6,-1:.02:34);
Z=logPfun([X(:) Y(:)]'); Z=reshape(Z,size(X));
contour(X,Y,exp(Z))
colormap(parula)
title('The Rosenbrock banana')
xlim([-4 6])
ylim([-1 34])
%% Apply the MCMC hammer:
%
% Now we apply the Goodman & Weare MCMC sampler and plot the results on top
%
M=2; %number of model parameters
Nwalkers=40; %number of walkers/chains.
minit=randn(M,Nwalkers);
tic
models=gwmcmc(minit, logPfun,100000,'StepSize',30,'burnin',.2);
toc
%flatten the chain: analyze all the chains as one
models=models(:,:);
%plot the results
hold on
plot(models(1,:),models(2,:),'k.')
legend('Rosenbrock','GWMCMC samples','location','northwest')
%% References:
% * Goodman & Weare (2010), Ensemble Samplers With Affine Invariance, Comm. App. Math. Comp. Sci., Vol. 5, No. 1, 65�80
% * Foreman-Mackey, Hogg, Lang, Goodman (2013), emcee: The MCMC Hammer, arXiv:1202.3665
%
% -Aslak Grinsted 2015