using Pkg
Pkg.add("Turing")
Getting Started
Installation
To use Turing, you need to install Julia first and then install Turing.
You will need to install Julia 1.7 or greater, which you can get from the official Julia website.
Turing is officially registered in the Julia General package registry, which means that you can install a stable version of Turing by running the following in the Julia REPL:
Example usage
First, we load the Turing and StatsPlots modules. The latter is required for visualising the results.
using Turing
using StatsPlots
We then specify our model, which is a simple Gaussian model with unknown mean and variance. Models are defined as ordinary Julia functions, prefixed with the @model
macro. Each statement inside closely resembles how the model would be defined with mathematical notation. Here, both x
and y
are observed values, and are therefore passed as function parameters. m
and s²
are the parameters to be inferred.
@model function gdemo(x, y)
~ InverseGamma(2, 3)
s² ~ Normal(0, sqrt(s²))
m ~ Normal(m, sqrt(s²))
x ~ Normal(m, sqrt(s²))
y end
gdemo (generic function with 2 methods)
Suppose we observe x = 1.5
and y = 2
, and want to infer the mean and variance. We can pass these data as arguments to the gdemo
function, and run a sampler to collect the results. Here, we collect 1000 samples using the No U-Turn Sampler (NUTS) algorithm.
= sample(gdemo(1.5, 2), NUTS(), 1000, progress=false) chain
┌ Info: Found initial step size
└ ϵ = 1.6
Chains MCMC chain (1000×14×1 Array{Float64, 3}):
Iterations = 501:1:1500
Number of chains = 1
Samples per chain = 1000
Wall duration = 5.94 seconds
Compute duration = 5.94 seconds
parameters = s², m
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 mcse ess_bulk ess_tail rhat e ⋯
Symbol Float64 Float64 Float64 Float64 Float64 Float64 ⋯
s² 2.0879 1.7963 0.0846 626.1492 463.8684 1.0046 ⋯
m 1.1661 0.8188 0.0440 390.6433 350.5584 0.9999 ⋯
1 column omitted
Quantiles
parameters 2.5% 25.0% 50.0% 75.0% 97.5%
Symbol Float64 Float64 Float64 Float64 Float64
s² 0.5728 1.0369 1.5107 2.4102 7.2829
m -0.4219 0.6966 1.1443 1.6322 2.8379
We can plot the results:
plot(chain)
and obtain summary statistics by indexing the chain:
mean(chain[:m]), mean(chain[:s²])
(1.1660905221576197, 2.0879236605973883)
Where to go next
Familiarity with Julia is assumed throughout the Turing documentation. If you are new to Julia, Learning Julia is a good starting point.
The underlying theory of Bayesian machine learning is not explained in detail in this documentation. A thorough introduction to the field is Pattern Recognition and Machine Learning (Bishop, 2006); an online version is available here (PDF, 18.1 MB).
The next page on Turing’s core functionality explains the basic features of the Turing language. From there, you can either look at worked examples of how different models are implemented in Turing, or specific tips and tricks that can help you get the most out of Turing.