Question 1:

First write a function that will produce a sample of random variable that is dis- tributed as follows:

\(f(x)\quad =\quad x,\quad 0\quad \le \quad x\quad \le \quad 1\)

\(f(x)\quad =\quad 2\quad -\quad x,\quad 1<\quad x\quad \le \quad 2\)

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.

x <- function(x)(x)
integrate(x, 0, 1)
## 0.5 with absolute error < 5.6e-15
x2 <- function(x)(2 - x)
integrate(x2, 1.5, 2)
## 0.125 with absolute error < 1.4e-15

Now, write a function that will produce a sample of random variable that is dis- tributed as follows:

\(f(x)\quad =\quad 1- x,\quad 0\quad \le \quad x\quad \le \quad 1\)

\(f(x)\quad =\quad x - 1,\quad 1<\quad x\quad \le \quad 2\)

#x3 <- function(x)(1-x)
x3 <- seq(0,1, by = 0.01) 
x3
##   [1] 0.00 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.10 0.11 0.12 0.13
##  [15] 0.14 0.15 0.16 0.17 0.18 0.19 0.20 0.21 0.22 0.23 0.24 0.25 0.26 0.27
##  [29] 0.28 0.29 0.30 0.31 0.32 0.33 0.34 0.35 0.36 0.37 0.38 0.39 0.40 0.41
##  [43] 0.42 0.43 0.44 0.45 0.46 0.47 0.48 0.49 0.50 0.51 0.52 0.53 0.54 0.55
##  [57] 0.56 0.57 0.58 0.59 0.60 0.61 0.62 0.63 0.64 0.65 0.66 0.67 0.68 0.69
##  [71] 0.70 0.71 0.72 0.73 0.74 0.75 0.76 0.77 0.78 0.79 0.80 0.81 0.82 0.83
##  [85] 0.84 0.85 0.86 0.87 0.88 0.89 0.90 0.91 0.92 0.93 0.94 0.95 0.96 0.97
##  [99] 0.98 0.99 1.00
plot(1-x3, x3)

x4 <- seq(1,2, by = 0.01) 
x4
##   [1] 1.00 1.01 1.02 1.03 1.04 1.05 1.06 1.07 1.08 1.09 1.10 1.11 1.12 1.13
##  [15] 1.14 1.15 1.16 1.17 1.18 1.19 1.20 1.21 1.22 1.23 1.24 1.25 1.26 1.27
##  [29] 1.28 1.29 1.30 1.31 1.32 1.33 1.34 1.35 1.36 1.37 1.38 1.39 1.40 1.41
##  [43] 1.42 1.43 1.44 1.45 1.46 1.47 1.48 1.49 1.50 1.51 1.52 1.53 1.54 1.55
##  [57] 1.56 1.57 1.58 1.59 1.60 1.61 1.62 1.63 1.64 1.65 1.66 1.67 1.68 1.69
##  [71] 1.70 1.71 1.72 1.73 1.74 1.75 1.76 1.77 1.78 1.79 1.80 1.81 1.82 1.83
##  [85] 1.84 1.85 1.86 1.87 1.88 1.89 1.90 1.91 1.92 1.93 1.94 1.95 1.96 1.97
##  [99] 1.98 1.99 2.00
plot(x4 - 1, x4)

Question 3:

Draw 1000 samples (call your function 1000 times each) from each of the above two distributions and plot the resulting histograms. You should have one histogram for each PDF. See that it matches your understanding of these PDFs.

suppressWarnings(library(mc2d))
## Loading required package: mvtnorm
## 
## Attaching package: 'mc2d'
## The following objects are masked from 'package:base':
## 
##     pmax, pmin
par(mfrow=c(2,2))
a = rtriang(1000, min = 0, mode = 1, max = 2)
hist(a)


par(mfrow=c(1, 1))

b <- rtriang(500, min=0, mode=0,max= 1)
c <- rtriang(500, min=1, mode=2, max=2)
d <- c(b,c)
hist(d)

Question 4:

Now, write a program that will take a sample set size n as a parameter and the PDF as the second parameter, and perform 1000 iterations where it samples from the PDF, each time taking n samples and computes the mean of these n samples.It then plots a histogram of these 1000 means that it computes.

funct = function(x, y){
  f = rep(0, 1000)
  for (j in 1:1000){
    funct2 = sample(y, x, replace = T)
    f[j] = mean(funct2)
  }
   means = sum(f)/1000
   hist(f)
   return(means)
}
funct(1, d)

## [1] 1.001329
funct(3, d)

## [1] 0.9869555
funct(4, d)

## [1] 0.9795932
funct(5, d)

## [1] 1.002512
funct(10, d)

## [1] 1.010838
funct(20, d)

## [1] 0.9970571
funct(200, d)

## [1] 0.9960997
funct(500, d)

## [1] 0.9967695
funct(1000, d)

## [1] 0.996656