.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/03_continuous_data.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_continuous_data.py: Learn from continuous data ========================== With ``criterion="gaussian_bic"`` or ``criterion="bge"``, every estimator scores a linear Gaussian model of each node given its parents; here :class:`~causalbase.search.GES` and :class:`~causalbase.search.XGES` do. The data below come from a known linear model, so the answer can be checked. .. GENERATED FROM PYTHON SOURCE LINES 10-27 .. code-block:: Python import numpy as np import pandas as pd from causalbase import GES, XGES rng = np.random.default_rng(0) n = 5000 smoking = rng.normal(size=n) pollution = rng.normal(size=n) cancer = 0.8 * smoking + 0.6 * pollution + rng.normal(scale=0.5, size=n) cough = 0.7 * cancer + rng.normal(scale=0.5, size=n) xray = 0.9 * cancer + rng.normal(scale=0.5, size=n) frame = pd.DataFrame( {"smoking": smoking, "pollution": pollution, "cancer": cancer, "cough": cough, "xray": xray} ) .. GENERATED FROM PYTHON SOURCE LINES 28-31 ``smoking -> cancer <- pollution`` is a v-structure, so both of its edges are compelled, and so are the edges out of ``cancer``, which the v-structure orients. .. GENERATED FROM PYTHON SOURCE LINES 31-35 .. code-block:: Python model = GES(criterion="gaussian_bic").fit(frame) model.adjacency_ .. raw:: html
smoking pollution cancer cough xray
smoking 0 0 1 0 0
pollution 0 0 1 0 0
cancer 0 0 0 1 1
cough 0 0 0 0 0
xray 0 0 0 0 0


.. GENERATED FROM PYTHON SOURCE LINES 36-38 XGES learns the same class here; it differs from GES on denser graphs, where GES's early insertions keep out better edges. .. GENERATED FROM PYTHON SOURCE LINES 38-41 .. code-block:: Python (XGES(criterion="gaussian_bic").fit(frame).adjacency_ == model.adjacency_).all().all() .. rst-class:: sphx-glr-script-out .. code-block:: none np.True_ .. GENERATED FROM PYTHON SOURCE LINES 42-45 ``score(X)`` is the mean log likelihood per row of held-out data under the learned structure, with the linear Gaussian model fitted on the training rows. .. GENERATED FROM PYTHON SOURCE LINES 45-48 .. code-block:: Python train, test = frame.iloc[:4000], frame.iloc[4000:] GES(criterion="gaussian_bic").fit(train).score(test) .. rst-class:: sphx-glr-script-out .. code-block:: none -5.011780541088802 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.636 seconds) .. _sphx_glr_download_auto_examples_03_continuous_data.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 03_continuous_data.ipynb <03_continuous_data.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 03_continuous_data.py <03_continuous_data.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 03_continuous_data.zip <03_continuous_data.zip>`