From c72532803db576e80afb1e7901c59b1e6c2e5e73 Mon Sep 17 00:00:00 2001 From: pdaude Date: Fri, 2 Apr 2021 10:44:19 +0200 Subject: [PATCH] Updata toy_document.Rmd and create html file --- module2/exo1/toy_document_fr.Rmd | 24 +- module2/exo1/toy_document_fr.html | 435 ++++++++++++++++++++++++++++++ 2 files changed, 456 insertions(+), 3 deletions(-) create mode 100644 module2/exo1/toy_document_fr.html diff --git a/module2/exo1/toy_document_fr.Rmd b/module2/exo1/toy_document_fr.Rmd index 4f2d28d..aabe5b2 100644 --- a/module2/exo1/toy_document_fr.Rmd +++ b/module2/exo1/toy_document_fr.Rmd @@ -1,7 +1,7 @@ --- title: "À propos du calcul de pi" -author: "Arnaud Legrand" -date: "25 juin 2018" +author: "*Arnaud Legrand*" +date: "*25 juin 2018*" output: html_document --- @@ -12,7 +12,7 @@ knitr::opts_chunk$set(echo = TRUE) ## En demandant à la lib maths -Mon ordinateur m’indique que $\pi$ vaut approximativement. +Mon ordinateur m’indique que $\pi$ vaut *approximativement*. ```{r } pi @@ -30,3 +30,21 @@ theta = pi/2*runif(N) 2/(mean(x+sin(theta)>1)) ``` +## 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≤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() +``` + +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) +``` \ No newline at end of file diff --git a/module2/exo1/toy_document_fr.html b/module2/exo1/toy_document_fr.html new file mode 100644 index 0000000..c953f0c --- /dev/null +++ b/module2/exo1/toy_document_fr.html @@ -0,0 +1,435 @@ + + + + + + + + + + + + + + +À propos du calcul de pi + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + +
+

En demandant à la lib maths

+

Mon ordinateur m’indique que \(\pi\) vaut approximativement.

+
pi
+
## [1] 3.141593
+
+
+

En utilisant la méthode des aiguilles de Buffon

+

Mais calculé avec la méthode des aiguilles de Buffon, on obtiendrait comme approximation :

+
set.seed(42)
+N = 100000
+x = runif(N)
+theta = pi/2*runif(N)
+2/(mean(x+sin(theta)>1))
+
## [1] 3.14327
+
+
+

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≤1] = \pi/4 )\) (voir méthode de Monte Carlo sur Wikipedia). Le code suivant illustre ce fait:

+
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:

+
4*mean(df$Accept)
+
## [1] 3.156
+
+ + + + +
+ + + + + + + + + + + + + + + -- 2.18.1