Let us generate a sample from a random variable. Without any particular reason other than to illustrate my question, let us generate a sample of size 1'000'000 from a variable that follows a normal distribution with mean 30 and variance 10 —that is, $X$~$N(30, 10)$—. The way we'll be doing that is in R.
Here's the code:
set.seed(20)
x = rnorm(1000000, 30, 10)
Lets say that we want to know what's the probability of $X\leq20$. Naturally, we can compute that probability by calculating the area below the density function in the interval $]-\infty$, 20]. Again, the following chunk displays how we can do that in R. Needless to say, for this to work package pracma should have been previously installed.
arg = (20 - 30)/(sqrt(10)*sqrt(2))
library(pracma)
prob = (erf(arg) + 1)/2
So now variable prob stores the value of $P(X\leq20)$. It turns out the value is $0.0007827011$.
Now is when my question actually starts. Since we have a —rather big— sample from that random variable, I suppose that a good estimation of $P(X\leq20)$ should be obtained by counting how many observations of the sample have a value lower than or equal to 20, and then dividing that amount by the size of the sample. Let us do just that.
sum(x <= 20)/length(x)
The value that this line of code yields is $0.159008$.
Therefore, even though our sample is —I think— quite big, the relative frequency of the observations that are lower than or equal to a given value t is not a good estimator of $P(X\leq t)$.
My question is why.
rnorm(1000000, 30, sqrt(10))
. $\endgroup$