{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Modelling the recovery process using probability" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Recall code from last time for $I_{n+1} = (1 + c) I_n = \\lambda * I_n$:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "run_infection (generic function with 3 methods)" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "function run_infection(I_0, λ, T=20) # T=20 is default value\n", "\n", " I = zeros(T)\n", " I[1] = I_0\n", "\n", " for n in 1:T-1\n", " I[n+1] = λ * I[n] \n", " end\n", "\n", " return I\n", "end" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "run_infection(1.0, 0.1);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Adding new data to extend a vector" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1-element Array{Float64,1}:\n", " 1.0" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v = [1.0]" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2-element Array{Float64,1}:\n", " 1.0\n", " 7.0" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "push!(v, 7.0) # ! means: function `push!` modifies its first argument" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "length(v)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "ename": "BoundsError", "evalue": "BoundsError: attempt to access 2-element Array{Float64,1} at index [3]", "output_type": "error", "traceback": [ "BoundsError: attempt to access 2-element Array{Float64,1} at index [3]", "", "Stacktrace:", " [1] getindex(::Array{Float64,1}, ::Int64) at ./array.jl:787", " [2] top-level scope at In[14]:2" ] } ], "source": [ "a = 1\n", "\n", "v[3]" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2-element Array{Float64,1}:\n", " 1.0\n", " 7.0" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3-element Array{Float64,1}:\n", " 1.0\n", " 7.0\n", " 10.0" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[v; 10]" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2-element Array{Float64,1}:\n", " 1.0\n", " 7.0" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3-element Array{Float64,1}:\n", " 1.0\n", " 7.0\n", " 10.0" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v2 = [v; 10]" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3-element Array{Float64,1}:\n", " 1.0\n", " 7.0\n", " 10.0" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v2 = copy(v)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "run_infection (generic function with 3 methods)" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "function run_infection(I_0, λ, T=20) # T=20 is default value\n", "\n", " Is = [I_0]\n", " \n", " I = I_0 # current value of I\n", "\n", " for n in 1:T-1\n", " I_next = λ * I\n", " \n", " push!(Is, I_next)\n", " end\n", "\n", " return Is\n", "end" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "20-element Array{Float64,1}:\n", " 1.0\n", " 1.1\n", " 1.1\n", " 1.1\n", " 1.1\n", " 1.1\n", " 1.1\n", " 1.1\n", " 1.1\n", " 1.1\n", " 1.1\n", " 1.1\n", " 1.1\n", " 1.1\n", " 1.1\n", " 1.1\n", " 1.1\n", " 1.1\n", " 1.1\n", " 1.1" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "run_infection(1.0, 1.1)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "run_infection (generic function with 3 methods)" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "function run_infection(I_0, λ, T=20) # T=20 is default value\n", "\n", " Is = [I_0]\n", " I = I_0 # current value of I\n", "\n", " for n in 1:T-1\n", " I_next = λ * I\n", " \n", " push!(Is, I_next)\n", " \n", " I = I_next\n", " end\n", " \n", " return Is\n", "end" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "20-element Array{Float64,1}:\n", " 1.0\n", " 1.1\n", " 1.2100000000000002\n", " 1.3310000000000004\n", " 1.4641000000000006\n", " 1.6105100000000008\n", " 1.771561000000001\n", " 1.9487171000000014\n", " 2.1435888100000016\n", " 2.357947691000002\n", " 2.5937424601000023\n", " 2.853116706110003\n", " 3.1384283767210035\n", " 3.4522712143931042\n", " 3.797498335832415\n", " 4.177248169415656\n", " 4.594972986357222\n", " 5.054470284992944\n", " 5.559917313492239\n", " 6.115909044841463" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "run_infection(1.0, 1.1)" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3-element Array{Float64,1}:\n", " 1.0\n", " 7.0\n", " 10.0" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4-element Array{Float64,1}:\n", " 8.0\n", " 1.0\n", " 7.0\n", " 10.0" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pushfirst!(v, 8) # deque - double-ended queue" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Randomness" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.6254465754499758" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "r = rand()" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.9251027927183173" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "r = rand()" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [], "source": [ "using Random" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [], "source": [ "Random.seed!(3);" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.8116984049958615" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "r = rand()" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.9884323655013432" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "r = rand()" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [], "source": [ "Random.seed!(3); # mechanism to get repeatable sequences of random numbers" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.8116984049958615" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "r = rand()" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.9884323655013432" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "r = rand()" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10-element Array{Float64,1}:\n", " 0.8076220876500786\n", " 0.9700908450487538\n", " 0.14006111319509862\n", " 0.5094438024440222\n", " 0.05869740597593154\n", " 0.004257960600515309\n", " 0.9746379934512355\n", " 0.5572251384524507\n", " 0.4644219211852372\n", " 0.2875090558291695" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rand(10)" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [], "source": [ "using Plots" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [], "source": [ "r = rand(50);" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "scatter(r)" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.0" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "one(0.5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "scatter(r, 0.5 .* one.(r), ylim=(0, 1))" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "data": { "application/vnd.webio.node+json": { "children": [ { "children": [ { "children": [ { "children": [ { "children": [ { "children": [ "n" ], "instanceArgs": { "namespace": "html", "tag": "label" }, "nodeType": "DOM", "props": { "className": "interact ", "style": { "padding": "5px 10px 0px 10px" } }, "type": "node" } ], "instanceArgs": { "namespace": "html", "tag": "div" }, "nodeType": "DOM", "props": { "className": "interact-flex-row-left" }, "type": "node" }, { "children": [ { "children": [], "instanceArgs": { "namespace": "html", "tag": "input" }, "nodeType": "DOM", "props": { "attributes": { "data-bind": "numericValue: index, valueUpdate: 'input', event: {change: function (){this.changes(this.changes()+1)}}", "orient": "horizontal", "type": "range" }, "className": "slider slider is-fullwidth", "max": 100, "min": 1, "step": 1, "style": {} }, "type": "node" } ], "instanceArgs": { "namespace": "html", "tag": "div" }, "nodeType": "DOM", "props": { "className": "interact-flex-row-center" }, "type": "node" }, { "children": [ { "children": [], "instanceArgs": { "namespace": "html", "tag": "p" }, "nodeType": "DOM", "props": { "attributes": { "data-bind": "text: formatted_val" } }, "type": "node" } ], "instanceArgs": { "namespace": "html", "tag": "div" }, "nodeType": "DOM", "props": { "className": "interact-flex-row-right" }, "type": "node" } ], "instanceArgs": { "namespace": "html", "tag": "div" }, "nodeType": "DOM", "props": { "className": "interact-flex-row interact-widget" }, "type": "node" } ], "instanceArgs": { "handlers": { "changes": [ "(function (val){return (val!=this.model[\"changes\"]()) ? (this.valueFromJulia[\"changes\"]=true, this.model[\"changes\"](val)) : undefined})" ], "index": [ "(function (val){return (val!=this.model[\"index\"]()) ? (this.valueFromJulia[\"index\"]=true, this.model[\"index\"](val)) : undefined})" ] }, "id": "2580909790707749940", "imports": { "data": [ { "name": "knockout", "type": "js", "url": "/assetserver/800fa077652c804a9c5f68b6c1e29c6c61f85df7-knockout.js" }, { "name": "knockout_punches", "type": "js", "url": "/assetserver/81e3f253b58efa296c3c65b00e2bf7b25de6cca9-knockout_punches.js" }, { "name": null, "type": "js", "url": "/assetserver/5d2042c6908ffff0b7c8d26135740476bcf00827-all.js" }, { "name": null, "type": "css", "url": "/assetserver/87ad319ae61de5d44f67c268a0a8f65862fca478-style.css" }, { "name": null, "type": "css", "url": "/assetserver/a062b31556f81f574879e47f48c7fae2687f3c82-bulma_confined.min.css" } ], "type": "async_block" }, "mount_callbacks": [ "function () {\n var handler = (function (ko, koPunches) {\n ko.punches.enableAll();\n ko.bindingHandlers.numericValue = {\n init: function(element, valueAccessor, allBindings, data, context) {\n var stringified = ko.observable(ko.unwrap(valueAccessor()));\n stringified.subscribe(function(value) {\n var val = parseFloat(value);\n if (!isNaN(val)) {\n valueAccessor()(val);\n }\n });\n valueAccessor().subscribe(function(value) {\n var str = JSON.stringify(value);\n if ((str == \"0\") && ([\"-0\", \"-0.\"].indexOf(stringified()) >= 0))\n return;\n if ([\"null\", \"\"].indexOf(str) >= 0)\n return;\n stringified(str);\n });\n ko.applyBindingsToNode(\n element,\n {\n value: stringified,\n valueUpdate: allBindings.get('valueUpdate'),\n },\n context,\n );\n }\n };\n var json_data = {\"formatted_vals\":[\"1\",\"2\",\"3\",\"4\",\"5\",\"6\",\"7\",\"8\",\"9\",\"10\",\"11\",\"12\",\"13\",\"14\",\"15\",\"16\",\"17\",\"18\",\"19\",\"20\",\"21\",\"22\",\"23\",\"24\",\"25\",\"26\",\"27\",\"28\",\"29\",\"30\",\"31\",\"32\",\"33\",\"34\",\"35\",\"36\",\"37\",\"38\",\"39\",\"40\",\"41\",\"42\",\"43\",\"44\",\"45\",\"46\",\"47\",\"48\",\"49\",\"50\",\"51\",\"52\",\"53\",\"54\",\"55\",\"56\",\"57\",\"58\",\"59\",\"60\",\"61\",\"62\",\"63\",\"64\",\"65\",\"66\",\"67\",\"68\",\"69\",\"70\",\"71\",\"72\",\"73\",\"74\",\"75\",\"76\",\"77\",\"78\",\"79\",\"80\",\"81\",\"82\",\"83\",\"84\",\"85\",\"86\",\"87\",\"88\",\"89\",\"90\",\"91\",\"92\",\"93\",\"94\",\"95\",\"96\",\"97\",\"98\",\"99\",\"100\"],\"changes\":WebIO.getval({\"name\":\"changes\",\"scope\":\"2580909790707749940\",\"id\":\"ob_10\",\"type\":\"observable\"}),\"index\":WebIO.getval({\"name\":\"index\",\"scope\":\"2580909790707749940\",\"id\":\"ob_09\",\"type\":\"observable\"})};\n var self = this;\n function AppViewModel() {\n for (var key in json_data) {\n var el = json_data[key];\n this[key] = Array.isArray(el) ? ko.observableArray(el) : ko.observable(el);\n }\n \n [this[\"formatted_val\"]=ko.computed( function(){\n return this.formatted_vals()[parseInt(this.index())-(1)];\n }\n,this)]\n [this[\"changes\"].subscribe((function (val){!(this.valueFromJulia[\"changes\"]) ? (WebIO.setval({\"name\":\"changes\",\"scope\":\"2580909790707749940\",\"id\":\"ob_10\",\"type\":\"observable\"},val)) : undefined; return this.valueFromJulia[\"changes\"]=false}),self),this[\"index\"].subscribe((function (val){!(this.valueFromJulia[\"index\"]) ? (WebIO.setval({\"name\":\"index\",\"scope\":\"2580909790707749940\",\"id\":\"ob_09\",\"type\":\"observable\"},val)) : undefined; return this.valueFromJulia[\"index\"]=false}),self)]\n \n }\n self.model = new AppViewModel();\n self.valueFromJulia = {};\n for (var key in json_data) {\n self.valueFromJulia[key] = false;\n }\n ko.applyBindings(self.model, self.dom);\n}\n);\n (WebIO.importBlock({\"data\":[{\"name\":\"knockout\",\"type\":\"js\",\"url\":\"/assetserver/800fa077652c804a9c5f68b6c1e29c6c61f85df7-knockout.js\"},{\"name\":\"knockout_punches\",\"type\":\"js\",\"url\":\"/assetserver/81e3f253b58efa296c3c65b00e2bf7b25de6cca9-knockout_punches.js\"}],\"type\":\"async_block\"})).then((imports) => handler.apply(this, imports));\n}\n" ], "observables": { "changes": { "id": "ob_10", "sync": false, "value": 0 }, "index": { "id": "ob_09", "sync": true, "value": 50 } }, "systemjs_options": null }, "nodeType": "Scope", "props": {}, "type": "node" } ], "instanceArgs": { "namespace": "html", "tag": "div" }, "nodeType": "DOM", "props": { "className": "field interact-widget" }, "type": "node" }, { "children": [ { "children": [], "instanceArgs": { "id": "ob_16", "name": "obs-node" }, "nodeType": "ObservableNode", "props": {}, "type": "node" } ], "instanceArgs": { "handlers": {}, "id": "17050130591139244532", "imports": { "data": [], "type": "async_block" }, "mount_callbacks": [], "observables": { "obs-node": { "id": "ob_16", "sync": false, "value": { "children": [ { "children": [], "instanceArgs": { "namespace": "html", "tag": "div" }, "nodeType": "DOM", "props": { "setInnerHtml": "\n\n" }, "type": "node" } ], "instanceArgs": { "namespace": "html", "tag": "div" }, "nodeType": "DOM", "props": { "className": "interact-flex-row interact-widget" }, "type": "node" } } }, "systemjs_options": null }, "nodeType": "Scope", "props": {}, "type": "node" } ], "instanceArgs": { "namespace": "html", "tag": "div" }, "nodeType": "DOM", "props": {}, "type": "node" }, "text/html": [ "