x<-rcauchy(100)

mu<-seq(from=-1,to=1,length=100)
sigma<-exp(seq(from=-1,to=1,length=100))

loglikcauchy<-function(mu,sigma){
sum(dcauchy(x,location=mu,scale=sigma,log=TRUE))}
loglik<-function(mu,sigma){
matrix(mapply(loglikcauchy,
mu=as.vector(outer(mu,rep(1,length(sigma)))),
sigma=as.vector(outer(rep(1,length(mu)),sigma))),
length(mu),length(sigma),dimnames=list(mu=mu,sigma=sigma))}



ll<-loglik(mu,sigma)
contour(mu,sigma,ll)
contour(mu,sigma,ll,levels=max(ll)-c(0.001,qchisq(0.95,2),qchisq(0.99,2))/2,add=TRUE)
contour(mu,sigma,2*(ll-max(ll)))
contour(mu,sigma,2*(ll-max(ll)),levels=-c(0.001,qchisq(0.95,2),qchisq(0.99,2)),add=TRUE)

library(MASS)
fitdistr(x,dcauchy,start=list(location=0,scale=1))



contour(mu,sigma,2*(ll-max(ll)))
points(-0.0002059849 ,   1.1290475937)  # fill in the values you saw in fitdist


persp(mu,sigma,(ll-max(ll)),theta=45)




x<-rcauchy(9)
x<-c(x,9)

mu<-seq(from=-10,to=10,length=100)


ll<-loglik(mu,0.1)
plot(mu,ll,type="l")

points(x,loglik(x,0.1))


x<-rcauchy(10)
ll<-loglik(mu,1)
plot(mu,ll,type="l")
points(x,loglik(x,sigma))







x<-rexp(1000)*(2*(runif(1000)<0.5)-1)

loglikdexp<-function(mu,sigma){
sum(-((abs(x-mu))/sigma)-log(sigma))}

mu<-seq(from=-0.2,to=0.2,length=100)
sigma<-exp(seq(from=-0.2,to=0.2,length=100))

loglik<-function(mu,sigma){matrix(mapply(
loglikdexp,
mu=as.vector(outer(mu,rep(1,length(sigma)))),
sigma=as.vector(outer(rep(1,length(mu)),sigma))),
length(mu),length(sigma),dimnames=list(mu=mu,sigma=sigma))}

ll<-loglik(mu,sigma)
contour(mu,sigma,2*(ll-max(ll)))
contour(mu,sigma,2*(ll-max(ll)),levels=-c(0.001,qchisq(0.95,2),qchisq(0.99,2)),add=TRUE)



ddexp<-function(x,mu,sigma){
(1/(2*sigma))*exp(-abs(x-mu)/sigma)}

fitdistr(x,ddexp,start=list(mu=0,sigma=1))
points(0.01451103  , 1.06092940 )           # replace these numbers by what you saw in fitdist


persp(mu,sigma,2*(ll-max(ll)),theta=45)




x<-rexp(10)*(2*(runif(10)<0.5)-1)

ll<-loglik(mu,1)
plot(mu,ll,type="l")
points(x,loglik(x,1))

