\[ \begin{equation} f(x) = x, 0\leq x \leq 1 \\ f(x) = 2 - x, 1\lt x \leq 2 \end{equation} \]
That is, when your function is called, it will return a random variable between 0 and 2 that is distributed according to the above PDF. Please note that this is not the same as writing a function and sampling uniformly from it. In the online session this week, I’ll cover Sampling techniques. You will find it useful when you do the assignment for this week. In addition, as usual, there are one-liners in R that will give you samples from a function. We’ll cover both of these approaches in the online session.
pdf1 <- function(x){
if(x>=0 && x<=2){
if(x<=1){
return(x)
}else{
return(2-x)
}
}
}
\[ \begin{equation} f(x) = 1-x, 0\leq x \leq 1 \\ f(x) = x-1, 1\lt x \leq 2 \end{equation} \]
pdf2 <- function(x){
if(x>=0 && x<=2){
if(x<=1){
return(1-x)
}else{
return(x-1)
}
}
}
#This will give us a random sample of values within our range
samp <- runif(1000,0,2)
samp[1:10]
## [1] 1.8917914 1.7198534 0.8054960 0.4864305 0.8108921 0.5591463 1.9412815
## [8] 0.8468654 1.4005029 1.3590370
#sapply function inputs the sample valus into our PDFs
p1 <- sapply(samp, pdf1)
p1[1:10]
## [1] 0.10820859 0.28014658 0.80549601 0.48643052 0.81089210 0.55914625
## [7] 0.05871845 0.84686538 0.59949713 0.64096302
p2 <- sapply(samp, pdf2)
#I would have ended up with this had I not seen the lecture.
hist(p1)
I was lost on what to do next but found the following link which showed the use of the “prob” argument:
samp1 <- sample(samp, 1000, replace = TRUE, prob = p1)
hist(samp1, breaks=25)
samp2 <- sample(samp, 1000, replace = TRUE, prob = p2)
hist(samp2, breaks=25)
meanSamp <- function(set, n, pdf){
#take a large data set, the sample size you will take from the data set and the PDF
means <- c() #empty vector
for (i in 1:1000){
p <- sapply(set, pdf)
samp <- sample(set, n, replace = TRUE, prob = p)
#store the means in the empty vector
means <- c(means, mean(samp))
}
return (hist(means, breaks=25))
}
meanSamp(samp, 20, pdf1)
meanSamp(samp, 10, pdf1)
meanSamp(samp, 20, pdf2)
meanSamp(samp, 10, pdf2)
Even with a sample size of 10 the central limit theorem holds true