{ "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, ?it/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stderr", "output_type": "stream", "text": [ "WARNING:pgmpy:Probability values don't exactly sum to 1. Differ by: -2.220446049250313e-16. Adjusting values.\n" ] }, { "data": { "text/html": [ "
| \n", " | CO | \n", "ARTCO2 | \n", "MINVOLSET | \n", "FIO2 | \n", "LVEDVOLUME | \n", "VENTLUNG | \n", "ANAPHYLAXIS | \n", "PRESS | \n", "EXPCO2 | \n", "VENTMACH | \n", "... | \n", "HRSAT | \n", "ERRLOWOUTPUT | \n", "HRBP | \n", "HR | \n", "PULMEMBOLUS | \n", "INTUBATION | \n", "HYPOVOLEMIA | \n", "KINKEDTUBE | \n", "PAP | \n", "SAO2 | \n", "
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | \n", "HIGH | \n", "HIGH | \n", "NORMAL | \n", "NORMAL | \n", "LOW | \n", "ZERO | \n", "FALSE | \n", "LOW | \n", "LOW | \n", "NORMAL | \n", "... | \n", "NORMAL | \n", "FALSE | \n", "HIGH | \n", "HIGH | \n", "FALSE | \n", "NORMAL | \n", "TRUE | \n", "FALSE | \n", "NORMAL | \n", "LOW | \n", "
| 1 | \n", "HIGH | \n", "HIGH | \n", "NORMAL | \n", "NORMAL | \n", "HIGH | \n", "ZERO | \n", "FALSE | \n", "LOW | \n", "LOW | \n", "NORMAL | \n", "... | \n", "HIGH | \n", "FALSE | \n", "HIGH | \n", "HIGH | \n", "FALSE | \n", "NORMAL | \n", "TRUE | \n", "FALSE | \n", "NORMAL | \n", "LOW | \n", "
| 2 | \n", "HIGH | \n", "HIGH | \n", "LOW | \n", "NORMAL | \n", "NORMAL | \n", "ZERO | \n", "FALSE | \n", "HIGH | \n", "LOW | \n", "LOW | \n", "... | \n", "HIGH | \n", "FALSE | \n", "HIGH | \n", "HIGH | \n", "FALSE | \n", "NORMAL | \n", "FALSE | \n", "FALSE | \n", "NORMAL | \n", "LOW | \n", "
| 3 | \n", "LOW | \n", "LOW | \n", "HIGH | \n", "NORMAL | \n", "LOW | \n", "LOW | \n", "FALSE | \n", "LOW | \n", "LOW | \n", "HIGH | \n", "... | \n", "HIGH | \n", "TRUE | \n", "LOW | \n", "NORMAL | \n", "FALSE | \n", "NORMAL | \n", "TRUE | \n", "FALSE | \n", "NORMAL | \n", "HIGH | \n", "
| 4 | \n", "HIGH | \n", "HIGH | \n", "NORMAL | \n", "NORMAL | \n", "NORMAL | \n", "ZERO | \n", "FALSE | \n", "HIGH | \n", "LOW | \n", "NORMAL | \n", "... | \n", "HIGH | \n", "FALSE | \n", "HIGH | \n", "HIGH | \n", "FALSE | \n", "NORMAL | \n", "FALSE | \n", "FALSE | \n", "NORMAL | \n", "LOW | \n", "
5 rows × 37 columns
\n", "