source('https://myweb.uiowa.edu/pbreheny/7110/f20/notes/fun.R')
source('https://myweb.uiowa.edu/pbreheny/7110/f20/notes/radford-neal.R')

# Inconsistent MLE
set.seed(1)
x <- gen(2, 10)
x[10] <- 0.325
theta <- sort(c(seq(-1, 3, len=99), x))
l <- sapply(theta, function(a) log.lik(x, a))
plotL(theta, exp(l)); abline(v=2, col='gray', lty=2, lwd=2)
plotl(theta, l); abline(v=2, col='gray', lty=2, lwd=2)
x <- gen(2, 40)
theta <- sort(c(seq(-1, 3, len=99), x))
l <- sapply(theta, function(a) log.lik(x, a))
plotL(theta, exp(l-max(l))); abline(v=2, col='gray', lty=2, lwd=2)
plotl(theta, l); abline(v=2, col='gray', lty=2, lwd=2)

# Entropy
x <- seq(0, 1, len=101)
H <- function(p) {
  out <- -p*log(p) - (1-p)*log(1-p)
  out[p==0] <- 0
  out[p==1] <- 0
  out
}
plot(x, H(x), type='l', ylim=c(0, log(2)), bty='n', las=1, xlab=expression(pi), ylab=expression(H(pi)))

# Circle fig
theta <- seq(0, 2*pi, len=99)
plot(sin(theta), cos(theta), bty='l', las=1, type='l', axes=FALSE, ann=FALSE, col='slateblue', lwd=3)
points(1, 0, col='slateblue', pch=16, cex=2)
points(0, 1, col='slateblue', pch=16, cex=2)
points(-1, 0, col='slateblue', pch=16, cex=2)
points(0, -1, col='slateblue', pch=16, cex=2)
points(0, 0, col='slateblue', pch=16, cex=2)
text(0.15, 0.15, expression(theta^'*'))
mtext(expression(theta[1]), side=1, line=1)
mtext(expression(theta[2]), side=2, las=1, line=1)
