```
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 = 6.16 seconds
Compute duration = 6.16 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.9817 1.7005 0.0856 464.6259 594.7249 0.9995 ⋯
m 1.1630 0.7518 0.0309 606.2945 442.4734 0.9990 ⋯
1 column omitted
Quantiles
parameters 2.5% 25.0% 50.0% 75.0% 97.5%
Symbol Float64 Float64 Float64 Float64 Float64
s² 0.5287 1.0197 1.4903 2.3314 6.4281
m -0.3444 0.6632 1.1829 1.6399 2.7416
```

We can plot the results:

`plot(chain)`

and obtain summary statistics by indexing the chain:

`mean(chain[:m]), mean(chain[:s²])`

`(1.16298480631509, 1.981722494398813)`

### 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.