diff --git a/module2/exo1/toy_document_en.html b/module2/exo1/toy_document_en.html new file mode 100644 index 0000000000000000000000000000000000000000..8c2f1924e8dfa5bff45d0cb0434fdf11b590719a --- /dev/null +++ b/module2/exo1/toy_document_en.html @@ -0,0 +1,188 @@ + + + + + + + + + + + + + + +On the computation of pi + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + +
+

Asking the maths library

+

My computer tells me that \(\pi\) is approximatively

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

Buffon’s needle

+

Applying the method of Buffon’s needle, we get the approximation

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

Using a surface fraction argument

+

A method that is easier to understand and does not make use of the \(\sin\) function is based on the fact that if \(X\sim U(0,1)\) and \(Y\sim U(0,1)\), then \(P[X^2+Y^2\leq 1] = \pi/4\) (see “Monte Carlo method” on Wikipedia). The following code uses this approach:

+
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()
+

+

It is therefore straightforward to obtain a (not really good) approximation to \(\pi\) by counting how many times, on average, \(X^2 + Y^2\) is smaller than 1 :

+
4*mean(df$Accept)
+
## [1] 3.156
+
+ + + + +
+ + + + + + + + diff --git a/module2/exo1/toy_document_fr.html b/module2/exo1/toy_document_fr.html new file mode 100644 index 0000000000000000000000000000000000000000..0924a68872d33294b72ab820febb506b3aaaab25 --- /dev/null +++ b/module2/exo1/toy_document_fr.html @@ -0,0 +1,188 @@ + + + + + + + + + + + + + + +À 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\leq 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
+
+ + + + +
+ + + + + + + + diff --git a/module2/exo1/toy_document_orgmode_R_en.html b/module2/exo1/toy_document_orgmode_R_en.html new file mode 100644 index 0000000000000000000000000000000000000000..6ad7f633368232efc20f044b668d2f95a7c40b92 --- /dev/null +++ b/module2/exo1/toy_document_orgmode_R_en.html @@ -0,0 +1,369 @@ + + + + + + + +On the computation of pi + + + + + + + + + + + + + + +
+

On the computation of pi

+
+

Table of Contents

+ +
+ +
+

1 Asking the maths library

+
+

+My computer tells me that \(\pi\) is approximatively +

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

2 Buffon's needle

+
+

+Applying the method of Buffon's needle, we get the approximation +

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

3 Using a surface fraction argument

+
+

+A method that is easier to understand and does not make use of the \(\sin\) function is based on the fact that if \(X\sim U(0,1)\) and \(Y\sim U(0,1)\), then \(P[X^2+Y^2\leq 1] = \pi/4\) (see "Monte Carlo method" on Wikipedia). The following code uses this approach: +

+ +
+
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()
+
+
+ + +
+

figure_pi_mc1.png +

+
+ +

+It is then straightforward to obtain a (not really good) approximation to \(\pi\) by counting how many times, on average, \(X^2 + Y^2\) is smaller than 1: +

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

Author: Konrad Hinsen

+

Created: 2019-03-28 Thu 11:13

+

Validate

+
+ + diff --git a/module2/exo1/toy_document_orgmode_R_fr.html b/module2/exo1/toy_document_orgmode_R_fr.html new file mode 100644 index 0000000000000000000000000000000000000000..5b2a25bdad4d4344012e94e98bfe68dc4a886657 --- /dev/null +++ b/module2/exo1/toy_document_orgmode_R_fr.html @@ -0,0 +1,381 @@ + + + + + + + +À propos du calcul de \(\pi\) + + + + + + + + + + + + + + + +
+

À propos du calcul de \(\pi\)

+
+

Table des matières

+ +
+ +
+

1 En demandant à la lib maths

+
+

+Mon ordinateur m'indique que \(\pi\) vaut approximativement +

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

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

3 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). 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()
+
+
+ + +
+

figure_pi_mc1.png +

+
+ +

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

Auteur: Arnaud Legrand

+

Created: 2018-09-19 mer. 21:51

+

Validate

+
+ + diff --git a/module2/exo1/toy_document_orgmode_python_en.html b/module2/exo1/toy_document_orgmode_python_en.html new file mode 100644 index 0000000000000000000000000000000000000000..4eb6e9bf8efa3e7b1bd44784f6776581e2ca1239 --- /dev/null +++ b/module2/exo1/toy_document_orgmode_python_en.html @@ -0,0 +1,383 @@ + + + + + + + +On the computation of pi + + + + + + + + + + + + + + +
+

On the computation of pi

+
+

Table of Contents

+ +
+ +
+

1 Asking the math library

+
+

+My computer tells me that \(\pi\) is approximatively +

+ +
+
from math import *
+pi
+
+
+ +
+3.141592653589793
+
+
+
+ +
+

2 * Buffon's needle

+
+

+Applying the method of Buffon's needle, we get the approximation +

+ +
+
import numpy as np
+np.random.seed(seed=42)
+N = 10000
+x = np.random.uniform(size=N, low=0, high=1)
+theta = np.random.uniform(size=N, low=0, high=pi/2)
+2/(sum((x+np.sin(theta))>1)/N)
+
+
+ +
+3.12891113892
+
+
+
+ +
+

3 Using a surface fraction argument

+
+

+A method that is easier to understand and does not make use of the \(\sin\) function is based on the fact that if \(X\sim U(0,1)\) and \(Y\sim U(0,1)\), then \(P[X^2+Y^2\leq 1] = \pi/4\) (see "Monte Carlo method" on Wikipedia). The following code uses this approach: +

+ + +
+
import matplotlib.pyplot as plt
+
+np.random.seed(seed=42)
+N = 1000
+x = np.random.uniform(size=N, low=0, high=1)
+y = np.random.uniform(size=N, low=0, high=1)
+
+accept = (x*x+y*y) <= 1
+reject = np.logical_not(accept)
+
+fig, ax = plt.subplots(1)
+ax.scatter(x[accept], y[accept], c='b', alpha=0.2, edgecolor=None)
+ax.scatter(x[reject], y[reject], c='r', alpha=0.2, edgecolor=None)
+ax.set_aspect('equal')
+
+plt.savefig(matplot_lib_filename)
+print(matplot_lib_filename)
+
+
+ + +
+

figure_pi_mc2.png +

+
+ +

+It is then straightforward to obtain a (not really good) approximation to \(\pi\) by counting how many times, on average, \(X^2 + Y^2\) is smaller than 1: +

+ +
+
4*np.mean(accept)
+
+
+ +
+3.1120000000000001
+
+
+
+
+
+

Author: Konrad Hinsen

+

Created: 2019-03-28 Thu 11:13

+

Validate

+
+ + diff --git a/module2/exo1/toy_document_orgmode_python_fr.html b/module2/exo1/toy_document_orgmode_python_fr.html new file mode 100644 index 0000000000000000000000000000000000000000..a6c3ff6053fbca8f1a221d2fba08e80fb6fcd09c --- /dev/null +++ b/module2/exo1/toy_document_orgmode_python_fr.html @@ -0,0 +1,387 @@ + + + + + + + +À propos du calcul de \(\pi\) + + + + + + + + + + + + + + +
+

À propos du calcul de \(\pi\)

+
+

Table des matières

+ +
+ +
+

1 En demandant à la lib maths

+
+

+Mon ordinateur m'indique que \(\pi\) vaut approximativement: +

+ +
+
from math import *
+pi
+
+
+ +
+3.141592653589793
+
+
+
+ +
+

2 En utilisant la méthode des aiguilles de Buffon

+
+

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

+ +
+
import numpy as np
+np.random.seed(seed=42)
+N = 10000
+x = np.random.uniform(size=N, low=0, high=1)
+theta = np.random.uniform(size=N, low=0, high=pi/2)
+2/(sum((x+np.sin(theta))>1)/N)
+
+
+ +
+3.12891113892
+
+
+
+ +
+

3 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). Le code suivant illustre ce fait : +

+ +
+
import matplotlib.pyplot as plt
+
+np.random.seed(seed=42)
+N = 1000
+x = np.random.uniform(size=N, low=0, high=1)
+y = np.random.uniform(size=N, low=0, high=1)
+
+accept = (x*x+y*y) <= 1
+reject = np.logical_not(accept)
+
+fig, ax = plt.subplots(1)
+ax.scatter(x[accept], y[accept], c='b', alpha=0.2, edgecolor=None)
+ax.scatter(x[reject], y[reject], c='r', alpha=0.2, edgecolor=None)
+ax.set_aspect('equal')
+
+plt.savefig(matplot_lib_filename)
+print(matplot_lib_filename)
+
+
+ + +
+

figure_pi_mc2.png +

+
+ +

+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*np.mean(accept)
+
+
+ +
+3.1120000000000001
+
+
+
+
+
+

Auteur: Konrad Hinsen

+

Created: 2019-03-28 Thu 11:06

+

Validate

+
+ + diff --git a/module2/exo1/toy_notebook_en.pdf b/module2/exo1/toy_notebook_en.pdf new file mode 100644 index 0000000000000000000000000000000000000000..f84d42739e9c324fa861fc7a19631734566ec6f0 Binary files /dev/null and b/module2/exo1/toy_notebook_en.pdf differ diff --git a/module2/exo1/toy_notebook_fr.pdf b/module2/exo1/toy_notebook_fr.pdf new file mode 100644 index 0000000000000000000000000000000000000000..52e1666fa1abb3eab1c1e68df71d60e2fc96cae8 Binary files /dev/null and b/module2/exo1/toy_notebook_fr.pdf differ