#-----------------------------------------------------# #--- Classical and Bayesian inference (AMS132) ---# #--- Class 12 ---# #-----------------------------------------------------# #-- Bernoulli sampling n <- 30 # distribution function of T: tvalues <- 0:n pfT <- dbinom(tvalues, n, 0.2) plot(tvalues, pfT, type="h") pfT <- dbinom(tvalues, n, 0.4) plot(tvalues, pfT, type="h") pfT <- dbinom(tvalues, n, 0.8) plot(tvalues, pfT, type="h") # P(T < 10): probability <- function(theta){ t <- 0:9 sum(choose(n, t)*theta^t*(1-theta)^(n-t)) } theta <- seq(0, 1, length=100) probs <- sapply(theta, probability) plot(theta, probs, type="l") # M.L.E: n <- 10 theta <- seq(0.1, 0.9, by=0.1) probs <- dbinom(10*theta, n, theta) plot(theta, probs, type="p", lwd=3) # Bayes estimate: theta <- seq(0.1, 0.9, by=0.1) probs <- pbinom(30*theta-7, n, theta)-pbinom(30*theta-13, n, theta) points(theta, probs, col="blue", lwd=3)