Getting Started

Installation

To use Turing, you need to install Julia first and then install Turing.

You will need to install Julia 1.10 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:

using Pkg
Pkg.add("Turing")

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 are the parameters to be inferred.

@model function gdemo(x, y)
~ InverseGamma(2, 3)
    m ~ Normal(0, sqrt(s²))
    x ~ Normal(m, sqrt(s²))
    y ~ Normal(m, sqrt(s²))
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.

chain = sample(gdemo(1.5, 2), NUTS(), 1000, progress=false)
┌ 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     = 7.09 seconds
Compute duration  = 7.09 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²    1.9311    1.5539    0.0676   652.2642   617.5851    1.0191     ⋯
           m    1.1396    0.7899    0.0373   456.1036   451.8171    1.0024     ⋯
                                                                1 column omitted

Quantiles
  parameters      2.5%     25.0%     50.0%     75.0%     97.5%
      Symbol   Float64   Float64   Float64   Float64   Float64

          s²    0.5647    1.0333    1.4895    2.2588    5.7940
           m   -0.5129    0.6877    1.1446    1.6112    2.6989

We can plot the results:

plot(chain)

and obtain summary statistics by indexing the chain:

mean(chain[:m]), mean(chain[:s²])
(1.1395556664717745, 1.931128749869819)

Where to go next

Note on prerequisites

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.

Back to top