01: Plot dim d volumes, calculate variance of Metropolis

This commit is contained in:
2022-09-13 10:43:48 +02:00
parent bc85efdb6f
commit e507977f46

View File

@ -537,13 +537,17 @@
" return np.pi**(d/2)/scipy.special.gamma(d/2 + 1)\n", " return np.pi**(d/2)/scipy.special.gamma(d/2 + 1)\n",
"\n", "\n",
"N = 10000\n", "N = 10000\n",
"for d in range(1, 8):\n", "d_range = np.array(range(1, 8))\n",
" print(\"N = {}\".format(N),\n", "V_d_mc = [number_of_hits_in_d_dimensions(N, d)/N*2**d for d in d_range]\n",
" \"\\td = {}\".format(d),\n", "\n",
" \"\\tV_d = {:.3f}\".format(volume_d_ball(d)),\n", "## Plotting\n",
" \"\\tdirect-sampled V_d = {:5.3f}\".format(number_of_hits_in_d_dimensions(N, d)/N*2**d)\n", "plt.figure()\n",
" )\n", "plt.plot(d_range, V_d_mc, label=\"direct-sampling\")\n",
"# TODO: Plot the results as is asked." "plt.plot(d_range, volume_d_ball(d_range), label=\"exact\")\n",
"plt.xlabel(\"dimension $d$\")\n",
"plt.ylabel(\"volume $V_d$\")\n",
"plt.legend()\n",
"plt.show()"
] ]
}, },
{ {
@ -572,7 +576,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 14, "execution_count": 11,
"metadata": { "metadata": {
"deletable": false, "deletable": false,
"nbgrader": { "nbgrader": {
@ -592,37 +596,37 @@
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"N = 2000 \tdelta = 0.000 \tpi_estimate = 4.000\n", "N delta 4*hits/trials E[(4*hits/trials - pi)^2]\n",
"N = 2000 \tdelta = 0.333 \tpi_estimate = 3.149\n", "2000 0.0000 4.0000 0.737\n",
"N = 2000 \tdelta = 0.667 \tpi_estimate = 3.139\n", "2000 0.3333 3.1409 0.014\n",
"N = 2000 \tdelta = 1.000 \tpi_estimate = 3.135\n", "2000 0.6667 3.1351 0.005\n",
"N = 2000 \tdelta = 1.333 \tpi_estimate = 3.146\n", "2000 1.0000 3.1381 0.006\n",
"N = 2000 \tdelta = 1.667 \tpi_estimate = 3.150\n", "2000 1.3333 3.1406 0.004\n",
"N = 2000 \tdelta = 2.000 \tpi_estimate = 3.150\n", "2000 1.6667 3.1316 0.007\n",
"N = 2000 \tdelta = 2.333 \tpi_estimate = 3.141\n", "2000 2.0000 3.1377 0.007\n",
"N = 2000 \tdelta = 2.667 \tpi_estimate = 3.146\n", "2000 2.3333 3.1311 0.011\n",
"N = 2000 \tdelta = 3.000 \tpi_estimate = 3.146\n" "2000 2.6667 3.1547 0.011\n",
"2000 3.0000 3.1417 0.015\n"
] ]
} }
], ],
"source": [ "source": [
"N = 2000 # number of trials\n", "N = 2000 # number of trials\n",
"m = 200 # number of repetitions\n", "m = 200 # number of repetitions\n",
"delta_range = np.linspace(0, 3, 10) # throwing ranges\n",
"\n", "\n",
"p0 = np.array([0., 0.]) # starting position in origin\n", "p0 = np.array([0., 0.]) # starting position in origin\n",
"hits = np.zeros((len(delta_range), m), dtype=int)\n",
"\n", "\n",
"for delta in np.linspace(0, 3, 10):\n", "for index_delta, delta in enumerate(delta_range):\n",
" pi_estimate = 0\n",
" for i in range(m):\n", " for i in range(m):\n",
" hits = markov_pebble(p0, delta, N)\n", " hits[index_delta][i] = markov_pebble(p0, delta, N)\n",
" pi_estimate += hits/N*4\n",
" pi_estimate /= m\n",
" print(\"N = {}\".format(N),\n",
" \"\\tdelta = {:.3f}\".format(delta),\n",
" \"\\tpi_estimate = {:.3f}\".format(pi_estimate)\n",
" )\n",
"\n", "\n",
"# TODO: Investigate the rejection rate and the mean square deviation. Maybe only the latter, as the next question concerns the former." "print(\"{:4} {:6} {:8} {:<10}\".format('N','delta','4*hits/trials','E[(4*hits/trials - pi)^2]'))\n",
"for index_delta, delta in enumerate(delta_range):\n",
" pi_estimates = hits[index_delta]/N*4\n",
" print(\"{:4} {:1.4f} {:1.4f} {:1.3f}\"\n",
" .format(N, delta, np.mean(pi_estimates), np.mean((pi_estimates - np.pi)**2)))"
] ]
}, },
{ {