update lecture for today

This commit is contained in:
Andrew Straw 2024-11-08 09:44:08 +01:00
parent 3814634427
commit fb47f661d0

View file

@ -5,8 +5,147 @@
"id": "a795ba59-d48d-4f8d-934e-2cbc558071e4",
"metadata": {},
"source": [
"# News: \n",
"\n",
"I enabled a `.ipynb` viewer at https://strawlab-rp2.zoologie.uni-freiburg.de/straw/pm21-dragon.\n",
"\n",
"# `exercise-04` review\n",
"\n",
"## Flow control Q3\n",
"\n",
"Now create a function called `simulate_generation` which takes two arguments, `a` and `Rt`. This function should return `None`. The first argument `a` will be a list with the number of infected individuals and `Rt` is the effective reproduction number, as before. The function should compute the number of newly infected individuals after one generation and append this to the list passed as the first argument.\n",
"\n",
"Your function signature should look like this:\n",
"\n",
"```python\n",
" def simulate_generation(a,Rt):\n",
"```\n",
"\n",
"Here is an example that works:\n",
"\n",
"```python\n",
"b = [1]\n",
"simulate_generation(b,3)\n",
"simulate_generation(b,3)\n",
"simulate_generation(b,3)\n",
"b\n",
"```\n",
"\n",
"In this above example, `b` would be equal to `[1, 3, 9, 27]` at the end."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5dbf80df-9df2-479b-bc33-fa5ee7465faa",
"metadata": {},
"outputs": [],
"source": [
"def simulate_generation(a,Rt):\n",
" previous = a[-1]\n",
" new_infected = previous*Rt\n",
" a.append(new_infected)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "13f871cc-57bd-466b-ab2c-335097d45871",
"metadata": {},
"outputs": [],
"source": [
"b = [1]\n",
"simulate_generation(b,3)\n",
"simulate_generation(b,3)\n",
"simulate_generation(b,3)\n",
"b"
]
},
{
"cell_type": "markdown",
"id": "1d4cbed4-d429-423c-9730-8eec2e7be271",
"metadata": {},
"source": [
"## Flow control Q4\n",
"\n",
"Now create a function called `calculate_timeseries_to_1000` which takes one argument, `Rt`. This function should return a list containing the number of infected individuals after each cycle up to and including the first value over 1000 individuals.\n",
"\n",
"Your function signature should look like this:\n",
"\n",
"```python\n",
" def calculate_timeseries_to_1000(Rt):\n",
"```\n",
"\n",
"Your code should work so that `calculate_timeseries_to_1000(2)` would return `[1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024]` and that `calculate_timeseries_to_1000(3)` would return `[1, 3, 9, 27, 81, 243, 729, 2187]`."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "73d2d5a4-7697-4e91-9e0f-95f6ba36511b",
"metadata": {},
"outputs": [],
"source": [
"# Here is a wrong answer from someone:\n",
"\n",
"def calculate_timeseries_to_1000(Rt):\n",
" infected_series = [1]\n",
" while infected_series[-1] <1000:\n",
" print(infected_series)\n",
" infected_series = infected_series * Rt"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "eba13625-66b6-4910-8cdc-67b8cba4e6e0",
"metadata": {},
"outputs": [],
"source": [
"# Let's try to run it:\n",
"\n",
"calculate_timeseries_to_1000(2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fa76fadc-1ab5-42b3-9884-7542d5df0cac",
"metadata": {},
"outputs": [],
"source": [
"def calculate_timeseries_to_1000(Rt):\n",
" a = [1]\n",
" while a[-1] < 1000:\n",
" simulate_generation(a,Rt)\n",
" return a"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "56ea8eec-1128-4415-94ae-a1060cc2443c",
"metadata": {},
"outputs": [],
"source": [
"calculate_timeseries_to_1000(2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2b32e617-dc56-4f32-8953-ce4c4a12dce8",
"metadata": {},
"outputs": [],
"source": [
"calculate_timeseries_to_1000(3)"
]
},
{
"cell_type": "markdown",
"id": "8ef5d53f-d007-4727-8852-5d430eb7fef7",
"metadata": {},
"source": [
"* I check your answers based on file name. Please keep the files names exactly as specified, i.e. `my_name.py`.\n",
"\n",
"* Example answers:\n",