xxxxxxxxxx
1
1
using Distributions, Plots, Random, LinearAlgebra,LaTeXStrings
f (generic function with 1 method)
xxxxxxxxxx
7
1
begin
2
x = range(-3,stop=3,length=200)
3
y = range(-3,stop=10,length=200)
4
N = 1000
5
Sampleₙ = 5
6
f(x,y) = exp(-((1 - x)^2 + 100(y - x^2)^2) / 20)
7
end
xxxxxxxxxx
1
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]
)
xxxxxxxxxx
1
1
q = MvNormal(zeros(2),Diagonal(ones(2))*0.1)
xxxxxxxxxx
6
1
begin
2
X = -1:0.01:2
3
Y = -1:0.01:2
4
Z = [pdf(q,[i,j]) for i in X, j in Y]
5
plot(X,Y,Z,st=:contour)
6
end
xxxxxxxxxx
4
1
begin
2
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
end
xxxxxxxxxx
1
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:N
8
curr = samples[i-1,:]
9
prop = curr + rand(q)
10
alpha = f(prop...) / f(curr...)
11
if rand(udist) < alpha
12
curr = prop
13
end
14
samples[i,:] = curr
15
end
16
plot!(samples[:,1],samples[:,2],marker=(:circle,3), label=L"sample_{%$sᵢ}",
17
legend=:bottomleft)
18
end
19
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