Pure numeric core of the Bonsai likelihood model. Everything factorises over dimensions, so the methods below operate on D-length vectors and never touch full matrices. This is a direct translation of the functions in bonsai_treeHelpers.py (getLoglikAndGradStarTree, findNodeLtqsGivenLeafs, der2LeafTree, optimiseT3LeafStar, getDerivativesDownstream, ...).
Likelihood
01 Syntax
02 Methods
| Name | Overloads | Summary |
|---|---|---|
| findNodeLtqs | 1 | Effective coordinate of the parent integrating out all children: wbar_g = 1 / (ltqsVars_g + tParent_g) W_g = sum_i wbar_i xr_g = sum_i (ltqs_i * wbar_i) / W_g Mirrors… |
| findNodeW | 1 | Precision W_g = sum_i 1/(ltqsVars_i + tParent_i) (the complement of Likelihood.findNodeLtqs())). |
| loglikStarTree | 1 | Log-likelihood of a star-tree rooted at position xr_g with children given either as node objects or as explicit arrays. |
| loglikGradStarTree | 1 | Log-likelihood + gradient w.r.t. |
| der2LeafTree | 1 | Derivative of the 2-leaf log-likelihood with respect to the combined diffusion time, mirroring der2LeafTree. |
| getOptTime2LeafTree | 1 | Optimal total diffusion time between two leaves (on a star of two), found by bracketing the root of Likelihood.der2LeafTree()) with bisection/brentq. |
| optimiseT3LeafStar | 1 | Optimise the three branch times of a star of three children (the "merge" of two children plus the rest of the tree as a third pseudo-child) in log-space with a bounded L-BFGS opti… |
| logLGradStarTreeLogT | 1 | Objective for Likelihood.optimiseT3LeafStar()): negative log-likelihood and gradient in log-time space. |
| getDerivativesDownstream | 1 | Compute the derivative of the total tree log-likelihood w.r.t. |
| calcLogLComplete | 1 | Recursively integrate out all internal-node positions and return the complete tree log-likelihood. |
| calcSingleDLogL | 1 | dLogL of merging two candidate children child1, child2 under a root described by (xrAsIfRoot_g, WAsIfRoot_g). |
| makePseudoLeaf | 1 | Build a transient pseudo-leaf node wrapping an effective position/precision (used for scoring only). |
03 Fields
| Name | Overloads | Summary |
|---|---|---|
| EPS | 2 | eps for numerical stability (mirrors the tiny regulariser used throughout the reference code). |
04 Members
List(Of BonsaiNode))Effective coordinate of the parent integrating out all children: wbar_g = 1 / (ltqsVars_g + tParent_g) W_g = sum_i wbar_i xr_g = sum_i (ltqs_i * wbar_i) / W_g Mirrors findNodeLtqsGivenLeafs returning xr_g.
List(Of BonsaiNode))Precision W_g = sum_i 1/(ltqsVars_i + tParent_i) (the complement of Likelihood.findNodeLtqs()).
List(Of BonsaiNode), Double(), Double())Log-likelihood of a star-tree rooted at position xr_g with children given either as node objects or as explicit arrays. Mirrors getLoglikAndGradStarTree (returnGrad=False). loglik = sum_g [ sum_i log(wbar_i) - log(W_g) - sum_i wbar_i (xr_g - ltqs_i)^2 ]
List(Of BonsaiNode), Double(), Double())Log-likelihood + gradient w.r.t. each child's diffusion time, used by the tree-search to score candidate merges. Mirrors the gradient branch of getLoglikAndGradStarTree. grad[cInd] = sum_g wbar_g (wbar_g * sqDist_g - 1 + wbar_g / W_g)
Double, Object())Derivative of the 2-leaf log-likelihood with respect to the combined diffusion time, mirroring der2LeafTree. der = sum_g (sqDists_g / totVar_g - 1) / totVar_g where totVar_g = t12 + scale_g * summedLtqsVars_g honours the optional v_g diffusion prior (scale_g = 1 by default, reproducing the original behaviour exactly). args = (summedLtqsVars, sqDists, scale?) with scale optional.
Double(), Double(), Double(), Double(), Double())Optimal total diffusion time between two leaves (on a star of two), found by bracketing the root of Likelihood.der2LeafTree() with bisection/brentq. Mirrors getOptTime2LeafTree.
Double()(), Double()(), Double(), Double())Optimise the three branch times of a star of three children (the "merge" of two children plus the rest of the tree as a third pseudo-child) in log-space with a bounded L-BFGS optimisation. Returns the optimised times and the achieved log-likelihood. Mirrors optimiseT3LeafStar.
Double(), Object())Objective for Likelihood.optimiseT3LeafStar(): negative log-likelihood and gradient in log-time space. Mirrors logLGradStarTreeLogT. f = -sum_g [ sum_i log(wbar_i) - log(W_g) - sum_i wbar_i sqDists_i ] grad = -t_i * sum_g wbar_i (sqDists_i wbar_i - 1 + wbar_i / W_g)
Compute the derivative of the total tree log-likelihood w.r.t. every non-root branch time by propagating from the root down. Mirrors getDerivativesDownstream. After this call every node's BonsaiNode.dLoglikdtParent is populated.
Recursively integrate out all internal-node positions and return the complete tree log-likelihood. Mirrors getLtqsComplete + calcLogLComplete. After this call every internal node's ltqs/W are up to date and node.prefactor holds the accumulated log-likelihood.
dLogL of merging two candidate children child1, child2 under a root described by (xrAsIfRoot_g, WAsIfRoot_g). Mirrors calcSingleDLogL: the pair is modelled as a 3-leaf star (the two candidates plus the rest of the subtree as a single pseudo-leaf), the three times are optimised, and the gain is the difference in star log-likelihood before/after creation of the ancestor. The returned value is 0.5 * dLogL (the factor used by the reference implementation to avoid double counting).
Double(), Double())Build a transient pseudo-leaf node wrapping an effective position/precision (used for scoring only).
eps for numerical stability (mirrors the tiny regulariser used throughout the reference code).