diff --git a/module2/exo1/toy_document_fr.Rmd b/module2/exo1/toy_document_fr.Rmd index 28e9cc5404b1b0ee27cbfcc464cd51c9f25291c2..1069167aa9562bc1f962be51334e287198c84df9 100644 --- a/module2/exo1/toy_document_fr.Rmd +++ b/module2/exo1/toy_document_fr.Rmd @@ -30,4 +30,23 @@ 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∼U(0,1)$ et $Y∼U(0,1)$ alors $P[X^2+Y^2≤1]=π/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 message=FALSE} +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 π 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 index 43bd44053ee87aed117393c29a09984051f602b8..64123d752588f075df0e20d250f4595e10b76918 100644 --- a/module2/exo1/toy_document_fr.html +++ b/module2/exo1/toy_document_fr.html @@ -213,6 +213,21 @@ theta = pi/2*runif(N) 2/(mean(x+sin(theta)>1))
## [1] 3.14327
+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[X^2+Y^2≤1]=π/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 π en comptant combien de fois, en moyenne, \(X^2+Y^2\)
+est inférieur à 1:
+4*mean(df$Accept)
+## [1] 3.156
+