{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "31effaa3",
   "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": 9,
   "id": "71e1bd8e",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.9052450954029012\n",
      "0.268438983965372\n",
      "0.39642569360134305\n",
      "0.2855415942925523\n",
      "0.674638891062864\n",
      "0.397175943907469\n",
      "0.8916886173321832\n",
      "0.6433401281831508\n",
      "0.15638356293780564\n",
      "0.05006019580631338\n"
     ]
    }
   ],
   "source": [
    "for i in range(10):\n",
    "    print(random.rand())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "8f85cc95",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Un vecteur de nombres aléatoires de taille 4 : [0.60762988 0.33052185 0.13831248 0.14838135].\n",
      "Une matrice de nombres aléatoires de taille 2 par 3 : [[0.56791513 0.26399637 0.2785913 ]\n",
      " [0.17929815 0.45572534 0.76020978]].\n"
     ]
    }
   ],
   "source": [
    "print(f\"Un vecteur de nombres aléatoires de taille 4 : {random.rand(4)}.\")\n",
    "\n",
    "print(f\"Une matrice de nombres aléatoires de taille 2 par 3 : {random.rand(2,3)}.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "e5cc9927",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.7472226676638385\n",
      "---\n",
      "0.417022004702574\n",
      "0.7203244934421581\n",
      "0.00011437481734488664\n",
      "0.30233257263183977\n",
      "---\n",
      "0.417022004702574\n",
      "0.7203244934421581\n",
      "0.00011437481734488664\n",
      "0.30233257263183977\n"
     ]
    }
   ],
   "source": [
    "print(random.rand())\n",
    "print(\"---\")\n",
    "random.seed(seed=1) #ici on fixe la valeur de la graine\n",
    "print(random.rand())\n",
    "print(random.rand())\n",
    "print(random.rand())\n",
    "print(random.rand())\n",
    "print(\"---\")\n",
    "random.seed(seed=1)\n",
    "print(random.rand())\n",
    "print(random.rand())\n",
    "print(random.rand())\n",
    "print(random.rand())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "9cc832e2",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "def uniforme_continue(a,b):\n",
    "    return (b-a)*(random.rand()) + a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "49f2cf0b",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.14675589081711304\n",
      "0.0923385947687978\n",
      "0.1862602113776709\n",
      "0.34556072704304774\n",
      "0.39676747423066994\n",
      "0.538816734003357\n",
      "0.4191945144032948\n",
      "0.6852195003967595\n",
      "0.20445224973151743\n",
      "0.8781174363909454\n"
     ]
    }
   ],
   "source": [
    "a=0\n",
    "b=1\n",
    "\n",
    "for i in range(10):\n",
    "    print(uniforme_continue(a,b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "b4deeb6e",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "def test_disque(N):\n",
    "    Z = np.zeros((2,N))\n",
    "    for i in range(N):\n",
    "        Z[0, i] = uniforme_continue(-1, 1)\n",
    "        Z[1, i] = uniforme_continue(-1, 1)\n",
    "    return Z"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "b88accb4",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.94522481, -0.1653904 , -0.71922612],\n",
       "       [ 0.34093502,  0.11737966, -0.60379702]])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_disque(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "d28b766b",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 119756 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "a = test_disque(3000)\n",
    "plt.scatter(a[0],a[1])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "e8dcfdce",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "def kPremier(N):\n",
    "    a = test_disque(N)\n",
    "    x = []\n",
    "    y = []\n",
    "    xp = []\n",
    "    yp = []\n",
    "    for i in range(N):\n",
    "        if ((a[0][i]**2 + a[1][i]**2)**(1/2) <= 1) :\n",
    "            x.append(a[0][i])\n",
    "            y.append(a[1][i])\n",
    "        else:\n",
    "            xp.append(a[0][i])\n",
    "            yp.append(a[1][i])\n",
    "    \n",
    "    return np.array((x,y)), np.array((xp,yp))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "b0e68e09",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 127092 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "\n",
    "N = 3000\n",
    "a = kPremier(N)\n",
    "x = a[0]\n",
    "y = a[1]\n",
    "\n",
    "\n",
    "plt.scatter(x[0],x[1])\n",
    "plt.scatter(y[0],y[1])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "0546af8d",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 25052 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "n = 10\n",
    "p = 0.2\n",
    "x = np.array(range(1, n+1))\n",
    "y = [(1-p)**(k-1) * p for k in x]\n",
    "\n",
    "width = 0.1\n",
    "fig = plt.figure()\n",
    "\n",
    "plt.bar(x, y, width, align=\"center\")\n",
    "plt.scatter(x, y, color=\"black\")\n",
    "plt.xlabel(\"$e$\")\n",
    "plt.ylabel(\"$p_e$\")\n",
    "plt.title(f\"Diagramme en bâtons de la loi géométrique $X({n},{p})$.\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "f7eb1acd-6f41-44a3-b198-4539e1caf67e",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 27008 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 25844 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 22124 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "p = [0.1, 0.3, 0.5]\n",
    "width = 0.2\n",
    "fig = plt.figure()\n",
    "for i in p:\n",
    "    m = math.floor(math.log(0.05)/math.log(1-i))\n",
    "    x = np.array(range(1, m+1))\n",
    "    y = [(1-i)**(k-1) * i for k in x]\n",
    "    plt.bar(x, y, width, align=\"center\", label=f\"p={i}\")\n",
    "    plt.scatter(x, y, color=\"black\")\n",
    "    plt.xlabel(\"$e$\")\n",
    "    plt.ylabel(\"$p_e$\")\n",
    "    plt.title(f\"Diagramme en bâtons de la loi géométrique\")\n",
    "    plt.legend()\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "c5f52679-70ed-406f-8bf2-8629cd7cda30",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 19048 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "x = [1, 2, 3, 4]\n",
    "N = [450, 350, 50, 150]\n",
    "n = sum(N)\n",
    "frequence = [x/n for x in N]\n",
    "width = 0.05\n",
    "fig = plt.figure()\n",
    "plt.xlabel(\"$e$\")\n",
    "plt.ylabel(\"$p_e$\")\n",
    "plt.title(\"Diagramme en bâtons de la loi empirique\")\n",
    "plt.bar(x, frequence, width, align=\"center\")\n",
    "plt.scatter(x, frequence, color=\"black\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "739cc3ae-54d6-4e92-a9f0-59b6bec4fdd0",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "def de():\n",
    "    return math.ceil(6*random.rand())\n",
    "\n",
    "def max3de():\n",
    "    return max([de(), de(), de()])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "717db457-ba2a-48cd-b2c0-ad1ab117e3ed",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Figure size 640x480 with 0 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 19180 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "<Figure size 640x480 with 0 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 23604 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "<Figure size 640x480 with 0 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 23704 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "def test_max_3_de(N):\n",
    "    Z = np.zeros(N)\n",
    "    for i in range(N):\n",
    "        Z[i] = max3de()\n",
    "    card = plt.hist(Z, [1, 2, 3, 4, 5, 6, 7])\n",
    "    width = 0.1\n",
    "    \n",
    "    plt.clf()\n",
    "    plt.figure()\n",
    "    plt.grid()\n",
    "    plt.title(\"Diagramme en bâtons de la loi\")\n",
    "    plt.xlabel('x')\n",
    "    plt.ylabel('y')\n",
    "    plt.bar(range(1,7),card[0]/N,width = 0.2)\n",
    "    plt.bar(np.arange(1,7)+0.2,[(i**3-(i-1)**3)/6**3 for i in range(1,7)],width = 0.2)\n",
    "    plt.show()\n",
    "\n",
    "    \n",
    "test_max_3_de(10)\n",
    "test_max_3_de(100)\n",
    "test_max_3_de(1000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "a0307125-6436-4ead-8d9b-75e6ee8179d2",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "def showGraph(f, p, m):\n",
    "    n = 10000\n",
    "    card = np.zeros(m)\n",
    "    for i in range(n):\n",
    "        geom = f(p)\n",
    "        if geom <= m:\n",
    "            card[geom-1] += 1\n",
    "            \n",
    "    x = np.array(range(1, m+1))\n",
    "    y = [(1-p)**(k-1) * p for k in x]\n",
    "    width = 0.1\n",
    "    \n",
    "    fig = plt.figure()\n",
    "    plt.bar(x, y, width, align=\"center\", label=\"loi théorique\")\n",
    "    plt.bar(x+0.2, card/n, width, align=\"center\", label=\"mesure empirique\")\n",
    "    plt.xlabel(\"$e$\")\n",
    "    plt.ylabel(\"$p_e$\")\n",
    "    plt.title(f\"Diagramme de la loi géométrique $G({p})$.\")\n",
    "    plt.legend()\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "8da3c856-f908-4861-ac67-8a027211b1e1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 26484 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "def geometrique1(p):\n",
    "    U = uniforme_continue(0, 1)\n",
    "    b = 0\n",
    "    k = 0\n",
    "    while b < U:\n",
    "        k += 1\n",
    "        b += p * (1-p)**(k-1)\n",
    "    return k\n",
    "\n",
    "m = 12\n",
    "p = 0.2\n",
    "showGraph(geometrique1, p, m)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "c34cc7e0-8e47-495a-86bc-fcbc7d9a6d53",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 23920 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "def geometrique2(p):\n",
    "    k = 1\n",
    "    while uniforme_continue(0, 1) > p:\n",
    "        k += 1\n",
    "    return k\n",
    "        \n",
    "p = 0.3\n",
    "m = 13\n",
    "showGraph(geometrique2, p, m)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "adb2551d-503b-419c-82e9-0996815e2924",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 23752 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "def geometrique3(p):\n",
    "    return math.ceil(math.log(uniforme_continue(0, 1)) / math.log(1-p))\n",
    "        \n",
    "p = 0.3\n",
    "m = 12\n",
    "showGraph(geometrique3, p, m)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "ac8415ea-b15a-431a-824e-1aa6c23500ee",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 23916 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "def geometrique4(p):\n",
    "    return scs.geom.rvs(p) #np.random.geometric(p)\n",
    "\n",
    "p = 0.3\n",
    "m = 12\n",
    "showGraph(geometrique4, p, m)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "e85886dc-d8bb-4652-976e-b060d93402a1",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "La méthode 1 a pris 0.10540652275085449 secondes.\n",
      "La méthode 2 a pris 0.11203813552856445 secondes.\n",
      "La méthode 3 a pris 0.09955310821533203 secondes.\n",
      "La méthode 4 a pris 3.5846686363220215 secondes.\n"
     ]
    }
   ],
   "source": [
    "from time import time\n",
    "\n",
    "for index, f in enumerate([geometrique1, geometrique2, geometrique3, geometrique4]):\n",
    "    N = 100000\n",
    "    p = 0.5\n",
    "    t1 = time()\n",
    "    [f(p) for i in range(N)]\n",
    "    t2 = time()\n",
    "    temps1 = t2 - t1\n",
    "    print(f\"La méthode {index + 1} a pris {temps1} secondes.\")"
   ]
  }
 ],
 "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
}
