{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e637deae-9168-4fb2-b95f-4e42d8d72d9e",
   "metadata": {},
   "source": [
    "# DATA COLLECTION "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f8508d94-74a7-4bb0-8b81-c2e06850c25f",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd \n",
    "chemin_fichier = \"s3://projet-bdc-data/carmignac/AUM ENSAE V2 -20251105.csv\"\n",
    "df_aum2 = pd.read_csv(chemin_fichier, sep=';', engine='python')\n",
    "df_aum2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4644da13-5aea-4ca0-9fcf-947324766292",
   "metadata": {},
   "outputs": [],
   "source": [
    "chemin_fichier = \"s3://projet-bdc-data/carmignac/Flows ENSAE V2 -20251105.csv\"\n",
    "df_flows2 = pd.read_csv(chemin_fichier, sep=';', engine='python')\n",
    "df_flows2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "59d31eaf-c06c-4ebe-9f8c-cb9158a50976",
   "metadata": {},
   "source": [
    "## DATA ANALYSIS"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5773b911-6b84-448d-962f-8228eeac0250",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_aum2.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6f571810-c373-4d30-8ca5-c3a074b95b08",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_aum2.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "af25fd07-a613-4adc-b88b-93a8d300379c",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_flows2.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c6d0fe83-2957-430b-89cf-cd30833b7cab",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_flows2.columns"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4ce2ad22-08e6-4e63-96b2-c2301172516e",
   "metadata": {},
   "source": [
    "# ETUDE ET ANALYSE DES ANOMALIES"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c883dfc2-b9b9-4d3e-80d3-0140cd222492",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_aum2['Centralisation Date'] = pd.to_datetime(df_aum2['Centralisation Date'])\n",
    "df_flows2['Centralisation Date'] = pd.to_datetime(df_flows2['Centralisation Date'])\n",
    "\n",
    "\n",
    "key_cols = ['Registrar Account - ID', 'Product - Isin']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "64ca9883-372b-4a5e-8fc1-10e5d835c411",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Vérifier doublons exacts\n",
    "doublons_aum = df_aum2[df_aum2.duplicated(subset=key_cols + ['Centralisation Date'], keep=False)]\n",
    "doublons_flows = df_flows2[df_flows2.duplicated(subset=key_cols + ['Centralisation Date'], keep=False)]\n",
    "\n",
    "print(\"Doublons AUM:\", doublons_aum.shape[0])\n",
    "print(\"Doublons Flows:\", doublons_flows.shape[0])\n",
    "\n",
    "#same date, code isin du produit, et account ==> revoir les autres caracteristiques "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f47b276d-cce6-433c-87c5-860810d71d34",
   "metadata": {},
   "outputs": [],
   "source": [
    "cols= ['Company - Id', 'Company - Ultimate Parent Id',\n",
    "       'Registrar Account - ID', 'Registrar Account - Region','Product - Isin']\n",
    "\n",
    "doublons_aum2 = df_aum2[df_aum2.duplicated(subset=cols + ['Centralisation Date'], keep=False)]\n",
    "doublons_flows2 = df_flows2[df_flows2.duplicated(subset=cols + ['Centralisation Date'], keep=False)]\n",
    "\n",
    "print(\" Cols: \", cols)\n",
    "print(\"Doublons AUM:\", doublons_aum2.shape[0])\n",
    "print(\"Doublons Flows:\", doublons_flows2.shape[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8df98c34-b1f7-4fb9-bbc7-c9bbe762022a",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = df_flows2.copy()\n",
    "df[\"Date\"] = pd.to_datetime(df[\"Centralisation Date\"])\n",
    "\n",
    "# Groupby par ISIN et Date\n",
    "grouped = df.groupby([\"Product - Isin\", \"Date\"])\n",
    "\n",
    "transfers = []\n",
    "\n",
    "for (isin, date), group in grouped:\n",
    "    # Sépare flux positifs et négatifs\n",
    "    entrants = group[group[\"Value € - NetFlows\"] > 0][[\"Registrar Account - ID\", \"Value € - NetFlows\"]]\n",
    "    sortants = group[group[\"Value € - NetFlows\"] < 0][[\"Registrar Account - ID\", \"Value € - NetFlows\"]]\n",
    "\n",
    "    # On cherche des paires +M / -M\n",
    "    for _, row_sortie in sortants.iterrows():\n",
    "        montant_sortie = row_sortie[\"Value € - NetFlows\"]\n",
    "        compte_sortant = row_sortie[\"Registrar Account - ID\"]\n",
    "\n",
    "        # Chercher un +M qui matche exactement le -M\n",
    "        match = entrants[entrants[\"Value € - NetFlows\"] == -montant_sortie]\n",
    "\n",
    "        if len(match) > 0:\n",
    "            for _, row_entree in match.iterrows():\n",
    "                transfers.append({\n",
    "                    \"ISIN\": isin,\n",
    "                    \"Date\": date,\n",
    "                    \"Compte sortant\": compte_sortant,\n",
    "                    \"Montant sortie\": montant_sortie,\n",
    "                    \"Compte entrant\": row_entree[\"Registrar Account - ID\"],\n",
    "                    \"Montant entrée\": row_entree[\"Value € - NetFlows\"]\n",
    "                })\n",
    "\n",
    "\n",
    "transf_compte = pd.DataFrame(transfers)\n",
    "transf_compte"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c898b0c5-0a8e-4640-bc52-9490ee80e53d",
   "metadata": {},
   "source": [
    "# MERGE AND ANALYSIS"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "ce33dbf8-1c59-416a-adc4-6eb7c1ea9d8e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Merged dataset:\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "\n",
    "df_aum2 = df_aum2.rename(columns={\n",
    "    \"Registrar Account - ID\": \"Account_ID\",\n",
    "    \"Product - Isin\": \"ISIN\",\n",
    "    \"Centralisation Date\": \"Date\",\n",
    "    \"Value - AUM €\": \"AUM_EUR\"\n",
    "})\n",
    "\n",
    "df_flows2 = df_flows2.rename(columns={\n",
    "    \"Registrar Account - ID\": \"Account_ID\",\n",
    "    \"Product - Isin\": \"ISIN\",\n",
    "    \"Centralisation Date\": \"Date\",\n",
    "    \"Value € - NetFlows\": \"Flow_EUR\"\n",
    "})\n",
    "\n",
    "\n",
    "df_aum2[\"Date\"] = pd.to_datetime(df_aum2[\"Date\"])\n",
    "df_flows2[\"Date\"] = pd.to_datetime(df_flows2[\"Date\"])\n",
    "\n",
    "df_aum2[\"Account_ID\"] = df_aum2[\"Account_ID\"].astype(str)\n",
    "df_flows2[\"Account_ID\"] = df_flows2[\"Account_ID\"].astype(str)\n",
    "\n",
    "df_aum2[\"ISIN\"] = df_aum2[\"ISIN\"].str.upper()\n",
    "df_flows2[\"ISIN\"] = df_flows2[\"ISIN\"].str.upper()\n",
    "\n",
    "\n",
    "df_merged = pd.merge(\n",
    "    df_aum2[[\"Account_ID\", \"ISIN\", \"Date\", \"AUM_EUR\"]],\n",
    "    df_flows2[[\"Account_ID\", \"ISIN\", \"Date\", \"Flow_EUR\"]],\n",
    "    on=[\"Account_ID\", \"ISIN\", \"Date\"],\n",
    "    how=\"outer\"\n",
    ").sort_values([\"Account_ID\", \"ISIN\", \"Date\"])\n",
    "\n",
    "print(\"Merged dataset:\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "7e5d642e-5c16-4c78-8d83-075094902670",
   "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>Account_ID</th>\n",
       "      <th>ISIN</th>\n",
       "      <th>Date</th>\n",
       "      <th>AUM_EUR</th>\n",
       "      <th>Flow_EUR</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>100000014</td>\n",
       "      <td>LU0553415323</td>\n",
       "      <td>2015-01-31</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>100000014</td>\n",
       "      <td>LU0553415323</td>\n",
       "      <td>2015-02-28</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>100000014</td>\n",
       "      <td>LU0553415323</td>\n",
       "      <td>2015-03-31</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>100000014</td>\n",
       "      <td>LU0553415323</td>\n",
       "      <td>2015-04-30</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>100000014</td>\n",
       "      <td>LU0553415323</td>\n",
       "      <td>2015-05-31</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7369446</th>\n",
       "      <td>Private Client</td>\n",
       "      <td>LU2809794220</td>\n",
       "      <td>2025-10-30</td>\n",
       "      <td>NaN</td>\n",
       "      <td>-1623.71</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7369447</th>\n",
       "      <td>Private Client</td>\n",
       "      <td>LU2809794220</td>\n",
       "      <td>2025-10-31</td>\n",
       "      <td>4.438147e+06</td>\n",
       "      <td>4946.23</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7369448</th>\n",
       "      <td>Private Client</td>\n",
       "      <td>LU2809794576</td>\n",
       "      <td>2025-09-23</td>\n",
       "      <td>NaN</td>\n",
       "      <td>71660.14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7369449</th>\n",
       "      <td>Private Client</td>\n",
       "      <td>LU2809794576</td>\n",
       "      <td>2025-09-30</td>\n",
       "      <td>7.094499e+04</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7369450</th>\n",
       "      <td>Private Client</td>\n",
       "      <td>LU2809794576</td>\n",
       "      <td>2025-10-31</td>\n",
       "      <td>7.871629e+04</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>7369451 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "             Account_ID          ISIN       Date       AUM_EUR  Flow_EUR\n",
       "0             100000014  LU0553415323 2015-01-31  0.000000e+00       NaN\n",
       "1             100000014  LU0553415323 2015-02-28  0.000000e+00       NaN\n",
       "2             100000014  LU0553415323 2015-03-31  0.000000e+00       NaN\n",
       "3             100000014  LU0553415323 2015-04-30  0.000000e+00       NaN\n",
       "4             100000014  LU0553415323 2015-05-31  0.000000e+00       NaN\n",
       "...                 ...           ...        ...           ...       ...\n",
       "7369446  Private Client  LU2809794220 2025-10-30           NaN  -1623.71\n",
       "7369447  Private Client  LU2809794220 2025-10-31  4.438147e+06   4946.23\n",
       "7369448  Private Client  LU2809794576 2025-09-23           NaN  71660.14\n",
       "7369449  Private Client  LU2809794576 2025-09-30  7.094499e+04       NaN\n",
       "7369450  Private Client  LU2809794576 2025-10-31  7.871629e+04       NaN\n",
       "\n",
       "[7369451 rows x 5 columns]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_merged"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "e484a3df-260e-4532-850b-3917592fcfdf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Date   2025-11-28\n",
       "dtype: datetime64[ns]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_merged[['Date']].max() #2015-01-02 jusuqua 2025-11-28"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "ea14866a-1ce6-4b19-9225-d725304af8ec",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 31376 characters]",
      "text/plain": [
       "<Figure size 1000x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 2. HISTOGRAMME DES AUM (SANS ISIN)\n",
    "# We keep the mean AUM per Account \n",
    "aum_by_account = (\n",
    "    df_merged.groupby(\"Account_ID\")[\"AUM_EUR\"]\n",
    "    .mean()\n",
    "    .dropna()\n",
    ")\n",
    "plt.figure(figsize=(10,6))\n",
    "plt.hist(aum_by_account, bins=100)\n",
    "# ZOOM sur les petits AUM (0 à 0.5M €)\n",
    "plt.xlim(0, 0.5e7)\n",
    "plt.xlabel(\"Mean AUM value (€)\")\n",
    "plt.ylabel(\"Number of client accounts\")\n",
    "plt.title(\"Distribution of client average AUM \")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "8d0dec63-9b9a-47ea-90bd-ffe059bb232a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 36304 characters]",
      "text/plain": [
       "<Figure size 1000x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 2. HISTOGRAMME DES AUM (SANS ISIN) POUR 2022+\n",
    "df_filtered = df_merged[df_merged[\"Date\"].dt.year >= 2022]\n",
    "\n",
    "aum_by_account = (\n",
    "    df_filtered\n",
    "    .groupby(\"Account_ID\")[\"AUM_EUR\"]\n",
    "    .mean()\n",
    "    .dropna()\n",
    ")\n",
    "\n",
    "plt.figure(figsize=(10,6))\n",
    "plt.hist(aum_by_account, bins=100)\n",
    "\n",
    "# ZOOM sur les petits AUM (0 à 5M €)\n",
    "plt.xlim(0, 0.5e7)\n",
    "\n",
    "plt.xlabel(\"Mean AUM value (€)\")\n",
    "plt.ylabel(\"Number of client accounts\")\n",
    "plt.title(\"Distribution of client average AUM per account (2022–2025)\")\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "f13c213e-7f72-494a-bcf0-b4cd9feee55d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Monthly flow summary:\n",
      "           n_positive_flows  n_negative_flows\n",
      "YearMonth                                    \n",
      "2015-01               10688             11779\n",
      "2015-02               12905             10822\n",
      "2015-03               15497             13207\n",
      "2015-04               13838             11641\n",
      "2015-05               11143              9065\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 62184 characters]",
      "text/plain": [
       "<Figure size 1400x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# ANALYSE MENSUELLE DES FLOWS (ENTRANTS / SORTANTS)\n",
    "\n",
    "df_merged[\"YearMonth\"] = df_merged[\"Date\"].dt.to_period(\"M\")\n",
    "\n",
    "flows_monthly = df_merged.groupby(\"YearMonth\").agg(\n",
    "    n_positive_flows=(\"Flow_EUR\", lambda x: (x > 0).sum()),\n",
    "    n_negative_flows=(\"Flow_EUR\", lambda x: (x < 0).sum()),\n",
    ")\n",
    "\n",
    "print(\"Monthly flow summary:\")\n",
    "print(flows_monthly.head())\n",
    "\n",
    "flows_monthly.index = flows_monthly.index.astype(str)\n",
    "plt.figure(figsize=(14,6))\n",
    "plt.bar(flows_monthly.index, flows_monthly[\"n_positive_flows\"], label=\"Positive flows (inflows)\", alpha=0.7)\n",
    "plt.bar(flows_monthly.index, flows_monthly[\"n_negative_flows\"], label=\"Negative flows (outflows)\", alpha=0.7)\n",
    "plt.xticks(rotation=90)\n",
    "plt.xlabel(\"Year-Month\")\n",
    "plt.ylabel(\"Number of accounts with flows\")\n",
    "plt.title(\"Monthly number of accounts with inflows vs outflows\")\n",
    "plt.legend()\n",
    "plt.tight_layout()\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "4af0e205-a304-4b10-be1d-de69904aa0da",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 130072 characters]",
      "text/plain": [
       "<Figure size 1400x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "df_merged[\"YearMonth\"] = df_merged[\"Date\"].dt.to_period(\"M\")\n",
    "\n",
    "monthly_flow_amounts = df_merged.groupby(\"YearMonth\").agg(\n",
    "    avg_positive_flow=(\"Flow_EUR\", lambda x: x[x > 0].mean()),\n",
    "    avg_negative_flow=(\"Flow_EUR\", lambda x: x[x < 0].mean())\n",
    ")\n",
    "\n",
    "monthly_flow_amounts.index = monthly_flow_amounts.index.astype(str)\n",
    "\n",
    "plt.figure(figsize=(14,6))\n",
    "plt.plot(monthly_flow_amounts.index, monthly_flow_amounts[\"avg_positive_flow\"], label=\"Average inflow (positive)\", marker=\"o\")\n",
    "plt.plot(monthly_flow_amounts.index, monthly_flow_amounts[\"avg_negative_flow\"], label=\"Average outflow (negative)\", marker=\"o\")\n",
    "plt.xticks(rotation=90)\n",
    "plt.xlabel(\"Year-Month\")\n",
    "plt.ylabel(\"Average flow (€)\")\n",
    "plt.title(\"Monthly average inflows and outflows\")\n",
    "plt.legend()\n",
    "plt.tight_layout()\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "2d548152-196f-456a-b21e-4d05e39d03c5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 55216 characters]",
      "text/plain": [
       "<Figure size 1400x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "df = df_merged.copy()\n",
    "df[\"YearMonth\"] = df[\"Date\"].dt.to_period(\"M\")\n",
    "\n",
    "# 1) Flow mensuel total par client\n",
    "monthly_client_flow = (\n",
    "    df.groupby([\"Account_ID\", \"YearMonth\"])[\"Flow_EUR\"]\n",
    "    .sum()\n",
    "    .reset_index()\n",
    ")\n",
    "\n",
    "# 2) Calcul des seuils de quantiles (tu peux ajuster)\n",
    "q1 = monthly_client_flow[\"Flow_EUR\"].quantile(0.33)\n",
    "q2 = monthly_client_flow[\"Flow_EUR\"].quantile(0.66)\n",
    "\n",
    "# 3) Catégorisation des clients\n",
    "def categorize(flow):\n",
    "    if flow <= q1:\n",
    "        return \"Petits flows\"\n",
    "    elif flow <= q2:\n",
    "        return \"Flows moyens\"\n",
    "    else:\n",
    "        return \"Grands flows\"\n",
    "\n",
    "monthly_client_flow[\"Flow_category\"] = monthly_client_flow[\"Flow_EUR\"].apply(categorize)\n",
    "\n",
    "# 4) Nombre de clients dans chaque catégorie par mois\n",
    "category_counts = (\n",
    "    monthly_client_flow.groupby([\"YearMonth\", \"Flow_category\"])\n",
    "    .size()\n",
    "    .unstack(fill_value=0)\n",
    ")\n",
    "\n",
    "category_counts.index = category_counts.index.astype(str)\n",
    "\n",
    "# 5) Plot\n",
    "category_counts.plot(kind=\"bar\", stacked=True, figsize=(14,6))\n",
    "plt.title(\"Répartition des clients par niveau de flux mensuel\")\n",
    "plt.xlabel(\"Mois\")\n",
    "plt.ylabel(\"Nombre de clients\")\n",
    "plt.xticks(rotation=90)\n",
    "plt.tight_layout()\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "2c5a83e0-1faf-4745-88b3-c0fb363317fc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Répartition des comptes par catégorie :\n",
      "Grands flows : YearMonth\n",
      "2015-01    432\n",
      "2015-02    499\n",
      "2015-03    536\n",
      "2015-04    530\n",
      "2015-05    472\n",
      "          ... \n",
      "2025-07    412\n",
      "2025-08    388\n",
      "2025-09    407\n",
      "2025-10    418\n",
      "2025-11    231\n",
      "Name: Grands flows, Length: 131, dtype: int64 comptes\n",
      "Petits flows : YearMonth\n",
      "2015-01    7429\n",
      "2015-02    7380\n",
      "2015-03    7390\n",
      "2015-04    7416\n",
      "2015-05    7493\n",
      "           ... \n",
      "2025-07    3474\n",
      "2025-08    3507\n",
      "2025-09    3509\n",
      "2025-10    3529\n",
      "2025-11     174\n",
      "Name: Petits flows, Length: 131, dtype: int64 comptes\n"
     ]
    }
   ],
   "source": [
    "print(\"Répartition des comptes par catégorie :\")\n",
    "for cat, count in category_counts.items():\n",
    "    print(f\"{cat} : {count} comptes\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "943b359e-b1c0-435e-943b-94167f8c566f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Moyenne du nombre de comptes par catégorie (sur toute la période) :\n",
      "Grands flows : 334 comptes en moyenne par mois\n",
      "Petits flows : 6736 comptes en moyenne par mois\n"
     ]
    }
   ],
   "source": [
    "# Nombre de comptes dans chaque catégorie par mois\n",
    "category_counts_by_month = (\n",
    "    monthly_client_flow.groupby([\"YearMonth\", \"Flow_category\"])\n",
    "    .size()\n",
    "    .unstack(fill_value=0)\n",
    ")\n",
    "\n",
    "# Moyenne sur tous les mois\n",
    "category_counts_mean = category_counts_by_month.mean()\n",
    "print(\"Moyenne du nombre de comptes par catégorie (sur toute la période) :\")\n",
    "for cat, mean_val in category_counts_mean.items():\n",
    "    print(f\"{cat} : {mean_val:.0f} comptes en moyenne par mois\")\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
