#Exercise 2.11

#a
dens <- function (y, th){ 
  dens0 <- NULL
  for (i in 1:length(th))
    dens0 <- c(dens0, prod (dcauchy (y, th[i], 1))) 
  dens0
}
y <- c(43,44,45,46.5,47.5,1060)
step <- .1
theta <- seq(40, 50,by=step)
dens.unnorm <- dens(y,theta)
dens.norm <- dens.unnorm/(step*sum(dens.unnorm)) 
plot (theta, dens.norm, ylim=c(0,1.1*max(dens.norm)),
      type="l", xlab="theta", ylab="normalized density", xaxs="i", yaxs="i", cex=2)

#b
thetas <- sample (theta, 10000, step*dens.norm, replace=TRUE)
hist (thetas, xlab="theta", yaxt="n", cex=2)

#c
y6 <- rcauchy (length(thetas), thetas, 1) 
hist(y6, xlab="new observation", yaxt="n",
                                                nclass=100, cex=2)
