xxxxxxxxxx1
1
using Distributions, Plots, Random, LinearAlgebra,LaTeXStringsf (generic function with 1 method)xxxxxxxxxx7
1
begin2
x = range(-3,stop=3,length=200)3
y = range(-3,stop=10,length=200)4
N = 10005
Sampleₙ = 56
f(x,y) = exp(-((1 - x)^2 + 100(y - x^2)^2) / 20)7
endxxxxxxxxxx1
1
plot(x,y,(x,y)->f(x,y),st=:contour,label=L"f(x,y)",legend=:bottomleft)DiagNormal(
dim: 2
μ: [0.0, 0.0]
Σ: [0.1 0.0; 0.0 0.1]
)
xxxxxxxxxx1
1
q = MvNormal(zeros(2),Diagonal(ones(2))*0.1)xxxxxxxxxx6
1
begin2
X = -1:0.01:23
Y = -1:0.01:24
Z = [pdf(q,[i,j]) for i in X, j in Y] 5
plot(X,Y,Z,st=:contour)6
endxxxxxxxxxx4
1
begin2
Z₂ = [pdf(q,[i,j]) for i in X, j in Y] 3
plot(X,Y,Z₂,st=:surface,camera=(80,45),color=:amp)4
endxxxxxxxxxx1
1
plot(x,y,(x,y)->f(x,y),st=:surface,camera=(30,80),alpha=0.1,color=:heat)1
begin 2
plot(x,y,(x,y)->f(x,y),st=:contour)3
for sᵢ in 1:Sampleₙ4
samples = Array{Float64,2}(zeros(N,2))5
samples[1,:] = [rand(Uniform(-3,3)),rand(Uniform(-3,10))]6
udist = Uniform()7
for i in 2:N8
curr = samples[i-1,:]9
prop = curr + rand(q)10
alpha = f(prop...) / f(curr...) 11
if rand(udist) < alpha12
curr = prop13
end14
samples[i,:] = curr15
end 16
plot!(samples[:,1],samples[:,2],marker=(:circle,3), label=L"sample_{%$sᵢ}",17
legend=:bottomleft)18
end19
plot!([1.0],[1.0],st=:scatter,marker=(:star5,15),color=:yellow,label=L"(1.0,1.0)")20
#plot!([1,2],[1,-2],linewidth=2,label="")21
quiver!([1],[1],quiver=([2-1],[-2-1]),color=:red)22
annotate!(3, -2.5, text(L"f_{max}=1\;at\;(1,1)", :red, :right, 9))23
end