Skip to content

Counterfactual reasoning tutorial#150

Merged
ColtonPayne merged 7 commits into
mainfrom
daniel/counterfactual-tutorial
May 13, 2026
Merged

Counterfactual reasoning tutorial#150
ColtonPayne merged 7 commits into
mainfrom
daniel/counterfactual-tutorial

Conversation

@dcollado03
Copy link
Copy Markdown
Contributor

Adds a counterfactual reasoning tutorial

New files:

  • examples/counterfactual_tutorial_ex.py
  • docs/source/tutorials/counterfactual_tutorial.rst

Modified:

  • docs/source/tutorials/index.rst (registers the new tutorial in the toctree)

Builds on the cybersecurity inconsistency tutorial. Reuses the same knowledge graph and rule chain, adds a counterfactual harness that runs PyReason twice (baseline + perturbed) and diffs the resulting interpretations.

Three demos:

  1. Single-edge graph perturbation — remove an edge, watch the cascade collapse on the affected node.
  2. Mid-chain fact injection — inject at_risk(workstation_1):[0.0, 0.0] to break the rule chain partway through.
  3. Inconsistency attribution — remove the asserted patch_confidence fact for dev_server and observe whether the rule-triggered conflict resolves cleanly.
Screenshot 2026-05-07 143744 image image

dcollado03 and others added 4 commits May 7, 2026 14:35
Adds a tutorial demonstrating counterfactual reasoning on top of
PyReason's annotated logic engine, building on the cybersecurity
inconsistency tutorial. Three demos: single-edge graph perturbation,
mid-chain fact injection, and inconsistency attribution via fact removal.

Includes findings about PyReason's monotonicity behavior — bounds can
only tighten, so late conflicting writes are silently rejected rather
than triggering inconsistency resolution.
Copy link
Copy Markdown
Collaborator

@ColtonPayne ColtonPayne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice Updates, this helps me understand what you are doing a lot - small updates to demo 1 requested.

Each grounding is independent. Removing a graph edge or fact eliminates
any grounding that depended on it; the others fire normally. This is
why counterfactual perturbations have such localized effects -- they
surgically delete specific groundings without touching the rest.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deleting is only one type of pertubation - say something about modifying, adding, or removing specific groundings rather than just deleting them

web_server patch_confidence [0.0, 0.2] (none)
================ ================= ================= ==================

``workstation_1`` and ``dev_server`` are unaffected -- their groundings
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes sense, but show the table starting at line 86 with the grounding removed for at_risk(web_server) for an apples-to-apples comparison. Then, list the vulnerable, compromised, and patch_confidence) rules ahead of this table so we can see why these didn't fire.

workstation_1 patch_confidence [0.0, 0.2] (none)
================ ================= ================= ==================

A single injected fact eliminated three downstream groundings, exactly
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This demo is good, use the table structure for reference for demo 1. Just add the rule text for the vulnerable, compromised, and patch confidence rule in demo one and reference that here as well.

the baseline, consistent in the counterfactual. That is a more subtle
effect than Demos 1 and 2, where the fact change eliminated
groundings outright. Here the grounding stays; only its consistency
changes.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work on this demo.

Copy link
Copy Markdown
Collaborator

@ColtonPayne ColtonPayne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ColtonPayne ColtonPayne merged commit 0826924 into main May 13, 2026
3 checks passed
@ColtonPayne ColtonPayne deleted the daniel/counterfactual-tutorial branch May 13, 2026 16:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants