In [1]:
function walk(num_steps)
    
    x = 0
    
    for i in 1:num_steps
        x += rand( (-1, +1) )
    end
    
    return x
end
Out[1]:
walk (generic function with 1 method)
In [2]:
num_steps = 20
walk(num_steps)
Out[2]:
0
In [3]:
experiment(num_steps, num_walks) = [walk(num_steps) for i in 1:num_walks]
Out[3]:
experiment (generic function with 1 method)
In [17]:
data = experiment(20, 1000)

data'   # transpose(data) --  ' = transpose
Out[17]:
1×1000 LinearAlgebra.Adjoint{Int64,Array{Int64,1}}:
 2  -2  -10  -4  12  2  0  2  2  -2  4  …  4  -4  6  2  -2  2  4  0  0  2  -4
In [18]:
using StatsBase
In [20]:
counts = countmap(data);
In [4]:
using Plots
In [29]:
histogram(data, bins=10)  # in Plots.jl package
xlabel!("position x")
Out[29]:
-20 -10 0 10 0 100 200 300 400 position x y1
In [30]:
using StatsBase
In [31]:
h = fit(Histogram, data)
Out[31]:
Histogram{Int64,1,Tuple{StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}}}
edges:
  -20.0:5.0:15.0
weights: [1, 8, 109, 296, 438, 121, 27]
closed: left
isdensity: false
In [33]:
h.weights / sum(h.weights)
Out[33]:
7-element Array{Float64,1}:
 0.001
 0.008
 0.109
 0.296
 0.438
 0.121
 0.027
In [35]:
histogram(data, bins=30, normed=true)  # in Plots.jl package
Out[35]:
-15 -10 -5 0 5 10 0.00 0.05 0.10 0.15 y1
In [36]:
data
Out[36]:
1000-element Array{Int64,1}:
   2
  -2
 -10
  -4
  12
   2
   0
   2
   2
  -2
   4
   0
  -6
   ⋮
   2
   4
  -4
   6
   2
  -2
   2
   4
   0
   0
   2
  -4
In [37]:
mean(data)
Out[37]:
0.116
In [38]:
sum(data) / length(data)
Out[38]:
0.116

Spread away from mean

In [40]:
histogram(data, normed=true, bins=30)
Out[40]:
-15 -10 -5 0 5 10 0.00 0.05 0.10 0.15 y1
In [43]:
vline!([mean(data)], lw=3, ls=:dash, leg=false, c=:purple)
Out[43]:
-15 -10 -5 0 5 10 0.00 0.05 0.10 0.15
In [46]:
scatter(data)

hline!([mean(data)], lw=3, ls=:dash, c=:purple)
Out[46]:
0 250 500 750 1000 -15 -10 -5 0 5 10 y1 y2
In [49]:
m = mean(data)   # de-mean:  subtract:
centred_data = data .- m;
In [50]:
scatter(centred_data)

hline!([mean(centred_data)], lw=3, ls=:dash, c=:purple)
Out[50]:
0 250 500 750 1000 -15 -10 -5 0 5 10 y1 y2
In [51]:
mean(centred_data)
Out[51]:
2.9665159217984184e-16

1e-16 is notation for $10^{-16}$

In [52]:
nextfloat(1.0)
Out[52]:
1.0000000000000002
In [53]:
nextfloat(1.0) - 1.0   # machine epsilon for double precision -- Float64
Out[53]:
2.220446049250313e-16
In [54]:
abs_data = abs.(centred_data)
Out[54]:
1000-element Array{Float64,1}:
  1.884
  2.116
 10.116
  4.116
 11.884
  1.884
  0.116
  1.884
  1.884
  2.116
  3.884
  0.116
  6.116
  ⋮
  1.884
  3.884
  4.116
  5.884
  1.884
  2.116
  1.884
  3.884
  0.116
  0.116
  1.884
  4.116
In [55]:
scatter(abs_data)
Out[55]:
0 250 500 750 1000 0 5 10 15 y1
In [56]:
mean(abs_data)
Out[56]:
3.595024
In [61]:
scatter(abs_data, alpha=0.2)

hline!([mean(abs_data)], lw=3, ls=:dash, c=:purple)
Out[61]:
0 250 500 750 1000 0 5 10 15 y1 y2
In [64]:
m = mean(abs_data)

count(abs_data .< m) / length(abs_data)
Out[64]:
0.491
In [65]:
count(abs_data .< 2m) / length(abs_data)    # 2m instead of 2*m
Out[65]:
0.889
In [66]:
scatter(abs_data, alpha=0.2)

hline!([2*mean(abs_data)], lw=3, ls=:dash, c=:purple)
Out[66]:
0 250 500 750 1000 0 5 10 15 y1 y2
In [68]:
squared_data = data.^2;
In [71]:
scatter(squared_data, alpha=0.2)
Out[71]:
0 250 500 750 1000 0 50 100 150 200 250