function compute_posterior2(p, the_sample, the_prior)
W = sum(the_sample .== "W")
L = sum(the_sample .== "L")
W_prior = sum(the_prior .== "W")
L_prior = sum(the_prior .== "L")
pdf(Beta(W + W_prior + 1, L + L_prior + 1), p)
end
# Sample data
the_sample = ["W","L","W","W","W","L","W","L","W"]
# Generate a vector of x values
x = 0:0.01:1
# Single Plot (bottom right of fig 2.7)
post = compute_posterior2(x, the_sample, String[])
plot(
x, y=post, linewidth=2,
xlabel="", ylabel="", yticks = [],
legend=:outertop, legendfontsize=8,
legendforegroundcolor=nothing,
legendbackgroundcolor=nothing,
)
## NOT IN BOOK -- Reproduce Fig 2.7
# Create a 3x3 array to store individual plots
n = length(the_sample)
plots = Plots.Plot[plot() for i in 1:n]
# iterate through the sample one at a time, keeping a vector prior obs
for i in 1:n
the_prior = the_sample[1:(i-1)]
y_post = compute_posterior2(x, the_sample[1:i], the_prior)
y_prior = compute_posterior2(x, the_sample[1:i-1], the_prior[1:i-2])
label = join(the_sample[1:i], " ")
col = the_sample[i] == "W" ? :steelblue2 : :tomato
p = plot(
x, y_post, color=col, linewidth=2,
label=label, xlabel="", ylabel="", yticks = [],
legend=:outertop, legendfontsize=8,
legendforegroundcolor=nothing,
legendbackgroundcolor=nothing,
)
plots[i] = plot!(
p, x, y_prior, color = :black, linestyle = :dash, linewidth=1.5)
end
# Splat (...) `plots` into a single 3x3 grid plot (fig2_7)
fig2_7 = plot(plots..., layout=(3,3), size = (700,700));
display(fig2_7)