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:

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
└   ϵ = 0.8
Chains MCMC chain (1000×14×1 Array{Float64, 3}):

Iterations        = 501:1:1500
Number of chains  = 1
Samples per chain = 1000
Wall duration     = 6.82 seconds
Compute duration  = 6.82 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.0430    1.6590    0.0791   436.8845   516.4412    1.0021     ⋯
           m    1.1561    0.7920    0.0322   626.7483   540.5783    0.9991     ⋯
                                                                1 column omitted

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

          s²    0.5791    1.0870    1.5577    2.4050    6.6832
           m   -0.4069    0.6640    1.1838    1.6538    2.5730

We can plot the results:

plot(chain)

and obtain summary statistics by indexing the chain:

mean(chain[:m]), mean(chain[:s²])
(1.156126696064909, 2.0429538553981113)

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