Refactor code for improved readability and consistency across notebooks

- Standardized spacing around operators and function arguments in TP7_Kmeans.ipynb and neural_network.ipynb.
- Enhanced the formatting of model building and training code in neural_network.ipynb for better clarity.
- Updated the pyproject.toml to remove a specific TensorFlow version and added linting configuration for Ruff.
- Improved comments and organization in the code to facilitate easier understanding and maintenance.
This commit is contained in:
2025-07-01 20:46:08 +02:00
parent e273cf90f7
commit f94ff07cab
34 changed files with 5713 additions and 5047 deletions

View File

@@ -51,17 +51,18 @@
" \"\"\"\n",
" return S0 * np.exp((mu - 0.5 * sigma**2) * t + sigma * W)\n",
"\n",
"\n",
"def euler_maruyama(mu, sigma, T, N, X0=0.0):\n",
" \"\"\"\n",
" Simulation d'une EDS de Black-Scholes par la méthode d'Euler-Maruyama\n",
" \n",
"\n",
" Paramètres :\n",
" mu (float) : drift\n",
" sigma (float) : volatilité\n",
" T (int) : temps final\n",
" N (int) : nombre de pas de temps\n",
" X0 (float) : valeur initiale\n",
" \n",
"\n",
" Retourne :\n",
" t (array-like) : tableau des temps\n",
" X (array-like) : tableau des valeurs de l'EDS\n",
@@ -70,17 +71,18 @@
"\n",
" t = np.linspace(0, T, N + 1)\n",
" X = np.zeros(N + 1)\n",
" \n",
"\n",
" X[0] = X0\n",
"\n",
" dW = np.random.normal(0, np.sqrt(dt), N)\n",
" \n",
"\n",
" for i in range(N):\n",
" St = S(t[i], X[i], mu, sigma, dW[i])\n",
" X[i + 1] = X[i] + mu * St * dt + sigma * St * dW[i]\n",
" \n",
"\n",
" return t, X\n",
"\n",
"\n",
"def plot_brownien(t, X, B=None):\n",
" \"\"\"\n",
" Plot la simulation d'Euler-Maruyama\n",
@@ -90,15 +92,15 @@
" X (array-like) : tableau des valeurs de l'EDS\n",
" B (float) : barrière (optionnelle)\n",
" \"\"\"\n",
" plt.plot(t, X, alpha=0.5, label='Euler-Maruyama')\n",
" plt.title('Simulation d\\'Euler-Maruyama pour une EDS')\n",
" \n",
" plt.plot(t, X, alpha=0.5, label=\"Euler-Maruyama\")\n",
" plt.title(\"Simulation d'Euler-Maruyama pour une EDS\")\n",
"\n",
" if B is not None:\n",
" plt.axhline(B, label='Barrière', color='red', linestyle='--')\n",
" \n",
" plt.axhline(B, label=\"Barrière\", color=\"red\", linestyle=\"--\")\n",
"\n",
" plt.legend()\n",
" plt.xlabel('Temps')\n",
" plt.ylabel('X(t)')\n",
" plt.xlabel(\"Temps\")\n",
" plt.ylabel(\"X(t)\")\n",
" plt.grid()"
]
},
@@ -165,10 +167,11 @@
"\n",
"np.random.seed(333)\n",
"\n",
"\n",
"def plot_convergence(S0, mu, sigma, T):\n",
" \"\"\"\n",
" Plot la convergence du schéma d'Euler-Maruyama\n",
" \n",
"\n",
" Paramètres :\n",
" S0 (int) : valeur initiale\n",
" mu (float) : drift\n",
@@ -176,26 +179,27 @@
" T (int) : temps final\n",
" \"\"\"\n",
" errors = []\n",
" \n",
"\n",
" for N in N_list:\n",
" dt = T / N\n",
" dW = np.random.normal(0, np.sqrt(dt), N)\n",
" \n",
"\n",
" exact = S(T, S0, mu, sigma, dW)\n",
" _, X = euler_maruyama(mu=mu, sigma=sigma, T=T, N=N, X0=S0)\n",
" \n",
"\n",
" errors.append(np.max(np.abs(X[1:] - exact)))\n",
" \n",
" plt.plot(np.log(h_list), np.log(errors), 'o-', label='Erreur numérique')\n",
" plt.plot(np.log(h_list), 0.5 * np.log(h_list), '--', label='Ordre 1/2')\n",
" plt.plot(np.log(h_list), np.log(h_list), '--', label='Ordre 1')\n",
" plt.plot(np.log(h_list), 2*np.log(h_list), '--', label='Ordre 2')\n",
" plt.xlabel('log(h)')\n",
" plt.ylabel('log(Erreur)')\n",
" plt.title('Convergence du schéma d\\'Euler-Maruyama')\n",
"\n",
" plt.plot(np.log(h_list), np.log(errors), \"o-\", label=\"Erreur numérique\")\n",
" plt.plot(np.log(h_list), 0.5 * np.log(h_list), \"--\", label=\"Ordre 1/2\")\n",
" plt.plot(np.log(h_list), np.log(h_list), \"--\", label=\"Ordre 1\")\n",
" plt.plot(np.log(h_list), 2 * np.log(h_list), \"--\", label=\"Ordre 2\")\n",
" plt.xlabel(\"log(h)\")\n",
" plt.ylabel(\"log(Erreur)\")\n",
" plt.title(\"Convergence du schéma d'Euler-Maruyama\")\n",
" plt.legend()\n",
" plt.grid(True)\n",
"\n",
"\n",
"plt.figure(figsize=(10, 5))\n",
"plot_convergence(S0, r, sigma, T)\n",
"plt.show()"
@@ -269,6 +273,7 @@
"plot_brownien(t, X, B=B)\n",
"plt.show()\n",
"\n",
"\n",
"def is_barrier_breached(X, B):\n",
" \"\"\"Renvoie True si la barrière est franchie, False sinon\n",
" La barrière est franchie si X >= B\n",
@@ -282,7 +287,12 @@
" \"\"\"\n",
" return any(X >= B)\n",
"\n",
"print(\"La barrière a été franchie\" if is_barrier_breached(X, B) else \"La barrière n'a pas été franchie\")"
"\n",
"print(\n",
" \"La barrière a été franchie\"\n",
" if is_barrier_breached(X, B)\n",
" else \"La barrière n'a pas été franchie\"\n",
")"
]
},
{
@@ -299,18 +309,19 @@
" trajectories (list of tuples): Liste des trajectoires avec le temps et les valeurs\n",
" B (float): Valeur de la barrière\n",
" \"\"\"\n",
" for (t, X) in trajectories:\n",
" col = 'pink' if is_barrier_breached(X, B) else 'lime'\n",
" for t, X in trajectories:\n",
" col = \"pink\" if is_barrier_breached(X, B) else \"lime\"\n",
" plt.plot(t, X, alpha=0.5, color=col)\n",
" plt.title('Simulation d\\'Euler-Maruyama pour une EDS')\n",
" \n",
" plt.axhline(B, label='Barrière', color='red', linestyle='--')\n",
" \n",
" plt.title(\"Simulation d'Euler-Maruyama pour une EDS\")\n",
"\n",
" plt.axhline(B, label=\"Barrière\", color=\"red\", linestyle=\"--\")\n",
"\n",
" plt.legend()\n",
" plt.xlabel('Temps')\n",
" plt.ylabel('X(t)')\n",
" plt.xlabel(\"Temps\")\n",
" plt.ylabel(\"X(t)\")\n",
" plt.grid()\n",
" \n",
"\n",
"\n",
"def payoff(X, B, K):\n",
" \"\"\"Calcule le payoff d'une option en fonction des trajectoires.\n",
"\n",
@@ -324,9 +335,10 @@
" \"\"\"\n",
" if not is_barrier_breached(X, B):\n",
" return max(X[-1] - K, 0)\n",
" else: \n",
" else:\n",
" return 0\n",
" \n",
"\n",
"\n",
"def call_BS(x):\n",
" \"\"\"Calcul du prix d'une option d'achat européenne selon le modèle de Black-Scholes en fonction de x.\n",
"\n",
@@ -336,27 +348,34 @@
" Retourne:\n",
" float: Le prix de l'option d'achat européenne.\n",
" \"\"\"\n",
" d1 = (np.log(x/K) + (r + 0.5 * sigma**2) * T) / (sigma * np.sqrt(T))\n",
" d1 = (np.log(x / K) + (r + 0.5 * sigma**2) * T) / (sigma * np.sqrt(T))\n",
" d2 = d1 - sigma * np.sqrt(T)\n",
" return x * stats.norm.cdf(d1) - K * np.exp(-r * T) * stats.norm.cdf(d2)\n",
" \n",
"\n",
"\n",
"def compute_payoff_BS():\n",
" \"\"\"Calcul du prix d'une option d'achat Up-and-Out selon le modèle de Black-Scholes en fonction de la barrière.\n",
" \n",
"\n",
" Retourne:\n",
" float: Le prix de l'option d'achat Up-and-Out.\n",
" \"\"\"\n",
" lam = (r + 0.5 * sigma**2) / sigma**2\n",
" return call_BS(S0) - call_BS(S0) * (S0/B)**(2 * lam) + (S0/B)**(lam - 1) * (call_BS(B**2/S0) - (S0/B)**2 * call_BS(B**2/S0))\n",
" \n",
" return (\n",
" call_BS(S0)\n",
" - call_BS(S0) * (S0 / B) ** (2 * lam)\n",
" + (S0 / B) ** (lam - 1)\n",
" * (call_BS(B**2 / S0) - (S0 / B) ** 2 * call_BS(B**2 / S0))\n",
" )\n",
"\n",
"\n",
"def compute_payoff(trajectories, B, K):\n",
" \"\"\"Calcule le payoff d'une option en fonction des trajectoires.\n",
" \n",
"\n",
" Paramètres:\n",
" trajectories (list of tuples): Liste des trajectoires avec le temps et les valeurs.\n",
" B (float): Valeur de la barrière.\n",
" K (float): Prix d'exercice de l'option.\n",
" \n",
"\n",
" Retourne:\n",
" float: Valeur du payoff de l'option.\n",
" \"\"\"\n",
@@ -390,7 +409,13 @@
],
"source": [
"N_trajectories = 1000\n",
"trajectories = [(t, X) for (t, X) in [euler_maruyama(mu=r, sigma=sigma, T=T, N=1000, X0=S0) for _ in range(N_trajectories)]]\n",
"trajectories = [\n",
" (t, X)\n",
" for (t, X) in [\n",
" euler_maruyama(mu=r, sigma=sigma, T=T, N=1000, X0=S0)\n",
" for _ in range(N_trajectories)\n",
" ]\n",
"]\n",
"plt.figure(figsize=(10, 6))\n",
"plot_browniens(trajectories, B=B)\n",
"plt.show()\n",
@@ -431,28 +456,35 @@
"\n",
"np.random.seed(333)\n",
"\n",
"\n",
"def plot_payoff_errors():\n",
" \"\"\"Trace l'erreur de convergence du payoff actualisé en fonction de N.\"\"\"\n",
" errors = []\n",
" \n",
"\n",
" for N in N_list:\n",
" trajectories = [(t, X) for (t, X) in [euler_maruyama(mu=r, sigma=sigma, T=T, N=N, X0=S0) for _ in range(N_trajectories)]]\n",
" trajectories = [\n",
" (t, X)\n",
" for (t, X) in [\n",
" euler_maruyama(mu=r, sigma=sigma, T=T, N=N, X0=S0)\n",
" for _ in range(N_trajectories)\n",
" ]\n",
" ]\n",
" payoff_BS = compute_payoff_BS()\n",
" payoffs = compute_payoff(trajectories, B, K)\n",
" \n",
"\n",
" errors.append(np.max(np.abs(payoffs - payoff_BS)))\n",
" \n",
" \n",
" plt.plot(np.log(N_list), np.log(errors), 'o-', label='Erreur numérique')\n",
" plt.plot(np.log(N_list), 0.5 * np.log(N_list), '--', label='Ordre 1/2')\n",
" plt.plot(np.log(N_list), np.log(N_list), '--', label='Ordre 1')\n",
" plt.plot(np.log(N_list), 2*np.log(N_list), '--', label='Ordre 2')\n",
" plt.xlabel('log(h)')\n",
" plt.ylabel('log(Erreur)')\n",
" plt.title('Convergence de l\\'erreur du payoff actualisé')\n",
"\n",
" plt.plot(np.log(N_list), np.log(errors), \"o-\", label=\"Erreur numérique\")\n",
" plt.plot(np.log(N_list), 0.5 * np.log(N_list), \"--\", label=\"Ordre 1/2\")\n",
" plt.plot(np.log(N_list), np.log(N_list), \"--\", label=\"Ordre 1\")\n",
" plt.plot(np.log(N_list), 2 * np.log(N_list), \"--\", label=\"Ordre 2\")\n",
" plt.xlabel(\"log(h)\")\n",
" plt.ylabel(\"log(Erreur)\")\n",
" plt.title(\"Convergence de l'erreur du payoff actualisé\")\n",
" plt.legend()\n",
" plt.grid(True)\n",
"\n",
"\n",
"plt.figure(figsize=(10, 5))\n",
"plot_payoff_errors()\n",
"plt.show()"