{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b186bb3c",
   "metadata": {},
   "source": [
    "$\\newcommand\\N{\\mathbb N}\n",
    "\\newcommand\\Z{\\mathbb Z}\n",
    "\\newcommand\\Q{\\mathbb Q}\n",
    "\\newcommand\\R{\\mathbb R}\n",
    "\\newcommand\\C{\\mathbb C}\n",
    "\\newcommand\\too\\longrightarrow\n",
    "\\renewcommand\\phi\\varphi\n",
    "\\renewcommand\\epsilon\\varepsilon\n",
    "\\renewcommand\\hat\\widehat\n",
    "\\renewcommand\\tilde\\widetilde\n",
    "\\renewcommand\\vec\\overrightarrow\n",
    "\\renewcommand\\bar\\overline\n",
    "\\newcommand\\fl[1]{\\left\\lfloor #1\\right\\rfloor}\n",
    "\\newcommand\\llbracket{[\\![}\n",
    "\\newcommand\\rrbracket{]\\!]}\n",
    "\\newcommand\\bbr[2]{\\llbracket #1,#2\\rrbracket}\n",
    "\\newcommand\\todo{{\\bf TODO }}\n",
    "\\newcommand\\prob{\\mathbb P}\n",
    "\\newcommand\\esp{\\mathbb E}\n",
    "\\newcommand\\var{\\mathbb V}\n",
    "\\newcommand\\tribu{\\mathfrak T}$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "486fb3d5",
   "metadata": {},
   "source": [
    "# L'approche fréquentiste des probabilités : un échec\n",
    "\n",
    "Marc Lorenzi\n",
    "\n",
    "4 mai 2026"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "11402611",
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "import math\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9ac07dfa",
   "metadata": {},
   "source": [
    "## 1. Une expérience"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5d0fcb59",
   "metadata": {},
   "source": [
    "Soit $p\\in\\N^*\\setminus\\{1\\}$. Tout réel $x>0$ s'écrit de façon unique\n",
    "\n",
    "$$x=M_p(x)p^{E_p(x)}$$\n",
    "\n",
    "où $M_p(x)\\in[1,p[$ et $E_p(x)\\in\\Z$. \n",
    "\n",
    "- Le réel $M_p(x)$ est la *mantisse* de $x$ en base $p$.\n",
    "- L'entier $E_p(x)$ est *l'exposant* de $x$ en base $p$.\n",
    "\n",
    "Le *premier chiffre* de $x$ en base $p$ est $C_p(x)=\\lfloor M_p(x)\\rfloor$."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "150c06d2",
   "metadata": {},
   "source": [
    "La fonction `random_premier_chiffre` prend en paramètre un entier $p\\ge 2$. Elle renvoie le premier chiffre d'un réel strictement positif « aléatoire ». "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aa3c0bea",
   "metadata": {},
   "outputs": [],
   "source": [
    "def random_premier_chiffre(p):\n",
    "    return math.floor(p ** random.uniform(0, 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0953a5ca",
   "metadata": {},
   "outputs": [],
   "source": [
    "print([random_premier_chiffre(10) for k in range(20)])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8568066d",
   "metadata": {},
   "source": [
    "La fonction `frequences` prend en paramètre une liste d'entiers `xs` de longueur $n$ et un entier $a$. Pour tout $k\\in\\bbr0{n-1}$, soit $f_k$ la fréquence d'apparition de $a$ dans les $k+1$ premiers éléments de `xs` (bref, le nombre de $a$ divisé par $k+1$). La fonction renvoie la liste $[f_0,\\ldots,f_{n-1}]$. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9abddcb0",
   "metadata": {},
   "outputs": [],
   "source": [
    "def frequences(xs, a):\n",
    "    s = 0\n",
    "    n = len(xs)\n",
    "    ms = []\n",
    "    for k in range(n):\n",
    "        if xs[k] == a: s += 1\n",
    "        ms.append(s / (k + 1))\n",
    "    return ms"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4c9da046",
   "metadata": {},
   "source": [
    "Calculons le premier chiffre en base 10 de quelques réels « aléatoires » et traçons les fréquences d'apparition de l'entier 1."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2d09a65a",
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.rcParams['figure.figsize'] = (8, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f9f9545e",
   "metadata": {},
   "outputs": [],
   "source": [
    "n, p = 1000, 10\n",
    "ns = range(n)\n",
    "xs = [random_premier_chiffre(p) for k in range(n)]\n",
    "ys = frequences(xs, 1)\n",
    "plt.plot(ns, ys, 'k', lw=0.5)\n",
    "plt.ylim(0, 0.6)\n",
    "plt.grid()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c95ee1fe",
   "metadata": {},
   "source": [
    "Après quelques bizarreries, les fréquences semblent se stabiliser aux alentours d'un réel de l'ordre de $0.3$."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bab07785",
   "metadata": {},
   "source": [
    "## 2. Et alors ?"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "65b20491",
   "metadata": {},
   "source": [
    "Eh bien, rien du tout.\n",
    "\n",
    "Et si je prends $n=10^{10}$ ? Et si j'évalue la cellule un trillion de fois ? Une **infinité** de fois ? Et puis, c'est quoi un réel « aléatoire » ? Tout ceci n'a pas grand sens ...\n",
    "\n",
    "Le but du cours de probabilités est justement de donner un sens à tout ceci. Une fois ce cours terminé, nous comprendrons alors l'énoncé du théorème ci-dessous ... et nous ne serons toujours pas capables de le prouver."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "78bd8b91",
   "metadata": {},
   "source": [
    "<div style=\"border: 1px solid black; padding: 10px;background-color:#CCFFCC;color:#000000;border-radius:10px;\" >\n",
    "    <b>Proposition.</b> Soit $(\\Omega,\\mathcal T,\\prob)$ un espace probabilisé. On munit l'ensemble $E=[1,10[$ de la tribu des boréliens. Soit $\\mathcal F=(X_n)_{n\\ge 1}$ une suite de variables aléatoires définies sur l'univers $\\Omega$, à valeurs dans $E$, suivant la loi de Benford de paramètre $10$. On suppose la suite $\\mathcal F$ indépendante. Pour tout $n\\ge 1$, soit<br><br>\n",
    "    $$Y_n=\\mathbb 1_{\\{C_{10}(X_n)=1\\}}$$\n",
    "On a alors, lorsque $n$ tend vers l'infini,\n",
    "    $$\\frac 1 n\\sum_{k=1}^n Y_k\\overset{p.s.}\\too\\log 2$$\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "95ac8c93",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(math.log(2, 10))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "08a29686",
   "metadata": {},
   "source": [
    "Questions en vrac :\n",
    "\n",
    "1. Qu'est ce qu'un **espace probabilisé** ? un **univers** ?\n",
    "2. Qu'est-ce qu'une **tribu** ?\n",
    "3. C'est quoi, $\\prob$ ?\n",
    "4. Qu'est-ce qu'un **borélien** ?\n",
    "5. Qu'est-ce qu'une **variable aléatoire** ?\n",
    "6. Qu'est ce que la **loi de Benford** ?\n",
    "7. Qu'est-ce qu'une famille **indépendante** ?\n",
    "8. Que veut dire $\\{C_{10}(X_n)=1\\}$ ?\n",
    "9. Que signifie le « p.s. » au-dessus de la flèche ?\n",
    "\n",
    "Nous répondrons dans le cours de probas aux questions 1, 2, 3, 4, 5, 7 et 8. Laissons la question 9 au cours de l'année prochaine et la question 6 à un éventuel TIPE."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4642cfe4",
   "metadata": {},
   "source": [
    "#### « *Long is the road, Hard is the way, Heavy is my load, But deep is my faith.* »"
   ]
  }
 ],
 "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.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
