Turing.jl allows you to define probabilistic models using the @model macro. Within the macro, you specify Priors using the ~ operator and define the Likelihood of the observed data. Once a model is defined, you can perform Markov chain Monte Carlo (MCMC) sampling using the sample function, passing in a sampler (e.g., NUTS()).
using Turing
@model function linear_regression(x)
# Priors
α ~ Normal(0, 1)
β ~ Normal(0, 1)
σ² ~ truncated(Cauchy(0, 3); lower=0)
# Likelihood
μ = α .+ β .* x
y ~ MvNormal(μ, σ² * I)
end
# Prepare data
x, y = rand(10), rand(10)
# Instantiate the model with observed data using the semicolon syntax
posterior = linear_regression(x) | (; y = y)
# Perform MCMC sampling
chain = sample(posterior, NUTS(), 1000)