用约束下的两个参数求解最大似然 [英] Solve for maximum likelihood with two parameters under constraints

查看:169
本文介绍了用约束下的两个参数求解最大似然的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 R 中实现β几何概率模型(如

I'm trying to implement a beta-geometric probability model in R (as described in this whitepaper) which involves solving an equation with two unknown parameters. In the example, they use Excel to do this, starting the values off at alpha = beta = 1 and constraining them to alpha > 0.0001 < beta.

我几乎已经在 R 中实现了此功能,但是我似乎无法为我解决任何求解器.请帮忙.

I've nearly implemented this in R, but I can't seem to make any solver work for me. Please help.

请在此处提交

# probability mass function
P = function (alpha, beta, t) {  
    out = numeric(length=length(t))
    for (i in seq_along(t)) {
        if (t[i]==1) {
            out[i] = alpha / (alpha + beta)
        } else {
            out[i] = ((beta + t[i] - 2) / (alpha + beta + t[i] - 1)) * P(alpha, beta, t[i] - 1)
        }
    }
    out
}

# survival probability function
S = function(alpha, beta, t) {
    if (t==1) {
        1 - P(alpha, beta, t=t)
    } else {
        S(alpha, beta, t - 1) - P(alpha, beta, t=t)
    }
}

# log likelihood function
LL = function(alpha, beta=1, t, n) {
    sum(n * log(P(1,1,t))) + (sum(n[1:length(n)-1]) * log(S(alpha, beta, t=t[length(t)])))
}

# make some test data
n = c(239L, 2650L, 1063L, 1014L, 473L, 1304L)
t = 1:6

# log likelihood
LL(alpha=1, beta=1, n=n, t=t)

# use numerical optimization to find values of alpha and beta
optim(c(1,1), fn=LL, n=n, t=t)

require(stats4)
mle(LL, start=list(alpha=1, beta=1), t=t, n=n)

推荐答案

默认情况下,optim将最小化,但是您要最大化LL.另外,您想使用类似L-BFGS-B的方法,该方法使用绑定信息:

By default, optim will minimize, but you want to maximize LL. Also, you want to use a method like L-BFGS-B, which uses bound information:

optim(c(1, 1), function(x) -LL(x[1], x[2], n=n, t=t), method="L-BFGS-B",
      lower=c(0.0001, 0.0001))
# $par
# [1]    0.0001 9679.3562
# 
# $value
# [1] 17075.64
# 
# $counts
# function gradient 
#       87       87 
# 
# $convergence
# [1] 0
# 
# $message
# [1] "CONVERGENCE: REL_REDUCTION_OF_F <= FACTR*EPSMCH"

我们可以证明我们已经改善了对数可能性:

We can verify we've improved the log-likelihood:

LL(1, 1, n=n, t=t)
# [1] -27659.45

LL(0.0001, 9679.3562, n=n, t=t)
# [1] -17075.64

这篇关于用约束下的两个参数求解最大似然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆