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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fabeb46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d63d91d0",
   "metadata": {},
   "source": [
    "# Analyse de la dérive des données (Data Drift)\n",
    "\n",
    "Dans le cadre du déploiement du modèle de scoring crédit, nous mettons en place\n",
    "une analyse de dérive entre :\n",
    "\n",
    "- un **jeu de référence** (période stable / historique)\n",
    "- un **jeu courant** (période récente, proxy “production”)\n",
    "\n",
    "Ici, les jeux `ref_data.csv` et `prod_data.csv` sont issus des **logs de prédiction**\n",
    "(voir scripts de génération dans `monitoring/`).\n",
    "\n",
    "Objectif :\n",
    "détecter une dérive statistique susceptible d’impacter les performances du modèle."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "095ccab3",
   "metadata": {},
   "source": [
    "## Méthodologie\n",
    "\n",
    "Nous comparons la distribution de la variable de sortie du modèle\n",
    "(`probability_default`) entre :\n",
    "\n",
    "- un jeu de données de **référence** (début de l’historique)\n",
    "- un jeu de données **courant** (données plus récentes)\n",
    "\n",
    "Le test statistique utilisé est le **test de Kolmogorov–Smirnov (KS)** :\n",
    "\n",
    "- $H_0$ : les deux distributions sont identiques\n",
    "- $H_1$ : les distributions sont différentes\n",
    "\n",
    "Seuil de décision : **p-value < 0.05** → drift détecté.\n",
    "\n",
    "Remarque : on teste ici uniquement la **sortie du modèle** (drift des prédictions).\n",
    "En production, on complète généralement par une analyse de drift sur les features d’entrée."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1d326430",
   "metadata": {},
   "source": [
    "## Imports"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "7e01244e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from scipy.stats import ks_2samp"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "af52d879",
   "metadata": {},
   "source": [
    "## Chargement des données"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "d445c3ab",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(   probability_default\n",
       " 0               0.0405\n",
       " 1               0.0405\n",
       " 2               0.0229\n",
       " 3               0.0202,\n",
       "    probability_default\n",
       " 0               0.2363\n",
       " 1               0.0422\n",
       " 2               0.2363\n",
       " 3               0.2504\n",
       " 4               0.2391)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ref_path = \"./ref_data.csv\"\n",
    "prod_path = \"./prod_data.csv\"\n",
    "\n",
    "ref_df = pd.read_csv(ref_path)\n",
    "prod_df = pd.read_csv(prod_path)\n",
    "\n",
    "ref_df.head(), prod_df.head()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bd8ab615",
   "metadata": {},
   "source": [
    "## Sélection de la variable analysée"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "19846395",
   "metadata": {},
   "outputs": [],
   "source": [
    "feature = \"probability_default\"\n",
    "\n",
    "ref_values = ref_df[feature]\n",
    "prod_values = prod_df[feature]\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e737c5e1",
   "metadata": {},
   "source": [
    "## Test de dérive (KS)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "d6038d6d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "📊 Résultats du test KS\n",
      "KS statistic : 1.0000\n",
      "P-value      : 0.015873\n",
      "⚠️ Dérive détectée\n"
     ]
    }
   ],
   "source": [
    "ks_stat, p_value = ks_2samp(ref_values, prod_values)\n",
    "\n",
    "print(\"📊 Résultats du test KS\")\n",
    "print(f\"KS statistic : {ks_stat:.4f}\")\n",
    "print(f\"P-value      : {p_value:.6f}\")\n",
    "\n",
    "if p_value < 0.05:\n",
    "    print(\"⚠️ Dérive détectée\")\n",
    "else:\n",
    "    print(\"✅ Pas de dérive détectée\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bdb19396",
   "metadata": {},
   "source": [
    "## Statistiques descriptives"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "d8df5ed4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Référence</th>\n",
       "      <th>Production</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>4.000000</td>\n",
       "      <td>5.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>0.031025</td>\n",
       "      <td>0.200860</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>0.010996</td>\n",
       "      <td>0.088884</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>0.020200</td>\n",
       "      <td>0.042200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>0.022225</td>\n",
       "      <td>0.236300</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>0.031700</td>\n",
       "      <td>0.236300</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>0.040500</td>\n",
       "      <td>0.239100</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>0.040500</td>\n",
       "      <td>0.250400</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       Référence  Production\n",
       "count   4.000000    5.000000\n",
       "mean    0.031025    0.200860\n",
       "std     0.010996    0.088884\n",
       "min     0.020200    0.042200\n",
       "25%     0.022225    0.236300\n",
       "50%     0.031700    0.236300\n",
       "75%     0.040500    0.239100\n",
       "max     0.040500    0.250400"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "summary = pd.DataFrame({\n",
    "    \"Référence\": ref_values.describe(),\n",
    "    \"Production\": prod_values.describe()\n",
    "})\n",
    "\n",
    "summary\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b5937fcb",
   "metadata": {},
   "source": [
    "## Interprétation des résultats\n",
    "\n",
    "Le test KS indique une **dérive statistiquement significative** entre la référence et le courant :\n",
    "la p-value est **< 0.05**, donc on rejette $H_0$ et on conclut que les distributions sont différentes.\n",
    "\n",
    "Les statistiques descriptives confirment une différence de niveau : la moyenne en production est nettement\n",
    "plus élevée que la moyenne de référence sur cet échantillon.\n",
    "\n",
    "Conclusion (prototype) :\n",
    "- Drift détecté → **à investiguer** (qualité des données, changement de population, changement de process).\n",
    "- Actions typiques : analyser le drift des features, monitorer dans le temps, et envisager réentraînement\n",
    "  si la dérive persiste et se traduit par une baisse de performance.\n",
    "\n",
    "Note importante : les tailles d’échantillon sont faibles ici (quelques lignes), donc l’interprétation doit\n",
    "rester prudente — l’objectif est surtout de démontrer la chaîne de monitoring de bout en bout."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9ea29e10",
   "metadata": {},
   "source": [
    "## Limites et perspectives\n",
    "\n",
    "Limites :\n",
    "- Les données “production” sont un proxy basé sur les logs, pas un flux temps réel.\n",
    "- L’analyse porte uniquement sur la **sortie du modèle** (pas les features).\n",
    "- L’échantillon est petit : les tests peuvent être instables et doivent être confirmés sur plus de données.\n",
    "\n",
    "Perspectives :\n",
    "- Suivi temporel (fenêtres glissantes, comparaison semaine N vs N-1).\n",
    "- Seuils d’alerte automatiques + génération de rapports (ex: Evidently).\n",
    "- Analyse multi-features (drift des entrées + qualité des données).\n",
    "- Réentraînement conditionnel si drift + dégradation de performance."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "467869c1",
   "metadata": {},
   "source": [
    "## Rapport Evidently (HTML)\n",
    "\n",
    "Un rapport Evidently peut être généré via `monitoring/generate_evidently_report.py`\n",
    "et sauvegardé dans `monitoring/reports/drift_report.html`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "8daaad77",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "        <iframe\n",
       "            width=\"100%\"\n",
       "            height=\"800\"\n",
       "            src=\"reports\\drift_report.html\"\n",
       "            frameborder=\"0\"\n",
       "            allowfullscreen\n",
       "            \n",
       "        ></iframe>\n",
       "        "
      ],
      "text/plain": [
       "<IPython.lib.display.IFrame at 0x281cc01af90>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from pathlib import Path\n",
    "from IPython.display import IFrame, display\n",
    "\n",
    "report_path = Path(\"./reports/drift_report.html\")\n",
    "if report_path.exists():\n",
    "    display(IFrame(src=str(report_path), width=\"100%\", height=800))\n",
    "else:\n",
    "    print(\"Rapport introuvable. Génère-le avec: python monitoring/generate_evidently_report.py\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "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.13.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}