└── homework └── homework_1 ├── simple_rw.png ├── Evolution equations.ipynb ├── Homework 1, 18.337 2016.ipynb └── .ipynb_checkpoints ├── Homework 1, 18.337 2016-checkpoint.ipynb └── Absorbing random walk-checkpoint.ipynb /homework/homework_1/simple_rw.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpsanders/18.337/master/homework/homework_1/simple_rw.png -------------------------------------------------------------------------------- /homework/homework_1/Evolution equations.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Master equation " 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "Let $P_t(i)$ be the probability of a random walker being at site $i$ at time $t$.\n", 15 | "\n", 16 | "The master equation, i.e. the evolution equation for the probability distribution, is\n", 17 | "\n", 18 | "$$P_{t+1}(i) = \\sum_j P_{t}(j) \\, p(i \\, | \\, j).$$\n", 19 | "\n", 20 | "For a simple random walk, this gives, in the bulk (i.e. away from the boundaries):\n", 21 | "\n", 22 | "$$P_{t+1}(i) = \\textstyle \\frac{1}{2} \\left[ P_t(i-1) + P_t(i+1) \\right] $$" 23 | ] 24 | }, 25 | { 26 | "cell_type": "markdown", 27 | "metadata": {}, 28 | "source": [ 29 | "The initial probability distribution is" 30 | ] 31 | }, 32 | { 33 | "cell_type": "markdown", 34 | "metadata": {}, 35 | "source": [ 36 | "$$P_0(i) = \\delta_{i, 1} = \\begin{cases} 1, \\quad \\text{ if } i = 1 \\\\\n", 37 | " 0, \\quad \\text {otherwise}\n", 38 | " \\end{cases}$$" 39 | ] 40 | }, 41 | { 42 | "cell_type": "markdown", 43 | "metadata": {}, 44 | "source": [ 45 | "The absorbing boundary at site $i=0$ gives" 46 | ] 47 | }, 48 | { 49 | "cell_type": "markdown", 50 | "metadata": {}, 51 | "source": [ 52 | "$$P_{t+1}(0) = P_{t}(0) + \\textstyle \\frac{1}{2} P_{t}(1)$$\n", 53 | "$$P_{t+1}(1) = \\textstyle \\frac{1}{2} P_{t}(2)$$" 54 | ] 55 | }, 56 | { 57 | "cell_type": "markdown", 58 | "metadata": {}, 59 | "source": [ 60 | "The reflecting boundary at site $i=L$ implies" 61 | ] 62 | }, 63 | { 64 | "cell_type": "markdown", 65 | "metadata": {}, 66 | "source": [ 67 | "$$P_{t+1}(L) = \\textstyle \\frac{1}{2} P_{t}(L-1) + \\textstyle \\frac{1}{2} P_{t}(L) $$" 68 | ] 69 | } 70 | ], 71 | "metadata": { 72 | "kernelspec": { 73 | "display_name": "Julia 0.4.6", 74 | "language": "julia", 75 | "name": "julia-0.4" 76 | }, 77 | "language_info": { 78 | "file_extension": ".jl", 79 | "mimetype": "application/julia", 80 | "name": "julia", 81 | "version": "0.4.6" 82 | }, 83 | "widgets": { 84 | "state": {}, 85 | "version": "1.1.2" 86 | } 87 | }, 88 | "nbformat": 4, 89 | "nbformat_minor": 0 90 | } 91 | -------------------------------------------------------------------------------- /homework/homework_1/Homework 1, 18.337 2016.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Homework 1 for 18.337, September 2016\n", 8 | " \n", 9 | "Due before class on **Wednesday 14 September 2016**.\n", 10 | "\n", 11 | "Please submit the homework on Stellar." 12 | ] 13 | }, 14 | { 15 | "cell_type": "markdown", 16 | "metadata": {}, 17 | "source": [ 18 | "### 1. Absorbing random walk" 19 | ] 20 | }, 21 | { 22 | "cell_type": "markdown", 23 | "metadata": {}, 24 | "source": [ 25 | "Consider an MIT beaver doing a simple random walk on the positive integers, starting from site $i=2$. We want to find the time it takes to reach an absorbing site at $i=1$ (called the absorption time, first passage time or hitting time). There is a reflecting wall at site $i=L$:" 26 | ] 27 | }, 28 | { 29 | "cell_type": "markdown", 30 | "metadata": {}, 31 | "source": [ 32 | "" 33 | ] 34 | }, 35 | { 36 | "cell_type": "markdown", 37 | "metadata": {}, 38 | "source": [ 39 | "The transition probabilities in the bulk (i.e. away from the boundary) are $p(i+1 \\, | \\, i) = p(i-1 \\, | \\, i) = \\textstyle \\frac{1}{2}$ for $i = 2, 3, \\ldots$. \n", 40 | "\n", 41 | "[Here, $p(i \\, | \\, j) = p(j \\to i)$ is the probability to jump in one step from site $i$ to site $j$. We have $\\sum_i \\, p(i \\, | \\, j) = 1$ for every $j$.]\n", 42 | "\n", 43 | "We model the absorbing site by saying that the walker cannot leave site $i=1$ once it arrives: $p(1 \\, | \\, 1) = 1$ and $p(i \\, | \\, 1) = 0$ for $i \\neq 1$.\n", 44 | "\n", 45 | "The reflecting site is modelled as $p(L \\, | \\, L) = \\textstyle \\frac{1}{2}$, representing the fact that when jumping to the right from site $L$, it hits a mirror and bounces back to $L$ again." 46 | ] 47 | }, 48 | { 49 | "cell_type": "markdown", 50 | "metadata": {}, 51 | "source": [ 52 | "(1) Write a simple Monte Carlo simulation in Julia of the beaver's trajectory until Julia hits its goal, e.g. for $L=50$. Calculate the mean time over $N=10^3$, say, realizations of this process. Repeat this calculation $M$ times (say, $M = 10^4$) for the same value of $N$. \n", 53 | "\n", 54 | "Draw a histogram of the means. What is its shape?\n", 55 | "\n", 56 | "What does your data strongly suggest is the dependence of the means on the size $L$? What does this imply for $L \\to \\infty$? \n", 57 | "\n", 58 | "**Optional: Find the analytical solution for the mean hitting time as a function of $L$." 59 | ] 60 | }, 61 | { 62 | "cell_type": "markdown", 63 | "metadata": {}, 64 | "source": [ 65 | "(2) To understand this behavior, we need to calculate the complete probability distribution of hitting times. Although this can in principle be done using Monte Carlo (try it!), much better (=more precise) results are obtained using \"exact enumeration\", i.e. by solving the master equation describing the stochastic process in a numerically exact way, as follows.\n", 66 | "\n", 67 | "Let $P_t(i)$ be the probability that the walker is at site $i$ at time $t$.\n", 68 | "Find the equations that govern the time evolution of $P_t(i)$, taking care with the absorbing boundary at $i=0$ and the reflecting boundary at $i=L$. A convenient way to model the absorption is that any probability that has arrived at site $i=0$ remains there. The equations are available [here](Evolution equations.ipynb).\n", 69 | "\n", 70 | "Use these equations to calculate, using Julia, the time evolution of $P_t(i)$ for all sites $i$ and times $t$ from $0$ to, say, $t_\\mathrm{max} = 1000$. [Note that (standard) arrays are indexed starting at 1, not 0.]\n", 71 | "There are various ways to do this, including using [array comprehensions](http://docs.julialang.org/en/release-0.4/manual/arrays/#comprehensions), dense matrices and sparse matrices.\n", 72 | "What to you think will be the most efficient method? Try to implement several different versions.\n", 73 | "\n", 74 | "Work out a test case for a few sites by hand and write a [unit test](http://docs.julialang.org/en/release-0.4/stdlib/test/) to make sure that your code is correct.\n", 75 | "\n", 76 | "Use the results of this calculation to find the probability distribution of hitting times, i.e. the probability that is absorbed at each time $t$. Plot this for different $L$. Conclude what happens as $L \\to \\infty$. What is the asymptotic behavior of the distribution?" 77 | ] 78 | }, 79 | { 80 | "cell_type": "markdown", 81 | "metadata": {}, 82 | "source": [ 83 | "### 2. How fast is Julia?" 84 | ] 85 | }, 86 | { 87 | "cell_type": "markdown", 88 | "metadata": {}, 89 | "source": [ 90 | "(3) Use the [BenchmarkTools.jl](https://github.com/JuliaCI/BenchmarkTools.jl) package to time your code. Make sure to [optimize the code](http://docs.julialang.org/en/release-0.4/manual/performance-tips/) first." 91 | ] 92 | }, 93 | { 94 | "cell_type": "markdown", 95 | "metadata": {}, 96 | "source": [ 97 | "**Optional: If you are proficient at another language (other than Julia), rewrite the simulation code in the other language, and compare the performance, the ease of coding, and the number of lines of code required for the two versions." 98 | ] 99 | }, 100 | { 101 | "cell_type": "markdown", 102 | "metadata": {}, 103 | "source": [ 104 | "### Coming attraction: Parallelization" 105 | ] 106 | }, 107 | { 108 | "cell_type": "markdown", 109 | "metadata": {}, 110 | "source": [ 111 | "**Optional: Think about parallelization methodologies and how could you parallelize your Monte Carlo and exact enumeration codes with Julia." 112 | ] 113 | }, 114 | { 115 | "cell_type": "markdown", 116 | "metadata": {}, 117 | "source": [ 118 | "### 18.337 wisdom" 119 | ] 120 | }, 121 | { 122 | "cell_type": "markdown", 123 | "metadata": {}, 124 | "source": [ 125 | "\"Premature optimization is the root of all evil\". D. Knuth\n", 126 | "\n", 127 | "\"Optimize a serial code before you think about parallelizing it.\" " 128 | ] 129 | } 130 | ], 131 | "metadata": { 132 | "kernelspec": { 133 | "display_name": "Julia 0.4.6", 134 | "language": "julia", 135 | "name": "julia-0.4" 136 | }, 137 | "language_info": { 138 | "file_extension": ".jl", 139 | "mimetype": "application/julia", 140 | "name": "julia", 141 | "version": "0.4.6" 142 | }, 143 | "widgets": { 144 | "state": {}, 145 | "version": "1.1.2" 146 | } 147 | }, 148 | "nbformat": 4, 149 | "nbformat_minor": 0 150 | } 151 | -------------------------------------------------------------------------------- /homework/homework_1/.ipynb_checkpoints/Homework 1, 18.337 2016-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Homework 1 for 18.337, September 2016\n", 8 | " \n", 9 | "Due before class on **Wednesday 14 September 2016**.\n", 10 | "\n", 11 | "Please submit the homework on Stellar." 12 | ] 13 | }, 14 | { 15 | "cell_type": "markdown", 16 | "metadata": {}, 17 | "source": [ 18 | "### 1. Absorbing random walk" 19 | ] 20 | }, 21 | { 22 | "cell_type": "markdown", 23 | "metadata": {}, 24 | "source": [ 25 | "Consider an MIT beaver doing a simple random walk on the positive integers, starting from site $i=2$. We want to find the time it takes to reach an absorbing site at $i=1$ (called the absorption time, first passage time or hitting time). There is a reflecting wall at site $i=L$:" 26 | ] 27 | }, 28 | { 29 | "cell_type": "markdown", 30 | "metadata": {}, 31 | "source": [ 32 | "" 33 | ] 34 | }, 35 | { 36 | "cell_type": "markdown", 37 | "metadata": {}, 38 | "source": [ 39 | "The transition probabilities in the bulk (i.e. away from the boundary) are $p(i+1 \\, | \\, i) = p(i-1 \\, | \\, i) = \\textstyle \\frac{1}{2}$ for $i = 2, 3, \\ldots$. \n", 40 | "\n", 41 | "[Here, $p(i \\, | \\, j) = p(j \\to i)$ is the probability to jump in one step from site $i$ to site $j$. We have $\\sum_i \\, p(i \\, | \\, j) = 1$ for every $j$.]\n", 42 | "\n", 43 | "We model the absorbing site by saying that the walker cannot leave site $i=1$ once it arrives: $p(1 \\, | \\, 1) = 1$ and $p(i \\, | \\, 1) = 0$ for $i \\neq 1$.\n", 44 | "\n", 45 | "The reflecting site is modelled as $p(L \\, | \\, L) = \\textstyle \\frac{1}{2}$, representing the fact that when jumping to the right from site $L$, it hits a mirror and bounces back to $L$ again." 46 | ] 47 | }, 48 | { 49 | "cell_type": "markdown", 50 | "metadata": {}, 51 | "source": [ 52 | "(1) Write a simple Monte Carlo simulation in Julia of the beaver's trajectory until Julia hits its goal, e.g. for $L=50$. Calculate the mean time over $N=10^3$, say, realizations of this process. Repeat this calculation $M$ times (say, $M = 10^4$) for the same value of $N$. \n", 53 | "\n", 54 | "Draw a histogram of the means. What is its shape?\n", 55 | "\n", 56 | "What does your data strongly suggest is the dependence of the means on the size $L$? What does this imply for $L \\to \\infty$? \n", 57 | "\n", 58 | "**Optional: Find the analytical solution for the mean hitting time as a function of $L$." 59 | ] 60 | }, 61 | { 62 | "cell_type": "markdown", 63 | "metadata": {}, 64 | "source": [ 65 | "(2) To understand this behavior, we need to calculate the complete probability distribution of hitting times. Although this can in principle be done using Monte Carlo (try it!), much better (=more precise) results are obtained using \"exact enumeration\", i.e. by solving the master equation describing the stochastic process in a numerically exact way, as follows.\n", 66 | "\n", 67 | "Let $P_t(i)$ be the probability that the walker is at site $i$ at time $t$.\n", 68 | "Find the equations that govern the time evolution of $P_t(i)$, taking care with the absorbing boundary at $i=0$ and the reflecting boundary at $i=L$. A convenient way to model the absorption is that any probability that has arrived at site $i=0$ remains there. The equations are available [here](Evolution equations.ipynb).\n", 69 | "\n", 70 | "Use these equations to calculate, using Julia, the time evolution of $P_t(i)$ for all sites $i$ and times $t$ from $0$ to, say, $t_\\mathrm{max} = 1000$. [Note that (standard) arrays are indexed starting at 1, not 0.]\n", 71 | "There are various ways to do this, including using [array comprehensions](http://docs.julialang.org/en/release-0.4/manual/arrays/#comprehensions), dense matrices and sparse matrices.\n", 72 | "What to you think will be the most efficient method? Try to implement several different versions.\n", 73 | "\n", 74 | "Work out a test case for a few sites by hand and write a [unit test](http://docs.julialang.org/en/release-0.4/stdlib/test/) to make sure that your code is correct.\n", 75 | "\n", 76 | "Use the results of this calculation to find the probability distribution of hitting times, i.e. the probability that is absorbed at each time $t$. Plot this for different $L$. Conclude what happens as $L \\to \\infty$. What is the asymptotic behavior of the distribution?" 77 | ] 78 | }, 79 | { 80 | "cell_type": "markdown", 81 | "metadata": {}, 82 | "source": [ 83 | "### 2. How fast is Julia?" 84 | ] 85 | }, 86 | { 87 | "cell_type": "markdown", 88 | "metadata": {}, 89 | "source": [ 90 | "(3) Use the [BenchmarkTools.jl](https://github.com/JuliaCI/BenchmarkTools.jl) package to time your code. Make sure to [optimize the code](http://docs.julialang.org/en/release-0.4/manual/performance-tips/) first." 91 | ] 92 | }, 93 | { 94 | "cell_type": "markdown", 95 | "metadata": {}, 96 | "source": [ 97 | "**Optional: If you are proficient at another language (other than Julia), rewrite the simulation code in the other language, and compare the performance, the ease of coding, and the number of lines of code required for the two versions." 98 | ] 99 | }, 100 | { 101 | "cell_type": "markdown", 102 | "metadata": {}, 103 | "source": [ 104 | "### Coming attraction: Parallelization" 105 | ] 106 | }, 107 | { 108 | "cell_type": "markdown", 109 | "metadata": {}, 110 | "source": [ 111 | "**Optional: Think about parallelization methodologies and how could you parallelize your Monte Carlo and exact enumeration codes with Julia." 112 | ] 113 | }, 114 | { 115 | "cell_type": "markdown", 116 | "metadata": {}, 117 | "source": [ 118 | "### 18.337 wisdom" 119 | ] 120 | }, 121 | { 122 | "cell_type": "markdown", 123 | "metadata": {}, 124 | "source": [ 125 | "\"Premature optimization is the root of all evil\". D. Knuth\n", 126 | "\n", 127 | "\"Optimize a serial code before you think about parallelizing it.\" " 128 | ] 129 | } 130 | ], 131 | "metadata": { 132 | "kernelspec": { 133 | "display_name": "Julia 0.4.6", 134 | "language": "julia", 135 | "name": "julia-0.4" 136 | }, 137 | "language_info": { 138 | "file_extension": ".jl", 139 | "mimetype": "application/julia", 140 | "name": "julia", 141 | "version": "0.4.6" 142 | }, 143 | "widgets": { 144 | "state": {}, 145 | "version": "1.1.2" 146 | } 147 | }, 148 | "nbformat": 4, 149 | "nbformat_minor": 0 150 | } 151 | -------------------------------------------------------------------------------- /homework/homework_1/.ipynb_checkpoints/Absorbing random walk-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "Absorbing random walk" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "Global scope:" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 4, 20 | "metadata": { 21 | "collapsed": false 22 | }, 23 | "outputs": [ 24 | { 25 | "name": "stdout", 26 | "output_type": "stream", 27 | "text": [ 28 | " 10.432508 seconds (153.74 M allocations: 2.291 GB, 1.01% gc time)\n" 29 | ] 30 | } 31 | ], 32 | "source": [ 33 | "x = 1\n", 34 | "\n", 35 | "times = Int64[]\n", 36 | "\n", 37 | "@time begin\n", 38 | "for i in 1:10000\n", 39 | " t = 0\n", 40 | " x = 1\n", 41 | " while x != 0\n", 42 | " if rand() < 0.5\n", 43 | " x += 1\n", 44 | " else\n", 45 | " x -= 1\n", 46 | " end\n", 47 | " \n", 48 | " t += 1\n", 49 | " end\n", 50 | " \n", 51 | " push!(times, t)\n", 52 | "end\n", 53 | "end" 54 | ] 55 | }, 56 | { 57 | "cell_type": "markdown", 58 | "metadata": {}, 59 | "source": [ 60 | "Wrap in functions:" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": 6, 66 | "metadata": { 67 | "collapsed": false 68 | }, 69 | "outputs": [ 70 | { 71 | "data": { 72 | "text/plain": [ 73 | "run (generic function with 1 method)" 74 | ] 75 | }, 76 | "execution_count": 6, 77 | "metadata": {}, 78 | "output_type": "execute_result" 79 | } 80 | ], 81 | "source": [ 82 | "function walk()\n", 83 | " t = 0\n", 84 | " x = 1\n", 85 | " \n", 86 | " while x != 0\n", 87 | " if rand() < 0.5\n", 88 | " x += 1\n", 89 | " else\n", 90 | " x -= 1\n", 91 | " end\n", 92 | " \n", 93 | " t += 1\n", 94 | " end\n", 95 | " \n", 96 | " # alternative: x += 2*(rand() < 0.5) - 1\n", 97 | " \n", 98 | " return t\n", 99 | "\n", 100 | "end\n", 101 | "\n", 102 | "function run(N)\n", 103 | " times = Int64[]\n", 104 | " for i in 1:N\n", 105 | " push!(times, walk())\n", 106 | " end\n", 107 | " \n", 108 | " return times\n", 109 | "end\n" 110 | ] 111 | }, 112 | { 113 | "cell_type": "code", 114 | "execution_count": 7, 115 | "metadata": { 116 | "collapsed": false 117 | }, 118 | "outputs": [ 119 | { 120 | "data": { 121 | "text/plain": [ 122 | "10-element Array{Int64,1}:\n", 123 | " 19\n", 124 | " 17\n", 125 | " 1\n", 126 | " 85\n", 127 | " 3\n", 128 | " 965\n", 129 | " 49\n", 130 | " 19\n", 131 | " 1\n", 132 | " 1" 133 | ] 134 | }, 135 | "execution_count": 7, 136 | "metadata": {}, 137 | "output_type": "execute_result" 138 | } 139 | ], 140 | "source": [ 141 | "run(10)" 142 | ] 143 | }, 144 | { 145 | "cell_type": "code", 146 | "execution_count": 19, 147 | "metadata": { 148 | "collapsed": false 149 | }, 150 | "outputs": [ 151 | { 152 | "name": "stdout", 153 | "output_type": "stream", 154 | "text": [ 155 | " 0.000056 seconds (11 allocations: 2.422 KB)\n" 156 | ] 157 | }, 158 | { 159 | "data": { 160 | "text/plain": [ 161 | "51.92" 162 | ] 163 | }, 164 | "execution_count": 19, 165 | "metadata": {}, 166 | "output_type": "execute_result" 167 | } 168 | ], 169 | "source": [ 170 | "times = @time run(100)\n", 171 | "mean(times)" 172 | ] 173 | }, 174 | { 175 | "cell_type": "markdown", 176 | "metadata": {}, 177 | "source": [ 178 | "Add boundary:" 179 | ] 180 | }, 181 | { 182 | "cell_type": "code", 183 | "execution_count": 96, 184 | "metadata": { 185 | "collapsed": false 186 | }, 187 | "outputs": [ 188 | { 189 | "name": "stderr", 190 | "output_type": "stream", 191 | "text": [ 192 | "WARNING: Method definition walk(Any) in module Main at In[95]:2 overwritten at In[96]:2.\n", 193 | "WARNING: Method definition run(Any, Any) in module Main at In[95]:28 overwritten at In[96]:28.\n" 194 | ] 195 | }, 196 | { 197 | "data": { 198 | "text/plain": [ 199 | "run (generic function with 2 methods)" 200 | ] 201 | }, 202 | "execution_count": 96, 203 | "metadata": {}, 204 | "output_type": "execute_result" 205 | } 206 | ], 207 | "source": [ 208 | "function walk(L)\n", 209 | " t = 0\n", 210 | " x = 1\n", 211 | " \n", 212 | " while x != 0\n", 213 | " if rand() < 0.5\n", 214 | " x += 1\n", 215 | " else\n", 216 | " x -= 1\n", 217 | " end\n", 218 | " \n", 219 | " # alternative: x += 2*(rand() < 0.5) - 1\n", 220 | "\n", 221 | " \n", 222 | " if x > L \n", 223 | " x = L # bounce back\n", 224 | " end\n", 225 | " \n", 226 | " t += 1\n", 227 | " end\n", 228 | " \n", 229 | " \n", 230 | " return t\n", 231 | "\n", 232 | "end\n", 233 | "\n", 234 | "function run(L, N)\n", 235 | " return [walk(L) for i in 1:N]\n", 236 | "end\n" 237 | ] 238 | }, 239 | { 240 | "cell_type": "code", 241 | "execution_count": 126, 242 | "metadata": { 243 | "collapsed": false 244 | }, 245 | "outputs": [ 246 | { 247 | "name": "stdout", 248 | "output_type": "stream", 249 | "text": [ 250 | " 0.000204 seconds (7 allocations: 8.125 KB)\n" 251 | ] 252 | }, 253 | { 254 | "data": { 255 | "text/plain": [ 256 | "20.803" 257 | ] 258 | }, 259 | "execution_count": 126, 260 | "metadata": {}, 261 | "output_type": "execute_result" 262 | } 263 | ], 264 | "source": [ 265 | "@time mean(run(10, 1000))" 266 | ] 267 | }, 268 | { 269 | "cell_type": "code", 270 | "execution_count": null, 271 | "metadata": { 272 | "collapsed": true 273 | }, 274 | "outputs": [], 275 | "source": [] 276 | }, 277 | { 278 | "cell_type": "markdown", 279 | "metadata": { 280 | "collapsed": true 281 | }, 282 | "source": [ 283 | "## Master equation" 284 | ] 285 | }, 286 | { 287 | "cell_type": "code", 288 | "execution_count": 20, 289 | "metadata": { 290 | "collapsed": false 291 | }, 292 | "outputs": [ 293 | { 294 | "name": "stderr", 295 | "output_type": "stream", 296 | "text": [ 297 | "WARNING: Method definition evolve(Any) in module Main at In[15]:1 overwritten at In[20]:1.\n" 298 | ] 299 | }, 300 | { 301 | "data": { 302 | "text/plain": [ 303 | "evolve (generic function with 1 method)" 304 | ] 305 | }, 306 | "execution_count": 20, 307 | "metadata": {}, 308 | "output_type": "execute_result" 309 | } 310 | ], 311 | "source": [ 312 | "evolve(p) = (L = length(p);\n", 313 | " [p[1] + p[2]/2; p[3]/2; \n", 314 | " [ (p[i-1] + p[i+1]) / 2 for i in 3:L-1]; \n", 315 | " (p[L-1] + p[L]) / 2]\n", 316 | " )" 317 | ] 318 | }, 319 | { 320 | "cell_type": "code", 321 | "execution_count": 29, 322 | "metadata": { 323 | "collapsed": false 324 | }, 325 | "outputs": [ 326 | { 327 | "data": { 328 | "text/plain": [ 329 | "10-element Array{Rational{Int64},1}:\n", 330 | " 0//1\n", 331 | " 1//1\n", 332 | " 0//1\n", 333 | " 0//1\n", 334 | " 0//1\n", 335 | " 0//1\n", 336 | " 0//1\n", 337 | " 0//1\n", 338 | " 0//1\n", 339 | " 0//1" 340 | ] 341 | }, 342 | "execution_count": 29, 343 | "metadata": {}, 344 | "output_type": "execute_result" 345 | } 346 | ], 347 | "source": [ 348 | "L = 10\n", 349 | "p = [i == 2 ? 1//1 : 0//1 for i in 1:L]" 350 | ] 351 | }, 352 | { 353 | "cell_type": "code", 354 | "execution_count": 22, 355 | "metadata": { 356 | "collapsed": false 357 | }, 358 | "outputs": [ 359 | { 360 | "data": { 361 | "text/plain": [ 362 | "10-element Array{Rational{Int64},1}:\n", 363 | " 1//2\n", 364 | " 0//1\n", 365 | " 1//2\n", 366 | " 0//1\n", 367 | " 0//1\n", 368 | " 0//1\n", 369 | " 0//1\n", 370 | " 0//1\n", 371 | " 0//1\n", 372 | " 0//1" 373 | ] 374 | }, 375 | "execution_count": 22, 376 | "metadata": {}, 377 | "output_type": "execute_result" 378 | } 379 | ], 380 | "source": [ 381 | "evolve(p)" 382 | ] 383 | }, 384 | { 385 | "cell_type": "code", 386 | "execution_count": 23, 387 | "metadata": { 388 | "collapsed": false 389 | }, 390 | "outputs": [ 391 | { 392 | "data": { 393 | "text/plain": [ 394 | "10-element Array{Rational{Int64},1}:\n", 395 | " 1//2\n", 396 | " 1//4\n", 397 | " 0//1\n", 398 | " 1//4\n", 399 | " 0//1\n", 400 | " 0//1\n", 401 | " 0//1\n", 402 | " 0//1\n", 403 | " 0//1\n", 404 | " 0//1" 405 | ] 406 | }, 407 | "execution_count": 23, 408 | "metadata": {}, 409 | "output_type": "execute_result" 410 | } 411 | ], 412 | "source": [ 413 | "evolve(evolve(p))" 414 | ] 415 | }, 416 | { 417 | "cell_type": "code", 418 | "execution_count": 24, 419 | "metadata": { 420 | "collapsed": false 421 | }, 422 | "outputs": [ 423 | { 424 | "data": { 425 | "text/plain": [ 426 | "10-element Array{Rational{Int64},1}:\n", 427 | " 5//8\n", 428 | " 0//1\n", 429 | " 1//4\n", 430 | " 0//1\n", 431 | " 1//8\n", 432 | " 0//1\n", 433 | " 0//1\n", 434 | " 0//1\n", 435 | " 0//1\n", 436 | " 0//1" 437 | ] 438 | }, 439 | "execution_count": 24, 440 | "metadata": {}, 441 | "output_type": "execute_result" 442 | } 443 | ], 444 | "source": [ 445 | "evolve(evolve(evolve(p)))" 446 | ] 447 | }, 448 | { 449 | "cell_type": "markdown", 450 | "metadata": {}, 451 | "source": [ 452 | "## Towards more efficiency\n", 453 | "\n", 454 | "And more readable:" 455 | ] 456 | }, 457 | { 458 | "cell_type": "code", 459 | "execution_count": 27, 460 | "metadata": { 461 | "collapsed": false 462 | }, 463 | "outputs": [ 464 | { 465 | "data": { 466 | "text/plain": [ 467 | "evolve2 (generic function with 1 method)" 468 | ] 469 | }, 470 | "execution_count": 27, 471 | "metadata": {}, 472 | "output_type": "execute_result" 473 | } 474 | ], 475 | "source": [ 476 | "function evolve2(p)\n", 477 | " p_new = zeros(p)\n", 478 | " \n", 479 | " p_new[1] = p[1] + p[2]/2\n", 480 | " p_new[2] = p[3] / 2\n", 481 | " \n", 482 | " for i in 3:L-1\n", 483 | " p_new = (p[i-1] + p[i+1]) / 2 \n", 484 | " end\n", 485 | " \n", 486 | " p_new[L] = (p[L-1] + p[L]) / 2\n", 487 | "end" 488 | ] 489 | }, 490 | { 491 | "cell_type": "markdown", 492 | "metadata": {}, 493 | "source": [ 494 | "Most efficient: Pre-allocate so remove memory allocation at each step:" 495 | ] 496 | }, 497 | { 498 | "cell_type": "code", 499 | "execution_count": 31, 500 | "metadata": { 501 | "collapsed": false 502 | }, 503 | "outputs": [ 504 | { 505 | "name": "stderr", 506 | "output_type": "stream", 507 | "text": [ 508 | "WARNING: Method definition evolve2(Any, Any) in module Main at In[28]:3 overwritten at In[31]:3.\n" 509 | ] 510 | }, 511 | { 512 | "data": { 513 | "text/plain": [ 514 | "evolve2 (generic function with 2 methods)" 515 | ] 516 | }, 517 | "execution_count": 31, 518 | "metadata": {}, 519 | "output_type": "execute_result" 520 | } 521 | ], 522 | "source": [ 523 | "function evolve2(p, p_new)\n", 524 | " \n", 525 | " p_new[1] = p[1] + p[2]/2\n", 526 | " p_new[2] = p[3] / 2\n", 527 | " \n", 528 | " for i in 3:L-1\n", 529 | " p_new[i] = (p[i-1] + p[i+1]) / 2 \n", 530 | " end\n", 531 | " \n", 532 | " p_new[L] = (p[L-1] + p[L]) / 2\n", 533 | "end" 534 | ] 535 | }, 536 | { 537 | "cell_type": "code", 538 | "execution_count": 34, 539 | "metadata": { 540 | "collapsed": false 541 | }, 542 | "outputs": [ 543 | { 544 | "data": { 545 | "text/plain": [ 546 | "([0.5,0.0,0.5,0.0,0.0,0.0,0.0,0.0,0.0,0.0],[0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0])" 547 | ] 548 | }, 549 | "execution_count": 34, 550 | "metadata": {}, 551 | "output_type": "execute_result" 552 | } 553 | ], 554 | "source": [ 555 | "L = 10\n", 556 | "p = zeros(L); p[2] = 1.0\n", 557 | "p_new = zeros(L)\n", 558 | "\n", 559 | "evolve2(p, p_new)\n", 560 | "p, p_new = p_new, p # swap" 561 | ] 562 | }, 563 | { 564 | "cell_type": "code", 565 | "execution_count": 35, 566 | "metadata": { 567 | "collapsed": false 568 | }, 569 | "outputs": [ 570 | { 571 | "data": { 572 | "text/plain": [ 573 | "10-element Array{Float64,1}:\n", 574 | " 0.5\n", 575 | " 0.0\n", 576 | " 0.5\n", 577 | " 0.0\n", 578 | " 0.0\n", 579 | " 0.0\n", 580 | " 0.0\n", 581 | " 0.0\n", 582 | " 0.0\n", 583 | " 0.0" 584 | ] 585 | }, 586 | "execution_count": 35, 587 | "metadata": {}, 588 | "output_type": "execute_result" 589 | } 590 | ], 591 | "source": [ 592 | "p" 593 | ] 594 | }, 595 | { 596 | "cell_type": "code", 597 | "execution_count": 38, 598 | "metadata": { 599 | "collapsed": false 600 | }, 601 | "outputs": [ 602 | { 603 | "data": { 604 | "text/plain": [ 605 | "([0.625,0.0,0.25,0.0,0.125,0.0,0.0,0.0,0.0,0.0],[0.5,0.25,0.0,0.25,0.0,0.0,0.0,0.0,0.0,0.0])" 606 | ] 607 | }, 608 | "execution_count": 38, 609 | "metadata": {}, 610 | "output_type": "execute_result" 611 | } 612 | ], 613 | "source": [ 614 | "evolve2(p, p_new)\n", 615 | "p, p_new = p_new, p # swap" 616 | ] 617 | }, 618 | { 619 | "cell_type": "code", 620 | "execution_count": 39, 621 | "metadata": { 622 | "collapsed": false 623 | }, 624 | "outputs": [ 625 | { 626 | "data": { 627 | "text/plain": [ 628 | "([0.625,0.125,0.0,0.1875,0.0,0.0625,0.0,0.0,0.0,0.0],[0.625,0.0,0.25,0.0,0.125,0.0,0.0,0.0,0.0,0.0])" 629 | ] 630 | }, 631 | "execution_count": 39, 632 | "metadata": {}, 633 | "output_type": "execute_result" 634 | } 635 | ], 636 | "source": [ 637 | "evolve2(p, p_new)\n", 638 | "p, p_new = p_new, p # swap" 639 | ] 640 | }, 641 | { 642 | "cell_type": "code", 643 | "execution_count": 40, 644 | "metadata": { 645 | "collapsed": false 646 | }, 647 | "outputs": [ 648 | { 649 | "data": { 650 | "text/plain": [ 651 | "([0.6875,0.0,0.15625,0.0,0.125,0.0,0.03125,0.0,0.0,0.0],[0.625,0.125,0.0,0.1875,0.0,0.0625,0.0,0.0,0.0,0.0])" 652 | ] 653 | }, 654 | "execution_count": 40, 655 | "metadata": {}, 656 | "output_type": "execute_result" 657 | } 658 | ], 659 | "source": [ 660 | "evolve2(p, p_new)\n", 661 | "p, p_new = p_new, p # swap" 662 | ] 663 | }, 664 | { 665 | "cell_type": "code", 666 | "execution_count": null, 667 | "metadata": { 668 | "collapsed": true 669 | }, 670 | "outputs": [], 671 | "source": [] 672 | } 673 | ], 674 | "metadata": { 675 | "kernelspec": { 676 | "display_name": "Julia 0.5.0-rc3", 677 | "language": "julia", 678 | "name": "julia-0.5" 679 | }, 680 | "language_info": { 681 | "file_extension": ".jl", 682 | "mimetype": "application/julia", 683 | "name": "julia", 684 | "version": "0.5.0" 685 | }, 686 | "widgets": { 687 | "state": {}, 688 | "version": "1.1.2" 689 | } 690 | }, 691 | "nbformat": 4, 692 | "nbformat_minor": 0 693 | } 694 | --------------------------------------------------------------------------------