
## com129b of august 2025
#  can i make an iteration thing to illustrate Bayes
#  for multinomial Dirichlet?
#  with more data, one learns ptrue,
#  even if prior is strong and wrong 

kk <- 25
ptrue <- (1:kk) / sum(1:kk)
well <- sin(1:kk)^2
ptrue <- well/sum(well) 
plot(1:kk,ptrue)

pprior <- (1/kk) + 0*(1:kk)

matplot(1:kk,cbind(pprior,ptrue),type="l")

# dirichlet c*pprior
# we learn more quickly from data with low c
# and more slowly with strong prior i.e. big c 

cc <- 1000 # or rather bigger for stronger initial beliefs 
sim <- 5000

new <- 0*(1:sim) %*% t(1:kk)
new[1, ] <- cc*pprior

par(bty="l")
par(cex.lab=1.4142) 

for (ss in 2:sim)
{
yy <- rmultinom(1,1,ptrue)
new[ss, ] <- new[ss-1, ] + yy
print(ss) 
print("updated guesses")
matplot(1:kk,cbind(ptrue, new[ss, ]/sum(new[ss, ])),
   type="l",lwd=3,lty=c(2,1),col=c(2,1),
   xlab="probabilities 1 to k", ylab="truth, with current estimates")
}

