diff --git a/module2/exo1/toy_document_fr.Rmd b/module2/exo1/toy_document_fr.Rmd index e45bd2de3adc749ac6cb6689660d35383a135c73..8d291c474574959abf6cf56cf9ad00884a69899b 100644 --- a/module2/exo1/toy_document_fr.Rmd +++ b/module2/exo1/toy_document_fr.Rmd @@ -42,10 +42,10 @@ 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 π en comptant combien de fois, en moyenne, $X{2}+Y{2}$ est inférieur à 1: + +Il est alors aisé d’obtenir une approximation (pas terrible) de π en comptant combien de fois, en moyenne, $X^{2}+Y^{2}$ est inférieur à 1: ```{r } 4*mean(df$Accept) ``` -## [1] 3.156 \ No newline at end of file diff --git a/module2/exo1/toy_document_fr.html b/module2/exo1/toy_document_fr.html index 16fee120fbfc39754b23f5e3ab6df4fa07f13088..751f4722fd6129242b689ea8f8f21c51a37d2e6f 100644 --- a/module2/exo1/toy_document_fr.html +++ b/module2/exo1/toy_document_fr.html @@ -1,42 +1,459 @@ ---- -title: "À Propos du calcul de pi" -author: "Arnaud Legrand" -date: "06/25/2020" -output: html_document ---- - -## En demandant à la lib maths -Mon ordinateur m'indique que \pi vaut *approximativement* - -```{r} -pi -``` - -## En tulisant la méthode des aiguilles de Buffon -Mais calculer avec la **méthode** des [aiguilles de Buffon](https://fr.wikipedia.org/wiki/Aiguille_de_Buffon), on obtiendrait comme **approximation** : -```{r} -set.seed(42) -N = 100000 -x = runif(N) -theta = pi/2*runif(N) -2/(mean(x+sin(theta)>1)) -``` + + + + + + + + + + + + + + +À propos du calcul de pi + + + + + + + + + + + + + + + + + + + + + + -## 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∼U(0,1) et Y∼U(0,1) alors P[X2+Y2≤1]=\pi/4 (voir [méthode de Monte Carlo sur Wikipedia](https://fr.wikipedia.org/wiki/Méthode_de_Monte-Carlo#Détermination_de_la_valeur_de_π)). Le code suivant illustre ce fait: -```{r} -set.seed(42) + + + + + + + + + + + + + + + + + + +
+ + + + + + + +
+

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} \le 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)
+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()
-```
+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 π en comptant combien de fois, en moyenne, \(X^{2}+Y^{2}\) est inférieur à 1:

+
4*mean(df$Accept)
+
## [1] 3.156
+
+ + + + +
+ + + + + + + + + -Il est alors aisé d’obtenir une approximation (pas terrible) de \pi en comptant combien de fois, en moyenne, X2+Y2 est inférieur à 1: + + -```{r} -4*mean(df$Accept) -``` \ No newline at end of file + +