Note
Go to the end to download the full example code.
Compare the searches on one network#
Every estimator on BDeu reports score_, the score of the network it
learned under the same prior, so the searches can be compared directly, and
against the network the data were sampled from.
import time
from causalbase import FLOP, GES, XGES, HillClimbing, OrderSearch
from causalbase.datasets import load_insurance
from causalbase.graph import dag_to_cpdag
from causalbase.metrics import shd
bundle = load_insurance()
truth = dag_to_cpdag(bundle.truth)
print(f"{bundle.data.n_vars} variables, {bundle.data.n_rows} rows")
27 variables, 5000 rows
The order searches run for max_iter kicks here, so that the example
gives the same result every time; in practice time_limit is the usual
budget. The SHD is computed between CPDAGs, which does not depend on the
member of the equivalence class a search returns.
estimators = {
"GES": GES(),
"XGES": XGES(),
"Hill climbing": HillClimbing(),
"Tabu search": HillClimbing(tabu_length=100, max_tabu_moves=100),
"OrderSearch": OrderSearch(time_limit=None, max_iter=10, random_state=0),
"FLOP": FLOP(time_limit=None, max_iter=10, random_state=0),
}
for name, model in estimators.items():
start = time.perf_counter()
model.fit(bundle.data)
seconds = time.perf_counter() - start
print(
f"{name:14s} BDeu {model.score_:10.1f} SHD {shd(truth, model.cpdag_):3d}"
f" {seconds:5.2f} s"
)
GES BDeu -67131.2 SHD 25 0.08 s
XGES BDeu -67098.0 SHD 15 0.49 s
Hill climbing BDeu -67247.0 SHD 30 3.69 s
Tabu search BDeu -67206.0 SHD 23 0.03 s
OrderSearch BDeu -67096.1 SHD 13 0.48 s
FLOP BDeu -67075.0 SHD 14 0.36 s
trace_ records, for the searches that iterate, when the best score
improved: (seconds, iteration, score).
0.10 s, kick 0: -67122.6
0.33 s, kick 6: -67096.1
Total running time of the script: (0 minutes 5.179 seconds)