.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/04_background_knowledge.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_04_background_knowledge.py: Add background knowledge ======================== :class:`~causalbase.search.GES` and :class:`~causalbase.search.XGES` take pairs of variables that must be adjacent (``required``) or must not be (``forbidden``), by name or by column index. Knowledge fixes adjacency, not direction: a search over equivalence classes still orients edges from the data. .. GENERATED FROM PYTHON SOURCE LINES 11-25 .. code-block:: Python from causalbase import GES from causalbase.datasets import load_asia bundle = load_asia() frame = bundle.data.to_pandas() def neighbours(model, node): """Return the variables joined to `node` by an edge of either kind.""" adjacency = model.adjacency_ return [v for v in adjacency.columns if adjacency.loc[node, v] or adjacency.loc[v, node]] .. GENERATED FROM PYTHON SOURCE LINES 26-28 In asia, ``either`` is tuberculosis or lung cancer, and GES joins it to both of its causes and to its two effects. .. GENERATED FROM PYTHON SOURCE LINES 28-32 .. code-block:: Python free = GES().fit(frame) neighbours(free, "either") .. rst-class:: sphx-glr-script-out .. code-block:: none ['lung', 'xray', 'tub', 'dysp'] .. GENERATED FROM PYTHON SOURCE LINES 33-36 Forbidding ``lung -- either`` keeps the two apart whatever the data say. The search then explains the dependence through other edges, such as a direct one between ``lung`` and ``xray``. .. GENERATED FROM PYTHON SOURCE LINES 36-40 .. code-block:: Python constrained = GES(forbidden=[("lung", "either")]).fit(frame) neighbours(constrained, "either"), neighbours(constrained, "lung") .. rst-class:: sphx-glr-script-out .. code-block:: none (['smoke', 'xray', 'tub', 'dysp'], ['smoke', 'xray', 'tub']) .. GENERATED FROM PYTHON SOURCE LINES 41-43 The constraint costs score: under the same BDeu, the free network is better. .. GENERATED FROM PYTHON SOURCE LINES 43-45 .. code-block:: Python print(f"Free {free.score_:.1f}, with the forbidden pair {constrained.score_:.1f}") .. rst-class:: sphx-glr-script-out .. code-block:: none Free -11292.4, with the forbidden pair -11543.1 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.031 seconds) .. _sphx_glr_download_auto_examples_04_background_knowledge.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 04_background_knowledge.ipynb <04_background_knowledge.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 04_background_knowledge.py <04_background_knowledge.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 04_background_knowledge.zip <04_background_knowledge.zip>`