.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/02_federated_structure.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_federated_structure.py: Learn one structure from data held by several sites =================================================== :class:`~fedcausal.fedges.FedGES` learns from discrete data that stays where it is. Each round every site runs GES on its own rows, a server fuses the sites' structures into a global one, and each site fuses that back into its own model. Only structures and scores leave a site. Here ``alarm``'s 5000 rows are split into five sites of 1000. .. GENERATED FROM PYTHON SOURCE LINES 12-26 .. code-block:: Python from causalbase import GES from causalbase.datasets import load_alarm from causalbase.metrics import shd from fedcausal import FedGES from fedcausal.data import contiguous_split bundle = load_alarm() sites = contiguous_split(bundle.data, 5) model = FedGES(n_rounds=10).fit(sites) print(f"{model.n_rounds_} rounds, {model.dag_.num_edges} edges") .. rst-class:: sphx-glr-script-out .. code-block:: none 4 rounds, 44 edges .. GENERATED FROM PYTHON SOURCE LINES 27-30 ``history_`` has one row per model built: each site's local model with its own score, and the global model (``site == -1``), which the server cannot score because it holds no data. .. GENERATED FROM PYTHON SOURCE LINES 30-33 .. code-block:: Python model.history_.pivot(index="round", columns="site", values="edges") .. raw:: html
site -1 0 1 2 3 4
round
1 44.0 44.0 44.0 45.0 46.0 44.0
2 45.0 44.0 44.0 46.0 44.0 44.0
3 44.0 44.0 44.0 46.0 44.0 44.0
4 NaN 44.0 44.0 46.0 44.0 44.0


.. GENERATED FROM PYTHON SOURCE LINES 34-36 The federation against one site on its own and against pooling everything, which is what federation exists to avoid. .. GENERATED FROM PYTHON SOURCE LINES 36-43 .. code-block:: Python one_site = GES().fit(sites[0]).dag_ pooled = GES().fit(bundle.data).dag_ print(f"SHD, one site alone: {shd(bundle.truth, one_site)}") print(f"SHD, FedGES: {shd(bundle.truth, model.dag_)}") print(f"SHD, pooled data: {shd(bundle.truth, pooled)}") .. rst-class:: sphx-glr-script-out .. code-block:: none SHD, one site alone: 6 SHD, FedGES: 5 SHD, pooled data: 10 .. GENERATED FROM PYTHON SOURCE LINES 44-47 With ``n_jobs`` the sites learn in separate processes, each keeping its own score cache between rounds. It pays off once a site's search takes longer than starting a process, at a few hundred variables. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 19.178 seconds) .. _sphx_glr_download_auto_examples_02_federated_structure.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 02_federated_structure.ipynb <02_federated_structure.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 02_federated_structure.py <02_federated_structure.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 02_federated_structure.zip <02_federated_structure.zip>`