bnlearn Utilities¶
causaliq_core.r.bnlearn provides Python wrappers around bnlearn graph
operations. All functions require R and the bnlearn package to be available.
Functions¶
bnlearn_cpdag¶
Convert a PDAG to a CPDAG using bnlearn's cpdag() function.
This is used as a reference implementation for testing CausalIQ's native
pdag_to_cpdag against bnlearn's output.
Parameters:
pdag— acausaliq_core.graph.PDAGinstance to convert.
Returns: A new PDAG representing the CPDAG (completed partially directed
acyclic graph) produced by bnlearn.
Raises:
RNotAvailableError— if R is not available.RRuntimeError— if bnlearn raises an error (e.g. the input graph is not a valid PDAG).
Example:
from causaliq_core.graph import PDAG
from causaliq_core.r.bnlearn import bnlearn_cpdag
pdag = PDAG(["A", "B", "C"], [("A", "-->", "B"), ("B", "---", "C")])
cpdag = bnlearn_cpdag(pdag)
bnlearn_compare¶
Compare a learned graph against a reference using bnlearn's compare() and
shd() functions.
Parameters:
pdag— the learned graph to evaluate.ref— the reference (ground-truth) graph.
Returns: A dictionary with four integer metrics:
| Key | Meaning |
|---|---|
tp |
True positives — edges in both pdag and ref |
fp |
False positives — edges in pdag but not ref |
fn |
False negatives — edges in ref but not pdag |
shd |
Structural Hamming Distance |
Example:
from causaliq_core.graph import PDAG
from causaliq_core.r.bnlearn import bnlearn_compare
learned = PDAG(["A", "B", "C"], [("A", "-->", "B")])
ref = PDAG(["A", "B", "C"], [("A", "-->", "B"), ("B", "-->", "C")])
metrics = bnlearn_compare(learned, ref)
# {"tp": 1, "fp": 0, "fn": 1, "shd": 1}
bnlearn_import¶
Load a fitted bnlearn Gaussian BN from an .rda file.
The .rda file must contain a single object named bn of class
bn.fit with Gaussian (GaussianNode) conditional distributions.
Parameters:
rda_path— path to the.rdafile.
Returns: A causaliq_core.bn.BN instance with a DAG structure and
LinGauss conditional node distributions.
Raises:
RNotAvailableError— if R is not available.RRuntimeError— if the.rdafile cannot be loaded or is not a Gaussianbn.fitobject.
Example:
from causaliq_core.r.bnlearn import bnlearn_import
bn = bnlearn_import("/path/to/model.rda")
print(bn.dag.nodes) # ['A', 'B', 'C']
print(bn.dag.edges) # [('A', 'B'), ('B', 'C')]
Data Exchange Format¶
bnlearn functions communicate with R via tab-separated stdout. The R scripts write results to stdout; Python parses them line by line.
Arcs (for bnlearn_cpdag):
Metrics (for bnlearn_compare):
BN parameters (for bnlearn_import):