UnknDist <- function (x){
return(sin(x*pi/2)*(pi/2))
}
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)
}
hist(mySample(UnknDist, 1000), breaks = seq(0, 1, 0.01))
mySamples <- function(sampleFunction, nsamples, ...) {
s <- rep(NA, nsamples)
for (i in 1:nsamples) {
s[i] <- mean(sampleFunction(...))
}
return(s)
}
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))