Random Walk MH

We can write the following Julia code which use uniform distribution U[δ,δ]{\mathcal U}[-\delta,\delta] as gg.

# random walk Metropolis-Hastings
function rmh(T, delta, f::Function, initval = 5)
    x = ones(T+1)
    x[1] = initval
    for t = 1:T
        # generate Yt
        epsi = rand() * 2 * delta - delta
        Yt = epsi + x[t]
        # accept or not
        u = rand()
        r = f(Yt)/f(x[t])
        if r >= 1
            x[t+1] = Yt
        else
            if u <= r
                x[t+1] = Yt
            else
                x[t+1] = x[t]
            end
        end
    end
    return(x)
end

Then apply this algorithm to the normal distribution:

We will get the table of mean and variance as showed in Table 6.3.2 of Robert and Casella (2013)

and the curve of each case:

Last updated