You are on page 1of 6

Lab 3

Data Analysis and Programming in R 1/5


Data Analysis and Programming in R 1/5
Solve the following exercises.
1. Let fn denote the nth Fibonacci number. (a) Construct a sequence of
fn
ratios of the form fn−1 , n = 1, 2, . . . , 30. Does the sequence appear to

be converging? (b) Compute the golden ratio (1 + 5)/2. Is the
sequence converging to this ratio? Can you mathematically prove
this? (c) Determine the number of Fibonacci numbers less than
1000000.
2. A twin prime is a pair of primes (x, y), such that y = x + 2.
Construct a list of all twin primes less than 1000.
3. Mortgage interest rates can sometimes depend on whether the
Pi
mortgage term is open or closed. Use the formula R = 1−(1+i) −n to

create a function to calculate a monthly mortgage payment R where i


is an interest rate (compounded monthly), P is the original principal,
and n is the length of the term (in months). The function should take
n, P, and open as arguments. If open==TRUE, then take i = 0.005;
otherwise, take i = 0.004.

Data Analysis and Programming in R 2/5


Pick three of the following exercises and solve them.
1. Simulate 10000 values of a uniform random variable on (0, 1), U1 ,
using runif(), and simulate another set of 10000 values of a uniform
random variable on (0, 1) U2 . Assign these vectors to U1 and U2,
respectively. Since the values in U1 and U2 are approximately
independent, we can view them as independent random variables.
a Estimate E [U1 + U2 ]. Compare with the true value, and compare with
an estimate of E [U1 ] + E [U2 ].
b Estimate Var (U1 + U2 ) and Var (U1 ) + Var (U2 ). Are they equal?
Should the true values be equal?
Estimate P(U√1 + U√2 ≤ 1.5).
c

d Estimate P( U1 + U2 ≤1.5).
2. Use the round() function together with runif() to generate 1000
pseudorandom integers which take values from 1 through 10,
assigning these values to a vector called discreteunif . Use the table()
function to check whether the observed frequencies for each value are
close to what you expect. If they are not close, how should you
modify your procedure?
Data Analysis and Programming in R 3/5
3. Write an R function which simulates 500 light bulbs, each of which
has probability 0.99 of working. Using simulation, estimate the
expected value and variance of the random variable X, which is 1 if
the light bulb works and 0 if the light bulb does not work. What are
the theoretical values?
4. Simulate 10000 binomial pseudorandom numbers with parameters 20
and 0.3, assigning them to a vector called binsim. Let X be a
Binomial( 20, 0.3) random variable. Use the simulated numbers to
estimate the following.
a P(X ≤ 5).
b P(X = 5).
c E [X ].
d Var (X ).
e The 95th percentile of X. (You may use the quantile() function.)
f The 99th percentile of X.
g The 99.9999th quantile of X.
In each case, compare your estimates with the true values. What is
required to estimate extreme quantities accurately?
Data Analysis and Programming in R 4/5
5. Estimate the mean and variance of a Poisson random variable whose
mean is 7.2 by simulating 10000 Poisson pseudorandom numbers.
Compare with the theoretical values.
6. Simulate 1000 realizations of a standard normal random variable Z,
and use your simulated sample to estimate
a P(Z > 2.5);
b P(0 < Z < 1.645);
c P(1.2 < Z < 1.45);
d P(1.2 < Z < 1.3).
Compare with the theoretical values.
7. A χ2 random variable on n degrees of freedom has the same
distribution as the sum of n independent standard normal random
variables. Simulate a χ2 random variable on eight degrees of freedom,
and estimate its mean and variance. (Compare with the theoretical
values: 8, 16.)

Data Analysis and Programming in R 5/5

You might also like