{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e637deae-9168-4fb2-b95f-4e42d8d72d9e",
   "metadata": {},
   "source": [
    "# DATA COLLECTION "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f8508d94-74a7-4bb0-8b81-c2e06850c25f",
   "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>Agreement - Code</th>\n",
       "      <th>Company - Id</th>\n",
       "      <th>Company - Ultimate Parent Id</th>\n",
       "      <th>Registrar Account - ID</th>\n",
       "      <th>Registrar Account - Region</th>\n",
       "      <th>RegistrarAccount - Country</th>\n",
       "      <th>Product - Asset Type</th>\n",
       "      <th>Product - Strategy</th>\n",
       "      <th>Product - Legal Status</th>\n",
       "      <th>Product - Is Dedie ?</th>\n",
       "      <th>Product - Fund</th>\n",
       "      <th>Product - Shareclass Type</th>\n",
       "      <th>Product - Shareclass Currency</th>\n",
       "      <th>Product - Isin</th>\n",
       "      <th>Centralisation Date</th>\n",
       "      <th>Quantity - AUM</th>\n",
       "      <th>Value - AUM CCY</th>\n",
       "      <th>Value - AUM €</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>003</td>\n",
       "      <td>166</td>\n",
       "      <td>166</td>\n",
       "      <td>200000647</td>\n",
       "      <td>France</td>\n",
       "      <td>France</td>\n",
       "      <td>Diversified</td>\n",
       "      <td>Patrimoine</td>\n",
       "      <td>FCP</td>\n",
       "      <td>NO</td>\n",
       "      <td>Carmignac Patrimoine</td>\n",
       "      <td>A</td>\n",
       "      <td>EUR</td>\n",
       "      <td>FR0010135103</td>\n",
       "      <td>2015-03-31</td>\n",
       "      <td>35.368</td>\n",
       "      <td>2.464867e+04</td>\n",
       "      <td>2.464867e+04</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>003</td>\n",
       "      <td>166</td>\n",
       "      <td>166</td>\n",
       "      <td>200000647</td>\n",
       "      <td>France</td>\n",
       "      <td>France</td>\n",
       "      <td>Diversified</td>\n",
       "      <td>Patrimoine</td>\n",
       "      <td>FCP</td>\n",
       "      <td>NO</td>\n",
       "      <td>Carmignac Patrimoine</td>\n",
       "      <td>A</td>\n",
       "      <td>EUR</td>\n",
       "      <td>FR0010135103</td>\n",
       "      <td>2015-11-30</td>\n",
       "      <td>35.368</td>\n",
       "      <td>2.241306e+04</td>\n",
       "      <td>2.241306e+04</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>003</td>\n",
       "      <td>166</td>\n",
       "      <td>166</td>\n",
       "      <td>200000647</td>\n",
       "      <td>France</td>\n",
       "      <td>France</td>\n",
       "      <td>Diversified</td>\n",
       "      <td>Patrimoine</td>\n",
       "      <td>FCP</td>\n",
       "      <td>NO</td>\n",
       "      <td>Carmignac Patrimoine</td>\n",
       "      <td>A</td>\n",
       "      <td>EUR</td>\n",
       "      <td>FR0010135103</td>\n",
       "      <td>2015-12-31</td>\n",
       "      <td>35.368</td>\n",
       "      <td>2.205124e+04</td>\n",
       "      <td>2.205124e+04</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>003</td>\n",
       "      <td>166</td>\n",
       "      <td>166</td>\n",
       "      <td>200000647</td>\n",
       "      <td>France</td>\n",
       "      <td>France</td>\n",
       "      <td>Diversified</td>\n",
       "      <td>Patrimoine</td>\n",
       "      <td>FCP</td>\n",
       "      <td>NO</td>\n",
       "      <td>Carmignac Patrimoine</td>\n",
       "      <td>A</td>\n",
       "      <td>EUR</td>\n",
       "      <td>FR0010135103</td>\n",
       "      <td>2016-03-31</td>\n",
       "      <td>35.368</td>\n",
       "      <td>2.162612e+04</td>\n",
       "      <td>2.162612e+04</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>003</td>\n",
       "      <td>166</td>\n",
       "      <td>166</td>\n",
       "      <td>200000647</td>\n",
       "      <td>France</td>\n",
       "      <td>France</td>\n",
       "      <td>Diversified</td>\n",
       "      <td>Patrimoine</td>\n",
       "      <td>FCP</td>\n",
       "      <td>NO</td>\n",
       "      <td>Carmignac Patrimoine</td>\n",
       "      <td>A</td>\n",
       "      <td>EUR</td>\n",
       "      <td>FR0010135103</td>\n",
       "      <td>2016-11-30</td>\n",
       "      <td>35.368</td>\n",
       "      <td>2.248945e+04</td>\n",
       "      <td>2.248945e+04</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4880292</th>\n",
       "      <td>Private Client</td>\n",
       "      <td>Private Client</td>\n",
       "      <td>Private Client</td>\n",
       "      <td>Private Client</td>\n",
       "      <td>Switzerland</td>\n",
       "      <td>Switzerland</td>\n",
       "      <td>Fixed Income</td>\n",
       "      <td>Sécurité</td>\n",
       "      <td>SICAV</td>\n",
       "      <td>NO</td>\n",
       "      <td>Carmignac Portfolio Sécurité</td>\n",
       "      <td>AW &amp; AW-R</td>\n",
       "      <td>EUR</td>\n",
       "      <td>LU1299306321</td>\n",
       "      <td>2020-02-29</td>\n",
       "      <td>26801.000</td>\n",
       "      <td>2.740670e+06</td>\n",
       "      <td>2.740670e+06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4880293</th>\n",
       "      <td>Private Client</td>\n",
       "      <td>Private Client</td>\n",
       "      <td>Private Client</td>\n",
       "      <td>Private Client</td>\n",
       "      <td>Switzerland</td>\n",
       "      <td>Switzerland</td>\n",
       "      <td>Fixed Income</td>\n",
       "      <td>Sécurité</td>\n",
       "      <td>SICAV</td>\n",
       "      <td>NO</td>\n",
       "      <td>Carmignac Portfolio Sécurité</td>\n",
       "      <td>AW &amp; AW-R</td>\n",
       "      <td>EUR</td>\n",
       "      <td>LU1299306321</td>\n",
       "      <td>2020-06-30</td>\n",
       "      <td>3099.000</td>\n",
       "      <td>3.122862e+05</td>\n",
       "      <td>3.122862e+05</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4880294</th>\n",
       "      <td>Private Client</td>\n",
       "      <td>Private Client</td>\n",
       "      <td>Private Client</td>\n",
       "      <td>Private Client</td>\n",
       "      <td>Switzerland</td>\n",
       "      <td>Switzerland</td>\n",
       "      <td>Fixed Income</td>\n",
       "      <td>Sécurité</td>\n",
       "      <td>SICAV</td>\n",
       "      <td>NO</td>\n",
       "      <td>Carmignac Portfolio Sécurité</td>\n",
       "      <td>AW &amp; AW-R</td>\n",
       "      <td>EUR</td>\n",
       "      <td>LU1299306321</td>\n",
       "      <td>2020-10-31</td>\n",
       "      <td>3099.000</td>\n",
       "      <td>3.184222e+05</td>\n",
       "      <td>3.184222e+05</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4880295</th>\n",
       "      <td>Private Client</td>\n",
       "      <td>Private Client</td>\n",
       "      <td>Private Client</td>\n",
       "      <td>Private Client</td>\n",
       "      <td>Switzerland</td>\n",
       "      <td>Switzerland</td>\n",
       "      <td>Fixed Income</td>\n",
       "      <td>Sécurité</td>\n",
       "      <td>SICAV</td>\n",
       "      <td>NO</td>\n",
       "      <td>Carmignac Portfolio Sécurité</td>\n",
       "      <td>AW &amp; AW-R</td>\n",
       "      <td>EUR</td>\n",
       "      <td>LU1299306321</td>\n",
       "      <td>2021-07-31</td>\n",
       "      <td>2835.000</td>\n",
       "      <td>2.976183e+05</td>\n",
       "      <td>2.976183e+05</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4880296</th>\n",
       "      <td>Private Client</td>\n",
       "      <td>Private Client</td>\n",
       "      <td>Private Client</td>\n",
       "      <td>Private Client</td>\n",
       "      <td>Switzerland</td>\n",
       "      <td>Switzerland</td>\n",
       "      <td>Fixed Income</td>\n",
       "      <td>Sécurité</td>\n",
       "      <td>SICAV</td>\n",
       "      <td>NO</td>\n",
       "      <td>Carmignac Portfolio Sécurité</td>\n",
       "      <td>FW &amp; FW-R</td>\n",
       "      <td>EUR</td>\n",
       "      <td>LU1792391911</td>\n",
       "      <td>2020-07-31</td>\n",
       "      <td>2916.394</td>\n",
       "      <td>2.874106e+05</td>\n",
       "      <td>2.874106e+05</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>4880297 rows × 18 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        Agreement - Code    Company - Id Company - Ultimate Parent Id  \\\n",
       "0                    003             166                          166   \n",
       "1                    003             166                          166   \n",
       "2                    003             166                          166   \n",
       "3                    003             166                          166   \n",
       "4                    003             166                          166   \n",
       "...                  ...             ...                          ...   \n",
       "4880292   Private Client  Private Client               Private Client   \n",
       "4880293   Private Client  Private Client               Private Client   \n",
       "4880294   Private Client  Private Client               Private Client   \n",
       "4880295   Private Client  Private Client               Private Client   \n",
       "4880296   Private Client  Private Client               Private Client   \n",
       "\n",
       "        Registrar Account - ID Registrar Account - Region  \\\n",
       "0                    200000647                     France   \n",
       "1                    200000647                     France   \n",
       "2                    200000647                     France   \n",
       "3                    200000647                     France   \n",
       "4                    200000647                     France   \n",
       "...                        ...                        ...   \n",
       "4880292         Private Client                Switzerland   \n",
       "4880293         Private Client                Switzerland   \n",
       "4880294         Private Client                Switzerland   \n",
       "4880295         Private Client                Switzerland   \n",
       "4880296         Private Client                Switzerland   \n",
       "\n",
       "        RegistrarAccount - Country Product - Asset Type Product - Strategy  \\\n",
       "0                           France          Diversified         Patrimoine   \n",
       "1                           France          Diversified         Patrimoine   \n",
       "2                           France          Diversified         Patrimoine   \n",
       "3                           France          Diversified         Patrimoine   \n",
       "4                           France          Diversified         Patrimoine   \n",
       "...                            ...                  ...                ...   \n",
       "4880292                Switzerland         Fixed Income           Sécurité   \n",
       "4880293                Switzerland         Fixed Income           Sécurité   \n",
       "4880294                Switzerland         Fixed Income           Sécurité   \n",
       "4880295                Switzerland         Fixed Income           Sécurité   \n",
       "4880296                Switzerland         Fixed Income           Sécurité   \n",
       "\n",
       "        Product - Legal Status Product - Is Dedie ?  \\\n",
       "0                          FCP                   NO   \n",
       "1                          FCP                   NO   \n",
       "2                          FCP                   NO   \n",
       "3                          FCP                   NO   \n",
       "4                          FCP                   NO   \n",
       "...                        ...                  ...   \n",
       "4880292                  SICAV                   NO   \n",
       "4880293                  SICAV                   NO   \n",
       "4880294                  SICAV                   NO   \n",
       "4880295                  SICAV                   NO   \n",
       "4880296                  SICAV                   NO   \n",
       "\n",
       "                       Product - Fund Product - Shareclass Type  \\\n",
       "0                Carmignac Patrimoine                         A   \n",
       "1                Carmignac Patrimoine                         A   \n",
       "2                Carmignac Patrimoine                         A   \n",
       "3                Carmignac Patrimoine                         A   \n",
       "4                Carmignac Patrimoine                         A   \n",
       "...                               ...                       ...   \n",
       "4880292  Carmignac Portfolio Sécurité                 AW & AW-R   \n",
       "4880293  Carmignac Portfolio Sécurité                 AW & AW-R   \n",
       "4880294  Carmignac Portfolio Sécurité                 AW & AW-R   \n",
       "4880295  Carmignac Portfolio Sécurité                 AW & AW-R   \n",
       "4880296  Carmignac Portfolio Sécurité                 FW & FW-R   \n",
       "\n",
       "        Product - Shareclass Currency Product - Isin Centralisation Date  \\\n",
       "0                                 EUR   FR0010135103          2015-03-31   \n",
       "1                                 EUR   FR0010135103          2015-11-30   \n",
       "2                                 EUR   FR0010135103          2015-12-31   \n",
       "3                                 EUR   FR0010135103          2016-03-31   \n",
       "4                                 EUR   FR0010135103          2016-11-30   \n",
       "...                               ...            ...                 ...   \n",
       "4880292                           EUR   LU1299306321          2020-02-29   \n",
       "4880293                           EUR   LU1299306321          2020-06-30   \n",
       "4880294                           EUR   LU1299306321          2020-10-31   \n",
       "4880295                           EUR   LU1299306321          2021-07-31   \n",
       "4880296                           EUR   LU1792391911          2020-07-31   \n",
       "\n",
       "         Quantity - AUM  Value - AUM CCY  Value - AUM €  \n",
       "0                35.368     2.464867e+04   2.464867e+04  \n",
       "1                35.368     2.241306e+04   2.241306e+04  \n",
       "2                35.368     2.205124e+04   2.205124e+04  \n",
       "3                35.368     2.162612e+04   2.162612e+04  \n",
       "4                35.368     2.248945e+04   2.248945e+04  \n",
       "...                 ...              ...            ...  \n",
       "4880292       26801.000     2.740670e+06   2.740670e+06  \n",
       "4880293        3099.000     3.122862e+05   3.122862e+05  \n",
       "4880294        3099.000     3.184222e+05   3.184222e+05  \n",
       "4880295        2835.000     2.976183e+05   2.976183e+05  \n",
       "4880296        2916.394     2.874106e+05   2.874106e+05  \n",
       "\n",
       "[4880297 rows x 18 columns]"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "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": 2,
   "id": "4644da13-5aea-4ca0-9fcf-947324766292",
   "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>Agreement - Code</th>\n",
       "      <th>Company - Id</th>\n",
       "      <th>Company - Ultimate Parent Id</th>\n",
       "      <th>Registrar Account - ID</th>\n",
       "      <th>Registrar Account - Region</th>\n",
       "      <th>RegistrarAccount - Country</th>\n",
       "      <th>Product - Asset Type</th>\n",
       "      <th>Product - Strategy</th>\n",
       "      <th>Product - Legal Status</th>\n",
       "      <th>Product - Is Dedie ?</th>\n",
       "      <th>...</th>\n",
       "      <th>Centralisation Date</th>\n",
       "      <th>Quantity - Subscription</th>\n",
       "      <th>Quantity - Redemption</th>\n",
       "      <th>Quantity - NetFlows</th>\n",
       "      <th>Value Ccy - Subscription</th>\n",
       "      <th>Value Ccy - Redemption</th>\n",
       "      <th>Value Ccy - NetFlows</th>\n",
       "      <th>Value € - Subscription</th>\n",
       "      <th>Value € - Redemption</th>\n",
       "      <th>Value € - NetFlows</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>003</td>\n",
       "      <td>166</td>\n",
       "      <td>166</td>\n",
       "      <td>200127202</td>\n",
       "      <td>France</td>\n",
       "      <td>France</td>\n",
       "      <td>Equity</td>\n",
       "      <td>Investissement</td>\n",
       "      <td>SICAV</td>\n",
       "      <td>NO</td>\n",
       "      <td>...</td>\n",
       "      <td>2020-11-05</td>\n",
       "      <td>1636.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>1636.000</td>\n",
       "      <td>280983.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>280983.00</td>\n",
       "      <td>280983.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>280983.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>003</td>\n",
       "      <td>166</td>\n",
       "      <td>166</td>\n",
       "      <td>406533</td>\n",
       "      <td>France</td>\n",
       "      <td>France</td>\n",
       "      <td>Diversified</td>\n",
       "      <td>Patrimoine</td>\n",
       "      <td>FCP</td>\n",
       "      <td>NO</td>\n",
       "      <td>...</td>\n",
       "      <td>2015-03-09</td>\n",
       "      <td>144.690</td>\n",
       "      <td>0.000</td>\n",
       "      <td>144.690</td>\n",
       "      <td>99985.13</td>\n",
       "      <td>0.00</td>\n",
       "      <td>99985.13</td>\n",
       "      <td>99985.13</td>\n",
       "      <td>0.00</td>\n",
       "      <td>99985.13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>003</td>\n",
       "      <td>166</td>\n",
       "      <td>166</td>\n",
       "      <td>406533</td>\n",
       "      <td>France</td>\n",
       "      <td>France</td>\n",
       "      <td>Equity</td>\n",
       "      <td>Investissement</td>\n",
       "      <td>FCP</td>\n",
       "      <td>NO</td>\n",
       "      <td>...</td>\n",
       "      <td>2016-10-26</td>\n",
       "      <td>0.000</td>\n",
       "      <td>-8.321</td>\n",
       "      <td>-8.321</td>\n",
       "      <td>0.00</td>\n",
       "      <td>-9384.76</td>\n",
       "      <td>-9384.76</td>\n",
       "      <td>0.00</td>\n",
       "      <td>-9384.76</td>\n",
       "      <td>-9384.76</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>003</td>\n",
       "      <td>166</td>\n",
       "      <td>166</td>\n",
       "      <td>406533</td>\n",
       "      <td>France</td>\n",
       "      <td>France</td>\n",
       "      <td>Equity</td>\n",
       "      <td>Investissement</td>\n",
       "      <td>FCP</td>\n",
       "      <td>NO</td>\n",
       "      <td>...</td>\n",
       "      <td>2018-10-18</td>\n",
       "      <td>0.000</td>\n",
       "      <td>-22.083</td>\n",
       "      <td>-22.083</td>\n",
       "      <td>0.00</td>\n",
       "      <td>-25227.40</td>\n",
       "      <td>-25227.40</td>\n",
       "      <td>0.00</td>\n",
       "      <td>-25227.40</td>\n",
       "      <td>-25227.40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>003</td>\n",
       "      <td>166</td>\n",
       "      <td>166</td>\n",
       "      <td>406533</td>\n",
       "      <td>France</td>\n",
       "      <td>France</td>\n",
       "      <td>Equity</td>\n",
       "      <td>Investissement</td>\n",
       "      <td>FCP</td>\n",
       "      <td>NO</td>\n",
       "      <td>...</td>\n",
       "      <td>2019-04-08</td>\n",
       "      <td>0.000</td>\n",
       "      <td>-465.992</td>\n",
       "      <td>-465.992</td>\n",
       "      <td>0.00</td>\n",
       "      <td>-563775.76</td>\n",
       "      <td>-563775.76</td>\n",
       "      <td>0.00</td>\n",
       "      <td>-563775.76</td>\n",
       "      <td>-563775.76</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2574456</th>\n",
       "      <td>Private Client</td>\n",
       "      <td>Private Client</td>\n",
       "      <td>Private Client</td>\n",
       "      <td>Private Client</td>\n",
       "      <td>Luxembourg</td>\n",
       "      <td>Luxembourg</td>\n",
       "      <td>Fixed Income</td>\n",
       "      <td>Sécurité</td>\n",
       "      <td>FCP</td>\n",
       "      <td>NO</td>\n",
       "      <td>...</td>\n",
       "      <td>2015-06-12</td>\n",
       "      <td>0.000</td>\n",
       "      <td>-20.000</td>\n",
       "      <td>-20.000</td>\n",
       "      <td>0.00</td>\n",
       "      <td>-34294.40</td>\n",
       "      <td>-34294.40</td>\n",
       "      <td>0.00</td>\n",
       "      <td>-34294.40</td>\n",
       "      <td>-34294.40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2574457</th>\n",
       "      <td>Private Client</td>\n",
       "      <td>Private Client</td>\n",
       "      <td>Private Client</td>\n",
       "      <td>Private Client</td>\n",
       "      <td>Luxembourg</td>\n",
       "      <td>Luxembourg</td>\n",
       "      <td>Fixed Income</td>\n",
       "      <td>Sécurité</td>\n",
       "      <td>FCP</td>\n",
       "      <td>NO</td>\n",
       "      <td>...</td>\n",
       "      <td>2015-09-18</td>\n",
       "      <td>328.726</td>\n",
       "      <td>0.000</td>\n",
       "      <td>328.726</td>\n",
       "      <td>564028.07</td>\n",
       "      <td>0.00</td>\n",
       "      <td>564028.07</td>\n",
       "      <td>564028.07</td>\n",
       "      <td>0.00</td>\n",
       "      <td>564028.07</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2574458</th>\n",
       "      <td>Private Client</td>\n",
       "      <td>Private Client</td>\n",
       "      <td>Private Client</td>\n",
       "      <td>Private Client</td>\n",
       "      <td>Luxembourg</td>\n",
       "      <td>Luxembourg</td>\n",
       "      <td>Fixed Income</td>\n",
       "      <td>Sécurité</td>\n",
       "      <td>FCP</td>\n",
       "      <td>NO</td>\n",
       "      <td>...</td>\n",
       "      <td>2015-09-25</td>\n",
       "      <td>4.443</td>\n",
       "      <td>0.000</td>\n",
       "      <td>4.443</td>\n",
       "      <td>7603.66</td>\n",
       "      <td>0.00</td>\n",
       "      <td>7603.66</td>\n",
       "      <td>7603.66</td>\n",
       "      <td>0.00</td>\n",
       "      <td>7603.66</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2574459</th>\n",
       "      <td>Private Client</td>\n",
       "      <td>Private Client</td>\n",
       "      <td>Private Client</td>\n",
       "      <td>Private Client</td>\n",
       "      <td>Luxembourg</td>\n",
       "      <td>Luxembourg</td>\n",
       "      <td>Fixed Income</td>\n",
       "      <td>Sécurité</td>\n",
       "      <td>FCP</td>\n",
       "      <td>NO</td>\n",
       "      <td>...</td>\n",
       "      <td>2015-11-09</td>\n",
       "      <td>0.000</td>\n",
       "      <td>-440.000</td>\n",
       "      <td>-440.000</td>\n",
       "      <td>0.00</td>\n",
       "      <td>-754696.80</td>\n",
       "      <td>-754696.80</td>\n",
       "      <td>0.00</td>\n",
       "      <td>-754696.80</td>\n",
       "      <td>-754696.80</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2574460</th>\n",
       "      <td>Private Client</td>\n",
       "      <td>Private Client</td>\n",
       "      <td>Private Client</td>\n",
       "      <td>Private Client</td>\n",
       "      <td>Luxembourg</td>\n",
       "      <td>Luxembourg</td>\n",
       "      <td>Fixed Income</td>\n",
       "      <td>Sécurité</td>\n",
       "      <td>SICAV</td>\n",
       "      <td>NO</td>\n",
       "      <td>...</td>\n",
       "      <td>2016-01-11</td>\n",
       "      <td>3595.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>3595.000</td>\n",
       "      <td>358385.55</td>\n",
       "      <td>0.00</td>\n",
       "      <td>358385.55</td>\n",
       "      <td>358385.55</td>\n",
       "      <td>0.00</td>\n",
       "      <td>358385.55</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2574461 rows × 24 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        Agreement - Code    Company - Id Company - Ultimate Parent Id  \\\n",
       "0                    003             166                          166   \n",
       "1                    003             166                          166   \n",
       "2                    003             166                          166   \n",
       "3                    003             166                          166   \n",
       "4                    003             166                          166   \n",
       "...                  ...             ...                          ...   \n",
       "2574456   Private Client  Private Client               Private Client   \n",
       "2574457   Private Client  Private Client               Private Client   \n",
       "2574458   Private Client  Private Client               Private Client   \n",
       "2574459   Private Client  Private Client               Private Client   \n",
       "2574460   Private Client  Private Client               Private Client   \n",
       "\n",
       "        Registrar Account - ID Registrar Account - Region  \\\n",
       "0                    200127202                     France   \n",
       "1                       406533                     France   \n",
       "2                       406533                     France   \n",
       "3                       406533                     France   \n",
       "4                       406533                     France   \n",
       "...                        ...                        ...   \n",
       "2574456         Private Client                 Luxembourg   \n",
       "2574457         Private Client                 Luxembourg   \n",
       "2574458         Private Client                 Luxembourg   \n",
       "2574459         Private Client                 Luxembourg   \n",
       "2574460         Private Client                 Luxembourg   \n",
       "\n",
       "        RegistrarAccount - Country Product - Asset Type Product - Strategy  \\\n",
       "0                           France               Equity     Investissement   \n",
       "1                           France          Diversified         Patrimoine   \n",
       "2                           France               Equity     Investissement   \n",
       "3                           France               Equity     Investissement   \n",
       "4                           France               Equity     Investissement   \n",
       "...                            ...                  ...                ...   \n",
       "2574456                 Luxembourg         Fixed Income           Sécurité   \n",
       "2574457                 Luxembourg         Fixed Income           Sécurité   \n",
       "2574458                 Luxembourg         Fixed Income           Sécurité   \n",
       "2574459                 Luxembourg         Fixed Income           Sécurité   \n",
       "2574460                 Luxembourg         Fixed Income           Sécurité   \n",
       "\n",
       "        Product - Legal Status Product - Is Dedie ?  ... Centralisation Date  \\\n",
       "0                        SICAV                   NO  ...          2020-11-05   \n",
       "1                          FCP                   NO  ...          2015-03-09   \n",
       "2                          FCP                   NO  ...          2016-10-26   \n",
       "3                          FCP                   NO  ...          2018-10-18   \n",
       "4                          FCP                   NO  ...          2019-04-08   \n",
       "...                        ...                  ...  ...                 ...   \n",
       "2574456                    FCP                   NO  ...          2015-06-12   \n",
       "2574457                    FCP                   NO  ...          2015-09-18   \n",
       "2574458                    FCP                   NO  ...          2015-09-25   \n",
       "2574459                    FCP                   NO  ...          2015-11-09   \n",
       "2574460                  SICAV                   NO  ...          2016-01-11   \n",
       "\n",
       "        Quantity - Subscription Quantity - Redemption Quantity - NetFlows  \\\n",
       "0                      1636.000                 0.000            1636.000   \n",
       "1                       144.690                 0.000             144.690   \n",
       "2                         0.000                -8.321              -8.321   \n",
       "3                         0.000               -22.083             -22.083   \n",
       "4                         0.000              -465.992            -465.992   \n",
       "...                         ...                   ...                 ...   \n",
       "2574456                   0.000               -20.000             -20.000   \n",
       "2574457                 328.726                 0.000             328.726   \n",
       "2574458                   4.443                 0.000               4.443   \n",
       "2574459                   0.000              -440.000            -440.000   \n",
       "2574460                3595.000                 0.000            3595.000   \n",
       "\n",
       "        Value Ccy - Subscription  Value Ccy - Redemption  \\\n",
       "0                      280983.00                    0.00   \n",
       "1                       99985.13                    0.00   \n",
       "2                           0.00                -9384.76   \n",
       "3                           0.00               -25227.40   \n",
       "4                           0.00              -563775.76   \n",
       "...                          ...                     ...   \n",
       "2574456                     0.00               -34294.40   \n",
       "2574457                564028.07                    0.00   \n",
       "2574458                  7603.66                    0.00   \n",
       "2574459                     0.00              -754696.80   \n",
       "2574460                358385.55                    0.00   \n",
       "\n",
       "         Value Ccy - NetFlows  Value € - Subscription  Value € - Redemption  \\\n",
       "0                   280983.00               280983.00                  0.00   \n",
       "1                    99985.13                99985.13                  0.00   \n",
       "2                    -9384.76                    0.00              -9384.76   \n",
       "3                   -25227.40                    0.00             -25227.40   \n",
       "4                  -563775.76                    0.00            -563775.76   \n",
       "...                       ...                     ...                   ...   \n",
       "2574456             -34294.40                    0.00             -34294.40   \n",
       "2574457             564028.07               564028.07                  0.00   \n",
       "2574458               7603.66                 7603.66                  0.00   \n",
       "2574459            -754696.80                    0.00            -754696.80   \n",
       "2574460             358385.55               358385.55                  0.00   \n",
       "\n",
       "         Value € - NetFlows  \n",
       "0                 280983.00  \n",
       "1                  99985.13  \n",
       "2                  -9384.76  \n",
       "3                 -25227.40  \n",
       "4                -563775.76  \n",
       "...                     ...  \n",
       "2574456           -34294.40  \n",
       "2574457           564028.07  \n",
       "2574458             7603.66  \n",
       "2574459          -754696.80  \n",
       "2574460           358385.55  \n",
       "\n",
       "[2574461 rows x 24 columns]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "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": 3,
   "id": "5773b911-6b84-448d-962f-8228eeac0250",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(4880297, 18)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_aum2.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "6f571810-c373-4d30-8ca5-c3a074b95b08",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['Agreement - Code', 'Company - Id', 'Company - Ultimate Parent Id',\n",
       "       'Registrar Account - ID', 'Registrar Account - Region',\n",
       "       'RegistrarAccount - Country', 'Product - Asset Type',\n",
       "       'Product - Strategy', 'Product - Legal Status', 'Product - Is Dedie ?',\n",
       "       'Product - Fund', 'Product - Shareclass Type',\n",
       "       'Product - Shareclass Currency', 'Product - Isin',\n",
       "       'Centralisation Date', 'Quantity - AUM', 'Value - AUM CCY',\n",
       "       'Value - AUM €'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_aum2.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "af25fd07-a613-4adc-b88b-93a8d300379c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2574461, 24)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_flows2.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "c6d0fe83-2957-430b-89cf-cd30833b7cab",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['Agreement - Code', 'Company - Id', 'Company - Ultimate Parent Id',\n",
       "       'Registrar Account - ID', 'Registrar Account - Region',\n",
       "       'RegistrarAccount - Country', 'Product - Asset Type',\n",
       "       'Product - Strategy', 'Product - Legal Status', 'Product - Is Dedie ?',\n",
       "       'Product - Fund', 'Product - Shareclass Type',\n",
       "       'Product - Shareclass Currency', 'Product - Isin',\n",
       "       'Centralisation Date', 'Quantity - Subscription',\n",
       "       'Quantity - Redemption', 'Quantity - NetFlows',\n",
       "       'Value Ccy - Subscription', 'Value Ccy - Redemption',\n",
       "       'Value Ccy - NetFlows', 'Value € - Subscription',\n",
       "       'Value € - Redemption', 'Value € - NetFlows'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "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": 7,
   "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": 8,
   "id": "64ca9883-372b-4a5e-8fc1-10e5d835c411",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Doublons AUM: 250232\n",
      "Doublons Flows: 4849\n"
     ]
    }
   ],
   "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": 9,
   "id": "f47b276d-cce6-433c-87c5-860810d71d34",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " Cols:  ['Company - Id', 'Company - Ultimate Parent Id', 'Registrar Account - ID', 'Registrar Account - Region', 'Product - Isin']\n",
      "Doublons AUM: 26452\n",
      "Doublons Flows: 0\n"
     ]
    }
   ],
   "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": "d2f355e6-30c5-420e-a3db-9095dd5e0147",
   "metadata": {},
   "outputs": [],
   "source": [
    "# # Comptes avec same flux et same product ISIN mais IDs différents ---> candidats pseudo-client\n",
    "# #plusieurs comptes diff réalisent EXACTEMENT le même flux sur le même produit le même jour.\n",
    "# #date?\n",
    "\n",
    "# pseudo_client_candidates = df_flows2.groupby(['Product - Isin','Centralisation Date', 'Quantity - NetFlows', 'Value € - NetFlows'])['Registrar Account - ID'].nunique()\n",
    "# pseudo_client_candidates = pseudo_client_candidates[pseudo_client_candidates > 1]\n",
    "# print(\"Comptes potentiels à fusionner:\")\n",
    "# print(pseudo_client_candidates)"
   ]
  },
  {
   "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",
    "\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",
    "\n",
    "# plt.figure(figsize=(10,6))\n",
    "# plt.hist(aum_by_account, bins=50)\n",
    "# plt.xlabel(\"Mean AUM value (€)\")\n",
    "# plt.ylabel(\"Number of client accounts\")\n",
    "# plt.title(\"Distribution of client average AUM (one value per Account_ID)\")\n",
    "# plt.grid(True)\n",
    "# plt.show()\n",
    "\n",
    "plt.figure(figsize=(10,6))\n",
    "plt.hist(aum_by_account, bins=100)\n",
    "\n",
    "# ZOOM sur les petits AUM (0 à 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 \")\n",
    "plt.show()\n"
   ]
  },
  {
   "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": 12,
   "id": "c79ae705-833e-420f-ab3e-51ccb2ae52c5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# # 1) Créer une colonne Année-Mois\n",
    "\n",
    "# df_filtered = df_merged[df_merged[\"Date\"].dt.year >= 2022]\n",
    "# df_filtered[\"YearMonth\"] = df_merged[\"Date\"].dt.to_period(\"M\")\n",
    "\n",
    "# # 2) AUM moyen par compte ET par mois\n",
    "# aum_monthly = (\n",
    "#     df_merged.groupby([\"Account_ID\", \"YearMonth\"])[\"AUM_EUR\"]\n",
    "#     .mean()\n",
    "#     .reset_index()\n",
    "# )\n",
    "\n",
    "# # 3) Pour chaque compte : moyenne des AUM mensuels sur toute la période\n",
    "# aum_avg_over_time = (\n",
    "#     aum_monthly.groupby(\"Account_ID\")[\"AUM_EUR\"]\n",
    "#     .mean()\n",
    "#     .dropna()\n",
    "# )\n",
    "\n",
    "# # 4) Histogramme des AUM mensuels moyens par compte (sur toute l’historique)\n",
    "# plt.figure(figsize=(10,6))\n",
    "# plt.hist(aum_avg_over_time, bins=100)\n",
    "\n",
    "# # Option : zoom sur les petits AUM (ex. 0 à 5M €)\n",
    "# plt.xlim(0, 0.5e7)\n",
    "\n",
    "# plt.xlabel(\"Average monthly AUM per account (€)\")\n",
    "# plt.ylabel(\"Number of client accounts\")\n",
    "# plt.title(\"Distribution of average monthly AUM per account (full history)\")\n",
    "# plt.grid(True)\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": [
    "###############################################################################\n",
    "# 4. 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",
    "# ---- Plot bar chart ----\n",
    "flows_monthly.index = flows_monthly.index.astype(str)\n",
    "\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",
    "\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"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c6bb455c-91f9-4169-a179-273e98ef3ebd",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
