BDeuScore#

class causalbase.scores.BDeuScore(*args, **kwargs)[source]#

BDeu local score over a discrete dataset, with a structure prior.

The local score of a family is its log marginal likelihood under a BDeu prior with equivalent sample size ess, plus a structure prior on its number of parents k, one of two:

  • prior="bernoulli" (the default): the prior of FamilyScore, in which each of the n_vars - 1 other variables is a parent with probability p = min(expected_parents / (n_vars - 1), 1/2). Only the part that depends on k, k * log(p / (1 - p)), is added. Families are cached.

  • prior="tetrad": the prior of Tetrad 7.6.5’s BDeu, k * log(p) + (m - k) * log(1 - p) with p = expected_parents / m and m = n_rows - 1: the Bernoulli prior with the number of rows in place of the number of variables, constant included, so that the values are Tetrad’s (and those of MCTS-BN, IPMU 2024). expected_parents is Tetrad’s structure prior. The values come from the compiled engine, which does not cache them.

Every search takes either. GES and XGES search Tetrad’s prior through the Bernoulli BDeu with the same p, whose differences between the families of a node are the same (as long as p stays below the cap of one half, that is, expected_parents <= (n_rows - 1) / 2); the order searches, hill climbing, FLOP and the exact searches score either prior with the compiled engine.

Parameters:
dataDiscreteDataset

The data to score against.

essfloat, default=1.0

Equivalent sample size of the BDeu prior.

expected_parentsfloat or None, default=1.0

The parameter of the structure prior: the expected number of parents per node among the n_vars - 1 candidates for "bernoulli", among n_rows - 1 for "tetrad". Smaller values penalise edges more. None gives a uniform prior over structures, the plain BDeu score, as does 0 for "tetrad". One expected parent recovers markedly better structures than the uniform prior once networks reach a few dozen variables, and about the same below that.

prior{“bernoulli”, “tetrad”}, default=”bernoulli”

The structure prior.

n_jobsint, default=1

Threads of the compiled kernels for large batches of candidate parents and for the exact search; -1 is every thread numba has. Results do not depend on it.

Attributes:
n_familiesint

int: Families the compiled engine has scored so far.

Raises:
ValueError

If ess is not positive and finite, prior is unknown, expected_parents is out of range (positive for "bernoulli", at least 0 and below n_rows - 1 for "tetrad"), or n_jobs is neither a positive integer nor -1.

Notes

Not safe to share between threads: the score keeps per-thread work buffers for its parallel kernel and a cache of families, and two threads calling it at once would overwrite each other’s. Use one score per thread, or processes.

Examples

>>> from causalbase.datasets import load_asia
>>> from causalbase.scores import BDeuScore
>>> from causalbase.search import ges
>>> score = BDeuScore(load_asia().data, prior="tetrad")
>>> round(score.score_dag(ges(score).dag), 2)
-11347.46
property n_families#

int: Families the compiled engine has scored so far.

compute_family(node, parents)[source]#

Return the BDeu log marginal likelihood of the family.

compute_extensions(node, parents, extra)[source]#

Return the BDeu of every extension, counted together.