-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcausal_effect.py
More file actions
166 lines (129 loc) · 7.33 KB
/
Copy pathcausal_effect.py
File metadata and controls
166 lines (129 loc) · 7.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# pylint: disable=too-few-public-methods
"""This module contains the CausalEffect abstract class, as well as the concrete extension classes:
ExactValue, Positive, Negative, SomeEffect, NoEffect"""
from abc import ABC, abstractmethod
import numpy as np
from causal_testing.estimation.effect_estimate import EffectEstimate
class CausalEffect(ABC):
"""An abstract class representing an expected causal effect."""
def __init__(self, effect_type: str = "direct"):
self.effect_type = effect_type
@abstractmethod
def apply(self, effect_estimate: EffectEstimate) -> bool:
"""Abstract apply method that should return a bool representing if the result meets the outcome
:param effect_estimate: EffectEstimate to be checked
:return: Bool that is true if outcome is met
"""
def __str__(self) -> str:
return type(self).__name__
def to_dict(self):
"""
Convert the expected effect to a python dictionary for easy serialisation as JSON.
:returns: A JSON serialisable dict representing the expected effect.
"""
return {"name": self.__class__.__name__, "effect_type": self.effect_type}
class SomeEffect(CausalEffect):
"""An extension of CausalEffect representing that the expected causal effect should not be zero."""
def apply(self, effect_estimate: EffectEstimate) -> bool:
if effect_estimate.type in ("risk_ratio", "hazard_ratio", "unit_odds_ratio", "odds_ratio"):
value_to_check = 1
elif effect_estimate.type in ("coefficient", "ate"):
value_to_check = 0
else:
raise ValueError(f"Test Value type {effect_estimate.type} is not valid for this CausalEffect")
return (~((effect_estimate.ci_low <= value_to_check) & (value_to_check <= effect_estimate.ci_high))).any()
class NoEffect(CausalEffect):
"""
An extension of CausalEffect representing that the expected causal effect should be zero.
:param atol: Arithmetic tolerance. The test will pass if the absolute value of the causal effect is less than
atol.
:param ctol: Categorical tolerance. The test will pass if this proportion of categories pass.
"""
def __init__(self, effect_type: str = "direct", atol: float = 0, ctol: float = 0.0):
super().__init__(effect_type=effect_type)
self.atol = atol
self.ctol = ctol
def apply(self, effect_estimate: EffectEstimate) -> bool:
if effect_estimate.type in ("risk_ratio", "hazard_ratio", "unit_odds_ratio", "odds_ratio"):
value_to_check = 1
elif effect_estimate.type in ("coefficient", "ate"):
value_to_check = 0
else:
raise ValueError(f"Test Value type {effect_estimate.type} is not valid for this CausalEffect")
return sum(
((effect_estimate.ci_low <= value_to_check) & (value_to_check <= effect_estimate.ci_high))
| (np.isclose(effect_estimate.value, value_to_check, atol=self.atol))
) / len(effect_estimate.value) >= (1 - self.ctol)
def to_dict(self):
"""
Convert the expected effect to a python dictionary for easy serialisation as JSON.
:returns: A JSON serialisable dict representing the expected effect.
"""
return super().to_dict() | {"atol": self.atol, "ctol": self.ctol}
class ExactValue(CausalEffect):
"""An extension of CausalEffect representing that the expected causal effect should be a specific value."""
def __init__(
self, value: float, effect_type: str = "direct", atol: float = 0, ci_low: float = None, ci_high: float = None
):
super().__init__(effect_type=effect_type)
if (ci_low is not None) ^ (ci_high is not None):
raise ValueError("If specifying confidence intervals, must specify `ci_low` and `ci_high` parameters.")
if atol is not None and atol < 0:
raise ValueError("Tolerance must be an absolute (positive) value.")
self.value = value
self.ci_low = ci_low
self.ci_high = ci_high
self.atol = atol
if self.ci_low is not None and self.ci_high is not None:
if not self.ci_low <= self.value <= self.ci_high:
raise ValueError("Specified value falls outside the specified confidence intervals.")
if self.value - self.atol < self.ci_low or self.value + self.atol > self.ci_high:
raise ValueError(
"Arithmetic tolerance falls outside the confidence intervals."
f"Try specifying wider intervals or a value of atol smaller than the current vlaue {self.atol}."
)
def apply(self, effect_estimate: EffectEstimate) -> bool:
close = np.isclose(effect_estimate.value, self.value, atol=self.atol)
if effect_estimate.ci_valid and self.ci_low is not None and self.ci_high is not None:
return (
close.all()
and (self.ci_low <= effect_estimate.ci_low).all()
and (self.ci_high >= effect_estimate.ci_high).all()
)
return close.all()
def __str__(self):
return f"ExactValue: {self.value}±{self.atol}"
def to_dict(self):
"""
Convert the expected effect to a python dictionary for easy serialisation as JSON or CSV.
:returns: A JSON serialisable dict representing the expected effect.
"""
effect = {"value": self.value, "atol": self.atol}
if self.ci_low:
effect["ci_low"] = self.ci_low
if self.ci_low:
effect["ci_high"] = self.ci_high
return super().to_dict() | effect
class Positive(SomeEffect):
"""An extension of CausalEffect representing that the expected causal effect should be positive.
Currently only single values are supported for the test value"""
def apply(self, effect_estimate: EffectEstimate) -> bool:
if len(effect_estimate.value) > 1:
raise ValueError("Positive Effects are currently only supported on single float datatypes")
if effect_estimate.type in {"ate", "coefficient"}:
return any(0 < ci_low < ci_high for ci_low, ci_high in zip(effect_estimate.ci_low, effect_estimate.ci_high))
if effect_estimate.type in ["risk_ratio", "unit_odds_ratio"]:
return any(1 < ci_low < ci_high for ci_low, ci_high in zip(effect_estimate.ci_low, effect_estimate.ci_high))
raise ValueError(f"Test Value type {effect_estimate.type} is not valid for this CausalEffect")
class Negative(SomeEffect):
"""An extension of CausalEffect representing that the expected causal effect should be negative.
Currently only single values are supported for the test value"""
def apply(self, effect_estimate: EffectEstimate) -> bool:
if len(effect_estimate.value) > 1:
raise ValueError("Negative Effects are currently only supported on single float datatypes")
if effect_estimate.type in {"ate", "coefficient"}:
return any(ci_low < ci_high < 0 for ci_low, ci_high in zip(effect_estimate.ci_low, effect_estimate.ci_high))
if effect_estimate.type in ["risk_ratio", "unit_odds_ratio"]:
return any(ci_low < ci_high < 1 for ci_low, ci_high in zip(effect_estimate.ci_low, effect_estimate.ci_high))
# Dead code but necessary for pylint
raise ValueError(f"Test Value type {effect_estimate.type} is not valid for this CausalEffect")