Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 58 additions & 4 deletions rabbits-and-foxes-kmc-question.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -79,13 +79,67 @@
}
},
"outputs": [],
"source": []
"source": [
"\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"time = 0\n",
"rabbit = 400\n",
"fox = 200\n",
"\n",
"times = [time]\n",
"rabbits = [rabbit]\n",
"foxes = [fox]\n",
"\n",
"end_time = 600\n",
"\n",
"k1 = 0.015 # birth rate of rabbits /day\n",
"k2 = 0.00004 # death rate of rabbits due to foxes /day\n",
"k3 = 0.0004 # birth rate of foxes due to rabbits /day\n",
"k4 = 0.04 # death rate of foxes /day\n",
"\n",
"while time < end_time:\n",
" rate_rabbit_birth = k1 * rabbit\n",
" rate_rabbit_death = k2 * rabbit * fox\n",
" rate_fox_birth = k3 * rabbit * fox\n",
" rate_fox_death = k4 * fox\n",
"\n",
" rates = np.array([rate_rabbit_birth, rate_rabbit_death, rate_fox_birth, rate_fox_death])\n",
" cumulative_rates = rates\n",
" total_rate = cumulative_rates[-1]\n",
"\n",
" u = np.random.uniform(0,1)\n",
" break # exit loop while its incomplete\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for n in range(10):\n",
" u = np.random.uniform(0,1)\n",
" for i, cum_rate in enumerate(cumulative_rates):\n",
" if cum_rate/total_rate > u:\n",
" event = i\n",
" break\n",
" print(u, event )\n",
" "
]
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "5137",
"display_name": "jb",
"language": "python",
"name": "python3"
},
Expand All @@ -99,7 +153,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
"version": "3.12.11"
}
},
"nbformat": 4,
Expand Down