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.29 seconds
Compute duration  = 7.29 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.0045    0.9965    0.0290   1184.0604   780.8105    1.0008    ⋯
    x_raw[1]   -0.0307    1.0128    0.0270   1406.3486   825.1024    1.0013    ⋯
    x_raw[2]   -0.0576    0.9742    0.0298   1069.5989   636.3127    1.0004    ⋯
    x_raw[3]    0.0066    0.9629    0.0329    857.2181   754.9340    1.0005    ⋯
    x_raw[4]   -0.0257    0.9960    0.0285   1222.8137   808.7797    0.9991    ⋯
    x_raw[5]   -0.0119    1.0023    0.0280   1262.7928   738.0395    1.0007    ⋯
    x_raw[6]   -0.0104    1.0010    0.0270   1378.3857   804.4935    1.0010    ⋯
    x_raw[7]    0.0191    0.9841    0.0289   1160.0075   767.1033    1.0028    ⋯
    x_raw[8]    0.0194    0.9918    0.0269   1382.3016   669.2901    0.9994    ⋯
    x_raw[9]    0.0102    0.9665    0.0252   1479.8801   706.9567    1.0034    ⋯
                                                                1 column omitted

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

       y_raw   -1.9742   -0.7016    0.0095    0.7032    1.8223
    x_raw[1]   -2.0630   -0.7029   -0.0703    0.6428    1.9959
    x_raw[2]   -1.9105   -0.7057   -0.0580    0.5907    1.8253
    x_raw[3]   -1.8642   -0.6703    0.0418    0.6161    1.8949
    x_raw[4]   -1.8953   -0.6950   -0.0298    0.6270    1.9582
    x_raw[5]   -1.8167   -0.7005   -0.0275    0.6375    1.8951
    x_raw[6]   -1.9453   -0.6707   -0.0174    0.6588    1.9549
    x_raw[7]   -1.9449   -0.6500    0.0166    0.6661    1.9270
    x_raw[8]   -1.9363   -0.6299    0.0638    0.7092    1.9015
    x_raw[9]   -1.7925   -0.5920   -0.0058    0.6750    1.7586

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.623014625152013, -5.598430914011998, 1.8542044066696979, 0.7949996667417717, -1.6364895645928834, -6.737593084339642, 5.278554089888163, -0.5226724925128964, 2.120341091848292, 2.7679131163302215]
 [4.697359005759272, 2.524405512249154, 0.4722680124575986, 0.12255624430502476, 0.7984405981342515, 3.8701356357017875, -3.0389109558581064, 0.5424636165861604, -0.5285875990817807, 1.504027469723602]
 [-0.9221608309608879, -0.3126678073247857, -0.3953393645875563, -0.0007845169280105706, 0.649041205204082, -0.00737469855147712, 0.4713110215486139, 0.5468599668073696, 0.12536727914139426, -1.430895681282509]
 [-1.1657217437495715, 0.7918145322902669, 0.1840833813935034, -0.0965408558362968, 1.6927946017166142, 0.07238384837725459, 2.151698963284374, 0.8367081935195064, 0.5225579168763801, -0.008183169172152172]
 [2.946884961713095, -0.9362404553780227, 0.036663754134383435, -2.56859220621097, 1.041523703821523, -1.4081604446723888, -3.893546520758072, -2.0938291501199346, -0.2738868969148337, 1.1933595891471556]
 [-0.9729742683830306, 0.8634905472949141, 0.021492658531930598, 0.6604800844234683, -0.12142593252302772, -0.43160336440259084, 0.7568191478482178, 0.6489812355781083, -0.33852991887532846, -1.3048414314254955]
 [-0.9729742683830306, 0.8634905472949141, 0.021492658531930598, 0.6604800844234683, -0.12142593252302772, -0.43160336440259084, 0.7568191478482178, 0.6489812355781083, -0.33852991887532846, -1.3048414314254955]
 [-0.16639455761333552, -0.09360821234700803, 0.22457016733610966, -0.40691938700942304, -0.40105780599346813, 0.2587681056843721, 0.2328425618296149, -0.18893897923875141, 0.3778734810582637, -2.4208236624951023]
 [2.5292030941850814, 1.7424300746177201, -1.9090214364793319, 2.6131709725275387, 6.37097953814657, -4.300786013734454, -3.8362324224061135, 1.0516814191146735, -2.586033037884757, 2.594109008777446]
 [-1.4042519613970343, 0.5194827478717681, 1.8570239267307145, 3.445554840472315, -3.7358230654670033, 0.22889741660640933, 2.613162325614037, 0.8933073713039505, 0.11537330062909801, 0.8915741885010792]
 ⋮
 [-0.2553989810099922, 0.02662104688465328, 0.03326025225873085, -0.04582483739365543, -0.0894764265826117, -0.07700091366308762, 0.01181487338567097, -0.15155755089073802, -0.10337078076544702, -3.2084246490108908]
 [4.685131777311126, -4.782747765989739, 19.920534660394527, 3.1030099271728395, -2.0863382342496557, 9.711912596148846, -0.8510688310348143, 17.711244345809668, 10.093142534636094, 6.4166191128157495]
 [0.0003776956263464649, -0.011081733171428946, -0.02806582456868483, -0.00925240077959069, -0.0025272409832307946, -0.003353087330246627, -0.003006225228452126, -0.0067755919538851584, -0.006675002965090408, -7.756947757362123]
 [-59.56353793235481, 51.36095467337971, 90.98560661412387, -29.372967394810715, 49.45900173232577, 19.02923285941226, 9.841503818976916, 45.62930065862701, 66.74490019367401, 9.098909295905544]
 [0.03132602756265828, 0.751109630140212, -0.6756694659099781, 0.6010049001503172, -0.6247591119200503, 1.713987506311361, 0.23372290718785851, -0.708127704001507, -0.023823339007616753, -0.26295098185226884]
 [0.6067459290664836, 0.2770243013273006, 0.13079217397369722, 0.48434986794800583, -0.41305165170157243, -0.2881788902274723, 0.08536918637003701, 0.04001220411694769, -0.03800067887945132, -1.3984043928457082]
 [-0.381773154669162, -0.17455739362914943, -0.30391113214533977, -0.538411598728484, -0.6419217536970417, 1.1757867192474396, -0.16963860468554398, 0.3974103250676658, 0.6001014085984934, -0.19780431310217028]
 [0.2269053827268816, -0.03263398745145003, 0.0930146668164503, -0.1325255985555043, -0.21973635005268274, 0.08857152672565319, -0.04860202939016287, 0.17691291983476923, 0.08408855787126271, -2.4949284639430473]
 [-1.757020529324008, -1.6170889334376937, 0.37261325356524694, 1.5881420408813651, 3.0633515009682935, -1.1809624866789268, 1.6963129972188444, -1.0389095828945232, -1.3183397642818093, 2.3116865092502845]

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.62301      -5.59843     1.8542     …   2.12034     2.76791
   4.69736       2.52441     0.472268      -0.528588    1.50403
  -0.922161     -0.312668   -0.395339       0.125367   -1.4309
  -1.16572       0.791815    0.184083       0.522558   -0.00818317
   2.94688      -0.93624     0.0366638     -0.273887    1.19336
  -0.972974      0.863491    0.0214927  …  -0.33853    -1.30484
  -0.972974      0.863491    0.0214927     -0.33853    -1.30484
  -0.166395     -0.0936082   0.22457        0.377873   -2.42082
   2.5292        1.74243    -1.90902       -2.58603     2.59411
  -1.40425       0.519483    1.85702        0.115373    0.891574
   ⋮                                    ⋱              
  -0.255399      0.026621    0.0332603     -0.103371   -3.20842
   4.68513      -4.78275    19.9205        10.0931      6.41662
   0.000377696  -0.0110817  -0.0280658     -0.006675   -7.75695
 -59.5635       51.361      90.9856        66.7449      9.09891
   0.031326      0.75111    -0.675669   …  -0.0238233  -0.262951
   0.606746      0.277024    0.130792      -0.0380007  -1.3984
  -0.381773     -0.174557   -0.303911       0.600101   -0.197804
   0.226905     -0.032634    0.0930147      0.0840886  -2.49493
  -1.75702      -1.61709     0.372613      -1.31834     2.31169

from which we can recover a vector of our samples:

x1_samples = reparam_chain[:, 1]
y_samples = reparam_chain[:, 10]
1000-element Vector{Float64}:
  2.7679131163302215
  1.504027469723602
 -1.430895681282509
 -0.008183169172152172
  1.1933595891471556
 -1.3048414314254955
 -1.3048414314254955
 -2.4208236624951023
  2.594109008777446
  0.8915741885010792
  ⋮
 -3.2084246490108908
  6.4166191128157495
 -7.756947757362123
  9.098909295905544
 -0.26295098185226884
 -1.3984043928457082
 -0.19780431310217028
 -2.4949284639430473
  2.3116865092502845
Back to top