-
Notifications
You must be signed in to change notification settings - Fork 943
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug fix for frontdoor identification and a new set of tests (#1093)
* fixed frontdoor bug and added tests Signed-off-by: Amit Sharma <amit_sharma@live.com> * updated docstring Signed-off-by: Amit Sharma <amit_sharma@live.com> * reformatted file Signed-off-by: Amit Sharma <amit_sharma@live.com> --------- Signed-off-by: Amit Sharma <amit_sharma@live.com>
- Loading branch information
1 parent
1d050f0
commit 048117c
Showing
8 changed files
with
209 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import numpy as np | ||
import pandas as pd | ||
import pytest | ||
|
||
from dowhy import CausalModel | ||
from dowhy.causal_identifier import AutoIdentifier, BackdoorAdjustment | ||
from dowhy.causal_identifier.auto_identifier import identify_frontdoor | ||
from dowhy.causal_identifier.identify_effect import EstimandType | ||
|
||
from .base import IdentificationTestFrontdoorGraphSolution, example_frontdoor_graph_solution | ||
|
||
|
||
class TestFrontdoorIdentification(object): | ||
def test_identify_frontdoor_functional_api( | ||
self, example_frontdoor_graph_solution: IdentificationTestFrontdoorGraphSolution | ||
): | ||
graph = example_frontdoor_graph_solution.graph | ||
expected_sets = example_frontdoor_graph_solution.valid_frontdoor_sets | ||
invalid_sets = example_frontdoor_graph_solution.invalid_frontdoor_sets | ||
frontdoor_set = identify_frontdoor( | ||
graph, | ||
observed_nodes=example_frontdoor_graph_solution.observed_nodes, | ||
action_nodes=["X"], | ||
outcome_nodes=["Y"], | ||
) | ||
|
||
assert ( | ||
(len(frontdoor_set) == 0) and (len(expected_sets) == 0) | ||
) or ( # No adjustments exist and that's expected. | ||
set(frontdoor_set) in expected_sets and set(frontdoor_set) not in invalid_sets | ||
) | ||
|
||
def test_identify_frontdoor_causal_model( | ||
self, example_frontdoor_graph_solution: IdentificationTestFrontdoorGraphSolution | ||
): | ||
graph = example_frontdoor_graph_solution.graph | ||
expected_sets = example_frontdoor_graph_solution.valid_frontdoor_sets | ||
invalid_sets = example_frontdoor_graph_solution.invalid_frontdoor_sets | ||
observed_nodes = example_frontdoor_graph_solution.observed_nodes | ||
# Building the causal model | ||
num_samples = 10 | ||
df = pd.DataFrame(np.random.random((num_samples, len(observed_nodes))), columns=observed_nodes) | ||
model = CausalModel(data=df, treatment="X", outcome="Y", graph=graph) | ||
estimand = model.identify_effect() | ||
frontdoor_set = estimand.frontdoor_variables | ||
assert ( | ||
(len(frontdoor_set) == 0) and (len(expected_sets) == 0) | ||
) or ( # No adjustments exist and that's expected. | ||
(set(frontdoor_set) in expected_sets) and (set(frontdoor_set) not in invalid_sets) | ||
) |