Skip to content
Extraits de code Groupes Projets
Valider 436e4504 rédigé par Jérôme de Favereau de Jeneret's avatar Jérôme de Favereau de Jeneret
Parcourir les fichiers

Update NM4_ex_solution.ipynb

parent e8124c25
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
%% Cell type:markdown id:broken-executive tags:
# Exercice : méthode de la transformée inverse
But : générer des points selon une distribution de forme $f(x) = \cos x$ dans l'intervalle $\left[0.1, 1.5\right]$
Premièrtement, on ajoute une **constante de normalisation** $\alpha$ de façon à ce que l'intégrale entre les bornes de notre distribution soit bien égale à $1$ (la probabilité de tirer un nombre entre les deux bornes. On a $f(x) = \alpha \cos x$
Premièrement, on ajoute une **constante de normalisation** $\alpha$ de façon à ce que l'intégrale entre les bornes de notre distribution soit bien égale à $1$ (la probabilité de tirer un nombre entre les deux bornes. On a $f(x) = \alpha \cos x$
On calcule $F(x) = \int_a^x f(x') dx'$
$$
\int_a^x f(x') dx' = \big[ \alpha \sin x \big]_a^x = \alpha ( \sin x - \sin a )
$$
Notez que je n'utilise pas la constante $\beta$ comme aux le cours, qui constitue une étape inutile. On fixe la valeur de $\alpha$ pour avoir $F(b) = 1$:
$$
F(b) = \alpha ( \sin b - \sin a ) = 1 \Rightarrow \alpha = \frac{1}{\sin b - \sin a}
$$
La fonction $F(x)$ est donc après normalisation:
$$
F(x) = \frac{\sin x - \sin a}{\sin b - \sin a}
$$
Et la fonction inverse:
$$
T(u) = \arcsin((\sin b - \sin a) u + \sin(a))
$$
%% Cell type:code id:french-candle tags:
``` python
import math
import random
import matplotlib.pyplot as plt
def fm1(y, inf, sup):
return math.asin((math.sin(sup) - math.sin(inf)) * y + math.sin(inf))
inf, sup = 0.1, 1.5
print("inf:", fm1(0, inf, sup), "sup:", fm1(1, inf, sup)) # should be inf, sup
uni = [random.random() for i in range(1000000)]
new = [fm1(x, inf, sup) for x in uni]
plt.hist(new, 14, rwidth=0.8, zorder=2)
plt.grid()
plt.show()
```
%% Output
inf: 0.1 sup: 1.5000000000000018
%% Cell type:code id:suspected-namibia tags:
``` python
```
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter