{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"walk (generic function with 1 method)"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"function walk(num_steps)\n",
" \n",
" x = 0\n",
" \n",
" for i in 1:num_steps\n",
" x += rand( (-1, +1) )\n",
" end\n",
" \n",
" return x\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"num_steps = 20\n",
"walk(num_steps)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"experiment (generic function with 1 method)"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"experiment(num_steps, num_walks) = [walk(num_steps) for i in 1:num_walks]"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1×1000 LinearAlgebra.Adjoint{Int64,Array{Int64,1}}:\n",
" 2 -2 -10 -4 12 2 0 2 2 -2 4 … 4 -4 6 2 -2 2 4 0 0 2 -4"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data = experiment(20, 1000)\n",
"\n",
"data' # transpose(data) -- ' = transpose"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"using StatsBase"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"counts = countmap(data);"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"using Plots"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"histogram(data, bins=10) # in Plots.jl package\n",
"xlabel!(\"position x\")"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [],
"source": [
"using StatsBase"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Histogram{Int64,1,Tuple{StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}}}\n",
"edges:\n",
" -20.0:5.0:15.0\n",
"weights: [1, 8, 109, 296, 438, 121, 27]\n",
"closed: left\n",
"isdensity: false"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"h = fit(Histogram, data)"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"7-element Array{Float64,1}:\n",
" 0.001\n",
" 0.008\n",
" 0.109\n",
" 0.296\n",
" 0.438\n",
" 0.121\n",
" 0.027"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"h.weights / sum(h.weights)"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"histogram(data, bins=30, normed=true) # in Plots.jl package\n"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1000-element Array{Int64,1}:\n",
" 2\n",
" -2\n",
" -10\n",
" -4\n",
" 12\n",
" 2\n",
" 0\n",
" 2\n",
" 2\n",
" -2\n",
" 4\n",
" 0\n",
" -6\n",
" ⋮\n",
" 2\n",
" 4\n",
" -4\n",
" 6\n",
" 2\n",
" -2\n",
" 2\n",
" 4\n",
" 0\n",
" 0\n",
" 2\n",
" -4"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.116"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mean(data)"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.116"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sum(data) / length(data)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Spread away from mean"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
]
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"histogram(data, normed=true, bins=30)"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
]
},
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"vline!([mean(data)], lw=3, ls=:dash, leg=false, c=:purple)"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"