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     = 6.91 seconds
Compute duration  = 6.91 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.0049    1.0205    0.0311   1042.4091   687.8570    0.9993    ⋯
    x_raw[1]    0.0263    1.0231    0.0253   1623.7469   568.7980    0.9996    ⋯
    x_raw[2]   -0.0225    0.9914    0.0244   1584.5444   782.0201    1.0003    ⋯
    x_raw[3]    0.0443    1.0469    0.0266   1563.2235   835.6863    1.0016    ⋯
    x_raw[4]   -0.0206    1.0204    0.0309   1088.6301   723.6072    1.0003    ⋯
    x_raw[5]   -0.0200    0.9509    0.0235   1639.2688   792.1886    0.9992    ⋯
    x_raw[6]    0.0043    0.9806    0.0207   2304.6087   580.2492    0.9991    ⋯
    x_raw[7]   -0.0089    1.0127    0.0302   1137.7063   715.0619    1.0011    ⋯
    x_raw[8]   -0.0174    0.9802    0.0238   1732.8518   886.9318    0.9993    ⋯
    x_raw[9]    0.0417    0.9893    0.0334    876.2684   678.3714    1.0023    ⋯
                                                                1 column omitted

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

       y_raw   -1.9821   -0.7042   -0.0204    0.7105    2.1069
    x_raw[1]   -2.0599   -0.6731    0.0283    0.7312    2.0333
    x_raw[2]   -1.9070   -0.7464    0.0066    0.6892    1.9361
    x_raw[3]   -2.0056   -0.6609    0.0397    0.7581    2.0819
    x_raw[4]   -1.9721   -0.7412    0.0177    0.6919    1.9462
    x_raw[5]   -1.8414   -0.6615   -0.0171    0.6282    1.7642
    x_raw[6]   -1.8654   -0.6802   -0.0114    0.7010    1.9378
    x_raw[7]   -1.9247   -0.7435    0.0148    0.6878    2.0526
    x_raw[8]   -2.0525   -0.7129    0.0047    0.6640    1.9413
    x_raw[9]   -1.9507   -0.6139    0.0614    0.6752    2.0411

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}}:
 [0.48768532465602166, -1.5266760444148317, 3.73488420078738, -1.0909559270000706, -1.9596728712134954, 3.0852175728819105, -2.5195585858461467, 1.2629429506796548, -1.591599530392493, 1.9023302173713397]
 [-0.05734781244683999, 0.3977088828436452, -1.025571477756659, 0.41296877821321076, 0.4942144789609048, -0.795376769683169, 0.4863302604767323, -0.10686760230858111, 1.3952552222790144, -0.8340639327394488]
 [-0.1635258174366138, -1.5794615108489594, -0.2383471379499093, -1.5777235846749658, -2.3796390159908274, 0.32287645940727194, -0.47879968367776493, -0.877545342909656, 1.3158798556224653, 1.6753481693380896]
 [-0.028865811437003782, 0.20902646504082328, 0.06208632174265271, 0.19363887403671945, 0.3382086329259947, -0.027075462223248237, 0.12821889784695306, 0.04614160300839528, -0.07705015516108804, -2.416563685276117]
 [-0.2827169519882472, 0.2126543782525096, -0.6785746454605384, 1.501392110809468, -0.1332109497499928, -0.33632582820033935, -0.803215684460826, 1.4307155816782857, -2.501628249516429, 0.5802884701851463]
 [4.38779914889448, 8.642084465132752, -2.4716328583664406, -11.088880294477509, -0.8523390416852469, 3.722867620612254, 1.7217293246326717, -6.45424074846697, -3.3234625032887144, 3.914258847791232]
 [3.0457743042965664, 1.50741605379873, -6.601359879201403, -5.193563844812964, -4.731900000728814, 0.8182062741637043, 2.5886487198355557, 0.032287249952450706, 7.342152101543887, 3.127047813497603]
 [-0.10397963947085465, -0.028004357257766153, 0.18026973468587382, 0.13048789444719938, 0.15934236180397773, -0.0082207046364272, -0.04576184333996519, 0.016167338519608103, -0.3137589493740973, -3.919988848105371]
 [2.705832342269569, 0.44900580342465024, -6.694077561777419, -4.212950758178549, -7.186923991534962, 0.11408142880750857, 0.7692133473335854, -1.342107319522106, 8.085298823837384, 3.1338567456999407]
 [1.1040185547138073, -0.5414381741938825, 1.0701416241808663, 0.3974841448454017, 1.1517508433285584, -0.24039633292376916, -0.10709443361704662, -0.43228834083074513, -1.2904040098699587, -0.529516931961663]
 ⋮
 [0.05076310057414889, -0.11002431004138186, 0.19208970492820446, -0.003586673262555621, -0.06743085099676832, 0.170663300399116, 0.1457632191847885, -0.0799965768955598, -0.024612945147261082, -4.33367621166452]
 [48.05302876514998, 46.82614749302566, -118.85830356157047, -47.39676944916765, -33.76087219909403, 81.54131720642876, -39.11639780468609, 10.730290798967898, 7.462225699850914, 8.626876208810053]
 [-0.00807942957607871, -0.014138752842613476, 0.03412181922129519, 0.008897243732711394, 0.01953471154677091, -0.02503423070204854, 0.017445364220063042, 0.002103765735407101, 0.01914670770936081, -7.624234506582526]
 [8.019314245630634, 12.281391731441687, -38.68415024650252, -6.943240913041845, -21.763725283283115, 24.11480505643119, -21.426880135481223, -5.55739676756192, -5.607852128789843, 6.366285416700511]
 [0.17759884166978251, -0.041860980345948255, 0.3257136140384577, 0.05624358870156626, 0.007457055249199862, 0.047254067226272435, 0.0747963736224535, 0.0021960430145839087, 0.030417524591437255, -3.8490638314202474]
 [-6.1456783266007315, 2.0639825468474013, -9.100053529126066, -1.7268168057528352, 0.9685175803073007, -2.0852174432067154, -2.4886030180844925, -3.1018750733608877, -1.6852258288655844, 2.9269749845141204]
 [0.47597660057441155, -0.1257610705583851, 0.7319731911248898, 0.01306604268164509, -0.12698249293333957, 0.22107799495236927, 0.3245376288312054, 0.36592155686462746, 0.40064273474742623, -2.0613888778412823]
 [1.6521511230430308, 1.394406345726079, 0.3225233615476245, 1.2156824922831395, -2.145421755354559, -0.2977462128608438, -1.5594861785604417, 0.8410244032127643, 2.330971928778687, 0.31941323357064777]
 [0.725861387588008, 0.31391793517428146, -0.36139239788413025, 0.24479664640930127, -0.1991463684577484, -0.025187867806529728, 1.3111318987164706, 1.1400499938577873, 0.886195673376815, -0.6243011961358138]

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:
  0.487685    -1.52668       3.73488    …   1.26294     -1.5916      1.90233
 -0.0573478    0.397709     -1.02557       -0.106868     1.39526    -0.834064
 -0.163526    -1.57946      -0.238347      -0.877545     1.31588     1.67535
 -0.0288658    0.209026      0.0620863      0.0461416   -0.0770502  -2.41656
 -0.282717     0.212654     -0.678575       1.43072     -2.50163     0.580288
  4.3878       8.64208      -2.47163    …  -6.45424     -3.32346     3.91426
  3.04577      1.50742      -6.60136        0.0322872    7.34215     3.12705
 -0.10398     -0.0280044     0.18027        0.0161673   -0.313759   -3.91999
  2.70583      0.449006     -6.69408       -1.34211      8.0853      3.13386
  1.10402     -0.541438      1.07014       -0.432288    -1.2904     -0.529517
  ⋮                                     ⋱                           
  0.0507631   -0.110024      0.19209       -0.0799966   -0.0246129  -4.33368
 48.053       46.8261     -118.858         10.7303       7.46223     8.62688
 -0.00807943  -0.0141388     0.0341218      0.00210377   0.0191467  -7.62423
  8.01931     12.2814      -38.6842        -5.5574      -5.60785     6.36629
  0.177599    -0.041861      0.325714   …   0.00219604   0.0304175  -3.84906
 -6.14568      2.06398      -9.10005       -3.10188     -1.68523     2.92697
  0.475977    -0.125761      0.731973       0.365922     0.400643   -2.06139
  1.65215      1.39441       0.322523       0.841024     2.33097     0.319413
  0.725861     0.313918     -0.361392       1.14005      0.886196   -0.624301

from which we can recover a vector of our samples:

x1_samples = reparam_chain[:, 1]
y_samples = reparam_chain[:, 10]
1000-element Vector{Float64}:
  1.9023302173713397
 -0.8340639327394488
  1.6753481693380896
 -2.416563685276117
  0.5802884701851463
  3.914258847791232
  3.127047813497603
 -3.919988848105371
  3.1338567456999407
 -0.529516931961663
  ⋮
 -4.33367621166452
  8.626876208810053
 -7.624234506582526
  6.366285416700511
 -3.8490638314202474
  2.9269749845141204
 -2.0613888778412823
  0.31941323357064777
 -0.6243011961358138
Back to top