using Turing
using StatsPlots
using DataFrames
using Logging
using CSV
# setting default attributes for plots
default(labels=false)
Code 0.1
println("All models are wrong, but some are useful!")
All models are wrong, but some are useful!
Code 0.2
x = range(1, 2, length=2)
x = x .* 10 .|> log |> sum |> exp
x
200.0000000000001
Code 0.3
[
log(0.01^200);
200 * log(0.01)
]
2-element Vector{Float64}: -Inf -921.0340371976182
Code 0.4
# load data
df = DataFrame(CSV.File("data/cars.csv"));
# fit a linear regression on distance on speed
@model function speed_dist(speed, dist)
a ~ Uniform(-20, 20)
b ~ Uniform(1, 5)
m = a .+ b * speed
dist ~ MvNormal(m, 1)
end
Logging.disable_logging(Logging.Warn)
chain = sample(speed_dist(df.speed, df.dist), NUTS(0.65), 1000)
display(chain)
# extract mean params
mean_a, mean_b = mean(chain)[:,:mean]
# get residuals of the data
resid = df.dist - (mean_a .+ mean_b * df.speed);
scatter(df.speed, resid; xlabel = "Speed", ylabel = "Model residual")
Chains MCMC chain (1000×14×1 Array{Float64, 3}): Iterations = 501:1:1500 Number of chains = 1 Samples per chain = 1000 Wall duration = 7.43 seconds Compute duration = 7.43 seconds parameters = a, b internals = lp, n_steps, is_accept, acceptance_rate, log_density, hamiltonian_energy, hamiltonian_energy_error, max_hamiltonian_energy_error, tree_depth, numerical_error, step_size, nom_step_size Summary Statistics parameters mean std naive_se mcse ess rhat ⋯ Symbol Float64 Float64 Float64 Float64 Float64 Float64 ⋯ a -17.5744 0.4365 0.0138 0.0254 198.9546 1.0002 ⋯ b 3.9324 0.0268 0.0008 0.0016 187.5408 0.9991 ⋯ 1 column omitted Quantiles parameters 2.5% 25.0% 50.0% 75.0% 97.5% Symbol Float64 Float64 Float64 Float64 Float64 a -18.4492 -17.8596 -17.5499 -17.2814 -16.7692 b 3.8828 3.9140 3.9307 3.9527 3.9861
Code 0.5
import Pkg
# this list of dependencies might be incomplete, as I'm not maintaining it.
# please check Project.toml for the full list
Pkg.add(["Turing", "CSV", "DataFrames", "Plots", "StatsPlots", "StatisticalRethinking"])
Updating registry at `~/.julia/registries/General` Updating git-repo `https://github.com/JuliaRegistries/General.git` Resolving package versions... Updating `~/work/rethinking/rethinking-2ed-julia/Project.toml` [2d09df54] ~ StatisticalRethinking v4.4.3 `https://github.com/StatisticalRethinkingJulia/StatisticalRethinking.jl#master` ⇒ v4.4.3 Updating `~/work/rethinking/rethinking-2ed-julia/Manifest.toml` [2d09df54] ~ StatisticalRethinking v4.4.3 `https://github.com/StatisticalRethinkingJulia/StatisticalRethinking.jl#master` ⇒ v4.4.3