.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/02_compare_searches.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_02_compare_searches.py: 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. .. GENERATED FROM PYTHON SOURCE LINES 9-21 .. code-block:: Python 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") .. rst-class:: sphx-glr-script-out .. code-block:: none 27 variables, 5000 rows .. GENERATED FROM PYTHON SOURCE LINES 22-26 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. .. GENERATED FROM PYTHON SOURCE LINES 26-44 .. code-block:: Python 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" ) .. rst-class:: sphx-glr-script-out .. code-block:: none 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 .. GENERATED FROM PYTHON SOURCE LINES 45-47 ``trace_`` records, for the searches that iterate, when the best score improved: ``(seconds, iteration, score)``. .. GENERATED FROM PYTHON SOURCE LINES 47-50 .. code-block:: Python for seconds, iteration, score in estimators["OrderSearch"].trace_: print(f"{seconds:6.2f} s, kick {iteration:2d}: {score:.1f}") .. rst-class:: sphx-glr-script-out .. code-block:: none 0.10 s, kick 0: -67122.6 0.33 s, kick 6: -67096.1 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 5.179 seconds) .. _sphx_glr_download_auto_examples_02_compare_searches.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 02_compare_searches.ipynb <02_compare_searches.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 02_compare_searches.py <02_compare_searches.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 02_compare_searches.zip <02_compare_searches.zip>`