4
Universit´ e Paris-Dauphine Ann´ ee 2012-2013 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. Enregistrez r´ eguli` erement vos fichiers sur l’ordinateur, sans utiliser d’accents ni d’espace, ni de caract` eres sp´ eciaux. 2. Si vous utilisez Rkward, vous devez enregistrer ` a l’aide du bouton “Save script” (ou “Save script as”) et non “Save”. 3. 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 ` a l’aide d’un autre ´ editeur de texte afin de v´ erifier qu’il contient bien tout votre code R. 4. En cas de probl` eme ou d’inqui´ etude, contacter un enseignant sans vous d´ econnecter. Il nous est sinon impossible de r´ ecup´ erer les fichiers de sauvegarde automatique. Aucun document informatique n’est autoris´ e, seuls les livres de R le sont. 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 trois et uniquement trois exercices au choix. Exercice 1 Given the probability density f (x|k,θ)= C θ k |x| k-1 e - |x| θ , 1. explain why an importance sampling technique, designed to approximate the constant C , that is based on the Normal density cannot not work. Illustrate this lack of convergence with a numerical experiment using k = 1 and θ = 2. 2. Propose a more suitable importance distribution. We now focus on the integral I = Z R x 2 f (x|1, 2)dx using samples of size n = 10 2

R exam (B) given in Paris-Dauphine, Licence Mido, Jan. 11, 2013

Embed Size (px)

DESCRIPTION

This is one of two exams given to our students this year. They had two hours to solve three problems and had to return R codes as well as handwritten explanations.

Citation preview

Page 1: R exam (B) given in Paris-Dauphine, Licence Mido, Jan. 11, 2013

Universite Paris-Dauphine Annee 2012-2013Departement de Mathematique

Examen NOISE, sujet B

Preliminaires

Cet examen est a realiser sur ordinateur en utilisant le langage R et arendre simultanement sur papier pour les reponses detaillees et sur fichierinformatique Examen pour les fonctions R utilisees. Les fichiers informa-tiques seront a sauvegarder suivant la procedure ci-dessous et seront prisen compte pour la note finale. Toute duplication de fichiers R fera l’objetd’une poursuite disciplinaire. L’absence de document enregistre donneralieu a une note nulle sans possibilite de contestation.

1. Enregistrez regulierement vos fichiers sur l’ordinateur, sans utiliserd’accents ni d’espace, ni de caracteres speciaux.

2. Si vous utilisez Rkward, vous devez enregistrer a l’aide du bouton“Save script” (ou “Save script as”) et non “Save”.

3. Verifiez que vos fichiers ont bien ete enregistres en les rouvrant avantde vous deconnecter. N’hesitez pas a rouvrir votre fichier a l’aide d’unautre editeur de texte afin de verifier qu’il contient bien tout votrecode R.

4. En cas de probleme ou d’inquietude, contacter un enseignant sansvous deconnecter. Il nous est sinon impossible de recuperer les fichiersde sauvegarde automatique.

Aucun document informatique n’est autorise, seuls les livres de R le sont.L’utilisation de tout service de messagerie ou de mail est interdite et, encas d’utilisation averee, se verra sanctionnee.

Les problemes sont independants, peuvent etre traites dans n’importe quelordre. Resoudre trois et uniquement trois exercices au choix.

Exercice 1Given the probability density

f(x|k, θ) =C

θk|x|k−1e−

|x|θ ,

1. explain why an importance sampling technique, designed to approximate theconstant C, that is based on the Normal density cannot not work. Illustrate thislack of convergence with a numerical experiment using k = 1 and θ = 2.

2. Propose a more suitable importance distribution.

We now focus on the integral

I =

∫Rx2f(x|1, 2)dx

using samples of size n = 102

Page 2: R exam (B) given in Paris-Dauphine, Licence Mido, Jan. 11, 2013

3. Propose a Monte Carlo approximation of I. (Hint : Note that the integral over Ris twice the integral over R+ and connect f with a standard distribution on R+.)

4. Approximate I by importance sampling using the same distribution g as in question2.

5. Compute a confidence interval on I at level 95% for each of your method. Whichone of the two estimates does reach the lowest precision ?

6. Design a Monte Carlo experiment in order to check whether or not the asymptoticcoverage level of the CI holds. Repeat the experiment with samples of size n = 103.

Exercice 2Given the density on R∗+,

f(x|α, β) =βα

Γ(α)x−α−1e−

βx

1. Determine which of the following distributions can be used in an A/R algorithmdesigned to sample from f(x|2, 4) :

g1(x) =k

λ(x

λ)k−1e−(xλ)

kg2(x) =

1

θk1

Γ(k)xk−1e−

xθ g3(x) = (1 + αx)−1/α−1

which are respectively a Weibull, a Gamma and a generalized Pareto distributiondensity. (Motivate your choice.)

2. Using the inversion method write an algorithm that samples from the selected g.

3. Write an R function AR() that samples from f(x|2, 4). (Extra-credit : Optimize theparameters of the proposal density g.)

4. Based on a sample of size 104 from f(x|2, 4), estimate by Monte Carlo the meanand variance of h(X) = 1/X and give a confidence interval at level 95% for bothquantities.

5. The distribution associated with f can be obtained by the transform 1/Z whereZ ∼ Gamma(α, 1/β). Establish this result and test it, based on the sample used inquestion 4.

Exercice 3If X1, X2, . . . , Xk is a sample from the N(0, 1) distribution, then Yk =

∑X2i follows the

χ2(k) distribution. We wish to verify a convergence theorem due to R. A. Fisher whichstates that √

2Yk −√

2k − 1L−−−→

k→∞N(0, 1)

1. Create a function rchisq2(n,k) which simulates n realizations of the χ2(k) distri-bution, using nk realizations of the standard normal distribution. (Note : if you donot manage this question, you can use the R function rchisq() for the remainderof the exercise.)

2. For k = 50 and n = 1000, propose a graphical way to verify the fit of√

2Yk−√

2k − 1to the N(0, 1) distribution.

3. Using ks.test() and n = 1000, check whether the normal distribution is an accep-table fit when k = 3, k = 30, k = 300.

Page 3: R exam (B) given in Paris-Dauphine, Licence Mido, Jan. 11, 2013

4. From now on, k = 300 and n = 1000. We now have a test to check the fit of asample x to the χ2(k) distribution : we accept the null hypothesis that x comesfrom the χ2(k) distribution iff the Kolmogorov-Smirnov test accepts the hypothesisthat

√2Yk −

√2k − 1 fits the N(0, 1) distribution. Perform a bootstrap experiment

to calculate the probability of accepting the null hypothesis for a sample whichcomes from the Beta(1, k) distribution.

5. Perform another bootstrap experiment to calculate the same probability when usingdirectly the Kolmogorov-Smirnov test for fit to the χ2(k) distribution (whose cdfexists in R as pchisq).

Exercice 4The Frechet(α, s,m) distribution defines a random variable X which takes values in]m,+∞[ and with cumulative distribution function

F (x) = exp

(−(x−ms

)−α)1. Using the generic inversion method, write a function rfrechet(n,α,s,m) which

outputs n realizations of the Frechet(α, s,m) distribution.

2. For α = 5, s = 1, m = −3, give a Monte Carlo experiment to estimate V ar(X)and the median of X. Calculate (on paper) the theoretical value of the median andcompare it to your estimate.

3. Propose a bootstrap experiment to evaluate the bias of your variance and medianestimators.

4. For α = 5, s = 1, m = −3, use the Kolmogorov-Smirnov test to verify that thevariable

Y =

(1

X −m

)αfollows an Exp(1) distribution.

Exercice 5Consider the density function on the real line R

fk(x) =(2k + 1)! Φ(x)kΦ(−x)k

(k!)2√

2π exp(x2/2)

where k ≥ 1 is an integer and Φ is the normal cdf.

1. Check by numerical integration that fk is a proper density for k = 6, 12, 24

2. Design an accept-reject algorithm function on R that produce an iid sample ofarbitrary size m for an arbitrary parameter k. (Hint : Notice that either Φ(x)or Φ(−x) is necessarily less than 1/2 and that Φ(−x) = 1 − Φ(x). Deduce thatΦ(x)Φ(−x) < 1/4.) Produce a graphical verification of the fit for m = 103 andk = 6, 12, 24.

3. We want to check from the acceptance rate of this accept-reject algorithm thatthe normalisation is correct in the above. Produce 250 realisations of an empiricalacceptance rate based on 100 proposals and deduce a 97% confidence interval onthe expectation of the acceptance rate. Check whether or not it contains the inversenormalising constant.

Page 4: R exam (B) given in Paris-Dauphine, Licence Mido, Jan. 11, 2013

4. This density is actually the distribution of the median of a normal sample of sizen = 2k + 1. (Extra-credit : Establish this rigorously.) Generate a sample from theabove accept-reject algorithm with m = 250 and k = 10, then another sample ofm = 250 medians from samples of 21 normal variates. Test whether they have thesame distribution.

5. Check whether or not the p-value of the above test is distributed as a uniform U(0, 1)random variate.

Exercice 6Download the dataset Nile :

> data(Nile)

> nile = jitter(as.vector(Nile))

We will assume that those are iid realisations of a random variable X, producing asample Xn = (X1, . . . , Xn).

We denote by IQ0.8(Xn) an inter-quantile interval of the sample, defined by

IQ0.8(Xn) = Q0.9(Xn)−Q0.1(Xn)

where Q0.9(Xn) and Q0.1(Xn) are the empirical quantiles of the sample at levels 90%and 10%. We would like to calibrate IQ0.8(Xn) by a coefficient α so that it becomes anunbiased estimator of the standard deviation of the distribution of the Xi’s.

1. Write an R function iqan(x) which produces the statistic IQ20.8(Xn) associated

with the sample x. Compute the outcome of your function on the dataset nile.

2. Simulate 104 replicas of a Cauchy C(µ, σ) (µ being the location and σ the scale)sample Xn of size n = 100 and deduce a Monte Carlo evaluation of the coefficientα such that αE[IQ2

0.8(Xn)] = σ2. (Extra-credit : Explain why the values of µ and σcan be chosen arbitrarily.)

3. Based on the previous experiment, and using the 104 realisations of IQ0.8(Xn)generated in question 2., deduce a 93% confidence interval on IQ0.8(Xn)/σ. (Hint :Use the empirical cdf, rather than bootstrap.) Compare with the asymptoticallynormal 93% confidence interval on E[IQ0.8(Xn)]/σ. Check whether or not 6.1554belongs to these intervals. (Extra-credit : Justify the choice α = 1/6.1554.)

4. By running a Monte Carlo experiment based on 105 replications of Cauchy randomvariates with location µ and scale σ of your choice, check whether or not the 93%confidence interval on log |X − µ| contains log(σ).

We will assume in the rest of the exercise that µ = Q0.5(Xn), the median of the sample,and

σ(Xn) = exp{(log |X1 −Q0.5(Xn)|+ . . .+ log |Xn −Q0.5(Xn)|)/n}are acceptable estimators of µ and σ. (Extra-credit : Explain why the usual empiricalmoments do not work for the Cauchy distribution.)

5. Check whether or not nile is distributed from a Cauchy sample (with unknownlocation and scake).

6. Since nile is not necessarily a Cauchy sample, denoting by σ the standard deviationof the distribution of the Xi’s, construct by bootstrap a 93% confidence interval onIQ0.8(Xn)/σ, using σ(Xn) based on nile as the estimate of σ. Does it still contain6.1554 ?