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.56 seconds
Compute duration  = 7.56 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.0052    0.9990    0.0277   1317.6898   738.0266    1.0018    ⋯
    x_raw[1]    0.0156    0.9898    0.0290   1187.7194   643.0315    0.9996    ⋯
    x_raw[2]   -0.0188    0.9390    0.0264   1261.0876   767.9180    1.0008    ⋯
    x_raw[3]   -0.0127    0.9717    0.0270   1268.1972   792.1886    0.9990    ⋯
    x_raw[4]   -0.0268    0.9834    0.0284   1200.5732   903.5186    1.0042    ⋯
    x_raw[5]    0.0346    1.0648    0.0256   1746.0846   696.7496    1.0004    ⋯
    x_raw[6]   -0.0101    1.0074    0.0260   1501.4107   820.4943    1.0004    ⋯
    x_raw[7]    0.0409    0.9980    0.0292   1165.1899   744.2742    0.9999    ⋯
    x_raw[8]   -0.0407    1.0140    0.0294   1191.9175   742.6619    1.0021    ⋯
    x_raw[9]    0.0121    0.9904    0.0256   1529.3234   713.5469    0.9997    ⋯
                                                                1 column omitted

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

       y_raw   -2.0105   -0.6835    0.0169    0.7215    1.9214
    x_raw[1]   -1.8395   -0.6545    0.0171    0.6608    1.9966
    x_raw[2]   -1.7991   -0.6729   -0.0453    0.6389    1.8015
    x_raw[3]   -1.9990   -0.6722   -0.0131    0.6055    1.9149
    x_raw[4]   -1.8789   -0.7040   -0.0029    0.6293    1.9237
    x_raw[5]   -2.1542   -0.6700    0.0134    0.7381    2.2231
    x_raw[6]   -1.9728   -0.6867    0.0192    0.6385    1.9452
    x_raw[7]   -1.9863   -0.6592    0.0679    0.6718    1.8632
    x_raw[8]   -2.1044   -0.7249   -0.0224    0.6544    1.8480
    x_raw[9]   -1.9301   -0.6633   -0.0763    0.6950    2.0400

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}}:
 [1.1785106466040116, 1.258011858354877, 0.15970119198997734, 1.1694188046836977, 0.2830489421677565, -0.27895341852321764, 1.421537125424868, -0.8265535583753623, -3.316059160828001, 0.4865611856385721]
 [6.2057162999631705, 7.211947149071256, 2.9715987724224866, 0.6175149580407491, 4.811524914318784, 3.527505435781519, 0.99818411387379, -7.794395713954156, -8.004620447532071, 3.1368456827018543]
 [-0.10235793607755103, -0.006977934972558454, 0.044842006388706196, -0.04418926722006559, -0.049863477392649416, -0.08429704584827626, -0.0004934919285527286, -0.005926977151045139, 0.09139196121915788, -6.1792916816723595]
 [-0.06275229206153737, -0.12334953719370118, 0.1390110107077551, 0.02664054211362997, -0.04205532695225731, 0.004723530132187884, 0.021791255493305985, 0.0948904700731361, 0.017577732385194264, -4.247289337886385]
 [-1.0151685529907863, 0.0370429028335741, 0.3400941770180665, -0.8078396463433167, 0.6789673199032725, -1.4782379912354529, 0.9772763830160365, 0.9578807105811517, -0.1246873460879262, -0.589717450520056]
 [-5.939256087226003, 3.065270688526023, 6.996462729059352, 3.0606487920687897, 1.6657161106686234, -2.8909427703487185, 3.602546456701424, 3.7085365766317717, -1.1272389633035202, 2.1639811681413588]
 [0.25574664586719953, 0.20723934197494973, 0.1820301098087354, -0.14165881186626883, -0.13573311287793918, -0.45188734045895335, 0.3805968535668568, -0.5840250030155854, -0.2911398397786437, -0.8474149800130504]
 [0.6789070285928156, 0.7634581343167878, -0.7769010352810415, 0.9224977228034071, 0.2159439936092345, 0.05045542810949032, 0.7051404936615981, -1.1300127485309313, -0.23439549047267869, -0.17745243473521696]
 [0.6789070285928156, 0.7634581343167878, -0.7769010352810415, 0.9224977228034071, 0.2159439936092345, 0.05045542810949032, 0.7051404936615981, -1.1300127485309313, -0.23439549047267869, -0.17745243473521696]
 [-0.2955252414905726, 0.23156158684154993, -1.3697300233746466, 0.6459832708740778, 0.10983901953627787, -0.375509659672494, 0.26194961448245035, -0.05631386488150584, 0.580979407417679, -1.175086930524446]
 ⋮
 [6.674549728570448, 13.949391850186814, 21.591111984112633, 2.8024739982021645, 15.263670745784552, -6.776925278536575, 7.409214152452891, -26.8071969881845, -2.033643078367319, 4.826590655307233]
 [0.014338900375658223, -0.08342189832398267, -0.09983799234405077, 0.05583610100211765, 0.15789261553341175, -0.3582156738912289, 0.06031793182841933, 0.002330341140554312, -0.055173667663799233, -4.060813317565849]
 [-194.48942013010276, 402.6667018323114, -87.78050763012354, 19.17780207363099, 64.84003295057344, 142.97330300857155, -86.86406743074821, 7.0697065455562065, 413.504671382537, 10.749298118516155]
 [-124.96452990069201, 34.41827180574219, -55.58745009766644, 92.2787952309879, 2.9850697502957737, 55.22090775652416, -168.09183839463822, -150.74635164394712, 108.94923825628919, 8.920449204522168]
 [-0.003197190172303578, 0.17679236031493833, 0.17116144959229468, 0.06355064919386627, -0.19184835629256336, -0.04645953763244997, 0.14924002510505194, -0.27272517428724663, -0.03644882390894103, -4.3389390633927984]
 [0.06532122688695537, -0.009911952345622624, 0.2652302896255144, -0.07072067543346454, -0.0017861392784118987, -0.009459789156814424, 0.24412706003581217, -0.31815938496048063, -0.0037311508230443375, -3.2689539673863868]
 [2.9355939779119367, -2.363218350892557, 1.0019003280514052, -0.9280533423433422, -2.5175199927003864, -3.2280233353695835, 4.4001077402903395, 0.17852039450650187, -1.0239379324254565, 1.3450937861590333]
 [0.24942122910143993, 0.3249210886669888, 0.1480936525864675, 0.17395226631927754, 0.5534592690846567, 0.025070965573098934, -0.07244174676643088, 0.10725295251117134, -0.12219512398929452, -2.889624502112472]
 [-10.389531488210356, -16.126734063310025, -5.546821111691614, -8.68602335213304, -28.72431653401113, -6.703490055625951, -3.0612474223348034, -2.108247823709377, 7.025115043972609, 4.921712680898986]

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:
    1.17851       1.25801       0.159701  …   -3.31606      0.486561
    6.20572       7.21195       2.9716        -8.00462      3.13685
   -0.102358     -0.00697793    0.044842       0.091392    -6.17929
   -0.0627523    -0.12335       0.139011       0.0175777   -4.24729
   -1.01517       0.0370429     0.340094      -0.124687    -0.589717
   -5.93926       3.06527       6.99646   …   -1.12724      2.16398
    0.255747      0.207239      0.18203       -0.29114     -0.847415
    0.678907      0.763458     -0.776901      -0.234395    -0.177452
    0.678907      0.763458     -0.776901      -0.234395    -0.177452
   -0.295525      0.231562     -1.36973        0.580979    -1.17509
    ⋮                                     ⋱                
    6.67455      13.9494       21.5911        -2.03364      4.82659
    0.0143389    -0.0834219    -0.099838      -0.0551737   -4.06081
 -194.489       402.667       -87.7805       413.505       10.7493
 -124.965        34.4183      -55.5875       108.949        8.92045
   -0.00319719    0.176792      0.171161  …   -0.0364488   -4.33894
    0.0653212    -0.00991195    0.26523       -0.00373115  -3.26895
    2.93559      -2.36322       1.0019        -1.02394      1.34509
    0.249421      0.324921      0.148094      -0.122195    -2.88962
  -10.3895      -16.1267       -5.54682        7.02512      4.92171

from which we can recover a vector of our samples:

x1_samples = reparam_chain[:, 1]
y_samples = reparam_chain[:, 10]
1000-element Vector{Float64}:
  0.4865611856385721
  3.1368456827018543
 -6.1792916816723595
 -4.247289337886385
 -0.589717450520056
  2.1639811681413588
 -0.8474149800130504
 -0.17745243473521696
 -0.17745243473521696
 -1.175086930524446
  ⋮
  4.826590655307233
 -4.060813317565849
 10.749298118516155
  8.920449204522168
 -4.3389390633927984
 -3.2689539673863868
  1.3450937861590333
 -2.889624502112472
  4.921712680898986
Back to top