如何根据 R 中的概率密度计算自定义连续分布的平均值(期望值)

问题描述 投票:0回答:1

概率密度和“手动”平均值的计算如下:

我将概率密度函数编码为:

myfunc <- function(x){
  ifelse(x >= 0 & x < 0.5, 1,
         ifelse (x >= 0.5 & x < 1, 0.2,
                 ifelse(x >= 1 & x < 2, 0.8*(x-1), 0)))
}

我知道 EV 是加权积分,但我正在努力编写计算代码。

另外:从长远来看,我如何模拟这个(0.867)结果?

有人可以帮忙吗?

r mean distribution integral continuous
1个回答
1
投票

这是一种解决方案,

integrate

integrate(function(x) x*myfunc(x),lower=0,upper=2)

0.866666666666667 with absolute error < 9.6e-15
© www.soinside.com 2019 - 2024. All rights reserved.