Skip to content

Castle Algorithm#3422

Draft
Manas-7854 wants to merge 11 commits into
pgmpy:devfrom
Manas-7854:castle
Draft

Castle Algorithm#3422
Manas-7854 wants to merge 11 commits into
pgmpy:devfrom
Manas-7854:castle

Conversation

@Manas-7854

@Manas-7854 Manas-7854 commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

The following checklist is mandatory

Your PR will be closed if you remove the checklist. Use of LLMs is strictly forbidden for any part of this checklist (including for improving language), and will result in a ban if we find any use of LLMs.

Your checklist for this pull request

  • Have you followed all the steps from our Contributing Guide?
  • Does the PR fully address the linked issue and is within its defined scope? If you are still working on the PR, mark it as draft. - Marked as draft
  • Are all the GitHub Actions checks passing? If not, mark your PR as draft while you fix it.

If you have used AI/LLMs for any assistance, please answer the following questions. Please refer #2622 for an example of the level of detail we expect:

  • Have you reviewed our AI usage policy?

  • Are you able to fully explain your changes? We expect you to fully understand the algorithm and take full responsibility for any changes in this PR.

  • Please list the AI tool(s) used, along with the model and its version used.

GPT - 5.2 Codex

  • Please describe in detail how and what you used AI assistance for? Please outline your whole workflow with the AI tool.

I provided the detailed api of the algorithm to it and asked to just write the skeleton of the algorithm with TBD inside the actual function which will be implemented later on

  • What steps have you taken to verify that the changes correctly address the issue? What edge cases have you considered? Other than running tests, what else have you verified?

I have gone through the provided skeleton and cross checked everything with the api. Tests are TBD.

  • Have you used AI for generating tests? Can you compress them into a smaller number of tests without losing coverage?

TBD

Issue number(s) that this pull request fixes

List of changes to the codebase in this pull request

  • added pgmpy/pgmpy/castle.py
  • updated pgmpy/pgmpy/__init__.py with the castle algorithm

Copilot AI review requested due to automatic review settings June 2, 2026 20:29
@Manas-7854 Manas-7854 changed the title Added skeleton for castle Castle Algorithm Jun 2, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Introduces a new CASTLE causal discovery estimator and exposes it via the pgmpy.causal_discovery package API.

Changes:

  • Added a new pgmpy/causal_discovery/castle.py module with configuration dataclasses and a (currently stubbed) CASTLE implementation.
  • Exported CASTLE from pgmpy/causal_discovery/__init__.py.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 6 comments.

File Description
pgmpy/causal_discovery/castle.py Adds CASTLE module skeleton, torch soft-dependency check, and public CASTLE class stub.
pgmpy/causal_discovery/init.py Exposes CASTLE in the package-level API (__all__).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@@ -1,3 +1,4 @@
from .castle import CASTLE
Comment thread pgmpy/causal_discovery/castle.py Outdated
Comment on lines +8 to +13
_check_soft_dependencies(
"torch",
msg=("CASTLE requires torch to be installed. "),
)
torch = _safe_import("torch")
nn = torch.nn
edge_threshold: float


class _CASTLEModel(nn.Module):
Comment on lines +53 to +56
def train(self, X_tensor):
"""Train the CASTLE model and return the adjacency matrix."""
# TODO: Implement the CASTLE training loop.
raise NotImplementedError("TBD")
Comment thread pgmpy/causal_discovery/castle.py Outdated
raise NotImplementedError("TBD")


class CASTLE(_BaseCausalDiscovery):
Comment thread pgmpy/causal_discovery/castle.py Outdated
Comment on lines +69 to +88
def __init__(
self,
dag_weight: float = 1.0,
sparsity_weight: float = 5.0,
dag_penalty: float = 1.0,
optimizer: object | None = None,
batch_size: int = 32,
hidden_dim: int = 32,
edge_threshold: float = 0.3,
target_col: int | str | None = None,
max_epochs: int = 200,
min_loss_improvement: float = 1e-4,
early_stop_patience: int = 10,
scaler: object | None = None,
tensorboard_log_dir: str | None = None,
seed: int = 42,
):
"""Initialize the CASTLE estimator."""
# TODO: Implement CASTLE parameter setup.
raise NotImplementedError("TBD")
@Manas-7854 Manas-7854 marked this pull request as draft June 2, 2026 20:30
@Manas-7854

Manas-7854 commented Jun 2, 2026

Copy link
Copy Markdown
Contributor Author

@ankurankan @DARHWOLF
I have added this draft PR for castle, I will work on this and commit changes along the way.
As of now to define the optimizer and scalar in the dataclass, I have used object - however this can be changed to Any, which will require importing typing library, however if we stick to object, we will have to call isinstance every single time we interact with it.
I would suggest switching to Any

@ankurankan ankurankan moved this from Backlog to PR In Progress in Mentored Projects (May - Aug) Jun 3, 2026
@DARHWOLF DARHWOLF added the Causal Discovery Related to causal discovery algorithms. label Jun 7, 2026
@ankurankan ankurankan marked this pull request as ready for review June 10, 2026 15:41
@Manas-7854 Manas-7854 marked this pull request as draft June 10, 2026 15:55
@codecov

codecov Bot commented Jun 12, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 99.19355% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 95.75%. Comparing base (4c2cdb9) to head (d774f71).

Files with missing lines Patch % Lines
pgmpy/causal_discovery/castle.py 97.46% 2 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##              dev    #3422      +/-   ##
==========================================
+ Coverage   95.72%   95.75%   +0.02%     
==========================================
  Files         585      587       +2     
  Lines       33074    33312     +238     
==========================================
+ Hits        31661    31897     +236     
- Misses       1413     1415       +2     
Files with missing lines Coverage Δ
pgmpy/causal_discovery/__init__.py 100.00% <100.00%> (ø)
pgmpy/tests/test_causal_discovery/test_castle.py 100.00% <100.00%> (ø)
pgmpy/causal_discovery/castle.py 97.46% <97.46%> (ø)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Causal Discovery Related to causal discovery algorithms.

Projects

Status: PR In Progress

Development

Successfully merging this pull request may close these issues.

[ENH] CASTLE Algorithm

4 participants