# Import libraries.
using Turing, StatsPlots, Random
Probabilistic Programming in Thirty Seconds
If you are already well-versed in probabilistic programming and want to take a quick look at how Turing’s syntax works or otherwise just want a model to start with, we have provided a complete Bayesian coin-flipping model below.
This example can be run wherever you have Julia installed (see Getting Started, but you will need to install the packages Turing
and StatsPlots
if you have not done so already.
This is an excerpt from a more formal example which can be found here.
Import Libraries
# Set the true probability of heads in a coin.
= 0.5
p_true
# Iterate from having seen 0 observations to 100 observations.
= 0:100 Ns
0:100
# Draw data from a Bernoulli distribution, i.e. draw heads or tails.
Random.seed!(12)
= rand(Bernoulli(p_true), last(Ns)) data
100-element Vector{Bool}:
1
0
0
0
1
1
1
0
1
0
⋮
0
1
0
0
1
0
0
0
1
Declare Turing Model
# Declare our Turing model.
@model function coinflip(y)
# Our prior belief about the probability of heads in a coin.
~ Beta(1, 1)
p
# The number of observations.
= length(y)
N for n in 1:N
# Heads or tails of a coin are drawn from a Bernoulli distribution.
~ Bernoulli(p)
y[n] end
end
coinflip (generic function with 2 methods)
Setting HMC Sampler
# Settings of the Hamiltonian Monte Carlo (HMC) sampler.
= 1000
iterations = 0.05
ϵ = 10
τ
# Start sampling.
= sample(coinflip(data), HMC(ϵ, τ), iterations, progress=false) chain
Chains MCMC chain (1000×11×1 Array{Float64, 3}):
Iterations = 1:1:1000
Number of chains = 1
Samples per chain = 1000
Wall duration = 3.44 seconds
Compute duration = 3.44 seconds
parameters = p
internals = lp, n_steps, is_accept, acceptance_rate, log_density, hamiltonian_energy, hamiltonian_energy_error, numerical_error, step_size, nom_step_size
Summary Statistics
parameters mean std mcse ess_bulk ess_tail rhat ⋯
Symbol Float64 Float64 Float64 Float64 Float64 Float64 ⋯
p 0.4315 0.0490 0.0009 3000.0000 555.1093 1.0023 ⋯
1 column omitted
Quantiles
parameters 2.5% 25.0% 50.0% 75.0% 97.5%
Symbol Float64 Float64 Float64 Float64 Float64
p 0.3403 0.3987 0.4331 0.4620 0.5250
Plot a summary
# Plot a summary of the sampling process for the parameter p, i.e. the probability of heads in a coin.
histogram(chain[:p])