Skip to content

Commit d8ee91c

Browse files
authored
Merge pull request #163 from CITCOM-project/functional_form
Functional form
2 parents e23b9b1 + e1b73b0 commit d8ee91c

File tree

12 files changed

+235
-369
lines changed

12 files changed

+235
-369
lines changed

causal_testing/testing/estimators.py

Lines changed: 61 additions & 110 deletions
Large diffs are not rendered by default.

examples/covasim_/doubling_beta/causal_test_beta.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,18 @@ def doubling_beta_CATE_on_csv(observational_data_path: str, simulate_counterfact
5050
linear_regression_estimator = LinearRegressionEstimator('beta', 0.032, 0.016,
5151
{'avg_age', 'contacts'}, # We use custom adjustment set
5252
'cum_infections',
53-
df=past_execution_df)
53+
df=past_execution_df,
54+
formula="cum_infections ~ beta + np.power(beta, 2) + avg_age + contacts")
5455

5556
# Add squared terms for beta, since it has a quadratic relationship with cumulative infections
56-
linear_regression_estimator.add_squared_term_to_df('beta')
5757
causal_test_result = causal_test_engine.execute_test(linear_regression_estimator, causal_test_case, 'ate')
5858

5959
# Repeat for association estimate (no adjustment)
6060
no_adjustment_linear_regression_estimator = LinearRegressionEstimator('beta', 0.032, 0.016,
6161
set(),
6262
'cum_infections',
63-
df=past_execution_df)
64-
no_adjustment_linear_regression_estimator.add_squared_term_to_df('beta')
63+
df=past_execution_df,
64+
formula="cum_infections ~ beta + np.power(beta, 2)")
6565
association_test_result = causal_test_engine.execute_test(no_adjustment_linear_regression_estimator, causal_test_case, 'ate')
6666

6767
# Store results for plotting
@@ -82,8 +82,8 @@ def doubling_beta_CATE_on_csv(observational_data_path: str, simulate_counterfact
8282
counterfactual_linear_regression_estimator = LinearRegressionEstimator('beta', 0.032, 0.016,
8383
{'avg_age', 'contacts'},
8484
'cum_infections',
85-
df=counterfactual_past_execution_df)
86-
counterfactual_linear_regression_estimator.add_squared_term_to_df('beta')
85+
df=counterfactual_past_execution_df,
86+
formula="cum_infections ~ beta + np.power(beta, 2) + avg_age + contacts")
8787
counterfactual_causal_test_result = causal_test_engine.execute_test(linear_regression_estimator, causal_test_case, 'ate')
8888
results_dict['counterfactual'] = {'ate': counterfactual_causal_test_result.test_value.value,
8989
'cis': counterfactual_causal_test_result.confidence_intervals,
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
intensity_num_shapes_results_random_1000.csv
2+
width_num_shapes_results_random_1000.csv

examples/poisson-line-process/causal_test_poisson.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,20 +117,18 @@ def test_intensity_num_shapes(
117117
effect_modifiers=causal_test_case.effect_modifier_configuration,
118118
)
119119
else:
120+
square_terms = [f"np.power({t}, 2)" for t in square_terms]
121+
inverse_terms = [f"np.float_power({t}, -1)" for t in inverse_terms]
120122
estimator = LinearRegressionEstimator(
121123
treatment=treatment,
122124
control_value=causal_test_case.control_value,
123125
treatment_value=causal_test_case.treatment_value,
124126
adjustment_set=set(),
125127
outcome=outcome,
126128
df=data,
127-
intercept=0,
128129
effect_modifiers=causal_test_case.effect_modifier_configuration,
130+
formula=f"{outcome} ~ {treatment} + {'+'.join(square_terms + inverse_terms + list([e.name for e in causal_test_case.effect_modifier_configuration]))} -1"
129131
)
130-
for t in square_terms:
131-
estimator.add_squared_term_to_df(t)
132-
for t in inverse_terms:
133-
estimator.add_inverse_term_to_df(t)
134132

135133
# 10. Execute the test
136134
causal_test_result = causal_test_engine.execute_test(

examples/poisson-line-process/intensity_num_shapes_results_random_1000.csv

Lines changed: 0 additions & 41 deletions
This file was deleted.

examples/poisson-line-process/width_num_shapes_results_random_1000.csv

Lines changed: 0 additions & 154 deletions
This file was deleted.

examples/poisson/README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,3 @@ To run this case study:
77
(instructions are provided in the project README).
88
2. Change directory to `causal_testing/examples/poisson`.
99
3. Run the command `python run_causal_tests.py --data_path data.csv --dag_path dag.dot --json_path causal_tests.json`
10-
11-
This should print a series of causal test results and produce two CSV files. `intensity_num_shapes_results_random_1000.csv` corresponds to table 1, and `width_num_shapes_results_random_1000.csv` relates to our findings regarding the relationship of width and `P_u`.

examples/poisson/run_causal_tests.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,7 @@ class MyJsonUtility(JsonUtility):
150150
def add_modelling_assumptions(self, estimation_model: Estimator):
151151
# Add squared intensity term as a modelling assumption if intensity is the treatment of the test
152152
if "intensity" in estimation_model.treatment[0]:
153-
estimation_model.add_squared_term_to_df("intensity")
154153
estimation_model.intercept = 0
155-
if isinstance(estimation_model, WidthHeightEstimator):
156-
estimation_model.add_product_term_to_df("width", "intensity")
157-
estimation_model.add_product_term_to_df("height", "intensity")
158154

159155

160156
if __name__ == "__main__":

tests/json_front_tests/test_json_class.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,6 @@ def test_argparse(self):
6767

6868
def test_setup_modelling_scenario(self):
6969
self.json_class.setup()
70-
print(type(self.json_class.modelling_scenario))
71-
print(self.json_class.modelling_scenario)
7270
self.assertIsInstance(self.json_class.modelling_scenario, Scenario)
7371

7472
def test_setup_causal_specification(self):

tests/testing_tests/test_causal_test_engine.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,8 @@ def test_execute_test_observational_linear_regression_estimator_squared_term(sel
218218
self.minimal_adjustment_set,
219219
"C",
220220
self.causal_test_engine.scenario_execution_data_df,
221+
formula=f"C ~ A + {'+'.join(self.minimal_adjustment_set)} + (D ** 2)",
221222
)
222-
estimation_model.add_squared_term_to_df("D")
223223
causal_test_result = self.causal_test_engine.execute_test(estimation_model, self.causal_test_case)
224224
self.assertAlmostEqual(round(causal_test_result.test_value.value, 1), 4, delta=1)
225225

@@ -240,7 +240,7 @@ def test_execute_observational_causal_forest_estimator_cates(self):
240240
self.minimal_adjustment_set,
241241
"C",
242242
self.causal_test_engine.scenario_execution_data_df,
243-
effect_modifiers={Input("M", int): None},
243+
effect_modifiers={"M": None},
244244
)
245245
causal_test_result = self.causal_test_engine.execute_test(
246246
estimation_model, self.causal_test_case, estimate_type="cate"

0 commit comments

Comments
 (0)