# MLE for double expon. X <- c(-1.3, -0.9, -0.5, -0.1, 0.2, 0.8, 1.2) # some "data" n <- length(X) loglik <- function(theta){ - sum(abs(X-theta)) } xc <- seq(-1,1, by = 0.01) yc <- 0*xc for (i in 1:length(xc)){ yc[i] <- loglik(xc[i]) } plot(xc, yc) n <- 5000 X <- rexp(n,1) * sample(c(-1,1), n, replace= TRUE) # assume theta_0 = 0 showX <- pmax(pmin(X,5),-5) hist(showX, breaks = seq(-5,5,by = 0.2)) xc <- seq(-0.1,0.1, by = 0.001) yc <- 0*xc for (i in 1:length(xc)){ yc[i] <- loglik(xc[i]) } plot(xc, yc,xlim=c(-0.05,0.05)) # plot(xc, yc) that <- median(X) yc2 <- loglik(that) - n/2*(xc - that)^2 lines(xc, yc2, col="red" , lwd=2) # overlays plot with the Fisher infromation-based parabola ## MLE for Cauchy: problem 6.1.8 X <- c(-1.94, 0.59, -5.98, -0.08, -0.77) n <- length(X) loglikn <- function(th){ # negative log-lkhd, to use in Nelder-Mead sum( log (1+(X-th)^2) ) } xc <- seq (-2,2,0.1) yc <- 0*xc for (i in 1:length(xc)){ yc[i] <- -loglikn(xc[i]) } plot(xc,yc, type="l") optim(0,loglikn) # this gives a numerical value for MLE # ------------------------------------------------------------------- n <- 50 X <- rt(n,df = 1) # random sample from Cauchy xc <- seq (-1,1,0.01) yc <- 0*xc for (i in 1:length(xc)){ yc[i] <- -loglikn(xc[i]) } plot(xc,yc, type="l", lwd=2) # this represents the log-likhd curve when n is "large"