bondscell_results$f1f89502-0494-11eb-2303-0b79d8bbd13fqueued¤logsrunning¦outputbody;frequencies_plot_with_mean (generic function with 1 method)mimetext/plainrootassigneelast_run_timestampAtGpersist_js_state·has_pluto_hook_features§cell_id$f1f89502-0494-11eb-2303-0b79d8bbd13fdepends_on_disabled_cells§runtimeRpublished_object_keysdepends_on_skipped_cells§errored$95771ce2-0403-11eb-3056-f1dc3a8b7ec3queued¤logsrunning¦outputbodyh

👉 Write a function simulation that does the following:

  1. Generate the $N$ agents.

  2. Run sweep! a number $T$ of times. Calculate and store the total number of agents with each status at each step in variables S_counts, I_counts and R_counts.

  3. Return the vectors S_counts, I_counts and R_counts in a named tuple, with keys S, I and R.

You've seen an example of named tuples before: the student variable at the top of the notebook!

Feel free to store the counts in a different way, as long as the return type is the same.

mimetext/htmlrootassigneelast_run_timestampA|persist_js_state·has_pluto_hook_features§cell_id$95771ce2-0403-11eb-3056-f1dc3a8b7ec3depends_on_disabled_cells§runtimeʢpublished_object_keysdepends_on_skipped_cells§errored$e6219c7c-0420-11eb-3faa-13126f7c8007queued¤logsrunning¦outputbody٤

Abstraction, lines 1-219

Array Basics, lines 1-137

Course Intro, lines 1-44

(for example)

mimetext/htmlrootassigneelines_i_editedlast_run_timestampA׬3Ypersist_js_state·has_pluto_hook_features§cell_id$e6219c7c-0420-11eb-3faa-13126f7c8007depends_on_disabled_cells§runtimeصpublished_object_keysdepends_on_skipped_cells§errored$b817f466-04d4-11eb-0a26-c1c667f9f7f7queued¤logsrunning¦outputbodyـ

Got it!

Splendid!

mimetext/htmlrootassigneelast_run_timestampA persist_js_state·has_pluto_hook_features§cell_id$b817f466-04d4-11eb-0a26-c1c667f9f7f7depends_on_disabled_cells§runtimeƵpublished_object_keysdepends_on_skipped_cells§errored$08e2bc64-0417-11eb-1457-21c0d18e8c51queued¤logsrunning¦outputbody

Hint

Do you remember how we worked with dictionaries in Homework 3? You can create an empty dictionary using Dict(). You may want to use either the function haskey or the function get on your dictionary – check the documentation for how to use these functions.

mimetext/htmlrootassigneelast_run_timestampA׾ΰpersist_js_state·has_pluto_hook_features§cell_id$08e2bc64-0417-11eb-1457-21c0d18e8c51depends_on_disabled_cells§runtime-0published_object_keysdepends_on_skipped_cells§errored$bb8aeb58-042f-11eb-18b8-f995631df619queued¤logsrunning¦outputbody

As you separately vary $p$ and $N$, what do you observe about the mean in each case? Does that make sense?

mimetext/htmlrootassigneelast_run_timestampAߙȰpersist_js_state·has_pluto_hook_features§cell_id$bb8aeb58-042f-11eb-18b8-f995631df619depends_on_disabled_cells§runtimeǵpublished_object_keysdepends_on_skipped_cells§errored$223933a4-042c-11eb-10d3-852229f25a35queued¤logsrunning¦outputbodymimetext/plainrootassigneelast_run_timestampAypersist_js_state·has_pluto_hook_features§cell_id$223933a4-042c-11eb-10d3-852229f25a35depends_on_disabled_cells§runtime published_object_keysdepends_on_skipped_cells§errored$ae4ac4b4-041f-11eb-14f5-1bcde35d18f2queued¤logsrunning¦outputbodymimetext/plainrootassigneelast_run_timestampApersist_js_state·has_pluto_hook_features§cell_id$ae4ac4b4-041f-11eb-14f5-1bcde35d18f2depends_on_disabled_cells§runtime)published_object_keysdepends_on_skipped_cells§errored$7f635722-04d0-11eb-3209-4b603c9e843cqueued¤logsrunning¦outputbody
mimetext/htmlrootassigneelast_run_timestampAׇpersist_js_state·has_pluto_hook_features§cell_id$7f635722-04d0-11eb-3209-4b603c9e843cdepends_on_disabled_cells§runtime0P published_object_keysdepends_on_skipped_cells§errored$7c515a7a-04d5-11eb-0f36-4fcebff709d5queued¤logsrunning¦outputbodyٜ

Keep working on it!

The answer is not quite right.

mimetext/htmlrootassigneelast_run_timestampAepersist_js_state·has_pluto_hook_features§cell_id$7c515a7a-04d5-11eb-0f36-4fcebff709d5depends_on_disabled_cells§runtimepublished_object_keysdepends_on_skipped_cells§errored$107e65a4-0403-11eb-0c14-37d8d828b469queued¤logsrunning¦outputbodyT

Let's create a package environment:

mimetext/htmlrootassigneelast_run_timestampAܳppersist_js_state·has_pluto_hook_features§cell_id$107e65a4-0403-11eb-0c14-37d8d828b469depends_on_disabled_cells§runtime`published_object_keysdepends_on_skipped_cells§errored$1c6aa208-04d1-11eb-0b87-cf429e6ff6d0queued¤logsrunning¦outputbodymimetext/plainrootassigneelast_run_timestampApersist_js_state·has_pluto_hook_features§cell_id$1c6aa208-04d1-11eb-0b87-cf429e6ff6d0depends_on_disabled_cells§runtime%published_object_keysdepends_on_skipped_cells§errored$80e6f1e0-04b1-11eb-0d4e-475f1d80c2bbqueued¤logsrunning¦outputbody

In the cell below, we plot the evolution of the number of $I$ individuals as a function of time for each of the simulations on the same plot using transparency (alpha=0.5 inside the plot command).

mimetext/htmlrootassigneelast_run_timestampA Opersist_js_state·has_pluto_hook_features§cell_id$80e6f1e0-04b1-11eb-0d4e-475f1d80c2bbdepends_on_disabled_cells§runtime@`published_object_keysdepends_on_skipped_cells§errored$7f4e121c-041d-11eb-0dff-cd0cbfdfd606queued¤logsrunning¦outputbodymissingmimetext/plainrootassigneetest_statuslast_run_timestampABpersist_js_state·has_pluto_hook_features§cell_id$7f4e121c-041d-11eb-0dff-cd0cbfdfd606depends_on_disabled_cells§runtime- published_object_keysdepends_on_skipped_cells§errored$3d3b672c-0426-11eb-0a36-153ce3c276b9queued¤logsrunning¦outputbody+simulation (generic function with 1 method)mimetext/plainrootassigneelast_run_timestampA֯.persist_js_state·has_pluto_hook_features§cell_id$3d3b672c-0426-11eb-0a36-153ce3c276b9depends_on_disabled_cells§runtime+published_object_keysdepends_on_skipped_cells§errored$4f19e872-0414-11eb-0dfd-e53d2aecc4dcqueued¤logsrunning¦outputbodyn

Function library

Just some helper functions used in the notebook.

mimetext/htmlrootassigneelast_run_timestampApersist_js_state·has_pluto_hook_features§cell_id$4f19e872-0414-11eb-0dfd-e53d2aecc4dcdepends_on_disabled_cells§runtime]published_object_keysdepends_on_skipped_cells§errored$759bc42e-04ab-11eb-0ab1-b12e008c02a9queued¤logsrunning¦outputbody

Got it!

Your function treats the susceptible agent case correctly!

mimetext/htmlrootassigneelast_run_timestampA,=`persist_js_state·has_pluto_hook_features§cell_id$759bc42e-04ab-11eb-0ab1-b12e008c02a9depends_on_disabled_cells§runtimeLpublished_object_keysdepends_on_skipped_cells§errored$5689841e-0414-11eb-0492-63c77ddbd136queued¤logsrunning¦outputbody




mimetext/htmlrootassigneelast_run_timestampABpersist_js_state·has_pluto_hook_features§cell_id$5689841e-0414-11eb-0492-63c77ddbd136depends_on_disabled_cells§runtime)|published_object_keysdepends_on_skipped_cells§errored$99ef7b2a-0403-11eb-08ef-e1023cd151aequeued¤logsrunning¦outputbody

👉 Make a new method for the interact! function that accepts the new infection type as argument, reusing as much functionality as possible from the previous version.

Write it in the same cell as our previous interact! method, and use a begin block to group the two definitions together.

mimetext/htmlrootassigneelast_run_timestampAwͰpersist_js_state·has_pluto_hook_features§cell_id$99ef7b2a-0403-11eb-08ef-e1023cd151aedepends_on_disabled_cells§runtimepublished_object_keysdepends_on_skipped_cells§errored$77b54c10-0403-11eb-16ad-65374d29a817queued¤logsrunning¦outputbody

👉 Write an interactive visualization that draws the histogram and mean for $p$ between $0.01$ (not $0$!) and $1$, and $N$ between $1$ and $100,000$, say. To avoid a naming conflict, call them p_interactive and N_interactive, instead of just p and N.

mimetext/htmlrootassigneelast_run_timestampAmepersist_js_state·has_pluto_hook_features§cell_id$77b54c10-0403-11eb-16ad-65374d29a817depends_on_disabled_cells§runtimeJpublished_object_keysdepends_on_skipped_cells§errored$7768a2dc-0403-11eb-39b7-fd660dc952fequeued¤logsrunning¦outputbodyٲ

👉 Write the function frequencies_plot_with_mean that calculates the mean recovery time and displays it using a vertical line.

mimetext/htmlrootassigneelast_run_timestampASŰpersist_js_state·has_pluto_hook_features§cell_id$7768a2dc-0403-11eb-39b7-fd660dc952fedepends_on_disabled_cells§runtime'Kpublished_object_keysdepends_on_skipped_cells§errored$60a8b708-04c8-11eb-37b1-3daec644ac90queued¤logsrunning¦outputbodymimetext/plainrootassigneelast_run_timestampApersist_js_state·has_pluto_hook_features§cell_id$60a8b708-04c8-11eb-37b1-3daec644ac90depends_on_disabled_cells§runtimeEpublished_object_keysdepends_on_skipped_cells§errored$95eb9f88-0403-11eb-155b-7b2d3a07cff0queued¤logsrunning¦outputbodyO

👉 Write a function sir_mean_error_plot that does the same as sir_mean_plot, which also computes the standard deviation $\sigma$ of $S$, $I$, $R$ at each step. Add this to the plot using error bars, using the option yerr=σ in the plot command; use transparency.

This should confirm that the distribution of $I$ at each step is pretty wide!

mimetext/htmlrootassigneelast_run_timestampApersist_js_state·has_pluto_hook_features§cell_id$95eb9f88-0403-11eb-155b-7b2d3a07cff0depends_on_disabled_cells§runtimeSpublished_object_keysdepends_on_skipped_cells§errored$955321de-0403-11eb-04ce-fb1670dfbb9equeued¤logsrunning¦outputbodyC

👉 Write a function sweep!. It runs step! $N$ times, where $N$ is the number of agents. Thus each agent acts, on average, once per sweep; a sweep is thus the unit of time in our Monte Carlo simulation.

mimetext/htmlrootassigneelast_run_timestampApersist_js_state·has_pluto_hook_features§cell_id$955321de-0403-11eb-04ce-fb1670dfbb9edepends_on_disabled_cells§runtime]published_object_keysdepends_on_skipped_cells§errored$ae70625a-041f-11eb-3082-0753419d6d57queued¤logsrunning¦outputbody

When you define a new type like this, Julia automatically defines one or more constructors, which are methods of a generic function with the same name as the type. These are used to create objects of that type.

👉 Use the methods function to check how many constructors are pre-defined for the Agent type.

mimetext/htmlrootassigneelast_run_timestampA困persist_js_state·has_pluto_hook_features§cell_id$ae70625a-041f-11eb-3082-0753419d6d57depends_on_disabled_cells§runtimepublished_object_keysdepends_on_skipped_cells§errored$843fd63c-04d0-11eb-0113-c58d346179d6queued¤logsrunning¦outputbodymimetext/plainrootassigneelast_run_timestampApersist_js_state·has_pluto_hook_features§cell_id$843fd63c-04d0-11eb-0113-c58d346179d6depends_on_disabled_cells§runtime8published_object_keysdepends_on_skipped_cells§errored$189cae1e-0424-11eb-2666-65bf297d8bddqueued¤logsrunning¦outputbodyٕ

👉 Create an agent test_agent with status S and num_infected equal to 0.

mimetext/htmlrootassigneelast_run_timestampApersist_js_state·has_pluto_hook_features§cell_id$189cae1e-0424-11eb-2666-65bf297d8bdddepends_on_disabled_cells§runtimeTpublished_object_keysdepends_on_skipped_cells§errored$7f744644-041d-11eb-08a0-3719cc0adeb7queued¤logsrunning¦outputbody{

👉 Use the typeof function to find the type of test_status.

mimetext/htmlrootassigneelast_run_timestampApersist_js_state·has_pluto_hook_features§cell_id$7f744644-041d-11eb-08a0-3719cc0adeb7depends_on_disabled_cells§runtimepublished_object_keysdepends_on_skipped_cells§errored$488771e2-049f-11eb-3b0a-0de260457731queued¤logsrunning¦outputbodyprefixMain.workspace#4.AgentelementsprefixAgentelementsstatusI::InfectionStatus = 1text/plainnum_infected0text/plaintypestructprefix_shortAgentobjectid9aee8c8dac38f129!application/vnd.pluto.tree+objectprefixAgentelementsstatusS::InfectionStatus = 0text/plainnum_infected0text/plaintypestructprefix_shortAgentobjectid5815e61b2687a4dd!application/vnd.pluto.tree+objectprefixAgentelementsstatusS::InfectionStatus = 0text/plainnum_infected0text/plaintypestructprefix_shortAgentobjectid484603bc86e7e284!application/vnd.pluto.tree+objecttypeArrayprefix_shortobjectid6412d27f9ae43251mime!application/vnd.pluto.tree+objectrootassigneelast_run_timestampAxpersist_js_state·has_pluto_hook_features§cell_id$488771e2-049f-11eb-3b0a-0de260457731depends_on_disabled_cells§runtime3published_object_keysdepends_on_skipped_cells§errored$393041ec-049f-11eb-3089-2faf378445f3queued¤logsrunning¦outputbodyِ

Got it!

You got the right answer!

mimetext/htmlrootassigneelast_run_timestampA%ܰpersist_js_state·has_pluto_hook_features§cell_id$393041ec-049f-11eb-3089-2faf378445f3depends_on_disabled_cells§runtime^

Before you submit

Remember to fill in your name and Kerberos ID at the top of this notebook.

mimetext/htmlrootassigneelast_run_timestampA׮5persist_js_state·has_pluto_hook_features§cell_id$531d13c2-0414-11eb-0acd-4905a684869ddepends_on_disabled_cells§runtimeU͵published_object_keysdepends_on_skipped_cells§errored$221bc0ac-04aa-11eb-1331-2b16ebc1ee57queued¤logsrunning¦outputbodymsg&UndefVarError: Reinfection not definedstacktracecall_shorttop-level scopeinlined£urlpaths/home/runner/work/disorganised-mess/disorganised-mess/hw4 some solutions.jl#==#221bc0ac-04aa-11eb-1331-2b16ebc1ee57source_packagecalltop-level scopelinfo_typeCore.CodeInfolinefile=hw4 some solutions.jl#==#221bc0ac-04aa-11eb-1331-2b16ebc1ee57functop-level scopeparent_modulefrom_c¤mime'application/vnd.pluto.stacktrace+objectrootassigneelast_run_timestampAր persist_js_state·has_pluto_hook_features§cell_id$221bc0ac-04aa-11eb-1331-2b16ebc1ee57depends_on_disabled_cells§runtimepublished_object_keysdepends_on_skipped_cells§errored$32cea7ba-0429-11eb-06bc-a5f4ae3ffe37queued¤logsrunning¦outputbody'sweep! (generic function with 1 method)mimetext/plainrootassigneelast_run_timestampA֣persist_js_state·has_pluto_hook_features§cell_id$32cea7ba-0429-11eb-06bc-a5f4ae3ffe37depends_on_disabled_cells§runtime published_object_keysdepends_on_skipped_cells§errored$06f30b2a-0403-11eb-0f05-8badebe1011dqueued¤logsrunning¦outputbodyu

Homework 4: Epidemic modeling I

18.S191, fall 2020

This notebook contains built-in, live answer checks! In some exercises you will see a coloured box, which runs a test case on your code, and provides feedback based on the result. Simply edit the code, run it, and the check runs again.

For MIT students: there will also be some additional (secret) test cases that will be run as part of the grading process, and we will look at your notebook and write comments.

Feel free to ask questions!

mimetext/htmlrootassigneelast_run_timestampAܙpersist_js_state·has_pluto_hook_features§cell_id$06f30b2a-0403-11eb-0f05-8badebe1011ddepends_on_disabled_cells§runtime>\published_object_keysdepends_on_skipped_cells§errored$9c39974c-04a5-11eb-184d-317eb542452cqueued¤logsrunning¦outputbodyelementsagentprefixAgentelementsstatusI::InfectionStatus = 1text/plainnum_infected0text/plaintypestructprefix_shortAgentobjectide60322d6a9845160!application/vnd.pluto.tree+objectsourceprefixAgentelementsstatusI::InfectionStatus = 1text/plainnum_infected1text/plaintypestructprefix_shortAgentobjectid676a3c152c5281e3!application/vnd.pluto.tree+objecttypeNamedTupleobjectidd153e50a8f4a4f65mime!application/vnd.pluto.tree+objectrootassigneelast_run_timestampA֍տpersist_js_state·has_pluto_hook_features§cell_id$9c39974c-04a5-11eb-184d-317eb542452cdepends_on_disabled_cells§runtimeBpublished_object_keysdepends_on_skipped_cells§errored$1ddbaa18-0494-11eb-1fc8-250ab6ae89f1queued¤logsrunning¦outputbodyr1
mimetext/htmlrootassigneelast_run_timestampAս^Spersist_js_state·has_pluto_hook_features§cell_id$1ddbaa18-0494-11eb-1fc8-250ab6ae89f1depends_on_disabled_cells§runtime 4published_object_keysdepends_on_skipped_cells§errored$06089d1e-0495-11eb-0ace-a7a7dc60e5b2queued¤logsrunning¦outputbodymissingmimetext/plainrootassigneelast_run_timestampAypersist_js_state·has_pluto_hook_features§cell_id$06089d1e-0495-11eb-0ace-a7a7dc60e5b2depends_on_disabled_cells§runtime,published_object_keysdepends_on_skipped_cells§errored$15187690-0403-11eb-2dfd-fd924faa3513queued¤logslinemsg;Failed to load integration with PlotlyBase & PlotlyKaleido.text/plaincell_id$15187690-0403-11eb-2dfd-fd924faa3513kwargsexceptionmsgيArgumentError: Package PlotlyBase not found in current path: - Run `import Pkg; Pkg.add("PlotlyBase")` to install the PlotlyBase package. stacktracecall_short"require(into::Module, mod::Symbol)inlined£urlehttps://github.com/JuliaLang/julia/tree/742b9abb4dd4621b667ec5bb3434b8b3602f96fd/base/loading.jl#L959path./loading.jlsource_packagecall"require(into::Module, mod::Symbol)linfo_typeCore.MethodInstancelineǤfileloading.jlfuncrequireparent_modulefrom_cŒcall_shorttop-level scopeinlined£urlpath8/home/runner/.julia/packages/Plots/uiCPf/src/backends.jlsource_packagecalltop-level scopelinfo_typeCore.CodeInfoline8filebackends.jlfunctop-level scopeparent_modulefrom_cŒcall_shortevalinlinedãurlpath./boot.jlsource_packagecallevallinfo_typeNothinglineufileboot.jlfuncevalparent_modulefrom_cŒcall_short-_initialize_backend(pkg::Plots.PlotlyBackend)inlined£url?file:///home/runner/.julia/packages/Plots/uiCPf/src/backends.jlpath8/home/runner/.julia/packages/Plots/uiCPf/src/backends.jlsource_packagecall-_initialize_backend(pkg::Plots.PlotlyBackend)linfo_typeCore.MethodInstanceline7filebackends.jlfunc_initialize_backendparent_modulefrom_cŒcall_short!backend(pkg::Plots.PlotlyBackend)inlined£url?file:///home/runner/.julia/packages/Plots/uiCPf/src/backends.jlpath8/home/runner/.julia/packages/Plots/uiCPf/src/backends.jlsource_packagecall!backend(pkg::Plots.PlotlyBackend)linfo_typeCore.MethodInstancelinefilebackends.jlfuncbackendparent_modulefrom_cŒcall_short#plotly#324inlinedãurlpath8/home/runner/.julia/packages/Plots/uiCPf/src/backends.jlsource_packagecall#plotly#324linfo_typeNothinglineVfilebackends.jlfunc#plotly#324parent_modulefrom_cŒcall_shortplotly()inlined£url?file:///home/runner/.julia/packages/Plots/uiCPf/src/backends.jlpath8/home/runner/.julia/packages/Plots/uiCPf/src/backends.jlsource_packagecallplotly()linfo_typeCore.MethodInstancelineVfilebackends.jlfuncplotlyparent_modulefrom_cŒcall_shorttop-level scopeinlined£urlpaths/home/runner/work/disorganised-mess/disorganised-mess/hw4 some solutions.jl#==#15187690-0403-11eb-2dfd-fd924faa3513source_packagecalltop-level scopelinfo_typeCore.CodeInfolinefile=hw4 some solutions.jl#==#15187690-0403-11eb-2dfd-fd924faa3513functop-level scopeparent_modulefrom_c'application/vnd.pluto.stacktrace+objectidPlots_b627f5e6file8/home/runner/.julia/packages/Plots/uiCPf/src/backends.jlgroupbackendslevelWarnlinemsg)1 Updating registry at `~/.julia/registries/General.toml`  Resolving package versions...  Updating `/tmp/jl_SHw1SH/Project.toml`  [91a5bcdd] + Plots v1.40.14  [7f904dfe] + PlutoUI v0.7.64  Updating `/tmp/jl_SHw1SH/Manifest.toml`  [6e696c72] + AbstractPlutoDingetjes v1.3.2  [66dad0bd] + AliasTables v1.1.3  [d1d4a3ce] + BitFlags v0.1.9  [d360d2e6] + ChainRulesCore v1.25.1  [9e997f8a] + ChangesOfVariables v0.1.10  [944b1d66] + CodecZlib v0.7.8  [35d6a980] + ColorSchemes v3.29.0  [3da002f7] + ColorTypes v0.12.1  [c3611d14] + ColorVectorSpace v0.11.0  [5ae59095] + Colors v0.13.1  [34da2185] + Compat v4.16.0  [f0e56b4a] + ConcurrentUtilities v2.5.0  [187b0558] + ConstructionBase v1.5.8  [d38c429a] + Contour v0.6.3  [9a962f9c] + DataAPI v1.16.0  [864edb3b] + DataStructures v0.18.22  [ffbed154] + DocStringExtensions v0.9.5  [460bff9d] + ExceptionUnwrapping v0.1.11  [c87230d0] + FFMPEG v0.4.2  [53c48c17] + FixedPointNumbers v0.8.5  [1fa38f19] + Format v1.3.7  [28b8d3ca] + GR v0.73.6  [42e2da0e] + Grisu v1.0.2  [cd3eb016] + HTTP v1.10.16  [47d2ed2b] + Hyperscript v0.0.5  [ac1192a8] + HypertextLiteral v0.9.5  [b5f81e59] + IOCapture v0.2.5  [3587e190] + InverseFunctions v0.1.17  [92d709cd] + IrrationalConstants v0.2.4  [1019f520] + JLFzf v0.1.11  [692b3bcd] + JLLWrappers v1.7.0  [682c06a0] + JSON v0.21.4  [b964fa9f] + LaTeXStrings v1.4.0  [23fbe1c1] + Latexify v0.16.8  [2ab3a3ac] + LogExpFunctions v0.3.28  [e6f89c97] + LoggingExtras v1.1.0  [6c6e2e6c] + MIMEs v1.1.0  [1914dd2f] + MacroTools v0.5.16  [739be429] + MbedTLS v1.1.9  [442fdcdd] + Measures v0.3.2  [e1d29d7a] + Missings v1.2.0  [77ba4419] + NaNMath v1.0.3  [4d8831e6] + OpenSSL v1.5.0  [bac558e1] + OrderedCollections v1.8.1  [69de0a69] + Parsers v2.8.3  [ccf2f8ad] + PlotThemes v3.3.0  [995b91a9] + PlotUtils v1.4.3  [91a5bcdd] + Plots v1.40.14  [7f904dfe] + PlutoUI v0.7.64  [aea7be01] + PrecompileTools v1.2.1  [21216c6a] + Preferences v1.4.3  [43287f4e] + PtrArrays v1.3.0  [3cdcf5f2] + RecipesBase v1.3.4  [01d81517] + RecipesPipeline v0.6.12  [189a3867] + Reexport v1.2.2  [05181044] + RelocatableFolders v1.0.1  [ae029012] + Requires v1.3.1  [6c6a2e73] + Scratch v1.2.1  [992d4aef] + Showoff v1.0.3  [777ac1f9] + SimpleBufferStream v1.2.0  [a2af1166] + SortingAlgorithms v1.2.1  [860ef19b] + StableRNGs v1.0.3  [82ae8749] + StatsAPI v1.7.1  [2913bbd2] + StatsBase v0.34.4  [62fd8b95] + TensorCore v0.1.1  [3bb67fe8] + TranscodingStreams v0.11.3  [410a4b4d] + Tricks v0.1.10  [5c2747f8] + URIs v1.5.2  [1cfade01] + UnicodeFun v0.4.1  [1986cc42] + Unitful v1.23.1  [45397f5d] + UnitfulLatexify v1.7.0  [41fe7b60] + Unzip v0.2.0  [6e34b625] + Bzip2_jll v1.0.9+0  [83423d85] + Cairo_jll v1.18.5+0  [ee1fde0b] + Dbus_jll v1.16.2+0  [2702e6a9] + EpollShim_jll v0.0.20230411+1  [2e619515] + Expat_jll v2.6.5+0  [b22a6f82] + FFMPEG_jll v4.4.4+1  [a3f928ae] + Fontconfig_jll v2.16.0+0  [d7e528f0] + FreeType2_jll v2.13.4+0  [559328eb] + FriBidi_jll v1.0.17+0  [0656b61e] + GLFW_jll v3.4.0+2  [d2c73de3] + GR_jll v0.73.6+0  [78b55507] + Gettext_jll v0.21.0+0  [7746bdde] + Glib_jll v2.84.0+0  [3b182d85] + Graphite2_jll v1.3.15+0  [2e76f6c2] + HarfBuzz_jll v8.5.1+0  [aacddb02] + JpegTurbo_jll v3.1.1+0  [c1c5ebd0] + LAME_jll v3.100.2+0  [88015f11] + LERC_jll v3.0.0+1  [1d63c593] + LLVMOpenMP_jll v18.1.8+0  [dd4b983a] + LZO_jll v2.10.3+0  [e9f186c6] + Libffi_jll v3.4.7+0  [7e76a0d4] + Libglvnd_jll v1.7.1+1  [94ce4f54] + Libiconv_jll v1.18.0+0  [4b2f31a3] + Libmount_jll v2.41.0+0  [89763e89] + Libtiff_jll v4.5.1+1  [38a345b3] + Libuuid_jll v2.41.0+0  [e7412a2a] + Ogg_jll v1.3.5+1  [458c3c95] + OpenSSL_jll v3.5.0+0  [91d4177d] + Opus_jll v1.3.3+0  [36c8627f] + Pango_jll v1.56.3+0  [30392449] + Pixman_jll v0.44.2+0  [c0090381] + Qt6Base_jll v6.7.1+1  [a44049a8] + Vulkan_Loader_jll v1.3.243+0  [a2964d1f] + Wayland_jll v1.23.1+0  [2381bf8a] + Wayland_protocols_jll v1.44.0+0  [02c8fc9c] + XML2_jll v2.13.6+1  [ffd25f8a] + XZ_jll v5.8.1+0  [f67eecfb] + Xorg_libICE_jll v1.1.2+0  [c834827a] + Xorg_libSM_jll v1.2.6+0  [4f6342f7] + Xorg_libX11_jll v1.8.12+0  [0c0b7dd1] + Xorg_libXau_jll v1.0.13+0  [935fb764] + Xorg_libXcursor_jll v1.2.4+0  [a3789734] + Xorg_libXdmcp_jll v1.1.6+0  [1082639a] + Xorg_libXext_jll v1.3.7+0  [d091e8ba] + Xorg_libXfixes_jll v6.0.1+0  [a51aa0fd] + Xorg_libXi_jll v1.8.3+0  [d1454406] + Xorg_libXinerama_jll v1.1.6+0  [ec84b674] + Xorg_libXrandr_jll v1.5.5+0  [ea2f1a96] + Xorg_libXrender_jll v0.9.12+0  [c7cfdc94] + Xorg_libxcb_jll v1.17.1+0  [cc61e674] + Xorg_libxkbfile_jll v1.1.3+0  [e920d4aa] + Xorg_xcb_util_cursor_jll v0.1.4+0  [12413925] + Xorg_xcb_util_image_jll v0.4.1+0  [2def613f] + Xorg_xcb_util_jll v0.4.1+0  [975044d2] + Xorg_xcb_util_keysyms_jll v0.4.1+0  [0d47668e] + Xorg_xcb_util_renderutil_jll v0.3.10+0  [c22f9ab0] + Xorg_xcb_util_wm_jll v0.4.2+0  [35661453] + Xorg_xkbcomp_jll v1.4.7+0  [33bec58e] + Xorg_xkeyboard_config_jll v2.44.0+0  [c5fb5394] + Xorg_xtrans_jll v1.6.0+0  [3161d3a3] + Zstd_jll v1.5.7+1  [35ca27e7] + eudev_jll v3.2.14+0  [214eeab7] + fzf_jll v0.61.1+0  [a4ae2306] + libaom_jll v3.11.0+0  [0ac62f75] + libass_jll v0.15.2+0  [1183f4f0] + libdecor_jll v0.2.2+0  [2db6ffa8] + libevdev_jll v1.13.4+0  [f638f0a6] + libfdk_aac_jll v2.0.3+0  [36db933b] + libinput_jll v1.28.1+0  [b53b4c65] + libpng_jll v1.6.49+0  [f27f6e37] + libvorbis_jll v1.3.7+2  [009596ad] + mtdev_jll v1.1.7+0  [1270edf5] + x264_jll v2021.5.5+0  [dfaa095f] + x265_jll v3.5.0+0  [d8fb68d0] + xkbcommon_jll v1.8.1+0  [0dad84c5] + ArgTools  [56f22d72] + Artifacts  [2a0f44e3] + Base64  [ade2ca70] + Dates  [8bb1440f] + DelimitedFiles  [f43a241f] + Downloads  [7b1f6079] + FileWatching  [b77e0a4c] + InteractiveUtils  [b27032c2] + LibCURL  [76f85450] + LibGit2  [8f399da3] + Libdl  [37e2e46d] + LinearAlgebra  [56ddb016] + Logging  [d6f4376e] + Markdown  [a63ad114] + Mmap  [ca575930] + NetworkOptions  [44cfe95a] + Pkg  [de0858da] + Printf  [3fa0cd96] + REPL  [9a3f8284] + Random  [ea8e919c] + SHA  [9e88b42a] + Serialization  [6462fe0b] + Sockets  [2f01184e] + SparseArrays  [10745b16] + Statistics  [fa267f1f] + TOML  [a4e569a6] + Tar  [8dfed614] + Test  [cf7118a7] + UUIDs  [4ec0a83e] + Unicode  [e66e0078] + CompilerSupportLibraries_jll  [deac9b47] + LibCURL_jll  [29816b5a] + LibSSH2_jll  [c8ffd9c3] + MbedTLS_jll  [14a3606d] + MozillaCACerts_jll  [4536629a] + OpenBLAS_jll  [05823500] + OpenLibm_jll  [efcefdf7] + PCRE2_jll  [83775a58] + Zlib_jll  [8e850b90] + libblastrampoline_jll  [8e850ede] + nghttp2_jll  [3f19e933] + p7zip_jll text/plaincell_id$15187690-0403-11eb-2dfd-fd924faa3513kwargsidPlutoRunner_d1acb81efileP/home/runner/.julia/packages/Pluto/6smog/src/runner/PlutoRunner/src/io/stdout.jlgroupstdoutlevelLogLevel(-555)running¦outputbodymimetext/plainrootassigneelast_run_timestampAi77persist_js_state·has_pluto_hook_features§cell_id$15187690-0403-11eb-2dfd-fd924faa3513depends_on_disabled_cells§runtimeֵpublished_object_keysdepends_on_skipped_cells§errored$d8abd2f6-0416-11eb-1c2a-f9157d9760a7queued¤logsrunning¦outputbodyprefixInt64elements1text/plain1text/plain1text/plain1text/plain5text/plain1text/plain2text/plain1text/plain 2text/plain 1text/plain 1text/plain 1text/plain 3text/plain3text/plain1text/plain4text/plain1text/plain5text/plain3text/plain3text/plaintypeArrayprefix_shortobjectid858b67f6a95e205mime!application/vnd.pluto.tree+objectrootassigneesmall_experimentlast_run_timestampA*Ͱpersist_js_state·has_pluto_hook_features§cell_id$d8abd2f6-0416-11eb-1c2a-f9157d9760a7depends_on_disabled_cells§runtime>published_object_keysdepends_on_skipped_cells§errored$9374e63c-0493-11eb-0952-4b97512d7cdbqueued¤logsrunning¦outputbody

Great! Feel free to experiment with this function, try giving it a different array as argument. Plots.jl is pretty clever, it even works with an array of strings!

Exercise 1.4

Next, we want to add a new element to our plot: a vertical line. To demonstrate how this works, here we added a vertical line at the maximum value.

To write this function, we first create a base plot, we then modify that plot to add the vertical line, and finally, we return the plot. More on this in the next info box.

mimetext/htmlrootassigneelast_run_timestampA?persist_js_state·has_pluto_hook_features§cell_id$9374e63c-0493-11eb-0952-4b97512d7cdbdepends_on_disabled_cells§runtimepublished_object_keysdepends_on_skipped_cells§errored$2d3bba2a-04a8-11eb-2c40-87794b6aeeacqueued¤logsrunning¦outputbodyy

Exercise 2.5

👉 Write a function interact! that takes an affected agent of type Agent, an source of type Agent and an infection of type InfectionRecovery. It implements a single (one-sided) interaction between two agents:

mimetext/htmlrootassigneelast_run_timestampA tpersist_js_state·has_pluto_hook_features§cell_id$2d3bba2a-04a8-11eb-2c40-87794b6aeeacdepends_on_disabled_cells§runtime ^Xpublished_object_keysdepends_on_skipped_cells§errored$10cf6db8-04b8-11eb-2267-3db6d7f9c89aqueued¤logsrunning¦outputbody.sir_mean_plot (generic function with 1 method)mimetext/plainrootassigneelast_run_timestampAfΰpersist_js_state·has_pluto_hook_features§cell_id$10cf6db8-04b8-11eb-2267-3db6d7f9c89adepends_on_disabled_cells§runtime1published_object_keysdepends_on_skipped_cells§errored$dfb99ace-04cf-11eb-0739-7d694c837d59queued¤logsrunning¦outputbody

👉 Allow $p_\text{infection}$ and $p_\text{recovery}$ to be changed interactively and find parameter values for which you observe an epidemic outbreak.

mimetext/htmlrootassigneelast_run_timestampApersist_js_state·has_pluto_hook_features§cell_id$dfb99ace-04cf-11eb-0739-7d694c837d59depends_on_disabled_cells§runtimeQpublished_object_keysdepends_on_skipped_cells§errored$271ec5f0-041d-11eb-041b-db46ec1465e0queued¤logsrunning¦outputbodyN

We have just defined a new type InfectionStatus, as well as names S, I and R that are the (only) possible values that a variable of this type can take.

👉 Define a variable test_status whose value is S.

mimetext/htmlrootassigneelast_run_timestampAkpersist_js_state·has_pluto_hook_features§cell_id$271ec5f0-041d-11eb-041b-db46ec1465e0depends_on_disabled_cells§runtimexpublished_object_keysdepends_on_skipped_cells§errored$7946d83a-04a0-11eb-224b-2b315e87bc84queued¤logsrunning¦outputbodymimetext/plainrootassigneelast_run_timestampA`persist_js_state·has_pluto_hook_features§cell_id$7946d83a-04a0-11eb-224b-2b315e87bc84depends_on_disabled_cells§runtime:published_object_keysdepends_on_skipped_cells§errored$02b0c2fc-0415-11eb-2b40-7bca8ea4eef9queued¤logsrunning¦outputbodymimetext/plainrootassigneelast_run_timestampAWpersist_js_state·has_pluto_hook_features§cell_id$02b0c2fc-0415-11eb-2b40-7bca8ea4eef9depends_on_disabled_cells§runtime;Tpublished_object_keysdepends_on_skipped_cells§errored$9635c944-0403-11eb-3982-4df509f6a556queued¤logsrunning¦outputbody

Exercse 3.4

👉 What are three simple ways in which you could characterise the magnitude (size) of the epidemic outbreak? Find approximate values of these quantities for one of the runs of your simulation.

mimetext/htmlrootassigneelast_run_timestampApersist_js_state·has_pluto_hook_features§cell_id$9635c944-0403-11eb-3982-4df509f6a556depends_on_disabled_cells§runtimeGpublished_object_keysdepends_on_skipped_cells§errored$f3f81172-041c-11eb-2b9b-e99b7b9400edqueued¤logsrunning¦outputbody

Note about plotting

Plots.jl has an interesting property: a plot is an object, not an action. Functions like plot, bar, histogram don't draw anything on your screen - they just return a Plots.Plot. This is a struct that contains the description of a plot (what data should be plotted in what way?), not the picture.

So a Pluto cell with a single line, plot(1:10), will show a plot, because the result of the function plot is a Plot object, and Pluto just shows the result of a cell.

Modifying plots

Nice plots are often formed by overlaying multiple plots. In Plots.jl, this is done using the modifying functions: plot!, bar!, vline!, etc. These take an extra (first) argument: a previous plot to modify.

For example, to plot the sin, cos and tan functions in the same view, we do:

function sin_cos_plot()
    T = -1.0:0.01:1.0
    
    result = plot(T, sin.(T))
    plot!(result, T, cos.(T))
    plot!(result, T, tan.(T))

    return result
end

💡 This example demonstrates a useful pattern to combine plots:

  1. Create a new plot and store it in a variable

  2. Modify that plot to add more elements

  3. Return the plot

It is recommended that these 3 steps happen within a single cell. This can prevent some strange glitches when re-running cells. There are three ways to group expressions together into a single cell: begin, let and function. More on this later!

mimetext/htmlrootassigneelast_run_timestampA; persist_js_state·has_pluto_hook_features§cell_id$f3f81172-041c-11eb-2b9b-e99b7b9400eddepends_on_disabled_cells§runtime7ݵpublished_object_keysdepends_on_skipped_cells§errored$8a1d5aea-04ae-11eb-2177-eb37822db4f1queued¤logsrunning¦outputbody&step! (generic function with 1 method)mimetext/plainrootassigneelast_run_timestampA֘persist_js_state·has_pluto_hook_features§cell_id$8a1d5aea-04ae-11eb-2177-eb37822db4f1depends_on_disabled_cells§runtimeFpublished_object_keysdepends_on_skipped_cells§errored$4ad11052-042c-11eb-3643-8b2b3e1269bcqueued¤logsrunning¦outputbodymimetext/plainrootassigneelast_run_timestampA$persist_js_state·has_pluto_hook_features§cell_id$4ad11052-042c-11eb-3643-8b2b3e1269bcdepends_on_disabled_cells§runtimewpublished_object_keysdepends_on_skipped_cells§errored$76d117d4-0403-11eb-05d2-c5ea47d06f43queued¤logsrunning¦outputbodyٗ

👉 Write a function recovery_time(p) that returns the time taken until the person recovers.

mimetext/htmlrootassigneelast_run_timestampAqpersist_js_state·has_pluto_hook_features§cell_id$76d117d4-0403-11eb-05d2-c5ea47d06f43depends_on_disabled_cells§runtimeHpublished_object_keysdepends_on_skipped_cells§errored$095cbf46-0403-11eb-0c37-35de9562cebcqueued¤logsrunning¦outputbodyelementsname"Jazzy Doe"text/plainkerberos_id"jazz"text/plaintypeNamedTupleobjectideb71b675ed8a366bmime!application/vnd.pluto.tree+objectrootassigneestudentlast_run_timestampAѥpersist_js_state·has_pluto_hook_features§cell_id$095cbf46-0403-11eb-0c37-35de9562cebcdepends_on_disabled_cells§runtime=published_object_keysdepends_on_skipped_cells§errored$6d5c6a84-0415-11eb-3fdf-9355200cb520queued¤logsrunning¦outputbody.recovery_time (generic function with 1 method)mimetext/plainrootassigneelast_run_timestampANpersist_js_state·has_pluto_hook_features§cell_id$6d5c6a84-0415-11eb-3fdf-9355200cb520depends_on_disabled_cells§runtime Bpublished_object_keysdepends_on_skipped_cells§errored$26e2978e-0435-11eb-0d61-25f552d2771equeued¤logsrunning¦outputbodymimetext/plainrootassigneelast_run_timestampApersist_js_state·has_pluto_hook_features§cell_id$26e2978e-0435-11eb-0d61-25f552d2771edepends_on_disabled_cells§runtimepublished_object_keysdepends_on_skipped_cells§errored$6de37d6c-0415-11eb-1b05-85ac820016c7queued¤logsrunning¦outputbody^

👉 What happens for $p=1$?

mimetext/htmlrootassigneelast_run_timestampANpersist_js_state·has_pluto_hook_features§cell_id$6de37d6c-0415-11eb-1b05-85ac820016c7depends_on_disabled_cells§runtimepublished_object_keysdepends_on_skipped_cells§errored$18d308c4-0424-11eb-176d-49feec6889cfqueued¤logsrunning¦outputbodymissingmimetext/plainrootassigneetest_agentlast_run_timestampA ٰpersist_js_state·has_pluto_hook_features§cell_id$18d308c4-0424-11eb-176d-49feec6889cfdepends_on_disabled_cells§runtime*ѵpublished_object_keysdepends_on_skipped_cells§errored$46133a74-04b1-11eb-0b46-0bc74e564680queued¤logsrunning¦outputbodymimetext/plainrootassigneelast_run_timestampAHpersist_js_state·has_pluto_hook_features§cell_id$46133a74-04b1-11eb-0b46-0bc74e564680depends_on_disabled_cells§runtime

Exercise 1.3

👉 Write a function frequencies(data) that calculates and returns the frequencies (i.e. probability distribution) of input data.

The input will be an array of integers, with duplicates, and the result will be a dictionary that maps each occured value to its frequency in the data.

For example,

frequencies([7, 8, 9, 7])

should give

Dict(
	7 => 0.5, 
	8 => 0.25, 
	9 => 0.25
)

As with any probability distribution, it should be normalised to $1$, in the sense that the total probability should be $1$.

mimetext/htmlrootassigneelast_run_timestampAxpersist_js_state·has_pluto_hook_features§cell_id$771c8f0c-0403-11eb-097e-ab24d0714ad5depends_on_disabled_cells§runtime +published_object_keysdepends_on_skipped_cells§errored$3f497394-0a46-11eb-3369-8189905f011cqueued¤logsrunning¦outputbodymimetext/plainrootassigneelast_run_timestampA?persist_js_state·has_pluto_hook_features§cell_id$3f497394-0a46-11eb-3369-8189905f011cdepends_on_disabled_cells§runtimeQpublished_object_keysdepends_on_skipped_cells§errored$80c2cd88-04b1-11eb-326e-0120a39405eaqueued¤logsrunning¦outputbodyprefixFNamedTuple{(:S, :I, :R), Tuple{Vector{Any}, Vector{Any}, Vector{Any}}}elementselementsSprefixAnyelements99text/plain99text/plain99text/plain99text/plain99text/plainmore蒡0text/plaintypeArrayprefix_shortobjectid74f7608fbc82e5a8!application/vnd.pluto.tree+objectIprefixAnyelements1text/plain1text/plain1text/plain1text/plain1text/plainmore蒢22text/plaintypeArrayprefix_shortobjectid4a237e8966db1388!application/vnd.pluto.tree+objectRprefixAnyelements0text/plain0text/plain0text/plain0text/plain0text/plainmore蒢78text/plaintypeArrayprefix_shortobjectid557a58c57d88c390!application/vnd.pluto.tree+objecttypeNamedTupleobjectid9c6166a77619e8d6!application/vnd.pluto.tree+objectelementsSprefixAnyelements99text/plain99text/plain99text/plain99text/plain99text/plainmore蒡1text/plaintypeArrayprefix_shortobjectid8a29fd2be6e80d7c!application/vnd.pluto.tree+objectIprefixAnyelements1text/plain1text/plain1text/plain1text/plain1text/plainmore蒢39text/plaintypeArrayprefix_shortobjectid1bd99ae60a4749e2!application/vnd.pluto.tree+objectRprefixAnyelements0text/plain0text/plain0text/plain0text/plain0text/plainmore蒢60text/plaintypeArrayprefix_shortobjectidb289ef34b7a7f55d!application/vnd.pluto.tree+objecttypeNamedTupleobjectid43047ddb07568c2d!application/vnd.pluto.tree+objectelementsSprefixAnyelements99text/plain99text/plain99text/plain99text/plain99text/plainmore蒡0text/plaintypeArrayprefix_shortobjectid798a4253872e57!application/vnd.pluto.tree+objectIprefixAnyelements1text/plain1text/plain1text/plain1text/plain1text/plainmore蒢29text/plaintypeArrayprefix_shortobjectidfea5acb74fde9019!application/vnd.pluto.tree+objectRprefixAnyelements0text/plain0text/plain0text/plain0text/plain0text/plainmore蒢71text/plaintypeArrayprefix_shortobjectide9d318defcd96ba!application/vnd.pluto.tree+objecttypeNamedTupleobjectidfe82a96a4f80c5de!application/vnd.pluto.tree+objectelementsSprefixAnyelements99text/plain99text/plain99text/plain99text/plain99text/plainmore蒢99text/plaintypeArrayprefix_shortobjectid1abe72dd88102225!application/vnd.pluto.tree+objectIprefixAnyelements1text/plain1text/plain1text/plain1text/plain1text/plainmore蒡0text/plaintypeArrayprefix_shortobjectidd493fa467bbb2cd0!application/vnd.pluto.tree+objectRprefixAnyelements0text/plain0text/plain0text/plain0text/plain0text/plainmore蒡1text/plaintypeArrayprefix_shortobjectidb11332b9b4b99f7f!application/vnd.pluto.tree+objecttypeNamedTupleobjectide8cbe300eac6e348!application/vnd.pluto.tree+objectelementsSprefixAnyelements99text/plain99text/plain99text/plain98text/plain98text/plainmore蒡0text/plaintypeArrayprefix_shortobjectidd6eb128c80694cc8!application/vnd.pluto.tree+objectIprefixAnyelements1text/plain1text/plain1text/plain2text/plain2text/plainmore蒢18text/plaintypeArrayprefix_shortobjectidd4a80ea07be345e8!application/vnd.pluto.tree+objectRprefixAnyelements0text/plain0text/plain0text/plain0text/plain0text/plainmore蒢82text/plaintypeArrayprefix_shortobjectid789af64e1a68dc4!application/vnd.pluto.tree+objecttypeNamedTupleobjectidc23381c89836654d!application/vnd.pluto.tree+objectelementsSprefixAnyelements99text/plain99text/plain99text/plain99text/plain99text/plainmore蒡0text/plaintypeArrayprefix_shortobjectid8f7d0159718d2d3b!application/vnd.pluto.tree+objectIprefixAnyelements1text/plain1text/plain1text/plain1text/plain1text/plainmore蒢29text/plaintypeArrayprefix_shortobjectid17873de681a1a74f!application/vnd.pluto.tree+objectRprefixAnyelements0text/plain0text/plain0text/plain0text/plain0text/plainmore蒢71text/plaintypeArrayprefix_shortobjectid401931b8d2c59547!application/vnd.pluto.tree+objecttypeNamedTupleobjectida487f2a57dfb469d!application/vnd.pluto.tree+objectelementsSprefixAnyelements99text/plain99text/plain99text/plain99text/plain99text/plainmore蒡0text/plaintypeArrayprefix_shortobjectid7bf7035e4a813425!application/vnd.pluto.tree+objectIprefixAnyelements1text/plain1text/plain1text/plain1text/plain1text/plainmore蒢22text/plaintypeArrayprefix_shortobjectidcffd6a42728e02e8!application/vnd.pluto.tree+objectRprefixAnyelements0text/plain0text/plain0text/plain0text/plain0text/plainmore蒢78text/plaintypeArrayprefix_shortobjectid6421d51d1ad6def3!application/vnd.pluto.tree+objecttypeNamedTupleobjectid31e897714242eecd!application/vnd.pluto.tree+objectelementsSprefixAnyelements99text/plain99text/plain99text/plain99text/plain98text/plainmore蒡0text/plaintypeArrayprefix_shortobjectidb45d7ee0bb4e2e0d!application/vnd.pluto.tree+objectIprefixAnyelements1text/plain1text/plain1text/plain1text/plain2text/plainmore蒢23text/plaintypeArrayprefix_shortobjectid8d99a0afedc68c1c!application/vnd.pluto.tree+objectRprefixAnyelements0text/plain0text/plain0text/plain0text/plain0text/plainmore蒢77text/plaintypeArrayprefix_shortobjectidfb7fab284992a22f!application/vnd.pluto.tree+objecttypeNamedTupleobjectideb454f42a08693fa!application/vnd.pluto.tree+object elementsSprefixAnyelements99text/plain99text/plain99text/plain99text/plain99text/plainmore蒡0text/plaintypeArrayprefix_shortobjectid5bc711d08a21713e!application/vnd.pluto.tree+objectIprefixAnyelements1text/plain1text/plain1text/plain1text/plain1text/plainmore蒢29text/plaintypeArrayprefix_shortobjectid7d4351874d19f1c7!application/vnd.pluto.tree+objectRprefixAnyelements0text/plain0text/plain0text/plain0text/plain0text/plainmore蒢71text/plaintypeArrayprefix_shortobjectidafdfd1293252ec9e!application/vnd.pluto.tree+objecttypeNamedTupleobjectidaca3379257ccdb08!application/vnd.pluto.tree+object elementsSprefixAnyelements99text/plain99text/plain99text/plain99text/plain99text/plainmore蒡0text/plaintypeArrayprefix_shortobjectidcc9d09fc6bcd6594!application/vnd.pluto.tree+objectIprefixAnyelements1text/plain1text/plain1text/plain1text/plain1text/plainmore蒢23text/plaintypeArrayprefix_shortobjectidf7618b57c15665ea!application/vnd.pluto.tree+objectRprefixAnyelements0text/plain0text/plain0text/plain0text/plain0text/plainmore蒢77text/plaintypeArrayprefix_shortobjectid1f446eef111c1edc!application/vnd.pluto.tree+objecttypeNamedTupleobjectid150986293ad6897f!application/vnd.pluto.tree+object elementsSprefixAnyelements99text/plain99text/plain99text/plain99text/plain99text/plainmore蒢99text/plaintypeArrayprefix_shortobjectidc5c8b492de24a5d9!application/vnd.pluto.tree+objectIprefixAnyelements1text/plain1text/plain1text/plain0text/plain0text/plainmore蒡0text/plaintypeArrayprefix_shortobjectidf9c5c937461ed045!application/vnd.pluto.tree+objectRprefixAnyelements0text/plain0text/plain0text/plain1text/plain1text/plainmore蒡1text/plaintypeArrayprefix_shortobjectid715016bd3533713b!application/vnd.pluto.tree+objecttypeNamedTupleobjectid8a0a568fa36b3c87!application/vnd.pluto.tree+object elementsSprefixAnyelements99text/plain99text/plain99text/plain99text/plain99text/plainmore蒡0text/plaintypeArrayprefix_shortobjectid9322191778d7747b!application/vnd.pluto.tree+objectIprefixAnyelements1text/plain1text/plain1text/plain1text/plain1text/plainmore蒢13text/plaintypeArrayprefix_shortobjectid1ed845de1043d41e!application/vnd.pluto.tree+objectRprefixAnyelements0text/plain0text/plain0text/plain0text/plain0text/plainmore蒢87text/plaintypeArrayprefix_shortobjectid597238b78577b69c!application/vnd.pluto.tree+objecttypeNamedTupleobjectidda296ddedcf26bc!application/vnd.pluto.tree+object elementsSprefixAnyelements99text/plain99text/plain99text/plain99text/plain99text/plainmore蒡0text/plaintypeArrayprefix_shortobjectid695dc8d0a54fefb6!application/vnd.pluto.tree+objectIprefixAnyelements1text/plain1text/plain1text/plain1text/plain1text/plainmore蒢28text/plaintypeArrayprefix_shortobjectidc118f5f854c64adc!application/vnd.pluto.tree+objectRprefixAnyelements0text/plain0text/plain0text/plain0text/plain0text/plainmore蒢72text/plaintypeArrayprefix_shortobjectidcf3b844ef10ac5b9!application/vnd.pluto.tree+objecttypeNamedTupleobjectid71f70baef54a6331!application/vnd.pluto.tree+objectelementsSprefixAnyelements99text/plain99text/plain99text/plain98text/plain98text/plainmore蒡0text/plaintypeArrayprefix_shortobjectid768bbbcb3fab308b!application/vnd.pluto.tree+objectIprefixAnyelements1text/plain1text/plain1text/plain2text/plain2text/plainmore蒢30text/plaintypeArrayprefix_shortobjectidf6e51dff1705d8a!application/vnd.pluto.tree+objectRprefixAnyelements0text/plain0text/plain0text/plain0text/plain0text/plainmore蒢70text/plaintypeArrayprefix_shortobjectid992f2b8904f211ce!application/vnd.pluto.tree+objecttypeNamedTupleobjectid52c6f11510796fb2!application/vnd.pluto.tree+objectelementsSprefixAnyelements99text/plain99text/plain99text/plain99text/plain99text/plainmore蒡0text/plaintypeArrayprefix_shortobjectidb90e0b39c4afd24b!application/vnd.pluto.tree+objectIprefixAnyelements1text/plain1text/plain1text/plain1text/plain1text/plainmore蒢23text/plaintypeArrayprefix_shortobjectide5ec17829e6bebb0!application/vnd.pluto.tree+objectRprefixAnyelements0text/plain0text/plain0text/plain0text/plain0text/plainmore蒢77text/plaintypeArrayprefix_shortobjectidcf8bdaf0f1ab7379!application/vnd.pluto.tree+objecttypeNamedTupleobjectidb79bb163c086a377!application/vnd.pluto.tree+objectelementsSprefixAnyelements99text/plain99text/plain99text/plain99text/plain99text/plainmore蒢99text/plaintypeArrayprefix_shortobjectid58c04c25dac5ca7!application/vnd.pluto.tree+objectIprefixAnyelements1text/plain1text/plain1text/plain1text/plain1text/plainmore蒡0text/plaintypeArrayprefix_shortobjectida58f714b1db336ca!application/vnd.pluto.tree+objectRprefixAnyelements0text/plain0text/plain0text/plain0text/plain0text/plainmore蒡1text/plaintypeArrayprefix_shortobjectid1387cc5179a3edf5!application/vnd.pluto.tree+objecttypeNamedTupleobjectida8ebe5659ead70b2!application/vnd.pluto.tree+objectelementsSprefixAnyelements99text/plain97text/plain97text/plain97text/plain97text/plainmore蒡0text/plaintypeArrayprefix_shortobjectid1db2bfb60df9d58a!application/vnd.pluto.tree+objectIprefixAnyelements1text/plain3text/plain3text/plain3text/plain3text/plainmore蒢19text/plaintypeArrayprefix_shortobjectid3e0102814e965bf0!application/vnd.pluto.tree+objectRprefixAnyelements0text/plain0text/plain0text/plain0text/plain0text/plainmore蒢81text/plaintypeArrayprefix_shortobjectid2c2b71e52aeb3bd8!application/vnd.pluto.tree+objecttypeNamedTupleobjectidbbb28a787626979b!application/vnd.pluto.tree+objectelementsSprefixAnyelements99text/plain99text/plain99text/plain99text/plain99text/plainmore蒡0text/plaintypeArrayprefix_shortobjectid1abdb834080fc962!application/vnd.pluto.tree+objectIprefixAnyelements1text/plain1text/plain1text/plain1text/plain1text/plainmore蒢38text/plaintypeArrayprefix_shortobjectid76b047f73ff4e9bb!application/vnd.pluto.tree+objectRprefixAnyelements0text/plain0text/plain0text/plain0text/plain0text/plainmore蒢62text/plaintypeArrayprefix_shortobjectid2ec626d73020adac!application/vnd.pluto.tree+objecttypeNamedTupleobjectid55b0e5ad3436778e!application/vnd.pluto.tree+objectelementsSprefixAnyelements99text/plain99text/plain99text/plain99text/plain99text/plainmore蒡0text/plaintypeArrayprefix_shortobjectidd35639a4f940cf25!application/vnd.pluto.tree+objectIprefixAnyelements1text/plain1text/plain1text/plain1text/plain1text/plainmore蒢21text/plaintypeArrayprefix_shortobjectid43dd20935a4e9dd4!application/vnd.pluto.tree+objectRprefixAnyelements0text/plain0text/plain0text/plain0text/plain0text/plainmore蒢79text/plaintypeArrayprefix_shortobjectid327a5ccd8e31a9c!application/vnd.pluto.tree+objecttypeNamedTupleobjectidbeededd80fcaf4c9!application/vnd.pluto.tree+objectelementsSprefixAnyelements99text/plain99text/plain99text/plain99text/plain99text/plainmore蒡0text/plaintypeArrayprefix_shortobjectidc72bfa1560ebc62a!application/vnd.pluto.tree+objectIprefixAnyelements1text/plain1text/plain1text/plain1text/plain1text/plainmore蒢43text/plaintypeArrayprefix_shortobjectide963e0b6a55b8f4c!application/vnd.pluto.tree+objectRprefixAnyelements0text/plain0text/plain0text/plain0text/plain0text/plainmore蒢57text/plaintypeArrayprefix_shortobjectid7ae75f46c8629256!application/vnd.pluto.tree+objecttypeNamedTupleobjectid1def7af6d6cb6454!application/vnd.pluto.tree+objecttypeArrayprefix_shortobjectid731197afd9a5259amime!application/vnd.pluto.tree+objectrootassigneesimulationslast_run_timestampARpersist_js_state·has_pluto_hook_features§cell_id$80c2cd88-04b1-11eb-326e-0120a39405eadepends_on_disabled_cells§runtimepublished_object_keysdepends_on_skipped_cells§errored$105d347e-041c-11eb-2fc8-1d9e5eda2be0queued¤logsrunning¦outputbodymimetext/plainrootassigneelast_run_timestampA3persist_js_state·has_pluto_hook_features§cell_id$105d347e-041c-11eb-2fc8-1d9e5eda2be0depends_on_disabled_cells§runtime9صpublished_object_keysdepends_on_skipped_cells§errored$2c62b4ae-04b3-11eb-0080-a1035a7e31a2queued¤logsrunning¦outputbodyelementsSprefixAnyelements99text/plain99text/plain99text/plain99text/plain99text/plain99text/plain99text/plain99text/plain 99text/plainmore蒢99text/plaintypeArrayprefix_shortobjectid6a449c1bc8b6833e!application/vnd.pluto.tree+objectIprefixAnyelements1text/plain1text/plain0text/plain0text/plain0text/plain0text/plain0text/plain0text/plain 0text/plainmore蒡0text/plaintypeArrayprefix_shortobjectidf231b62ad890b61a!application/vnd.pluto.tree+objectRprefixAnyelements0text/plain0text/plain1text/plain1text/plain1text/plain1text/plain1text/plain1text/plain 1text/plainmore蒡1text/plaintypeArrayprefix_shortobjectid1128e60e967f17d1!application/vnd.pluto.tree+objecttypeNamedTupleobjectidd13019d3659c3ca1mime!application/vnd.pluto.tree+objectrootassigneelast_run_timestampAôgpersist_js_state·has_pluto_hook_features§cell_id$2c62b4ae-04b3-11eb-0080-a1035a7e31a2depends_on_disabled_cells§runtimeT8published_object_keysdepends_on_skipped_cells§errored$6db6c894-0415-11eb-305a-c75b119d89e9queued¤logsrunning¦outputbody

We should always be aware of special cases (sometimes called "boundary conditions"). Make sure not to run the code with $p=0$! What would happen in that case? Your code should check for this and throw an ArgumentError as follows:

throw(ArgumentError("..."))  

with a suitable error message.

mimetext/htmlrootassigneelast_run_timestampApersist_js_state·has_pluto_hook_features§cell_id$6db6c894-0415-11eb-305a-c75b119d89e9depends_on_disabled_cells§runtime$Upublished_object_keysdepends_on_skipped_cells§errored$77db111e-0403-11eb-2dea-4b42ceed65d6queued¤logsrunning¦outputbodyl

Exercise 1.6

👉 Use $N = 10,000$ to calculate the mean time $\langle \tau(p) \rangle$ to recover as a function of $p$ between $0.001$ and $1$ (say). Plot this relationship.

mimetext/htmlrootassigneelast_run_timestampA!persist_js_state·has_pluto_hook_features§cell_id$77db111e-0403-11eb-2dea-4b42ceed65d6depends_on_disabled_cells§runtimepublished_object_keysdepends_on_skipped_cells§errored$6d480cf0-0425-11eb-18a9-1737455371d7queued¤logsrunning¦outputbody0generate_agents (generic function with 1 method)mimetext/plainrootassigneelast_run_timestampA] Rpersist_js_state·has_pluto_hook_features§cell_id$6d480cf0-0425-11eb-18a9-1737455371d7depends_on_disabled_cells§runtimepublished_object_keysdepends_on_skipped_cells§errored$287ee7aa-0435-11eb-0ca3-951dbbe69404queued¤logsrunning¦outputbody4sir_mean_error_plot (generic function with 1 method)mimetext/plainrootassigneelast_run_timestampA׫persist_js_state·has_pluto_hook_features§cell_id$287ee7aa-0435-11eb-0ca3-951dbbe69404depends_on_disabled_cells§runtime ۵published_object_keysdepends_on_skipped_cells§errored$3d88c056-0414-11eb-0025-05d3aff1588bqueued¤logsrunning¦outputbody)correct (generic function with 2 methods)mimetext/plainrootassigneelast_run_timestampApersist_js_state·has_pluto_hook_features§cell_id$3d88c056-0414-11eb-0025-05d3aff1588bdepends_on_disabled_cells§runtime published_object_keysdepends_on_skipped_cells§errored$03a85970-0403-11eb-334a-812b59c0905bqueued¤logsrunning¦outputbodym

Submission by: Jazzy Doe (jazz@mit.edu)

mimetext/htmlrootassigneelast_run_timestampA?persist_js_state·has_pluto_hook_features§cell_id$03a85970-0403-11eb-334a-812b59c0905bdepends_on_disabled_cells§runtime7published_object_keysdepends_on_skipped_cells§errored$6d906d0c-0415-11eb-0c1c-b5c0aca841dbqueued¤logsrunning¦outputbody

Hint

Remember to always re-use work you have done previously: in this case you should re-use the function bernoulli.

mimetext/htmlrootassigneelast_run_timestampA׾lpersist_js_state·has_pluto_hook_features§cell_id$6d906d0c-0415-11eb-0c1c-b5c0aca841dbdepends_on_disabled_cells§runtimexDpublished_object_keysdepends_on_skipped_cells§errored$866299e8-0403-11eb-085d-2b93459cc141queued¤logsrunning¦outputbodyٸ

👉 We will also need functions is_susceptible and is_infected that check if a given agent is in those respective states.

mimetext/htmlrootassigneelast_run_timestampAapersist_js_state·has_pluto_hook_features§cell_id$866299e8-0403-11eb-085d-2b93459cc141depends_on_disabled_cells§runtimen5published_object_keysdepends_on_skipped_cells§errored$461586dc-0414-11eb-00f3-4984b57bfac5queued¤logsrunning¦outputbody'almost (generic function with 1 method)mimetext/plainrootassigneelast_run_timestampA_cpersist_js_state·has_pluto_hook_features§cell_id$461586dc-0414-11eb-00f3-4984b57bfac5depends_on_disabled_cells§runtimeBpublished_object_keysdepends_on_skipped_cells§errored$c5156c72-04af-11eb-1106-b13969b036caqueued¤logsrunning¦outputbody
mimetext/htmlrootassigneelast_run_timestampA0persist_js_state·has_pluto_hook_features§cell_id$c5156c72-04af-11eb-1106-b13969b036cadepends_on_disabled_cells§runtimetյpublished_object_keysdepends_on_skipped_cells§errored$c4a8694a-04d4-11eb-1eef-c9e037e6b21fqueued¤logsrunning¦outputbody٥

Here we go!

Replace missing with your answer.

mimetext/htmlrootassigneelast_run_timestampAipersist_js_state·has_pluto_hook_features§cell_id$c4a8694a-04d4-11eb-1eef-c9e037e6b21fdepends_on_disabled_cells§runtime&published_object_keysdepends_on_skipped_cells§errored$1491a078-04aa-11eb-0106-19a3cf1e94b0queued¤logsrunning¦outputbody

Got it!

Your function treats the infectious agent case correctly!

mimetext/htmlrootassigneelast_run_timestampA/persist_js_state·has_pluto_hook_features§cell_id$1491a078-04aa-11eb-0106-19a3cf1e94b0depends_on_disabled_cells§runtimeʵpublished_object_keysdepends_on_skipped_cells§errored$c5c7cb86-041b-11eb-3360-45463105f3c9queued¤logsrunning¦outputbodymimetext/plainrootassigneelast_run_timestampAVpersist_js_state·has_pluto_hook_features§cell_id$c5c7cb86-041b-11eb-3360-45463105f3c9depends_on_disabled_cells§runtime;6published_object_keysdepends_on_skipped_cells§errored$01341648-0403-11eb-2212-db450c299f35queued¤logsrunning¦outputbodyB

homework 4, version 0

mimetext/htmlrootassigneelast_run_timestampAx6persist_js_state·has_pluto_hook_features§cell_id$01341648-0403-11eb-2212-db450c299f35depends_on_disabled_cells§runtimecpublished_object_keysdepends_on_skipped_cells§errored$d8797684-0414-11eb-1869-5b1e2c469011queued¤logsrunning¦outputbody*bernoulli (generic function with 1 method)mimetext/plainrootassigneelast_run_timestampApersist_js_state·has_pluto_hook_features§cell_id$d8797684-0414-11eb-1869-5b1e2c469011depends_on_disabled_cells§runtimeϵpublished_object_keysdepends_on_skipped_cells§errored$48a16c42-0414-11eb-0e0c-bf52bbb0f618queued¤logsrunning¦outputbody%hint (generic function with 1 method)mimetext/plainrootassigneelast_run_timestampA׺>persist_js_state·has_pluto_hook_features§cell_id$48a16c42-0414-11eb-0e0c-bf52bbb0f618depends_on_disabled_cells§runtime[6published_object_keysdepends_on_skipped_cells§errored$98beb336-0425-11eb-3886-4f8cfd210288queued¤logsrunning¦outputbody,set_status! (generic function with 1 method)mimetext/plainrootassigneelast_run_timestampA;persist_js_state·has_pluto_hook_features§cell_id$98beb336-0425-11eb-3886-4f8cfd210288depends_on_disabled_cells§runtimepublished_object_keysdepends_on_skipped_cells§errored$bb63f3cc-042f-11eb-04ff-a128aec3c378queued¤logsrunning¦outputbodymimetext/plainrootassigneelast_run_timestampA߀persist_js_state·has_pluto_hook_features§cell_id$bb63f3cc-042f-11eb-04ff-a128aec3c378depends_on_disabled_cells§runtimeꟵpublished_object_keysdepends_on_skipped_cells§errored$61c00724-0403-11eb-228d-17c11670e5d1queued¤logsrunning¦outputbody

Exercise 4: Reinfection

In this exercise we will re-use our simulation infrastructure to study the dynamics of a different type of infection: there is no immunity, and hence no "recovery" rather, susceptible individuals may now be re-infected

Exercise 4.1

👉 Make a new infection type Reinfection. This has the same two fields as InfectionRecovery (p_infection and p_recovery). However, "recovery" now means "becomes susceptible again", instead of "moves to the R class.

This new type Reinfection should also be a subtype of AbstractInfection. This allows us to reuse our previous functions, which are defined for the abstract supertype.

mimetext/htmlrootassigneelast_run_timestampAHpersist_js_state·has_pluto_hook_features§cell_id$61c00724-0403-11eb-228d-17c11670e5d1depends_on_disabled_cells§runtime µpublished_object_keysdepends_on_skipped_cells§errored$9cf9080a-04b1-11eb-12a0-17013f2d37f5queued¤logsrunning¦outputbody,

👉 Calculate the mean number of infectious agents of our simulations for each time step. Add it to the plot using a heavier line (lw=3 for "linewidth") by modifying the cell above.

Check the answer yourself: does your curve follow the average trend?

Hint

This exercise requires some creative juggling with arrays, anonymous functions, maps, or whatever you see fit!

mimetext/htmlrootassigneelast_run_timestampA׿ (persist_js_state·has_pluto_hook_features§cell_id$9cf9080a-04b1-11eb-12a0-17013f2d37f5depends_on_disabled_cells§runtimepublished_object_keysdepends_on_skipped_cells§errored$4b3ec86c-0419-11eb-26fd-cbbfdf19afa8queued¤logsrunning¦outputbodyprefixInt64elements5text/plain1text/plain9text/plain2text/plain4text/plain1text/plain1text/plain13text/plain 4text/plain 3text/plain 3text/plain 7text/plain 9text/plain2text/plain5text/plain6text/plain1text/plain16text/plain1text/plain10text/plainmore'1text/plain'2text/plain' 6text/plain' 6text/plain' 3text/plain' 5text/plain' 1text/plain'11text/plain'8text/plain'2text/plaintypeArrayprefix_shortobjectid3cf54089ffc203eamime!application/vnd.pluto.tree+objectrootassigneelarge_experimentlast_run_timestampAU߰persist_js_state·has_pluto_hook_features§cell_id$4b3ec86c-0419-11eb-26fd-cbbfdf19afa8depends_on_disabled_cells§runtimepublished_object_keysdepends_on_skipped_cells§errored$61789646-0403-11eb-0042-f3b8308f11baqueued¤logsrunning¦outputbody3

Exercise 2: Agent-based model for an epidemic outbreak – types

In this and the following exercises we will develop a simple stochastic model for combined infection and recovery in a population, which may exhibit an epidemic outbreak (i.e. a large spike in the number of infectious people). The population is well mixed, i.e. everyone is in contact with everyone else. [An example of this would be a small school or university in which people are constantly moving around and interacting with each other.]

The model is an individual-based or agent-based model: we explicitly keep track of each individual, or agent, in the population and their infection status. For the moment we will not keep track of their position in space; we will just assume that there is some mechanism, not included in the model, by which they interact with other individuals.

Exercise 2.1

Each agent will have its own internal state, modelling its infection status, namely "susceptible", "infectious" or "recovered". We would like to code these as values S, I and R, respectively. One way to do this is using an enumerated type or enum. Variables of this type can take only a pre-defined set of values; the Julia syntax is as follows:

mimetext/htmlrootassigneelast_run_timestampAPpersist_js_state·has_pluto_hook_features§cell_id$61789646-0403-11eb-0042-f3b8308f11badepends_on_disabled_cells§runtime 7 published_object_keysdepends_on_skipped_cells§errored$39dffa3c-0414-11eb-0197-e72b299e9c63queued¤logsrunning¦outputbodymimetext/plainrootassigneelast_run_timestampA8>Spersist_js_state·has_pluto_hook_features§cell_id$39dffa3c-0414-11eb-0197-e72b299e9c63depends_on_disabled_cells§runtimehKpublished_object_keysdepends_on_skipped_cells§errored$5950b37e-0a46-11eb-3480-d5520013692equeued¤logsrunning¦outputbodymimetext/plainrootassigneelast_run_timestampAJpersist_js_state·has_pluto_hook_features§cell_id$5950b37e-0a46-11eb-3480-d5520013692edepends_on_disabled_cells§runtimepublished_object_keysdepends_on_skipped_cells§errored$cdaade9c-0416-11eb-0550-7b5b3d33e240queued¤logsrunning¦outputbody.do_experiment (generic function with 1 method)mimetext/plainrootassigneelast_run_timestampA Lpersist_js_state·has_pluto_hook_features§cell_id$cdaade9c-0416-11eb-0550-7b5b3d33e240depends_on_disabled_cells§runtime ˵published_object_keysdepends_on_skipped_cells§errored$88c53208-041d-11eb-3b1e-31b57ba99f05queued¤logsrunning¦outputbodymimetext/plainrootassigneelast_run_timestampApersist_js_state·has_pluto_hook_features§cell_id$88c53208-041d-11eb-3b1e-31b57ba99f05depends_on_disabled_cells§runtimeOpublished_object_keysdepends_on_skipped_cells§errored$f8e05d94-04ac-11eb-26d4-6f1d2c5ed272queued¤logsrunning¦outputbody

Got it!

Your function treats the recovered agent case correctly!

mimetext/htmlrootassigneelast_run_timestampA39persist_js_state·has_pluto_hook_features§cell_id$f8e05d94-04ac-11eb-26d4-6f1d2c5ed272depends_on_disabled_cells§runtimeNpublished_object_keysdepends_on_skipped_cells§errored$887d27fc-04bc-11eb-0ab9-eb95ef9607f8queued¤logsrunning¦outputbodymimetext/plainrootassigneelast_run_timestampAڰpersist_js_state·has_pluto_hook_features§cell_id$887d27fc-04bc-11eb-0ab9-eb95ef9607f8depends_on_disabled_cells§runtime:published_object_keysdepends_on_skipped_cells§errored$d57c6a5a-041b-11eb-3ab4-774a2d45a891queued¤logsrunning¦outputbodymimetext/plainrootassigneelast_run_timestampAݽopersist_js_state·has_pluto_hook_features§cell_id$d57c6a5a-041b-11eb-3ab4-774a2d45a891depends_on_disabled_cells§runtime=5published_object_keysdepends_on_skipped_cells§errored$12cc2940-0403-11eb-19a7-bb570de58f6fqueued¤logslinemsg@ Activating new project at `/tmp/jl_SHw1SH` text/plaincell_id$12cc2940-0403-11eb-19a7-bb570de58f6fkwargsidPlutoRunner_d1acb81efileP/home/runner/.julia/packages/Pluto/6smog/src/runner/PlutoRunner/src/io/stdout.jlgroupstdoutlevelLogLevel(-555)running¦outputbodymimetext/plainrootassigneelast_run_timestampA&Ȱpersist_js_state·has_pluto_hook_features§cell_id$12cc2940-0403-11eb-19a7-bb570de58f6fdepends_on_disabled_cells§runtimeg]jpublished_object_keysdepends_on_skipped_cells§errored$76f62d64-0403-11eb-27e2-3de58366b619queued¤logsrunning¦outputbody

Exercise 1.2

👉 Write a function do_experiment(p, N) that runs the function recovery_time N times and collects the results into a vector.

mimetext/htmlrootassigneelast_run_timestampA opersist_js_state·has_pluto_hook_features§cell_id$76f62d64-0403-11eb-27e2-3de58366b619depends_on_disabled_cells§runtime

Exercise 2.2

For each agent we want to keep track of its infection status and the number of other agents that it infects during the simulation. A good solution for this is to define a new type Agent to hold all of the information for one agent, as follows:

mimetext/htmlrootassigneelast_run_timestampA2persist_js_state·has_pluto_hook_features§cell_id$860790fc-0403-11eb-2f2e-355f77dcc7afdepends_on_disabled_cells§runtimeߵpublished_object_keysdepends_on_skipped_cells§errored$b21475c6-04ac-11eb-1366-f3b5e967402dqueued¤logsrunning¦outputbody

Play around with the test case below to test your function! Try changing the definitions of agent, source and infection. Since we are working with randomness, you might want to run the cell multiple times.

mimetext/htmlrootassigneelast_run_timestampArpersist_js_state·has_pluto_hook_features§cell_id$b21475c6-04ac-11eb-1366-f3b5e967402ddepends_on_disabled_cells§runtimeʵpublished_object_keysdepends_on_skipped_cells§errored$823364ce-041c-11eb-2467-7ffa4f751527queued¤logsrunning¦outputbody>frequencies_plot_with_maximum (generic function with 1 method)mimetext/plainrootassigneelast_run_timestampAՙͳpersist_js_state·has_pluto_hook_features§cell_id$823364ce-041c-11eb-2467-7ffa4f751527depends_on_disabled_cells§runtime _published_object_keysdepends_on_skipped_cells§errored$619c8a10-0403-11eb-2e89-8b0974fb01d0queued¤logsrunning¦outputbody

Exercise 3: Agent-based model for an epidemic outbreak – Monte Carlo simulation

In this exercise we will build on Exercise 2 to write a Monte Carlo simulation of how an infection propagates in a population.

Make sure to re-use the functions that we have already written, and introduce new ones if they are helpful! Short functions make it easier to understand what the function does and build up new functionality piece by piece.

You should not use any global variables inside the functions: Each function must accept as arguments all the information it requires to carry out its task. You need to think carefully about what the information each function requires.

Exercise 3.1

👉 Write a function step! that takes a vector of Agents and an infection of type InfectionRecovery. It implements a single step of the infection dynamics as follows:

  • Choose two random agents: an agent and a source.

  • Apply interact!(agent, source, infection).

  • Return agents.

mimetext/htmlrootassigneelast_run_timestampA⚮persist_js_state·has_pluto_hook_features§cell_id$619c8a10-0403-11eb-2e89-8b0974fb01d0depends_on_disabled_cells§runtime Bpublished_object_keysdepends_on_skipped_cells§errored$38b1aa5a-04cf-11eb-11a2-930741fc9076queued¤logsrunning¦outputbody3repeat_simulations (generic function with 1 method)mimetext/plainrootassigneelast_run_timestampA;;%persist_js_state·has_pluto_hook_features§cell_id$38b1aa5a-04cf-11eb-11a2-930741fc9076depends_on_disabled_cells§runtimepublished_object_keysdepends_on_skipped_cells§errored$8dd97820-04a5-11eb-36c0-8f92d4b859a8queued¤logsrunning¦outputbodymimetext/plainrootassigneelast_run_timestampA[persist_js_state·has_pluto_hook_features§cell_id$8dd97820-04a5-11eb-36c0-8f92d4b859a8depends_on_disabled_cells§runtimelpublished_object_keysdepends_on_skipped_cells§errored$df8547b4-0400-11eb-07c6-fb370b61c2b6queued¤logsrunning¦outputbodyP

Exercise 1: Modelling recovery

In this exercise we will investigate a simple stochastic (probabilistic) model of recovery from an infection and the time $\tau$ needed to recover. Although this model can be easily studied analytically using probability theory, we will instead use computational methods. (If you know about this distribution already, try to ignore what you know about it!)

In this model, an individual who is infected has a constant probability $p$ to recover each day. If they recover on day $n$ then $\tau$ takes the value $n$. Each time we run a new experiment $\tau$ will take on different values, so $\tau$ is a (discrete) random variable. We thus need to study statistical properties of $\tau$, such as its mean and its probability distribution.

Exercise 1.1 - Probability distributions

👉 Define the function bernoulli(p), which returns true with probability $p$ and false with probability $(1 - p)$.

mimetext/htmlrootassigneelast_run_timestampAװpersist_js_state·has_pluto_hook_features§cell_id$df8547b4-0400-11eb-07c6-fb370b61c2b6depends_on_disabled_cells§runtime |published_object_keysdepends_on_skipped_cells§errored$1d3356c4-0403-11eb-0f48-01b5eb14a585queued¤logsrunning¦outputbody mimetext/htmlrootassigneelast_run_timestampAزpersist_js_state·has_pluto_hook_features§cell_id$1d3356c4-0403-11eb-0f48-01b5eb14a585depends_on_disabled_cells§runtime

We will also need types representing different infections.

Let's define an (immutable) struct called InfectionRecovery with parameters p_infection and p_recovery. We will make it a subtype of an abstract AbstractInfection type, because we will define more infection types later.

mimetext/htmlrootassigneelast_run_timestampA莰persist_js_state·has_pluto_hook_features§cell_id$86d98d0a-0403-11eb-215b-c58ad721a90bdepends_on_disabled_cells§runtimeZpublished_object_keysdepends_on_skipped_cells§errored$dc784864-0430-11eb-1478-d1153e017310queued¤logsrunning¦outputbody

The frequencies dictionary is difficult to interpret on its own, so instead, we will plot it, i.e. plot $P(\tau = n)$ against $n$, where $n$ is the recovery time.

Plots.jl comes with a function bar, which does exactly what we want:

mimetext/htmlrootassigneelast_run_timestampAfpersist_js_state·has_pluto_hook_features§cell_id$dc784864-0430-11eb-1478-d1153e017310depends_on_disabled_cells§runtimeHpublished_object_keysdepends_on_skipped_cells§errored$3f5e0af8-0414-11eb-34a7-a71e7aaf6443queued¤logsrunning¦outputbodyprefixMarkdown.MDelements2

Fantastic!

text/html1

Splendid!

text/html.

Great!

text/html+

Yay ❤

text/html3

Great! 🎉

text/html2

Well done!

text/html3

Keep it up!

text/html1

Good job!

text/html 0

Awesome!

text/html A

You got the right answer!

text/html J

Let's move on to the next section.

text/htmltypeArrayprefix_shortobjectid641ac141a725789cmime!application/vnd.pluto.tree+objectrootassigneeyayslast_run_timestampA4persist_js_state·has_pluto_hook_features§cell_id$3f5e0af8-0414-11eb-34a7-a71e7aaf6443depends_on_disabled_cells§runtime/ŵpublished_object_keysdepends_on_skipped_cells§errored$3c0528a0-0414-11eb-2f68-a5657ab9e73dqueued¤logsrunning¦outputbody,not_defined (generic function with 1 method)mimetext/plainrootassigneelast_run_timestampAذpersist_js_state·has_pluto_hook_features§cell_id$3c0528a0-0414-11eb-2f68-a5657ab9e73ddepends_on_disabled_cells§runtime rpublished_object_keysdepends_on_skipped_cells§errored$7335de44-042f-11eb-2873-8bceef722432queued¤logsrunning¦outputbodymimetext/plainrootassigneelast_run_timestampA°persist_js_state·has_pluto_hook_features§cell_id$7335de44-042f-11eb-2873-8bceef722432depends_on_disabled_cells§runtime$lpublished_object_keysdepends_on_skipped_cells§errored$406aabea-04a5-11eb-06b8-312879457c42queued¤logsrunning¦outputbodymimetext/plainrootassigneelast_run_timestampAX6persist_js_state·has_pluto_hook_features§cell_id$406aabea-04a5-11eb-06b8-312879457c42depends_on_disabled_cells§runtime<0published_object_keysdepends_on_skipped_cells§errored$c61f35ea-04d6-11eb-2503-17a79f8d0298queued¤logsrunning¦outputbodyق

Got it!

Keep it up!

mimetext/htmlrootassigneelast_run_timestampA@persist_js_state·has_pluto_hook_features§cell_id$c61f35ea-04d6-11eb-2503-17a79f8d0298depends_on_disabled_cells§runtimeYDpublished_object_keysdepends_on_skipped_cells§errored$9cd2bb00-04b1-11eb-1d83-a703907141a7queued¤logsrunning¦outputbody Ȯ
mimetext/htmlrootassigneelast_run_timestampAZpersist_js_state·has_pluto_hook_features§cell_id$9cd2bb00-04b1-11eb-1d83-a703907141a7depends_on_disabled_cells§runtime]published_object_keysdepends_on_skipped_cells§errored$8a28c56e-04b4-11eb-279c-3b4dfb2a9f9bqueued¤logsrunning¦outputbodyo
mimetext/htmlrootassigneelast_run_timestampAՎ@persist_js_state·has_pluto_hook_features§cell_id$8a28c56e-04b4-11eb-279c-3b4dfb2a9f9bdepends_on_disabled_cells§runtime7Mڵpublished_object_keysdepends_on_skipped_cells§errored$8631a536-0403-11eb-0379-bb2e56927727queued¤logsrunning¦outputbody

Exercise 2.3

👉 Write functions set_status!(a) and set_num_infected!(a) which modify the respective fields of an Agent. Check that they work. [Note the bang ("!") at the end of the function names to signify that these functions modify their argument.]

mimetext/htmlrootassigneelast_run_timestampAIopersist_js_state·has_pluto_hook_features§cell_id$8631a536-0403-11eb-0379-bb2e56927727depends_on_disabled_cells§runtimeJpublished_object_keysdepends_on_skipped_cells§errored$9a13b17c-0403-11eb-024f-9b37e95e211bqueued¤logsrunning¦outputbody

Exercise 4.2

👉 Run the simulation 20 times and plot $I$ as a function of time for each one, together with the mean over the 20 simulations (as you did in the previous exercises).

Note that you should be able to re-use the sweep! and simulation functions , since those should be sufficiently generic to work with the new step! function! (Modify them if they are not.)

mimetext/htmlrootassigneelast_run_timestampAupersist_js_state·has_pluto_hook_features§cell_id$9a13b17c-0403-11eb-024f-9b37e95e211bdepends_on_disabled_cells§runtimetrpublished_object_keysdepends_on_skipped_cells§errored$b92f1cec-04ae-11eb-0072-3535d1118494queued¤logsrunning¦outputbodyelementsSprefixAnyelements2text/plain0text/plain0text/plain0text/plain0text/plain0text/plain0text/plain0text/plain 0text/plainmore0text/plaintypeArrayprefix_shortobjectid761d83d545bbd707!application/vnd.pluto.tree+objectIprefixAnyelements1text/plain3text/plain2text/plain1text/plain1text/plain1text/plain1text/plain1text/plain 1text/plainmore1text/plaintypeArrayprefix_shortobjectid198db94dcb28194!application/vnd.pluto.tree+objectRprefixAnyelements0text/plain0text/plain1text/plain2text/plain2text/plain2text/plain2text/plain2text/plain 2text/plainmore2text/plaintypeArrayprefix_shortobjectidb9945178cca96c89!application/vnd.pluto.tree+objecttypeNamedTupleobjectidb99b34bcb4905a68mime!application/vnd.pluto.tree+objectrootassigneelast_run_timestampAMpersist_js_state·has_pluto_hook_features§cell_id$b92f1cec-04ae-11eb-0072-3535d1118494depends_on_disabled_cells§runtimeaõpublished_object_keysdepends_on_skipped_cells§errored$5ef5813a-0a46-11eb-00d3-01ec142e3897queued¤logsrunning¦outputbodymimetext/plainrootassigneelast_run_timestampAMpersist_js_state·has_pluto_hook_features§cell_id$5ef5813a-0a46-11eb-00d3-01ec142e3897depends_on_disabled_cells§runtime(published_object_keysdepends_on_skipped_cells§errored$9a377b32-0403-11eb-2799-e7e59caa6a45queued¤logsrunning¦outputbody

👉 Run the new simulation and draw $I$ (averaged over runs) as a function of time. Is the behaviour qualitatively the same or different? Describe what you see.

mimetext/htmlrootassigneelast_run_timestampA8persist_js_state·has_pluto_hook_features§cell_id$9a377b32-0403-11eb-2799-e7e59caa6a45depends_on_disabled_cells§runtimepublished_object_keysdepends_on_skipped_cells§errored$778ec25c-0403-11eb-3146-1d11c294bb1fqueued¤logsrunning¦outputbodyٲ

Exercise 1.5

👉 What shape does the distribution seem to have? Can you verify that by adding a second plot with the expected shape?

mimetext/htmlrootassigneelast_run_timestampA߲Ѱpersist_js_state·has_pluto_hook_features§cell_id$778ec25c-0403-11eb-3146-1d11c294bb1fdepends_on_disabled_cells§runtimepublished_object_keysdepends_on_skipped_cells§errored$9611ca24-0403-11eb-3582-b7e3bb243e62queued¤logsrunning¦outputbody

Exercise 3.3

👉 Plot the probability distribution of num_infected. Does it have a recognisable shape? (Feel free to increase the number of agents in order to get better statistics.)

mimetext/htmlrootassigneelast_run_timestampA䰰persist_js_state·has_pluto_hook_features§cell_id$9611ca24-0403-11eb-3582-b7e3bb243e62depends_on_disabled_cells§runtimeYpublished_object_keysdepends_on_skipped_cells§errored$26f84600-041d-11eb-1856-b12a3e5c1dc7queued¤logsrunning¦outputbodymimetext/plainrootassigneelast_run_timestampAGkpersist_js_state·has_pluto_hook_features§cell_id$26f84600-041d-11eb-1856-b12a3e5c1dc7depends_on_disabled_cells§runtimeX;published_object_keysdepends_on_skipped_cells§errored$1ca7a8c2-041a-11eb-146a-15b8cdeaea72queued¤logsrunning¦outputbodyprefixDict{Any, Any}elements5text/plain0.1text/plain4text/plain0.05text/plain2text/plain0.1text/plain3text/plain0.2text/plain1text/plain0.55text/plaintypeDictprefix_shortDictobjectid44cd86ee795993f0mime!application/vnd.pluto.tree+objectrootassigneelast_run_timestampAO]persist_js_state·has_pluto_hook_features§cell_id$1ca7a8c2-041a-11eb-146a-15b8cdeaea72depends_on_disabled_cells§runtime >published_object_keysdepends_on_skipped_cells§errored$190deebc-0424-11eb-19fe-615997093e14queued¤logsrunning¦outputbody

👉 For convenience, define a new constructor (i.e. a new method for the function) that takes no arguments and creates an Agent with status S and number infected 0, by calling one of the default constructors that Julia creates. This new method lives outside (not inside) the definition of the struct. (It is called an outer constructor.)

(In Pluto, multiple methods for the same function need to be combined in a single cell using a begin end block.)

Let's check that the new method works correctly. How many methods does the constructor have now?

mimetext/htmlrootassigneelast_run_timestampA.0persist_js_state·has_pluto_hook_features§cell_id$190deebc-0424-11eb-19fe-615997093e14depends_on_disabled_cells§runtime[published_object_keysdepends_on_skipped_cells§errored$1a654bdc-0421-11eb-2c38-7d35060e2565queued¤logsrunning¦outputbodymimetext/plainrootassigneelast_run_timestampA{'persist_js_state·has_pluto_hook_features§cell_id$1a654bdc-0421-11eb-2c38-7d35060e2565depends_on_disabled_cells§runtimeKpublished_object_keysdepends_on_skipped_cells§errored$41cefa68-0414-11eb-3bad-6530360d6f68queued¤logsrunning¦outputbody.keep_working (generic function with 2 methods)mimetext/plainrootassigneelast_run_timestampAɰpersist_js_state·has_pluto_hook_features§cell_id$41cefa68-0414-11eb-3bad-6530360d6f68depends_on_disabled_cells§runtime epublished_object_keysdepends_on_skipped_cells§errored$847d0fc2-041d-11eb-2864-79066e223b45queued¤logsrunning¦outputbody

👉 Convert x to an integer using the Integer function. What value does it have? What values do I and R have?

mimetext/htmlrootassigneelast_run_timestampAஒpersist_js_state·has_pluto_hook_features§cell_id$847d0fc2-041d-11eb-2864-79066e223b45depends_on_disabled_cells§runtimepublished_object_keysdepends_on_skipped_cells§errored$77428072-0403-11eb-0068-81e3728f2ebequeued¤logsrunning¦outputbodyٗ

Let's run an experiment with $p=0.25$ and $N=10,000$.

mimetext/htmlrootassigneelast_run_timestampA°persist_js_state·has_pluto_hook_features§cell_id$77428072-0403-11eb-0068-81e3728f2ebedepends_on_disabled_cells§runtime-published_object_keysdepends_on_skipped_cells§errored$9a837b52-0425-11eb-231f-a74405ff6e23queued¤logsrunning¦outputbody/is_susceptible (generic function with 1 method)mimetext/plainrootassigneelast_run_timestampAGpersist_js_state·has_pluto_hook_features§cell_id$9a837b52-0425-11eb-231f-a74405ff6e23depends_on_disabled_cells§runtime\Vpublished_object_keysdepends_on_skipped_cells§errored$0a967f38-0493-11eb-0624-77e40b24d757queued¤logsrunning¦outputbody

We used a let block in this cell to group multiple expressions together, but how is it different from begin or function?

function vs. begin vs. let

Writing functions is a way to group multiple expressions (i.e. lines of code) together into a mini-program. Note the following about functions:

  • A function always returns one object.[1] This object can be given explicitly by writing return x, or implicitly: Julia functions always return the result of the last expression by default. So f(x) = x+2 is the same as f(x) = return x+2.

  • Variables defined inside a function are not accessible outside the function. We say that function bodies have a local scope. This helps to keep your program easy to read and write: if you define a local variable, then you don't need to worry about it in the rest of the notebook.

There are two other ways to group epxressions together that you might have seen before: begin and let.

begin

begin will group expressions together, and it takes the value of its last subexpression.

We use it in this notebook when we want multiple expressions to always run together.

let

let also groups multiple expressions together into one, but variables defined inside of it are local: they don't affect code outside of the block. So like begin, it is just a block of code, but like function, it has a local variable scope.

We use it when we want to define some local (temporary) variables to produce a complicated result, without interfering with other cells. Pluto allows only one definition per global variable of the same name, but you can define local variables with the same names whenever you wish!

1

Even a function like

f(x) = return

returns one object: the object nothing — try it out!

mimetext/htmlrootassigneelast_run_timestampAɰpersist_js_state·has_pluto_hook_features§cell_id$0a967f38-0493-11eb-0624-77e40b24d757depends_on_disabled_cells§runtime 93published_object_keysdepends_on_skipped_cells§errored$28db9d98-04ca-11eb-3606-9fb89fa62f36queued¤logsrunning¦outputbodymmimetext/htmlrootassigneelast_run_timestampAWpersist_js_state·has_pluto_hook_features§cell_id$28db9d98-04ca-11eb-3606-9fb89fa62f36depends_on_disabled_cells§runtime published_object_keysdepends_on_skipped_cells§errored$8692bf42-0403-11eb-191f-b7d08895274fqueued¤logsrunning¦outputbody8

Exericse 2.4

👉 Write a function generate_agents(N) that returns a vector of N freshly created Agents. They should all be initially susceptible, except one, chosen at random (i.e. uniformly), who is infectious.

mimetext/htmlrootassigneelast_run_timestampA{lpersist_js_state·has_pluto_hook_features§cell_id$8692bf42-0403-11eb-191f-b7d08895274fdepends_on_disabled_cells§runtimepublished_object_keysdepends_on_skipped_cells§errored$da49710e-0420-11eb-092e-4f1173868738queued¤logsrunning¦outputbody

Exercise 5 - Lecture transcript

(MIT students only) Please see the link for hw 4 transcript document on Canvas. We want each of you to correct about 400 lines, but don’t spend more than 15 minutes on it. See the the beginning of the document for more instructions. :point_right: Please mention the name of the video(s) and the line ranges you edited:

mimetext/htmlrootassigneelast_run_timestampA1persist_js_state·has_pluto_hook_features§cell_id$da49710e-0420-11eb-092e-4f1173868738depends_on_disabled_cells§runtime˵published_object_keysdepends_on_skipped_cells§errored$7bb8e426-0495-11eb-3a8b-cbbab61a1631queued¤logsrunning¦outputbodyڋ:
mimetext/htmlrootassigneelast_run_timestampApersist_js_state·has_pluto_hook_features§cell_id$7bb8e426-0495-11eb-3a8b-cbbab61a1631depends_on_disabled_cells§runtime `tpublished_object_keysdepends_on_skipped_cells§errored$a4c9ccdc-12ca-11eb-072f-e34595520548queued¤logsrunning¦outputbodyl"(S = [0.99, 0.989, 0.989, 0.988, 0.9875, 0.9875, 0.9875, 0.987, 0.9865, 0.9865, 0.986, 0.9855, 0.985, 0.985, 0.985, 0.985, 0.985, 0.985, 0.985, 0.985, 0.985, 0.985, 0.985, 0.9845, 0.9845, 0.9835, 0.983, 0.982, 0.982, 0.982, 0.982, 0.9815, 0.9815, 0.981, 0.9805, 0.98, 0.98, 0.9795, 0.9795, 0.9795, " ⋯ 21644 bytes ⋯ "0.605, 0.606, 0.606, 0.606, 0.607, 0.6075, 0.6085, 0.609, 0.6095, 0.6095, 0.61, 0.61, 0.61, 0.6105, 0.611, 0.6115, 0.6125, 0.6135, 0.6145, 0.6145, 0.615, 0.615, 0.616, 0.616, 0.617, 0.6185, 0.619, 0.619, 0.619, 0.6205, 0.6205, 0.6205, 0.6205, 0.6215, 0.623, 0.6245, 0.6255, 0.626, 0.6265, 0.6265])"mimetext/plainrootassigneelast_run_timestampAנpersist_js_state·has_pluto_hook_features§cell_id$a4c9ccdc-12ca-11eb-072f-e34595520548depends_on_disabled_cells§runtime 9published_object_keysdepends_on_skipped_cells§errored$21c50840-0435-11eb-1307-7138ecde0691queued¤logsrunning¦outputbodymimetext/plainrootassigneelast_run_timestampApersist_js_state·has_pluto_hook_features§cell_id$21c50840-0435-11eb-1307-7138ecde0691depends_on_disabled_cells§runtimeꁵpublished_object_keysdepends_on_skipped_cells§errored$2ade2694-0425-11eb-2fb2-390da43d9695queued¤logsrunning¦outputbodymimetext/plainrootassigneelast_run_timestampAKpersist_js_state·has_pluto_hook_features§cell_id$2ade2694-0425-11eb-2fb2-390da43d9695depends_on_disabled_cells§runtime7.published_object_keysdepends_on_skipped_cells§errored$aa6673d4-0417-11eb-32cc-79560896c195queued¤logsrunning¦outputbody,frequencies (generic function with 1 method)mimetext/plainrootassigneelast_run_timestampA54persist_js_state·has_pluto_hook_features§cell_id$aa6673d4-0417-11eb-32cc-79560896c195depends_on_disabled_cells§runtime ;published_object_keysdepends_on_skipped_cells§errored$73047bba-0416-11eb-1047-23e9c3dbde05queued¤logsrunning¦outputbody-

blablabla

mimetext/htmlrootassigneeinterpretation_of_p_equals_onelast_run_timestampA*^persist_js_state·has_pluto_hook_features§cell_id$73047bba-0416-11eb-1047-23e9c3dbde05depends_on_disabled_cells§runtimepublished_object_keysdepends_on_skipped_cells§errored$bf6fd176-04cc-11eb-008a-2fb6ff70a9cbqueued¤logsrunning¦outputbody

Exercise 3.2

Alright! Every time that we run the simulation, we get slightly different results, because it is based on randomness. By running the simulation a number of times, you start to get an idea of the mean behaviour of our model. This is the essence of a Monte Carlo method! You use computer-generated randomness to generate samples.

Instead of pressing the button many times, let's have the computer repeat the simulation. In the next cells, we run your simulation num_simulations=20 times with $N=100$, $p_\text{infection} = 0.02$, $p_\text{infection} = 0.002$ and $T = 1000$.

Every single simulation returns a named tuple with the status counts, so the result of multiple simulations will be an array of those. Have a look inside the result, simulations, and make sure that its structure is clear.

mimetext/htmlrootassigneelast_run_timestampApersist_js_state·has_pluto_hook_features§cell_id$bf6fd176-04cc-11eb-008a-2fb6ff70a9cbdepends_on_disabled_cells§runtime)published_object_keysdepends_on_skipped_cells§errored$43e6e856-0414-11eb-19ca-07358aa8b667queued¤logsrunning¦outputbody/still_missing (generic function with 2 methods)mimetext/plainrootassigneelast_run_timestampAԕ%persist_js_state·has_pluto_hook_features§cell_id$43e6e856-0414-11eb-19ca-07358aa8b667depends_on_disabled_cells§runtime published_object_keysdepends_on_skipped_cells§errored$95c598d4-0403-11eb-2328-0175ed564915queued¤logsrunning¦outputbody

👉 Write a function sir_mean_plot that returns a plot of the means of $S$, $I$ and $R$ as a function of time on a single graph.

mimetext/htmlrootassigneelast_run_timestampA8ɰpersist_js_state·has_pluto_hook_features§cell_id$95c598d4-0403-11eb-2328-0175ed564915depends_on_disabled_cells§runtimeJpublished_object_keysdepends_on_skipped_cells§errored$2b26dc42-0403-11eb-205f-cd2c23d8cb03queued¤logsrunning¦outputbody




mimetext/htmlrootassigneelast_run_timestampABLpersist_js_state·has_pluto_hook_features§cell_id$2b26dc42-0403-11eb-205f-cd2c23d8cb03depends_on_disabled_cells§runtime"published_object_keysdepends_on_skipped_cells§errored$1ac4b33a-0435-11eb-36f8-8f3f81ae7844queued¤logsrunning¦outputbodymimetext/plainrootassigneelast_run_timestampAPpersist_js_state·has_pluto_hook_features§cell_id$1ac4b33a-0435-11eb-36f8-8f3f81ae7844depends_on_disabled_cells§runtimepublished_object_keysdepends_on_skipped_cells§errored$a8dd5cae-0425-11eb-119c-bfcbf832d695queued¤logsrunning¦outputbody,is_infected (generic function with 1 method)mimetext/plainrootassigneelast_run_timestampAR"}persist_js_state·has_pluto_hook_features§cell_id$a8dd5cae-0425-11eb-119c-bfcbf832d695depends_on_disabled_cells§runtime published_object_keysdepends_on_skipped_cells§errored±cell_dependencies$f1f89502-0494-11eb-2303-0b79d8bbd13fprecedence_heuristic cell_id$f1f89502-0494-11eb-2303-0b79d8bbd13fdownstream_cells_mapfrequencies_plot_with_mean$06089d1e-0495-11eb-0ace-a7a7dc60e5b2upstream_cells_mapmissing$95771ce2-0403-11eb-3056-f1dc3a8b7ec3precedence_heuristic cell_id$95771ce2-0403-11eb-3056-f1dc3a8b7ec3downstream_cells_mapupstream_cells_map@md_strgetindex$e6219c7c-0420-11eb-3faa-13126f7c8007precedence_heuristic cell_id$e6219c7c-0420-11eb-3faa-13126f7c8007downstream_cells_maplines_i_editedupstream_cells_map@md_strgetindex$b817f466-04d4-11eb-0a26-c1c667f9f7f7precedence_heuristic cell_id$b817f466-04d4-11eb-0a26-c1c667f9f7f7downstream_cells_mapupstream_cells_map@md_strBase.getindexMissing!@isdefinedBoolstill_missing$43e6e856-0414-11eb-19ca-07358aa8b667isaBasecorrect$3d88c056-0414-11eb-0025-05d3aff1588bkeep_working$41cefa68-0414-11eb-3bad-6530360d6f68not_defined$3c0528a0-0414-11eb-2f68-a5657ab9e73d==bernoulli$d8797684-0414-11eb-1869-5b1e2c469011$08e2bc64-0417-11eb-1457-21c0d18e8c51precedence_heuristic cell_id$08e2bc64-0417-11eb-1457-21c0d18e8c51downstream_cells_mapupstream_cells_map@md_strhint$48a16c42-0414-11eb-0e0c-bf52bbb0f618getindex$bb8aeb58-042f-11eb-18b8-f995631df619precedence_heuristic cell_id$bb8aeb58-042f-11eb-18b8-f995631df619downstream_cells_mapupstream_cells_map@md_strgetindex$223933a4-042c-11eb-10d3-852229f25a35precedence_heuristic cell_id$223933a4-042c-11eb-10d3-852229f25a35downstream_cells_mapAbstractInfection$1a654bdc-0421-11eb-2c38-7d35060e2565$8a1d5aea-04ae-11eb-2177-eb37822db4f1$32cea7ba-0429-11eb-06bc-a5f4ae3ffe37$3d3b672c-0426-11eb-0a36-153ce3c276b9upstream_cells_map$ae4ac4b4-041f-11eb-14f5-1bcde35d18f2precedence_heuristic cell_id$ae4ac4b4-041f-11eb-14f5-1bcde35d18f2downstream_cells_mapAgent$82f2580a-04c8-11eb-1eea-bdb4e50eee3b$98beb336-0425-11eb-3886-4f8cfd210288$7c515a7a-04d5-11eb-0f36-4fcebff709d5$9a837b52-0425-11eb-231f-a74405ff6e23$a8dd5cae-0425-11eb-119c-bfcbf832d695$c4a8694a-04d4-11eb-1eef-c9e037e6b21f$6d480cf0-0425-11eb-18a9-1737455371d7$393041ec-049f-11eb-3089-2faf378445f3$221bc0ac-04aa-11eb-1331-2b16ebc1ee57$9c39974c-04a5-11eb-184d-317eb542452c$759bc42e-04ab-11eb-0ab1-b12e008c02a9$1491a078-04aa-11eb-0106-19a3cf1e94b0$f8e05d94-04ac-11eb-26d4-6f1d2c5ed272$8a1d5aea-04ae-11eb-2177-eb37822db4f1$32cea7ba-0429-11eb-06bc-a5f4ae3ffe37upstream_cells_mapInt64InfectionStatus$26f84600-041d-11eb-1856-b12a3e5c1dc7$7f635722-04d0-11eb-3209-4b603c9e843cprecedence_heuristic cell_id$7f635722-04d0-11eb-3209-4b603c9e843cdownstream_cells_mapupstream_cells_mapsimulations$80c2cd88-04b1-11eb-326e-0120a39405easir_mean_plot$10cf6db8-04b8-11eb-2267-3db6d7f9c89a$7c515a7a-04d5-11eb-0f36-4fcebff709d5precedence_heuristic cell_id$7c515a7a-04d5-11eb-0f36-4fcebff709d5downstream_cells_mapupstream_cells_mapI$26f84600-041d-11eb-1856-b12a3e5c1dc7correct$3d88c056-0414-11eb-0025-05d3aff1588bkeep_working$41cefa68-0414-11eb-3bad-6530360d6f68!not_defined$3c0528a0-0414-11eb-2f68-a5657ab9e73d@isdefinedAgent$ae4ac4b4-041f-11eb-14f5-1bcde35d18f2R$26f84600-041d-11eb-1856-b12a3e5c1dc7==set_status!$98beb336-0425-11eb-3886-4f8cfd210288$107e65a4-0403-11eb-0c14-37d8d828b469precedence_heuristic cell_id$107e65a4-0403-11eb-0c14-37d8d828b469downstream_cells_mapupstream_cells_map@md_strgetindex$1c6aa208-04d1-11eb-0b87-cf429e6ff6d0precedence_heuristic cell_id$1c6aa208-04d1-11eb-0b87-cf429e6ff6d0downstream_cells_mapupstream_cells_map$80e6f1e0-04b1-11eb-0d4e-475f1d80c2bbprecedence_heuristic cell_id$80e6f1e0-04b1-11eb-0d4e-475f1d80c2bbdownstream_cells_mapupstream_cells_map@md_strgetindex$7f4e121c-041d-11eb-0dff-cd0cbfdfd606precedence_heuristic cell_id$7f4e121c-041d-11eb-0dff-cd0cbfdfd606downstream_cells_maptest_statusupstream_cells_mapmissing$3d3b672c-0426-11eb-0a36-153ce3c276b9precedence_heuristic cell_id$3d3b672c-0426-11eb-0a36-153ce3c276b9downstream_cells_mapsimulation$b92f1cec-04ae-11eb-0072-3535d1118494$2c62b4ae-04b3-11eb-0080-a1035a7e31a2$c5156c72-04af-11eb-1106-b13969b036ca$38b1aa5a-04cf-11eb-11a2-930741fc9076upstream_cells_map:sumAbstractInfection$223933a4-042c-11eb-10d3-852229f25a35sweep!$32cea7ba-0429-11eb-06bc-a5f4ae3ffe37Integergenerate_agents$6d480cf0-0425-11eb-18a9-1737455371d7I$26f84600-041d-11eb-1856-b12a3e5c1dc7S$26f84600-041d-11eb-1856-b12a3e5c1dc7push!R$26f84600-041d-11eb-1856-b12a3e5c1dc7==$4f19e872-0414-11eb-0dfd-e53d2aecc4dcprecedence_heuristic cell_id$4f19e872-0414-11eb-0dfd-e53d2aecc4dcdownstream_cells_mapupstream_cells_map@md_strgetindex$759bc42e-04ab-11eb-0ab1-b12e008c02a9precedence_heuristic cell_id$759bc42e-04ab-11eb-0ab1-b12e008c02a9downstream_cells_mapupstream_cells_map@md_strBase.getindex!@isdefinedAgent$ae4ac4b4-041f-11eb-14f5-1bcde35d18f2interact!$221bc0ac-04aa-11eb-1331-2b16ebc1ee57BaseI$26f84600-041d-11eb-1856-b12a3e5c1dc7S$26f84600-041d-11eb-1856-b12a3e5c1dc7correct$3d88c056-0414-11eb-0025-05d3aff1588bkeep_working$41cefa68-0414-11eb-3bad-6530360d6f68not_defined$3c0528a0-0414-11eb-2f68-a5657ab9e73d!=R$26f84600-041d-11eb-1856-b12a3e5c1dc7==almost$461586dc-0414-11eb-00f3-4984b57bfac5InfectionRecovery$1a654bdc-0421-11eb-2c38-7d35060e2565$5689841e-0414-11eb-0492-63c77ddbd136precedence_heuristic cell_id$5689841e-0414-11eb-0492-63c77ddbd136downstream_cells_mapupstream_cells_mapbigbreak$39dffa3c-0414-11eb-0197-e72b299e9c63$99ef7b2a-0403-11eb-08ef-e1023cd151aeprecedence_heuristic cell_id$99ef7b2a-0403-11eb-08ef-e1023cd151aedownstream_cells_mapupstream_cells_map@md_strgetindex$77b54c10-0403-11eb-16ad-65374d29a817precedence_heuristic cell_id$77b54c10-0403-11eb-16ad-65374d29a817downstream_cells_mapupstream_cells_map@md_strgetindex$7768a2dc-0403-11eb-39b7-fd660dc952feprecedence_heuristic cell_id$7768a2dc-0403-11eb-39b7-fd660dc952fedownstream_cells_mapupstream_cells_map@md_strgetindex$60a8b708-04c8-11eb-37b1-3daec644ac90precedence_heuristic cell_id$60a8b708-04c8-11eb-37b1-3daec644ac90downstream_cells_mapupstream_cells_map$95eb9f88-0403-11eb-155b-7b2d3a07cff0precedence_heuristic cell_id$95eb9f88-0403-11eb-155b-7b2d3a07cff0downstream_cells_mapupstream_cells_map@md_strgetindex$955321de-0403-11eb-04ce-fb1670dfbb9eprecedence_heuristic cell_id$955321de-0403-11eb-04ce-fb1670dfbb9edownstream_cells_mapupstream_cells_map@md_strgetindex$ae70625a-041f-11eb-3082-0753419d6d57precedence_heuristic cell_id$ae70625a-041f-11eb-3082-0753419d6d57downstream_cells_mapupstream_cells_map@md_strgetindex$843fd63c-04d0-11eb-0113-c58d346179d6precedence_heuristic cell_id$843fd63c-04d0-11eb-0113-c58d346179d6downstream_cells_mapupstream_cells_map$189cae1e-0424-11eb-2666-65bf297d8bddprecedence_heuristic cell_id$189cae1e-0424-11eb-2666-65bf297d8bdddownstream_cells_mapupstream_cells_map@md_strgetindex$7f744644-041d-11eb-08a0-3719cc0adeb7precedence_heuristic cell_id$7f744644-041d-11eb-08a0-3719cc0adeb7downstream_cells_mapupstream_cells_map@md_strgetindex$488771e2-049f-11eb-3b0a-0de260457731precedence_heuristic cell_id$488771e2-049f-11eb-3b0a-0de260457731downstream_cells_mapupstream_cells_mapgenerate_agents$6d480cf0-0425-11eb-18a9-1737455371d7$393041ec-049f-11eb-3089-2faf378445f3precedence_heuristic cell_id$393041ec-049f-11eb-3089-2faf378445f3downstream_cells_mapupstream_cells_map@md_strsumMissing!Vectorlengthcorrect$3d88c056-0414-11eb-0025-05d3aff1588bkeep_working$41cefa68-0414-11eb-3bad-6530360d6f68not_defined$3c0528a0-0414-11eb-2f68-a5657ab9e73d!===almost$461586dc-0414-11eb-00f3-4984b57bfac5SetBase.getindexNothing@isdefinedAgent$ae4ac4b4-041f-11eb-14f5-1bcde35d18f2still_missing$43e6e856-0414-11eb-19ca-07358aa8b667generate_agents$6d480cf0-0425-11eb-18a9-1737455371d7isaBaseallI$26f84600-041d-11eb-1856-b12a3e5c1dc7$531d13c2-0414-11eb-0acd-4905a684869dprecedence_heuristic cell_id$531d13c2-0414-11eb-0acd-4905a684869ddownstream_cells_mapupstream_cells_map@md_strstudent$095cbf46-0403-11eb-0c37-35de9562cebc==getindex$221bc0ac-04aa-11eb-1331-2b16ebc1ee57precedence_heuristic cell_id$221bc0ac-04aa-11eb-1331-2b16ebc1ee57downstream_cells_mapinteract!$9c39974c-04a5-11eb-184d-317eb542452c$759bc42e-04ab-11eb-0ab1-b12e008c02a9$1491a078-04aa-11eb-0106-19a3cf1e94b0$f8e05d94-04ac-11eb-26d4-6f1d2c5ed272$8a1d5aea-04ae-11eb-2177-eb37822db4f1upstream_cells_mapInfectionRecovery$1a654bdc-0421-11eb-2c38-7d35060e2565I$26f84600-041d-11eb-1856-b12a3e5c1dc7S$26f84600-041d-11eb-1856-b12a3e5c1dc7Reinfection+Agent$ae4ac4b4-041f-11eb-14f5-1bcde35d18f2R$26f84600-041d-11eb-1856-b12a3e5c1dc7==bernoulli$d8797684-0414-11eb-1869-5b1e2c469011$32cea7ba-0429-11eb-06bc-a5f4ae3ffe37precedence_heuristic cell_id$32cea7ba-0429-11eb-06bc-a5f4ae3ffe37downstream_cells_mapsweep!$3d3b672c-0426-11eb-0a36-153ce3c276b9upstream_cells_mapstep!$8a1d5aea-04ae-11eb-2177-eb37822db4f1AbstractInfection$223933a4-042c-11eb-10d3-852229f25a35Agent$ae4ac4b4-041f-11eb-14f5-1bcde35d18f2Vector$06f30b2a-0403-11eb-0f05-8badebe1011dprecedence_heuristic cell_id$06f30b2a-0403-11eb-0f05-8badebe1011ddownstream_cells_mapupstream_cells_map@md_strgetindex$9c39974c-04a5-11eb-184d-317eb542452cprecedence_heuristic cell_id$9c39974c-04a5-11eb-184d-317eb542452cdownstream_cells_mapupstream_cells_mapInfectionRecovery$1a654bdc-0421-11eb-2c38-7d35060e2565I$26f84600-041d-11eb-1856-b12a3e5c1dc7S$26f84600-041d-11eb-1856-b12a3e5c1dc7Agent$ae4ac4b4-041f-11eb-14f5-1bcde35d18f2interact!$221bc0ac-04aa-11eb-1331-2b16ebc1ee57$1ddbaa18-0494-11eb-1fc8-250ab6ae89f1precedence_heuristic cell_id$1ddbaa18-0494-11eb-1fc8-250ab6ae89f1downstream_cells_mapupstream_cells_maplarge_experiment$4b3ec86c-0419-11eb-26fd-cbbfdf19afa8frequencies_plot_with_maximum$823364ce-041c-11eb-2467-7ffa4f751527$06089d1e-0495-11eb-0ace-a7a7dc60e5b2precedence_heuristic cell_id$06089d1e-0495-11eb-0ace-a7a7dc60e5b2downstream_cells_mapupstream_cells_maplarge_experiment$4b3ec86c-0419-11eb-26fd-cbbfdf19afa8frequencies_plot_with_mean$f1f89502-0494-11eb-2303-0b79d8bbd13f$15187690-0403-11eb-2dfd-fd924faa3513precedence_heuristiccell_id$15187690-0403-11eb-2dfd-fd924faa3513downstream_cells_mapPlutoUIPlotsupstream_cells_mapPkg.addPkg$12cc2940-0403-11eb-19a7-bb570de58f6fplotly$d8abd2f6-0416-11eb-1c2a-f9157d9760a7precedence_heuristic cell_id$d8abd2f6-0416-11eb-1c2a-f9157d9760a7downstream_cells_mapsmall_experiment$1ca7a8c2-041a-11eb-146a-15b8cdeaea72upstream_cells_mapdo_experiment$cdaade9c-0416-11eb-0550-7b5b3d33e240$9374e63c-0493-11eb-0952-4b97512d7cdbprecedence_heuristic cell_id$9374e63c-0493-11eb-0952-4b97512d7cdbdownstream_cells_mapupstream_cells_map@md_strgetindex$2d3bba2a-04a8-11eb-2c40-87794b6aeeacprecedence_heuristic cell_id$2d3bba2a-04a8-11eb-2c40-87794b6aeeacdownstream_cells_mapupstream_cells_map@md_strBase.getindexBaseBase.Docs.HTML@html_str$10cf6db8-04b8-11eb-2267-3db6d7f9c89aprecedence_heuristic cell_id$10cf6db8-04b8-11eb-2267-3db6d7f9c89adownstream_cells_mapsir_mean_plot$7f635722-04d0-11eb-3209-4b603c9e843cupstream_cells_mapNamedTuplelength:summapplot/firstplot!Vector$dfb99ace-04cf-11eb-0739-7d694c837d59precedence_heuristic cell_id$dfb99ace-04cf-11eb-0739-7d694c837d59downstream_cells_mapupstream_cells_map@md_strgetindex$271ec5f0-041d-11eb-041b-db46ec1465e0precedence_heuristic cell_id$271ec5f0-041d-11eb-041b-db46ec1465e0downstream_cells_mapupstream_cells_map@md_strgetindex$7946d83a-04a0-11eb-224b-2b315e87bc84precedence_heuristic cell_id$7946d83a-04a0-11eb-224b-2b315e87bc84downstream_cells_mapupstream_cells_map$02b0c2fc-0415-11eb-2b40-7bca8ea4eef9precedence_heuristic cell_id$02b0c2fc-0415-11eb-2b40-7bca8ea4eef9downstream_cells_mapupstream_cells_map$9635c944-0403-11eb-3982-4df509f6a556precedence_heuristic cell_id$9635c944-0403-11eb-3982-4df509f6a556downstream_cells_mapupstream_cells_map@md_strgetindex$f3f81172-041c-11eb-2b9b-e99b7b9400edprecedence_heuristic cell_id$f3f81172-041c-11eb-2b9b-e99b7b9400eddownstream_cells_mapupstream_cells_map@md_strBase.getindexBaseBase.Docs.HTML@html_str$8a1d5aea-04ae-11eb-2177-eb37822db4f1precedence_heuristic cell_id$8a1d5aea-04ae-11eb-2177-eb37822db4f1downstream_cells_mapstep!$32cea7ba-0429-11eb-06bc-a5f4ae3ffe37upstream_cells_mapVectorAbstractInfection$223933a4-042c-11eb-10d3-852229f25a35Agent$ae4ac4b4-041f-11eb-14f5-1bcde35d18f2randinteract!$221bc0ac-04aa-11eb-1331-2b16ebc1ee57$4ad11052-042c-11eb-3643-8b2b3e1269bcprecedence_heuristic cell_id$4ad11052-042c-11eb-3643-8b2b3e1269bcdownstream_cells_mapupstream_cells_map$76d117d4-0403-11eb-05d2-c5ea47d06f43precedence_heuristic cell_id$76d117d4-0403-11eb-05d2-c5ea47d06f43downstream_cells_mapupstream_cells_map@md_strgetindex$095cbf46-0403-11eb-0c37-35de9562cebcprecedence_heuristic cell_id$095cbf46-0403-11eb-0c37-35de9562cebcdownstream_cells_mapstudent$03a85970-0403-11eb-334a-812b59c0905b$531d13c2-0414-11eb-0acd-4905a684869dupstream_cells_map$6d5c6a84-0415-11eb-3fdf-9355200cb520precedence_heuristic cell_id$6d5c6a84-0415-11eb-3fdf-9355200cb520downstream_cells_maprecovery_time$c61f35ea-04d6-11eb-2503-17a79f8d0298$cdaade9c-0416-11eb-0550-7b5b3d33e240upstream_cells_map<=throw+≤ArgumentErrorbernoulli$d8797684-0414-11eb-1869-5b1e2c469011$26e2978e-0435-11eb-0d61-25f552d2771eprecedence_heuristic cell_id$26e2978e-0435-11eb-0d61-25f552d2771edownstream_cells_mapupstream_cells_map$6de37d6c-0415-11eb-1b05-85ac820016c7precedence_heuristic cell_id$6de37d6c-0415-11eb-1b05-85ac820016c7downstream_cells_mapupstream_cells_map@md_strgetindex$18d308c4-0424-11eb-176d-49feec6889cfprecedence_heuristic cell_id$18d308c4-0424-11eb-176d-49feec6889cfdownstream_cells_maptest_agentupstream_cells_mapmissing$46133a74-04b1-11eb-0b46-0bc74e564680precedence_heuristic cell_id$46133a74-04b1-11eb-0b46-0bc74e564680downstream_cells_mapupstream_cells_map$771c8f0c-0403-11eb-097e-ab24d0714ad5precedence_heuristic cell_id$771c8f0c-0403-11eb-097e-ab24d0714ad5downstream_cells_mapupstream_cells_map@md_strgetindex$3f497394-0a46-11eb-3369-8189905f011cprecedence_heuristic cell_id$3f497394-0a46-11eb-3369-8189905f011cdownstream_cells_mapupstream_cells_map$80c2cd88-04b1-11eb-326e-0120a39405eaprecedence_heuristic cell_id$80c2cd88-04b1-11eb-326e-0120a39405eadownstream_cells_mapsimulations$9cd2bb00-04b1-11eb-1d83-a703907141a7$7f635722-04d0-11eb-3209-4b603c9e843c$a4c9ccdc-12ca-11eb-072f-e34595520548upstream_cells_maprepeat_simulations$38b1aa5a-04cf-11eb-11a2-930741fc9076InfectionRecovery$1a654bdc-0421-11eb-2c38-7d35060e2565$105d347e-041c-11eb-2fc8-1d9e5eda2be0precedence_heuristic cell_id$105d347e-041c-11eb-2fc8-1d9e5eda2be0downstream_cells_mapupstream_cells_map$2c62b4ae-04b3-11eb-0080-a1035a7e31a2precedence_heuristic cell_id$2c62b4ae-04b3-11eb-0080-a1035a7e31a2downstream_cells_mapupstream_cells_mapsimulation$3d3b672c-0426-11eb-0a36-153ce3c276b9InfectionRecovery$1a654bdc-0421-11eb-2c38-7d35060e2565$6db6c894-0415-11eb-305a-c75b119d89e9precedence_heuristic cell_id$6db6c894-0415-11eb-305a-c75b119d89e9downstream_cells_mapupstream_cells_map@md_strgetindex$77db111e-0403-11eb-2dea-4b42ceed65d6precedence_heuristic cell_id$77db111e-0403-11eb-2dea-4b42ceed65d6downstream_cells_mapupstream_cells_map@md_strgetindex$6d480cf0-0425-11eb-18a9-1737455371d7precedence_heuristic cell_id$6d480cf0-0425-11eb-18a9-1737455371d7downstream_cells_mapgenerate_agents$488771e2-049f-11eb-3b0a-0de260457731$393041ec-049f-11eb-3089-2faf378445f3$3d3b672c-0426-11eb-0a36-153ce3c276b9upstream_cells_mapI$26f84600-041d-11eb-1856-b12a3e5c1dc7S$26f84600-041d-11eb-1856-b12a3e5c1dc7:Agent$ae4ac4b4-041f-11eb-14f5-1bcde35d18f2randInteger$287ee7aa-0435-11eb-0ca3-951dbbe69404precedence_heuristic cell_id$287ee7aa-0435-11eb-0ca3-951dbbe69404downstream_cells_mapsir_mean_error_plotupstream_cells_mapNamedTuplelengthmissingfirstVector$3d88c056-0414-11eb-0025-05d3aff1588bprecedence_heuristic cell_id$3d88c056-0414-11eb-0025-05d3aff1588bdownstream_cells_mapcorrect$b817f466-04d4-11eb-0a26-c1c667f9f7f7$c61f35ea-04d6-11eb-2503-17a79f8d0298$7c515a7a-04d5-11eb-0f36-4fcebff709d5$c4a8694a-04d4-11eb-1eef-c9e037e6b21f$393041ec-049f-11eb-3089-2faf378445f3$759bc42e-04ab-11eb-0ab1-b12e008c02a9$1491a078-04aa-11eb-0106-19a3cf1e94b0$f8e05d94-04ac-11eb-26d4-6f1d2c5ed272upstream_cells_mapMarkdown.Admonitionyays$3f5e0af8-0414-11eb-34a7-a71e7aaf6443Markdown.MDMarkdownrand$03a85970-0403-11eb-334a-812b59c0905bprecedence_heuristic cell_id$03a85970-0403-11eb-334a-812b59c0905bdownstream_cells_mapupstream_cells_map@md_strstudent$095cbf46-0403-11eb-0c37-35de9562cebcgetindex$6d906d0c-0415-11eb-0c1c-b5c0aca841dbprecedence_heuristic cell_id$6d906d0c-0415-11eb-0c1c-b5c0aca841dbdownstream_cells_mapupstream_cells_map@md_strhint$48a16c42-0414-11eb-0e0c-bf52bbb0f618getindex$866299e8-0403-11eb-085d-2b93459cc141precedence_heuristic cell_id$866299e8-0403-11eb-085d-2b93459cc141downstream_cells_mapupstream_cells_map@md_strgetindex$461586dc-0414-11eb-00f3-4984b57bfac5precedence_heuristic cell_id$461586dc-0414-11eb-00f3-4984b57bfac5downstream_cells_mapalmost$393041ec-049f-11eb-3089-2faf378445f3$759bc42e-04ab-11eb-0ab1-b12e008c02a9upstream_cells_mapMarkdown.AdmonitionMarkdown.MDMarkdown$c5156c72-04af-11eb-1106-b13969b036caprecedence_heuristic cell_id$c5156c72-04af-11eb-1106-b13969b036cadownstream_cells_mapupstream_cells_map:plotrun_basic_sir$28db9d98-04ca-11eb-3606-9fb89fa62f36simulation$3d3b672c-0426-11eb-0a36-153ce3c276b9plot!InfectionRecovery$1a654bdc-0421-11eb-2c38-7d35060e2565$c4a8694a-04d4-11eb-1eef-c9e037e6b21fprecedence_heuristic cell_id$c4a8694a-04d4-11eb-1eef-c9e037e6b21fdownstream_cells_mapupstream_cells_map@md_strBase.getindexMissing!is_susceptible$9a837b52-0425-11eb-231f-a74405ff6e23@isdefinedBoolAgent$ae4ac4b4-041f-11eb-14f5-1bcde35d18f2still_missing$43e6e856-0414-11eb-19ca-07358aa8b667is_infected$a8dd5cae-0425-11eb-119c-bfcbf832d695isaI$26f84600-041d-11eb-1856-b12a3e5c1dc7S$26f84600-041d-11eb-1856-b12a3e5c1dc7===keep_working$41cefa68-0414-11eb-3bad-6530360d6f68Basenot_defined$3c0528a0-0414-11eb-2f68-a5657ab9e73dcorrect$3d88c056-0414-11eb-0025-05d3aff1588bR$26f84600-041d-11eb-1856-b12a3e5c1dc7$1491a078-04aa-11eb-0106-19a3cf1e94b0precedence_heuristic cell_id$1491a078-04aa-11eb-0106-19a3cf1e94b0downstream_cells_mapupstream_cells_map@md_strBase.getindex!@isdefinedAgent$ae4ac4b4-041f-11eb-14f5-1bcde35d18f2interact!$221bc0ac-04aa-11eb-1331-2b16ebc1ee57BaseI$26f84600-041d-11eb-1856-b12a3e5c1dc7S$26f84600-041d-11eb-1856-b12a3e5c1dc7correct$3d88c056-0414-11eb-0025-05d3aff1588bkeep_working$41cefa68-0414-11eb-3bad-6530360d6f68not_defined$3c0528a0-0414-11eb-2f68-a5657ab9e73d!=R$26f84600-041d-11eb-1856-b12a3e5c1dc7==InfectionRecovery$1a654bdc-0421-11eb-2c38-7d35060e2565$c5c7cb86-041b-11eb-3360-45463105f3c9precedence_heuristic cell_id$c5c7cb86-041b-11eb-3360-45463105f3c9downstream_cells_mapupstream_cells_map$01341648-0403-11eb-2212-db450c299f35precedence_heuristic cell_id$01341648-0403-11eb-2212-db450c299f35downstream_cells_mapupstream_cells_map@md_strgetindex$d8797684-0414-11eb-1869-5b1e2c469011precedence_heuristic cell_id$d8797684-0414-11eb-1869-5b1e2c469011downstream_cells_mapbernoulli$b817f466-04d4-11eb-0a26-c1c667f9f7f7$6d5c6a84-0415-11eb-3fdf-9355200cb520$221bc0ac-04aa-11eb-1331-2b16ebc1ee57upstream_cells_map!isless@isdefinedIntegerstill_missing$43e6e856-0414-11eb-19ca-07358aa8b667isafirstround$21c50840-0435-11eb-1307-7138ecde0691precedence_heuristic cell_id$21c50840-0435-11eb-1307-7138ecde0691downstream_cells_mapupstream_cells_map$2ade2694-0425-11eb-2fb2-390da43d9695precedence_heuristic cell_id$2ade2694-0425-11eb-2fb2-390da43d9695downstream_cells_mapupstream_cells_map$aa6673d4-0417-11eb-32cc-79560896c195precedence_heuristic cell_id$aa6673d4-0417-11eb-32cc-79560896c195downstream_cells_mapfrequencies$1ca7a8c2-041a-11eb-146a-15b8cdeaea72$8a28c56e-04b4-11eb-279c-3b4dfb2a9f9b$823364ce-041c-11eb-2467-7ffa4f751527$7bb8e426-0495-11eb-3a8b-cbbab61a1631upstream_cells_maplengthDict/+get$73047bba-0416-11eb-1047-23e9c3dbde05precedence_heuristic cell_id$73047bba-0416-11eb-1047-23e9c3dbde05downstream_cells_mapinterpretation_of_p_equals_oneupstream_cells_map@md_strgetindex$bf6fd176-04cc-11eb-008a-2fb6ff70a9cbprecedence_heuristic cell_id$bf6fd176-04cc-11eb-008a-2fb6ff70a9cbdownstream_cells_mapupstream_cells_map@md_strgetindex$43e6e856-0414-11eb-19ca-07358aa8b667precedence_heuristic cell_id$43e6e856-0414-11eb-19ca-07358aa8b667downstream_cells_mapstill_missing$b817f466-04d4-11eb-0a26-c1c667f9f7f7$c61f35ea-04d6-11eb-2503-17a79f8d0298$c4a8694a-04d4-11eb-1eef-c9e037e6b21f$393041ec-049f-11eb-3089-2faf378445f3upstream_cells_map@md_strMarkdown.AdmonitionMarkdown.MDMarkdowngetindex$95c598d4-0403-11eb-2328-0175ed564915precedence_heuristic cell_id$95c598d4-0403-11eb-2328-0175ed564915downstream_cells_mapupstream_cells_map@md_strgetindex$2b26dc42-0403-11eb-205f-cd2c23d8cb03precedence_heuristic cell_id$2b26dc42-0403-11eb-205f-cd2c23d8cb03downstream_cells_mapupstream_cells_mapbigbreak$39dffa3c-0414-11eb-0197-e72b299e9c63$1ac4b33a-0435-11eb-36f8-8f3f81ae7844precedence_heuristic cell_id$1ac4b33a-0435-11eb-36f8-8f3f81ae7844downstream_cells_mapupstream_cells_map$a8dd5cae-0425-11eb-119c-bfcbf832d695precedence_heuristic cell_id$a8dd5cae-0425-11eb-119c-bfcbf832d695downstream_cells_mapis_infected$c4a8694a-04d4-11eb-1eef-c9e037e6b21fupstream_cells_mapmissingAgent$ae4ac4b4-041f-11eb-14f5-1bcde35d18f2cell_execution_order$12cc2940-0403-11eb-19a7-bb570de58f6f$15187690-0403-11eb-2dfd-fd924faa3513$01341648-0403-11eb-2212-db450c299f35$06f30b2a-0403-11eb-0f05-8badebe1011d$095cbf46-0403-11eb-0c37-35de9562cebc$03a85970-0403-11eb-334a-812b59c0905b$107e65a4-0403-11eb-0c14-37d8d828b469$1d3356c4-0403-11eb-0f48-01b5eb14a585$df8547b4-0400-11eb-07c6-fb370b61c2b6$d8797684-0414-11eb-1869-5b1e2c469011$02b0c2fc-0415-11eb-2b40-7bca8ea4eef9$76d117d4-0403-11eb-05d2-c5ea47d06f43$6d5c6a84-0415-11eb-3fdf-9355200cb520$d57c6a5a-041b-11eb-3ab4-774a2d45a891$6db6c894-0415-11eb-305a-c75b119d89e9$6de37d6c-0415-11eb-1b05-85ac820016c7$73047bba-0416-11eb-1047-23e9c3dbde05$76f62d64-0403-11eb-27e2-3de58366b619$cdaade9c-0416-11eb-0550-7b5b3d33e240$c5c7cb86-041b-11eb-3360-45463105f3c9$d8abd2f6-0416-11eb-1c2a-f9157d9760a7$771c8f0c-0403-11eb-097e-ab24d0714ad5$aa6673d4-0417-11eb-32cc-79560896c195$105d347e-041c-11eb-2fc8-1d9e5eda2be0$1ca7a8c2-041a-11eb-146a-15b8cdeaea72$77428072-0403-11eb-0068-81e3728f2ebe$4b3ec86c-0419-11eb-26fd-cbbfdf19afa8$dc784864-0430-11eb-1478-d1153e017310$8a28c56e-04b4-11eb-279c-3b4dfb2a9f9b$9374e63c-0493-11eb-0952-4b97512d7cdb$823364ce-041c-11eb-2467-7ffa4f751527$1ddbaa18-0494-11eb-1fc8-250ab6ae89f1$f3f81172-041c-11eb-2b9b-e99b7b9400ed$7768a2dc-0403-11eb-39b7-fd660dc952fe$f1f89502-0494-11eb-2303-0b79d8bbd13f$06089d1e-0495-11eb-0ace-a7a7dc60e5b2$77b54c10-0403-11eb-16ad-65374d29a817$bb63f3cc-042f-11eb-04ff-a128aec3c378$bb8aeb58-042f-11eb-18b8-f995631df619$778ec25c-0403-11eb-3146-1d11c294bb1f$3f497394-0a46-11eb-3369-8189905f011c$7bb8e426-0495-11eb-3a8b-cbbab61a1631$5ef5813a-0a46-11eb-00d3-01ec142e3897$5950b37e-0a46-11eb-3480-d5520013692e$77db111e-0403-11eb-2dea-4b42ceed65d6$7335de44-042f-11eb-2873-8bceef722432$61789646-0403-11eb-0042-f3b8308f11ba$26f84600-041d-11eb-1856-b12a3e5c1dc7$271ec5f0-041d-11eb-041b-db46ec1465e0$7f4e121c-041d-11eb-0dff-cd0cbfdfd606$7f744644-041d-11eb-08a0-3719cc0adeb7$88c53208-041d-11eb-3b1e-31b57ba99f05$847d0fc2-041d-11eb-2864-79066e223b45$860790fc-0403-11eb-2f2e-355f77dcc7af$ae4ac4b4-041f-11eb-14f5-1bcde35d18f2$ae70625a-041f-11eb-3082-0753419d6d57$60a8b708-04c8-11eb-37b1-3daec644ac90$189cae1e-0424-11eb-2666-65bf297d8bdd$18d308c4-0424-11eb-176d-49feec6889cf$190deebc-0424-11eb-19fe-615997093e14$82f2580a-04c8-11eb-1eea-bdb4e50eee3b$8631a536-0403-11eb-0379-bb2e56927727$98beb336-0425-11eb-3886-4f8cfd210288$866299e8-0403-11eb-085d-2b93459cc141$9a837b52-0425-11eb-231f-a74405ff6e23$a8dd5cae-0425-11eb-119c-bfcbf832d695$8692bf42-0403-11eb-191f-b7d08895274f$6d480cf0-0425-11eb-18a9-1737455371d7$7946d83a-04a0-11eb-224b-2b315e87bc84$488771e2-049f-11eb-3b0a-0de260457731$86d98d0a-0403-11eb-215b-c58ad721a90b$223933a4-042c-11eb-10d3-852229f25a35$1a654bdc-0421-11eb-2c38-7d35060e2565$2d3bba2a-04a8-11eb-2c40-87794b6aeeac$221bc0ac-04aa-11eb-1331-2b16ebc1ee57$406aabea-04a5-11eb-06b8-312879457c42$b21475c6-04ac-11eb-1366-f3b5e967402d$9c39974c-04a5-11eb-184d-317eb542452c$619c8a10-0403-11eb-2e89-8b0974fb01d0$8a1d5aea-04ae-11eb-2177-eb37822db4f1$2ade2694-0425-11eb-2fb2-390da43d9695$955321de-0403-11eb-04ce-fb1670dfbb9e$32cea7ba-0429-11eb-06bc-a5f4ae3ffe37$46133a74-04b1-11eb-0b46-0bc74e564680$95771ce2-0403-11eb-3056-f1dc3a8b7ec3$3d3b672c-0426-11eb-0a36-153ce3c276b9$887d27fc-04bc-11eb-0ab9-eb95ef9607f8$b92f1cec-04ae-11eb-0072-3535d1118494$2c62b4ae-04b3-11eb-0080-a1035a7e31a2$28db9d98-04ca-11eb-3606-9fb89fa62f36$c5156c72-04af-11eb-1106-b13969b036ca$0a967f38-0493-11eb-0624-77e40b24d757$bf6fd176-04cc-11eb-008a-2fb6ff70a9cb$38b1aa5a-04cf-11eb-11a2-930741fc9076$80c2cd88-04b1-11eb-326e-0120a39405ea$80e6f1e0-04b1-11eb-0d4e-475f1d80c2bb$9cd2bb00-04b1-11eb-1d83-a703907141a7$95c598d4-0403-11eb-2328-0175ed564915$10cf6db8-04b8-11eb-2267-3db6d7f9c89a$843fd63c-04d0-11eb-0113-c58d346179d6$7f635722-04d0-11eb-3209-4b603c9e843c$a4c9ccdc-12ca-11eb-072f-e34595520548$dfb99ace-04cf-11eb-0739-7d694c837d59$1c6aa208-04d1-11eb-0b87-cf429e6ff6d0$95eb9f88-0403-11eb-155b-7b2d3a07cff0$287ee7aa-0435-11eb-0ca3-951dbbe69404$9611ca24-0403-11eb-3582-b7e3bb243e62$26e2978e-0435-11eb-0d61-25f552d2771e$9635c944-0403-11eb-3982-4df509f6a556$4ad11052-042c-11eb-3643-8b2b3e1269bc$61c00724-0403-11eb-228d-17c11670e5d1$8dd97820-04a5-11eb-36c0-8f92d4b859a8$99ef7b2a-0403-11eb-08ef-e1023cd151ae$9a13b17c-0403-11eb-024f-9b37e95e211b$1ac4b33a-0435-11eb-36f8-8f3f81ae7844$9a377b32-0403-11eb-2799-e7e59caa6a45$21c50840-0435-11eb-1307-7138ecde0691$da49710e-0420-11eb-092e-4f1173868738$e6219c7c-0420-11eb-3faa-13126f7c8007$531d13c2-0414-11eb-0acd-4905a684869d$4f19e872-0414-11eb-0dfd-e53d2aecc4dc$48a16c42-0414-11eb-0e0c-bf52bbb0f618$6d906d0c-0415-11eb-0c1c-b5c0aca841db$08e2bc64-0417-11eb-1457-21c0d18e8c51$9cf9080a-04b1-11eb-12a0-17013f2d37f5$461586dc-0414-11eb-00f3-4984b57bfac5$43e6e856-0414-11eb-19ca-07358aa8b667$41cefa68-0414-11eb-3bad-6530360d6f68$3f5e0af8-0414-11eb-34a7-a71e7aaf6443$3d88c056-0414-11eb-0025-05d3aff1588b$3c0528a0-0414-11eb-2f68-a5657ab9e73d$b817f466-04d4-11eb-0a26-c1c667f9f7f7$c61f35ea-04d6-11eb-2503-17a79f8d0298$7c515a7a-04d5-11eb-0f36-4fcebff709d5$c4a8694a-04d4-11eb-1eef-c9e037e6b21f$393041ec-049f-11eb-3089-2faf378445f3$759bc42e-04ab-11eb-0ab1-b12e008c02a9$1491a078-04aa-11eb-0106-19a3cf1e94b0$f8e05d94-04ac-11eb-26d4-6f1d2c5ed272$39dffa3c-0414-11eb-0197-e72b299e9c63$2b26dc42-0403-11eb-205f-cd2c23d8cb03$5689841e-0414-11eb-0492-63c77ddbd136last_hot_reload_timeshortpathhw4 some solutions.jlprocess_statusreadypathK/home/runner/work/disorganised-mess/disorganised-mess/hw4 some solutions.jlpluto_versionv0.19.47last_save_timeAϪcell_order$01341648-0403-11eb-2212-db450c299f35$03a85970-0403-11eb-334a-812b59c0905b$06f30b2a-0403-11eb-0f05-8badebe1011d$095cbf46-0403-11eb-0c37-35de9562cebc$107e65a4-0403-11eb-0c14-37d8d828b469$12cc2940-0403-11eb-19a7-bb570de58f6f$15187690-0403-11eb-2dfd-fd924faa3513$1d3356c4-0403-11eb-0f48-01b5eb14a585$2b26dc42-0403-11eb-205f-cd2c23d8cb03$df8547b4-0400-11eb-07c6-fb370b61c2b6$d8797684-0414-11eb-1869-5b1e2c469011$02b0c2fc-0415-11eb-2b40-7bca8ea4eef9$b817f466-04d4-11eb-0a26-c1c667f9f7f7$76d117d4-0403-11eb-05d2-c5ea47d06f43$6d5c6a84-0415-11eb-3fdf-9355200cb520$d57c6a5a-041b-11eb-3ab4-774a2d45a891$c61f35ea-04d6-11eb-2503-17a79f8d0298$6d906d0c-0415-11eb-0c1c-b5c0aca841db$6db6c894-0415-11eb-305a-c75b119d89e9$6de37d6c-0415-11eb-1b05-85ac820016c7$73047bba-0416-11eb-1047-23e9c3dbde05$76f62d64-0403-11eb-27e2-3de58366b619$cdaade9c-0416-11eb-0550-7b5b3d33e240$c5c7cb86-041b-11eb-3360-45463105f3c9$d8abd2f6-0416-11eb-1c2a-f9157d9760a7$771c8f0c-0403-11eb-097e-ab24d0714ad5$aa6673d4-0417-11eb-32cc-79560896c195$105d347e-041c-11eb-2fc8-1d9e5eda2be0$1ca7a8c2-041a-11eb-146a-15b8cdeaea72$08e2bc64-0417-11eb-1457-21c0d18e8c51$77428072-0403-11eb-0068-81e3728f2ebe$4b3ec86c-0419-11eb-26fd-cbbfdf19afa8$dc784864-0430-11eb-1478-d1153e017310$8a28c56e-04b4-11eb-279c-3b4dfb2a9f9b$9374e63c-0493-11eb-0952-4b97512d7cdb$823364ce-041c-11eb-2467-7ffa4f751527$1ddbaa18-0494-11eb-1fc8-250ab6ae89f1$f3f81172-041c-11eb-2b9b-e99b7b9400ed$7768a2dc-0403-11eb-39b7-fd660dc952fe$f1f89502-0494-11eb-2303-0b79d8bbd13f$06089d1e-0495-11eb-0ace-a7a7dc60e5b2$77b54c10-0403-11eb-16ad-65374d29a817$bb63f3cc-042f-11eb-04ff-a128aec3c378$bb8aeb58-042f-11eb-18b8-f995631df619$778ec25c-0403-11eb-3146-1d11c294bb1f$3f497394-0a46-11eb-3369-8189905f011c$7bb8e426-0495-11eb-3a8b-cbbab61a1631$5ef5813a-0a46-11eb-00d3-01ec142e3897$5950b37e-0a46-11eb-3480-d5520013692e$77db111e-0403-11eb-2dea-4b42ceed65d6$7335de44-042f-11eb-2873-8bceef722432$61789646-0403-11eb-0042-f3b8308f11ba$26f84600-041d-11eb-1856-b12a3e5c1dc7$271ec5f0-041d-11eb-041b-db46ec1465e0$7f4e121c-041d-11eb-0dff-cd0cbfdfd606$7f744644-041d-11eb-08a0-3719cc0adeb7$88c53208-041d-11eb-3b1e-31b57ba99f05$847d0fc2-041d-11eb-2864-79066e223b45$860790fc-0403-11eb-2f2e-355f77dcc7af$ae4ac4b4-041f-11eb-14f5-1bcde35d18f2$ae70625a-041f-11eb-3082-0753419d6d57$60a8b708-04c8-11eb-37b1-3daec644ac90$189cae1e-0424-11eb-2666-65bf297d8bdd$18d308c4-0424-11eb-176d-49feec6889cf$190deebc-0424-11eb-19fe-615997093e14$82f2580a-04c8-11eb-1eea-bdb4e50eee3b$8631a536-0403-11eb-0379-bb2e56927727$98beb336-0425-11eb-3886-4f8cfd210288$7c515a7a-04d5-11eb-0f36-4fcebff709d5$866299e8-0403-11eb-085d-2b93459cc141$9a837b52-0425-11eb-231f-a74405ff6e23$a8dd5cae-0425-11eb-119c-bfcbf832d695$c4a8694a-04d4-11eb-1eef-c9e037e6b21f$8692bf42-0403-11eb-191f-b7d08895274f$6d480cf0-0425-11eb-18a9-1737455371d7$7946d83a-04a0-11eb-224b-2b315e87bc84$488771e2-049f-11eb-3b0a-0de260457731$393041ec-049f-11eb-3089-2faf378445f3$86d98d0a-0403-11eb-215b-c58ad721a90b$223933a4-042c-11eb-10d3-852229f25a35$1a654bdc-0421-11eb-2c38-7d35060e2565$2d3bba2a-04a8-11eb-2c40-87794b6aeeac$221bc0ac-04aa-11eb-1331-2b16ebc1ee57$406aabea-04a5-11eb-06b8-312879457c42$b21475c6-04ac-11eb-1366-f3b5e967402d$9c39974c-04a5-11eb-184d-317eb542452c$759bc42e-04ab-11eb-0ab1-b12e008c02a9$1491a078-04aa-11eb-0106-19a3cf1e94b0$f8e05d94-04ac-11eb-26d4-6f1d2c5ed272$619c8a10-0403-11eb-2e89-8b0974fb01d0$8a1d5aea-04ae-11eb-2177-eb37822db4f1$2ade2694-0425-11eb-2fb2-390da43d9695$955321de-0403-11eb-04ce-fb1670dfbb9e$32cea7ba-0429-11eb-06bc-a5f4ae3ffe37$46133a74-04b1-11eb-0b46-0bc74e564680$95771ce2-0403-11eb-3056-f1dc3a8b7ec3$3d3b672c-0426-11eb-0a36-153ce3c276b9$887d27fc-04bc-11eb-0ab9-eb95ef9607f8$b92f1cec-04ae-11eb-0072-3535d1118494$2c62b4ae-04b3-11eb-0080-a1035a7e31a2$c5156c72-04af-11eb-1106-b13969b036ca$28db9d98-04ca-11eb-3606-9fb89fa62f36$0a967f38-0493-11eb-0624-77e40b24d757$bf6fd176-04cc-11eb-008a-2fb6ff70a9cb$38b1aa5a-04cf-11eb-11a2-930741fc9076$80c2cd88-04b1-11eb-326e-0120a39405ea$80e6f1e0-04b1-11eb-0d4e-475f1d80c2bb$9cd2bb00-04b1-11eb-1d83-a703907141a7$9cf9080a-04b1-11eb-12a0-17013f2d37f5$95c598d4-0403-11eb-2328-0175ed564915$10cf6db8-04b8-11eb-2267-3db6d7f9c89a$843fd63c-04d0-11eb-0113-c58d346179d6$7f635722-04d0-11eb-3209-4b603c9e843c$a4c9ccdc-12ca-11eb-072f-e34595520548$dfb99ace-04cf-11eb-0739-7d694c837d59$1c6aa208-04d1-11eb-0b87-cf429e6ff6d0$95eb9f88-0403-11eb-155b-7b2d3a07cff0$287ee7aa-0435-11eb-0ca3-951dbbe69404$9611ca24-0403-11eb-3582-b7e3bb243e62$26e2978e-0435-11eb-0d61-25f552d2771e$9635c944-0403-11eb-3982-4df509f6a556$4ad11052-042c-11eb-3643-8b2b3e1269bc$61c00724-0403-11eb-228d-17c11670e5d1$8dd97820-04a5-11eb-36c0-8f92d4b859a8$99ef7b2a-0403-11eb-08ef-e1023cd151ae$9a13b17c-0403-11eb-024f-9b37e95e211b$1ac4b33a-0435-11eb-36f8-8f3f81ae7844$9a377b32-0403-11eb-2799-e7e59caa6a45$21c50840-0435-11eb-1307-7138ecde0691$da49710e-0420-11eb-092e-4f1173868738$e6219c7c-0420-11eb-3faa-13126f7c8007$5689841e-0414-11eb-0492-63c77ddbd136$531d13c2-0414-11eb-0acd-4905a684869d$4f19e872-0414-11eb-0dfd-e53d2aecc4dc$48a16c42-0414-11eb-0e0c-bf52bbb0f618$461586dc-0414-11eb-00f3-4984b57bfac5$43e6e856-0414-11eb-19ca-07358aa8b667$41cefa68-0414-11eb-3bad-6530360d6f68$3f5e0af8-0414-11eb-34a7-a71e7aaf6443$3d88c056-0414-11eb-0025-05d3aff1588b$3c0528a0-0414-11eb-2f68-a5657ab9e73d$39dffa3c-0414-11eb-0197-e72b299e9c63published_objectsnbpkginstall_time_nsinstantiatedòinstalled_versionsterminal_outputsenabled·restart_recommended_msgrestart_required_msgbusy_packageswaiting_for_permission,waiting_for_permission_but_probably_disabled«cell_inputs$f1f89502-0494-11eb-2303-0b79d8bbd13fcell_id$f1f89502-0494-11eb-2303-0b79d8bbd13fcodeقfunction frequencies_plot_with_mean(data) # start out by copying the frequencies_plot_with_maximum function return missing endmetadatashow_logsèdisabled®skip_as_script«code_folded$95771ce2-0403-11eb-3056-f1dc3a8b7ec3cell_id$95771ce2-0403-11eb-3056-f1dc3a8b7ec3code:md""" 👉 Write a function `simulation` that does the following: 1. Generate the $N$ agents. 2. Run `sweep!` a number $T$ of times. Calculate and store the total number of agents with each status at each step in variables `S_counts`, `I_counts` and `R_counts`. 3. Return the vectors `S_counts`, `I_counts` and `R_counts` in a **named tuple**, with keys `S`, `I` and `R`. You've seen an example of named tuples before: the `student` variable at the top of the notebook! _Feel free to store the counts in a different way, as long as the return type is the same._ """metadatashow_logsèdisabled®skip_as_script«code_folded$e6219c7c-0420-11eb-3faa-13126f7c8007cell_id$e6219c7c-0420-11eb-3faa-13126f7c8007codezlines_i_edited = md""" Abstraction, lines 1-219 Array Basics, lines 1-137 Course Intro, lines 1-44 (_for example_) """metadatashow_logsèdisabled®skip_as_script«code_folded$b817f466-04d4-11eb-0a26-c1c667f9f7f7cell_id$b817f466-04d4-11eb-0a26-c1c667f9f7f7codegif !@isdefined(bernoulli) not_defined(:bernoulli) else let result = bernoulli(0.5) if result isa Missing still_missing() elseif !(result isa Bool) keep_working(md"Make sure that you return either `true` or `false`.") else if bernoulli(0.0) == false && bernoulli(1.0) == true correct() else keep_working() end end end endmetadatashow_logsèdisabled®skip_as_script«code_folded$08e2bc64-0417-11eb-1457-21c0d18e8c51cell_id$08e2bc64-0417-11eb-1457-21c0d18e8c51codehint(md""" Do you remember how we worked with dictionaries in Homework 3? You can create an empty dictionary using `Dict()`. You may want to use either the function `haskey` or the function `get` on your dictionary -- check the documentation for how to use these functions. """)metadatashow_logsèdisabled®skip_as_script«code_folded$bb8aeb58-042f-11eb-18b8-f995631df619cell_id$bb8aeb58-042f-11eb-18b8-f995631df619codexmd""" As you separately vary $p$ and $N$, what do you observe about the **mean** in each case? Does that make sense? """metadatashow_logsèdisabled®skip_as_script«code_folded$223933a4-042c-11eb-10d3-852229f25a35cell_id$223933a4-042c-11eb-10d3-852229f25a35code#abstract type AbstractInfection endmetadatashow_logsèdisabled®skip_as_script«code_folded$ae4ac4b4-041f-11eb-14f5-1bcde35d18f2cell_id$ae4ac4b4-041f-11eb-14f5-1bcde35d18f2codeFmutable struct Agent status::InfectionStatus num_infected::Int64 endmetadatashow_logsèdisabled®skip_as_script«code_folded$7f635722-04d0-11eb-3209-4b603c9e843ccell_id$7f635722-04d0-11eb-3209-4b603c9e843ccodesir_mean_plot(simulations)metadatashow_logsèdisabled®skip_as_script«code_folded$7c515a7a-04d5-11eb-0f36-4fcebff709d5cell_id$7c515a7a-04d5-11eb-0f36-4fcebff709d5codeٿif !@isdefined(set_status!) not_defined(:set_status!) else let agent = Agent(I,2) set_status!(agent, R) if agent.status == R correct() else keep_working() end end endmetadatashow_logsèdisabled®skip_as_script«code_folded$107e65a4-0403-11eb-0c14-37d8d828b469cell_id$107e65a4-0403-11eb-0c14-37d8d828b469code)md"_Let's create a package environment:_"metadatashow_logsèdisabled®skip_as_script«code_folded$1c6aa208-04d1-11eb-0b87-cf429e6ff6d0cell_id$1c6aa208-04d1-11eb-0b87-cf429e6ff6d0codemetadatashow_logsèdisabled®skip_as_script«code_folded$80e6f1e0-04b1-11eb-0d4e-475f1d80c2bbcell_id$80e6f1e0-04b1-11eb-0d4e-475f1d80c2bbcodemd""" In the cell below, we plot the evolution of the number of $I$ individuals as a function of time for each of the simulations on the same plot using transparency (`alpha=0.5` inside the plot command). """metadatashow_logsèdisabled®skip_as_script«code_folded$7f4e121c-041d-11eb-0dff-cd0cbfdfd606cell_id$7f4e121c-041d-11eb-0dff-cd0cbfdfd606codetest_status = missingmetadatashow_logsèdisabled®skip_as_script«code_folded$3d3b672c-0426-11eb-0a36-153ce3c276b9cell_id$3d3b672c-0426-11eb-0a36-153ce3c276b9codefunction simulation(N::Integer, T::Integer, infection::AbstractInfection) agents = generate_agents(N) S_counts = [] I_counts = [] R_counts = [] for _ in 1:T push!(S_counts, sum(a -> a.status == S, agents)) push!(I_counts, sum(a -> a.status == I, agents)) push!(R_counts, sum(a -> a.status == R, agents)) sweep!(agents, infection) end return (S=S_counts, I=I_counts, R=R_counts) endmetadatashow_logsèdisabled®skip_as_script«code_folded$4f19e872-0414-11eb-0dfd-e53d2aecc4dccell_id$4f19e872-0414-11eb-0dfd-e53d2aecc4dccodeImd"## Function library Just some helper functions used in the notebook."metadatashow_logsèdisabled®skip_as_script«code_folded$759bc42e-04ab-11eb-0ab1-b12e008c02a9cell_id$759bc42e-04ab-11eb-0ab1-b12e008c02a9code/if !@isdefined(interact!) not_defined(:interact!) else let agent = Agent(S, 9) source = Agent(I, 0) interact!(agent, source, InfectionRecovery(0.0, 1.0)) if source.status != I || source.num_infected != 0 keep_working(md"The `source` should not be modified if no infection occured.") elseif agent.status != S keep_working(md"The `agent` should get infected with the right probability.") else agent = Agent(S, 9) source = Agent(S, 0) interact!(agent, source, InfectionRecovery(1.0, 1.0)) if source.status != S || source.num_infected != 0 || agent.status != S keep_working(md"The `agent` should get infected with the right probability if the source is infectious.") else agent = Agent(S, 9) source = Agent(I, 3) interact!(agent, source, InfectionRecovery(1.0, 1.0)) if agent.status == R almost(md"The agent should not recover immediately after becoming infectious.") elseif agent.status == S keep_working(md"The `agent` should recover from an infectious state with the right probability.") elseif source.status != I || source.num_infected != 4 almost(md"The `source` did not get updated correctly after infecting the `agent`.") else correct(md"Your function treats the **susceptible** agent case correctly!") end end end end endmetadatashow_logsèdisabled®skip_as_script«code_folded$5689841e-0414-11eb-0492-63c77ddbd136cell_id$5689841e-0414-11eb-0492-63c77ddbd136codebigbreakmetadatashow_logsèdisabled®skip_as_script«code_folded$99ef7b2a-0403-11eb-08ef-e1023cd151aecell_id$99ef7b2a-0403-11eb-08ef-e1023cd151aecodeJmd""" 👉 Make a *new method* for the `interact!` function that accepts the new infection type as argument, reusing as much functionality as possible from the previous version. Write it in the same cell as [our previous `interact!` method](#interactfunction), and use a `begin` block to group the two definitions together. """metadatashow_logsèdisabled®skip_as_script«code_folded$77b54c10-0403-11eb-16ad-65374d29a817cell_id$77b54c10-0403-11eb-16ad-65374d29a817codemd""" 👉 Write an interactive visualization that draws the histogram and mean for $p$ between $0.01$ (not $0$!) and $1$, and $N$ between $1$ and $100,000$, say. To avoid a naming conflict, call them `p_interactive` and `N_interactive`, instead of just `p` and `N`. """metadatashow_logsèdisabled®skip_as_script«code_folded$7768a2dc-0403-11eb-39b7-fd660dc952fecell_id$7768a2dc-0403-11eb-39b7-fd660dc952fecodeٍmd""" 👉 Write the function `frequencies_plot_with_mean` that calculates the mean recovery time and displays it using a vertical line. """metadatashow_logsèdisabled®skip_as_script«code_folded$60a8b708-04c8-11eb-37b1-3daec644ac90cell_id$60a8b708-04c8-11eb-37b1-3daec644ac90codemetadatashow_logsèdisabled®skip_as_script«code_folded$95eb9f88-0403-11eb-155b-7b2d3a07cff0cell_id$95eb9f88-0403-11eb-155b-7b2d3a07cff0codeomd""" 👉 Write a function `sir_mean_error_plot` that does the same as `sir_mean_plot`, which also computes the **standard deviation** $\sigma$ of $S$, $I$, $R$ at each step. Add this to the plot using **error bars**, using the option `yerr=σ` in the plot command; use transparency. This should confirm that the distribution of $I$ at each step is pretty wide! """metadatashow_logsèdisabled®skip_as_script«code_folded$955321de-0403-11eb-04ce-fb1670dfbb9ecell_id$955321de-0403-11eb-04ce-fb1670dfbb9ecodemd""" 👉 Write a function `sweep!`. It runs `step!` $N$ times, where $N$ is the number of agents. Thus each agent acts, on average, once per sweep; a sweep is thus the unit of time in our Monte Carlo simulation. """metadatashow_logsèdisabled®skip_as_script«code_folded$ae70625a-041f-11eb-3082-0753419d6d57cell_id$ae70625a-041f-11eb-3082-0753419d6d57codeImd""" When you define a new type like this, Julia automatically defines one or more **constructors**, which are methods of a generic function with the *same name* as the type. These are used to create objects of that type. 👉 Use the `methods` function to check how many constructors are pre-defined for the `Agent` type. """metadatashow_logsèdisabled®skip_as_script«code_folded$843fd63c-04d0-11eb-0113-c58d346179d6cell_id$843fd63c-04d0-11eb-0113-c58d346179d6codeټ# function sir_mean_plot(simulations::Vector{<:NamedTuple}) # # you might need T for this function, here's a trick to get it: # T = length(first(simulations).S) # return missing # endmetadatashow_logsèdisabled®skip_as_script«code_folded$189cae1e-0424-11eb-2666-65bf297d8bddcell_id$189cae1e-0424-11eb-2666-65bf297d8bddcodeZmd""" 👉 Create an agent `test_agent` with status `S` and `num_infected` equal to 0. """metadatashow_logsèdisabled®skip_as_script«code_folded$7f744644-041d-11eb-08a0-3719cc0adeb7cell_id$7f744644-041d-11eb-08a0-3719cc0adeb7codeKmd""" 👉 Use the `typeof` function to find the type of `test_status`. """metadatashow_logsèdisabled®skip_as_script«code_folded$488771e2-049f-11eb-3b0a-0de260457731cell_id$488771e2-049f-11eb-3b0a-0de260457731codegenerate_agents(3)metadatashow_logsèdisabled®skip_as_script«code_folded$393041ec-049f-11eb-3089-2faf378445f3cell_id$393041ec-049f-11eb-3089-2faf378445f3code*if !@isdefined(generate_agents) not_defined(:generate_agents) else let result = generate_agents(4) if result isa Missing still_missing() elseif result isa Nothing keep_working("The function returned `nothing`. Did you forget to return something?") elseif !(result isa Vector) || !all(x -> x isa Agent, result) keep_working(md"Make sure that you return an array of objects of the type `Agent`.") elseif length(result) != 4 almost(md"Make sure that you return `N` agents.") elseif length(Set(result)) != 4 almost(md"You returned the **same** agent `N` times. You need to call the `Agent` constructor `N` times, not once.") else if sum(a -> a.status == I, result) != 1 almost(md"Exactly one of the agents should be infectious.") else correct() end end end endmetadatashow_logsèdisabled®skip_as_script«code_folded$531d13c2-0414-11eb-0acd-4905a684869dcell_id$531d13c2-0414-11eb-0acd-4905a684869dcode٦if student.name == "Jazzy Doe" md""" !!! danger "Before you submit" Remember to fill in your **name** and **Kerberos ID** at the top of this notebook. """ endmetadatashow_logsèdisabled®skip_as_script«code_folded$221bc0ac-04aa-11eb-1331-2b16ebc1ee57cell_id$221bc0ac-04aa-11eb-1331-2b16ebc1ee57codebegin function interact!(agent::Agent, source::Agent, infection::InfectionRecovery) if agent.status == S && source.status == I if bernoulli(infection.p_infection) agent.status = I source.num_infected += 1 end elseif agent.status == I if bernoulli(infection.p_recovery) agent.status = R end end end function interact!(agent::Agent, source::Agent, infection::Reinfection) if agent.status == S && source.status == I if bernoulli(infection.p_infection) agent.status = I source.num_infected += 1 end elseif agent.status == I if bernoulli(infection.p_recovery) agent.status = S end end end endmetadatashow_logsèdisabled®skip_as_script«code_folded$32cea7ba-0429-11eb-06bc-a5f4ae3ffe37cell_id$32cea7ba-0429-11eb-06bc-a5f4ae3ffe37codeyfunction sweep!(agents::Vector{Agent}, infection::AbstractInfection) for _ in agents step!(agents, infection) end endmetadatashow_logsèdisabled®skip_as_script«code_folded$06f30b2a-0403-11eb-0f05-8badebe1011dcell_id$06f30b2a-0403-11eb-0f05-8badebe1011dcodemd""" # **Homework 4**: _Epidemic modeling I_ `18.S191`, fall 2020 This notebook contains _built-in, live answer checks_! In some exercises you will see a coloured box, which runs a test case on your code, and provides feedback based on the result. Simply edit the code, run it, and the check runs again. _For MIT students:_ there will also be some additional (secret) test cases that will be run as part of the grading process, and we will look at your notebook and write comments. Feel free to ask questions! """metadatashow_logsèdisabled®skip_as_script«code_folded$9c39974c-04a5-11eb-184d-317eb542452ccell_id$9c39974c-04a5-11eb-184d-317eb542452ccode٢let agent = Agent(S, 0) source = Agent(I, 0) infection = InfectionRecovery(0.9, 0.5) interact!(agent, source, infection) (agent=agent, source=source) endmetadatashow_logsèdisabled®skip_as_script«code_folded$1ddbaa18-0494-11eb-1fc8-250ab6ae89f1cell_id$1ddbaa18-0494-11eb-1fc8-250ab6ae89f1code/frequencies_plot_with_maximum(large_experiment)metadatashow_logsèdisabled®skip_as_script«code_folded$06089d1e-0495-11eb-0ace-a7a7dc60e5b2cell_id$06089d1e-0495-11eb-0ace-a7a7dc60e5b2code,frequencies_plot_with_mean(large_experiment)metadatashow_logsèdisabled®skip_as_script«code_folded$15187690-0403-11eb-2dfd-fd924faa3513cell_id$15187690-0403-11eb-2dfd-fd924faa3513codePbegin Pkg.add(["Plots", "PlutoUI",]) using Plots plotly() using PlutoUI endmetadatashow_logsèdisabled®skip_as_script«code_folded$d8abd2f6-0416-11eb-1c2a-f9157d9760a7cell_id$d8abd2f6-0416-11eb-1c2a-f9157d9760a7code)small_experiment = do_experiment(0.5, 20)metadatashow_logsèdisabled®skip_as_script«code_folded$9374e63c-0493-11eb-0952-4b97512d7cdbcell_id$9374e63c-0493-11eb-0952-4b97512d7cdbcode*md""" Great! Feel free to experiment with this function, try giving it a different array as argument. Plots.jl is pretty clever, it even works with an array of strings! #### Exercise 1.4 Next, we want to **add a new element** to our plot: a vertical line. To demonstrate how this works, here we added a vertical line at the _maximum value_. To write this function, we first create a **base plot**, we then **modify** that plot to add the vertical line, and finally, we **return** the plot. More on this in [the next info box](#note_about_plotting). """metadatashow_logsèdisabled®skip_as_script«code_folded$2d3bba2a-04a8-11eb-2c40-87794b6aeeaccell_id$2d3bba2a-04a8-11eb-2c40-87794b6aeeaccodemd""" #### Exercise 2.5 👉 Write a function `interact!` that takes an affected `agent` of type `Agent`, an `source` of type `Agent` and an `infection` of type `InfectionRecovery`. It implements a single (one-sided) interaction between two agents: - If the `agent` is susceptible and the `source` is infectious, then the `source` infects our `agent` with the given infection probability. If the `source` successfully infects the other agent, then its `num_infected` record must be updated. - If the `agent` is infected then it recovers with the relevant probability. - Otherwise, nothing happens. $(html"") """metadatashow_logsèdisabled®skip_as_script«code_folded$10cf6db8-04b8-11eb-2267-3db6d7f9c89acell_id$10cf6db8-04b8-11eb-2267-3db6d7f9c89acodefunction sir_mean_plot(simulations::Vector{<:NamedTuple}) T = length(first(simulations).S) all_S_counts = map(result -> result.S, simulations) all_I_counts = map(result -> result.I, simulations) all_R_counts = map(result -> result.R, simulations) p = plot() plot!(p, 1:T, sum(all_S_counts) ./ length(simulations), lw=5, label="S") plot!(p, 1:T, sum(all_I_counts) ./ length(simulations), lw=5, label="I") plot!(p, 1:T, sum(all_R_counts) ./ length(simulations), lw=5, label="R") p endmetadatashow_logsèdisabled®skip_as_script«code_folded$dfb99ace-04cf-11eb-0739-7d694c837d59cell_id$dfb99ace-04cf-11eb-0739-7d694c837d59code٣md""" 👉 Allow $p_\text{infection}$ and $p_\text{recovery}$ to be changed interactively and find parameter values for which you observe an epidemic outbreak. """metadatashow_logsèdisabled®skip_as_script«code_folded$271ec5f0-041d-11eb-041b-db46ec1465e0cell_id$271ec5f0-041d-11eb-041b-db46ec1465e0codemd""" We have just defined a new type `InfectionStatus`, as well as names `S`, `I` and `R` that are the (only) possible values that a variable of this type can take. 👉 Define a variable `test_status` whose value is `S`. """metadatashow_logsèdisabled®skip_as_script«code_folded$7946d83a-04a0-11eb-224b-2b315e87bc84cell_id$7946d83a-04a0-11eb-224b-2b315e87bc84code@# function generate_agents(N::Integer) # return missing # endmetadatashow_logsèdisabled®skip_as_script«code_folded$02b0c2fc-0415-11eb-2b40-7bca8ea4eef9cell_id$02b0c2fc-0415-11eb-2b40-7bca8ea4eef9code9# function bernoulli(p::Number) # return missing # endmetadatashow_logsèdisabled®skip_as_script«code_folded$9635c944-0403-11eb-3982-4df509f6a556cell_id$9635c944-0403-11eb-3982-4df509f6a556codemd""" #### Exercse 3.4 👉 What are three *simple* ways in which you could characterise the magnitude (size) of the epidemic outbreak? Find approximate values of these quantities for one of the runs of your simulation. """metadatashow_logsèdisabled®skip_as_script«code_folded$f3f81172-041c-11eb-2b9b-e99b7b9400edcell_id$f3f81172-041c-11eb-2b9b-e99b7b9400edcode+md""" $(html"") > ### Note about plotting > > Plots.jl has an interesting property: a plot is an object, not an action. Functions like `plot`, `bar`, `histogram` don't draw anything on your screen - they just return a `Plots.Plot`. This is a struct that contains the _description_ of a plot (what data should be plotted in what way?), not the _picture_. > > So a Pluto cell with a single line, `plot(1:10)`, will show a plot, because the _result_ of the function `plot` is a `Plot` object, and Pluto just shows the result of a cell. > > ##### Modifying plots > Nice plots are often formed by overlaying multiple plots. In Plots.jl, this is done using the **modifying functions**: `plot!`, `bar!`, `vline!`, etc. These take an extra (first) argument: a previous plot to modify. > > For example, to plot the `sin`, `cos` and `tan` functions in the same view, we do: > ```julia > function sin_cos_plot() > T = -1.0:0.01:1.0 > > result = plot(T, sin.(T)) > plot!(result, T, cos.(T)) > plot!(result, T, tan.(T)) > > return result > end > ``` > > 💡 This example demonstrates a useful pattern to combine plots: > 1. Create a **new** plot and store it in a variable > 2. **Modify** that plot to add more elements > 3. Return the plot > > It is recommended that these 3 steps happen **within a single cell**. This can prevent some strange glitches when re-running cells. There are three ways to group expressions together into a single cell: `begin`, `let` and `function`. More on this [later](#function_begin_let)! """metadatashow_logsèdisabled®skip_as_script«code_folded$8a1d5aea-04ae-11eb-2177-eb37822db4f1cell_id$8a1d5aea-04ae-11eb-2177-eb37822db4f1codeyfunction step!(agents::Vector{Agent}, infection::AbstractInfection) interact!(rand(agents), rand(agents), infection) endmetadatashow_logsèdisabled®skip_as_script«code_folded$4ad11052-042c-11eb-3643-8b2b3e1269bccell_id$4ad11052-042c-11eb-3643-8b2b3e1269bccodemetadatashow_logsèdisabled®skip_as_script«code_folded$76d117d4-0403-11eb-05d2-c5ea47d06f43cell_id$76d117d4-0403-11eb-05d2-c5ea47d06f43codejmd""" 👉 Write a function `recovery_time(p)` that returns the time taken until the person recovers. """metadatashow_logsèdisabled®skip_as_script«code_folded$095cbf46-0403-11eb-0c37-35de9562cebccell_id$095cbf46-0403-11eb-0c37-35de9562cebccode# edit the code below to set your name and kerberos ID (i.e. email without @mit.edu) student = (name = "Jazzy Doe", kerberos_id = "jazz") # you might need to wait until all other cells in this notebook have completed running. # scroll around the page to see what's upmetadatashow_logsèdisabled®skip_as_script«code_folded$6d5c6a84-0415-11eb-3fdf-9355200cb520cell_id$6d5c6a84-0415-11eb-3fdf-9355200cb520codefunction recovery_time(p) if p ≤ 0 throw(ArgumentError("p must be positive: p = 0 cannot result in a recovery")) end recovered = bernoulli(p) if recovered 1 else 1 + recovery_time(p) end endmetadatashow_logsèdisabled®skip_as_script«code_folded$26e2978e-0435-11eb-0d61-25f552d2771ecell_id$26e2978e-0435-11eb-0d61-25f552d2771ecodemetadatashow_logsèdisabled®skip_as_script«code_folded$6de37d6c-0415-11eb-1b05-85ac820016c7cell_id$6de37d6c-0415-11eb-1b05-85ac820016c7code'md""" 👉 What happens for $p=1$? """metadatashow_logsèdisabled®skip_as_script«code_folded$18d308c4-0424-11eb-176d-49feec6889cfcell_id$18d308c4-0424-11eb-176d-49feec6889cfcodetest_agent = missingmetadatashow_logsèdisabled®skip_as_script«code_folded$46133a74-04b1-11eb-0b46-0bc74e564680cell_id$46133a74-04b1-11eb-0b46-0bc74e564680codeN# function sweep!(agents::Vector{Agent}, infection::AbstractInfection) # endmetadatashow_logsèdisabled®skip_as_script«code_folded$771c8f0c-0403-11eb-097e-ab24d0714ad5cell_id$771c8f0c-0403-11eb-097e-ab24d0714ad5code7md""" #### Exercise 1.3 👉 Write a function `frequencies(data)` that calculates and returns the frequencies (i.e. probability distribution) of input data. The input will be an array of integers, **with duplicates**, and the result will be a dictionary that maps each occured value to its frequency in the data. For example, ```julia frequencies([7, 8, 9, 7]) ``` should give ```julia Dict( 7 => 0.5, 8 => 0.25, 9 => 0.25 ) ``` As with any probability distribution, it should be normalised to $1$, in the sense that the *total* probability should be $1$. """metadatashow_logsèdisabled®skip_as_script«code_folded$3f497394-0a46-11eb-3369-8189905f011ccell_id$3f497394-0a46-11eb-3369-8189905f011ccodemetadatashow_logsèdisabled®skip_as_script«code_folded$80c2cd88-04b1-11eb-326e-0120a39405eacell_id$80c2cd88-04b1-11eb-326e-0120a39405eacodeOsimulations = repeat_simulations(100, 1000, InfectionRecovery(0.02, 0.002), 20)metadatashow_logsèdisabled®skip_as_script«code_folded$105d347e-041c-11eb-2fc8-1d9e5eda2be0cell_id$105d347e-041c-11eb-2fc8-1d9e5eda2be0code8# function frequencies(values) # return missing # endmetadatashow_logsèdisabled®skip_as_script«code_folded$2c62b4ae-04b3-11eb-0080-a1035a7e31a2cell_id$2c62b4ae-04b3-11eb-0080-a1035a7e31a2code4simulation(100, 1000, InfectionRecovery(0.005, 0.2))metadatashow_logsèdisabled®skip_as_script«code_folded$6db6c894-0415-11eb-305a-c75b119d89e9cell_id$6db6c894-0415-11eb-305a-c75b119d89e9codeDmd""" We should always be aware of special cases (sometimes called "boundary conditions"). Make sure *not* to run the code with $p=0$! What would happen in that case? Your code should check for this and throw an `ArgumentError` as follows: ```julia throw(ArgumentError("...")) ``` with a suitable error message. """metadatashow_logsèdisabled®skip_as_script«code_folded$77db111e-0403-11eb-2dea-4b42ceed65d6cell_id$77db111e-0403-11eb-2dea-4b42ceed65d6codeپmd""" #### Exercise 1.6 👉 Use $N = 10,000$ to calculate the mean time $\langle \tau(p) \rangle$ to recover as a function of $p$ between $0.001$ and $1$ (say). Plot this relationship. """metadatashow_logsèdisabled®skip_as_script«code_folded$6d480cf0-0425-11eb-18a9-1737455371d7cell_id$6d480cf0-0425-11eb-18a9-1737455371d7codenfunction generate_agents(N::Integer) agents = [Agent(S, 0) for _ in 1:N] rand(agents).status = I agents endmetadatashow_logsèdisabled®skip_as_script«code_folded$287ee7aa-0435-11eb-0ca3-951dbbe69404cell_id$287ee7aa-0435-11eb-0ca3-951dbbe69404codeٸfunction sir_mean_error_plot(simulations::Vector{<:NamedTuple}) # you might need T for this function, here's a trick to get it: T = length(first(simulations).S) return missing endmetadatashow_logsèdisabled®skip_as_script«code_folded$3d88c056-0414-11eb-0025-05d3aff1588bcell_id$3d88c056-0414-11eb-0025-05d3aff1588bcodeYcorrect(text=rand(yays)) = Markdown.MD(Markdown.Admonition("correct", "Got it!", [text]))metadatashow_logsèdisabled®skip_as_script«code_folded$03a85970-0403-11eb-334a-812b59c0905bcell_id$03a85970-0403-11eb-334a-812b59c0905bcodePmd""" Submission by: **_$(student.name)_** ($(student.kerberos_id)@mit.edu) """metadatashow_logsèdisabled®skip_as_script«code_folded$6d906d0c-0415-11eb-0c1c-b5c0aca841dbcell_id$6d906d0c-0415-11eb-0c1c-b5c0aca841dbcode{hint(md"Remember to always re-use work you have done previously: in this case you should re-use the function `bernoulli`.")metadatashow_logsèdisabled®skip_as_script«code_folded$866299e8-0403-11eb-085d-2b93459cc141cell_id$866299e8-0403-11eb-085d-2b93459cc141codeىmd""" 👉 We will also need functions `is_susceptible` and `is_infected` that check if a given agent is in those respective states. """metadatashow_logsèdisabled®skip_as_script«code_folded$461586dc-0414-11eb-00f3-4984b57bfac5cell_id$461586dc-0414-11eb-00f3-4984b57bfac5codeSalmost(text) = Markdown.MD(Markdown.Admonition("warning", "Almost there!", [text]))metadatashow_logsèdisabled®skip_as_script«code_folded$c5156c72-04af-11eb-1106-b13969b036cacell_id$c5156c72-04af-11eb-1106-b13969b036cacodelet run_basic_sir N = 100 T = 1000 sim = simulation(N, T, InfectionRecovery(0.02, 0.002)) result = plot(1:T, sim.S, ylim=(0, N), label="Susceptible") plot!(result, 1:T, sim.I, ylim=(0, N), label="Infectious") plot!(result, 1:T, sim.R, ylim=(0, N), label="Recovered") endmetadatashow_logsèdisabled®skip_as_script«code_folded$c4a8694a-04d4-11eb-1eef-c9e037e6b21fcell_id$c4a8694a-04d4-11eb-1eef-c9e037e6b21fcodeif !@isdefined(is_susceptible) not_defined(:is_susceptible) else let result1 = is_susceptible(Agent(I,2)) result2 = is_infected(Agent(I,2)) if result1 isa Missing || result2 isa Missing still_missing() elseif !(result1 isa Bool) || !(result2 isa Bool) keep_working(md"Make sure that you return either `true` or `false`.") elseif result1 === false && result2 === true if is_susceptible(Agent(S,3)) && !is_infected(Agent(R,9)) correct() else keep_working() end else keep_working() end end endmetadatashow_logsèdisabled®skip_as_script«code_folded$1491a078-04aa-11eb-0106-19a3cf1e94b0cell_id$1491a078-04aa-11eb-0106-19a3cf1e94b0codeif !@isdefined(interact!) not_defined(:interact!) else let agent = Agent(I, 9) source = Agent(S, 0) interact!(agent, source, InfectionRecovery(1.0, 1.0)) if source.status != S || source.num_infected != 0 keep_working("The `source` should not be modified if `agent` is infectious.") elseif agent.status != R keep_working("The `agent` should recover from an infectious state with the right probability.") elseif agent.num_infected != 9 keep_working(md"`agent.num_infected` should not be modified if `agent` is infectious.") else let agent = Agent(I, 9) source = Agent(R, 0) interact!(agent, source, InfectionRecovery(1.0, 0.0)) if agent.status == R keep_working("The `agent` should recover from an infectious state with the right probability.") else correct(md"Your function treats the **infectious** agent case correctly!") end end end end endmetadatashow_logsèdisabled®skip_as_script«code_folded$c5c7cb86-041b-11eb-3360-45463105f3c9cell_id$c5c7cb86-041b-11eb-3360-45463105f3c9code8# function do_experiment(p, N) # return missing # endmetadatashow_logsèdisabled®skip_as_script«code_folded$01341648-0403-11eb-2212-db450c299f35cell_id$01341648-0403-11eb-2212-db450c299f35codemd"_homework 4, version 0_"metadatashow_logsèdisabled®skip_as_script«code_folded$d8797684-0414-11eb-1869-5b1e2c469011cell_id$d8797684-0414-11eb-1869-5b1e2c469011code-function bernoulli(p::Number) rand() < p endmetadatashow_logsèdisabled®skip_as_script«code_folded$48a16c42-0414-11eb-0e0c-bf52bbb0f618cell_id$48a16c42-0414-11eb-0e0c-bf52bbb0f618codeEhint(text) = Markdown.MD(Markdown.Admonition("hint", "Hint", [text]))metadatashow_logsèdisabled®skip_as_script«code_folded$98beb336-0425-11eb-3886-4f8cfd210288cell_id$98beb336-0425-11eb-3886-4f8cfd210288codeEfunction set_status!(agent::Agent, new_status::InfectionStatus) endmetadatashow_logsèdisabled®skip_as_script«code_folded$bb63f3cc-042f-11eb-04ff-a128aec3c378cell_id$bb63f3cc-042f-11eb-04ff-a128aec3c378codemetadatashow_logsèdisabled®skip_as_script«code_folded$61c00724-0403-11eb-228d-17c11670e5d1cell_id$61c00724-0403-11eb-228d-17c11670e5d1codemd""" ## **Exercise 4:** _Reinfection_ In this exercise we will *re-use* our simulation infrastructure to study the dynamics of a different type of infection: there is no immunity, and hence no "recovery" rather, susceptible individuals may now be **re-infected** #### Exercise 4.1 👉 Make a new infection type `Reinfection`. This has the *same* two fields as `InfectionRecovery` (`p_infection` and `p_recovery`). However, "recovery" now means "becomes susceptible again", instead of "moves to the `R` class. This new type `Reinfection` should also be a **subtype** of `AbstractInfection`. This allows us to reuse our previous functions, which are defined for the abstract supertype. """metadatashow_logsèdisabled®skip_as_script«code_folded$9cf9080a-04b1-11eb-12a0-17013f2d37f5cell_id$9cf9080a-04b1-11eb-12a0-17013f2d37f5codemd""" 👉 Calculate the **mean number of infectious agents** of our simulations for each time step. Add it to the plot using a heavier line (`lw=3` for "linewidth") by modifying the cell above. Check the answer yourself: does your curve follow the average trend? $(hint(md"This exercise requires some creative juggling with arrays, anonymous functions, `map`s, or whatever you see fit!")) """metadatashow_logsèdisabled®skip_as_script«code_folded$4b3ec86c-0419-11eb-26fd-cbbfdf19afa8cell_id$4b3ec86c-0419-11eb-26fd-cbbfdf19afa8code[large_experiment = do_experiment(0.25, 10_000) # (10_000 is just 10000 but easier to read)metadatashow_logsèdisabled®skip_as_script«code_folded$61789646-0403-11eb-0042-f3b8308f11bacell_id$61789646-0403-11eb-0042-f3b8308f11bacode.md""" ## **Exercise 2:** _Agent-based model for an epidemic outbreak -- types_ In this and the following exercises we will develop a simple stochastic model for combined infection and recovery in a population, which may exhibit an **epidemic outbreak** (i.e. a large spike in the number of infectious people). The population is **well mixed**, i.e. everyone is in contact with everyone else. [An example of this would be a small school or university in which people are constantly moving around and interacting with each other.] The model is an **individual-based** or **agent-based** model: we explicitly keep track of each individual, or **agent**, in the population and their infection status. For the moment we will not keep track of their position in space; we will just assume that there is some mechanism, not included in the model, by which they interact with other individuals. #### Exercise 2.1 Each agent will have its own **internal state**, modelling its infection status, namely "susceptible", "infectious" or "recovered". We would like to code these as values `S`, `I` and `R`, respectively. One way to do this is using an [**enumerated type**](https://en.wikipedia.org/wiki/Enumerated_type) or **enum**. Variables of this type can take only a pre-defined set of values; the Julia syntax is as follows: """metadatashow_logsèdisabled®skip_as_script«code_folded$39dffa3c-0414-11eb-0197-e72b299e9c63cell_id$39dffa3c-0414-11eb-0197-e72b299e9c63code&bigbreak = html"




";metadatashow_logsèdisabled®skip_as_script«code_folded$5950b37e-0a46-11eb-3480-d5520013692ecell_id$5950b37e-0a46-11eb-3480-d5520013692ecodemetadatashow_logsèdisabled®skip_as_script«code_folded$cdaade9c-0416-11eb-0550-7b5b3d33e240cell_id$cdaade9c-0416-11eb-0550-7b5b3d33e240codeGfunction do_experiment(p, N) map(1:N) do _ recovery_time(p) end endmetadatashow_logsèdisabled®skip_as_script«code_folded$88c53208-041d-11eb-3b1e-31b57ba99f05cell_id$88c53208-041d-11eb-3b1e-31b57ba99f05codemetadatashow_logsèdisabled®skip_as_script«code_folded$f8e05d94-04ac-11eb-26d4-6f1d2c5ed272cell_id$f8e05d94-04ac-11eb-26d4-6f1d2c5ed272codeif !@isdefined(interact!) not_defined(:interact!) else let agent = Agent(R, 9) source = Agent(I, 0) interact!(agent, source, InfectionRecovery(1.0, 1.0)) if source.status != I || source.num_infected != 0 keep_working(md"The `source` should not be modified if no infection occured.") elseif agent.status != R || agent.num_infected != 9 keep_working(md"The `agent` should not be momdified if it is in a recoved state.") else correct(md"Your function treats the **recovered** agent case correctly!") end end endmetadatashow_logsèdisabled®skip_as_script«code_folded$887d27fc-04bc-11eb-0ab9-eb95ef9607f8cell_id$887d27fc-04bc-11eb-0ab9-eb95ef9607f8code~# function simulation(N::Integer, T::Integer, infection::AbstractInfection) # return (S=missing, I=missing, R=missing) # endmetadatashow_logsèdisabled®skip_as_script«code_folded$d57c6a5a-041b-11eb-3ab4-774a2d45a891cell_id$d57c6a5a-041b-11eb-3ab4-774a2d45a891code# function recovery_time(p) # if p ≤ 0 # throw(ArgumentError("p must be positive: p = 0 cannot result in a recovery")) # end # # Your code here. See the comment below about the p ≤ 0 case. # return missing # endmetadatashow_logsèdisabled®skip_as_script«code_folded$12cc2940-0403-11eb-19a7-bb570de58f6fcell_id$12cc2940-0403-11eb-19a7-bb570de58f6fcode/begin using Pkg Pkg.activate(mktempdir()) endmetadatashow_logsèdisabled®skip_as_script«code_folded$76f62d64-0403-11eb-27e2-3de58366b619cell_id$76f62d64-0403-11eb-27e2-3de58366b619code٠md""" #### Exercise 1.2 👉 Write a function `do_experiment(p, N)` that runs the function `recovery_time` `N` times and collects the results into a vector. """metadatashow_logsèdisabled®skip_as_script«code_folded$860790fc-0403-11eb-2f2e-355f77dcc7afcell_id$860790fc-0403-11eb-2f2e-355f77dcc7afcodemd""" #### Exercise 2.2 For each agent we want to keep track of its infection status and the number of *other* agents that it infects during the simulation. A good solution for this is to define a *new type* `Agent` to hold all of the information for one agent, as follows: """metadatashow_logsèdisabled®skip_as_script«code_folded$b21475c6-04ac-11eb-1366-f3b5e967402dcell_id$b21475c6-04ac-11eb-1366-f3b5e967402dcodemd""" Play around with the test case below to test your function! Try changing the definitions of `agent`, `source` and `infection`. Since we are working with randomness, you might want to run the cell multiple times. """metadatashow_logsèdisabled®skip_as_script«code_folded$823364ce-041c-11eb-2467-7ffa4f751527cell_id$823364ce-041c-11eb-2467-7ffa4f751527codeٖfunction frequencies_plot_with_maximum(data::Vector) base = bar(frequencies(data)) vline!(base, [maximum(data)], label="maximum") return base endmetadatashow_logsèdisabled®skip_as_script«code_folded$619c8a10-0403-11eb-2e89-8b0974fb01d0cell_id$619c8a10-0403-11eb-2e89-8b0974fb01d0codemd""" ## **Exercise 3:** _Agent-based model for an epidemic outbreak -- Monte Carlo simulation_ In this exercise we will build on Exercise 2 to write a Monte Carlo simulation of how an infection propagates in a population. Make sure to re-use the functions that we have already written, and introduce new ones if they are helpful! Short functions make it easier to understand what the function does and build up new functionality piece by piece. You should not use any global variables inside the functions: Each function must accept as arguments all the information it requires to carry out its task. You need to think carefully about what the information each function requires. #### Exercise 3.1 👉 Write a function `step!` that takes a vector of `Agent`s and an `infection` of type `InfectionRecovery`. It implements a single step of the infection dynamics as follows: - Choose two random agents: an `agent` and a `source`. - Apply `interact!(agent, source, infection)`. - Return `agents`. """metadatashow_logsèdisabled®skip_as_script«code_folded$38b1aa5a-04cf-11eb-11a2-930741fc9076cell_id$38b1aa5a-04cf-11eb-11a2-930741fc9076codeٖfunction repeat_simulations(N, T, infection, num_simulations) N = 100 T = 1000 map(1:num_simulations) do _ simulation(N, T, infection) end endmetadatashow_logsèdisabled®skip_as_script«code_folded$8dd97820-04a5-11eb-36c0-8f92d4b859a8cell_id$8dd97820-04a5-11eb-36c0-8f92d4b859a8codemetadatashow_logsèdisabled®skip_as_script«code_folded$df8547b4-0400-11eb-07c6-fb370b61c2b6cell_id$df8547b4-0400-11eb-07c6-fb370b61c2b6codemd""" ## **Exercise 1:** _Modelling recovery_ In this exercise we will investigate a simple stochastic (probabilistic) model of recovery from an infection and the time $\tau$ needed to recover. Although this model can be easily studied analytically using probability theory, we will instead use computational methods. (If you know about this distribution already, try to ignore what you know about it!) In this model, an individual who is infected has a constant probability $p$ to recover each day. If they recover on day $n$ then $\tau$ takes the value $n$. Each time we run a new experiment $\tau$ will take on different values, so $\tau$ is a (discrete) random variable. We thus need to study statistical properties of $\tau$, such as its mean and its probability distribution. #### Exercise 1.1 - _Probability distributions_ 👉 Define the function `bernoulli(p)`, which returns `true` with probability $p$ and `false` with probability $(1 - p)$. """metadatashow_logsèdisabled®skip_as_script«code_folded$1d3356c4-0403-11eb-0f48-01b5eb14a585cell_id$1d3356c4-0403-11eb-0f48-01b5eb14a585codehtml""" """metadatashow_logsèdisabled®skip_as_script«code_folded$82f2580a-04c8-11eb-1eea-bdb4e50eee3bcell_id$82f2580a-04c8-11eb-1eea-bdb4e50eee3bcodeAgent()metadatashow_logsèdisabled®skip_as_script«code_folded$86d98d0a-0403-11eb-215b-c58ad721a90bcell_id$86d98d0a-0403-11eb-215b-c58ad721a90bcode,md""" We will also need types representing different infections. Let's define an (immutable) `struct` called `InfectionRecovery` with parameters `p_infection` and `p_recovery`. We will make it a subtype of an abstract `AbstractInfection` type, because we will define more infection types later. """metadatashow_logsèdisabled®skip_as_script«code_folded$dc784864-0430-11eb-1478-d1153e017310cell_id$dc784864-0430-11eb-1478-d1153e017310codemd""" The frequencies dictionary is difficult to interpret on its own, so instead, we will **plot** it, i.e. plot $P(\tau = n)$ against $n$, where $n$ is the recovery time. Plots.jl comes with a function `bar`, which does exactly what we want: """metadatashow_logsèdisabled®skip_as_script«code_folded$3f5e0af8-0414-11eb-34a7-a71e7aaf6443cell_id$3f5e0af8-0414-11eb-34a7-a71e7aaf6443codeyays = [md"Fantastic!", md"Splendid!", md"Great!", md"Yay ❤", md"Great! 🎉", md"Well done!", md"Keep it up!", md"Good job!", md"Awesome!", md"You got the right answer!", md"Let's move on to the next section."]metadatashow_logsèdisabled®skip_as_script«code_folded$3c0528a0-0414-11eb-2f68-a5657ab9e73dcell_id$3c0528a0-0414-11eb-2f68-a5657ab9e73dcodeٱnot_defined(variable_name) = Markdown.MD(Markdown.Admonition("danger", "Oopsie!", [md"Make sure that you define a variable called **$(Markdown.Code(string(variable_name)))**"]))metadatashow_logsèdisabled®skip_as_script«code_folded$7335de44-042f-11eb-2873-8bceef722432cell_id$7335de44-042f-11eb-2873-8bceef722432codemetadatashow_logsèdisabled®skip_as_script«code_folded$406aabea-04a5-11eb-06b8-312879457c42cell_id$406aabea-04a5-11eb-06b8-312879457c42codeW# function interact!(agent::Agent, source::Agent, infection::InfectionRecovery) # endmetadatashow_logsèdisabled®skip_as_script«code_folded$c61f35ea-04d6-11eb-2503-17a79f8d0298cell_id$c61f35ea-04d6-11eb-2503-17a79f8d0298code4if !@isdefined(recovery_time) not_defined(:recovery_time) else let result = recovery_time(1.0) if result isa Missing still_missing() elseif !(result isa Integer) keep_working(md"Make sure that you return an integer: the recovery time.") else if result == 1 samples = [recovery_time(0.2) for _ in 1:256] a, b = extrema(samples) if a == 1 && b > 20 correct() else keep_working() end else keep_working(md"`p = 1.0` should return `1`: the agent recovers after the first time step.") end end end endmetadatashow_logsèdisabled®skip_as_script«code_folded$9cd2bb00-04b1-11eb-1d83-a703907141a7cell_id$9cd2bb00-04b1-11eb-1d83-a703907141a7codejlet p = plot() for sim in simulations plot!(p, 1:1000, sim.I, alpha=.5, label=nothing) end p endmetadatashow_logsèdisabled®skip_as_script«code_folded$8a28c56e-04b4-11eb-279c-3b4dfb2a9f9bcell_id$8a28c56e-04b4-11eb-279c-3b4dfb2a9f9bcode"bar(frequencies(large_experiment))metadatashow_logsèdisabled®skip_as_script«code_folded$8631a536-0403-11eb-0379-bb2e56927727cell_id$8631a536-0403-11eb-0379-bb2e56927727codemd""" #### Exercise 2.3 👉 Write functions `set_status!(a)` and `set_num_infected!(a)` which modify the respective fields of an `Agent`. Check that they work. [Note the bang ("`!`") at the end of the function names to signify that these functions *modify* their argument.] """metadatashow_logsèdisabled®skip_as_script«code_folded$9a13b17c-0403-11eb-024f-9b37e95e211bcell_id$9a13b17c-0403-11eb-024f-9b37e95e211bcodemd""" #### Exercise 4.2 👉 Run the simulation 20 times and plot $I$ as a function of time for each one, together with the mean over the 20 simulations (as you did in the previous exercises). Note that you should be able to re-use the `sweep!` and `simulation` functions , since those should be sufficiently **generic** to work with the new `step!` function! (Modify them if they are not.) """metadatashow_logsèdisabled®skip_as_script«code_folded$b92f1cec-04ae-11eb-0072-3535d1118494cell_id$b92f1cec-04ae-11eb-0072-3535d1118494code.simulation(3, 20, InfectionRecovery(0.9, 0.2))metadatashow_logsèdisabled®skip_as_script«code_folded$5ef5813a-0a46-11eb-00d3-01ec142e3897cell_id$5ef5813a-0a46-11eb-00d3-01ec142e3897codemetadatashow_logsèdisabled®skip_as_script«code_folded$9a377b32-0403-11eb-2799-e7e59caa6a45cell_id$9a377b32-0403-11eb-2799-e7e59caa6a45code٭md""" 👉 Run the new simulation and draw $I$ (averaged over runs) as a function of time. Is the behaviour qualitatively the same or different? Describe what you see. """metadatashow_logsèdisabled®skip_as_script«code_folded$778ec25c-0403-11eb-3146-1d11c294bb1fcell_id$778ec25c-0403-11eb-3146-1d11c294bb1fcodeٔmd""" #### Exercise 1.5 👉 What shape does the distribution seem to have? Can you verify that by adding a second plot with the expected shape? """metadatashow_logsèdisabled®skip_as_script«code_folded$9611ca24-0403-11eb-3582-b7e3bb243e62cell_id$9611ca24-0403-11eb-3582-b7e3bb243e62codemd""" #### Exercise 3.3 👉 Plot the probability distribution of `num_infected`. Does it have a recognisable shape? (Feel free to increase the number of agents in order to get better statistics.) """metadatashow_logsèdisabled®skip_as_script«code_folded$26f84600-041d-11eb-1856-b12a3e5c1dc7cell_id$26f84600-041d-11eb-1856-b12a3e5c1dc7code@enum InfectionStatus S I Rmetadatashow_logsèdisabled®skip_as_script«code_folded$1ca7a8c2-041a-11eb-146a-15b8cdeaea72cell_id$1ca7a8c2-041a-11eb-146a-15b8cdeaea72codefrequencies(small_experiment)metadatashow_logsèdisabled®skip_as_script«code_folded$190deebc-0424-11eb-19fe-615997093e14cell_id$190deebc-0424-11eb-19fe-615997093e14code?md""" 👉 For convenience, define a new constructor (i.e. a new method for the function) that takes no arguments and creates an `Agent` with status `S` and number infected 0, by calling one of the default constructors that Julia creates. This new method lives *outside* (not inside) the definition of the `struct`. (It is called an **outer constructor**.) (In Pluto, multiple methods for the same function need to be combined in a single cell using a `begin end` block.) Let's check that the new method works correctly. How many methods does the constructor have now? """metadatashow_logsèdisabled®skip_as_script«code_folded$1a654bdc-0421-11eb-2c38-7d35060e2565cell_id$1a654bdc-0421-11eb-2c38-7d35060e2565codeJstruct InfectionRecovery <: AbstractInfection p_infection p_recovery endmetadatashow_logsèdisabled®skip_as_script«code_folded$41cefa68-0414-11eb-3bad-6530360d6f68cell_id$41cefa68-0414-11eb-3bad-6530360d6f68codeفkeep_working(text=md"The answer is not quite right.") = Markdown.MD(Markdown.Admonition("danger", "Keep working on it!", [text]))metadatashow_logsèdisabled®skip_as_script«code_folded$847d0fc2-041d-11eb-2864-79066e223b45cell_id$847d0fc2-041d-11eb-2864-79066e223b45codeـmd""" 👉 Convert `x` to an integer using the `Integer` function. What value does it have? What values do `I` and `R` have? """metadatashow_logsèdisabled®skip_as_script«code_folded$77428072-0403-11eb-0068-81e3728f2ebecell_id$77428072-0403-11eb-0068-81e3728f2ebecode?md""" Let's run an experiment with $p=0.25$ and $N=10,000$. """metadatashow_logsèdisabled®skip_as_script«code_folded$9a837b52-0425-11eb-231f-a74405ff6e23cell_id$9a837b52-0425-11eb-231f-a74405ff6e23code;function is_susceptible(agent::Agent) return missing endmetadatashow_logsèdisabled®skip_as_script«code_folded$0a967f38-0493-11eb-0624-77e40b24d757cell_id$0a967f38-0493-11eb-0624-77e40b24d757codemd""" We used a `let` block in this cell to group multiple expressions together, but how is it different from `begin` or `function`? $(html"") > ##### _**function**_ vs. _**begin**_ vs. _**let**_ > Writing functions is a way to group multiple expressions (i.e. lines of code) together into a mini-program. Note the following about functions: > - A function always returns **one object**.[^1] This object can be given explicitly by writing `return x`, or implicitly: Julia functions always return the result of the last expression by default. So `f(x) = x+2` is the same as `f(x) = return x+2`. > - Variables defined inside a function are _not accessible outside the function_. We say that function bodies have a **local scope**. This helps to keep your program easy to read and write: if you define a local variable, then you don't need to worry about it in the rest of the notebook. > > There are two other ways to group epxressions together that you might have seen before: `begin` and `let`. > > ###### begin > **`begin`** will group expressions together, and it takes the value of its last subexpression. > > We use it in this notebook when we want multiple expressions to always run together. > > ###### let > **`let`** also groups multiple expressions together into one, but variables defined inside of it are **local**: they don't affect code outside of the block. So like `begin`, it is just a block of code, but like `function`, it has a local variable scope. > > We use it when we want to define some local (temporary) variables to produce a complicated result, without interfering with other cells. Pluto allows only one definition per _global_ variable of the same name, but you can define _local_ variables with the same names whenever you wish! > > [^1]: Even a function like > > `f(x) = return` > > returns **one object**: the object `nothing` — try it out! """metadatashow_logsèdisabled®skip_as_script«code_folded$28db9d98-04ca-11eb-3606-9fb89fa62f36cell_id$28db9d98-04ca-11eb-3606-9fb89fa62f36code3@bind run_basic_sir Button("Run simulation again!")metadatashow_logsèdisabled®skip_as_script«code_folded$8692bf42-0403-11eb-191f-b7d08895274fcell_id$8692bf42-0403-11eb-191f-b7d08895274fcodemd""" #### Exericse 2.4 👉 Write a function `generate_agents(N)` that returns a vector of `N` freshly created `Agent`s. They should all be initially susceptible, except one, chosen at random (i.e. uniformly), who is infectious. """metadatashow_logsèdisabled®skip_as_script«code_folded$da49710e-0420-11eb-092e-4f1173868738cell_id$da49710e-0420-11eb-092e-4f1173868738codemd""" ## **Exercise 5** - _Lecture transcript_ (MIT students only) Please see the link for hw 4 transcript document on [Canvas](https://canvas.mit.edu/courses/5637). We want each of you to correct about 400 lines, but don’t spend more than 15 minutes on it. See the the beginning of the document for more instructions. :point_right: Please mention the name of the video(s) and the line ranges you edited: """metadatashow_logsèdisabled®skip_as_script«code_folded$7bb8e426-0495-11eb-3a8b-cbbab61a1631cell_id$7bb8e426-0495-11eb-3a8b-cbbab61a1631codeيlet expected_shape(x) = .25exp.(-.3(x)) p = bar(frequencies(large_experiment)) plot!(p, LinRange(0.0, 30.0, 100), expected_shape) endmetadatashow_logsèdisabled®skip_as_script«code_folded$a4c9ccdc-12ca-11eb-072f-e34595520548cell_id$a4c9ccdc-12ca-11eb-072f-e34595520548codelet T = length(first(simulations).S) all_S_counts = map(result -> result.S, simulations) all_I_counts = map(result -> result.I, simulations) all_R_counts = map(result -> result.R, simulations) (S=round.(sum(all_S_counts) ./ length(simulations) ./ 100, digits=4), I=round.(sum(all_I_counts) ./ length(simulations) ./ 100, digits=4), R=round.(sum(all_R_counts) ./ length(simulations) ./ 100, digits=4)) end |> stringmetadatashow_logsèdisabled®skip_as_script«code_folded$21c50840-0435-11eb-1307-7138ecde0691cell_id$21c50840-0435-11eb-1307-7138ecde0691codemetadatashow_logsèdisabled®skip_as_script«code_folded$2ade2694-0425-11eb-2fb2-390da43d9695cell_id$2ade2694-0425-11eb-2fb2-390da43d9695codeM# function step!(agents::Vector{Agent}, infection::AbstractInfection) # endmetadatashow_logsèdisabled®skip_as_script«code_folded$aa6673d4-0417-11eb-32cc-79560896c195cell_id$aa6673d4-0417-11eb-32cc-79560896c195codeقfunction frequencies(values) result = Dict() for x in values result[x] = get(result, x, 0) + 1/length(values) end result endmetadatashow_logsèdisabled®skip_as_script«code_folded$73047bba-0416-11eb-1047-23e9c3dbde05cell_id$73047bba-0416-11eb-1047-23e9c3dbde05code4interpretation_of_p_equals_one = md""" blablabla """metadatashow_logsèdisabled®skip_as_script«code_folded$bf6fd176-04cc-11eb-008a-2fb6ff70a9cbcell_id$bf6fd176-04cc-11eb-008a-2fb6ff70a9cbcode?md""" #### Exercise 3.2 Alright! Every time that we run the simulation, we get slightly different results, because it is based on randomness. By running the simulation a number of times, you start to get an idea of the _mean behaviour_ of our model. This is the essence of a Monte Carlo method! You use computer-generated randomness to generate samples. Instead of pressing the button many times, let's have the computer repeat the simulation. In the next cells, we run your simulation `num_simulations=20` times with $N=100$, $p_\text{infection} = 0.02$, $p_\text{infection} = 0.002$ and $T = 1000$. Every single simulation returns a named tuple with the status counts, so the result of multiple simulations will be an array of those. Have a look inside the result, `simulations`, and make sure that its structure is clear. """metadatashow_logsèdisabled®skip_as_script«code_folded$43e6e856-0414-11eb-19ca-07358aa8b667cell_id$43e6e856-0414-11eb-19ca-07358aa8b667codeـstill_missing(text=md"Replace `missing` with your answer.") = Markdown.MD(Markdown.Admonition("warning", "Here we go!", [text]))metadatashow_logsèdisabled®skip_as_script«code_folded$95c598d4-0403-11eb-2328-0175ed564915cell_id$95c598d4-0403-11eb-2328-0175ed564915codeٍmd""" 👉 Write a function `sir_mean_plot` that returns a plot of the means of $S$, $I$ and $R$ as a function of time on a single graph. """metadatashow_logsèdisabled®skip_as_script«code_folded$2b26dc42-0403-11eb-205f-cd2c23d8cb03cell_id$2b26dc42-0403-11eb-205f-cd2c23d8cb03codebigbreakmetadatashow_logsèdisabled®skip_as_script«code_folded$1ac4b33a-0435-11eb-36f8-8f3f81ae7844cell_id$1ac4b33a-0435-11eb-36f8-8f3f81ae7844codemetadatashow_logsèdisabled®skip_as_script«code_folded$a8dd5cae-0425-11eb-119c-bfcbf832d695cell_id$a8dd5cae-0425-11eb-119c-bfcbf832d695code8function is_infected(agent::Agent) return missing endmetadatashow_logsèdisabled®skip_as_script«code_folded«notebook_id$03ce5f0e-4aa7-11f0-2d78-7f415ce24d69in_temp_dir¨metadata