TuringModels

Basic Example

We define a simple Gaussian model with unknown mean and variance.

  1. Model
  2. Output

Model

using Turing

@model function gdemo(x, y)
  s ~ InverseGamma(2, 3)
  m ~ Normal(0, sqrt(s))
  x ~ Normal(m, sqrt(s))
  y ~ Normal(m, sqrt(s))
end;

Output

and run the sampler:

chns = sample(gdemo(1.5, 2), NUTS(), 1000)
Chains MCMC chain (1000×14×1 Array{Float64, 3}):

Iterations        = 501:1:1500
Number of chains  = 1
Samples per chain = 1000
Wall duration     = 5.27 seconds
Compute duration  = 5.27 seconds
parameters        = m, s
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   naive_se      mcse        ess      rhat   ess_per_sec
      Symbol   Float64   Float64    Float64   Float64    Float64   Float64       Float64

           s    2.0767    1.7855     0.0565    0.1226   274.1249    0.9990       52.0359
           m    1.1529    0.8111     0.0256    0.0341   536.5472    1.0040      101.8503

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

           s    0.5732    1.0641    1.5254    2.3961    6.6743
           m   -0.5036    0.6731    1.1434    1.5989    2.8225

using StatsPlots

StatsPlots.plot(chns)