{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "10b8aa2c-cf98-4170-966c-a1f5e6e7f55a",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import scipy.stats as stats"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "52e1d61e-cde8-47ec-94d8-285dab47089e",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.legend.Legend at 0x7f834444ca30>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 24828 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "lam = 1\n",
    "nb_repl = 100000\n",
    "sample = np.random.exponential(lam, nb_repl)\n",
    "intervalle = np.linspace(0, 5, 100)\n",
    "plt.hist(sample, bins=intervalle, density=True, label='Echantillon')\n",
    "\n",
    "densite = stats.expon().pdf\n",
    "plt.plot(intervalle, densite(intervalle), label='Fonction densité')\n",
    "plt.legend()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "168e2038-b080-4a28-b56a-61d6cd26c4d3",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.legend.Legend at 0x7f833c22b7c0>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 38316 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "np_repl = 10000\n",
    "sampleX, sampleY = 2*np.random.rand(np_repl)-1, 2*np.random.rand(np_repl)-1\n",
    "\n",
    "intervalle = np.linspace(-2, 2, 100)\n",
    "plt.hist(sampleX + sampleY, bins=intervalle, density=True, label='Echantillon')\n",
    "\n",
    "densite = stats.uniform(-1, 2).pdf\n",
    "plt.plot(intervalle, densite(intervalle), label='Fonction densité')\n",
    "\n",
    "plt.plot(intervalle, 1/4 * np.maximum(2 - np.abs(intervalle), 0), label='Fonction')\n",
    "plt.legend()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "9db2df29-5793-4275-be83-20e7a88ab194",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.legend.Legend at 0x7f833c3d9060>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 17740 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "lam = 1\n",
    "nb_repl = 10000\n",
    "sampleX, sampleY, sampleZ = np.random.exponential(lam, nb_repl), np.random.exponential(lam, nb_repl), np.random.exponential(lam, nb_repl)\n",
    "intervalle = np.linspace(-5, 5, 100)\n",
    "plt.hist(sampleX - sampleY/2, bins=intervalle, density=True, alpha=.7, label='Echantillon de X - Y/2')\n",
    "plt.hist(sampleZ/2, bins=intervalle, density=True, alpha=.7, label='Echantillon de Z')\n",
    "plt.legend()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "14d34eed-3ca6-48c6-a004-97436d750d73",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 34888 characters]",
      "text/plain": [
       "<Figure size 1800x500 with 3 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "p = 1/2\n",
    "nb_repl = 1000\n",
    "\n",
    "plt.figure(figsize=(18, 5))\n",
    "for i, k in enumerate([10, 100, 1000]):\n",
    "    plt.subplot(1, 3, i+1)\n",
    "    sample = np.random.binomial(k, p, nb_repl)\n",
    "    intervalle = np.linspace(np.min(sample), np.max(sample), 100)\n",
    "    plt.hist(sample, bins=intervalle, density=True, label=f'Echantillon de X pour n={k}')\n",
    "    plt.legend()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "0c6898fa-3cb8-4bec-9153-73ce08a95809",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "def sample_uniforme(N):\n",
    "    return 2*np.random.rand(N) - 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "63bd382b-8c4e-4eaa-93b3-20ae561811c9",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 12428 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 11464 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "nb_repl = 100_000\n",
    "nb_lgn = 1000\n",
    "liste_Sn = []\n",
    "\n",
    "for _ in range(nb_lgn):\n",
    "    liste_Sn.append(np.mean(sample_uniforme(nb_repl)))\n",
    "    \n",
    "nb_bins = 100\n",
    "intervalles = np.linspace(np.min(liste_Sn), np.max(liste_Sn), nb_bins)\n",
    "plt.hist(liste_Sn, density=True, bins=intervalles)\n",
    "plt.show()\n",
    "\n",
    "plt.figure()\n",
    "nb_bins = 100\n",
    "intervalles = np.linspace(-1, 1, nb_bins)\n",
    "plt.hist(liste_Sn, density=True, bins=intervalles)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "41ddaed5-d548-43f5-97ab-148f653e99da",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 18884 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "nb_repl = 100_000\n",
    "nb_lgn = 1000\n",
    "liste_Sn = []\n",
    "\n",
    "for _ in range(nb_lgn):\n",
    "    liste_Sn.append(np.mean(np.sqrt(3*nb_repl) * np.tan(np.pi/2 * sample_uniforme(nb_repl))))\n",
    "\n",
    "#nb_bins = 100\n",
    "#intervalles = np.linspace(np.min(liste_Sn), np.max(liste_Sn), nb_bins)\n",
    "#plt.hist(liste_Sn, density=True, bins=intervalles)\n",
    "#plt.show()\n",
    "\n",
    "plt.figure()\n",
    "densite = stats.norm(scale=1).pdf\n",
    "nb_bins = 100\n",
    "intervalles = np.linspace(-1000, 1000, nb_bins)\n",
    "plt.hist(liste_Sn, density=True, bins=intervalles)\n",
    "plt.plot(intervalle, densite(intervalle))\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0733430b-464e-486b-a66e-e89c7abc37b9",
   "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
}
