{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Random walks" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Short function definition (one-liner):" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "jump (generic function with 1 method)" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "jump() = rand( (-1, +1) )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Exactly the same:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "jump (generic function with 1 method)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "function jump()\n", " return rand( (-1, +1) )\n", "end " ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "jump (generic function with 1 method)" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "jump" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "jump()" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rand( [-1, +1] )" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(-1, 1)" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t = (-1, +1)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Tuple{Int64,Int64}" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "typeof(t)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "ename": "MethodError", "evalue": "MethodError: no method matching setindex!(::Tuple{Int64,Int64}, ::Int64, ::Int64)", "output_type": "error", "traceback": [ "MethodError: no method matching setindex!(::Tuple{Int64,Int64}, ::Int64, ::Int64)", "", "Stacktrace:", " [1] top-level scope at In[10]:1" ] } ], "source": [ "t[1] = 10" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Side remark: Simple bernoulli function:" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "bernoulli (generic function with 1 method)" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bernoulli(p) = rand() < p" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "false" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bernoulli(0.25)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-1" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "jump()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Random walks" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10-element Array{Int64,1}:\n", " 1\n", " -1\n", " 1\n", " 1\n", " 1\n", " 1\n", " -1\n", " 1\n", " -1\n", " 1" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[jump() for i in 1:10]" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "walk (generic function with 1 method)" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "function walk(n)\n", " x = 0\n", " \n", " for i in 1:n\n", " x += jump() # x = x + jump()\n", " end\n", " \n", " return x\n", "end" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "walk(10)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "trajectory (generic function with 1 method)" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "function trajectory(n)\n", " x = 0\n", " \n", " xs = [x]\n", " \n", " for i in 1:n\n", " x += jump() # x = x + jump()\n", " end\n", " \n", " return x\n", "end" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0-element Array{Any,1}" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v = []" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1-element Array{Any,1}:\n", " 10" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "push!(v, 10)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1-element Array{Any,1}:\n", " 10" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "3-element Array{Any,1}:\n", " 10\n", " \"hellohellohellohellohellohello\"\n", " 20" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "push!(v, \"hellohellohellohellohellohello\", 20)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0-element Array{Int64,1}" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v = Int64[]" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1-element Array{Int64,1}:\n", " 10" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "push!(v, 10)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "ename": "MethodError", "evalue": "MethodError: Cannot `convert` an object of type String to an object of type Int64\nClosest candidates are:\n convert(::Type{T}, !Matched::T) where T<:Number at number.jl:6\n convert(::Type{T}, !Matched::Number) where T<:Number at number.jl:7\n convert(::Type{T}, !Matched::Ptr) where T<:Integer at pointer.jl:23\n ...", "output_type": "error", "traceback": [ "MethodError: Cannot `convert` an object of type String to an object of type Int64\nClosest candidates are:\n convert(::Type{T}, !Matched::T) where T<:Number at number.jl:6\n convert(::Type{T}, !Matched::Number) where T<:Number at number.jl:7\n convert(::Type{T}, !Matched::Ptr) where T<:Integer at pointer.jl:23\n ...", "", "Stacktrace:", " [1] push!(::Array{Int64,1}, ::String) at ./array.jl:912", " [2] top-level scope at In[24]:1" ] } ], "source": [ "push!(v, \"hello\")" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "trajectory (generic function with 1 method)" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "function trajectory(n)\n", " x = 0\n", " \n", " xs = [x] # try to initialize array with an initial value; \n", " # determines the element type of array \n", " for i in 1:n\n", " x += jump() # x = x + jump()\n", " \n", " push!(xs, x)\n", " end\n", " \n", " return xs\n", "end" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1-element Array{Int64,1}:\n", " 10" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v = [10]" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [], "source": [ "traj = trajectory(100);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Plotting" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "ename": "UndefVarError", "evalue": "UndefVarError: plot not defined", "output_type": "error", "traceback": [ "UndefVarError: plot not defined", "", "Stacktrace:", " [1] top-level scope at In[28]:1" ] } ], "source": [ "plot(traj)" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [], "source": [ "using Plots" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "traj = trajectory(100);\n", "plot(traj, m=:o)" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "traj = trajectory(100);\n", "plot(traj, m=:o)" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "traj = trajectory(100);\n", "plot(traj, m=:o, size=(300, 200))\n", "\n", "hline!([0], ls=:dash) # ! functions add to the existing plot" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "num_walkers = 1000\n", "num_steps = 100\n", "\n", "p = plot(size=(500, 400), leg=false)\n", "\n", "for i in 1:num_walkers\n", " traj = trajectory(num_steps)\n", " \n", " plot!(traj)\n", "end\n", "\n", "p" ] }, { "cell_type": "code", "execution_count": 40, "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": 41, "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": 20, "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": "4860698096510693037", "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\"],\"changes\":WebIO.getval({\"name\":\"changes\",\"scope\":\"4860698096510693037\",\"id\":\"ob_02\",\"type\":\"observable\"}),\"index\":WebIO.getval({\"name\":\"index\",\"scope\":\"4860698096510693037\",\"id\":\"ob_01\",\"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\":\"4860698096510693037\",\"id\":\"ob_02\",\"type\":\"observable\"},val)) : undefined; return this.valueFromJulia[\"changes\"]=false}),self),this[\"index\"].subscribe((function (val){!(this.valueFromJulia[\"index\"]) ? (WebIO.setval({\"name\":\"index\",\"scope\":\"4860698096510693037\",\"id\":\"ob_01\",\"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_02", "sync": false, "value": 0 }, "index": { "id": "ob_01", "sync": true, "value": 10 } }, "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_08", "name": "obs-node" }, "nodeType": "ObservableNode", "props": {}, "type": "node" } ], "instanceArgs": { "handlers": {}, "id": "1032921873996854121", "imports": { "data": [], "type": "async_block" }, "mount_callbacks": [], "observables": { "obs-node": { "id": "ob_08", "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": [ "