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.93 seconds
Compute duration  = 7.93 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.0092    1.0230    0.0325    996.5408   807.2221    1.0017    ⋯
    x_raw[1]    0.0442    0.9663    0.0305   1002.0275   689.1683    1.0019    ⋯
    x_raw[2]   -0.0148    1.0572    0.0313   1140.8593   768.8827    1.0005    ⋯
    x_raw[3]   -0.0205    1.0027    0.0254   1592.0354   700.7774    1.0008    ⋯
    x_raw[4]   -0.0585    0.9944    0.0285   1232.4704   616.4302    1.0008    ⋯
    x_raw[5]    0.0217    0.9713    0.0303   1019.0150   602.8197    0.9990    ⋯
    x_raw[6]   -0.0027    0.9728    0.0305    989.9405   875.6889    1.0000    ⋯
    x_raw[7]   -0.0268    0.9982    0.0275   1330.7456   847.0356    1.0015    ⋯
    x_raw[8]   -0.0263    1.0264    0.0315   1066.3734   657.7236    1.0004    ⋯
    x_raw[9]    0.0007    1.0435    0.0283   1363.8933   837.6591    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.8935   -0.6871   -0.0502    0.7162    2.0939
    x_raw[1]   -1.7661   -0.5834    0.0142    0.6992    2.0113
    x_raw[2]   -2.0785   -0.6966   -0.0193    0.6504    1.9912
    x_raw[3]   -1.9174   -0.7468   -0.0415    0.6532    1.9336
    x_raw[4]   -2.1163   -0.7449   -0.0453    0.5897    1.8688
    x_raw[5]   -1.8501   -0.5675    0.0106    0.6586    2.0384
    x_raw[6]   -1.8665   -0.5566   -0.0285    0.6334    1.9055
    x_raw[7]   -2.0040   -0.6799   -0.0486    0.6223    1.9533
    x_raw[8]   -2.0172   -0.6923    0.0078    0.6597    1.9127
    x_raw[9]   -1.9974   -0.7051   -0.0432    0.7611    2.0238

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.3084229359685713, -0.08593641246231407, 0.15048114192441017, -0.15051917200950873, 0.14197790909707325, -0.11535450199271315, -0.3379776507176297, 0.09674432786475734, 0.003711890471968385, -3.590076188484895]
 [0.23469770278185714, 0.07222654172027147, 0.6532141890964774, 0.16847113506765773, 1.007229601084469, -0.7358331331524789, 0.49151273882352103, -0.06760201084110205, -0.38396096552492653, -1.1754142757903037]
 [0.23469770278185714, 0.07222654172027147, 0.6532141890964774, 0.16847113506765773, 1.007229601084469, -0.7358331331524789, 0.49151273882352103, -0.06760201084110205, -0.38396096552492653, -1.1754142757903037]
 [0.09173438513567231, 0.21444007403774873, -0.2247278674757103, 0.1335490366350324, 0.0836842434177088, -0.08749266352656124, -0.1246663270511024, -0.04244980604812785, 0.17238802164346584, -3.914530765492532]
 [-1.4698756198817047, 0.6524508710224923, 5.92921022586297, -0.3860128819331682, -2.5957553803059548, 3.7633311170285233, 4.22718884758822, 0.5748807274512939, 2.947655400382615, 2.0486482013010576]
 [9.125428537074221, 0.7766389202113599, 24.7992350345148, 0.7644405501907885, -19.02524913663892, -21.323319472195262, 22.64825187765959, -12.905036308305256, -0.43703523355979906, 5.226588615578645]
 [-0.012599858250894411, 0.012090768353936281, -0.17029996594885202, -0.034505429045352996, 0.12656781184496535, 0.16948618600186655, -0.023308966933462863, 0.06495173242166696, -0.07600346028905841, -4.231360809893823]
 [0.028113691924099134, 0.0506517978648443, -0.10384992587968167, -0.12787828816079364, -0.043856227469544, 0.2763228270177803, -0.03206648360680812, -0.06626710471489557, -0.33349201442126464, -3.860958239880135]
 [-0.018431025909871685, -0.06586856790231974, -0.7658093504858743, -1.1300479975996134, -0.10085594834335627, 1.3214012172264178, 1.2690392532081223, -0.2998934578921174, -1.884242003887184, -0.11530276599844691]
 [-4.238670096191106, -0.9638148643139729, -1.4468427671553767, 0.6273591355489795, -0.2019564117100117, -0.25113955056660836, 2.28506922276553, -2.8110047983882516, 0.4479049162295953, 0.8502838242162222]
 ⋮
 [0.2902290686220856, 0.8421037012111606, -0.14708360630650905, 0.7811056325543866, 0.8615478486201157, -0.29780711561597906, -0.7540578879509938, -0.2459269924479105, -0.26536040273361305, -1.2564298271268184]
 [-6.416027457184739, 30.565746466742205, -8.463658709202008, 21.05037833898754, 5.377573884277475, 4.490171858333876, -28.475274208580778, -4.878063344960393, -10.48238471282555, 5.946542784548699]
 [10.990030420984441, 4.315588141460533, -10.829750618729713, 7.274882211052059, 7.51815598674197, 8.882736836149144, -4.3850800696902565, 11.426302967740536, -23.663008531425927, 5.187563694095212]
 [-0.6853308038219691, -0.09789848771408223, 0.5178749704674415, -0.3737661469859409, -0.12635332096340507, -0.3815988161052068, 0.27989866378719813, 0.01352985088158952, 0.847313824018721, -1.3778929986202375]
 [-3.1424963160945176, -11.658850095028528, -4.529028238864294, -7.85838959656182, 2.2064215463818493, -5.834995100533027, 2.810162476971428, 3.0823152261540163, -9.936097556724285, 3.5856393793028434]
 [-3.1642182183298377, 7.132787197219897, -0.11133433828440308, 4.002882006164821, 2.852847845561166, 3.0628449991594184, 1.7655258608521753, -6.157080522448575, 4.493328887220953, 2.480447721515843]
 [-0.008332001111637549, -0.0032292043404005834, 0.0067991301895043935, 0.023013997677646232, -0.0076762270843658414, -0.0029526306772877653, 0.005688360610499245, -0.007691402721154673, 0.030954046839632873, -8.167897238254822]
 [44.02997939107904, -6.105072159294819, 22.086829395812416, -44.02132571758295, 28.62567560603451, 22.915425902676215, 29.400505255779155, 83.47194449389113, -53.15511075583229, 8.162636637938277]
 [0.029188276230677136, -0.02207847940258454, -0.026363728914836813, 0.029863277498247524, -0.10593328779674335, -0.17173616882256038, -0.013135802031030479, -0.1413122171902363, 0.1456083620889594, -3.606275984945963]

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.308423    -0.0859364    0.150481    …    0.00371189  -3.59008
  0.234698     0.0722265    0.653214        -0.383961    -1.17541
  0.234698     0.0722265    0.653214        -0.383961    -1.17541
  0.0917344    0.21444     -0.224728         0.172388    -3.91453
 -1.46988      0.652451     5.92921          2.94766      2.04865
  9.12543      0.776639    24.7992      …   -0.437035     5.22659
 -0.0125999    0.0120908   -0.1703          -0.0760035   -4.23136
  0.0281137    0.0506518   -0.10385         -0.333492    -3.86096
 -0.018431    -0.0658686   -0.765809        -1.88424     -0.115303
 -4.23867     -0.963815    -1.44684          0.447905     0.850284
  ⋮                                     ⋱                
  0.290229     0.842104    -0.147084        -0.26536     -1.25643
 -6.41603     30.5657      -8.46366        -10.4824       5.94654
 10.99         4.31559    -10.8298         -23.663        5.18756
 -0.685331    -0.0978985    0.517875         0.847314    -1.37789
 -3.1425     -11.6589      -4.52903     …   -9.9361       3.58564
 -3.16422      7.13279     -0.111334         4.49333      2.48045
 -0.008332    -0.0032292    0.00679913       0.030954    -8.1679
 44.03        -6.10507     22.0868         -53.1551       8.16264
  0.0291883   -0.0220785   -0.0263637        0.145608    -3.60628

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.590076188484895
 -1.1754142757903037
 -1.1754142757903037
 -3.914530765492532
  2.0486482013010576
  5.226588615578645
 -4.231360809893823
 -3.860958239880135
 -0.11530276599844691
  0.8502838242162222
  ⋮
 -1.2564298271268184
  5.946542784548699
  5.187563694095212
 -1.3778929986202375
  3.5856393793028434
  2.480447721515843
 -8.167897238254822
  8.162636637938277
 -3.606275984945963
Back to top