Math 267 - Spring 2015 Homework 2 Note: Some parts of problem 2 will require you to submit R-code. Please submit your R-code in the form of a R-Markdown file on Canvas and hand in a printout of the cor- responding html file in class. For the parts of this assignment that do not require you to produce R-code, you may write down your answers in any way you like (handwritten, typed up in any program of your choice, or typed up in Markdown). Please arrange writ- ten and/or printed problem parts in order (i.e., first 1 (a), then 1 (b), then 2 (a) etc.) and staple them together. Don’t forget to put your name clearly visible somewhere on the first page.
\(1.\) Let the random variable X have a distribution with density
\[f(x) = \left\{ \begin{array}{ll} \frac{1}{2}x & \mbox{if } 0 < x < 1 \\ \frac{1}{2} & \mbox{if } 1 \le x \le \frac{5}{2} \end{array} \right.\]
Generate a random variable from f(x), using (In both cases, use \(u = 0.3\) as a realization of a \(U[0,1]\) random variable. Use \(y = 1.8\) as a realization of a random variable with distribution \(g\).)
\((a)\) the inverse-transform method
For interval \(0<x<1\), \[\begin{equation}\int_0^x \frac{1}{2}x\space dx =\left.\frac{1}{4}x^2\right|_0^x =\boxed{\frac{1}{4}x^2} \end{equation}\]
For interval \(1\le x \le \frac{5}{2}\), \[\begin{equation} \int_0^1 \frac{1}{2}x\space dx + \int_1^x \frac{1}{2}dx =\boxed{\frac{1}{2}x - \frac{1}{4}} \end{equation}\]
Combine together, \[F(x) = \left\{ \begin{array}{ll} \frac{1}{4}x^2 & \mbox{if } 0 < x < 1 \\ \frac{1}{2}x - \frac{1}{4} & \mbox{if } 1 \le x \le \frac{5}{2} \end{array} \right.\]
When \(0<x<1\), \[\begin{equation} \begin{split} F(x) & = \frac{1}{4}x^2 = u \\ & = x^2 = 4u \\ F^{-1}(u) & = 2u^{\frac{1}{2}} \end{split} \end{equation}\]
When \(1\le x \le \frac{5}{2}\), \[\begin{equation} \begin{split} F(x) & = \frac{1}{2}x - \frac{1}{4} = u \\ F^{-1}(u) & = 2u+\frac{1}{2} \end{split} \end{equation}\]
Combine together, \[F^{-1}(u) = \left\{ \begin{array}{ll} 2u^{\frac{1}{2}} & \mbox{if } 0 < u < \frac{1}{4} \\ 2u+\frac{1}{2} & \mbox{if } \frac{1}{4} \le u \le 1 \end{array} \right.\]
FinverseU <- function(u){
if (u < 1/4 & u >0) {
sqrt(4*u)
} else if (u > 1/4 & u < 1){
2*u+1/2
} else {0}
}
FinverseU(0.3)
## [1] 1.1
Hence, when \(u=0.3\), the random number is \(1.1\).
\((b)\) the acceptance-rejection method with proposal density \[g(x)= \frac{8}{25}x, 0≤x≤\frac{5}{2}\]
Based on the \(pdf\) of \(f(x)\), the smallest constant \(M\) that satisfies \(\frac{f(x)}{g(x)} \le M\) is \(\frac{25}{16}\).
Since \(y=1.8\) is in the \(1\le x \le\frac{5}{2}\) range, so we only consider the \(pdf\) \(f(x)=\frac{1}{2}\)
\[\begin{equation} \begin{split} \frac{f(y)}{fMg(y)} & = \frac{\frac{1}{2}}{M\frac{8}{25}y}\\ & = \frac{1}{1.8}\\ & \approx 0.56 \end{split} \end{equation}\] Hence, \(u=0.3 \le \frac{f(y)}{Mg(x)}\), return \(x=1.8\)
\(2.\) Consider a random variable with the following triangular distribution. That is let the density be
\[f(x) = \left\{ \begin{array}{lll} 1+x & \mbox{if } -1 \le x \le 0 \\ 1-x & \mbox{if } 0 < x \le 1 \\ 0 & \mbox{else} \end{array} \right.\]
\((a)\) Write an R function \(\mathtt{dtriang()}\) to compute the density of this triangular distribution at one or more values of \(x\).
To calculate the density of this triangular distribution:
dtriang <- function(x){
ifelse((x<=0&x>=-1), 1+x, ifelse((x<=1&x>0), 1-x, 0)) # setting up the condition #
}
# Let test it #
set.seed(10)
x <- runif(10^4, -2, 2)
dtriang(x)[1:20]
## [1] 0.97008719 0.22707402 0.70763067 0.22759168 0.00000000 0.00000000
## [7] 0.09812209 0.08922026 0.53668277 0.71868610 0.39337733 0.72904899
## [13] 0.00000000 0.61629878 0.43219990 0.71523767 0.00000000 0.05671067
## [19] 0.59516292 0.00000000
\((b)\) Derive the formula for the cumulative distribution function \(F(x)\).
For \(-1 \le x \le 0\), \[\begin{equation} \begin{split} F(x) & = \int_{-1}^x 1+x \space dx \\ & = \frac{1}{2}(1+x)^2 \end{split} \end{equation}\]
For \(0 < x \le 1\), \[\begin{equation} \begin{split} F(x) & = \int_{-1}^0 1+x \space dx + \int_{0}^x 1-x \space dx \\ & = \frac{1}{2} - \frac{1}{2}x^2 + x \end{split} \end{equation}\]
Combine together, \[F(x) = \left\{ \begin{array}{lll} \frac{1}{2}(1+x)^2 & \mbox{if } -1 \le x \le 0 \\ \frac{1}{2} - \frac{1}{2}x^2 + x & \mbox{if } 0 < x \le 1 \\ 0 & \mbox{else} \end{array} \right.\]
\((c)\) Write an R function \(\mathtt{ptriang()}\) which takes one or more values of the random variable and computes the probability for a Triangular distribution to be less than or equal to the specified value(s).
ptriang <- function(x){
ifelse((x>=-1&x<=0), 0.5*(1+x)^2, ifelse((x>0&x<=1), 0.5-0.5*x^2+x, 0) )
}
#Let test it#
set.seed(10)
x <- runif(10)
ptriang(x)
## [1] 0.8787111 0.7597150 0.8357826 0.9529068 0.5815119 0.7000258 0.7368470
## [8] 0.7352300 0.9262064 0.8373627
\((d)\) Derive the formula for the inverse of the CDF, \(F^{−1}(x)\).
\[F^{-1}(u) = \left\{ \begin{array}{ll} \sqrt{2u}-1 & \mbox{if } 0 \le u \le \frac{1}{2} \\ 1-\sqrt{2(1-u)} & \mbox{if } \frac{1}{2} < u \le 1 \end{array} \right.\]
\((e)\) Use the inverse CDF to define a function \(\mathtt{dtriang()}\) that produces \(\mathit{IID}\) samples from a triangular distribution.
rtriang <- function(u){
ifelse ((u>=0&u<=1/2), sqrt(2*u)-1, ifelse((u>1/2&u<1), 1-sqrt(2-2*u), 0) )
}
# to generate a large number of IID #
set.seed(10)
u <- runif(10^5)
rtriang(u)[1:20]
## [1] 0.007506376 -0.216713965 -0.075978716 0.216548765 -0.587359796
## [6] -0.328529053 -0.259013464 -0.262022946 0.123449155 -0.072992421
## [11] 0.165321220 0.070201906 -0.523535978 0.101028705 -0.153773110
## [16] -0.073922878 -0.677809615 -0.273119449 -0.106925836 0.427521430
\((f)\) Produce graphs of the density and CDF using the functions you wrote. Use your \(\mathtt{rtriang()}\) function to draw a large \(\mathit{IID}\) sample and produce a graph to verify that your function is working as intended.
Produce graphs of the density and CDF using the functions I wrote:
set.seed(10)
a <- seq(-1, 1, 0.1) # just generate a large sample of random numbers from the range of [-1,1] #
plot(a, dtriang(a), type="l")
plot(a, ptriang(a), type="l")
The density and \(CDF\) indicate that my \(\mathtt{dtriang}\) and \(\mathtt{ptriang}\) functions are working.
Produce graph to see if my \(\mathtt{rtriang()}\) is working as intended.
set.seed(10)
u <- runif(10^5)
hist(rtriang(u))
Based on the graph shown above, when we generate random numbers form \(\mathit{U}\sim U[0,1]\) and plug into our inverse function, we indeed can have random numbers with triangular distribution.