R final exam (B), Université Paris-Dauphine, Jan. 18, 2014

Dfbaebe5e96e827d993483f842c74fa2?s=47 Xi'an
January 21, 2014

R final exam (B), Université Paris-Dauphine, Jan. 18, 2014

Dfbaebe5e96e827d993483f842c74fa2?s=128

Xi'an

January 21, 2014
Tweet

Transcript

  1. Universit´ e Paris-Dauphine Ann´ ee 2013-2014 D´ epartement de Math´

    ematique Examen NOISE, sujet B Pr´ eliminaires Cet examen est ` a r´ ealiser sur ordinateur en utilisant le langage R et ` a rendre simultan´ ement sur papier pour les r´ eponses d´ etaill´ ees et sur fichier informatique Examen pour les fonctions R utilis´ ees. Les fichiers informa- tiques seront ` a sauvegarder suivant la proc´ edure ci-dessous et seront pris en compte pour la note finale. Toute duplication de fichiers R fera l’objet d’une poursuite disciplinaire. L’absence de document enregistr´ e donnera lieu ` a une note nulle sans possibilit´ e de contestation. 1. Reporter votre login sur votre feuille pour associer votre nom au compte anonyme. 2. Enregistrez r´ eguli` erement vos fichiers sur l’ordinateur, avec un nom de fichier sans accents ni espace, ni caract` eres sp´ eciaux. 3. Sauvegardez votre script pour chaque exercice dans un nouveau fi- chier au nom caract´ eristique comme exercice.2.R. Utilisez le dossier Examen pour stocker ces fichiers. 4. V´ erifiez que vos fichiers ont bien ´ et´ e enregistr´ es en les rouvrant avant de vous d´ econnecter. N’h´ esitez pas ` a rouvrir votre fichier en dehors de R (par exemple avec la commande cat) afin de v´ erifier qu’il contient bien tout votre code R. 5. En cas de probl` eme ou d’inqui´ etude, contacter un enseignant sans vous d´ econnecter ni sortir de R. Il nous est sinon impossible de r´ ecup´ erer les fichiers de sauvegarde automatique. Aucun document n’est autoris´ e, seuls les documents disponibles sur le compte anonyme sont permis. L’utilisation de tout service de messagerie ou de mail est interdite et, en cas d’utilisation av´ er´ ee, se verra sanctionn´ ee. Les probl` emes sont ind´ ependants, peuvent ˆ etre trait´ es dans n’importe quel ordre. R´ esoudre deux et uniquement deux exercices au choix. Exercise 1 1. If X, Y are normal N(0, 1) random variables, show (or use the fact) that the pro- bability that X2 + Y 2 ≤ 1 is equal to P(X2 + Y 2 ≤ 1) = 1 − 1/ √ e 2. Write an R function called hitin(n) that (a) generates n random realisations (x, y) from the standard bivariate normal distribution N2(0, I) and (b) returns the propor- tion of points within the unit circle {x2 +y2 ≤ 1}. Use hitin to derive an approxima- tion of the constant e by this experiment and plot the evolution of the approximation when the number n of dots grows from 10 to 106. Provide a confidence interval that has a 95% probability to contain 1/ √ e.
  2. 3. We now consider Buffon’s needle experiment, where metal needles

    of length are thrown upon a wooden floor made of planks of width t ≥ and where the number of times the needles cross a plank separation (or line) is counted. The resulting proportion leads to an approximation of π since the probability of crossing a line is 2 /tπ. (This model assumes all planks are identical, parallel, and horizontal.) (a) Implement the following R code : BuffonsPi <- function(N=1000,D=50,L=.25){ # warning: L is the half-length numbhits <- function(A,B){ sum(abs(trunc(A[,2])-trunc(B[,2]))>0)} O <- runif(N,min=0,max=pi/2) #angle U <- L+runif(N)*(D*sqrt(1+apply(cbind(sin(O)^2, cos(O)^2),1,min))-2*L) C <- cbind(U*cos(O),U*sin(O)) # centre A <- C+L*cbind(cos(O),sin(O)) # endpoint A B <- C-L*cbind(cos(O),sin(O)) # endpoint B return(2*2*L*N/numbhits(A,B)) } and explain the meaning of the outcome. (b) Explain why (or accept the fact that) this R code is only approximately solving Buffon’s original problem and identify from the R code what the corresponding values of and t are. (c) Study how the corresponding approximation of π converges with N, including a confidence interval in your evaluation. (d) Repeat the study with D=100, then with D=10. Exercise 2 Given the integral I = 3 2 e−x+5 (1 + x2) dx 1. Give the exact value of I using the R integrate function (up to a small precision). 2. Propose a Monte-Carlo method using an n-sample from an exponential distribution with λ = 1. Give the corresponding 95% confidence interval for i with n = 104. Ex- plain or show why using the Cauchy distribution would result in a worse estimator. 3. Propose an alternative method based on an n-sample from a uniform distribution. Give the corresponding 95% confidence interval for i with n equal to the previous exercise. 4. Establish which method is better by graphical (plotting the evolution of both esti- mators as n increase) and numerical means.
  3. Exercise 3 We wish to estimate the integral I =

    ∞ 0 x exp(−x2) sin2(x) dx 1. Find the constant k such that f : x → kx exp(−x2)I{x>0} is a probability density function. 2. Use the generic inversion method to create an R function which outputs n realiza- tions from the density f. 3. Create an Accept-Reject algorithm to simulate from the density g such that g(x) ∝ x exp(−x2) sin2(x)I{x>0} . 4. What is the estimated acceptation probability of your algorithm ? Deduce an esti- mate of I. Exercise 4 Let us consider a random variable X, whose probability density is given by : f(x) = xe−x2/21 R+ (x) 1. Show that Y = X2 is distributed according to the exponential distribution with rate parameter 1/2, then write a function rsqrtexp(n) that generates n realizations of X. Check graphically that your algorithm is correct. 2. Compute a Monte-Carlo estimate of the expected value and variance of X, together with a 95% confidence interval on the expected value. Illustrate the convergence of the Monte-Carlo estimate to the true expected value ( π 2 ). 3. Compute the analytical expression of the cumulative distribution function FX(x). Compute a Monte-Carlo estimate of FX(x) and the associated 95% confidence in- terval, for x ∈ (0.5, 1, 2), then compare the estimates to the true values. 4. Compute the analytical expression of the p-th quantile of the law of X. Give the approximate values of the 25%, 50% and 75% quantiles of the law of X, and compare these approximations to the true values.