Statistical Computing
Homework 1: RNG and Writing R functions Due Date: March 10th, 2004
1. Read p37 to 41 in R-intro.pdf about random number generator and various plots on examining the distribution of a set of data.
2. Read p45 to 48 in R-intro.pdf about “Grouping, loops and conditional execution”
and writing your own function.
3. Write a R program (or other programming language) to implement a uniform random number generator using a multiplicative congruential method with xi+1 = 17xi mod m and m = 213 − 1. Generate 500 numbers for the starting point x0 = 100. (help For the sequence ui = xi/m:
(a) Plot a histogram to display the results. (help(hist))
(b) Calculate the coefficient correlation of the pairs of successive number ui and ui+1. (help(cor)).
(c) Plot the pairs (ui, ui+1) on a 2D plot. (help(plot))
4. Write a program to find the period of a random number generator for a given seed. Use this program to find the period of the sequence generated by xi+1= 7xi
mod 13 and x0 = 19. Find the period if a is changed to 3.
5. Read p69-70 and 78-79 in R-intro.pdf about how to add a graph in terms of
“Low-level plotting commands” to existing plot which is produced by “High- level plotting commands.”
6. Write a program to sample k values from the probability mass function pi = i/55, i = 1, 2, . . . , 10. Plot the histogram of generated values for k = 50, 500 and 5000.
(One plot! Not three plots!)
7. Let a discrete random variable X has a probability mass function pj = P (X = j).
Define a new function
λn = P (X = n | X > n − 1) = pn 1 −Pn−1j=1pj.
The quantities λn, n ≥ 1 are called discrete hazard rates since if we think of X as the lifetime of some item then λn represents the probability that an item has reached the age n will die before n + 1. If we are given the mass function p, we can simulate X. Write a program to simulate X when only λns are given.
For the mass function pj = (0.9)j, first compute the hazard function λn for n = 1, 2, . . . , 100. Then, use these λn’s in your program to simulate 500 values of X. Plot a histogram of these simulated values.
8. Derive and implement a method to generate samples of a Weibull random vari- able whose probability distribution function is given by
F (x) = 1 − exp(−αxβ), 0 < x < ∞.
Run your program to simulate 1000 values of Weibull random variable with α = 1 and β = 0.5.
9. Write a program that uses the rejection method to sample a random variable having the distribution function:
F (x) =
Z ∞ 0
xyexp(−y)dy, 0 ≤ x ≤ 1.
10. Suppose it is easy to generate a random variable from any of the distributions Fi, i = 1, 2, . . . , k. How can we generate a random variable from the distribution:
F (x) =
k
Y
i=1
Fi(x).
Hint: If Xis are random variables with distributions Fis, respectively, then what random variable X has the distribution F ?
2