
## com364a of still september pre chicago 2025
#  re celine's stk4021 exam 2018 with seven \lambda_i

# Experiment 1 2 3 4 5 6 7
# ni    4    9    12   3    5    7    8
# ybari 0.37 1.89 0.70 0.37 0.44 0.65 0.40

nn <- c(4,    9,    12,   3,    5,    7,    8)
ybari <- c(0.37, 1.89, 0.70, 0.37, 0.44, 0.65, 0.40)

mm <- length(nn) 

logL <- function(para)
{
a <- para[1]
b <- para[2]
#
check <- 1*(a > 0)*(b > 0)
#
# aux <- lgamma(a+nn)-lgamma(a) + a*log(b) - (a+nn)*log(b+nn*ybari)
# 
ifelse(check==1,
   sum(lgamma(a+nn)-lgamma(a) + a*log(b) - (a+nn)*log(b+nn*ybari)),-1066)
}

logL(c(2.2,3.3))
logL(c(2.2,-3.3))


minuslogL <- function(para)
{-logL(para)}

nils <- nlm(minuslogL,c(2,3),hessian=T)

ML <- nils$estimate
Jhat <- nils$hessian
se <- sqrt(diag(solve(Jhat)))
show <- cbind(ML,se)
round(show,4)

#ML     se
#[1,] 5.5732 4.4496
#[2,] 3.3260 3.0112

ahat <- ML[1]
bhat <- ML[2]

lambdaraw <- 1/ybari
lambdaeB <- (ahat + nn)/(bhat + nn*ybari) 

matplot(1:mm, cbind(lambdaraw,lambdaeB),type="l",lwd=3,
   lty=c(2,1),col=c(2,1))

sim <- 10^5
uu <- runif(sim)
eps1 <- rnorm(sim)
eps2 <- rnorm(sim)
ok <- 0*(1:sim)
pracc <- 0*(1:sim) 

mcmc <- 0*(1:sim) %*% t(1:2) 
# mcmc[1, ] <- c(15.33,16.32)
mcmc[1, ] <- c(ML[1],ML[2])

sigma <- 0.123

## i take a rather noninformative prior for (a,b): 

logprior <- function(para)
{
a <- para[1]
b <- para[2]
check <- 1*(a > 0)*(b > 0)
#
ifelse(check==1,
   log(dgamma(a,0.1,0.1))+log(dgamma(b,0.1,0.1)),
   -1066)
}

logprior(c(4.4,5.5))
logprior(c(4.4,-5.5))

logL(c(4.4,5.5))
logL(c(4.4,-5.5))

for (ss in 2:sim)
{
old <- mcmc[ss-1, ]
prop <- old + sigma*c(eps1[ss],eps2[ss])
aux1 <- logprior(prop) - logprior(old)
aux2 <- logL(prop) - logL(old)
pracc[ss] <- min(c(1,exp(aux1 + aux2)))
ok[ss] <- 1*(uu[ss] <= pracc[ss])
mcmc[ss, ] <- (1-ok[ss])*old + ok[ss]*prop
}

# checking mcmc a bit: 

# need some care: 
# a bit high autocorrelation when i use sigma = 0.123 above ; 

acf(mcmc[ ,1],lag=200)
acf(mcmc[ ,2],lag=200)

plot(pracc) # should allow the range [0,1] also for high ss 

# these should not be far off from 1 : 
print( mean(mcmc[ ,1]) / ML[1] )
print( mean(mcmc[ ,2]) / ML[2] ) 

# these should be stable: 
plot(mcmc[ ,1])
plot(mcmc[ ,2])

## ok, then full posteriors for \lambda_1, ..., \lambda_m

asim <- mcmc[ ,1]
bsim <- mcmc[ ,2]

# checking how the rgamma handles vectors : 
rgamma(4,c(10,20,30,40),1/c(10,20,30,40))

lambdasim <- 0*(1:sim) %*% t(1:mm)

for (ss in 1:sim)
{ lambdasim[ss, ] <- rgamma(mm,asim[ss]+nn,bsim[ss]+nn*ybari) }

# can inspect each of the seven lambda: 
hist(lambdasim[ ,1],breaks=40)

postmeans <- 0*(1:mm)
B05 <- 0*(1:mm)
B50 <- 0*(1:mm)
B95 <- 0*(1:mm)

for (j in 1:mm)
{
postmeans[j] <- mean(lambdasim[ ,j])
#
B05[j] <- quantile(lambdasim[ ,j],0.05)
B50[j] <- quantile(lambdasim[ ,j],0.50)
B95[j] <- quantile(lambdasim[ ,j],0.95)
}

keep <- cbind(lambdaraw,lambdaeB,postmeans,B05,B50,B95)

print(round(keep,3))

checking <- B50 / lambdaeB # not far from 1 


## so, for the seven experiments:
#  one at a time raw estimates; empirical Bayes; posterior means;
#  credibility intervals with 0.05, 0.50, 0.95 

#    lambdaraw lambdaeB postmeans   B05   B50   B95
#[1,]     2.703    1.992     2.156 0.994 2.012 3.801
#[2,]     0.529    0.717     0.678 0.377 0.658 1.050
#[3,]     1.429    1.499     1.489 0.919 1.457 2.166
#[4,]     2.703    1.933     2.106 0.903 1.942 3.890
#[5,]     2.273    1.913     2.011 0.995 1.905 3.385
#[6,]     1.538    1.596     1.595 0.869 1.541 2.507
#[7,]     2.500    2.080     2.187 1.217 2.101 3.463



