Open GaryBAYLOR opened 9 years ago
##################################
## The MLE of gamma distribution
##################################
mle_gamma <- function(x, tol = 1e-4, max_iter = 500) {
n <- length(x)
mx <- mean(x)
g <- function(alpha) {
log(alpha) - log(mx) + mean(log(x)) - digamma(alpha)
}
g_prime <- function(alpha) {
1 / alpha - trigamma(alpha)
}
alpha <- 1
iter <- 1
while (iter < max_iter) {
alpha_new <- alpha - g(alpha) / g_prime(alpha)
diff <- abs(alpha_new - alpha)
if(diff < tol | iter > max_iter) break
alpha <- alpha_new
iter <- iter + 1
}
lambda <- alpha_new / mean(x)
res <- c(alpha_new, lambda)
names(res) <- c("alpha", "lambda")
print(paste("Iterations: ", iter))
res
}
> set.seed(101)
> data <- rgamma(500, shape = 4, rate = 2)
> mle.gamma(data)
$hat_alpha
[1] 3.80092760920525
$hat_lambda
[1] 1.88312007132253
> mle_gamma(data)
[1] "Iterations: 7"
alpha lambda
3.80092764816972 1.88312009062697
MLE for gamma distribution using Bisection method
This is a function that I wrote to test bisection method for getting MLE for gamma distribution. After running the function code, we can try an example in R.
We can see that the estimation is reasonable. It can be used for any other estimation when there is no closed form solution. The following is the code for the function
mle.gamma()
.