# Function based on PDF 1
pdf_function_1 <- function(){
# get random variable from 0 to 2
x <- runif(1, 0, 2)
if (x <= 1) { return(x) }
else return(2-x)
}
# Function based on PDF 2
pdf_function_2 <- function(x){
x <- runif(1, 0, 2)
if (x <= 1) { return(1-x) }
else return(x-1)
}
# Call each function 1000 times and store their output vectors f1 and f2
# create histogram for each variable
f1 <- replicate(1000, pdf_function_1())
hist(f1)
f2 <- replicate(1000, pdf_function_2())
hist(f2)
The histogram distributions are uniform. This tells us that the numbers in sample sets are picked randomly.
# This function takes in sample size n and function to process
sample_means <- function(samplesize, pdf_function){
# run function provided in parameter 1000 times with the sample size given
m <- replicate(1000, mean(replicate(samplesize, pdf_function())))
hist(m)
}
# Run samples with first function
sample_means(10,{pdf_function_1})
sample_means(20,{pdf_function_1})
# Run samples with second function
sample_means(10,{pdf_function_2})
sample_means(20,{pdf_function_2})
As noted from the histograms, the mean distribution of sample sets is nearly normal as the sample size becomes larger. The peak in the middle becomes larger. This is true for both function 1 and function 2.