In this notebook we experiment with the Bayesian update of the "probability of a probability" from the number of successes in repeated independent experiments.
We start from a Beta prior, which can model many different kinds of previous knowledge about a probability. For example:
Using the tools described in this post and the "grid" approximation used by Kruschke we can model the experiment as follows:
exper = joint (coins 5) prior coins n p = replicateM n (coin p) coin p = bernoulli p 'H' 'T' -- weighted [('H',p),('T',1-p)] prior = discretize nT (0+deltaHist,1-deltaHist) (betaD 3 2)
exper
0.00 % ("HHHHH","0.01") 0.00 % ("HHHHH","0.02") 0.00 % ("HHHHH","0.03") ⋮ 0.00 % ("TTTTT","0.98") 0.00 % ("TTTTT","0.99")
We are actually interested in the total number of heads:
exper2 = (length . filter (=='H') *** id) <-- exper
exper2
0.00 % (0,"0.01") 0.00 % (0,"0.02") 0.01 % (0,"0.03") ⋮ 0.21 % (5,"0.98") 0.11 % (5,"0.99")
We can now compute the posterior distribution given the observed number of heads:
snd <-- exper2 ! (0==) . fst
0.02 % "0.01" 0.09 % "0.02" 0.19 % "0.03" ⋮ 0.00 % "0.98" 0.00 % "0.99"
We graphically show the update process for different priors and observations:
Obviously, for a larger number of coins there are too many outcomes in exper
and this exhaustive approach breaks down. Alternately, we can directly compute the distribution of the number of heads using the binomial distribution.
(In fact, this problem has a beautiful analytic solution since the Beta and binomial are conjugate distributions, but we are interested here in a general computational approach valid for other priors and observation models.)
Using this shortcut the result is exactly the same as above and we can handle bigger experiments:
You can try other parameters: