{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Cumulative distribution of some geometric random variables" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Probability that we have recovered at or before time $N$" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "plot_cumulative!" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"\"\"\n", " plot_cumulative!(p, N, δ=1)\n", "\n", "- `p` is prob to decay at each time step\n", "- `N` is total number of time steps\n", "- `δ` is time between steps\n", "\n", "Prob mass function is ``f_X(n) = p (1 - p)^{n-1}``\n", "\"\"\"\n", "function plot_cumulative!(p, N, δ=1; label=\"\")\n", " ps = [p * (1 - p)^(n-1) for n in 1:N]\n", " cumulative = cumsum(ps)\n", "\n", " ys = [0; reduce(vcat, [ [cumulative[n], cumulative[n]] for n in 1:N ])]\n", "\n", " pop!(ys)\n", " pushfirst!(ys, 0)\n", "\n", " xs = [0; reduce(vcat, [ [n*δ, n*δ] for n in 1:N ])];\n", "\n", " plot!(xs, ys, label=label)\n", " scatter!([n*δ for n in 1:N], cumulative, label=\"\")\n", "end" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "search: \u001b[0m\u001b[1mp\u001b[22m\u001b[0m\u001b[1ml\u001b[22m\u001b[0m\u001b[1mo\u001b[22m\u001b[0m\u001b[1mt\u001b[22m\u001b[0m\u001b[1m_\u001b[22m\u001b[0m\u001b[1mc\u001b[22m\u001b[0m\u001b[1mu\u001b[22m\u001b[0m\u001b[1mm\u001b[22m\u001b[0m\u001b[1mu\u001b[22m\u001b[0m\u001b[1ml\u001b[22m\u001b[0m\u001b[1ma\u001b[22m\u001b[0m\u001b[1mt\u001b[22m\u001b[0m\u001b[1mi\u001b[22m\u001b[0m\u001b[1mv\u001b[22m\u001b[0m\u001b[1me\u001b[22m\u001b[0m\u001b[1m!\u001b[22m\n", "\n" ] }, { "data": { "text/latex": [ "\\begin{verbatim}\n", "plot_cumulative!(p, N, δ=1)\n", "\\end{verbatim}\n", "\\begin{itemize}\n", "\\item \\texttt{p} is prob to decay at each time step\n", "\n", "\n", "\\item \\texttt{N} is total number of time steps\n", "\n", "\n", "\\item \\texttt{δ} is time between steps\n", "\n", "\\end{itemize}\n", "Prob mass function is $f_X(n) = p (1 - p)^{n-1}$\n", "\n" ], "text/markdown": [ "```\n", "plot_cumulative!(p, N, δ=1)\n", "```\n", "\n", " * `p` is prob to decay at each time step\n", " * `N` is total number of time steps\n", " * `δ` is time between steps\n", "\n", "Prob mass function is $f_X(n) = p (1 - p)^{n-1}$\n" ], "text/plain": [ "\u001b[36m plot_cumulative!(p, N, δ=1)\u001b[39m\n", "\n", " • \u001b[36mp\u001b[39m is prob to decay at each time step\n", "\n", " • \u001b[36mN\u001b[39m is total number of time steps\n", "\n", " • \u001b[36mδ\u001b[39m is time between steps\n", "\n", " Prob mass function is \u001b[35mf_X(n) = p (1 - p)^{n-1}\u001b[39m" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "?plot_cumulative!" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "using Plots" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "plot_cumulative!(0.1, 20)\n", "\n", "xlabel!(\"n\")\n", "ylabel!(\"cum. prob. <= n\")" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p = 0.1\n", "N = 50\n", "δ = 0.4\n", "\n", "plot()\n", "plot_cumulative!(p, N, 1.0, label=\"delta = 1\")\n", "plot_cumulative!(p, N, δ, label=\"delta = $(δ)\") # $a inserts value of variable a\n", "\n" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", " Unable to load WebIO. Please make sure WebIO works for your Jupyter client.\n", " For troubleshooting, please see \n", " the WebIO/IJulia documentation.\n", " \n", "

\n" ], "text/plain": [ "HTML{String}(\"\\n\\n Unable to load WebIO. Please make sure WebIO works for your Jupyter client.\\n For troubleshooting, please see \\n the WebIO/IJulia documentation.\\n \\n

\\n\")" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "using Interact" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "application/vnd.webio.node+json": { "children": [ { "children": [ { "children": [ { "children": [ { "children": [ { "children": [ "p2" ], "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": "9374953195556779067", "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/9354074556c360cc537db1e8b0f0283e19c8ea9d-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\":[\"0.01\",\"0.02\",\"0.03\",\"0.04\",\"0.05\",\"0.06\",\"0.07\",\"0.08\",\"0.09\",\"0.1\",\"0.11\",\"0.12\",\"0.13\",\"0.14\",\"0.15\",\"0.16\",\"0.17\",\"0.18\",\"0.19\",\"0.2\",\"0.21\",\"0.22\",\"0.23\",\"0.24\",\"0.25\",\"0.26\",\"0.27\",\"0.28\",\"0.29\",\"0.3\",\"0.31\",\"0.32\",\"0.33\",\"0.34\",\"0.35\",\"0.36\",\"0.37\",\"0.38\",\"0.39\",\"0.4\",\"0.41\",\"0.42\",\"0.43\",\"0.44\",\"0.45\",\"0.46\",\"0.47\",\"0.48\",\"0.49\",\"0.5\",\"0.51\",\"0.52\",\"0.53\",\"0.54\",\"0.55\",\"0.56\",\"0.57\",\"0.58\",\"0.59\",\"0.6\",\"0.61\",\"0.62\",\"0.63\",\"0.64\",\"0.65\",\"0.66\",\"0.67\",\"0.68\",\"0.69\",\"0.7\",\"0.71\",\"0.72\",\"0.73\",\"0.74\",\"0.75\",\"0.76\",\"0.77\",\"0.78\",\"0.79\",\"0.8\",\"0.81\",\"0.82\",\"0.83\",\"0.84\",\"0.85\",\"0.86\",\"0.87\",\"0.88\",\"0.89\",\"0.9\",\"0.91\",\"0.92\",\"0.93\",\"0.94\",\"0.95\",\"0.96\",\"0.97\",\"0.98\",\"0.99\",\"1.0\"],\"changes\":WebIO.getval({\"name\":\"changes\",\"scope\":\"9374953195556779067\",\"id\":\"ob_57\",\"type\":\"observable\"}),\"index\":WebIO.getval({\"name\":\"index\",\"scope\":\"9374953195556779067\",\"id\":\"ob_56\",\"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\":\"9374953195556779067\",\"id\":\"ob_57\",\"type\":\"observable\"},val)) : undefined; return this.valueFromJulia[\"changes\"]=false}),self),this[\"index\"].subscribe((function (val){!(this.valueFromJulia[\"index\"]) ? (WebIO.setval({\"name\":\"index\",\"scope\":\"9374953195556779067\",\"id\":\"ob_56\",\"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_57", "sync": false, "value": 0 }, "index": { "id": "ob_56", "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_63", "name": "obs-node" }, "nodeType": "ObservableNode", "props": {}, "type": "node" } ], "instanceArgs": { "handlers": {}, "id": "6513363634745203205", "imports": { "data": [], "type": "async_block" }, "mount_callbacks": [], "observables": { "obs-node": { "id": "ob_63", "sync": false, "value": { "children": [ { "children": [], "instanceArgs": { "namespace": "html", "tag": "div" }, "nodeType": "DOM", "props": { "setInnerHtml": "\n\n\n \n \n \n\n\n\n \n \n \n\n\n\n \n \n \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\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": [ "\n", " \n", "\n" ], "text/plain": [ "Node{WebIO.DOM}(WebIO.DOM(:html, :div), Any[Node{WebIO.DOM}(WebIO.DOM(:html, :div), Any[Scope(Node{WebIO.DOM}(WebIO.DOM(:html, :div), Any[Node{WebIO.DOM}(WebIO.DOM(:html, :div), Any[Node{WebIO.DOM}(WebIO.DOM(:html, :label), Any[\"p2\"], Dict{Symbol,Any}(:className => \"interact \",:style => Dict{Any,Any}(:padding => \"5px 10px 0px 10px\")))], Dict{Symbol,Any}(:className => \"interact-flex-row-left\")), Node{WebIO.DOM}(WebIO.DOM(:html, :div), Any[Node{WebIO.DOM}(WebIO.DOM(:html, :input), Any[], Dict{Symbol,Any}(:max => 100,:min => 1,:attributes => Dict{Any,Any}(:type => \"range\",Symbol(\"data-bind\") => \"numericValue: index, valueUpdate: 'input', event: {change: function (){this.changes(this.changes()+1)}}\",\"orient\" => \"horizontal\"),:step => 1,:className => \"slider slider is-fullwidth\",:style => Dict{Any,Any}()))], Dict{Symbol,Any}(:className => \"interact-flex-row-center\")), Node{WebIO.DOM}(WebIO.DOM(:html, :div), Any[Node{WebIO.DOM}(WebIO.DOM(:html, :p), Any[], Dict{Symbol,Any}(:attributes => Dict(\"data-bind\" => \"text: formatted_val\")))], Dict{Symbol,Any}(:className => \"interact-flex-row-right\"))], Dict{Symbol,Any}(:className => \"interact-flex-row interact-widget\")), Dict{String,Tuple{Observables.AbstractObservable,Union{Nothing, Bool}}}(\"changes\" => (Observable{Int64} with 1 listeners. Value:\n", "0, nothing),\"index\" => (Observable{Any} with 2 listeners. Value:\n", "50, nothing)), Set(String[]), nothing, Asset[Asset(\"js\", \"knockout\", \"/Users/dpsanders/.julia/packages/Knockout/IP1uR/src/../assets/knockout.js\"), Asset(\"js\", \"knockout_punches\", \"/Users/dpsanders/.julia/packages/Knockout/IP1uR/src/../assets/knockout_punches.js\"), Asset(\"js\", nothing, \"/Users/dpsanders/.julia/packages/InteractBase/9mFwe/src/../assets/all.js\"), Asset(\"css\", nothing, \"/Users/dpsanders/.julia/packages/InteractBase/9mFwe/src/../assets/style.css\"), Asset(\"css\", nothing, \"/Users/dpsanders/.julia/packages/Interact/SbgIk/src/../assets/bulma_confined.min.css\")], Dict{Any,Any}(\"changes\" => Any[WebIO.JSString(\"(function (val){return (val!=this.model[\\\"changes\\\"]()) ? (this.valueFromJulia[\\\"changes\\\"]=true, this.model[\\\"changes\\\"](val)) : undefined})\")],\"index\" => Any[WebIO.JSString(\"(function (val){return (val!=this.model[\\\"index\\\"]()) ? (this.valueFromJulia[\\\"index\\\"]=true, this.model[\\\"index\\\"](val)) : undefined})\")]), WebIO.ConnectionPool(Channel{Any}(sz_max:32,sz_curr:0), Set(AbstractConnection[]), Base.GenericCondition{Base.AlwaysLockedST}(Base.InvasiveLinkedList{Task}(Task (runnable) @0x00000001287dba90, Task (runnable) @0x00000001287dba90), Base.AlwaysLockedST(1))), WebIO.JSString[WebIO.JSString(\"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\\\":[\\\"0.01\\\",\\\"0.02\\\",\\\"0.03\\\",\\\"0.04\\\",\\\"0.05\\\",\\\"0.06\\\",\\\"0.07\\\",\\\"0.08\\\",\\\"0.09\\\",\\\"0.1\\\",\\\"0.11\\\",\\\"0.12\\\",\\\"0.13\\\",\\\"0.14\\\",\\\"0.15\\\",\\\"0.16\\\",\\\"0.17\\\",\\\"0.18\\\",\\\"0.19\\\",\\\"0.2\\\",\\\"0.21\\\",\\\"0.22\\\",\\\"0.23\\\",\\\"0.24\\\",\\\"0.25\\\",\\\"0.26\\\",\\\"0.27\\\",\\\"0.28\\\",\\\"0.29\\\",\\\"0.3\\\",\\\"0.31\\\",\\\"0.32\\\",\\\"0.33\\\",\\\"0.34\\\",\\\"0.35\\\",\\\"0.36\\\",\\\"0.37\\\",\\\"0.38\\\",\\\"0.39\\\",\\\"0.4\\\",\\\"0.41\\\",\\\"0.42\\\",\\\"0.43\\\",\\\"0.44\\\",\\\"0.45\\\",\\\"0.46\\\",\\\"0.47\\\",\\\"0.48\\\",\\\"0.49\\\",\\\"0.5\\\",\\\"0.51\\\",\\\"0.52\\\",\\\"0.53\\\",\\\"0.54\\\",\\\"0.55\\\",\\\"0.56\\\",\\\"0.57\\\",\\\"0.58\\\",\\\"0.59\\\",\\\"0.6\\\",\\\"0.61\\\",\\\"0.62\\\",\\\"0.63\\\",\\\"0.64\\\",\\\"0.65\\\",\\\"0.66\\\",\\\"0.67\\\",\\\"0.68\\\",\\\"0.69\\\",\\\"0.7\\\",\\\"0.71\\\",\\\"0.72\\\",\\\"0.73\\\",\\\"0.74\\\",\\\"0.75\\\",\\\"0.76\\\",\\\"0.77\\\",\\\"0.78\\\",\\\"0.79\\\",\\\"0.8\\\",\\\"0.81\\\",\\\"0.82\\\",\\\"0.83\\\",\\\"0.84\\\",\\\"0.85\\\",\\\"0.86\\\",\\\"0.87\\\",\\\"0.88\\\",\\\"0.89\\\",\\\"0.9\\\",\\\"0.91\\\",\\\"0.92\\\",\\\"0.93\\\",\\\"0.94\\\",\\\"0.95\\\",\\\"0.96\\\",\\\"0.97\\\",\\\"0.98\\\",\\\"0.99\\\",\\\"1.0\\\"],\\\"changes\\\":WebIO.getval({\\\"name\\\":\\\"changes\\\",\\\"scope\\\":\\\"9374953195556779067\\\",\\\"id\\\":\\\"ob_57\\\",\\\"type\\\":\\\"observable\\\"}),\\\"index\\\":WebIO.getval({\\\"name\\\":\\\"index\\\",\\\"scope\\\":\\\"9374953195556779067\\\",\\\"id\\\":\\\"ob_56\\\",\\\"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\\\":\\\"9374953195556779067\\\",\\\"id\\\":\\\"ob_57\\\",\\\"type\\\":\\\"observable\\\"},val)) : undefined; return this.valueFromJulia[\\\"changes\\\"]=false}),self),this[\\\"index\\\"].subscribe((function (val){!(this.valueFromJulia[\\\"index\\\"]) ? (WebIO.setval({\\\"name\\\":\\\"index\\\",\\\"scope\\\":\\\"9374953195556779067\\\",\\\"id\\\":\\\"ob_56\\\",\\\"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\")])], Dict{Symbol,Any}(:className => \"field interact-widget\")), Observable{Any} with 0 listeners. Value:\n", "Node{WebIO.DOM}(WebIO.DOM(:html, :div), Any[Plot{Plots.GRBackend() n=4}], Dict{Symbol,Any}(:className => \"interact-flex-row interact-widget\"))], Dict{Symbol,Any}())" ] }, "execution_count": 37, "metadata": { "application/vnd.webio.node+json": { "kernelId": "37a56126-9666-45b2-8f7c-8b0792b6b7f5" } }, "output_type": "execute_result" } ], "source": [ "p = 0.1\n", "N = 100\n", "δ = 0.5\n", "\n", "@manipulate for p2 in 0.01:0.01:1.0\n", " plot()\n", " plot_cumulative!(p, 40, 1.0, label=\"delta = 1\")\n", " plot_cumulative!(p2, N, δ, label=\"delta = $(δ)\") # $a inserts value of variable a\n", " \n", " xlims!(0, 40)\n", " ylims!(0, 1)\n", "end\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Cumulative distribution function for geometric random variable $X \\sim \\text{Geom}(p)$\n", "\n", "$$F_X(n) = 1 - (1 - p)^n$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$F_{Y_\\delta}(n) = 1 - [1 - p(\\delta)]^n$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Fix time $t = n$. Find $p(\\delta)$ such that probabilities are the same at time $t$\n", "\n", "$t = n \\delta$\n", "\n", "So $n = t / \\delta$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$1 - [1 - p(\\delta)]^{t/\\delta} = 1 - (1 - p)^t$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Exercise: Solve for $p(\\delta)$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Continuous limit" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Limit $\\delta \\to 0$\n", "\n", "Need $p(\\delta) \\to 0$\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If $x$ is small, $1 - x \\simeq \\exp(-x)$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$\\exp(x) = 1 + x + \\frac{1}{2} x^2 + \\frac{1}{3!} x^3 + \\cdots + \\frac{1}{n!} x^n + \\cdots$\n", "\n", "$\\exp(x) = \\sum_{n=0}^\\infty \\frac{1}{n!} x^n$ " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Define $\\exp{}$ as the function which is equal to its derivative and suppose that I can write as a power series in $x$" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.01" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = 0.01" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.99" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 - x" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.9900498337491681" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "exp(-x)" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5.0e-5" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x^2 / 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$1 −[1−𝑝(𝛿)]^{𝑡/𝛿} \\simeq 1 - \\{ \\exp[-p(\\delta)] \\}^{t/\\delta}$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$1 − \\exp \\left[ −𝑝(𝛿) \\frac{𝑡}{𝛿} \\right]$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Need $p(\\delta) / \\delta \\to \\lambda$ when $\\delta \\to 0$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "End result: $F(t) = 1 - \\exp[-\\lambda t]$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$\\lambda$ is **rate** -- probability per unit time" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$1 - (1 - p)^t$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Choose $\\lambda$ such that $1 - p = \\exp(- \\lambda)$" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "plot()\n", "\n", "p = 0.1\n", "λ = -log(1 - p)\n", "plot_cumulative!(p, 50)\n", "\n", "plot!(0:0.01:20, t -> 1 - exp(-λ*t), lw=3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exponential random variable** $Z$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$F_Z(t) = \\mathbb{P}(Z \\le t)$" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "@webio": { "lastCommId": "6ae2db5ad18b4a08b03385e6256b9115", "lastKernelId": "37a56126-9666-45b2-8f7c-8b0792b6b7f5" }, "kernelspec": { "display_name": "Julia 1.4.1", "language": "julia", "name": "julia-1.4" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.4.1" } }, "nbformat": 4, "nbformat_minor": 4 }