How to find the MLE of a uniform distribution?

孤人 提交于 2020-05-16 06:32:08

问题


I am trying to find the maximum likelihood estimators a_hat and b_hat for a given uniform distribution X ~ UNIF(1,3) using R. Below is my code and its output:

##Example: Uniform Distribution
x<-runif(100,1,3)
n<-length(x)
ll<-function(a,b){

  -sum(1/(b-a)^n,log=TRUE)

}
m0<-mle2(ll,start=list(a=1,b=2))
summary(m0)

> summary(m0)

Maximum likelihood estimation

Call:
mle2(minuslogl = ll, start = list(a = 1, b = 2))

Coefficients:
Estimate Std. Error z value Pr(z)
a   1.5159         NA      NA    NA
b   1.4841         NA      NA    NA

-2 log L: -1.542595e+150 
Warning message:
In sqrt(diag(object@vcov)) : NaNs produced 

I am not able to reason why my coefficients are so off from the original values. I am pretty much sure I am using the correct likelihood function for uniform distribution, but I may be wrong for it as well syntax somewhere. I am using library(bbmle)


回答1:


Thanks everyone who helped.

  1. I contacted my professor and it turned out I can't use the "bbmle" for distributions which are not differentiable.
  2. In this case log(constant=1/b-a) is not differentiable to get a maxima.
  3. There is another R package called "ExtDist" which output MLE very well for all distributions (so far for me, including uniform) but doesn't provide standard error of them, which infact "bbmle" does

Just to help anyone who may stumble upon this post in future:

Normal, "bbmle":

#Comparison of mentioned packages
  #Example for normal distribution
  set.seed(123)
  library("bbmle")
  x<-rnorm(100,1,3) #mean=1, sd = 3
  n<-length(x)
  ll<-function(a,b){
    -sum(dnorm(x,a,b,log=TRUE)) 
  }
  m0<-mle2(ll,start=list(a=1,b=2))
  summary(m0)

Results:

Maximum likelihood estimation

 Call:
 mle2(minuslogl = ll, start = list(a = 1, b = 2))

 Coefficients:
   Estimate Std. Error z value     Pr(z)    
 a  1.27122    0.27247  4.6655 3.079e-06 ***
 b  2.72473    0.19267 14.1421 < 2.2e-16 ***
 ---
 Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

 -2 log L: 484.2609 

Normal, "ExtDist":

library("ExtDist")
  m1<-eNormal(X=x,method = "unbiased.MLE")
  m1

Results:

 Parameters for the Normal distribution. 
 (found using the  unbiased.MLE method.)

  Parameter     Type Estimate      S.E.
       mean location 1.271218 0.2738448
         sd    scale 2.738448 0.1946130

Uniform , "bbmle":

#Example for uniform distribution
  set.seed(123)
  x<-runif(100,1,3) #minimum =1, maximum = 3
  range(x)  #To know beforehand the original minimum and maximum before the      package estimates
 [1] 1.00125 2.98854
  n<-length(x)
  ll<-function(a,b){ 
    -sum(dunif(x,a,b,log=TRUE))   
  }
 m3<-mle2(ll,start=list(a=1,b=2))
 Error in optim(par = c(1, 2), fn = function (p)  : 
   initial value in 'vmmin' is not finite
  summary(m3)

Error message:

 Error in optim(par = c(1, 2), fn = function (p)  : 
 initial value in 'vmmin' is not finite

Uniform, "ExtDist":

 m4<-eUniform(X=x,method = "unbiased.MLE")
  m4

 Parameters for the Uniform distribution. 
 (found using the  numerical.MLE method.)

  Parameter     Type Estimate
          a boundary 1.001245
          b boundary 2.988544



回答2:


The negative log-likelihood is -n*log(1/(b-a))=n*log(b-a) if a<min(x) and b>max(x). If these constraints are not satisfied then the likelihood is 0.

You can specifiy the constraints with method = "L-BFGS-B":

library(bbmle)
x <- runif(100,1,3)
n <- length(x)
ll <- function(a,b){
  n*log(b-a)
}

m0 <- mle2(ll, start=list(a=0, b=4), 
           lower=c(a=-Inf, b=max(x)), upper=c(a=min(x), b=Inf), 
           method="L-BFGS-B")

You get:

Warning message:
In mle2(ll, start = list(a = 0, b = 4), lower = c(a = -Inf, b = max(x)),  :
  some parameters are on the boundary: variance-covariance calculations based on Hessian may be unreliable


> m0

Coefficients:
       a        b 
1.003692 2.956433 

Log-likelihood: -66.92 

The results are correct. The maximum likelihood estimates of a and b respectively are min(x) and max(x).

> min(x)
[1] 1.003692
> max(x)
[1] 2.956433


来源:https://stackoverflow.com/questions/47280336/how-to-find-the-mle-of-a-uniform-distribution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!