{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Structure Learning in Bayesian Networks" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this notebook, we show a few examples of Causal Discovery or Structure Learning in pgmpy. pgmpy currently has the following algorithm for causal discovery:\n", "\n", "1. **PC**: Has $3$ variants original, stable, and parallel. PC is a constraint-based algorithm that utilizes Conditional Independence tests to construct the model.\n", "2. **Hill-Climb Search**: Hill-Climb Search is a greedy optimization-based algorithm that makes iterative local changes to the model structure such that it improves the overall score of the model.\n", "3. **Exhaustive Search**: Exhaustive search iterates over all possible network structures on the given variables to find the most optimal one. As it tries to enumerate all possible network structures, it is intractable when the number of variables in the data is large.\n", "\n", "The following Conditional Independence Tests are available to use with the PC algorithm:\n", "1. **Pillai**: Test for mixed data based on residualization approach. It is a generalization of Pearsonr to mixed data.\n", "1. **Chi-Square test**: Works only for discrete data.\n", "2. **Pearsonr**: A partial correlation-based test. Works for Gaussian continuous variables.\n", "3. **G-squared**: Works only for discrete data.\n", "4. **Log-likelihood**: Works only for discrete data.\n", "\n", "For Hill-Climb and Exhausitive Search the following scoring methods can be used:\n", "1. **BIC Score**\n", "2. **AIC Score**\n", "3. **K2 Score**\n", "4. **BDeU Score**\n", "5. **BDs Score**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Generate some data" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from itertools import combinations\n", "\n", "import networkx as nx\n", "from sklearn.metrics import f1_score\n", "\n", "from pgmpy.estimators import PC, HillClimbSearch, ExhaustiveSearch\n", "from pgmpy.estimators import K2Score\n", "from pgmpy.utils import get_example_model\n", "import numpy as np" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "093b82ef3ab54cc79d6a6b342316fedc", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/37 [00:00\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
COARTCO2MINVOLSETFIO2LVEDVOLUMEVENTLUNGANAPHYLAXISPRESSEXPCO2VENTMACH...HRSATERRLOWOUTPUTHRBPHRPULMEMBOLUSINTUBATIONHYPOVOLEMIAKINKEDTUBEPAPSAO2
0HIGHHIGHNORMALNORMALLOWZEROFALSELOWLOWNORMAL...NORMALFALSEHIGHHIGHFALSENORMALTRUEFALSENORMALLOW
1HIGHHIGHNORMALNORMALHIGHZEROFALSELOWLOWNORMAL...HIGHFALSEHIGHHIGHFALSENORMALTRUEFALSENORMALLOW
2HIGHHIGHLOWNORMALNORMALZEROFALSEHIGHLOWLOW...HIGHFALSEHIGHHIGHFALSENORMALFALSEFALSENORMALLOW
3LOWLOWHIGHNORMALLOWLOWFALSELOWLOWHIGH...HIGHTRUELOWNORMALFALSENORMALTRUEFALSENORMALHIGH
4HIGHHIGHNORMALNORMALNORMALZEROFALSEHIGHLOWNORMAL...HIGHFALSEHIGHHIGHFALSENORMALFALSEFALSENORMALLOW
\n", "

5 rows × 37 columns

\n", "" ], "text/plain": [ " CO ARTCO2 MINVOLSET FIO2 LVEDVOLUME VENTLUNG ANAPHYLAXIS PRESS EXPCO2 \\\n", "0 HIGH HIGH NORMAL NORMAL LOW ZERO FALSE LOW LOW \n", "1 HIGH HIGH NORMAL NORMAL HIGH ZERO FALSE LOW LOW \n", "2 HIGH HIGH LOW NORMAL NORMAL ZERO FALSE HIGH LOW \n", "3 LOW LOW HIGH NORMAL LOW LOW FALSE LOW LOW \n", "4 HIGH HIGH NORMAL NORMAL NORMAL ZERO FALSE HIGH LOW \n", "\n", " VENTMACH ... HRSAT ERRLOWOUTPUT HRBP HR PULMEMBOLUS INTUBATION \\\n", "0 NORMAL ... NORMAL FALSE HIGH HIGH FALSE NORMAL \n", "1 NORMAL ... HIGH FALSE HIGH HIGH FALSE NORMAL \n", "2 LOW ... HIGH FALSE HIGH HIGH FALSE NORMAL \n", "3 HIGH ... HIGH TRUE LOW NORMAL FALSE NORMAL \n", "4 NORMAL ... HIGH FALSE HIGH HIGH FALSE NORMAL \n", "\n", " HYPOVOLEMIA KINKEDTUBE PAP SAO2 \n", "0 TRUE FALSE NORMAL LOW \n", "1 TRUE FALSE NORMAL LOW \n", "2 FALSE FALSE NORMAL LOW \n", "3 TRUE FALSE NORMAL HIGH \n", "4 FALSE FALSE NORMAL LOW \n", "\n", "[5 rows x 37 columns]" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model = get_example_model(\"alarm\")\n", "samples = model.simulate(int(1e3))\n", "samples.head()" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "# Funtion to evaluate the learned model structures.\n", "def get_f1_score(estimated_model, true_model):\n", " nodes = estimated_model.nodes()\n", " est_adj = nx.to_numpy_array(\n", " estimated_model.to_undirected(), nodelist=nodes, weight=None\n", " )\n", " true_adj = nx.to_numpy_array(\n", " true_model.to_undirected(), nodelist=nodes, weight=None\n", " )\n", "\n", " f1 = f1_score(np.ravel(true_adj), np.ravel(est_adj))\n", " print(\"F1-score for the model skeleton: \", f1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Learn the model structure using PC" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "eaa8bb6396a04eb0acfc789cec96c379", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/4 [00:00