causal_testing.discovery.abstract_discovery

This module implements the abstract Discovery class to infer causal DAGs from data.

Module Contents

Classes

Discovery

Abstract class for causal discovery.

Functions

simple_cycle(causal_dag)

Find a cycle in the given CausalDAG, if one exists, returns the first found.

effect_direction(→ str)

Check whether the estimated causal effect is negative or positive.

is_match(u, v, patterns)

Check whether a given edge matches a given pattern.

Attributes

TestResult

causal_testing.discovery.abstract_discovery.TestResult
causal_testing.discovery.abstract_discovery.simple_cycle(causal_dag: causal_testing.specification.causal_dag.CausalDAG)

Find a cycle in the given CausalDAG, if one exists, returns the first found.

Parameters:

causal_dag – The CausalDAG to check for cycles.

Returns:

A list of edges in the cycle, or an empty list if there are no cycles.

causal_testing.discovery.abstract_discovery.effect_direction(result: causal_testing.testing.causal_test_result.CausalTestResult) str

Check whether the estimated causal effect is negative or positive.

Parameters:

result – The causal test result object.

Returns:

Whether the estimated causal test is positive or negative (or no effect).

causal_testing.discovery.abstract_discovery.is_match(u: str, v: str, patterns: list[str])

Check whether a given edge matches a given pattern.

Parameters:
  • u – The origin node of the edge.

  • v – The destination node of the edge.

  • patterns – A list of tuples containing the patterns to check against.

Returns:

True if the edge matches the pattern, False otherwise.

class causal_testing.discovery.abstract_discovery.Discovery(df: pandas.DataFrame, random_seed: int = 0, exclude_edges: str = None, include_edges: str = None, alpha: float = 0.05)

Bases: abc.ABC

Abstract class for causal discovery.

abstract discover() causal_testing.specification.causal_dag.CausalDAG

Discover the causal DAG.

Returns:

The inferred causal DAG.

remove_cycles(causal_dag: causal_testing.specification.causal_dag.CausalDAG)

Remove cycles from individuals by iteratively deleting a random edge from each cycle until there are no more cycles.

Parameters:

causal_dag – The CausalDAG to be repaired.

write_dot(individual: causal_testing.specification.causal_dag.CausalDAG, output_file: str)

Write the given individual to the given output file.

Parameters:
  • individual – The causal DAG to output.

  • output_file – The name of the file to write to.

_json_stub_params(outcome: str) str
evaluate_tests(causal_dag: causal_testing.specification.causal_dag.CausalDAG) pandas.DataFrame

Generate and evaluate causal test cases from the supplied CausalDAG and return a list of edges for which the corresponding causal test case failed. These results are then assigned to a new attribute test_results within the individual for later reuse.

Parameters:

causal_dag – The CausalDAG to evaluate.

Returns:

Pandas dataframe with test outcome details (result, expected effect, treatment, outcome, effect direction).