124 lines
2.8 KiB
Plaintext
124 lines
2.8 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 8,
|
|
"id": "3b5b09c5-9188-49d8-a6c8-2f89ea1c7d7a",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from random import random\n",
|
|
"import numpy as np\n",
|
|
"\n",
|
|
"\n",
|
|
"def decide (prob):\n",
|
|
" return True if random() <= prob else False\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "6e68a721",
|
|
"metadata": {},
|
|
"source": [
|
|
"Question A: Try to simulate Figure. 1.11, assuming that probability to return to start if right is 0.9 while probability to return to start if attempt is wrong is 0.99. Assume that right and wrong are produced equally often and start 100000 times and for each time count how many end in right, respectively wrong state with 1 sorting step, and with 2 sorting steps."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 24,
|
|
"id": "56a4b642-2454-4021-98dd-dc95b07ae5bf",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"244509.0\n",
|
|
"270193.0\n",
|
|
"485298\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"N = int(1e6)\n",
|
|
"\n",
|
|
"isright = True\n",
|
|
"n = 0\n",
|
|
"\n",
|
|
"n_wrong = np.zeros(N)\n",
|
|
"n_right = np.zeros(N)\n",
|
|
"n_0 = 0\n",
|
|
"\n",
|
|
"for i in range(0,N):\n",
|
|
" if n == 0:\n",
|
|
" isright = decide(.5)\n",
|
|
" n += 1\n",
|
|
" n_0 += 1\n",
|
|
" \n",
|
|
" elif isright:\n",
|
|
" n_right[n] += 1\n",
|
|
" if decide(0.9):\n",
|
|
" n = 0\n",
|
|
" else:\n",
|
|
" n += 1\n",
|
|
" else:\n",
|
|
" n_wrong[n] += 1\n",
|
|
" if decide(0.99):\n",
|
|
" n = 0\n",
|
|
" else:\n",
|
|
" n += 1\n",
|
|
" \n",
|
|
" \n",
|
|
"print(np.sum(n_wrong))\n",
|
|
"print(np.sum(n_right))\n",
|
|
"print(n_0)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "8b1131fa-c7f8-4ff6-81cd-5ae6eec2d03c",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "81e08034-a023-4309-8314-44bcca4b052c",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "3356adc3-ef39-4e63-8f2c-d1e4cec9c503",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3",
|
|
"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.7.3"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|