From 5659784badcaca95b757391b9bf28850e77477eb Mon Sep 17 00:00:00 2001 From: Charles Tapley Hoyt Date: Tue, 23 Apr 2024 22:44:30 +0200 Subject: [PATCH] Minor cleanup on unfinished notebooks These don't run as is --- notebooks/Demo_CTFTR.ipynb | 149 ++++++---------------------------- notebooks/Demo_Sigma_TR.ipynb | 60 +------------- notebooks/Demo_Tian_ID.ipynb | 99 +--------------------- 3 files changed, 31 insertions(+), 277 deletions(-) diff --git a/notebooks/Demo_CTFTR.ipynb b/notebooks/Demo_CTFTR.ipynb index 820c1d9a..65cf5a4d 100644 --- a/notebooks/Demo_CTFTR.ipynb +++ b/notebooks/Demo_CTFTR.ipynb @@ -3,26 +3,25 @@ { "cell_type": "code", "execution_count": 1, - "id": "1da63220-3532-49ae-a441-bb526121b804", + "id": "01e73bea-b68f-41c1-9e9f-09b8414a1ebc", "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "ImportError", + "evalue": "cannot import name '_inconsistent_counterfactual_factor_variable_and_intervention_values' from 'y0.algorithm.counterfactual_transportability' (/Users/cthoyt/dev/y0/src/y0/algorithm/counterfactual_transportability.py)", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mImportError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[1], line 36\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01my0\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mdsl\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m (\n\u001b[1;32m 5\u001b[0m One,\n\u001b[1;32m 6\u001b[0m P,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 33\u001b[0m Fraction,\n\u001b[1;32m 34\u001b[0m )\n\u001b[1;32m 35\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01my0\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mgraph\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m NxMixedGraph\n\u001b[0;32m---> 36\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01my0\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01malgorithm\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mcounterfactual_transportability\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m (\n\u001b[1;32m 37\u001b[0m _any_variables_with_inconsistent_values,\n\u001b[1;32m 38\u001b[0m _compute_ancestral_components_from_ancestral_sets,\n\u001b[1;32m 39\u001b[0m _counterfactual_factor_is_inconsistent,\n\u001b[1;32m 40\u001b[0m _get_ancestral_components,\n\u001b[1;32m 41\u001b[0m _get_ancestral_set_after_intervening_on_conditioned_variables,\n\u001b[1;32m 42\u001b[0m _get_conditioned_variables_in_ancestral_set,\n\u001b[1;32m 43\u001b[0m _inconsistent_counterfactual_factor_variable_and_intervention_values,\n\u001b[1;32m 44\u001b[0m _inconsistent_counterfactual_factor_variable_intervention_values,\n\u001b[1;32m 45\u001b[0m _no_intervention_variables_in_domain,\n\u001b[1;32m 46\u001b[0m _no_transportability_nodes_in_domain,\n\u001b[1;32m 47\u001b[0m _reduce_reflexive_counterfactual_variables_to_interventions,\n\u001b[1;32m 48\u001b[0m _remove_repeated_variables_and_values,\n\u001b[1;32m 49\u001b[0m _remove_transportability_vertices,\n\u001b[1;32m 50\u001b[0m _split_event_by_reflexivity,\n\u001b[1;32m 51\u001b[0m _transport_unconditional_counterfactual_query_line_2,\n\u001b[1;32m 52\u001b[0m convert_to_counterfactual_factor_form,\n\u001b[1;32m 53\u001b[0m counterfactual_factors_are_transportable,\n\u001b[1;32m 54\u001b[0m do_counterfactual_factor_factorization,\n\u001b[1;32m 55\u001b[0m get_ancestors_of_counterfactual,\n\u001b[1;32m 56\u001b[0m get_counterfactual_factors,\n\u001b[1;32m 57\u001b[0m is_counterfactual_factor_form,\n\u001b[1;32m 58\u001b[0m make_selection_diagram,\n\u001b[1;32m 59\u001b[0m minimize,\n\u001b[1;32m 60\u001b[0m minimize_event,\n\u001b[1;32m 61\u001b[0m same_district,\n\u001b[1;32m 62\u001b[0m simplify,\n\u001b[1;32m 63\u001b[0m transport_conditional_counterfactual_query,\n\u001b[1;32m 64\u001b[0m transport_district_intervening_on_parents,\n\u001b[1;32m 65\u001b[0m transport_unconditional_counterfactual_query,\n\u001b[1;32m 66\u001b[0m )\n\u001b[1;32m 67\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01my0\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01malgorithm\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mtransport\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m transport_variable\n\u001b[1;32m 68\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01my0\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mdsl\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m (\n\u001b[1;32m 69\u001b[0m PP,\n\u001b[1;32m 70\u001b[0m TARGET_DOMAIN,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 91\u001b[0m Z,\n\u001b[1;32m 92\u001b[0m )\n", + "\u001b[0;31mImportError\u001b[0m: cannot import name '_inconsistent_counterfactual_factor_variable_and_intervention_values' from 'y0.algorithm.counterfactual_transportability' (/Users/cthoyt/dev/y0/src/y0/algorithm/counterfactual_transportability.py)" + ] + } + ], "source": [ "import os\n", "import sys\n", "\n", - "# To run the code, replace the following with something appropriate for your setup\n", - "if \"[redacted]/y0/src/\" not in sys.path:\n", - " sys.path.append(\"[redacted]/y0/src/\")" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "id": "01e73bea-b68f-41c1-9e9f-09b8414a1ebc", - "metadata": {}, - "outputs": [], - "source": [ - "# Previous notebook imports\n", "from y0.dsl import (\n", " One,\n", " P,\n", @@ -117,7 +116,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "id": "e03078dd-d705-45ce-ae34-573894143c3a", "metadata": {}, "outputs": [], @@ -153,19 +152,10 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": null, "id": "5c7bac3d-f305-4dad-97a9-0447c771b509", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "frozenset({Z})\n", - "frozenset({Z})\n" - ] - } - ], + "outputs": [], "source": [ "## Example 4.5, get_conditioned_variables_in_ancestral_set\n", "expected_result_1 = frozenset({Z})\n", @@ -175,25 +165,15 @@ " graph=figure_1_graph_no_transportability_nodes,\n", ")\n", "print(str(expected_result_1))\n", - "print(str(result_1))\n", - "# self.assertSetEqual(expected_result_1, result_1)" + "print(str(result_1))" ] }, { "cell_type": "code", - "execution_count": 7, + "execution_count": null, "id": "6da5af09-1ca0-4bb8-bae4-52d14e570b39", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "frozenset({frozenset({Z @ -X, X}), frozenset({Y @ -X})})\n", - "frozenset({frozenset({Y @ -X}), frozenset({X, Z @ -X})})\n" - ] - } - ], + "outputs": [], "source": [ "## Get ancestral components\n", "\"\"\"First test of a function to compute ancestral components for a graph.\n", @@ -207,60 +187,15 @@ " graph=figure_1_graph_no_transportability_nodes,\n", ")\n", "print(str(expected_result_1))\n", - "print(str(result_1))\n", - "# self.assertSetEqual(expected_result_1, result_1)" + "print(str(result_1))" ] }, { "cell_type": "code", - "execution_count": 12, + "execution_count": null, "id": "723a8030-f821-4fbe-a7e6-750cc2784a39", "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "In transport_unconditional_counterfactual_query: input event = [(Y @ (-X, -Z), -Y)]\n", - "In simplify before return: return value = [(Y @ (-X, -Z), -Y)]\n", - "In transport_unconditional_counterfactual_query: simplifed event = [(Y @ (-X, -Z), -Y)]\n", - "Updating ancestral set for variable Y @ (-X, -Z) with {Y @ (-X, -Z)}\n", - "In get_counterfactual_factors_retaining_variable_values(): district_mappings = defaultdict(, {frozenset({Y}): {(Y @ (-X, -Z), -Y)}})\n", - "In get_counterfactual_factors_retaining_variable_values(): returning [{(Y @ (-X, -Z), -Y)}]\n", - "In _inconsistent_counterfactual_factor_variable_and_intervention_values: dictionary = defaultdict(, {})\n", - "In _inconsistent_counterfactual_factor_variable_intervention_values: dictionary = defaultdict(, {Z: {-Z}, X: {-X}})\n", - "k = 0\n", - "k = 1\n", - "In sigma_tr: domain = 0\n", - "Subgraph_probability: P^{\\pi^\\ast}(X, Y, Z)\n", - "In _compute_c_factor: graph_topo = [X, Z, Y]\n", - "In _compute_c_factor: subgraph_topo = [X, Z, Y]\n", - "In _compute_c_factor_conditioning_on_topological_predecessors: topo = [X, Z, Y]\n", - "In _compute_c_factor_conditioning_on_topological_predecessors: graph_probability = P^{\\pi^\\ast}(X, Y, Z)\n", - "In _compute_c_factor_conditioning_on_topological_predecessors: returning P(Y | X, Z)\n", - "Return value in Latex form is P^{\\pi^\\ast}(Y | X, Z)\n", - "super_district_q_probability: P^{\\pi^\\ast}(Y | X, Z)\n", - "In identify_variables_in_district: A = C. Applying Lemma 3.\n", - " Subgraph_probability = P^{\\pi^\\ast}(Y | X, Z)\n", - " Returning Q value: P^{\\pi^\\ast}(Y | X, Z)\n", - "Returning from sigma_tr: P^{\\pi^\\ast}(Y | X, Z)\n", - "In transport_unconditional_counterfactual_query: got a Q value of P^{\\pi^\\ast}(Y | X, Z) for district {Y} corresponding to counterfactual factor {(Y @ (-X, -Z), -Y)}.\n", - "Returning: P^{\\pi^\\ast}(Y | X, Z) for simplified event: [(Y @ (-X, -Z), -Y)]\n", - "In transport_conditional_counterfactual_query: unconditional_query_result = (P(Y | X, Z), [(Y @ (-X, -Z), -Y)])\n", - "In transport_conditional_counterfactual_query: result_expression = \\frac{P^{\\pi^\\ast}(Y | X, Z)}{\\sum\\limits_{Y} P^{\\pi^\\ast}(Y | X, Z)}\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "expected_result_expr = \\frac{P^{\\pi^\\ast}(Y | X, Z)}{\\sum\\limits_{Y} P^{\\pi^\\ast}(Y | X, Z)}\n", - "Result_expr = \\frac{P^{\\pi^\\ast}(Y | X, Z)}{\\sum\\limits_{Y} P^{\\pi^\\ast}(Y | X, Z)}\n", - "Result_event = [(Y, -Y), (Z, -Z), (X, +X)]\n", - "expected_result_event = [(Y, -Y), (X, +X), (Z, -Z)]\n" - ] - } - ], + "outputs": [], "source": [ "## Transport a conditional counterfactual query! Example 4.5.\n", "\n", @@ -294,42 +229,8 @@ "print(\"expected_result_expr = \" + expected_result_expr.to_latex())\n", "print(\"Result_expr = \" + result_expr.to_latex())\n", "print(\"Result_event = \" + str(result_event))\n", - "print(\"expected_result_event = \" + str(expected_result_event))\n", - "# self.assert_expr_equal(expected_result_expr, result_expr)\n", - "# self.assertCountEqual(expected_result_event, result_event)" + "print(\"expected_result_event = \" + str(expected_result_event))" ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "d8aa2291-9dff-4ca9-a960-28728f8b1c30", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "17c3770d-fcd6-420a-b96c-f10f153bde91", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "8757a4aa-a109-400b-97ed-dd5397c24d63", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "57692605-c459-4a02-b46b-70bd4d70454e", - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { @@ -348,7 +249,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.12" + "version": "3.11.9" } }, "nbformat": 4, diff --git a/notebooks/Demo_Sigma_TR.ipynb b/notebooks/Demo_Sigma_TR.ipynb index 081a5c63..992ba1d4 100644 --- a/notebooks/Demo_Sigma_TR.ipynb +++ b/notebooks/Demo_Sigma_TR.ipynb @@ -1,20 +1,5 @@ { "cells": [ - { - "cell_type": "code", - "execution_count": 1, - "id": "d8724776-907d-4971-a383-09fe48c6c7b1", - "metadata": {}, - "outputs": [], - "source": [ - "import os\n", - "import sys\n", - "\n", - "# To run the code, replace the following with something appropriate for your setup\n", - "if \"[redacted]/y0/src/\" not in sys.path:\n", - " sys.path.append(\"[redacted]/y0/src/\")" - ] - }, { "cell_type": "code", "execution_count": 2, @@ -22,7 +7,8 @@ "metadata": {}, "outputs": [], "source": [ - "# Previous notebook imports\n", + "import os\n", + "import sys\n", "from y0.dsl import (\n", " One,\n", " P,\n", @@ -984,46 +970,6 @@ "# logger.warning(\" Expected result: \" + expected_result.to_latex())\n", "assert_expr_equal(result_4, expected_result)" ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "b40d46d8-905e-4805-8338-a196f04b6277", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "e928e62d-c5e6-442e-a6e6-21640bd1013f", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "a012cee4-bb9f-42b3-89e2-53f71f542734", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "701f6c2a-664a-4cc8-b151-c4af0b4c8617", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "10c3be23-6a4c-4876-a0f5-62bf35df4719", - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { @@ -1042,7 +988,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.12" + "version": "3.11.9" } }, "nbformat": 4, diff --git a/notebooks/Demo_Tian_ID.ipynb b/notebooks/Demo_Tian_ID.ipynb index 5e138ab7..9f228c6b 100644 --- a/notebooks/Demo_Tian_ID.ipynb +++ b/notebooks/Demo_Tian_ID.ipynb @@ -2,27 +2,14 @@ "cells": [ { "cell_type": "code", - "execution_count": 74, - "id": "d53b472f-0b99-40c2-8129-bc0506f07fd2", + "execution_count": 75, + "id": "303c066e-ed09-49d8-a917-144e3780f175", "metadata": {}, "outputs": [], "source": [ "import os\n", "import sys\n", "\n", - "# To run the code, replace the following with something appropriate for your setup\n", - "if \"[redacted]/y0/src/\" not in sys.path:\n", - " sys.path.append(\"[redacted]y0/src/\")" - ] - }, - { - "cell_type": "code", - "execution_count": 75, - "id": "303c066e-ed09-49d8-a917-144e3780f175", - "metadata": {}, - "outputs": [], - "source": [ - "# Previous notebook imports\n", "from y0.dsl import (\n", " One,\n", " P,\n", @@ -1620,86 +1607,6 @@ "source": [ "expected_result" ] - }, - { - "cell_type": "code", - "execution_count": 114, - "id": "72ac4f83-515f-4e83-a460-384642bb1ad7", - "metadata": {}, - "outputs": [ - { - "data": { - "text/latex": [ - "$P(X, Y, Z)$" - ], - "text/plain": [ - "P(X, Y, Z)" - ] - }, - "execution_count": 114, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "P({X} | {Y, Z})" - ] - }, - { - "cell_type": "code", - "execution_count": 118, - "id": "07d1a403-3e98-4695-825e-8ccaf3bcc2f3", - "metadata": {}, - "outputs": [ - { - "data": { - "text/latex": [ - "$P(X | Y, Z)$" - ], - "text/plain": [ - "P(X | Y, Z)" - ] - }, - "execution_count": 118, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "P(X | Y, Z)" - ] - }, - { - "cell_type": "code", - "execution_count": 119, - "id": "29f431b4-6145-4894-86a9-32e967977115", - "metadata": {}, - "outputs": [ - { - "data": { - "text/latex": [ - "$P(X | Y, Z)$" - ], - "text/plain": [ - "P(X | Y, Z)" - ] - }, - "execution_count": 119, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "P(X | {Y, Z})" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "56477856-9fde-4690-a940-2c1c209e2d83", - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { @@ -1718,7 +1625,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.12" + "version": "3.11.9" } }, "nbformat": 4,