using DataFrames
using StatsBase
using StatsPlots
using Random
using Distributions
default(label=false);
Code 10.1
p = DataFrame(
:A => [0, 0, 10, 0, 0],
:B => [0, 1, 8, 1, 0],
:C => [0, 2, 6, 2, 0],
:D => [1, 2, 4, 2, 1],
:E => [2, 2, 2, 2, 2],
)
Code 10.2
p_norm = mapcols(c -> c ./ sum(c), p)
Code 10.3
ent_vals = mapcols(entropy, p_norm)
Code 10.4
ways = [1, 90, 1260, 37800, 113400]
logwayspp = log.(ways)/10
txt = text.(names(ent_vals), :bottom, :right, 11)
scatter(logwayspp, collect(ent_vals[1,:]), txt=txt, xlab="log(ways) per pebble", ylab="entropy")
Code 10.5
p = [
[1/4, 1/4, 1/4, 1/4],
[2/6, 1/6, 1/6, 2/6],
[1/6, 2/6, 2/6, 1/6],
[1/8, 4/8, 2/8, 1/8],
]
map(x -> sum(x .* [0, 1, 1, 2]), p)
Code 10.6
Could be simplified with just map(entropy, p)
# compute entropy of each distribution
map(x -> -sum(x.*log.(x)), p)
Code 10.7
p = 0.7
A = [
(1-p)^2, p*(1-p), (1-p)*p, p^2,
]
Code 10.8
-sum(A.*log.(A))
Code 10.9
function sim_p(G::Float64 = 1.4)
p = rand(Uniform(), 3)
x4 = (G * sum(p) - p[2] - p[3])/(2-G)
push!(p, x4)
p ./= sum(p)
(entropy(p), p)
end
Code 10.10
Random.seed!(1)
cnt = 10^5
H = [sim_p(1.4) for _ ∈ 1:cnt];
density(first.(H))
Code 10.11
entropies = first.(H)
distributions = last.(H);
Code 10.12
maximum(entropies)
Code 10.13
distributions[findmax(entropies)[2]]
No code here, wheee!