{ "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" ] }, "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" ] }, "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" }, "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": [ "