我试图设置伽玛密度曲线下的面积的近似值,首先绘制密度并添加蒙特卡洛算法的点(蓝色或红色,取决于它们在伽马曲线上的位置).最后求出伽玛曲线下面积的值.但结果表明,我的近似值与理论值相差很远,我不知道如何调整它.

知道这一点:P(a≤X≤b)=P(X≤b)−P(X≤a),其中X是服从伽玛分布的随机变量.

还有这个: P(a≤X≤b)≈(b−a)×(红色点数/总点数)

以下是我的代码,我希望我们能找到解决方案:)


set.seed(1)

M=10^3
alpha <- 2 # Shape parameter of the Gamma distribution
beta <- 3  # Rate parameter of the Gamma distribution
a <- 1     # Lower bound of the interval
b <- 3     # Upper bound of the interval

prob_a <- pgamma(a, shape = alpha, rate = beta)
prob_b <- pgamma(b, shape = alpha, rate = beta)

probability <- prob_b - prob_a

x <- seq(0, 6, length.out=1000)

density <- dgamma(x, shape = alpha, rate = beta)

plot(x, density, type = "l", col = "black", lwd = 2,
     xlab = "x", ylab = "Density",
     main = "Density of Gamma Distribution")

abline(v = a, col = "black", lwd = 1)
abline(v = b, col = "black", lwd = 1)

U <- runif(M, min = a, max = b)

gamma_density <- dgamma(U, shape = alpha, rate = beta)

V <- runif(M, min = 0, max = max(density))

points(U[V <= gamma_density], V[V <= gamma_density], col = "red", pch = 20, cex = 0.5)

points(U[V > gamma_density], V[V > gamma_density], col = "blue", pch = 20, cex = 0.5)

points_under_curve <- sum(U >= a & U <= b)

area_approximation <- (b - a) * points_under_curve / M

print(area_approximation)

theoretical_probability <- pgamma(b, shape = alpha, rate = beta) - pgamma(a, shape = alpha, rate = beta)

print(theoretical_probability)

Density gamma distribution

Results approximation

我试着增加模拟的数量,寻找其他方法来计算理论值...

推荐答案

您的代码有两个问题:

  1. 曲线下的点数不能计算为sum(U >= a & U <= b)(因为按照您生成U的方式,该条件等于真).相反,它必须计算为sum(V <= gamma_density)(即y坐标位于曲线下方的点数);
  2. 面积近似必须考虑用于生成随机点的矩形的高度(因为这并不总是1,您应该应用类似于area-of-the-rectangle乘以proportion of points under the curve的公式).因此,您应该计算(b - a) * max(density) * points_under_curve / M.

Reprex:

set.seed(1)

M=10^5
alpha <- 2 # Shape parameter of the Gamma distribution
beta <- 3  # Rate parameter of the Gamma distribution
a <- 1     # Lower bound of the interval
b <- 3     # Upper bound of the interval

prob_a <- pgamma(a, shape = alpha, rate = beta)
prob_b <- pgamma(b, shape = alpha, rate = beta)

probability <- prob_b - prob_a

x <- seq(0, 6, length.out=1000)

density <- dgamma(x, shape = alpha, rate = beta)

plot(x, density, type = "l", col = "black", lwd = 2,
     xlab = "x", ylab = "Density",
     main = "Density of Gamma Distribution")

abline(v = a, col = "black", lwd = 1)
abline(v = b, col = "black", lwd = 1)

U <- runif(M, min = a, max = b)

gamma_density <- dgamma(U, shape = alpha, rate = beta)

V <- runif(M, min = 0, max = max(density))

points(U[V <= gamma_density], V[V <= gamma_density], col = "red", pch = ".", cex = 0.5)

points(U[V > gamma_density], V[V > gamma_density], col = "blue", pch = ".", cex = 0.5)


points_under_curve <- sum(V <= gamma_density)

area_approximation <- (b - a) * max(density) * points_under_curve / M

print(area_approximation)
#> [1] 0.1975212

theoretical_probability <- pgamma(b, shape = alpha, rate = beta) - pgamma(a, shape = alpha, rate = beta)

print(theoretical_probability)
#> [1] 0.1979142

创建于2024-02-18年第reprex v2.0.2

R相关问答推荐

过滤矩阵以获得R中的唯一组合

使用R的序列覆盖

如何 bootstrap glm回归、估计95%置信区间并绘制它?

在使用ggroove后,将图例合并在gplot中

如何从当前行上方找到符合特定条件的最接近值?

迭代通过1个长度的字符串长字符R

如何在modelsummary中重命名统计数据?

以更少间隔的较小表中的聚合离散频率表

如何对2个列表元素的所有组合进行操作?

在ggplot2的框图中绘制所有级别的系数

如何通过匹配R中所有可能的组合来从宽到长旋转多个列?

Data.table';S GForce-将多个函数应用于多列(带可选参数)

如何在R中通过多个变量创建交叉表?

R:用GGPLATE,如何在两个独立的变量中制作不同形状的散点图?

`夹心::vcovCL`不等于`AER::tobit`标准错误

根据列表中项目的名称合并数据框和列表

如何将一个方阵分解成没有循环的立方体

将统计检验添加到GGPUBR中的盒图,在R

在不对R中的变量分组的情况下取两行的平均值

用LOOCV进行K近邻问题