Tracking Extra Quantities

Often, there are quantities in models that we might be interested in viewing the values of, but which are not random variables in the model that are explicitly drawn from a distribution.

As a motivating example, the most natural parameterization for a model might not be the most computationally feasible. Consider the following (efficiently reparametrized) implementation of Neal’s funnel (Neal, 2003):

using Turing
setprogress!(false)

@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 nothing
end
[ Info: [Turing]: progress logging is disabled globally
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.

There are two ways to track these extra quantities in Turing.jl.

Using := (during inference)

The first way is to use the := operator, which behaves exactly like = except that the values of the variables on its left-hand side are automatically added to the chain returned by the sampler. For example:

@model function Neal_coloneq()
    # 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
end

sample(Neal_coloneq(), NUTS(), 1000)
Info: Found initial step size
  ϵ = 1.6
Chains MCMC chain (1000×32×1 Array{Float64, 3}):

Iterations        = 501:1:1500
Number of chains  = 1
Samples per chain = 1000
Wall duration     = 7.8 seconds
Compute duration  = 7.8 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], y, x[1], x[2], x[3], x[4], x[5], x[6], x[7], x[8], x[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

Use `describe(chains)` for summary statistics and quantiles.

Using returned (post-inference)

Alternatively, one can specify the extra quantities as part of the model function’s return statement:

@model function Neal_return()
    # Raw draws
    y_raw ~ Normal(0, 1)
    x_raw ~ arraydist([Normal(0, 1) for i in 1:9])

    # Transform and return as a NamedTuple
    y = 3 * y_raw
    x = exp.(y ./ 2) .* x_raw
    return (x=x, y=y)
end

chain = sample(Neal_return(), NUTS(), 1000)
Info: Found initial step size
  ϵ = 3.2
Chains MCMC chain (1000×22×1 Array{Float64, 3}):

Iterations        = 501:1:1500
Number of chains  = 1
Samples per chain = 1000
Wall duration     = 1.94 seconds
Compute duration  = 1.94 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

Use `describe(chains)` for summary statistics and quantiles.

The sampled chain does not contain x and y, but we can extract the values using the returned function. Calling this function outputs an array:

nts = returned(Neal_return(), chain)
1000×1 Matrix{@NamedTuple{x::Vector{Float64}, y::Float64}}:
 (x = [2.708481534435943, -0.12141941892505616, -2.367926995125931, -0.1600323260595759, -2.0301937327207273, 0.7378599452947469, -1.790970561361812, -1.507671966032512, -0.4103310106094694], y = 0.3874738809766588)
 (x = [-0.48397418357907046, 0.07504007779008906, 2.1798107936100117, -0.47299211429993065, 1.105423053491242, 0.004469670198642937, -0.8025730351809388, -0.06650732256889401, 0.936880705449163], y = 0.9239526687157988)
 (x = [-2.1322209626999773, -0.7168510361841711, 2.0964585545116567, -3.735456565709328, 3.3425372193050955, -0.26443660763259685, -4.045883734207802, -3.746218224033128, -0.4978371375103653], y = 2.8306149713896134)
 (x = [0.06548012650792276, -0.12953218072911615, -0.06624537521456071, 0.062345763665429994, -0.15756676458625665, 0.10838638669136996, -0.018940734168695225, -0.004803877337116265, 0.006695985663234425], y = -4.20231642977701)
 (x = [0.743598182019506, -0.3039806089289758, -0.2104855424597844, 0.013604349805470442, -0.6474395304499168, 0.21260157417148784, 0.03284655282828881, -0.5692960380487038, 0.7510292583471134], y = -1.1168401344229133)
 (x = [0.3526696132562072, -0.11802170368631486, 0.2806246089146878, 0.47891594390010916, -0.10010416273475063, 0.09904195475946467, -0.15051619418510764, -0.5760810747700775, 0.06380587509253423], y = -2.166138443912115)
 (x = [-3.3916452953642584, -1.1973709092684957, -2.4392884278777105, -4.209051576222259, 0.5474612629127071, 0.4054729705240938, -3.5514531966622314, 4.635655082222602, -0.9287046406184152], y = 2.168874845210044)
 (x = [0.053615326684572945, -0.14864868376237222, 0.2579906922406122, 0.30065342361655994, 0.09713122878032991, 0.058976161191843395, 0.13260298907385235, -0.47727657760789, 0.09983779401871411], y = -2.486613366939073)
 (x = [-0.1379762072385997, 0.5430053960607835, -0.567602056747569, -0.13069911239836596, -0.3875506708861083, -0.26686889830683114, -0.3057063345343988, 1.5049014783187933, -0.5252348352906646], y = 0.2546305912340505)
 (x = [0.675725796433604, -2.9298057513978666, 8.44961285484613, 13.469067128780631, -16.605954342627445, -1.9808456018200682, 3.530957669116475, -0.5169924570299583, -1.3768703661690975], y = 4.060026167115346)
 ⋮
 (x = [0.13234847549019244, -0.09734832387309965, 0.046183896001708406, 0.27810835284343083, -0.01001218690820942, -0.09901259063757017, -0.034573818532132594, 0.03100423575955887, -0.2808253468686141], y = -3.9953370701629725)
 (x = [-11.154446964109173, 2.728573765792313, -1.1424882728414891, -17.857562778548033, 4.538698473344972, -1.505267723594426, 8.219838546230545, -9.609079677356398, 16.24941038102967], y = 4.396137569688728)
 (x = [-4.5989792842618735, 1.6874775230597825, 0.8666727675220327, -3.2281854151196168, -0.36236909957254065, -1.0494623979238684, 0.21402046250880608, 1.853568198311815, 0.058097654749576226], y = 1.328072780103359)
 (x = [0.14910111719415242, -0.09080563512355652, -0.03125116505742404, 0.07669754332511211, 0.18192658701718012, 0.03873720514165291, 0.0678830712547326, -0.060795986584633505, 0.021811386682200472], y = -4.802834550257166)
 (x = [0.02215829115588249, -0.1118375636682968, 0.07481606802594265, 0.07376571785270833, 0.1711402164814821, -0.06643182000340983, -0.11576607336438419, -0.05736807437175226, 0.059570465269114796], y = -4.910886516976311)
 (x = [0.3343895985826651, 5.5793918202592785, -0.41126908933987827, -4.886980019876195, -1.8004511050615701, -3.441880009865616, 0.7083155924691512, 2.807716823244124, -3.309296563854751], y = 2.780436318562607)
 (x = [-7.999671910617071, 14.2660032483598, 3.158064959430704, 6.31019238714819, -17.327320209314433, -1.365165130511749, -1.5867501656222733, 7.3567904603672645, -8.480850515608996], y = 4.707571207389604)
 (x = [23.336210796585974, 12.618725440129623, -14.405063228173011, 31.125889035366146, 6.248030173610658, 19.89037814426967, -0.3274665408655737, 22.61231942804166, -7.043666236182368], y = 5.942838429976555)
 (x = [-0.022053405600500637, -0.033940820753977544, 0.020472909208218034, -0.05183881061401079, 0.007128140974553822, 0.0077483854663920205, 0.018956550662730862, -0.04902694130664566, 0.020642906763408034], y = -7.018182281469548)

where each element of which is a NamedTuple, as specified in the return statement of the model.

nts[1]
(x = [2.708481534435943, -0.12141941892505616, -2.367926995125931, -0.1600323260595759, -2.0301937327207273, 0.7378599452947469, -1.790970561361812, -1.507671966032512, -0.4103310106094694], y = 0.3874738809766588)

Which to use?

There are some pros and cons of using returned, as opposed to :=.

Firstly, returned is more flexible, as it allows you to track any type of object; := only works with variables that can be inserted into an MCMCChains.Chains object. (Notice that x is a vector, and in the first case where we used :=, reconstructing the vector value of x can also be rather annoying as the chain stores each individual element of x separately.)

A drawback is that naively using returned can lead to unnecessary computation during inference. This is because during the sampling process, the return values are also calculated (since they are part of the model function), but then thrown away. So, if the extra quantities are expensive to compute, this can be a problem.

To avoid this, you will essentially have to create two different models, one for inference and one for post-inference. The simplest way of doing this is to add a parameter to the model argument:

@model function Neal_coloneq_optional(track::Bool)
    # Raw draws
    y_raw ~ Normal(0, 1)
    x_raw ~ arraydist([Normal(0, 1) for i in 1:9])

    if track
        y = 3 * y_raw
        x = exp.(y ./ 2) .* x_raw
        return (x=x, y=y)
    else
        return nothing
    end
end

chain = sample(Neal_coloneq_optional(false), NUTS(), 1000)
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     = 1.85 seconds
Compute duration  = 1.85 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

Use `describe(chains)` for summary statistics and quantiles.

The above ensures that x and y are not calculated during inference, but allows us to still use returned to extract them:

returned(Neal_coloneq_optional(true), chain)
1000×1 Matrix{@NamedTuple{x::Vector{Float64}, y::Float64}}:
 (x = [1.2892064192277952, -2.130355013642776, -0.25091883104302115, 0.37459940656107343, -1.2224032657766837, -0.7266057742338711, -1.3264648886044406, 0.38225052768784473, -0.2582418799137356], y = 0.07343433340456329)
 (x = [0.12376059678233776, -0.4433779322796216, -0.03173262712921502, 0.013343170640830088, -0.005227400421891206, 0.09895747787868493, -0.17367273387337778, -0.041169422517034385, 0.18472605986597795], y = -3.8808260378275103)
 (x = [0.017143528852166717, -0.19720729425393455, 0.08217303012014684, -0.15496352399902485, -0.00918506677107581, -0.12542913388436291, -0.10478702466808046, -0.054943641218115985, 0.06683427533552547], y = -4.247261060524986)
 (x = [-0.013461584012054344, -0.641316944669885, 0.0713832105375498, -0.08589818291914401, -0.09007888661838975, -0.3915316793635361, -0.6953207253013028, -0.09914214300703002, -0.3812704957830031], y = -1.1272847634497822)
 (x = [-0.34339332994015415, -0.15806526678105426, 1.0241723162152159, 0.22231207387696508, 0.7837988924388917, 0.019698335991965993, -0.5674644170163944, -0.5780682102892082, 1.118603393095136], y = -0.2935847984032446)
 (x = [-0.019784056069199748, 0.025874990089649646, 0.028802252159158374, -0.05208937523691597, 0.025129121504662764, -0.0014613191395189647, -0.0796524431502691, 0.014820980412470918, -0.010812993132755216], y = -6.864181789412351)
 (x = [-13.038311354300191, 3.576777671340084, -19.29207053434301, 3.6396648043881075, -4.698350525398194, -1.219354237677556, 11.228901589388895, 5.456113356133842, -0.3624855198497022], y = 3.9609554741811506)
 (x = [0.4604025303433515, -0.1763466595251518, 1.524049949896116, -0.2542711437228486, -0.2871555636996592, -0.09411074847256491, -0.3247007431968486, -0.49609959406486265, 0.0811622235322983], y = -1.4556505734625274)
 (x = [0.3840231432287153, 0.09247624359718967, 2.0233901568575337, -0.018453573925998112, -0.15180452033532657, -0.5552889689901658, -0.19775274286256558, -0.881015780752438, -0.021014610335496477], y = -0.9950807884899728)
 (x = [0.00230567209916741, 0.4096135690646487, -0.28738901833126534, -0.08199090763137198, -0.18327782216891764, 0.10629154401245014, 0.04665017766470854, -0.01836314371816157, 0.22933192063578184], y = -2.745068416970144)
 ⋮
 (x = [0.051752642688746756, 0.2413224260254977, 0.17359387117215092, -0.2349573144005862, -0.03304315586993968, -0.0009595583763229398, 0.013938586416346535, -0.0007064431265213839, -0.022905655024400715], y = -3.8589854508830674)
 (x = [-1.59160303115259, -12.338897829788836, -5.182778069214695, 14.020366128344318, 3.4548435981853287, -3.6411028634989284, 1.0007250743206297, 6.341117453230908, 0.7335940692963456], y = 4.33281881055513)
 (x = [-0.11434846059981583, -0.2212805711844982, -0.03187352903828743, 0.038014031727079976, -0.12501272424497756, 0.07811288619957979, 0.018507371005505972, 0.01188549498764624, 0.22403156721187106], y = -2.9461044771380207)
 (x = [-8.455467361123619, -5.21844945500724, 0.3343090514074271, 2.9530322488477236, -9.74681797050703, -6.4702982565069025, -4.224553371228146, 3.500659926928281, 2.915727285824368], y = 4.0394157705624)
 (x = [1.7921041799296262, -2.120420171194075, -1.2526764131349635, 1.5287557815930704, 1.2868297828647335, 0.17854497965798724, 0.30312248226402333, 3.3430621959916293, 0.5527468557918128], y = 0.9018076891452225)
 (x = [-0.6601834543303841, 0.4774717940101484, 0.3423627155027204, -0.21658688260590908, 0.004091813783019803, -0.026826393973942556, -0.20879264386155913, -0.6438459264182171, -0.09115239801168983], y = -2.1991527984545116)
 (x = [-0.26660363419188715, 0.01932913164435719, 0.1339389145594649, 0.5669328392045453, 0.18773472657316928, 0.1112491443947913, -0.4423338613915276, -0.3166618278507729, -0.10040339718994098], y = -2.30074410248045)
 (x = [0.32186525888113904, 0.08582919978187703, -0.033305573089892994, -0.001938294913401548, 0.08208823337973851, 0.2934886908485019, 0.3034730775208234, -0.14298040134941892, -0.19138136264219358], y = -2.5263326665065686)
 (x = [-3.560437927922473, -0.8968930678602516, 0.6354060326318444, 0.2529682758750884, -1.0215638101761604, -3.440298325151325, -5.581706028931463, 1.031447325803883, 1.9157024333514243], y = 2.2935288342308633)

Another equivalent option is to use a submodel:

@model function Neal()
    y_raw ~ Normal(0, 1)
    x_raw ~ arraydist([Normal(0, 1) for i in 1:9])
    return (x_raw=x_raw, y_raw=y_raw)
end

chain = sample(Neal(), NUTS(), 1000)

@model function Neal_with_extras()
    neal ~ to_submodel(Neal(), false)
    y = 3 * neal.y_raw
    x = exp.(y ./ 2) .* neal.x_raw
    return (x=x, y=y)
end

returned(Neal_with_extras(), chain)
Info: Found initial step size
  ϵ = 1.6
1000×1 Matrix{@NamedTuple{x::Vector{Float64}, y::Float64}}:
 (x = [2.148646522339196, -3.825707826487371, -0.7350060226480565, 0.7334308033921981, -3.103256395121544, -0.798759835221513, -0.5595750854732061, -0.8517716549904698, -3.660879780120348], y = 1.4837018117232692)
 (x = [0.9683500335857639, -0.13239116597042366, 1.4250466313862062, 0.3846724457929464, -1.1456494734625544, 0.4208585334305074, 0.3875590702490939, -0.05851995881353835, -0.46591063734400195], y = 0.26969226484386316)
 (x = [-1.6567694712843253, -0.8317616436755195, 2.084624102422406, 0.015318227652632403, 3.8561373995964043, 2.518020735938062, -3.8693219283616322, 1.185388695750217, -0.7709406811435379], y = 1.7459183825987443)
 (x = [0.4495998207761321, -0.13615769407468226, -0.638469918813832, -0.39487915519715383, -1.0087607657253643, -0.6647440656707355, 0.9667831168407595, -0.30595677812604205, 0.20015370961093248], y = -0.9198403607225327)
 (x = [-0.2377234000287607, -0.12397982849223284, -0.27593410141823355, -0.02095238998897791, 0.05654328996168038, 0.06663499185136193, 0.12203908202053888, -0.09558050032464119, 0.19292298436506988], y = -2.594178908745576)
 (x = [0.8427452293676849, 0.6305071641501658, 0.15495749008312887, 0.3288689273107533, 0.08452100856719934, -1.9479379982225908, -0.10058551799110825, -0.2229698246056408, -0.2484673678643251], y = -0.8636699510097268)
 (x = [1.004232531076617, 1.0674665263808942, 0.46880592581373554, 0.9237749575103398, 0.09137332650800836, -0.9390946392604557, -0.41480939082450774, 0.442327070053304, 0.2272951662101146], y = -0.5842217843391055)
 (x = [-2.541319154485982, -2.831850248198028, -1.6063933865902023, -1.7566372596825572, -0.180932056771178, 2.3854260319214426, -0.049604327304148134, -1.2126924766398406, -0.5556761825166106], y = 1.329278329113147)
 (x = [-0.9464728390786463, 1.0085068503976453, -1.0645777234444829, -0.8051244718873901, -0.5661793027324249, 0.6994618052872802, -0.4794244538983604, 0.2257238205147248, 0.7579790463915008], y = -0.38420855002185883)
 (x = [0.23325456356727484, -0.72057976168648, 0.10908784655777985, 0.45624967618659873, 0.261608888468199, -0.25039010156270036, 0.0716757647282117, -0.23260986581824228, 0.32634189806902114], y = -2.304718616640153)
 ⋮
 (x = [1.3529223138146402, -3.3267457042264734, 0.06426148404218365, 0.22392992181753363, -1.3201520932408144, -0.806050458397943, -1.5499135117065082, -0.894883558218136, -1.050507568855746], y = 1.292345567451028)
 (x = [-0.276242386945868, 0.6487761123818303, -0.08084391857824066, 0.051263440316070365, 0.20457402618869208, 0.13817524191025787, 0.5037214637605776, 0.17872422418285785, 0.20571657459798737], y = -1.942161702864172)
 (x = [1.6991822508819339, -4.45321524357203, 0.1743835687034537, -1.9292609487886538, -1.5620137376130057, -0.5855271606744107, -1.8613153262111397, -1.3844490233522617, -1.4027848893039525], y = 1.856873963063653)
 (x = [-0.5963320007092255, 1.1680080226945633, -0.011670717596405533, 1.036553180167408, 0.7711484628136522, 0.2917251464181198, 0.801826070731666, 0.27246188551946005, 0.5257268815733777], y = -0.12360477662432445)
 (x = [-0.0385482148334289, 0.4978114966688972, 0.44018501253156006, 0.27156744560911933, 0.18589461180904074, -0.19112477377674914, 0.24044114177138176, 0.17558895634242022, -0.07071352344265816], y = -2.6269142908514977)
 (x = [-0.26548299740711945, 3.2973740111161867, 1.823131806684856, 0.34711249129227834, 0.5481127713645915, -1.0709033829063013, 0.1753535337607121, 0.7319196716404506, -0.33893914644678846], y = 0.6469935218966403)
 (x = [0.11859589622899347, -1.4768103461557753, -0.9673801328846298, -0.2876463651573591, -0.34643463104382305, 0.4878352674309306, 0.08593307830066824, -0.22598462953039927, 0.1735864140331872], y = -0.807351036588396)
 (x = [0.07780620645769779, -0.525853427487937, 0.11424063064225648, 0.26535245730836643, 0.1602412398707217, 0.13243702750820713, -0.028989492016372808, 0.0567784606379642, -0.022761422713479845], y = -2.907979916084566)
 (x = [-2.269633933510541, 13.111518041257092, -3.7278948225769355, -5.853914263706277, -4.7165540244779995, -3.888987889673963, -1.954177814976906, -0.28313631981041526, 0.4780484669188898], y = 3.87618375028359)

Note that for the returned call to work, the Neal_with_extras() model must have the same variable names as stored in chain. This means the submodel Neal() must not be prefixed, i.e. to_submodel() must be passed a second parameter false.

Back to top