From 88feffe00db6f09e47b862fe19a2f3a2c5f60a37 Mon Sep 17 00:00:00 2001 From: Alicia Culot <87ce1e69e12ec125bff8a5bca2fcf747@app-learninglab.inria.fr> Date: Fri, 10 Oct 2025 09:48:49 +0000 Subject: [PATCH] Update toy_document_fr.Rmd --- module2/exo1/toy_document_fr.Rmd | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/module2/exo1/toy_document_fr.Rmd b/module2/exo1/toy_document_fr.Rmd index 45a7e99..76cc944 100644 --- a/module2/exo1/toy_document_fr.Rmd +++ b/module2/exo1/toy_document_fr.Rmd @@ -4,6 +4,7 @@ author: "Arnaud Legrand" date: "14/10/2014" output: html_document --- + ```{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE) ``` @@ -18,7 +19,7 @@ pi Mais calculé avec la __méthode__ des [aiguilles de Buffon](https://fr.wikipedia.org/wiki/Aiguille_de_Buffon), on obtiendrait comme __approximation__ : -```{r pressure, echo=FALSE} +```{r} set.seed(42) N = 100000 x = runif(N) @@ -28,15 +29,18 @@ theta = pi/2*runif(N) ## Avec un argument "fréquentiel" de surface Sinon, une méthode plus simple à comprendre et ne faisant pas intervenir d'appel à la fonction sinus se base sur le fait que si $X\sim U(0,1)$ et $Y\sim U(0,1)$ alors $P[X^2+Y^2\leq 1] = \pi/4$ (voir [méthode de Monte Carlo sur Wikipedia](https://fr.wikipedia.org/wiki/M%C3%A9thode_de_Monte-Carlo#D%C3%A9termination_de_la_valeur_de_%CF%80)). Le code suivant illustre ce fait : -```{r} -set.seed(42) -N = 1000 -df = data.frame(X = runif(N), Y = runif(N)) -df$Accept = (df$X**2 + df$Y**2 <=1) -library(ggplot2) -ggplot(df, aes(x=X,y=Y,color=Accept)) + geom_point(alpha=.2) + coord_fixed() + theme_bw() -``` + +```{r} +set.seed(42) +N = 1000 +df = data.frame(X = runif(N), Y = runif(N)) +df$Accept = (df$X**2 + df$Y**2 <=1) +library(ggplot2) +ggplot(df, aes(x=X,y=Y,color=Accept)) + geom_point(alpha=.2) + coord_fixed() + theme_bw() +``` + Il est alors aisé d'obtenir une approximation (pas terrible) de $\pi$ en comptant combien de fois, en moyenne, $X^2 + Y^2$ est inférieur à 1 : -```{r} -4*mean(df$Accept) + +```{r} +4*mean(df$Accept) ``` \ No newline at end of file -- 2.18.1