基本思想是概率密度函数将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")