# include = FALSE suppresses the output of this block
library(rv2)

This vignette shows how we can see the central limit theorem emerge when we sum multiple discrete random variables together.

We'll start by creating a helper function that adds together a random variable n times, then divides by n

mean_n <- function(x, n) {
  out <- x
  for (i in seq_len(n - 1)) {
    out <- out + x
  }
  out / n
}

Let look what happens to the average from rolling a die:

dice <- rv(1:6)
plot(dice)
plot(mean_n(dice, 2))
plot(mean_n(dice, 3))
plot(mean_n(dice, 10))

Even if we have a highly non-normal distribution to start with, it still converges eventually:

skewed <- rv(c(1, 10), c(0.9, 0.1))
plot(skewed)
plot(mean_n(skewed, 10))
plot(mean_n(skewed, 50))
plot(mean_n(skewed, 100))


hadley/rv2 documentation built on May 17, 2019, 12:16 p.m.