-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpower_config.py
138 lines (117 loc) · 4.18 KB
/
power_config.py
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
import datetime
from dataclasses import dataclass
from typing import List, Optional
from cluster_experiments.cupac import EmptyRegressor, TargetAggregation
from cluster_experiments.experiment_analysis import (
ClusteredOLSAnalysis,
GeeExperimentAnalysis,
OLSAnalysis,
PairedTTestClusteredAnalysis,
TTestClusteredAnalysis,
)
from cluster_experiments.perturbator import BinaryPerturbator, UniformPerturbator
from cluster_experiments.random_splitter import (
BalancedClusteredSplitter,
BalancedSwitchbackSplitter,
ClusteredSplitter,
NonClusteredSplitter,
StratifiedClusteredSplitter,
StratifiedSwitchbackSplitter,
SwitchbackSplitter,
)
@dataclass
class PowerConfig:
"""
Dataclass to create a power analysis from.
Arguments:
splitter: Splitter object to use
perturbator: Perturbator object to use
analysis: ExperimentAnalysis object to use
cupac_model: CUPAC model to use
n_simulations: number of simulations to run
cluster_cols: list of columns to use as clusters
target_col: column to use as target
treatment_col: column to use as treatment
treatment: what value of treatment_col should be considered as treatment
control: what value of treatment_col should be considered as control
strata_cols: columns to stratify with
splitter_weights: weights to use for the splitter, should have the same length as treatments, each weight should correspond to an element in treatments
switch_frequency: how often to switch treatments
time_col: column to use as time in switchback splitter
covariates: list of columns to use as covariates
average_effect: average effect to use in the perturbator
treatments: list of treatments to use
alpha: alpha value to use in the power analysis
agg_col: column to use for aggregation in the CUPAC model
smoothing_factor: smoothing value to use in the CUPAC model
features_cupac_model: list of features to use in the CUPAC model
Usage:
```python
from cluster_experiments.power_config import PowerConfig
from cluster_experiments.power_analysis import PowerAnalysis
p = PowerConfig(
analysis="gee",
splitter="clustered_balance",
perturbator="uniform",
cluster_cols=["city"],
n_simulations=100,
alpha=0.05,
)
power_analysis = PowerAnalysis.from_config(p)
```
"""
# mappings
perturbator: str
splitter: str
analysis: str
washover: str = ""
# Needed
cluster_cols: Optional[List[str]] = None
# optional mappings
cupac_model: str = ""
comparison_col: str = ""
# Shared
target_col: str = "target"
treatment_col: str = "treatment"
treatment: str = "B"
# Perturbator
average_effect: Optional[float] = None
# Splitter
treatments: Optional[List[str]] = None
strata_cols: Optional[List[str]] = None
splitter_weights: Optional[List[float]] = None
switch_frequency: Optional[str] = None
# Switchback
time_col: Optional[str] = None
washover_time_delta: Optional[datetime.timedelta] = None
# Analysis
covariates: Optional[List[str]] = None
# Power analysis
n_simulations: int = 100
alpha: float = 0.05
control: str = "A"
# Cupac
agg_col: str = ""
smoothing_factor: float = 20
features_cupac_model: Optional[List[str]] = None
perturbator_mapping = {
"binary": BinaryPerturbator,
"uniform": UniformPerturbator,
}
splitter_mapping = {
"clustered": ClusteredSplitter,
"clustered_balance": BalancedClusteredSplitter,
"non_clustered": NonClusteredSplitter,
"clustered_stratified": StratifiedClusteredSplitter,
"switchback": SwitchbackSplitter,
"switchback_balance": BalancedSwitchbackSplitter,
"switchback_stratified": StratifiedSwitchbackSplitter,
}
analysis_mapping = {
"gee": GeeExperimentAnalysis,
"ols_non_clustered": OLSAnalysis,
"ols_clustered": ClusteredOLSAnalysis,
"ttest_clustered": TTestClusteredAnalysis,
"paired_ttest_clustered": PairedTTestClusteredAnalysis,
}
cupac_model_mapping = {"": EmptyRegressor, "mean_cupac_model": TargetAggregation}