【问题标题】:Ehrenfest Model in R:R中的Ehrenfest模型:
【发布时间】:2014-01-19 07:00:21
【问题描述】:

有两个瓮,一个(瓮 A)是空的,另一个(瓮)装有 n 个球。继续随机抽球,根据 Ehrenfest 模型,B 缸迟早会被清空,所有 n 个球都会传给 A 缸。

函数Ehr是:

    Ehr <- function(n)
{
    A <- numeric(0)
    B <- c(1:n)
    nest <- 0
repeat {
    out <- sample(c(A,B),size=1)
    nest <- nest + 1
    ris <- intersect(out,A)
    if (length(ris)==1) {
       A <- setdiff(A,out); B <- c(B,out) }
    else {
       B <- setdiff(B,out); A <- c(A,out)
    }
    if (length(A)==n) break
}
    return(nest)
}

它返回执行的提取次数 k(在单个模拟中)以将 n 个球从瓮瓮 B 完全传递到 A。

我要计算瓮B中最初有2个球的情况。通过重复模拟1000次(即调用函数Ehr 1000次),计算k的平均值,k的中值, k的方差,k的四分位距。

有人可以帮助我吗?

这是一个例子,最初在骨灰盒 B 中有 3 个球。

set.seed(123456) for (i in 1:5) cat(Ehr(3), "") cat("\n") 13 3 11 5 7 #可能的观测值

【问题讨论】:

    标签: r simulation mean median variance


    【解决方案1】:
    dd=sapply(rep(2,10000),Ehr)
    customSummary = function(x) { paste("Mean:",mean(x),"Median:",median(x),"Variance:",var(x),"IQR:",diff(quantile(x,probs=c(0.25,0.75)))) }
    customSummary(dd)
    

    只需重复并显示您的代码

    【讨论】:

      猜你喜欢
      • 2013-04-15
      • 2018-07-10
      • 1970-01-01
      • 2018-05-06
      • 2020-11-21
      • 2015-02-16
      • 2014-11-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多