01: Add Metropolis algorithm attempt

This commit is contained in:
2022-09-09 12:24:23 +02:00
parent 417497e25f
commit bc85efdb6f

View File

@ -542,7 +542,8 @@
" \"\\td = {}\".format(d),\n",
" \"\\tV_d = {:.3f}\".format(volume_d_ball(d)),\n",
" \"\\tdirect-sampled V_d = {:5.3f}\".format(number_of_hits_in_d_dimensions(N, d)/N*2**d)\n",
" )"
" )\n",
"# TODO: Plot the results as is asked."
]
},
{
@ -571,7 +572,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 14,
"metadata": {
"deletable": false,
"nbgrader": {
@ -586,10 +587,42 @@
"task": false
}
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"N = 2000 \tdelta = 0.000 \tpi_estimate = 4.000\n",
"N = 2000 \tdelta = 0.333 \tpi_estimate = 3.149\n",
"N = 2000 \tdelta = 0.667 \tpi_estimate = 3.139\n",
"N = 2000 \tdelta = 1.000 \tpi_estimate = 3.135\n",
"N = 2000 \tdelta = 1.333 \tpi_estimate = 3.146\n",
"N = 2000 \tdelta = 1.667 \tpi_estimate = 3.150\n",
"N = 2000 \tdelta = 2.000 \tpi_estimate = 3.150\n",
"N = 2000 \tdelta = 2.333 \tpi_estimate = 3.141\n",
"N = 2000 \tdelta = 2.667 \tpi_estimate = 3.146\n",
"N = 2000 \tdelta = 3.000 \tpi_estimate = 3.146\n"
]
}
],
"source": [
"# YOUR CODE HERE\n",
"raise NotImplementedError()"
"N = 2000 # number of trials\n",
"m = 200 # number of repetitions\n",
"\n",
"p0 = np.array([0., 0.]) # starting position in origin\n",
"\n",
"for delta in np.linspace(0, 3, 10):\n",
" pi_estimate = 0\n",
" for i in range(m):\n",
" hits = 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",
"# TODO: Investigate the rejection rate and the mean square deviation. Maybe only the latter, as the next question concerns the former."
]
},
{