jeudi 2 janvier 2020

Metropolis-Hastings in matlab

I am trying to use the Metropolis Hastings algorithm with a random walk sampler to simulate samples from a function f=sin(7(2x^2+3y^2)))^2 in matlab, but something is wrong with my code. The proposal density is the uniform PDF on the ellipse 2s^2 + 3t^2 ≤ 1/4 and alpha=(min(1,f(y1,y2)/f(x1,x2))). Can I use the acceptance rejection method to sample from the proposal density?

accept=0;
n=0
while(n < 5000)
    accept=false
    while(accept==false)
        s=1-rand*(2);
        t=1-rand*(2);
        accept=2*s^2 + 3*t^2 <= 1/4;
    end

and then draw uniformly distributed points checking that u< alpha?

    u = rand();
    c=(u<alpha(X(1,i-1),X(2,i-1),X(1,i-1)+s,X(2,i-1)+t));
    X(1,i)=c*s+X(1,i-1);
    X(2,i)=c*t+X(2,i-1);
    n=n+1;
end



Aucun commentaire:

Enregistrer un commentaire