|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "intro", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# Post-Hoc FDP Bounds\n", |
| 9 | + "\n", |
| 10 | + "This notebook shows the smallest workflow for attaching a post-hoc FDP certificate to unweighted empirical split conformal p-values." |
| 11 | + ] |
| 12 | + }, |
| 13 | + { |
| 14 | + "cell_type": "markdown", |
| 15 | + "id": "import", |
| 16 | + "metadata": {}, |
| 17 | + "source": [ |
| 18 | + "## Import\n", |
| 19 | + "\n", |
| 20 | + "This section loads the dependencies used throughout the notebook." |
| 21 | + ] |
| 22 | + }, |
| 23 | + { |
| 24 | + "cell_type": "code", |
| 25 | + "execution_count": 1, |
| 26 | + "id": "imports", |
| 27 | + "metadata": {}, |
| 28 | + "outputs": [], |
| 29 | + "source": [ |
| 30 | + "import logging\n", |
| 31 | + "\n", |
| 32 | + "import numpy as np\n", |
| 33 | + "import pandas as pd\n", |
| 34 | + "from oddball import Dataset, load\n", |
| 35 | + "from pyod.models.iforest import IForest\n", |
| 36 | + "\n", |
| 37 | + "from nonconform import ConformalDetector, Split\n", |
| 38 | + "from nonconform.fdr import conformal_fdp_upper_bound_from_result\n", |
| 39 | + "from nonconform.metrics import false_discovery_rate, statistical_power\n", |
| 40 | + "\n", |
| 41 | + "root_logger = logging.getLogger(\"nonconform\")\n", |
| 42 | + "if not root_logger.handlers:\n", |
| 43 | + " root_logger.addHandler(logging.NullHandler())\n", |
| 44 | + "root_logger.setLevel(logging.ERROR)" |
| 45 | + ] |
| 46 | + }, |
| 47 | + { |
| 48 | + "cell_type": "markdown", |
| 49 | + "id": "setup", |
| 50 | + "metadata": {}, |
| 51 | + "source": [ |
| 52 | + "## Setup\n", |
| 53 | + "\n", |
| 54 | + "We load Shuttle with a fixed seed and choose a few p-value thresholds to certify after p-values are computed." |
| 55 | + ] |
| 56 | + }, |
| 57 | + { |
| 58 | + "cell_type": "code", |
| 59 | + "execution_count": 2, |
| 60 | + "id": "load-data", |
| 61 | + "metadata": {}, |
| 62 | + "outputs": [ |
| 63 | + { |
| 64 | + "name": "stdout", |
| 65 | + "output_type": "stream", |
| 66 | + "text": [ |
| 67 | + "x_train: (22793, 9), x_test: (1000, 9)\n", |
| 68 | + "y_test positives: 100\n", |
| 69 | + "calibration size=1000\n" |
| 70 | + ] |
| 71 | + } |
| 72 | + ], |
| 73 | + "source": [ |
| 74 | + "x_train, x_test, y_test = load(Dataset.SHUTTLE, setup=True, seed=42)\n", |
| 75 | + "\n", |
| 76 | + "n_calib = 1_000\n", |
| 77 | + "thresholds = np.array([0.005, 0.01, 0.02, 0.05, 0.1])\n", |
| 78 | + "\n", |
| 79 | + "print(f\"x_train: {x_train.shape}, x_test: {x_test.shape}\")\n", |
| 80 | + "print(f\"y_test positives: {int(y_test.sum())}\")\n", |
| 81 | + "print(f\"calibration size={n_calib}\")" |
| 82 | + ] |
| 83 | + }, |
| 84 | + { |
| 85 | + "cell_type": "markdown", |
| 86 | + "id": "certificate", |
| 87 | + "metadata": {}, |
| 88 | + "source": [ |
| 89 | + "## FDP Certificate\n", |
| 90 | + "\n", |
| 91 | + "`conformal_fdp_upper_bound_from_result(...)` uses the cached `last_result` from `compute_p_values(...)` and returns threshold-level FDP and precision certificates." |
| 92 | + ] |
| 93 | + }, |
| 94 | + { |
| 95 | + "cell_type": "code", |
| 96 | + "execution_count": 3, |
| 97 | + "id": "fit-and-bound", |
| 98 | + "metadata": {}, |
| 99 | + "outputs": [ |
| 100 | + { |
| 101 | + "name": "stdout", |
| 102 | + "output_type": "stream", |
| 103 | + "text": [ |
| 104 | + "method=mc_thc, confidence=0.95\n", |
| 105 | + " threshold discoveries fdp_upper_bound precision_lower_bound\n", |
| 106 | + " 0.005 103 0.226 0.774\n", |
| 107 | + " 0.010 111 0.210 0.790\n", |
| 108 | + " 0.020 120 0.259 0.741\n", |
| 109 | + " 0.050 154 0.423 0.577\n", |
| 110 | + " 0.100 212 0.581 0.419\n" |
| 111 | + ] |
| 112 | + } |
| 113 | + ], |
| 114 | + "source": [ |
| 115 | + "detector = ConformalDetector(\n", |
| 116 | + " detector=IForest(n_estimators=100, max_samples=0.8, random_state=42),\n", |
| 117 | + " strategy=Split(n_calib=n_calib),\n", |
| 118 | + " seed=42,\n", |
| 119 | + ")\n", |
| 120 | + "\n", |
| 121 | + "detector.fit(x_train)\n", |
| 122 | + "p_values = detector.compute_p_values(x_test)\n", |
| 123 | + "bounds = conformal_fdp_upper_bound_from_result(\n", |
| 124 | + " detector.last_result,\n", |
| 125 | + " confidence=0.95,\n", |
| 126 | + " method=\"mc_thc\",\n", |
| 127 | + " seed=42,\n", |
| 128 | + " thresholds=thresholds,\n", |
| 129 | + ")\n", |
| 130 | + "\n", |
| 131 | + "print(f\"method={bounds.method}, confidence={bounds.confidence}\")\n", |
| 132 | + "print(bounds.to_frame().to_string(index=False, float_format=lambda x: f\"{x:.3f}\"))" |
| 133 | + ] |
| 134 | + }, |
| 135 | + { |
| 136 | + "cell_type": "markdown", |
| 137 | + "id": "threshold", |
| 138 | + "metadata": {}, |
| 139 | + "source": [ |
| 140 | + "## Use a Threshold\n", |
| 141 | + "\n", |
| 142 | + "`select(...)` applies the threshold. `bound_at(...)` and `precision_at(...)` attach the post-hoc certificate to that same threshold. The empirical columns below use labels only to check this benchmark example." |
| 143 | + ] |
| 144 | + }, |
| 145 | + { |
| 146 | + "cell_type": "code", |
| 147 | + "execution_count": 4, |
| 148 | + "id": "select-threshold", |
| 149 | + "metadata": {}, |
| 150 | + "outputs": [ |
| 151 | + { |
| 152 | + "name": "stdout", |
| 153 | + "output_type": "stream", |
| 154 | + "text": [ |
| 155 | + " threshold discoveries fdp_upper_bound precision_lower_bound empirical_fdr power\n", |
| 156 | + " 0.050 154 0.423 0.577 0.351 1.000\n" |
| 157 | + ] |
| 158 | + } |
| 159 | + ], |
| 160 | + "source": [ |
| 161 | + "threshold = 0.05\n", |
| 162 | + "selected = bounds.select(threshold)\n", |
| 163 | + "\n", |
| 164 | + "summary = pd.DataFrame(\n", |
| 165 | + " [\n", |
| 166 | + " {\n", |
| 167 | + " \"threshold\": threshold,\n", |
| 168 | + " \"discoveries\": int(selected.sum()),\n", |
| 169 | + " \"fdp_upper_bound\": bounds.bound_at(threshold),\n", |
| 170 | + " \"precision_lower_bound\": bounds.precision_at(threshold),\n", |
| 171 | + " \"empirical_fdr\": false_discovery_rate(y_test, selected),\n", |
| 172 | + " \"power\": statistical_power(y_test, selected),\n", |
| 173 | + " }\n", |
| 174 | + " ]\n", |
| 175 | + ")\n", |
| 176 | + "\n", |
| 177 | + "print(summary.to_string(index=False, float_format=lambda x: f\"{x:.3f}\"))" |
| 178 | + ] |
| 179 | + }, |
| 180 | + { |
| 181 | + "cell_type": "markdown", |
| 182 | + "id": "interpretation", |
| 183 | + "metadata": {}, |
| 184 | + "source": [ |
| 185 | + "## Interpretation\n", |
| 186 | + "\n", |
| 187 | + "Read `fdp_upper_bound` as a high-confidence cap on the false-positive fraction among the selected points at that threshold. `precision_lower_bound` is the matching conservative minimum precision. Use the table to choose a practical trade-off, then report the threshold and certificate together. This is different from `detector.select(..., alpha=...)`, which is the fixed-level FDR-control workflow." |
| 188 | + ] |
| 189 | + } |
| 190 | + ], |
| 191 | + "metadata": { |
| 192 | + "kernelspec": { |
| 193 | + "display_name": "Python 3", |
| 194 | + "language": "python", |
| 195 | + "name": "python3" |
| 196 | + }, |
| 197 | + "language_info": { |
| 198 | + "codemirror_mode": { |
| 199 | + "name": "ipython", |
| 200 | + "version": 3 |
| 201 | + }, |
| 202 | + "file_extension": ".py", |
| 203 | + "mimetype": "text/x-python", |
| 204 | + "name": "python", |
| 205 | + "nbconvert_exporter": "python", |
| 206 | + "pygments_lexer": "ipython3", |
| 207 | + "version": "3.13.3" |
| 208 | + } |
| 209 | + }, |
| 210 | + "nbformat": 4, |
| 211 | + "nbformat_minor": 5 |
| 212 | +} |
0 commit comments