{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f9a51919-f7c1-4a14-a921-f908e6e0f3d3",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import math\n",
    "import numpy as np\n",
    "import scipy.stats as scs\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy.random as random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d68b921d-3512-4e16-a46d-c21d4a609804",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x7f79a2509060>]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 9072 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "A = [2, -2, 1, -2, 1]\n",
    "B = [1, 3, 2, 6, 3]\n",
    "plt.step(A, B)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "203d4fd9-f1c4-4a31-8ac7-dc87a34ad473",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 14576 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "def bernouilli(p):\n",
    "    plt.step([-1, 0, 1, 1, 2], [0, 0, 1-p, 1, 1], label=f'p={p}')\n",
    "    plt.legend()\n",
    "    \n",
    "for p in [0.2, 0.5, 0.7]:\n",
    "    bernouilli(p)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "43c17c05-8468-4469-abda-fbc9d6b85b38",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 10532 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 10876 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "def uniforme_discrete(card):\n",
    "    plt.figure()\n",
    "    x = range(0, card +1 )\n",
    "    y = [0]\n",
    "    for i in range(1, card +1):\n",
    "        y.append(i/card)\n",
    "    plt.step(x, y)\n",
    "    \n",
    "uniforme_discrete(6)\n",
    "uniforme_discrete(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "892f6ef4-f53d-4d76-b4c6-5a2372c20925",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.legend.Legend at 0x7f799a2ebdc0>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 20020 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "def uniforme(a, b, card):\n",
    "    x = np.linspace(a, b, card)\n",
    "    y = np.zeros(card)\n",
    "    for i in range(card):\n",
    "        y[i] = i/card\n",
    "    addANumber = lambda A: np.array([A for i in range(100)])\n",
    "    x = np.concatenate((np.linspace(-1, 0, 100), x, np.linspace(1, 2, 100)))\n",
    "    y = np.concatenate((addANumber(0), y, addANumber(1)))\n",
    "    return x, y\n",
    "    \n",
    "Ux, Uy = uniforme(0, 1, 200)\n",
    "plt.plot(Ux, Uy, label='Loi uniforme')\n",
    "plt.legend()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "7139a7c7-a974-4a30-aa76-c1d298924972",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.legend.Legend at 0x7f799a218d00>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 14980 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 31440 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "def traceFctRepEmpirique01(ens):\n",
    "    card = len(ens)\n",
    "    x = np.concatenate((np.array([min(ens)-1]), ens, np.array([max(ens)+1])))\n",
    "    x.sort()\n",
    "    y = [0]\n",
    "    for i in range(card + 1):\n",
    "        y.append(i/card)\n",
    "    plt.step(x, y, label=f\"Card={card}\")\n",
    "    plt.legend()\n",
    "    plt.grid()\n",
    "    \n",
    "traceFctRepEmpirique01([0, 1, 9, 6, 3, 12, 2, 9])\n",
    "plt.figure()\n",
    "for n in [5, 15, 100]:\n",
    "    traceFctRepEmpirique01(np.random.rand(n))\n",
    "x,y = uniforme(0, 1, 200)\n",
    "plt.plot(x,y, label='Fonction limite')\n",
    "plt.legend()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "67f8652e-08ae-475e-b83b-5a3b1b457d78",
   "metadata": {},
   "source": [
    "Fonction de répartition: 1 - exp(-lambda * x)\n",
    "\n",
    "Fonction inverse : -1/lambda * ln(1-U)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "a05d8992-dc36-4880-9018-33447c5de924",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(0.0, 3.0)"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 39740 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "def inverse_F(U, lam):\n",
    "    return -1/lam * np.log(1-U)\n",
    "\n",
    "def simuExp():\n",
    "    return inverse_F(np.random.rand(), lam)\n",
    "\n",
    "def exponentielle(m):\n",
    "    return 1-np.exp(-lam * x)\n",
    "\n",
    "lam = 1\n",
    "m = math.ceil(-np.log(0.05)/lam)\n",
    "\n",
    "for n in [100, 1000, 5000]:\n",
    "    traceFctRepEmpirique01([simuExp() for _ in range(n)])\n",
    "    \n",
    "x = np.linspace(0, m, 200)\n",
    "plt.plot(x, exponentielle(x), label='Fonction de répartion')\n",
    "plt.legend()\n",
    "plt.gca().set_xlim(0, m)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "3958036d-a251-4e77-ba1d-438d0c0be2a8",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(-12.706204736174696, 12.706204736174696)"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 38232 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "def arct(X):\n",
    "    return np.arctan(X)/np.pi + 1/2\n",
    "\n",
    "def inverse_arct(X):\n",
    "    return np.tan(np.pi * (X - 1/2))\n",
    "\n",
    "def simuCauchy():\n",
    "    return inverse_arct(np.random.rand())\n",
    "\n",
    "m = np.tan(np.pi * 0.475)\n",
    "\n",
    "for n in [50, 100, 1000]:\n",
    "    traceFctRepEmpirique01([simuCauchy() for _ in range(n)])\n",
    "\n",
    "x = np.linspace(-m, m, 200)\n",
    "plt.plot(x, arct(x), label='Fonction de répartion')\n",
    "plt.legend()\n",
    "plt.gca().set_xlim(-m, m)"
   ]
  }
 ],
 "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.10.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
