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

Verify the Monte Carlo sampling variability of "π".spa

 

 

p = π/4 與 所得 0.7854 比較接近,故知足 Central Limit theorem。code

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)
相關文章
相關標籤/搜索