Commit c11cfe3f authored by Rémy Huet's avatar Rémy Huet 💻
Browse files

Questions 6 & 7

parent 748fdd99
......@@ -282,10 +282,78 @@
" plt.show()"
]
},
{
"cell_type": "markdown",
"id": "78974b3b",
"metadata": {},
"source": [
"Qu 7\n",
"\n",
"X ~ $B(n, \\theta)$ n connu et $\\theta$ ~ beta($\\alpha, \\beta$)\n",
"\n",
"On a $\\theta \\in [0;1]$\n",
"\n",
"On peut calculer : \n",
"* La distribution jointe $\\phi(x, t) = f(x | t)\\pi_\\theta(t)$\n",
"* La distribution marginale de X $f_X(x) = \\int_{\\Theta}\\phi(x,t)dt$\n",
"* La distribution postérieure de $\\theta$ sachant $X$ $\\pi_\\theta(t|x) = \\frac{\\phi(x,t)}{f_X(x)}$"
]
},
{
"cell_type": "markdown",
"id": "918e429c",
"metadata": {},
"source": [
"On a :\n",
"$$\\pi_\\theta(t,\\alpha,\\beta) = \\frac{t^{\\alpha - 1}(1 - t)^{\\beta - 1}}{B(\\alpha,\\beta)}$$\n",
"$$f_X(x|\\theta) = \\begin{pmatrix} n \\\\ x \\end{pmatrix} \\theta^x (1 - \\theta)^{n - x} $$\n",
"\n",
"La distribution jointe est :\n",
"$$\\phi(x, t) = \\begin{pmatrix} n \\\\ x \\end{pmatrix} \\frac{t^{\\alpha + x - 1}(1 - t)^{\\beta + n - x - 1}}{B(\\alpha, \\beta)}$$\n",
"\n",
"La distribution marginale de X est :\n",
"$$f_X(x) = \\int_0^1\\phi(x, t)dt = \\int_0^1\\begin{pmatrix} n \\\\ x \\end{pmatrix}\\frac{t^{\\alpha + x - 1}(1 - t)^{\\beta + n - x - 1}}{B(\\alpha, \\beta)}dt = \\frac{\\begin{pmatrix} n \\\\ x \\end{pmatrix}}{B(\\alpha, \\beta)} \\int_0^1 t^{\\alpha + x - 1}(1 - t)^{\\beta + n - x - 1}$$\n",
"\n",
"Soit $$f_X(x) = \\frac{\\begin{pmatrix} n \\\\ x \\end{pmatrix}}{B(\\alpha, \\beta)} B(\\alpha + x, \\beta + n - x)$$\n",
"\n",
"On en déduit la distribution postérieure\n",
"\n",
"$$\\pi_\\theta(t | x) = \\frac{t^{\\alpha + x - 1}(1 - t)^{\\beta + n - x - 1}}{B(\\alpha + x, \\beta + n - x)}$$\n",
"\n",
"On retrouve la loi bêta corrigée par l'échantillon."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0af57523",
"metadata": {},
"outputs": [],
"source": [
"def trace(a, b, n, x):\n",
" sample = np.linspace(0, 1, 100)\n",
" fig, ax = plt.subplots()\n",
" ax.plot(sample, spst.beta.pdf(sample, a, b), label='Prior distribution')\n",
" ax.plot(sample, spst.binom.pmf(x, n, sample), label=\"Likehood of theta\")\n",
" ax.plot(sample, spst.beta.pdf(sample, a + x, b + n - x), label=\"Posterior distribution\")\n",
" \n",
" ax.legend()\n",
" ax.set_title(\"Prior distribution, likehood of theta and posterior distribution\")\n",
"\n",
" plt.show()\n",
"\n",
"interact(trace, \n",
" a = widgets.FloatSlider(min = 0.1, max = 10, step = 0.1), \n",
" b = widgets.FloatSlider(min = 0.1, max = 10, step = 0.1),\n",
" n = widgets.IntSlider(min = 1, max = 20),\n",
" x = widgets.IntSlider(min = 0, max = 20)\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d2ee0d03",
"id": "b864cb67",
"metadata": {},
"outputs": [],
"source": []
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment