Verify the Monte Carlo sampling variability of "π".

 

[Bayes] Point --> Hist: Estimate "π" by R

 

p = π/4 与 所得 0.7854 比较接近,故满足 Central Limit theorem。

simulation = function(sampleSize){
  c = rep(0,sampleSize)
  countIn = 0
  for(i in 1:sampleSize){
    x = runif(1,-1,1)
    y = runif(1,-1,1)
    if(sqrt(x*x + y*y) <= 1){
      countIn = countIn + 1
    }
    piHat = countIn / i
    c[i] = piHat
  }
  return(c)
}

pointEstimate = function(testCount){
  cc = rep(0,testCount)
  for(i in 1:testCount){
    sampleSize = 1000
    res = simulation(sampleSize)
    cc[i] = res[1000] 
  }
  return(cc)
}

testCount = 1000
res = pointEstimate(testCount)
/* 获得很多要构成长条的点 */
hist(res, freq
= F, breaks = testCount) mean(res)

 

相关文章:

  • 2021-08-14
  • 2021-09-13
  • 2022-01-20
  • 2021-09-21
  • 2022-12-23
  • 2021-09-18
  • 2021-08-18
  • 2022-01-02
猜你喜欢
  • 2022-01-13
  • 2021-08-25
  • 2021-06-29
  • 2022-12-23
  • 2021-10-28
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案