Central Limit Theorem

Unknow Distribution

UnknDist <- function (x){
    return(sin(x*pi/2)*(pi/2))
}

Sampling from Unknown Distribution

mySample <- function(ncdf, nsamples) {
    x <- runif(nsamples);
    
    f = function(x, u) {
        # From non-cumulative density function to cumulative density function
        int = integrate(ncdf, 0, x)
        return(int$value - u)
    }
    
    z <- rep(NA, nsamples);
    
    for (i in 1:nsamples) {
        
        z[i] <- uniroot(f, c(0,1), tol = 1e-4, u = x[i])$root;
        
    }
    
    return(z)
}

Histogram of Sample

hist(mySample(UnknDist, 1000), breaks = seq(0, 1, 0.01))

Sampling the Averages of Several Samples of Unknown Distribution

mySamples <- function(sampleFunction, nsamples, ...) {
    s <- rep(NA, nsamples)
    for (i in 1:nsamples) {
        s[i] <- mean(sampleFunction(...))
    }
    return(s)
}

Results for Different Sample Sizes

Collect a sample of size 1, compute (then plot) mean, do this a thousand times.

hist(mySamples(mySample, 1000, UnknDist, 1), breaks = seq(0, 1, 0.01))

Collect a sample of size 2, compute (then plot) mean, do this a thousand times.

hist(mySamples(mySample, 1000, UnknDist, 2), breaks = seq(0, 1, 0.01))

Collect a sample of size 5, compute (then plot) mean, do this a thousand times.

hist(mySamples(mySample, 1000, UnknDist, 5), breaks = seq(0, 1, 0.01))

Collect a sample of size 10, compute (then plot) mean, do this a thousand times.

hist(mySamples(mySample, 1000, UnknDist, 10), breaks = seq(0, 1, 0.01))

Collect a sample of size 100, compute (then plot) mean, do this a thousand times.

hist(mySamples(mySample, 1000, UnknDist, 100), breaks = seq(0, 1, 0.01))

LS0tCnRpdGxlOiAiUiBOb3RlYm9vayIKb3V0cHV0OiBodG1sX25vdGVib29rCi0tLQojIENlbnRyYWwgTGltaXQgVGhlb3JlbQoKIyMgVW5rbm93IERpc3RyaWJ1dGlvbgpgYGB7cn0KVW5rbkRpc3QgPC0gZnVuY3Rpb24gKHgpewogICAgcmV0dXJuKHNpbih4KnBpLzIpKihwaS8yKSkKfQpgYGAKCiMjIFNhbXBsaW5nIGZyb20gVW5r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