Homework 8 – Due November 16, 12 am The total points on this homework is 200. Out of these 20 points are reserved for clarity of presentation, punctuation and commenting with respect to the code. 1. Greatest common divisor of two integers The greatest common divisor (gcd) of two integers m and n can be calculated using Euclid’s Algorithm: Divide m by n. If the remainder is zero, the gcd is n. If not, divide n by the remainder. If the remainder is zero, then the previous remainder is the gcd. If not, continue dividing the remainder into previous remainder until a remainder of zero is obtained. The gcd is the value of the last nonzero remainder. Write a function gcd(m,n) using a while loop to find the gcd of two integers m and n. [20 points] 2. eQTL mapping. (The following problem was suggested by Professor Dan Nettleton.) Write a function order.matrix which takes in a matrix x and returns a matrix containing the row and column indices of the sorted values of x. Test this function on a 4 × 3 matrix of independent χ21 pseudo-random deviates. [10 points] 3. Polar representation of a number. Let x ∈ IRp . The polar representation of x = (x1 , x2 , . . . , xp ) is given by (R, θ1 , θ2 , . . . , θp−1 ), where x1 = R cos θ1 x2 = R sin θ1 cos θ2 x3 = R sin θ1 sin θ2 cos θ3 ... = ... p−2 Y = R sin θi cos θp−1 xp−1 i=1 xp = R p−1 Y sin θi , i=1 where 0 ≤ R < ∞, 0 ≤ θ1 < 2π and 0 ≤ θi < π for i = 2, 3, . . . , p − 1. (a) Write a function polaroid which takes in an arbitrary p-dimensional vector x and provides its polar representation as a vector, with the first element as R and the remainder being θ1 , θ2 , . . . , θp−1 . [20 points] (b) Write a function normalize which takes in a matrix and returns its normalized form: i.e. the matrix with rows scaled such that the sum of squares of each row is equal to 1. [10 points] (c) Obtain a 1000 × 5 matrix y of N (0, 1) pseudo-random deviates. Use apply and normalize to obtain the normalized values. Call this matrix z. We test whether the columns of z are uniform on U (−1, 1). One may test whether a sample x ∼ U (−1, 1) using ks.test(x,"punif",min=-1,max=1) where punif represents the cumulative distribution function of the uniform over range (-1,1). Summarize your results. [20 points] (d) Obtain polar representations of w using your function polaroid and test whether R2 ∼ χ25 distribution. Provide a page of histograms or boxplots of θ1 , θ2 , θ3 , θ4 . Test whether these are from the uniform distributions on their respective ranges i.e., [0, 2π) for θ1 and [0, π) for θ2 , θ3 , θ4 . [20 points] 4. Exercises 10.6, Problem 8 (page 184). [15 points] 5. Exercises 10.6, Problem 9 (page 184). [20 points] Stat 579, Fall 2011 – Maitra 2 6. The following distribution has the density function: f (x; θ) = 1 − cos (x − θ) 2π for 0 ≤ x ≤ 2π, −π < θ < π For an observed random sample x1 , x2 , . . . , xn from this distribution, the log likelihood is seen to be `(θ) = −n log 2π + n X log {1 − cos (xi − θ)} i=1 Suppose that (3.91, 4.85, 2.28, 4.06, 3.70, 4.04, 5.46, 3.53, 2.28, 1.96, 2.53, 3.88, 2.22, 3.47, 4.82, 2.46, 2.99, 2.54, 0.52, 2.50) is an observed random sample from the above distribution. (a) Plot the log likelihood `(θ) in the range −π < θ < π. [15 points] (b) Use the R function optimize() to find the maximum likelihood estimate of θ. [10 points] (c) Use function newton() in the class handout to find the maximum likelihood estimate of θ by solving `0 (θ) = 0 using the starting value of θ(0) = 0. [15 points] (d) What happens if you use θ(0) = −2.0 and −2.7, respectively, as starting values? Explain why. [5 points] Turn in the plot, any functions you write, function calls, and the results. P.S. To help you out with the necessary derivatives, I derived them below but you need to check them out! n X sin (xi − θ) ∂` =− ∂θ 1 − cos (xi − θ) i=1 n X ∂2` 1 = − 2 ∂θ 1 − cos (xi − θ) i=1