{ "cells": [ { "cell_type": "markdown", "id": "missing_values-0", "metadata": { "cell_marker": "\"\"\"" }, "source": "# Missing values\n\n[![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/Abel-ai-lab/Andrey/blob/main/examples/missing_values.ipynb) {download}`Download this notebook <../../../examples/missing_values.ipynb>`\n\nA method that reads only the rows where a test's variables are observed is sound when values go\nmissing at random of everything. When whether a value is missing depends on another variable, those\nrows are a selected sample and the method can keep edges the data do not support. Here we remove\nvalues from simulated data in a known way, fit PC with its default handling and with\n`missing=\"mvpc\"`, fit GES with `missing=\"em\"`, and compare each graph with the true one." }, { "cell_type": "markdown", "id": "7fb27b941602401d91542211134fc71a", "metadata": {}, "source": [ "## Start\n", "\n", "Choose a CPU runtime and run this cell first. In Colab it installs `andrey-core==0.18.0` from PyPI;\n", "local kernels use the installed environment." ] }, { "cell_type": "code", "execution_count": 1, "id": "acae54e37e7d407bbb7b55eff062a284", "metadata": { "tags": [ "colab-setup" ] }, "outputs": [], "source": [ "import os\n", "import sys\n", "\n", "IN_COLAB = \"google.colab\" in sys.modules\n", "\n", "if IN_COLAB:\n", " os.environ[\"ANDREY_DEVICE\"] = \"cpu\"\n", " !pip install --quiet \"andrey-core[viz]==0.18.0\" pandas" ] }, { "cell_type": "code", "execution_count": 2, "id": "missing_values-3", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "\n", "import andrey\n", "import andrey.viz\n", "from andrey import data\n", "from andrey.data import missingness\n", "from andrey.metrics import shd, to_cpdag" ] }, { "cell_type": "markdown", "id": "missing_values-4", "metadata": {}, "source": [ "## Data with a known graph\n", "\n", "`data.sample_scm` draws 2,000 rows of ten variables from a linear-Gaussian model whose graph we\n", "know. PC and GES can recover that graph only up to its Markov equivalence class, so we score each\n", "result against the true CPDAG." ] }, { "cell_type": "code", "execution_count": 3, "id": "missing_values-5", "metadata": {}, "outputs": [ { "data": { "text/html": [ "0123456789" ], "text/plain": [ "'0123456789'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "complete = data.sample_scm(d=10, n=2000, seed=9, density=2)\n", "truth = to_cpdag(complete.graph)\n", "andrey.viz.draw(complete.graph)" ] }, { "cell_type": "markdown", "id": "missing_values-6", "metadata": {}, "source": [ "## Remove values\n", "\n", "`missingness.mar(0.3, form=\"quantile\")` is missing at random given the observed variables: a\n", "variable that can go missing has one parent among the variables that never do, and its value is\n", "missing with probability 0.9 when the parent is below a quantile of its values and 0.01 otherwise.\n", "`missingness.draw` applies the rule to the complete data and returns the mask and each indicator's\n", "parents." ] }, { "cell_type": "code", "execution_count": 4, "id": "missing_values-7", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "share missing, by column: [0.29, 0.0, 0.0, 0.3, 0.0, 0.3, 0.3, 0.3, 0.3, 0.3]\n", "parent of each missingness indicator: {0: [2], 3: [4], 5: [1], 6: [2], 7: [4], 8: [1], 9: [2]}\n" ] } ], "source": [ "rule = missingness.mar(0.3, form=\"quantile\")\n", "mask = missingness.draw(complete.data, rule, seed=9)\n", "gappy = np.where(mask.mask, np.nan, complete.data)\n", "print(\"share missing, by column:\", np.round(mask.mask.mean(axis=0), 2).tolist())\n", "print(\"parent of each missingness indicator:\", mask.parents)" ] }, { "cell_type": "markdown", "id": "missing_values-8", "metadata": {}, "source": [ "## PC with its default handling\n", "\n", "The default, `missing=None`, is test-wise deletion for PC: each test reads the rows where its\n", "variables are observed, and `metadata[\"rows_tested\"]` gives the fewest rows and the median number of\n", "rows a test read. The rows a test keeps are a selected sample, as they depend on the variables\n", "through the parents above." ] }, { "cell_type": "code", "execution_count": 5, "id": "missing_values-9", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "rows read by a test: {'min': 645, 'median': 950.0}\n", "errors against the true CPDAG: 5\n" ] } ], "source": [ "default = andrey.pc(gappy)\n", "print(\"rows read by a test:\", default.metadata[\"rows_tested\"])\n", "print(\"errors against the true CPDAG:\", shd(default, truth))" ] }, { "cell_type": "markdown", "id": "missing_values-10", "metadata": {}, "source": [ "## PC with `missing=\"mvpc\"`\n", "\n", "Missing-Value PC finds the variables each missingness indicator depends on, then tests again the\n", "edges such an indicator could have kept, on data rebuilt from the rows where those variables are\n", "observed." ] }, { "cell_type": "code", "execution_count": 6, "id": "missing_values-11", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "parents of the indicators it found: {0: [2], 3: [4], 5: [0, 1], 6: [2], 7: [4], 8: [1], 9: [2]}\n", "edges it tested again and removed: [[3, 9]]\n", "errors against the true CPDAG: 1\n" ] } ], "source": [ "mvpc = andrey.pc(gappy, missing=\"mvpc\")\n", "print(\"parents of the indicators it found:\", mvpc.metadata[\"missingness_parents\"])\n", "print(\"edges it tested again and removed:\", mvpc.metadata[\"corrected_edges\"][\"removed\"])\n", "print(\"errors against the true CPDAG:\", shd(mvpc, truth))" ] }, { "cell_type": "markdown", "id": "missing_values-12", "metadata": {}, "source": [ "## A score search with `missing=\"em\"`\n", "\n", "A search that reads the data only through their covariance cannot delete rows per test, so it takes\n", "`missing=\"em\"`: structural EM replaces each row's missing values by their expected values under the\n", "model of the current graph and adds their uncertainty, which gives the expected covariance of the\n", "data, and the search runs on that covariance over every row." ] }, { "cell_type": "code", "execution_count": 7, "id": "missing_values-13", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "errors against the true CPDAG: 0\n" ] } ], "source": [ "em = andrey.ges(gappy, missing=\"em\")\n", "print(\"errors against the true CPDAG:\", shd(em, truth))" ] }, { "cell_type": "markdown", "id": "missing_values-14", "metadata": {}, "source": [ "## Compare\n", "\n", "PC and GES on the complete data show what each method makes of these data when nothing is missing." ] }, { "cell_type": "code", "execution_count": 8, "id": "missing_values-15", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\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", "
errors (SHD)
PC, complete data1
GES, complete data0
PC, default5
PC, missing=\"mvpc\"1
GES, missing=\"em\"0
\n", "
" ], "text/plain": [ " errors (SHD)\n", "PC, complete data 1\n", "GES, complete data 0\n", "PC, default 5\n", "PC, missing=\"mvpc\" 1\n", "GES, missing=\"em\" 0" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "outputs = {\n", " \"PC, complete data\": andrey.pc(complete.data),\n", " \"GES, complete data\": andrey.ges(complete.data),\n", " \"PC, default\": default,\n", " 'PC, missing=\"mvpc\"': mvpc,\n", " 'GES, missing=\"em\"': em,\n", "}\n", "pd.Series({name: shd(out, truth) for name, out in outputs.items()}, name=\"errors (SHD)\").to_frame()" ] }, { "cell_type": "markdown", "id": "missing_values-16", "metadata": {}, "source": [ "## Ten datasets\n", "\n", "One draw can differ from the next. The same comparison on ten datasets, each with its own graph and\n", "its own missing values:" ] }, { "cell_type": "code", "execution_count": 9, "id": "missing_values-17", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\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", "
mean errors (SHD)
PC, complete data1.8
GES, complete data1.5
PC, default4.8
PC, missing=\"mvpc\"3.6
GES, missing=\"em\"1.1
\n", "
" ], "text/plain": [ " mean errors (SHD)\n", "PC, complete data 1.8\n", "GES, complete data 1.5\n", "PC, default 4.8\n", "PC, missing=\"mvpc\" 3.6\n", "GES, missing=\"em\" 1.1" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rows = []\n", "for seed in range(10):\n", " full = data.sample_scm(d=10, n=2000, seed=seed, density=2)\n", " gaps = missingness.draw(full.data, rule, seed=seed)\n", " x = np.where(gaps.mask, np.nan, full.data)\n", " true = to_cpdag(full.graph)\n", " rows.append(\n", " {\n", " \"PC, complete data\": shd(andrey.pc(full.data), true),\n", " \"GES, complete data\": shd(andrey.ges(full.data), true),\n", " \"PC, default\": shd(andrey.pc(x), true),\n", " 'PC, missing=\"mvpc\"': shd(andrey.pc(x, missing=\"mvpc\"), true),\n", " 'GES, missing=\"em\"': shd(andrey.ges(x, missing=\"em\"), true),\n", " }\n", " )\n", "pd.DataFrame(rows).mean().round(1).to_frame(\"mean errors (SHD)\")" ] }, { "cell_type": "markdown", "id": "missing_values-18", "metadata": {}, "source": [ "On this dataset, PC's default handling makes 5 errors where PC on the complete data makes 1;\n", "`missing=\"mvpc\"` removes the edge it traces to the missingness parents and is back to 1, and GES\n", "with `missing=\"em\"` makes none, as it does on the complete data. Over ten datasets PC makes 4.8\n", "errors by default and 3.6 with MVPC against 1.8 on the complete data, so MVPC closes part of the\n", "gap, while GES with EM makes 1.1 against 1.5 on the complete data. The two searches differ in more\n", "than how they treat gaps, so read each against its own fit on the complete data; the [Missing\n", "values](https://andrey.abel.ai/docs/guides/getting-started.html#missing-values) guide and the\n", "[0.16.0 release card](https://andrey.abel.ai/docs/benchmarks/0.16.0.html#what-we-measured-on-missing-data)\n", "give what we measured against reference implementations." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.14.4" } }, "nbformat": 4, "nbformat_minor": 5 }