.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/03_fedrcd.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_03_fedrcd.py: Learn a linear model from continuous data held by several sites =============================================================== :class:`~fedcausal.lingam.FedRCD` assumes a linear structural equation model with non-Gaussian noise, ``x = Bx + e``. Each site sends additive moment sums of its rows; their totals give third- and fourth-order cumulants, from which the causal order is identified and ``B`` estimated. No row leaves a site. .. GENERATED FROM PYTHON SOURCE LINES 10-30 .. code-block:: Python import numpy as np from causalbase.graph import DAG from causalbase.metrics import shd from fedcausal import FedRCD rng = np.random.default_rng(0) n_vars, n_rows = 6, 8000 b = np.zeros((n_vars, n_vars)) for child in range(1, n_vars): for parent in range(child): if rng.random() < 0.4: b[child, parent] = rng.uniform(0.5, 1.5) * rng.choice([-1, 1]) noise = rng.uniform(-1, 1, size=(n_rows, n_vars)) x = noise @ np.linalg.inv(np.eye(n_vars) - b).T truth = DAG.from_amat(b.T != 0) sites = np.array_split(x, 4) .. GENERATED FROM PYTHON SOURCE LINES 31-33 ``"deflation"`` needs one round of communication. ``"exact"`` needs one per variable but carries no estimation error from one step to the next. .. GENERATED FROM PYTHON SOURCE LINES 33-38 .. code-block:: Python for method in ("deflation", "exact"): model = FedRCD(method=method).fit(sites) print(f"{method:9s} order {model.causal_order_} SHD {shd(truth, model.dag_)}") .. rst-class:: sphx-glr-script-out .. code-block:: none deflation order ['x0', 'x3', 'x2', 'x1', 'x4', 'x5'] SHD 0 exact order ['x0', 'x3', 'x2', 'x1', 'x4', 'x5'] SHD 0 .. GENERATED FROM PYTHON SOURCE LINES 39-41 The published baselines are settings of the same estimator: FedISHC is ``FedRCD(method="ratio", order=3)`` and FedHC ``FedRCD(method="sort", order=3)``. .. GENERATED FROM PYTHON SOURCE LINES 41-46 .. code-block:: Python for method in ("ratio", "sort"): baseline = FedRCD(method=method, order=3).fit(sites) print(f"{method:9s} order {baseline.causal_order_} SHD {shd(truth, baseline.dag_)}") .. rst-class:: sphx-glr-script-out .. code-block:: none ratio order ['x1', 'x0', 'x3', 'x4', 'x2', 'x5'] SHD 0 sort order ['x1', 'x0', 'x3', 'x2', 'x4', 'x5'] SHD 0 .. GENERATED FROM PYTHON SOURCE LINES 47-49 ``coef_[child, parent]`` holds the estimated coefficients; the adaptive lasso sets the absent ones to zero. .. GENERATED FROM PYTHON SOURCE LINES 49-51 .. code-block:: Python np.round(model.coef_, 2) .. rst-class:: sphx-glr-script-out .. code-block:: none array([[ 0. , 0. , 0. , 0. , 0. , 0. ], [ 0. , 0. , 0. , 0. , 0. , 0. ], [-0.54, 0. , 0. , 0. , 0. , 0. ], [ 0. , 0. , 0. , 0. , 0. , 0. ], [ 0. , 0. , 0. , -1.37, 0. , 0. ], [ 1.23, 0. , 0. , -0.95, -0.63, 0. ]]) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.269 seconds) .. _sphx_glr_download_auto_examples_03_fedrcd.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 03_fedrcd.ipynb <03_fedrcd.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 03_fedrcd.py <03_fedrcd.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 03_fedrcd.zip <03_fedrcd.zip>`