Random walks

Short function definition (one-liner):

In [3]:
jump() = rand( (-1, +1) )
Out[3]:
jump (generic function with 1 method)

Exactly the same:

In [4]:
function jump()
    return rand( (-1, +1) )
end
Out[4]:
jump (generic function with 1 method)
In [5]:
jump
Out[5]:
jump (generic function with 1 method)
In [6]:
jump()
Out[6]:
1
In [7]:
rand( [-1, +1] )
Out[7]:
1
In [8]:
t = (-1, +1)
Out[8]:
(-1, 1)
In [9]:
typeof(t)
Out[9]:
Tuple{Int64,Int64}
In [10]:
t[1] = 10
MethodError: no method matching setindex!(::Tuple{Int64,Int64}, ::Int64, ::Int64)

Stacktrace:
 [1] top-level scope at In[10]:1

Side remark: Simple bernoulli function:

In [11]:
bernoulli(p) = rand() < p
Out[11]:
bernoulli (generic function with 1 method)
In [12]:
bernoulli(0.25)
Out[12]:
false
In [13]:
jump()
Out[13]:
-1

Random walks

In [14]:
[jump() for i in 1:10]
Out[14]:
10-element Array{Int64,1}:
  1
 -1
  1
  1
  1
  1
 -1
  1
 -1
  1
In [15]:
function walk(n)
    x = 0
    
    for i in 1:n
        x += jump()   # x = x + jump()
    end
    
    return x
end
Out[15]:
walk (generic function with 1 method)
In [16]:
walk(10)
Out[16]:
2
In [17]:
function trajectory(n)
    x = 0
    
    xs = [x]
    
    for i in 1:n
        x += jump()   # x = x + jump()
    end
    
    return x
end
Out[17]:
trajectory (generic function with 1 method)
In [18]:
v = []
Out[18]:
0-element Array{Any,1}
In [19]:
push!(v, 10)
Out[19]:
1-element Array{Any,1}:
 10
In [20]:
v
Out[20]:
1-element Array{Any,1}:
 10
In [21]:
push!(v, "hellohellohellohellohellohello", 20)
Out[21]:
3-element Array{Any,1}:
 10
   "hellohellohellohellohellohello"
 20
In [22]:
v = Int64[]
Out[22]:
0-element Array{Int64,1}
In [23]:
push!(v, 10)
Out[23]:
1-element Array{Int64,1}:
 10
In [24]:
push!(v, "hello")
MethodError: Cannot `convert` an object of type String to an object of type Int64
Closest candidates are:
  convert(::Type{T}, !Matched::T) where T<:Number at number.jl:6
  convert(::Type{T}, !Matched::Number) where T<:Number at number.jl:7
  convert(::Type{T}, !Matched::Ptr) where T<:Integer at pointer.jl:23
  ...

Stacktrace:
 [1] push!(::Array{Int64,1}, ::String) at ./array.jl:912
 [2] top-level scope at In[24]:1
In [25]:
function trajectory(n)
    x = 0
    
    xs = [x]  # try to initialize array with an initial value; 
                # determines the element type of array  
    for i in 1:n
        x += jump()   # x = x + jump()
        
        push!(xs, x)
    end
    
    return xs
end
Out[25]:
trajectory (generic function with 1 method)
In [26]:
v = [10]
Out[26]:
1-element Array{Int64,1}:
 10
In [27]:
traj = trajectory(100);

Plotting

In [28]:
plot(traj)
UndefVarError: plot not defined

Stacktrace:
 [1] top-level scope at In[28]:1
In [29]:
using Plots
In [30]:
traj = trajectory(100);
plot(traj, m=:o)
Out[30]:
0 25 50 75 100 0 2 4 6 8 10 10 y1
In [31]:
traj = trajectory(100);
plot(traj, m=:o)
Out[31]:
0 25 50 75 100 0.0 2.5 5.0 7.5 10.0 y1
In [32]:
traj = trajectory(100);
plot(traj, m=:o, size=(300, 200))

hline!([0], ls=:dash)  # ! functions add to the existing plot
Out[32]:
0 25 50 75 100 0 5 10 15 y1 y2
In [38]:
num_walkers = 1000
num_steps = 100

p = plot(size=(500, 400), leg=false)

for i in 1:num_walkers
    traj = trajectory(num_steps)
    
    plot!(traj)
end

p
Out[38]:
0 25 50 75 100 -30 -20 -10 0 10 20 30