The Law of Large Numbers in R

Copy and paste the following code to your R Studio platform or R version:

# Testing the Law of Large Numbers, does it follow a normal distribution
# for different sample sizes?
# Basically, we need to check the if the average value of the numbers, as we
# increase the sample size, approximates the expected value. In this case, we
# assume that the standard deviation is 1 and the mean is 0, so, the number should
# fall between -1 and 1 in 68.2% of the times, since {-1s,+1s} = 68.2% in the
# normal distribution.

# Let us start with a sample size of 100 and scale up to 1.000.000, by changing
# just the value of N

N <- 100 # sample size
counter <- 0 # reset counter

for(i in rnorm(N)) { # iterate over a vector of numbers

if(i >= -1 & i <= 1) { # check where the iterated variable falls

counter <- counter + 1 # increase counter if condition is met


answer <- counter / N #calculate hit ratio –compare with 68.2%
answer # print in console


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s