Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
215 views
in Technique[技术] by (71.8m points)

Weird R behavior when simulating LLN for standard deviations

I am simulating the law of large numbers and how it applies to standard deviation, as well. I wrote this code that works well but there is something that I am having a hard time understanding.

n <- 1000
dice.sd <- numeric(n)

sd(1:6) #1.870829

for (i in 1:n) {
  dice.sd[i] <- sd(sample(1:6, i, replace = TRUE))
}
plot(dice.sd)
abline(h=1.870829)
abline(h=1.7078)

As you can see, I made this loop to simulate LLN for standard deviations. According to the documentation, the sd() function uses n-1 for calculating the sample standard deviation, which should be about 1.87 for a die. However, when I run my simulation and graph the results, the standard deviation is converging to about 1.7078, which is the population standard deviation (using just n). Why is this the case? My loop originally was using the sample standard deviation, so why is it converging to the population standard deviation?

question from:https://stackoverflow.com/questions/65876392/weird-r-behavior-when-simulating-lln-for-standard-deviations

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
dice.sd[i] <- sd(sample(1:6, i, replace = TRUE))

This line creates a sample of size i, not a sample of size 6, which I assume is what you want. Your i goes up to 1000, which is large enough for the sample SD to be close to the population SD.

What you want is

dice.sd[i] <- sd(sample(1:6, 6, replace = TRUE))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...