{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "6dce0c79-7cf1-4144-954b-313254b08f0d",
   "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": 3,
   "id": "6f567a36-6229-4973-a160-379251a9ea6f",
   "metadata": {},
   "outputs": [],
   "source": [
    "def X():\n",
    "    p = p0\n",
    "    i = 0\n",
    "    q = p\n",
    "    u = random.rand()\n",
    "    while u > q:\n",
    "        p = c(i)*p\n",
    "        i +=1\n",
    "        q +=p\n",
    "    return i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "b3f9894a-5d85-469e-9978-d45bed25d7a7",
   "metadata": {},
   "outputs": [],
   "source": [
    "def simupoisson(a, borne):\n",
    "    p = math.exp(-a)\n",
    "    i = 0\n",
    "    q = p\n",
    "    u = random.random()\n",
    "\n",
    "    while u > q and q <= borne:\n",
    "        i += 1\n",
    "        p *= a / i\n",
    "        q += p\n",
    "\n",
    "    return i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "1b59e989-6510-4238-8896-fcdc8aa44fde",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 35424 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "def poisson(a, k):\n",
    "    return np.exp(-a) * a**k / np.math.factorial(k)\n",
    "\n",
    "def testpoisson(a, N, borne):\n",
    "    # Générer l'échantillon de taille N\n",
    "    echantillon = [simupoisson(a, borne) for _ in range(N)]\n",
    "\n",
    "    # Calculer les fréquences empiriques\n",
    "    frequences = [echantillon.count(valeur) / N for valeur in set(echantillon)]\n",
    "    valeurs = list(set(echantillon))\n",
    "\n",
    "    x = range(0, borne + 1)\n",
    "    \n",
    "    # Tracer le diagramme en bâton\n",
    "    width=0.3\n",
    "    plt.bar(valeurs, frequences, width=width, align='center', label='mesure empirique')\n",
    "    plt.bar(np.array(x) + width, [poisson(a, k) for k in x], width=width, label='loi theorique')\n",
    "    plt.xlabel('Valeurs')\n",
    "    plt.ylabel('Fréquences empiriques')\n",
    "    plt.legend()\n",
    "    plt.title(f'Diagramme en bâton - Loi de Poisson (a={a}, N={N})')\n",
    "    plt.show()\n",
    "\n",
    "testpoisson(4, 1000, 10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "773ecf2e-3e21-4c65-b8b2-93d6736c0206",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 53672 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 51036 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 45436 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 43548 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "def inverseF(U, alpha):\n",
    "    return U**(1/alpha)\n",
    "\n",
    "def simubeta(alpha):\n",
    "    return inverseF(np.random.rand(), alpha)\n",
    "\n",
    "def testbeta(alpha, M):\n",
    "    echantillon = [simubeta(alpha) for _ in range(M)]\n",
    "    x = np.concatenate((np.array([0]), echantillon, np.array([1])))\n",
    "    x.sort()\n",
    "    y = [0]\n",
    "    for i in range(M + 1):\n",
    "        y.append(i/M)\n",
    "    plt.step(x, y, label=f\"Card={M}\")\n",
    "    plt.legend()\n",
    "    plt.grid()\n",
    "\n",
    "for alpha in [1, 2, 5, 10]:\n",
    "    plt.figure()\n",
    "    for m in [10, 100, 1000, 10000]:\n",
    "        testbeta(alpha, m)\n",
    "\n",
    "    x = np.linspace(0, 1, 200)\n",
    "    plt.plot(x, x**alpha, label='Fonction limite')\n",
    "    plt.legend()\n",
    "    plt.grid()\n",
    "    plt.title(f'Loi et mesure empirique de  f(U) pour alpha={alpha}')\n",
    "    plt.xlabel('x')\n",
    "    plt.ylabel('y')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b247c8ff-b27c-48e0-a8ef-e4ccd3ed22fd",
   "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.10.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
