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

Question 8

parent c11cfe3f
......@@ -22,8 +22,10 @@
"import numpy as np\n",
"import scipy as sp\n",
"import scipy.stats as spst\n",
"import scipy.special as spsp\n",
"import matplotlib.pyplot as plt\n",
"import itertools\n",
"import math\n",
"\n",
"from __future__ import print_function\n",
"from ipywidgets import interact, interactive, fixed, interact_manual\n",
......@@ -284,7 +286,7 @@
},
{
"cell_type": "markdown",
"id": "78974b3b",
"id": "57359d84",
"metadata": {},
"source": [
"Qu 7\n",
......@@ -301,7 +303,7 @@
},
{
"cell_type": "markdown",
"id": "918e429c",
"id": "144c1f11",
"metadata": {},
"source": [
"On a :\n",
......@@ -326,7 +328,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "0af57523",
"id": "50098dfe",
"metadata": {},
"outputs": [],
"source": [
......@@ -345,15 +347,69 @@
"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",
" n = widgets.IntSlider(min = 1, max = 50),\n",
" x = widgets.IntSlider(min = 0, max = 50)\n",
")"
]
},
{
"cell_type": "markdown",
"id": "c57157f4",
"metadata": {},
"source": [
"8) Predictive distribution of $x_0$ new positive tries over $n_0$ tries, given $x, \\alpha, \\beta$ ?\n",
"\n",
"$$p_{X_0}(x_0 | x; \\alpha; \\beta) = \\int_0^1p_{X_0}(x_0 | t)\\pi_\\theta(t | x; \\alpha; \\beta)dt = \\frac{\\begin{pmatrix}n_0 \\\\ x_0\\end{pmatrix}}{B(\\alpha + x, \\beta + n -x)}B(\\alpha + x + x_0, \\beta + n + n_0 - x - x_0)$$"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8022b70b",
"metadata": {},
"outputs": [],
"source": [
"def fact(n):\n",
" if n == 0 :\n",
" return 1\n",
" return n * fact(n-1)\n",
"\n",
"def comb(n, k):\n",
" n = int(n)\n",
" k = int(k)\n",
" return fact(n) / (fact(k) * fact(n - k))\n",
"\n",
"def estimate(sample, a, b, n, x, n0):\n",
" res = []\n",
" for x0 in sample:\n",
" res.append(comb(n0, x0) / spsp.beta(a + x, b + n - x) * spsp.beta(a + x + x0, b + n + n0 - x - x0))\n",
" return res# math.comb(n0, x0) # / spsp.beta(a + x, b + n - x) * spsp.beta(a + x + x0, b + n + n0 - x - x0)\n",
"\n",
"def estimate_array(sample, a, b, n, x, n0):\n",
" return map(estimate, sample, a, b, n, x, n0)\n",
"\n",
"def predict(a, b, n, x, n0):\n",
" sample = np.arange(n0 + 1)\n",
" fig, ax = plt.subplots()\n",
" ax.bar(sample, estimate(sample, a, b, n, x, n0), width, label='Estimation of x0')\n",
"\n",
" ax.legend()\n",
" ax.set_title('Estimation of x0')\n",
" plt.show()\n",
"\n",
"interact(predict, \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 = 50),\n",
" x = widgets.IntSlider(min = 0, max = 50),\n",
" n0 = widgets.IntSlider(min = 1, max = 15)\n",
")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b864cb67",
"id": "aacbfb71",
"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