Skip to content

Concepts: composition verbs and primitives

A composed distribution is a multi-state event process: named events linked by delays, wired into a tree. This page maps the modelling concepts to the verbs that build them, so you can find the right primitive by intent before reading the worked examples in Composing distributions.

The layers

The package has four layers, each building on the one before.

The verb map

The verbs fall into five families, listed here as verb, what it does, and what it returns.

Structural composition wires named branches into a tree.

VerbWhat it doesReturns
composelowers a NamedTuple, table or matrix to the stacka composer
sequentiala conjunctive chain, where steps add upSequential
parallelindependent branches off one shared originParallel
resolveone outcome occurs by a fixed probabilityResolve
competeracing hazards, the first to fire winsCompete
choosea data field picks the branchChoose
sharedtags a leaf as a tied parameter group at build timea tied leaf
tieties leaves at named paths into one parameter groupa tied tree

Combination and lowering joins or collapses whole delays.

VerbWhat it doesReturns
convolve_distributionsthe sum X + YConvolved
differencethe dual X - YDifference
as_mixturethe mixture view of a one_of nodea MixtureModel
observed_distributioncollapses a chain to its convolved totala convolved leaf

Parameters read and prior the free parameters.

VerbWhat it doesReturns
params_tablethe flat free-parameter inventorya Tables.jl table
build_priorssupport-derived default priors from that tablea nested prior NamedTuple
default_priorthe default prior for one parameter rowa Distribution

Reading and editing inspect or reshape an assembled tree.

VerbWhat it doesReturns
event / event_names / event_treefetch a child or the record key namesa node, leaf or names
mean / varthe composed marginal momentsa number or NamedTuple
updatereplaces parameter values or whole nodesa same-shape tree
prune / splicedrops or inserts a branchan edited tree

Deferred leaves hold a distribution that resolves later. varying and uncertain are the two cases of one idea: a leaf that is a map to a distribution rather than a fixed distribution, delegating silently to a fallback until it is resolved. They differ only in what indexes the map — varying an observed covariate (time, stratum) resolved by instantiate, uncertain a latent parameter draw (with a prior) resolved by rand or collapsed by update — and they share one resolution walk, so a leaf can be both at once.

VerbWhat it doesReturns
varying / Context / instantiatean observed covariate picks the leafa resolved tree
has_varyingwhether any un-instantiated leaf remainsa Bool
uncertaindistribution-valued parameters that act as priorsan uncertain leaf
has_uncertainwhether any uncertain leaf remainsa Bool

The deferred-leaf verbs are worked through in Multi-strata trees and parameter uncertainty.

Concept to primitive

Modelling conceptPrimitiveWhat it builds
Steps in series (a chain)sequential / compose with a Vector valueSequential
Branches off one shared originparallel / compose with a NamedTupleParallel
One outcome by fixed probabilityresolveResolve
Racing outcomes (first to fire wins)competeCompete
A data field selects the sub-modelchooseChoose
Tie a leaf across branchesshared / tieone shared parameter group
Sum of two independent delaysconvolve_distributionsConvolved
Difference of two delaysdifferenceDifference
Mixture view of a one_of nodeas_mixturea MixtureModel
Collapse a chain to its totalobserved_distributionthe convolved marginal
The free-parameter inventoryparams_tablea Tables.jl table
Support-derived priorsbuild_priorsa nested prior NamedTuple
Read a child or descend a patheventa node or leaf
Flat / nested event namesevent_names / event_treethe record key names
Replace values or whole nodesupdatea same-shape tree
Drop or insert a branchprune / splicean edited tree
A leaf that varies with a covariatevarying, resolved by instantiateVarying
A leaf with parameter uncertaintyuncertain, collapsed by updateUncertain
Guard a fitting loop against unresolved leaveshas_varying / has_uncertaina Bool

One object, both directions

A composed object is a Distributions.jl distribution: it scores an observed record with logpdf and simulates a new one with rand, so a model is built once and used in both directions.

julia
using ComposedDistributions, Distributions

tree = compose((onset_admit = Gamma(2.0, 1.0),
    admit_death = LogNormal(0.5, 0.4)))

params_table(tree)
params_table (4 rows)
  edge         param  value  support     prior
  ───────────  ─────  ─────  ──────────  ─────
  onset_admit  shape  2.0    (0.0, Inf)       
  onset_admit  scale  1.0    (0.0, Inf)       
  admit_death  mu     0.5    (0.0, Inf)       
  admit_death  sigma  0.4    (0.0, Inf)

Read Composing distributions for each verb worked through end to end.