【问题标题】:R function for CDF for Difference of Two Uniform Distributions用于两个均匀分布差异的 CDF 的 R 函数
【发布时间】:2021-03-02 20:00:40
【问题描述】:

我很难找到符合我要求的 R 函数。假设我有两个均匀分布,A~U[alow,ahigh] 和 B~U(blow,bhigh)。

有一个随机变量 Z=A-B,这两个均匀分布之间的差异。我需要一个函数CDF函数来分配Z。

我会给这个函数一个截止值 x,它会返回 Z 低于截止值的概率。

我在 R 中,理想情况下,函数调用看起来像这样:

UniformDiffCDC(alow,ahigh,blow,bhigh,cutoff)

不幸的是,我不知道从哪里开始,或者这是否已经在 R 的某个地方实现了。救命!

【问题讨论】:

    标签: r statistics cran uniform-distribution


    【解决方案1】:

    基本思想是概率密度函数将form a trepezoid。我不知道有任何内置函数,因为它不是一个非常常见的分布,但使用一些几何你可以准确地求解这些值。

    UniformDiffCDF <- Vectorize(function(alow,ahigh,blow,bhigh,cutoff) {
      breaks <- c(alow-bhigh, ahigh-bhigh, alow-blow, ahigh-blow)
      height <- 2/sum(breaks * c(-1, -1, 1, 1))
      if (cutoff > breaks[4]) return(1) 
      prob <- 0
      if (cutoff < breaks[1]) return(prob) 
      if (cutoff < breaks[2]) {
        prob <- prob + 1/2 * (cutoff - breaks[1]) * approx(breaks[1:2], c(0, height), cutoff)$y
        return(prob)
      } else {
        prob <- prob + 1/2 * (breaks[2]-breaks[1]) * height
      }
      if (cutoff < breaks[3]) {
        prob <- prob + (cutoff-breaks[2])*height
        return(prob)
      } else {
        prob <- prob + (breaks[3]-breaks[2])*height
      }
      tri <- 1/2 * (breaks[4]-breaks[3]) * height
      prob <- prob + tri - 1/2 * (breaks[4]- cutoff) * approx(breaks[4:3], c(0,height), cutoff)$y
      return(prob)  
    }, vectorize.args="cutoff")
    

    例如

    curve(UniformDiffCDF(5,7,2,6, x), from=-2, to=6)
    

    相应的 PDF 将是

    UniformDiffPDF <- Vectorize(function(alow,ahigh,blow,bhigh,cutoff) {
      breaks <- c(alow-bhigh, ahigh-bhigh, alow-blow, ahigh-blow)
      height <- 2/sum(breaks * c(-1, -1, 1, 1))
      if (cutoff > breaks[4]) return(0)
      if (cutoff < breaks[1]) return(0) 
      if (cutoff < breaks[2]) {
        return(approx(breaks[1:2], c(0, height), cutoff)$y)
      }
      if (cutoff < breaks[3]) {
        return(height)
      }
      return(approx(breaks[4:3], c(0,height), cutoff)$y)
    }, vectorize.args="cutoff")
    

    【讨论】:

    • 这很棒。但是,我很惊讶地看到它有任何曲率,因为 PDF 是全线!我不应该因为某种原因感到惊讶吗?
    • 好吧,考虑直线 y=2x 就像它是 PDF 一样,当集成该函数以获得曲线下的面积时,您会得到 x^2+c 所以当您在直线下积分时会得到曲线线。希望这不会太令人惊讶。这只是基本的微积分。
    • 哈哈。是的,你是对的,我应该考虑到这一点。
    【解决方案2】:

    这样的?

       UniformDiffCDF <- function(alow,ahigh,blow,bhigh,cutoff,n=10000){
            a = runif(n,min=alow,max=ahigh)
            b = runif(n,min=blow,max=bhigh)
            z = (a-b)
            p = sum(z < cutoff)/n
            return(p)
    }
    

    【讨论】:

    • 鉴于这个问题有一个相对简单的精确解,似乎不需要近似值。然而,这可能是帮助验证准确解决方案的好方法。
    猜你喜欢
    • 2020-05-08
    • 1970-01-01
    • 1970-01-01
    • 2011-07-16
    • 2021-07-30
    • 1970-01-01
    • 1970-01-01
    • 2018-03-30
    • 2021-04-07
    相关资源
    最近更新 更多