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.53 seconds
Compute duration  = 5.53 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   naive_se      mcse        ess      rhat   ess_per_sec
      Symbol   Float64   Float64    Float64   Float64    Float64   Float64       Float64

           s    2.0921    1.8996     0.0601    0.0989   449.9904    0.9991       81.3726
           m    1.0945    0.8506     0.0269    0.0484   446.9420    0.9990       80.8213

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

           s    0.6175    1.0745    1.5660    2.3691    6.9518
           m   -0.7651    0.6135    1.1405    1.6455    2.5919

using StatsPlots

StatsPlots.plot(chns)
"/home/runner/work/TuringModels.jl/TuringModels.jl/__site/assets/models/basic-example/code/output/chns.svg"