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.76 seconds
Compute duration  = 7.76 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.0050    0.9817    0.0288   1168.7724   649.4251    1.0019    ⋯
    x_raw[1]   -0.0216    1.0261    0.0287   1268.0224   761.4163    0.9998    ⋯
    x_raw[2]    0.0065    0.9894    0.0317   1006.8453   727.1907    1.0034    ⋯
    x_raw[3]   -0.0405    1.0179    0.0311   1069.2843   810.2108    1.0013    ⋯
    x_raw[4]    0.0375    0.9969    0.0274   1336.4366   782.5589    1.0066    ⋯
    x_raw[5]    0.0127    1.0073    0.0289   1247.4958   806.6106    1.0076    ⋯
    x_raw[6]   -0.0272    1.0027    0.0264   1437.6465   628.6523    1.0113    ⋯
    x_raw[7]    0.0341    0.9872    0.0269   1348.2653   790.9478    0.9992    ⋯
    x_raw[8]   -0.0066    1.0086    0.0327    952.0118   745.6237    0.9995    ⋯
    x_raw[9]   -0.0213    1.0033    0.0249   1630.8551   844.6288    1.0017    ⋯
                                                                1 column omitted

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

       y_raw   -1.9467   -0.6629    0.0159    0.6465    1.8837
    x_raw[1]   -1.9617   -0.7040   -0.0381    0.6841    1.9654
    x_raw[2]   -1.9450   -0.6493    0.0091    0.6306    1.9576
    x_raw[3]   -2.0403   -0.7227   -0.0411    0.6174    1.9416
    x_raw[4]   -1.9037   -0.5896    0.0164    0.6414    2.1811
    x_raw[5]   -1.8859   -0.7419   -0.0173    0.7597    1.8596
    x_raw[6]   -1.9381   -0.7029   -0.0134    0.6665    1.8188
    x_raw[7]   -1.9328   -0.6392    0.0389    0.6864    2.0557
    x_raw[8]   -2.0051   -0.7094    0.0108    0.7211    1.8734
    x_raw[9]   -2.1235   -0.6978    0.0064    0.6199    2.0228

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}}:
 [-6.9308909367470894, 6.161057972480689, -1.3737267911882622, -10.04329831207582, 12.359979915407271, 20.923426023189574, -8.019212800564064, 10.603156719822415, 5.090224940440468, 4.5582658278174915]
 [2.590017085469881, 9.651255540284627, -3.678248326611069, -4.352898762008939, 4.784548806314627, 10.631038021015838, 3.4929451248759213, 10.799400145424205, 6.4321424972125465, 4.143104564259796]
 [-0.10797916342623322, -0.04468878681057731, -0.059831656235457685, 0.04414261400202078, -0.11204584743889848, -0.2704573842727217, -0.054879669224907675, -0.1858718719003321, -0.19287451138516834, -3.1407311811389054]
 [5.109414687423551, 2.8668588199836518, -1.3435842306015469, -1.4597390464690783, 2.2932655899680987, 5.352323872991354, 0.8746502550934188, -0.22056827431270037, 3.342600798708476, 2.9586186576909865]
 [-0.11138951744250096, 0.3245009868905333, 0.3169911223239834, 0.03021408900381637, -0.21251087780696806, 0.10447186518149111, 0.11692631358737697, -0.07482008485121601, -0.4453708342571407, -3.659838673685589]
 [-0.3608953989160053, 0.4518327424855591, -0.1568401152813258, 0.0859716810989969, 0.3281045843152074, -0.2654258861482167, 0.05202911979568314, -0.0948189598721803, -0.150810721659652, -3.0317324675192925]
 [-4.575712965130881, 0.9554847095782364, -4.745859952494173, -3.662475645216825, -5.531547049133647, 5.030084555639231, -6.002873967389168, -0.20407957203837418, -0.43494597533735374, 2.8085187736040353]
 [-0.8207634378796217, 0.6981682094928487, -1.5721428503187997, -1.230283235474628, -0.5491237454119143, -0.5686125119510222, -2.1937803425292337, -0.38877868891924255, -0.5008238568871604, -0.12689361996035342]
 [-15.666664043765058, 26.213862660560167, -29.56632540978944, 6.0188599093831945, -20.250637337728087, 36.238844764746034, 18.670932477297278, 17.506293579889768, 14.731096411704373, 5.818474091330546]
 [0.4090139110796011, -1.0800156085582233, -2.8951388615534546, -1.1658822519240069, 4.315559803869711, -0.11934009114611213, 0.529920888443598, 0.20453297237700302, -2.074676671647671, 1.4609285342787295]
 ⋮
 [-0.530602361659156, 0.05942239393051104, -0.35703859593861564, 0.24864931182469485, 0.8025552115134794, -0.7574100541468967, 0.3073407889990456, -0.2308431181808279, -0.10960925535701474, -1.7875529440501121]
 [-4.0624986661176425, 0.891813798012638, 1.502989199700422, 1.4190993511435683, 2.685158018549825, 0.9407250929893342, -0.5213022885246767, -1.6412680008023748, -1.6329215711801444, 1.1697814654312666]
 [0.3898868028020006, -0.11846838569449956, -0.09646337805580278, -0.08786867386735636, -0.17878909733450885, 0.3051396002134308, 0.14573846364258328, 0.20158205773366075, -0.061932220669915425, -3.4277989342923236]
 [0.32504680497504557, -0.3192884839031505, -0.5414586035395614, 0.03485843780163072, -0.2751849497095807, 0.5059024535367527, 0.4207136935472856, 0.13034101913500024, -0.051467936987668356, -2.2961522509384293]
 [3.8658271709579424, -3.84317175644219, -3.735084364750772, 3.5956434788512066, 2.876751871132674, 0.5394828014639138, 3.92314675513344, -2.03826116221592, -0.006490498468189088, 1.8062956548892029]
 [-1.9572992942309189, 1.4886446145630396, 0.5868874081994906, -1.923548051503399, -1.4365965523576787, -0.22109491898399924, -1.964178728155154, 0.6878056839958194, 0.11948578566877151, 0.2962327588039395]
 [0.7126941386472837, -0.9501431286044005, 0.2467171129102121, 0.7450099472859907, 0.5641791560653105, 0.09697641400903771, 0.8046828342261706, -0.227610026883948, 0.012648492913900508, -1.7311015184748837]
 [-0.03663236517325896, -0.15634264417883537, 0.1961896304547474, -0.14547915353768165, 0.13131593886383405, -0.19928110948143762, 0.26012267509409454, 0.013028911315095714, -0.017346292055121837, -4.088182183821857]
 [-0.3983193527576031, 0.30552684301483385, -0.02252216773785046, -0.003961461717470089, 0.8985605381572712, 1.1950116058834115, -0.6420738689193598, 0.004353956693831042, -0.10495274693314106, -0.7193063487573979]

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:
  -6.93089     6.16106     -1.37373    …  10.6032       5.09022     4.55827
   2.59002     9.65126     -3.67825       10.7994       6.43214     4.1431
  -0.107979   -0.0446888   -0.0598317     -0.185872    -0.192875   -3.14073
   5.10941     2.86686     -1.34358       -0.220568     3.3426      2.95862
  -0.11139     0.324501     0.316991      -0.0748201   -0.445371   -3.65984
  -0.360895    0.451833    -0.15684    …  -0.094819    -0.150811   -3.03173
  -4.57571     0.955485    -4.74586       -0.20408     -0.434946    2.80852
  -0.820763    0.698168    -1.57214       -0.388779    -0.500824   -0.126894
 -15.6667     26.2139     -29.5663        17.5063      14.7311      5.81847
   0.409014   -1.08002     -2.89514        0.204533    -2.07468     1.46093
   ⋮                                   ⋱                           
  -0.530602    0.0594224   -0.357039      -0.230843    -0.109609   -1.78755
  -4.0625      0.891814     1.50299       -1.64127     -1.63292     1.16978
   0.389887   -0.118468    -0.0964634      0.201582    -0.0619322  -3.4278
   0.325047   -0.319288    -0.541459       0.130341    -0.0514679  -2.29615
   3.86583    -3.84317     -3.73508    …  -2.03826     -0.0064905   1.8063
  -1.9573      1.48864      0.586887       0.687806     0.119486    0.296233
   0.712694   -0.950143     0.246717      -0.22761      0.0126485  -1.7311
  -0.0366324  -0.156343     0.19619        0.0130289   -0.0173463  -4.08818
  -0.398319    0.305527    -0.0225222      0.00435396  -0.104953   -0.719306

from which we can recover a vector of our samples:

x1_samples = reparam_chain[:, 1]
y_samples = reparam_chain[:, 10]
1000-element Vector{Float64}:
  4.5582658278174915
  4.143104564259796
 -3.1407311811389054
  2.9586186576909865
 -3.659838673685589
 -3.0317324675192925
  2.8085187736040353
 -0.12689361996035342
  5.818474091330546
  1.4609285342787295
  ⋮
 -1.7875529440501121
  1.1697814654312666
 -3.4277989342923236
 -2.2961522509384293
  1.8062956548892029
  0.2962327588039395
 -1.7311015184748837
 -4.088182183821857
 -0.7193063487573979
Back to top