FamilyScore#
- class causalbase.scores.FamilyScore(n_vars, expected_parents, *, n_jobs=1)[source]#
A decomposable score whose families are computed once and cached.
The base for a new score: a subclass implements
compute_family(), the score of one family, and may overridecompute_extensions(), the families an Insert compares, with a faster batch.local_score()andlocal_scores_with()(also callable aslocal_scores_add()), which the searches call, cache both and add a structure prior on the number of parentsk:k * log(p) + (m - k) * log(1 - p), p = min(expected_parents / m, 1/2)
a Bernoulli prior in which each of the
m = n_vars - 1candidate parents is present with probabilityp, so expected_parents is the expected in-degree under the prior.pis capped at one half, where the prior stops penalising edges, so on very few variables it has no effect. Only thek * log(p / (1 - p))part is added; the rest is the same for every family of a node.- Parameters:
- n_varsint
Number of variables.
- expected_parentsfloat or None
Expected in-degree under the structure prior;
Noneis a uniform prior over structures.- n_jobsint, default=1
Threads of the score’s compiled kernels, kept as
n_jobs; -1 is every thread numba has. A score without parallel kernels ignores it.
Notes
A score caches every family it computes and keeps work buffers between calls, so it is not safe to use one score object from several threads at once: give each thread its own. Several processes are fine, each with its own copy.
Examples
A score that rewards each parent by a fixed amount, which GES then adds until the prior stops it:
>>> import numpy as np >>> from causalbase.scores import FamilyScore >>> from causalbase.search import ges >>> class PerParent(FamilyScore): ... def compute_family(self, node, parents): ... return 0.5 * len(parents) >>> ges(PerParent(3, expected_parents=None)).cpdag PDAG(num_nodes=3, directed=0, undirected=3)
- compute_family(node, parents)[source]#
Return the score of node given the sorted parents, without the prior.
Called once per family; the result is cached. Subclasses must implement it.
- compute_extensions(node, parents, extra)[source]#
Return
compute_family()of node givenparents + [x], for each x in extra.One call per family by default; override it to score the batch together.
- local_score(node, parents)[source]#
Return the local score of node given parents.
- Parameters:
- nodeint
Index of the child.
- parentsCollection[int]
Indices of the parents, in any order.
- Returns:
- float
- local_scores_with(node, parents, extra)[source]#
Return the local score of node given
parents | {x}, for each x in extra.Equivalent to calling
local_score()once per x, but the families are cached together, one array per node and parents, and the ones not yet cached are computed together.
- local_scores_add(node, parents, candidates)[source]#
Return the local score of node given parents plus each one of candidates.
local_scores_with()under the name the order searches, hill climbing and FLOP call, so that every score works with them.