using Logging
Random.seed!(4750)
sample_count = 3000
points = rand(Uniform(-1, 1), (sample_count, 2))
inside = [points[i, 1]^2 + points[i, 2]^2 < 1 for i in 1:sample_count]
running_estimate = [4 * mean(inside[1:i]) for i in 1:sample_count]
circle_angles = range(0, 2π, length=500)
batch_size = 100
# Each frame redraws everything drawn so far, 100 more samples at a time.
animation = @animate for shown_count in batch_size:batch_size:sample_count
shown_points = points[1:shown_count, :]
shown_inside = inside[1:shown_count]
p_square = plot(cos.(circle_angles), sin.(circle_angles), color=:black, linewidth=1,
xlims=(-1, 1), ylims=(-1, 1), xticks=[-1, 0, 1], yticks=[-1, 0, 1],
aspect_ratio=1, legend=false)
scatter!(p_square, shown_points[shown_inside, 1], shown_points[shown_inside, 2],
color=cb_blue, markershape=:x, markersize=3)
scatter!(p_square, shown_points[.!shown_inside, 1], shown_points[.!shown_inside, 2],
color=cb_vermillion, markershape=:x, markersize=3)
p_estimate = plot(1:shown_count, running_estimate[1:shown_count], color=:black,
linewidth=3, xlims=(1, sample_count), ylims=(2.8, 3.5), legend=false,
xlabel="Number of samples", ylabel="Estimate")
hline!(p_estimate, [π], color=cb_green, linestyle=:dash, linewidth=2)
plot(p_square, p_estimate, layout=grid(2, 1, heights=[3/4, 1/4]), size=(600, 550),
right_margin=8mm)
end
# gif() prints a "saved animation" message; this keeps it off the slide.
Logging.disable_logging(Logging.Info)
gif(animation, "figures/mc_pi.gif", fps=2)