Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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

Xi'an
January 21, 2014

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

Xi'an

January 21, 2014
Tweet

More Decks by Xi'an

Other Decks in Education

Transcript

  1. Universit´
    e Paris-Dauphine Ann´
    ee 2013-2014

    epartement de Math´
    ematique
    Examen NOISE, sujet A
    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

    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. Write an R function called dart(n,t,l) that (a) uniformly generates n random points
    (x, y) inside the square [−1, 1] × [−1, 1] and (b) returns the proportion of those
    points within the unit circle, x2 +y2 ≤ 1. Use dart to derive an approximation of the
    constant π by this experiment and plot the evolutions of the approximation when
    the number n of dots grows from 10 to 105.
    2. Buffon’s needle is one of the earliest instances of using simulation to approximate
    an integral. It uses random throws of needles of length over a wooden floor made
    of planks of width t ≥ and derives the constant π from the proportion of needles

    View full-size slide

  2. crossing a plank separation (or line). This model assumes all planks are identical, ho-
    rizontal, and parallel. The theoretical probability that the needle crosses the closest
    line is 2 /tπ.
    (a) Implement the following R code :
    BuffonsNeedle <- function(n=100, sd=2){
    #(C.) Allan Roberts, 2013.
    X <- rnorm(n,5,sd)
    Y <- rnorm(n,5,sd)
    Angle <- runif(n,0,2*pi)
    X2 <- X + cos(Angle)
    Y2 <- Y + sin(Angle)
    CrossesLine <- ( floor(Y) != floor(Y2) )
    p <- sum(CrossesLine)/n
    return(list(prob=p,esti=2/p))
    }
    and explain the output of the function.
    (b) Identify from the R code what the corresponding values of and t are.
    (c) Explain why (or accept the fact that) this R code is only an approximation to
    Buffon’s problem.
    (d) For n = 105, give a confidence interval on π and check whether π belongs to
    this interval.
    (e) Repeat the above verification with sd=0.2, then with sd=.002.
    Exercise 2
    We wish to estimate the integral
    I =

    0
    x2 cos2(x) sin4(x) dx
    1. Find the constant k such that
    f : x → kx2
    I{0is 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) ∝ x2 cos2(x) sin4(x) I{0.
    4. What is the estimated acceptation probability of your algorithm ? Deduce an esti-
    mate of I.
    Exercise 3
    Given the integral
    I =
    3
    −3
    ex−x2
    2 dx

    View full-size slide

  3. 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 a normal distribution. Give
    the corresponding 95% confidence interval for I with n = 103. Can we take advantage
    of the fact that the normal distribution is symmetric to improve our 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-
    mates as n increase) and numerical means.
    Exercise 4
    Let us consider a random variable X, whose probability density is given by :
    f(x) =
    3
    2

    xe−x3/2
    1
    R+
    (x)
    1. Show that Y = X3/2 is distributed according to the exponential distribution with
    rate parameter 1, 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 (Γ(5/3)).
    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, 1.5), 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 30%, 60% and 90% quantiles of the law of X, and compare
    these approximations to the true values.

    View full-size slide