Skip to content

Commit

Permalink
Expose max_conditions in add_ci_undirected_edges (#221)
Browse files Browse the repository at this point in the history
  • Loading branch information
cthoyt committed Apr 29, 2024
1 parent 10289af commit 257b46a
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/y0/algorithm/conditional_independencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def add_ci_undirected_edges(
*,
method: Optional[CITest] = None,
significance_level: Optional[float] = None,
max_conditions: Optional[int] = None,
) -> NxMixedGraph:
"""Add undirected edges between d-separated nodes that fail a data-driven conditional independency test.
Expand All @@ -50,11 +51,17 @@ def add_ci_undirected_edges(
:param significance_level: The statistical tests employ this value for
comparison with the p-value of the test to determine the independence of
the tested variables. If none, defaults to 0.05.
:param max_conditions: Longest set of conditions to investigate
:returns: A copy of the input graph potentially with new undirected edges added
"""
rv = graph.copy()
for judgement, result in test_conditional_independencies(
graph=graph, data=data, method=method, boolean=True, significance_level=significance_level
graph=graph,
data=data,
method=method,
boolean=True,
significance_level=significance_level,
max_conditions=max_conditions,
):
if not result:
rv.add_undirected_edge(judgement.left, judgement.right)
Expand All @@ -69,6 +76,7 @@ def test_conditional_independencies(
boolean: bool = False,
significance_level: Optional[float] = None,
_method_checked: bool = False,
max_conditions: Optional[int] = None,
) -> List[Tuple[DSeparationJudgement, Union[bool, CITestTuple]]]:
"""Gets CIs with :func:`get_conditional_independencies` then tests them against data.
Expand All @@ -84,6 +92,7 @@ def test_conditional_independencies(
:param significance_level: The statistical tests employ this value for
comparison with the p-value of the test to determine the independence of
the tested variables. If none, defaults to 0.05.
:param max_conditions: Longest set of conditions to investigate
:returns: A copy of the input graph potentially with new undirected edges added
"""
if significance_level is None:
Expand All @@ -100,14 +109,15 @@ def test_conditional_independencies(
_method_checked=True,
),
)
for judgement in get_conditional_independencies(graph)
for judgement in get_conditional_independencies(graph, max_conditions=max_conditions)
]


def get_conditional_independencies(
graph: NxMixedGraph,
*,
policy=None,
max_conditions: Optional[int] = None,
**kwargs,
) -> Set[DSeparationJudgement]:
"""Get the conditional independencies from the given ADMG.
Expand All @@ -117,6 +127,7 @@ def get_conditional_independencies(
:param graph: An acyclic directed mixed graph
:param policy: Retention policy when more than one conditional independency option exists (see minimal for details)
:param max_conditions: Longest set of conditions to investigate
:param kwargs: Other keyword arguments are passed to :func:`d_separations`
:return: A set of conditional dependencies
Expand All @@ -125,7 +136,7 @@ def get_conditional_independencies(
if policy is None:
policy = get_topological_policy(graph)
return minimal(
d_separations(graph, **kwargs),
d_separations(graph, max_conditions=max_conditions, **kwargs),
policy=policy,
)

Expand Down

0 comments on commit 257b46a

Please sign in to comment.