From 1c1cd5be4b8f3cec3acbd196fca54e0ed4050cf8 Mon Sep 17 00:00:00 2001 From: mtanghu <54819091+mtanghu@users.noreply.github.com> Date: Tue, 1 Jun 2021 12:13:39 -0400 Subject: [PATCH 1/5] Correct default value of alphas The PopulationSummaryResults class should to default to self.alpha when the alpha parameter is not passed. This is not the case currently since some methods use the statement 'alpha = self.alpha if alpha is None else alpha', however alpha's default value in the methods is .1, thus self.alpha will be ignored even if the client does not pass the alpha parameter. Without the fix ate_interval(), marginal_ate_interval() and const_marginal_ate_interval() all ignore the alpha parameter. --- econml/inference/_inference.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/econml/inference/_inference.py b/econml/inference/_inference.py index ec9e823b3..72e83c3dc 100644 --- a/econml/inference/_inference.py +++ b/econml/inference/_inference.py @@ -1143,8 +1143,8 @@ class PopulationSummaryResults: """ - def __init__(self, pred, pred_stderr, mean_pred_stderr, d_t, d_y, alpha, value, decimals, tol, - output_names=None, treatment_names=None): + def __init__(self, pred, pred_stderr, mean_pred_stderr, d_t, d_y, value, decimals, tol, + alpha=.1, output_names=None, treatment_names=None): self.pred = pred self.pred_stderr = pred_stderr self.mean_pred_stderr = mean_pred_stderr @@ -1242,7 +1242,7 @@ def pvalue(self, *, value=0): pvalue = norm.sf(np.abs(self.zstat(value=value)), loc=0, scale=1) * 2 return pvalue - def conf_int_mean(self, *, alpha=.1): + def conf_int_mean(self, *, alpha=None): """ Get the confidence interval of the mean point estimate of each treatment on each outcome for sample X. @@ -1287,7 +1287,7 @@ def std_point(self): """ return np.std(self.pred, axis=0) - def percentile_point(self, *, alpha=.1): + def percentile_point(self, *, alpha=None): """ Get the confidence interval of the point estimate of each treatment on each outcome for sample X. @@ -1310,7 +1310,7 @@ def percentile_point(self, *, alpha=.1): upper_percentile_point = np.percentile(self.pred, (1 - alpha / 2) * 100, axis=0) return lower_percentile_point, upper_percentile_point - def conf_int_point(self, *, alpha=.1, tol=.001): + def conf_int_point(self, *, alpha=None, tol=.001): """ Get the confidence interval of the point estimate of each treatment on each outcome for sample X. From 890b8c8706a45c09978a07361b61e19355958a12 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 6 Jul 2021 12:45:49 -0700 Subject: [PATCH 2/5] Fix default values of PopulationSummaryResults --- econml/inference/_inference.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/econml/inference/_inference.py b/econml/inference/_inference.py index 491fffb37..9a441dbfd 100644 --- a/econml/inference/_inference.py +++ b/econml/inference/_inference.py @@ -1179,8 +1179,8 @@ class PopulationSummaryResults: """ - def __init__(self, pred, pred_stderr, mean_pred_stderr, d_t, d_y, value, decimals, tol, - alpha=.1, output_names=None, treatment_names=None): + def __init__(self, pred, pred_stderr, mean_pred_stderr, d_t, d_y, alpha=0.1, + value=0, decimals=3, tol=0.001, output_names=None, treatment_names=None): self.pred = pred self.pred_stderr = pred_stderr self.mean_pred_stderr = mean_pred_stderr @@ -1237,7 +1237,7 @@ def stderr_mean(self): raise AttributeError("Only point estimates are available!") return np.sqrt(np.mean(self.pred_stderr**2, axis=0)) - def zstat(self, *, value=0): + def zstat(self, *, value=None): """ Get the z statistic of the mean point estimate of each treatment on each outcome for sample X. @@ -1258,7 +1258,7 @@ def zstat(self, *, value=0): zstat = (self.mean_point - value) / self.stderr_mean return zstat - def pvalue(self, *, value=0): + def pvalue(self, *, value=None): """ Get the p value of the z test of each treatment on each outcome for sample X. @@ -1275,6 +1275,7 @@ def pvalue(self, *, value=0): the corresponding singleton dimensions in the output will be collapsed (e.g. if both are vectors, then the output of this method will be a scalar) """ + value = self.value if value is None else value pvalue = norm.sf(np.abs(self.zstat(value=value)), loc=0, scale=1) * 2 return pvalue @@ -1346,7 +1347,7 @@ def percentile_point(self, *, alpha=None): upper_percentile_point = np.percentile(self.pred, (1 - alpha / 2) * 100, axis=0) return lower_percentile_point, upper_percentile_point - def conf_int_point(self, *, alpha=None, tol=.001): + def conf_int_point(self, *, alpha=None, tol=None): """ Get the confidence interval of the point estimate of each treatment on each outcome for sample X. @@ -1389,7 +1390,7 @@ def stderr_point(self): """ return np.sqrt(self.stderr_mean**2 + self.std_point**2) - def summary(self, alpha=0.1, value=0, decimals=3, tol=0.001, output_names=None, treatment_names=None): + def summary(self, alpha=None, value=None, decimals=None, tol=None, output_names=None, treatment_names=None): """ Output the summary inferences above. From 7702c1a7230b7055c32edfbbf7b531990966293c Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 6 Jul 2021 16:01:49 -0700 Subject: [PATCH 3/5] Add test to ensure inference alpha is passed --- econml/tests/test_inference.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/econml/tests/test_inference.py b/econml/tests/test_inference.py index 6aebd7412..398aa81a3 100644 --- a/econml/tests/test_inference.py +++ b/econml/tests/test_inference.py @@ -287,6 +287,18 @@ def test_can_summarize(self): TestInference.W, inference=BootstrapInference(5) ).summary(1) + + def test_alpha(self): + Y, T, X, W = TestInference.Y, TestInference.T, TestInference.X, TestInference.W + est = LinearDML(model_y=LinearRegression(), model_t=LinearRegression()) + est.fit(Y, T, X=X, W=W) + + # ensure alpha is passed + lb, ub = est.const_marginal_ate_interval(X, alpha=1) + assert (lb == ub).all() + + lb, ub = est.const_marginal_ate_interval(X) + assert (lb != ub).all() def test_inference_with_none_stderr(self): Y, T, X, W = TestInference.Y, TestInference.T, TestInference.X, TestInference.W From eb70e898555a84e042e8beebae4cabe2d2f9599e Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 6 Jul 2021 16:02:35 -0700 Subject: [PATCH 4/5] Fix spelling --- econml/inference/_inference.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/econml/inference/_inference.py b/econml/inference/_inference.py index 9a441dbfd..1fff3e906 100644 --- a/econml/inference/_inference.py +++ b/econml/inference/_inference.py @@ -1243,7 +1243,7 @@ def zstat(self, *, value=None): Parameters ---------- - value: optinal float (default=0) + value: optional float (default=0) The mean value of the metric you'd like to test under null hypothesis. Returns @@ -1264,7 +1264,7 @@ def pvalue(self, *, value=None): Parameters ---------- - value: optinal float (default=0) + value: optional float (default=0) The mean value of the metric you'd like to test under null hypothesis. Returns From 259e5d56a1cbf3d12919fe4db722c9f2557bdf81 Mon Sep 17 00:00:00 2001 From: Keith Battocchi Date: Fri, 9 Jul 2021 14:47:18 -0400 Subject: [PATCH 5/5] Update test_inference.py Removing whitespace to fix linting --- econml/tests/test_inference.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/econml/tests/test_inference.py b/econml/tests/test_inference.py index 398aa81a3..d977ec1c1 100644 --- a/econml/tests/test_inference.py +++ b/econml/tests/test_inference.py @@ -287,7 +287,7 @@ def test_can_summarize(self): TestInference.W, inference=BootstrapInference(5) ).summary(1) - + def test_alpha(self): Y, T, X, W = TestInference.Y, TestInference.T, TestInference.X, TestInference.W est = LinearDML(model_y=LinearRegression(), model_t=LinearRegression())