from cluster_experiments.washover import *
¶
ConstantWashover
¶
Bases: Washover
Constant washover - we drop all rows in the washover period when there is a switch where the treatment is different.
Source code in cluster_experiments/washover.py
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 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 |
|
washover(df, truncated_time_col, treatment_col, cluster_cols, original_time_col=None)
¶
Constant washover - we drop all rows in the washover period when there is a switch where the treatment is different.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df
|
DataFrame
|
Input dataframe. |
required |
truncated_time_col
|
str
|
Name of the truncated time column. |
required |
treatment_col
|
str
|
Name of the treatment column. |
required |
cluster_cols
|
List[str]
|
List of clusters of experiment. |
required |
original_time_col
|
Optional[str]
|
Name of the original time column. |
None
|
Returns:
Type | Description |
---|---|
DataFrame
|
pd.DataFrame: Same dataframe as input without the rows in the washover period. |
Usage:
import numpy as np
import pandas as pd
from datetime import datetime, timedelta
from cluster_experiments import ConstantWashover
np.random.seed(42)
num_rows = 10
def random_timestamp(start_time, end_time):
time_delta = end_time - start_time
random_seconds = np.random.randint(0, time_delta.total_seconds())
return start_time + timedelta(seconds=random_seconds)
def generate_data(start_time, end_time, treatment):
data = {
'order_id': np.random.randint(10**9, 10**10, size=num_rows),
'city_code': 'VAL',
'activation_time_local': [random_timestamp(start_time, end_time) for _ in range(num_rows)],
'bin_start_time_local': start_time,
'treatment': treatment
}
return pd.DataFrame(data)
start_times = [datetime(2024, 1, 22, 9, 0), datetime(2024, 1, 22, 11, 0),
datetime(2024, 1, 22, 13, 0), datetime(2024, 1, 22, 15, 0)]
treatments = ['control', 'variation', 'variation', 'control']
dataframes = [generate_data(start, start + timedelta(hours=2), treatment) for start, treatment in zip(start_times, treatments)]
df = pd.concat(dataframes).sort_values(by='activation_time_local').reset_index(drop=True)
## Define washover with 30 min duration
washover = ConstantWashover(washover_time_delta=timedelta(minutes=30))
## Apply washover to the dataframe, the orders with activation time within the first 30 minutes after every change in the treatment column, clustering by city and 2h time bin, will be dropped
df_analysis_washover = washover.washover(
df=df,
truncated_time_col='bin_start_time_local',
treatment_col='treatment',
cluster_cols=['city_code','bin_start_time_local'],
original_time_col='activation_time_local',
)
Source code in cluster_experiments/washover.py
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 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 |
|
EmptyWashover
¶
Bases: Washover
No washover - assumes no spill-over effects from one treatment to another.
Source code in cluster_experiments/washover.py
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 |
|
washover(df, truncated_time_col, treatment_col, cluster_cols, original_time_col=None)
¶
No washover - returns the same dataframe as input.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df
|
DataFrame
|
Input dataframe. |
required |
truncated_time_col
|
str
|
Name of the truncated time column. |
required |
treatment_col
|
str
|
Name of the treatment column. |
required |
cluster_cols
|
List[str]
|
List of clusters of experiment. |
required |
original_time_col
|
Optional[str]
|
Name of the original time column. |
None
|
Returns:
Type | Description |
---|---|
DataFrame
|
pd.DataFrame: Same dataframe as input. |
Usage:
from cluster_experiments import SwitchbackSplitter
from cluster_experiments import EmptyWashover
washover = EmptyWashover()
n = 10
df = pd.DataFrame(
{
# Random time each minute in 2022-01-01, length 10
"time": pd.date_range("2022-01-01", "2022-01-02", freq="1min")[
np.random.randint(24 * 60, size=n)
],
"city": random.choices(["TGN", "NYC", "LON", "REU"], k=n),
}
)
splitter = SwitchbackSplitter(
washover=washover,
time_col="time",
cluster_cols=["city", "time"],
treatment_col="treatment",
switch_frequency="30T",
)
out_df = splitter.assign_treatment_df(df=washover_split_df)
Source code in cluster_experiments/washover.py
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 |
|
Washover
¶
Bases: ABC
Abstract class to model washovers in the switchback splitter.
Source code in cluster_experiments/washover.py
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 |
|
washover(df, truncated_time_col, treatment_col, cluster_cols, original_time_col=None)
abstractmethod
¶
Abstract method to add washvover to the dataframe.
Source code in cluster_experiments/washover.py
42 43 44 45 46 47 48 49 50 51 |
|