File Exchange

image thumbnail

Markov Chain Monte Carlo sampling of posterior distribution

version (4.29 KB) by Aslak Grinsted
MCMC sampling of using a cascaded metropolis


Updated 04 May 2015

View License

NOTE: I recommend using my new GWMCMC sampler which can also be downloaded from the file exchange:
Markov Chain Monte Carlo sampling of posterior distribution

A metropolis sampler
initialm: starting point fopr random walk
loglikelihood: function handle to likelihood function: logL(m)
logprior: function handle to the log model priori probability: logPapriori(m)
stepfunction: function handle with no inputs which returns a random
step in the random walk. (note stepfunction can also be a
matrix describing the size of a normally distributed
mccount: How long should the markov chain be?
skip: Thin the chain by only storing every N'th step [default=10]

EXAMPLE USAGE: fit a normal distribution to data
logmodelprior=@(m)0; %use a flat prior.
minit=[0 1];
m=mcmc(minit,loglike,logmodelprior,[.2 .5],10000);
m(1:100,:)=[]; %crop drift

--- Aslak Grinsted 2010

Cite As

Aslak Grinsted (2020). Markov Chain Monte Carlo sampling of posterior distribution (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (7)

shiva asadzadeh

hello. thanks for your submission. I need your help. I dont know initialm value for invese gamma distribution. please help me.

hina Shakir

Hello Great code...but could you guide me if i have to define two uniform priors for mu and sigma or say two parameters of a distribution how can i do it in your code?

Jisu Han

thanks so much for this amazing code!

Vipul Singhal

Awesome code. Using it now!

Aslak Grinsted

@abdul: I don't understand your comment.

abdul he

I did not get the same result


I just want to learn it!


updated link in description again

updated GWMCMC link in description

changed description

changed title

Bugfix for small values of skip

MATLAB Release Compatibility
Created with R2010a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Inspired by: Ensemble MCMC sampler

Inspired: Ensemble MCMC sampler