微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我将如何重复这个模拟 1000 次以获得我可以从中取平均值的列表输出?

如何解决我将如何重复这个模拟 1000 次以获得我可以从中取平均值的列表输出?

我正在尝试模拟以下情况的 1000 次试验以获得平均成本输出。除了下面包含的内容之外,如果 totalTime >= 6,则每个模拟都需要停止。我在 for 循环和模拟方面遇到了很多困难,因此如果有人能给我任何创建此模拟的提示,我将不胜感激。谢谢!

cost <- 0
devices <- 0
totalTime <- 0
unitCost <- 100

failureTime <- rgamma(1,shape=2,scale=.5)
if (failureTime>1) {
  cost <- cost + unitCost
  totalTime <- totalTime + failureTime
} else {
  cost <- cost
  totalTime <- totalTime + failureTime
} 

解决方法

您可以像下面那样使用 replicate

replicate(1000,{
    cost <- 0
    totalTime <- 0
    unitCost <- 100
    while (totalTime < 6) {
        failureTime <- rgamma(1,shape = 2,scale = .5)
        cost <- cost + unitCost * (failureTime > 1)
        totalTime <- totalTime + failureTime
    }
    cost
})

请注意,您需要一个 while 循环来判断 totalTime 是否到达 6,并且一旦 cost 到达 totalTime(或 while) {1}} 循环终止。


如果你想要data.frame输出,你可以试试下面的代码

out <- do.call(
    rbind,replicate(1000,{
            cost <- 0
            totalTime <- 0
            unitCost <- 100
            while (totalTime < 6) {
                failureTime <- rgamma(1,scale = .5)
                cost <- cost + unitCost * (failureTime > 1)
                totalTime <- totalTime + failureTime
            }
            data.frame(cost = cost,totalTime = totalTime)
        },simplify = FALSE
    )
)

给出

> head(out)
  cost totalTime
1  200  6.117942
2  200  6.010357
3  400  6.255933
4  400  6.082638
5  300  7.170587
6  200  7.046343
,

我认为首先进行一次完整的模拟工作很重要。要做到这一点并尊重您的“如果 totalTime >= 6 则停止”,您需要一个类似 while 的循环。

我建议编写一个函数来很好地完成这件事并返回您想要的两个变量(成本和总时间)。一旦你有了它,复制它就很简单了。

simfunc <- function(shape = 2,scale = 0.5,unitCost = 100) {
  cost <- 0
  totalTime <- 0
  while (totalTime < 6) {
    failureTime <- rgamma(1,shape = shape,scale = scale)
    if (failureTime > 1) {
      cost <- cost + unitCost
    }
    totalTime <- totalTime + failureTime
  }
  c(cost = cost,totalTime = totalTime)
}

set.seed(42)
simfunc()
#      cost totalTime 
#    200.00      6.86 
simfunc()
#      cost totalTime 
#    400.00      6.79 
simfunc()
#      cost totalTime 
#    300.00      6.95 

从这里,我们可以轻松复制它:

set.seed(42)
replicate(10,simfunc())
#             [,1]   [,2]   [,3]   [,4]   [,5]   [,6]   [,7] [,8]   [,9]  [,10]
# cost      200.00 400.00 300.00 300.00 200.00 300.00 300.00  200 400.00 300.00
# totalTime   6.86   6.79   6.95   6.62   6.34   6.81   7.14    6   6.73   6.75

(总而言之,将其存储在一个变量中并聚合您需要的行。如果您t将其转置为柱状,并且可能转换为 data.frame,您可能会发现更容易可视化,但这只是为了您的审美利益,无论如何统计数据都是一样的。)

此外,我推断您可能需要 totalTime,因为这样的模拟通常不仅想知道系统生命周期的成本,还想知道失败之间的平均时间以及对此的一些分析.在这种情况下,您似乎已经弄清楚了(基于您的 scale=shape= 参数),但无论如何它可能很有趣。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?