BGeScore#
- class causalbase.scores.BGeScore(data, *, alpha_mu=1.0, alpha_w=None, expected_parents=1.0, n_jobs=1)[source]#
BGe local score of a linear Gaussian model, memoised per family.
The log marginal likelihood of Geiger and Heckerman (1994, 2002) under a normal-Wishart prior, the Gaussian counterpart of BDeu: Markov equivalent DAGs score the same. With the corrections of Kuipers, Moffa and Heckerman (2014), the local score of a node
iwithlparentsPis- N/2 log(pi) + 1/2 log(a_mu / (N + a_mu)) + lgamma((N + a_w - n + l + 1) / 2) - lgamma((a_w - n + l + 1) / 2) + (a_w - n + 2l + 1) / 2 * log(t) + (N + a_w - n + l) / 2 * log|R_P| - (N + a_w - n + l + 1) / 2 * log|R_(P+i)|
for
Nrows andnvariables, withR = t I + S,Sthe matrix of sums of squares about the sample means andt = a_mu (a_w - n - 1) / (a_mu + 1). The prior mean is the vector of sample means, as bnlearn’sbgetakes it by default. The structure prior ofFamilyScoreis added.The prior scale
t Idoes not scale with the data, so the score, unlike the BIC, changes when a variable is rescaled; standardise the columns to give every variable the same prior.- Parameters:
- dataContinuousDataset
The data to score against.
- alpha_mufloat, default=1.0
Equivalent sample size of the prior on the mean.
- alpha_wfloat or None, default=None
Degrees of freedom of the Wishart prior on the precision matrix; it must exceed
n_vars + 1.Noneisn_vars + 2, the smallest integer allowed and bnlearn’s default.- expected_parentsfloat or None, default=1.0
Expected number of parents per node under the structure prior, as in
BDeuScore.Noneis the plain BGe.- n_jobsint, default=1
Threads of the exact search’s compiled kernels; -1 is every thread numba has. Results do not depend on it.
- Raises:
- ValueError
If alpha_mu is not positive and finite, alpha_w is not finite and above
n_vars + 1, expected_parents is not positive, or n_jobs is neither a positive integer nor -1.
Notes
Not safe to share between threads, since it caches every family it computes; use one score per thread.
Examples
>>> import numpy as np >>> from causalbase.data import ContinuousDataset >>> from causalbase.graph import DAG >>> from causalbase.scores import BGeScore >>> rng = np.random.default_rng(0) >>> x = rng.normal(size=500) >>> data = ContinuousDataset(np.column_stack([x, x + rng.normal(size=500)]), ["x", "y"]) >>> score = BGeScore(data) >>> forward, backward = DAG.from_edges(2, [(0, 1)]), DAG.from_edges(2, [(1, 0)]) >>> bool(np.isclose(score.score_dag(forward), score.score_dag(backward))) True