"# Exercice : méthode de la transformée inverse\n",
"\n",
"But : générer des points selon une distribution de forme $f(x) = \\cos x$ dans l'intervalle $\\left[0.1, 1.5\\right]$\n",
"\n",
"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$\n",
"\n",
"On calcule $F(x) = \\int_a^x f(x') dx'$\n",
"\n",
"$$\n",
"\\int_a^x f(x') dx' = \\big[ \\alpha \\sin x \\big]_a^x = \\alpha ( \\sin x - \\sin a )\n",
"$$\n",
"\n",
"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$:\n",
"\n",
"$$\n",
"F(b) = \\alpha ( \\sin b - \\sin a ) = 1 \\Rightarrow \\alpha = \\frac{1}{\\sin b - \\sin a}\n",
"$$\n",
"\n",
"La fonction $F(x)$ est donc après normalisation:\n",
"\n",
"$$\n",
"F(x) = \\frac{\\sin x - \\sin a}{\\sin b - \\sin a}\n",
"$$\n",
"\n",
"Et la fonction inverse:\n",
"\n",
"$$\n",
"T(u) = \\arcsin((\\sin b - \\sin a) u + \\sin(a))\n",
" return math.asin((math.sin(sup) - math.sin(inf)) * y + math.sin(inf))\n",
"\n",
"inf, sup = 0.1, 1.5\n",
"print(\"inf:\", fm1(0, inf, sup), \"sup:\", fm1(1, inf, sup)) # should be inf, sup\n",
"uni = [random.random() for i in range(1000000)]\n",
"new = [fm1(x, inf, sup) for x in uni]\n",
"plt.hist(new, 14, rwidth=0.8, zorder=2)\n",
"plt.grid()\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "suspected-namibia",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.10"
},
"varInspector": {
"cols": {
"lenName": 16,
"lenType": 16,
"lenVar": 40
},
"kernels_config": {
"python": {
"delete_cmd_postfix": "",
"delete_cmd_prefix": "del ",
"library": "var_list.py",
"varRefreshCmd": "print(var_dic_list())"
},
"r": {
"delete_cmd_postfix": ") ",
"delete_cmd_prefix": "rm(",
"library": "var_list.r",
"varRefreshCmd": "cat(var_dic_list()) "
}
},
"types_to_exclude": [
"module",
"function",
"builtin_function_or_method",
"instance",
"_Feature"
],
"window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 5
}
%% 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$
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}