Generated Quantities

Often, the most natural parameterization for a model is not the most computationally feasible. Consider the following (efficiently reparametrized) implementation of Neal’s funnel (Neal, 2003):

using Turing

@model function Neal()
    # Raw draws
    y_raw ~ Normal(0, 1)
    x_raw ~ arraydist([Normal(0, 1) for i in 1:9])

    # Transform:
    y = 3 * y_raw
    x = exp.(y ./ 2) .* x_raw

    # Return:
    return [x; y]
end
Neal (generic function with 2 methods)

In this case, the random variables exposed in the chain (x_raw, y_raw) are not in a helpful form — what we’re after are the deterministically transformed variables x and y.

More generally, there are often quantities in our models that we might be interested in viewing, but which are not explicitly present in our chain.

We can generate draws from these variables — in this case, x and y — by adding them as a return statement to the model, and then calling generated_quantities(model, chain). Calling this function outputs an array of values specified in the return statement of the model.

For example, in the above reparametrization, we sample from our model:

chain = sample(Neal(), NUTS(), 1000; progress=false)
┌ Info: Found initial step size
└   ϵ = 1.6
Chains MCMC chain (1000×22×1 Array{Float64, 3}):

Iterations        = 501:1:1500
Number of chains  = 1
Samples per chain = 1000
Wall duration     = 7.4 seconds
Compute duration  = 7.4 seconds
parameters        = y_raw, x_raw[1], x_raw[2], x_raw[3], x_raw[4], x_raw[5], x_raw[6], x_raw[7], x_raw[8], x_raw[9]
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    ⋯
      Symbol   Float64   Float64   Float64     Float64    Float64   Float64    ⋯

       y_raw    0.0138    1.0238    0.0257   1614.0019   716.4144    1.0003    ⋯
    x_raw[1]   -0.0003    1.0334    0.0309   1111.6127   736.5628    0.9990    ⋯
    x_raw[2]    0.0104    0.9903    0.0256   1487.0156   652.5839    1.0008    ⋯
    x_raw[3]   -0.0157    0.9729    0.0281   1210.0394   767.3903    1.0019    ⋯
    x_raw[4]   -0.0376    0.9510    0.0260   1333.2577   803.9658    0.9990    ⋯
    x_raw[5]    0.0286    1.0141    0.0309   1051.5807   915.5674    1.0042    ⋯
    x_raw[6]   -0.0540    1.0048    0.0262   1476.6980   619.3589    0.9994    ⋯
    x_raw[7]   -0.0233    1.0314    0.0272   1474.5338   694.2048    1.0004    ⋯
    x_raw[8]   -0.0357    1.0018    0.0252   1567.8838   601.0952    0.9996    ⋯
    x_raw[9]    0.0150    0.9974    0.0244   1672.8145   822.5266    0.9993    ⋯
                                                                1 column omitted

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

       y_raw   -1.9870   -0.6707   -0.0202    0.7288    1.9076
    x_raw[1]   -1.9933   -0.7012   -0.0176    0.7156    1.9918
    x_raw[2]   -1.9640   -0.6868   -0.0118    0.7080    1.9836
    x_raw[3]   -1.9312   -0.6268   -0.0126    0.6325    1.8898
    x_raw[4]   -1.8803   -0.6946   -0.0191    0.5845    1.7931
    x_raw[5]   -1.9267   -0.6689    0.0513    0.7205    2.0134
    x_raw[6]   -1.9645   -0.7542   -0.0863    0.6130    1.9905
    x_raw[7]   -2.1325   -0.6755    0.0013    0.6774    1.9111
    x_raw[8]   -1.9757   -0.6883   -0.0422    0.6988    1.8220
    x_raw[9]   -1.9382   -0.6295   -0.0042    0.6602    2.0576

Notice that only x_raw and y_raw are stored in the chain; x and y are not because they do not appear on the left-hand side of a tilde-statement.

To get x and y, we can then call:

generated_quantities(Neal(), chain)
1000×1 Matrix{Vector{Float64}}:
 [8.46607511341892, -0.9555452706504777, 9.487404228128351, -6.894463058169959, 1.578895256170451, -11.817949670772846, 1.9813113932814128, -0.5105897150855238, 2.2542340734717072, 3.565390721617785]
 [0.07139132232222047, 0.7812066600249625, 0.32324753807934037, 0.4122178314027469, 0.6118385992023281, 0.06845688716329966, -0.2671368236583288, 0.003945126649829803, 0.4839573546360743, -1.4077429323585329]
 [0.07139132232222047, 0.7812066600249625, 0.32324753807934037, 0.4122178314027469, 0.6118385992023281, 0.06845688716329966, -0.2671368236583288, 0.003945126649829803, 0.4839573546360743, -1.4077429323585329]
 [-1.1751507433695458, -0.8558589325391753, 0.46201890653552424, -1.1182666390210747, -1.7714502192610013, 0.5981031309073851, 0.5098716946044415, 0.08085727538734579, -1.8404238504466646, 1.2188149257561312]
 [-1.1721316708668508, -0.043800009286802534, 0.5615713608299835, -0.42211797727609696, 0.5248942235671518, -1.3052748224689752, -1.092546532314381, 0.87647212320793, 0.7955440901671174, -0.4796157140692691]
 [0.592746118483031, -0.6120328918146096, -0.2939456382728783, 1.3151465864381955, -0.06244595877023209, 0.08777125221789574, -0.5188158184710138, 0.006501893664331291, -0.6751468697740416, -0.5305653349116881]
 [-0.32034226468420174, 0.3275614013640697, -0.17156492515183083, 0.07969816646522321, -0.2423608687842169, 0.4365557314890503, -0.11242413112710752, 0.27689312671803, 0.10388541818656787, -2.507215565675441]
 [-0.418011020264853, -0.07702652357927464, -0.11047140939052362, -0.019439871762501556, 0.320575441335976, -1.28271715012653, 0.3678799582078887, -0.4908663494167071, -0.23271085157270918, -1.2200222412833766]
 [0.12933183423377168, 0.18147052377256778, -0.12368820324414019, 0.19071167714106366, -0.2982238730182292, 0.19922642554822773, 0.09254125333535809, 0.23211692766286898, -0.06784753017070531, -3.3497202574922342]
 [31.50246719938867, 111.31174950937455, -7.533138374313941, 2.7609932173252507, -33.295234153784534, 70.96704416196549, 49.38480627752693, -54.436735991344214, 34.073169989564256, 7.620905147923267]
 ⋮
 [0.33369600483827594, 0.8658689439389965, -0.034305780762622536, 0.038962919057563515, 0.0031271993651299157, -0.07015761586893315, 0.4341786851195327, 0.4136588221231516, -0.0804732952659582, -1.1522326161086451]
 [1.9233018020719486, -16.26091365838069, -2.894237660960723, 9.258193313202792, -10.261146365124958, -4.4052282636189375, 3.8059163917223744, -1.5512701776991333, 10.256223552594113, 4.022019686699226]
 [-0.1940822910004378, 0.9689588823182288, -0.70197582212476, -0.1581778580813806, -0.0035976509053538583, -0.025613924461538297, 0.4273577791733287, -0.07522182999948857, -0.8656687187751195, -1.480285577981095]
 [1.2319861049183947, 0.07860617472075086, -0.3268334394785345, 2.6551145249266894, -0.9810644867282614, -0.7510272791986478, -2.3589313653786625, -0.48548545459115094, -0.04887742676257378, 0.9798033222464093]
 [0.08563152101910591, -0.08139731397788391, 0.20586759396007878, -0.4281698920311147, -0.2407238479214281, 0.03852410886278958, 0.15963009902903147, -0.1800951594244438, 0.016116507303535622, -3.230745495332234]
 [-1.776751132666562, 3.441721533644156, -0.4906030744820253, -0.03638071225724555, -2.438309584823751, -1.5310849661729407, 1.9824400912220428, -0.7967583414585173, 2.4639873604963585, 1.7152268630887217]
 [0.3574994130439288, -0.011699086676376837, 0.18871885277767325, -0.15929724780760057, 0.213906495747333, -0.1946107153692646, 0.37796686650994266, -0.2991690367689757, 0.2866848797143121, -2.0240493954764185]
 [0.3448148488824335, 0.36875913456639603, -5.484586626976862, -1.9929168717250563, -3.617558679685897, 1.2868468247037042, -1.9438879427845623, -3.267027587888602, -1.4553894847266409, 2.645501598935469]
 [0.00097840660862175, 0.09954859816189189, -0.1284517459820068, 0.08461723487225847, 0.18461102082910563, -0.1278564598053825, 0.02045692232028167, 0.09110870378373578, 0.19396905653345148, -3.074472292910415]

Each element of this corresponds to an array with the values of x1, x2, ..., x9, y for each posterior sample.

In this case, it might be useful to reorganize our output into a matrix for plotting:

reparam_chain = reduce(hcat, generated_quantities(Neal(), chain))'
1000×10 adjoint(::Matrix{Float64}) with eltype Float64:
  8.46608       -0.955545   …   -0.51059      2.25423     3.56539
  0.0713913      0.781207        0.00394513   0.483957   -1.40774
  0.0713913      0.781207        0.00394513   0.483957   -1.40774
 -1.17515       -0.855859        0.0808573   -1.84042     1.21881
 -1.17213       -0.0438          0.876472     0.795544   -0.479616
  0.592746      -0.612033   …    0.00650189  -0.675147   -0.530565
 -0.320342       0.327561        0.276893     0.103885   -2.50722
 -0.418011      -0.0770265      -0.490866    -0.232711   -1.22002
  0.129332       0.181471        0.232117    -0.0678475  -3.34972
 31.5025       111.312         -54.4367      34.0732      7.62091
  ⋮                         ⋱                            
  0.333696       0.865869        0.413659    -0.0804733  -1.15223
  1.9233       -16.2609         -1.55127     10.2562      4.02202
 -0.194082       0.968959       -0.0752218   -0.865669   -1.48029
  1.23199        0.0786062      -0.485485    -0.0488774   0.979803
  0.0856315     -0.0813973  …   -0.180095     0.0161165  -3.23075
 -1.77675        3.44172        -0.796758     2.46399     1.71523
  0.357499      -0.0116991      -0.299169     0.286685   -2.02405
  0.344815       0.368759       -3.26703     -1.45539     2.6455
  0.000978407    0.0995486       0.0911087    0.193969   -3.07447

from which we can recover a vector of our samples:

x1_samples = reparam_chain[:, 1]
y_samples = reparam_chain[:, 10]
1000-element Vector{Float64}:
  3.565390721617785
 -1.4077429323585329
 -1.4077429323585329
  1.2188149257561312
 -0.4796157140692691
 -0.5305653349116881
 -2.507215565675441
 -1.2200222412833766
 -3.3497202574922342
  7.620905147923267
  ⋮
 -1.1522326161086451
  4.022019686699226
 -1.480285577981095
  0.9798033222464093
 -3.230745495332234
  1.7152268630887217
 -2.0240493954764185
  2.645501598935469
 -3.074472292910415
Back to top