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

标签 r mean distribution integral continuous

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

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

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)结果?

有人可以帮忙吗?

最佳答案

这是一种使用集成的解决方案:

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

0.866666666666667 with absolute error < 9.6e-15

关于r - 如何根据 R 中的概率密度计算自定义连续分布的平均值(期望值),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77202266/

相关文章:

python - 向 pandas DataFrame 添加一行列的方法

python - pandas number >mean() 或 <mean() ,然后输出一个数字

mysql创建频率分布

c++ - 如何尽可能均匀地分类(或分配)结构?

r - 安装 ggbiplot 时出错 - 未找到对象 'digest_impl'

r - 具有平滑置信区间的 ggplot2 误差条

r - R 中的全局变量

r - ggplot误差线问题

roles - 在 Meanjs 中添加用户角色

iphone - 应用请求在 iPhone 浏览器中不起作用,但在 Facebook 应用程序中起作用