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.68 seconds
Compute duration  = 7.68 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.0018    0.9954    0.0220   2071.3695   594.7040    0.9994    ⋯
    x_raw[1]   -0.0299    1.0551    0.0217   2387.3317   843.9815    0.9995    ⋯
    x_raw[2]   -0.0270    0.9994    0.0271   1348.0861   684.0823    0.9994    ⋯
    x_raw[3]    0.0096    0.9678    0.0229   1774.6329   843.6248    1.0021    ⋯
    x_raw[4]   -0.0429    0.9669    0.0221   1911.6878   794.0414    0.9998    ⋯
    x_raw[5]    0.0181    1.0084    0.0243   1716.6541   548.9811    1.0003    ⋯
    x_raw[6]   -0.0018    0.9579    0.0214   2016.7050   769.6006    0.9995    ⋯
    x_raw[7]    0.0241    0.9850    0.0241   1682.3846   834.3285    1.0001    ⋯
    x_raw[8]    0.0550    0.9907    0.0266   1383.6713   909.8848    1.0031    ⋯
    x_raw[9]    0.0160    0.9852    0.0245   1569.0148   664.3415    0.9991    ⋯
                                                                1 column omitted

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

       y_raw   -1.9836   -0.6719    0.0317    0.6695    1.9966
    x_raw[1]   -2.0123   -0.7770   -0.0255    0.7077    2.0225
    x_raw[2]   -2.0381   -0.7030    0.0062    0.5948    1.9447
    x_raw[3]   -1.8766   -0.6640    0.0598    0.6462    1.8647
    x_raw[4]   -1.9850   -0.7316   -0.0255    0.6240    1.9473
    x_raw[5]   -1.8659   -0.6410    0.0089    0.6813    1.9686
    x_raw[6]   -1.8156   -0.6873    0.0047    0.6214    1.8444
    x_raw[7]   -1.7783   -0.6440   -0.0055    0.6895    1.9448
    x_raw[8]   -1.8930   -0.5992    0.0633    0.6931    1.9042
    x_raw[9]   -1.8223   -0.7365    0.0168    0.7223    1.9370

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.6536642889710294, -1.119217015878384, 2.8336903544903107, 2.483302130485879, 0.9450805187500034, -0.43390246100748914, 1.7402645273479076, 7.607988580474797, -2.576850621170942, 1.6768754358822404]
 [4.742766653977721, -3.2366116443876103, 4.172748095688014, 1.9882405046816374, 2.2447541009865652, -2.8003129215933518, 0.5754932848337623, -12.088703298740981, 0.686625773731138, 2.8236781621106046]
 [-0.5484745242172931, -3.85301240990464, -6.583929303401666, -8.520620982033616, 2.025852606218094, -0.6048136686150379, 9.363297419563423, 5.928551039252448, 2.589986821864001, 2.7331187090005202]
 [-2.4063406992481418, -4.031472056438481, -2.8291368308283733, 0.03283285801522207, -0.14822158183419945, -1.3884475408837382, 1.732279690342313, 3.642188387974975, -0.1865683750400276, 1.3396821838347832]
 [-0.5530633324774467, -0.45196284666349507, -1.0897617096808057, -2.12293593946898, 1.0625583486491637, -1.4336678726597714, 0.3597414899098281, 1.1809886555956695, 0.1328800110652876, -0.351061560354475]
 [1.3887009636091823, 1.062177905617531, 1.3455081864870613, 0.5399275720796617, -1.6690730031742829, 0.9046033227184981, -0.031428308250497757, -0.7373649492426643, 1.619544142810378, 0.6488062448479575]
 [-0.7733830441789099, 0.031464278918045645, 0.997547358116923, 1.053069402624567, 0.079370235569387, 0.9821525216862576, 0.1976719907291822, 1.3270800829246316, -0.6836149103987148, -0.3255524236561016]
 [-0.1030242933581406, 0.12996848314516296, -2.2324392287136243, -0.3380526794950338, -0.3519057649877718, -0.3799123134897115, 0.5617022800111928, -1.0105493110302772, 0.27740012912660367, -0.4796967098892621]
 [0.04517097908990186, 0.41015364993540726, 0.8754741985357725, -0.37467801320975774, -0.283649445294305, 1.191145612846075, 0.49647969170177525, -0.6040814652068606, -0.4648952936834116, -0.6234016517621068]
 [0.04517097908990186, 0.41015364993540726, 0.8754741985357725, -0.37467801320975774, -0.283649445294305, 1.191145612846075, 0.49647969170177525, -0.6040814652068606, -0.4648952936834116, -0.6234016517621068]
 ⋮
 [0.14477713070762302, -0.4569148835852146, 0.30347422872483243, -1.5427992219684716, -0.5267262262287636, -0.09245535533213158, -0.02425694514467755, 0.47396689795654867, -0.7470033634478498, -1.0099477114986835]
 [-0.8096525564776064, 0.5183595725319005, 0.5560038952013339, -1.0773765075052526, -0.2870613080287448, -0.9514949760907917, 0.9426040754766322, 0.49969911520627897, 0.032299107789424325, 0.09552276168334528]
 [0.09060050040705388, 0.07311677860054468, -0.2537064323261237, -0.012465914773632658, 0.6574919538181037, -0.1815628335200281, 0.21154459876375337, 0.16561153192159714, -0.453464511455056, -2.34212995090863]
 [0.8220742095211874, -0.6389712752242055, 1.9928138558217263, -0.013366538004836967, 4.133618917028231, 0.4652930819109011, 3.356324263194469, -0.6596190570432642, 4.886489170470832, 1.747015497690675]
 [-0.1339815366167952, -0.04629284498961533, -0.373156793503007, 0.06436349332836191, -0.8511935817681237, 0.061820752749509586, -0.5846454474497389, 0.09886483571451732, -1.0057995130377353, -1.4277392502800192]
 [0.1592237597766523, 0.08785548975885304, -0.09083152562729573, 0.3477353407944944, 0.07112223397134002, 0.23993755571867267, 0.04517073981263179, -0.11895548997258933, -0.20893700188273076, -3.6782525437422278]
 [0.31771748157410257, 0.12137435582831853, -0.015955349723269185, 0.15336274504913433, -0.23274535635640642, 0.3572341803901132, -0.15348793504883146, -0.30191004680938127, -0.03265677972881116, -3.0862371464233522]
 [-1.7275620240058716, -0.9154373194803506, 0.36747218766976875, -0.7919331161442311, 1.357932279665118, -2.08248904594233, 0.6597313892345397, 1.8043403499532435, 0.05134179453913019, 0.4571376829540501]
 [-2.9028283536200883, -2.5841895809450315, 1.876535935330348, -11.831028622116618, 3.0995424343542695, -5.48654677563208, 6.305591314276628, 5.8940838560699325, 1.3018688435360766, 3.3678687707512545]

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.653664   -1.11922     2.83369    …    7.60799    -2.57685     1.67688
  4.74277    -3.23661     4.17275       -12.0887      0.686626    2.82368
 -0.548475   -3.85301    -6.58393         5.92855     2.58999     2.73312
 -2.40634    -4.03147    -2.82914         3.64219    -0.186568    1.33968
 -0.553063   -0.451963   -1.08976         1.18099     0.13288    -0.351062
  1.3887      1.06218     1.34551    …   -0.737365    1.61954     0.648806
 -0.773383    0.0314643   0.997547        1.32708    -0.683615   -0.325552
 -0.103024    0.129968   -2.23244        -1.01055     0.2774     -0.479697
  0.045171    0.410154    0.875474       -0.604081   -0.464895   -0.623402
  0.045171    0.410154    0.875474       -0.604081   -0.464895   -0.623402
  ⋮                                  ⋱                           
  0.144777   -0.456915    0.303474        0.473967   -0.747003   -1.00995
 -0.809653    0.51836     0.556004        0.499699    0.0322991   0.0955228
  0.0906005   0.0731168  -0.253706        0.165612   -0.453465   -2.34213
  0.822074   -0.638971    1.99281        -0.659619    4.88649     1.74702
 -0.133982   -0.0462928  -0.373157   …    0.0988648  -1.0058     -1.42774
  0.159224    0.0878555  -0.0908315      -0.118955   -0.208937   -3.67825
  0.317717    0.121374   -0.0159553      -0.30191    -0.0326568  -3.08624
 -1.72756    -0.915437    0.367472        1.80434     0.0513418   0.457138
 -2.90283    -2.58419     1.87654         5.89408     1.30187     3.36787

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.6768754358822404
  2.8236781621106046
  2.7331187090005202
  1.3396821838347832
 -0.351061560354475
  0.6488062448479575
 -0.3255524236561016
 -0.4796967098892621
 -0.6234016517621068
 -0.6234016517621068
  ⋮
 -1.0099477114986835
  0.09552276168334528
 -2.34212995090863
  1.747015497690675
 -1.4277392502800192
 -3.6782525437422278
 -3.0862371464233522
  0.4571376829540501
  3.3678687707512545
Back to top