Home

Wren is a library for probabilistic programming in Julia.

Installation

import PkgPkg.add("Wren")

Quick start

using Wren, Wren.Distsy = [28, 8, -3, 7, -1, 1, 18, 12]sigma = [15, 10, 16, 11, 9, 11, 10, 18]J = 8@wren function eight_schools(y, sigma, J)    mu ~ Normal(0, 5)    tau ~ HalfCauchy(0, 5)    theta ~ MvNormalScale(fill(mu, J), tau * I)    @batch for i in 1:J        @observe y[i] ~ Normal(theta[i], sigma[i])    end    return (mu=mu, tau=tau)endmodel = eight_schools(y, sigma, J)
Model eight_schools
├─ arguments
│  ├─ y => [28, 8, -3, 7, -1, 1, 18, 12]
│  ├─ sigma => [15, 10, 16, 11, 9, 11, 10, 18]
│  └─ J => 8
├─ conditioned (none)
└─ expressions
     mu ~ Normal(0, 5)
     tau ~ HalfCauchy(0, 5)
     theta ~ MvNormalScale(fill(mu, J), tau * I)
     @batch(name = Symbol("##batch#725"), for i = 1:J
             @observe y[i] ~ Normal(theta[i], sigma[i])
         end)
     return (mu = mu, tau = tau)

Draw from the prior

simulate(model)
ModelResult
├─ parameters => VarNamedTuple
│  ├─ mu => 3.8407758668804104
│  ├─ tau => 1.9952156325752688
│  └─ theta => [5.153894893953568, 1.5692172535675328, 0.4914460096984783, 1.841133721896543, 7.30755418535744, 3.47484950549632, -0.6636133368539681, 6.722005496127859]
├─ tracked_quantities => VarNamedTuple()
├─ return_value => (mu = 3.8407758668804104, tau = 1.9952156325752688)
├─ logprior => -25.8029707464404
└─ loglikelihood => -31.057112189691928

MCMC sampling

import Enzymenuts(model, 1000, 4)
╭─FlexiChain (1000 iterations, 4 chains) ──────────────────────────────────────╮
│ ↓ iter  = 1:1000                                                             │
│ → chain = 1:4                                                                │
│                                                                              │
│ Parameters (3) ── VarName                                                    │
│  Float64          mu, tau                                                    │
│  Vector{Float64}  theta (8,)                                                 │
│                                                                              │
│ Extras (10)                                                                  │
│  Float64                                 step_size, acceptance_rate          │
│                                          log_density, logprior               │
│                                          loglikelihood, logjoint             │
│  Int64                                   tree_depth                          │
│  Bool                                    is_divergent, is_max_depth          │
│  @NamedTuple{mu::Float64, tau::Float64}  return_value                        │
╰──────────────────────────────────────────────────────────────────────────────╯