【问题标题】:Can we use Base R to find the 95% of the area under a curve?我们可以使用 Base R 找到曲线下面积的 95% 吗?
【发布时间】:2018-01-23 23:12:06
【问题描述】:

使用 Base R,我想知道是否可以确定曲线下 95% 的面积,表示为下面的 posterior?

更具体地说,我想从mode(绿色虚线)向尾部移动,然后在我覆盖 95% 的曲线区域时停止。所需的 x 轴值是这 95% 区域的界限,如下图所示?

     prior = function(x) dbeta(x, 15.566, 7.051) 
likelihood = function(x) dbinom(55, 100, x)
 posterior = function(x) prior(x)*likelihood(x)

mode = optimize(posterior, interval = c(0, 1), maximum = TRUE, tol = 1e-12)[[1]]

curve(posterior, n = 1e4)

P.S 换句话说,如果这样的间隔是可能的最短 95% 间隔是非常可取的。

【问题讨论】:

    标签: r function optimization distribution integrate


    【解决方案1】:

    对称分布

    尽管 OP 的示例并不完全对称,但它已经足够接近了 - 并且从那里开始很有用,因为解决方案要简单得多。

    您可以使用integrate 和optimize 的组合。我将其编写为自定义函数,但请注意,如果您在其他情况下使用此函数,您可能需要重新考虑搜索分位数的界限。

    # For a distribution with a single peak, find the symmetric!
    # interval that contains probs probability. Search over 'range'.
    f_quan <- function(fun, probs, range=c(0,1)){
    
      mode <- optimize(fun, interval = range, maximum = TRUE, tol = 1e-12)[[1]]
    
      total_area <- integrate(fun, range[1], range[2])[[1]]
    
      O <- function(d){
        parea <- integrate(fun, mode-d, mode+d)[[1]] / total_area
        (probs - parea)^2
      }
      # Bounds for searching may need some adjustment depending on the problem!
      o <- optimize(O, c(0,range[2]/2 - 1E-02))[[1]]
    
    return(c(mode-o, mode+o))
    }
    

    这样使用,

    f <- f_quan(posterior, 0.95)
    curve(posterior, n = 1e4)
    abline(v=f, col="blue", lwd=2, lty=3)
    

    给予

    非对称分布

    在非对称分布的情况下,我们必须搜索满足 P(a

    解决方案中重要的是定义domain,即我们要搜索的区域(我们不能使用-Inf, Inf,因此用户必须将其设置为合理的值)。

    # consider interval (a,b) on the x-axis
    # integrate our function, normalize to total area, to 
    # get the total probability in the interval
    prob_ab <- function(fun, a, b, domain){
      totarea <- integrate(fun, domain[1], domain[2])[[1]]
      integrate(fun, a, b)[[1]] / totarea
    }
    
    # now given a and the probability, invert to find b
    invert_prob_ab <- function(fun, a, prob, domain){
    
      O <- function(b, fun, a, prob){
        (prob_ab(fun, a, b, domain=domain) - prob)^2
      }
    
      b <- optimize(O, c(a, domain[2]), a = a, fun=fun, prob=prob)$minimum
    
    return(b)
    }
    
    # now find the shortest interval by varying a
    # Simplification: don't search past the mode, otherwise getting close
    # to the right-hand side of domain will give serious trouble!
    prob_int_shortest <- function(fun, prob, domain){
    
      mode <- optimize(fun, interval = domain, maximum = TRUE, tol = 1e-12)[[1]]
    
      # objective function to be minimized: the width of the interval
      O <- function(a, fun, prob, domain){
        b <- invert_prob_ab(fun, a, prob, domain)
    
        b - a
      }
    
      # shortest interval that meets criterium
      abest <- optimize(O, c(0,mode), fun=fun, prob=prob, domain=domain)$minimum
    
      # now return the interval
      b <- invert_prob_ab(fun, abest, prob, domain)
    
    return(c(abest,b))
    }
    

    现在像这样使用上面的代码。我使用了一个非常不对称的函数(假设 mydist 实际上是一些复杂的 pdf,而不是 dgamma)。

    mydist <- function(x)dgamma(x, shape=2)
    curve(mydist(x), from=0,  to=10)
    abline(v=prob_int_shortest(mydist, 0.9, c(0,10)), lty=3, col="blue", lwd=2)
    

    在本例中,我将域设置为 (0,10),因为显然区间必须在某处。请注意,使用 (0, 1E05) 之类的非常大的值不起作用,因为integrate 在接近零的长序列方面存在问题。同样,根据您的情况,您将不得不调整域(除非有人有更好的主意!)。

    【讨论】:

    • 边界是问题:如果您搜索整个域(在您的情况下为 0-1),我们会遇到问题,因为该函数未在 0 或 1 处定义(但它在附近)。在函数中 d 是与模式的距离,这是为了找到 d ,其中 (mode-d) 到 (mode+d) 的积分等于请求的概率(在您的情况下为 0.95)。因此这只适用于对称函数,否则你必须优化两个参数。
    • 我认为如果它是不对称的,那么这个问题就不会有单一的解决方案!您可以找到集成到某个概率的 pdf 的许多间隔。或者,您实际上是在寻找 2.5% 和 97.% 的分位数(它们之间会整合到 95%)?如果是这样,那是可以做到的。
    • 可以做到——但请注意,这与您提出的原始问题完全不同!我不愿编辑我的帖子,因为这本身就很有用。我可能会添加另一个答案。
    • Remko,我同意 OP,您编辑的答案将非常有用,甚至会获得更多支持。
    【解决方案2】:

    这是使用Trapezoidal rule 的解决方案。您会注意到@Remko 提供的解决方案要优越得多,但该解决方案有望增加一些教学价值,因为它阐明了如何将复杂的问题简化为简单的几何、算术和基本的编程结构,例如for loops。

    findXVals <- function(lim, p) {
        ## (1/p) is the precision
    
        ## area of a trapezoid
        trapez <- function(h1, h2, w) {(h1 + h2) * w / 2}
    
        yVals <- posterior((1:(p - 1))/p)
        m <- which.max(yVals)
        nZ <- which(yVals > 1/p)
    
        b <- m + 1
        e <- m - 1
        a <- f <- m
    
        area <- 0
        myRng <- 1:(length(nZ)-1)
        totArea <- sum(trapez(yVals[nZ[myRng]], yVals[nZ[myRng+1]], 1/p))
        targetArea <- totArea * lim
    
        while (area < targetArea) {
            area <- area + trapez(yVals[a], yVals[b], 1/p) + trapez(yVals[e], yVals[f], 1/p)
            a <- b
            b <- b + 1
            f <- e
            e <- e - 1
        }
    
        c((a - 1)/p, (f + 1)/p)
    }
    
    findXVals(.95, 10^5)
    [1] 0.66375 0.48975
    

    【讨论】:

      猜你喜欢
      • 2017-09-03
      • 1970-01-01
      • 2019-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-29
      • 1970-01-01
      相关资源
      最近更新 更多