
## com6a of february 2022,
#  simple AIC illustration for stk 4060,
#  with binomial data, two coins, pA and pB 

H <- function(u)
{
u*log(u) + (1-u)*log(1-u)
}

curve(H,0,1)

nn <- 40 
xa <- 17
xb <- 23

pahat <- xa/nn # for coin A
pbhat <- xb/nn # for coin B 
phat <- (xa + xb)/(nn + nn) # estimate for the common, if common
print(c(pahat,pbhat,phat))

# model 1, one common para: 
l1max <- nn*H(phat) + nn*H(phat)

# model 2, two para: 
l2max <- nn*H(pahat) + nn*H(pbhat)

Delta <- l2max - l1max 
# needs to be at least 1, for AIC to like 2-para more than 1-para 

aic1 <- 2*1 - 2*l1max 
aic2 <- 2*2 - 2*l2max 

keep <- rbind(
c(1, 1, l1max, aic1),
c(2, 2, l2max, aic2))

print(round(keep,4))

# with data 17, 23: 
# model dim  loglmax   aic 
#   1    1   -55.4518  112.9035
#   2    2   -54.5484  113.0967
