xxxxxxxxxx
3
1
begin
2
using Distributions, Gadfly, Random, LinearAlgebra,LaTeXStrings,RDatasets,Compose
3
end
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)->f(x,y),-3,3,-3,10,Stat.contour(levels=10))
xxxxxxxxxx
1
1
using DataFrames
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)
1
begin
2
p = nothing
3
D = DataFrame(x=repeat(x, inner=length(x)), y=repeat(y,outer=length(y)))
4
D.z = map((x)->f(x...),zip(D.x,D.y))
5
p = []
6
push!(p,layer(D, x=:x, y=:y,z=:z,Geom.contour(levels=5)))
7
push!(p,layer(x=[1],y=[1],shape=[Shape.star2],alpha=[1.0],
8
Geom.point,
9
Theme(point_size=3mm,default_color=color("black")),
10
order=1))
11
push!(p,layer(x=[1,2.5],y=[1,-2.5],Geom.line,
12
Theme(default_color=color("black"),line_width=0.3mm)))
13
push!(p,Guide.annotation(compose(context(), text(2.3, -2.9, "fₘₐₓ(1,1)=1"))))
14
colors = ["orange","purple","darkgreen","red","blue"]
15
for sᵢ in 1:Sampleₙ
16
samples = Array{Float64,2}(zeros(N,2))
17
samples[1,:] = [rand(Uniform(-3,3)),rand(Uniform(-3,10))]
18
udist = Uniform()
19
for i in 2:N
20
curr = samples[i-1,:]
21
prop = curr + rand(q)
22
alpha = f(prop...) / f(curr...)
23
if rand(udist) < alpha
24
curr = prop
25
end
26
samples[i,:] = curr
27
end
28
#Geom.path는 Geom.line(preserve_order=true)과 동일
29
push!(p,layer(x=samples[:,1],y=samples[:,2],Geom.path ,Geom.point,
30
Theme(point_size=0.8mm,line_width=0.05mm,
31
default_color=color(colors[sᵢ]),
32
point_shapes=[Shape.circle],
33
alphas=[0.6],highlight_width=0.1mm),order=1))
34
end
35
plot(p...,
36
Coord.cartesian(xmin=-3, xmax=3, ymin=-3, ymax=10),
37
Guide.title("Rosenbrock densityt f(x)"),
38
Guide.xticks(ticks = -3:1:3),
39
Guide.yticks(ticks = -2:2:10))
40
end