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.62 seconds
Compute duration  = 7.62 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.0400    1.0027    0.0294   1163.2379   631.8591    1.0031    ⋯
    x_raw[1]   -0.0359    1.0236    0.0299   1167.8529   773.1359    0.9992    ⋯
    x_raw[2]   -0.0326    1.0063    0.0315   1011.0338   628.1608    1.0007    ⋯
    x_raw[3]    0.0273    0.9740    0.0274   1271.3146   696.9770    0.9993    ⋯
    x_raw[4]    0.0126    1.0691    0.0340   1004.8201   669.5703    0.9995    ⋯
    x_raw[5]   -0.0313    0.9383    0.0289   1054.5208   798.8780    1.0043    ⋯
    x_raw[6]   -0.0142    0.9512    0.0272   1243.8009   874.6563    0.9995    ⋯
    x_raw[7]    0.0347    1.0219    0.0246   1705.5102   874.9893    1.0011    ⋯
    x_raw[8]    0.0018    1.0310    0.0315   1081.7376   594.5669    1.0009    ⋯
    x_raw[9]   -0.0113    0.9779    0.0290   1148.5634   718.9735    1.0012    ⋯
                                                                1 column omitted

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

       y_raw   -1.8997   -0.6737    0.0643    0.7172    2.0282
    x_raw[1]   -2.0224   -0.7182   -0.0710    0.6143    1.9904
    x_raw[2]   -1.9999   -0.7343   -0.0259    0.5997    2.0489
    x_raw[3]   -1.8571   -0.6774   -0.0014    0.7007    1.9614
    x_raw[4]   -2.0541   -0.6578   -0.0141    0.6714    2.1571
    x_raw[5]   -1.7415   -0.6525   -0.0740    0.5910    1.8461
    x_raw[6]   -1.7698   -0.6731   -0.0266    0.6214    1.8756
    x_raw[7]   -1.9006   -0.6822    0.0484    0.7253    1.9915
    x_raw[8]   -2.1200   -0.6722    0.0164    0.6996    1.9861
    x_raw[9]   -1.9528   -0.6679   -0.0052    0.6341    1.8567

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.23982806014642302, -12.026031617902802, -2.037406379263268, 0.5594510461073163, 5.649152016365621, -0.6874130546084651, -1.4171048444870948, 3.130594494947662, -5.00201437836052, 3.3511183334725394]
 [-0.12619722224318655, 0.1963229259254191, 0.059206585312345336, -0.04577023599804678, -0.04197735553435888, 0.14126259652672565, 0.09975994040542863, -0.1247162728213712, 0.1820695798465833, -3.9628316810088817]
 [-3.8299313415625558, 1.6572191755730632, 0.43446450454033847, 0.9314154292653569, 4.186246039674738, -0.06999250702776269, 4.012709018135856, 0.48017187454863364, 5.750020488883771, 2.295014056767556]
 [0.1004520642550158, 0.04448452990722953, -0.08496043261406988, 0.2369524784466463, -0.696550940825862, 0.12121923273532671, -0.49546940351085483, -0.05692732668868106, -0.8391903248406145, -1.69976861322536]
 [-1.2894248626225482, 0.029371492684234165, 2.8860302085467655, -1.5020353742827326, 3.5001240264777445, 0.775998057920976, 0.530003973664125, 1.3311742972983418, 2.4289362481623535, 1.5024139272283774]
 [0.648833738598825, 0.7703061426258422, -1.9602676185074284, 0.4388964946763127, -2.0368834056210425, -0.21088904584083432, 0.1994721848675709, -2.800533638215846, -0.9449558253872753, 0.9879452878292841]
 [-0.09060957248744408, -0.16147732300177306, 0.8395858295296349, 0.4802644121778247, 0.593680225624215, 0.18732986743643143, 0.0897268857471661, 1.3447192631815323, 0.07931336921665548, -0.6987107961205955]
 [-6.935887585482537, 8.897606746168771, -16.441358529613204, 3.0729858833113233, -14.928764226760997, -5.351214907847597, 2.7705431606376614, 6.123084441488565, -4.876265416239628, 5.319411266059806]
 [0.07151367505013011, 0.01450221739287114, 0.12016953297409314, 0.050246445775428736, -0.03975842449768436, -0.05876753545525245, 0.0717564711088958, -0.07040075223905809, 0.012936256431243236, -4.695836220118558]
 [0.07151367505013011, 0.01450221739287114, 0.12016953297409314, 0.050246445775428736, -0.03975842449768436, -0.05876753545525245, 0.0717564711088958, -0.07040075223905809, 0.012936256431243236, -4.695836220118558]
 ⋮
 [1.7449551592163872, 0.3788784787887092, -3.419330237037078, 0.016663174710110393, 3.603018179653373, 4.373477895447204, 0.9285843982251076, 1.5414395059292816, 3.826843322277896, 1.9113965106302913]
 [-0.11937158194650327, -0.2792552428780874, 0.474732896311477, -0.13775484879004574, -0.44827574918468, -0.2813907664676357, -0.2911697184803863, -0.23499640029415997, -0.582909451968033, -2.099289885773954]
 [0.027875842203767476, -0.933606171673349, -0.9212519746933422, 0.12725882146268727, -0.04593623074868963, -0.5950276371760319, 0.5361014574799465, -0.9957756401097699, -0.46327746095888056, -0.7289323104358741]
 [-2.339420683026421, -5.528297056473322, 3.783342867954171, -1.9527867209595058, 0.7275159815422768, 1.6577289038922567, -1.8751550778438515, -4.121908004768926, 0.239537711332354, 1.8681190349555554]
 [0.3921894251640791, 0.6406305998424096, -0.5291534041623008, 0.22422633220460172, 0.09636612628135634, -0.16854404799152353, 0.293869188686642, 0.4624439946700869, 0.0737250705629036, -2.109062364479479]
 [0.2624998114823184, 0.4042093521968575, -0.5111586042093197, 0.10937146720512729, 0.33641775615524583, 0.044779034529036275, 0.336408657364416, 0.11883742518089309, 0.5519770330933399, -2.1462762303815586]
 [-2.206401409931068, -2.9463988226696602, 4.8296432526733, -0.3606018469401971, -2.8863501742359063, 0.9904059723965123, -3.2948582030364353, -1.9980417562668613, -5.196753708768799, 2.411375326058371]
 [0.5760149010476694, -0.2842918774727067, 0.8552956666744523, -0.08131986841772537, -0.0868948569239001, -0.5291363494658542, -1.1493480090969428, 0.15571637668511165, 0.8463276848854849, -0.35816918723461266]
 [-0.9912940641599162, -0.21215003099273033, -1.181274468502636, 1.2290884197072003, -1.6355596171768325, 0.17437520953729732, 1.5448423684267587, -0.2646355084725329, -0.9996426496489649, 0.2438912964463087]

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.239828   -12.026       -2.03741    …   3.13059    -5.00201     3.35112
 -0.126197     0.196323     0.0592066     -0.124716    0.18207    -3.96283
 -3.82993      1.65722      0.434465       0.480172    5.75002     2.29501
  0.100452     0.0444845   -0.0849604     -0.0569273  -0.83919    -1.69977
 -1.28942      0.0293715    2.88603        1.33117     2.42894     1.50241
  0.648834     0.770306    -1.96027    …  -2.80053    -0.944956    0.987945
 -0.0906096   -0.161477     0.839586       1.34472     0.0793134  -0.698711
 -6.93589      8.89761    -16.4414         6.12308    -4.87627     5.31941
  0.0715137    0.0145022    0.12017       -0.0704008   0.0129363  -4.69584
  0.0715137    0.0145022    0.12017       -0.0704008   0.0129363  -4.69584
  ⋮                                    ⋱                          
  1.74496      0.378878    -3.41933        1.54144     3.82684     1.9114
 -0.119372    -0.279255     0.474733      -0.234996   -0.582909   -2.09929
  0.0278758   -0.933606    -0.921252      -0.995776   -0.463277   -0.728932
 -2.33942     -5.5283       3.78334       -4.12191     0.239538    1.86812
  0.392189     0.640631    -0.529153   …   0.462444    0.0737251  -2.10906
  0.2625       0.404209    -0.511159       0.118837    0.551977   -2.14628
 -2.2064      -2.9464       4.82964       -1.99804    -5.19675     2.41138
  0.576015    -0.284292     0.855296       0.155716    0.846328   -0.358169
 -0.991294    -0.21215     -1.18127       -0.264636   -0.999643    0.243891

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.3511183334725394
 -3.9628316810088817
  2.295014056767556
 -1.69976861322536
  1.5024139272283774
  0.9879452878292841
 -0.6987107961205955
  5.319411266059806
 -4.695836220118558
 -4.695836220118558
  ⋮
  1.9113965106302913
 -2.099289885773954
 -0.7289323104358741
  1.8681190349555554
 -2.109062364479479
 -2.1462762303815586
  2.411375326058371
 -0.35816918723461266
  0.2438912964463087
Back to top