from cluster_experiments.cupac import *¶
CupacHandler
¶
CupacHandler class. It handles operations related to the cupac model.
Its main goal is to call the add_covariates method, where it will add the ouptut from the cupac model, and this should be used as covariates in the regression method for the hypothesis test.
Source code in cluster_experiments/cupac.py
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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 | |
_fit_cupac_model(pre_experiment_x, pre_experiment_y)
¶
Fits the cupac model. Caches the fitted model in the object, so we only fit it once. We can disable this by setting cache_fit to False.
Source code in cluster_experiments/cupac.py
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 | |
_predict_cupac_model(df_predict)
¶
Predicts the cupac model
Source code in cluster_experiments/cupac.py
238 239 240 241 242 243 244 | |
_prep_data_cupac(df, pre_experiment_df)
¶
Prepares data for training and prediction
Source code in cluster_experiments/cupac.py
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | |
add_covariates(df, pre_experiment_df=None)
¶
Train model to predict outcome variable (based on pre-experiment data) and add the prediction to the experiment dataframe. Only do this if we use cupac Args: pre_experiment_df: Dataframe with pre-experiment data. df: Dataframe with outcome and treatment variables.
Source code in cluster_experiments/cupac.py
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 | |
get_pre_experiment_y(pre_experiment_df)
¶
Returns the pre-experiment target variable, scaled if scale_col is provided.
Source code in cluster_experiments/cupac.py
163 164 165 166 167 168 169 | |
EmptyRegressor
¶
Bases: BaseEstimator
Empty regressor class. It does not do anything, used to glue the code of other estimators and PowerAnalysis
Each Regressor should have: - fit method: Uses pre experiment data to fit some kind of model to be used as a covariate and reduce variance. - predict method: Uses the fitted model to add the covariate on the experiment data.
It can add aggregates of the target in older data as a covariate, or a model (cupac) to predict the target.
Source code in cluster_experiments/cupac.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 | |
MLRateHandler
¶
MLRATE variance reducer (Guo et al., NeurIPS 2021).
Uses K-fold cross-fitting on experiment data to generate ML-predicted covariates without overfitting bias. Each unit's prediction comes from a model that never saw that unit during training.
When cluster_cols is provided, splits by cluster (GroupKFold) so no cluster appears in both train and validation of the same fold.
Source code in cluster_experiments/cupac.py
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 | |
TargetAggregation
¶
Bases: BaseEstimator
Adds average of target using pre-experiment data
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agg_col
|
str
|
Column to group by to aggregate target |
required |
target_col
|
str
|
Column to aggregate |
'target'
|
smoothing_factor
|
int
|
Smoothing factor for the smoothed mean |
20
|
Usage:
import pandas as pd
from cluster_experiments.cupac import TargetAggregation
df = pd.DataFrame({"agg_col": ["a", "a", "b", "b", "c", "c"], "target_col": [1, 2, 3, 4, 5, 6]})
new_df = pd.DataFrame({"agg_col": ["a", "a", "b", "b", "c", "c"]})
target_agg = TargetAggregation("agg_col", "target_col")
target_agg.fit(df.drop(columns="target_col"), df["target_col"])
df_with_target_agg = target_agg.predict(new_df)
print(df_with_target_agg)
Source code in cluster_experiments/cupac.py
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 | |
fit(X, y)
¶
Fits "target encoder" model to pre-experiment data
Source code in cluster_experiments/cupac.py
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | |
from_config(config)
classmethod
¶
Creates TargetAggregation from PowerConfig
Source code in cluster_experiments/cupac.py
125 126 127 128 129 130 131 132 | |
predict(X)
¶
Adds average target of pre-experiment data to experiment data
Source code in cluster_experiments/cupac.py
115 116 117 118 119 120 121 122 123 | |
build_ml_handler(cupac_model, ml_option='cupac', n_folds=5, target_col='target', features_cupac_model=None, scale_col=None, cluster_cols=None)
¶
Builds the covariate-injection handler shared by PowerAnalysis and NormalPowerAnalysis.
Source code in cluster_experiments/cupac.py
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 | |