r - R中两个随机变量的乘积,使用discreteRV包
问题描述
我正在尝试计算两个随机变量 X 和 Y 的乘积,但 X * Y 给我的结果与jointRV 相同。
XandY <- jointRV(outcomes = list(c(0, 9), c(-3, 1)), probs = c(t(outer(c(1/2, 1/2), c(1/7, 6/7)))))
X <- marginal(XandY, 1)
Y <- marginal(XandY, 2)
> X * Y
Random variable with 4 outcomes
Outcomes 0,-3 0,1 9,-3 9,1
Probs 1/14 3/7 1/14 3/7
> XandY
Random variable with 4 outcomes
Outcomes 0,-3 0,1 9,-3 9,1
Probs 1/14 3/7 1/14 3/7
解决方案
正如Stéphane Laurent所说,可能无法f(X,Y)
直接使用randomRV
包进行计算。然而,我们可以计算每一个可能X*Y
以及它们的概率。然后可以将这些存储为随机变量。此解决方案可能不是最佳的,但似乎有效。
product.matrix <- t(outer(c(0, 9),c(-3, 1),"*")) ## find all possible products
probability.matrix <- t(outer(c(1/2, 1/2), c(1/7, 6/7)))
unique.products <- unique(as.vector(product.matrix)) ## find the unique products
probability.vector <- rep(0, length(unique.products))
for(i in 1:length(probability.vector)){
z <- unique.products[i]
indices <- which(as.vector(product.matrix) == z) ## find which elements of product.matrix match up to z
probability.vector[i] <- sum(as.vector(probability.matrix)[indices]) ## sum their probabilities
}
XtimesY <- RV(outcomes = unique.products, probs = probability.vector) ## store as RV
然后你可以看到
> XtimesY
Random variable with 3 outcomes
Outcomes -27 0 9
Probs 1/14 1/2 3/7
给出 的分布X*Y
。
推荐阅读
- flutter - 提供者问题:找不到正确的提供者
- ethereum - 如何通过代币 A 或 B 计算链上 Uniswap 池数量?
- docker - 从经典的 nexus3 安装迁移到使用 Docker 的容器化安装
- amazon-web-services - 从 sqs 到 lambda 的 cloudwatch 规则
- python - 使用 pandas.read_sql_query 将参数传递给存储过程
- flutter - 我的 ListView.Builder 没有构建任何东西并显示空白屏幕
- html - Css 边框顶部和 bot 线性渐变
- c# - ImageView.SetFrame(int, int, int, int)' 由于其保护级别而无法访问
- google-analytics - 非电子商务网站的 GA4 总收入
- javascript - 将变量值从一个 React 函数传递给另一个(在两个不同的文件之间)