using Distributions
using Plots
pyplot()
pyplotclf() = backend() == Plots.PyPlotBackend() && PyPlot.clf()
using Random: seed!
seed!(11)
α_true = 10.0
θ_true = 0.1
dist_true = Gamma(α_true, θ_true)
n = 10
L = 30
α = range(0.01, 100, length=400)
θ = range(0.01, 1, length=401)
params = ((x, y) -> [x, y]).(α', θ)
sample = rand(dist_true, n)
f(s, t) = prod(pdf(Gamma(s, t), y) for y in sample)
z = f.(α', θ)
EAP = sum(z .* params)/sum(z)
MAP = (x -> [α[x[2]], θ[x[1]]])(findmax(z)[2])
logf(s, t) = max(-10, sum(logpdf(Gamma(s, t), y) for y in sample))
w = logf.(α', θ)
P1 = plot(size=(500, 400))
plot!(title="Posteriors of the Gamma distribution model, n = $n"; titlefontsize=10)
plot!(xlim=(0.01, 30), ylim=(0.01, 1.0), tickfontsize=7)
plot!(xlabel="\$\\alpha\$", ylabel="\$\\theta\$", guidefontsize=10)
heatmap!(α, θ, z; colorbar=false)
scatter!([MAP[1]], [MAP[2]]; label="MAP",
color=:blue, markerstrokecolor=:blue, marker=:diamond)
scatter!([EAP[1]], [EAP[2]]; label="EAP",
color=:cyan, markerstrokecolor=:cyan)
scatter!([α_true], [θ_true]; label="true",
color=:lightgreen, markerstrokecolor=:green, marker=:star, markersize=9)
P2 = plot(size=(500, 400))
plot!(title="log Posteriors of the Gamma distribution model, n = $n"; titlefontsize=10)
plot!(xlim=(0.01, 30), ylim=(0.01, 1.0), tickfontsize=7)
plot!(xlabel="\$\\alpha\$", ylabel="\$\\theta\$", guidefontsize=10)
heatmap!(α, θ, z; colorbar=false)
heatmap!(α, θ, w)
scatter!([MAP[1]], [MAP[2]]; label="MAP",
color=:blue, markerstrokecolor=:blue, marker=:diamond)
scatter!([EAP[1]], [EAP[2]]; label="EAP",
color=:cyan, markerstrokecolor=:cyan)
scatter!([α_true], [θ_true]; label="true",
color=:lightgreen, markerstrokecolor=:green, marker=:star, markersize=9)
P = plot(P1, P2; size=(880, 400))
savefig(P, "log_posterior_Gamma.png")
pyplotclf()
P
using Distributions
using Plots
pyplot()
pyplotclf() = backend() == Plots.PyPlotBackend() && PyPlot.clf()
α_true = 10.0
θ_true = 0.1
dist_true = Gamma(α_true, θ_true)
n = 10
L = 30
α = range(0.01, 100, length=400)
θ = range(0.01, 1, length=401)
params = ((x, y) -> [x, y]).(α', θ)
@time anim = @animate for k in 1:L
sample = rand(dist_true, n)
f(s, t) = prod(pdf(Gamma(s, t), y) for y in sample)
z = f.(α', θ)
EAP = sum(z .* params)/sum(z)
MAP = (x -> [α[x[2]], θ[x[1]]])(findmax(z)[2])
print("$k ")
plot(size=(420, 400))
plot!(title="Posteriors of the Gamma distribution model, n = $n"; titlefontsize=10)
plot!(xlim=(0.01, 30), ylim=(0.01, 0.3), tickfontsize=7)
plot!(xlabel="\$\\alpha\$", ylabel="\$\\theta\$", guidefontsize=10)
heatmap!(α, θ, z; colorbar=false)
scatter!([MAP[1]], [MAP[2]]; label="MAP",
color=:blue, markerstrokecolor=:blue, marker=:diamond)
scatter!([EAP[1]], [EAP[2]]; label="EAP",
color=:cyan, markerstrokecolor=:cyan)
scatter!([α_true], [θ_true]; label="true",
color=:lightgreen, markerstrokecolor=:green, marker=:star, markersize=9)
end
pyplotclf()
gif(anim, "posteriors_Gamma1.gif", fps=2.5)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 45.863888 seconds (15.59 M allocations: 1.155 GiB, 1.36% gc time)
┌ Info: Saved animation to │ fn = C:\Users\genkuroki\OneDrive\Math\Math0034\posteriors_Gamma1.gif └ @ Plots C:\Users\genkuroki\.julia\packages\Plots\O80o4\src\animation.jl:98
using Distributions
using Plots
pyplot()
pyplotclf() = backend() == Plots.PyPlotBackend() && PyPlot.clf()
α_true = 2.0
θ_true = 0.5
dist_true = Gamma(α_true, θ_true)
n = 10
L = 30
α = range(0.01, 20, length=400)
θ = range(0.01, 5, length=401)
params = ((x, y) -> [x, y]).(α', θ)
@time anim = @animate for k in 1:L
sample = rand(dist_true, n)
f(s, t) = prod(pdf(Gamma(s, t), y) for y in sample)
z = f.(α', θ)
EAP = sum(z .* params)/sum(z)
MAP = (x -> [α[x[2]], θ[x[1]]])(findmax(z)[2])
print("$k ")
plot(size=(420, 400))
plot!(title="Posteriors of the Gamma distribution model, n = $n"; titlefontsize=10)
plot!(xlim=(0.01, 6), ylim=(0.01, 1.5), tickfontsize=7)
plot!(xlabel="\$\\alpha\$", ylabel="\$\\theta\$", guidefontsize=10)
heatmap!(α, θ, z; colorbar=false)
scatter!([MAP[1]], [MAP[2]]; label="MAP",
color=:blue, markerstrokecolor=:blue, marker=:diamond)
scatter!([EAP[1]], [EAP[2]]; label="EAP",
color=:cyan, markerstrokecolor=:cyan)
scatter!([α_true], [θ_true]; label="true",
color=:lightgreen, markerstrokecolor=:green, marker=:star, markersize=9)
end
pyplotclf()
gif(anim, "posteriors_Gamma2.gif", fps=2.5)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 49.966403 seconds (15.53 M allocations: 1.150 GiB, 0.41% gc time)
┌ Info: Saved animation to │ fn = C:\Users\genkuroki\OneDrive\Math\Math0034\posteriors_Gamma2.gif └ @ Plots C:\Users\genkuroki\.julia\packages\Plots\O80o4\src\animation.jl:98