Last active
November 20, 2025 18:07
-
-
Save lukasvermeer/6c967113ce5f007485a6cd9d4e03c790 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| sample_size <- 100000 | |
| base_rate <- 0.2 | |
| base_rpv <- 100 | |
| sd_rpv <- 40 | |
| lift <- c(0.05, 0.1) | |
| # given treatment assigments for a user returns revenue | |
| outcome <- function(u) { | |
| # if not converted, or revenue <0 return 0 | |
| if (rbinom(n=1, size=1, prob=base_rate) == 0 | u['control_rpv'] <= 0) { | |
| return(0) | |
| } | |
| p = u['control_rpv'] | |
| # if user is in variant of test 1, apply lift 1 | |
| if (u['exp_1']) { p = p * (1+lift[1]) } | |
| # if user is in variant of test 2, apply lift 2 | |
| if (u['exp_2']) { p = p * (1+lift[2]) } | |
| # if user is in variant of test 1 and test 2, apply interaction effect | |
| if (u['exp_1'] && u['exp_2']) { p = p * (1+u['interaction_effect']) } | |
| # return revenue for this user | |
| return(p) | |
| } | |
| run <- function(i) { | |
| # construct data frame with treatment assignments and control revenue for all users in both tests | |
| df = data.frame(exp_1 = rbinom(n=sample_size, size=1, prob=0.5), | |
| exp_2 = rbinom(n=sample_size, size=1, prob=0.5), | |
| control_rpv = rnorm(n=sample_size, mean=base_rpv, sd=sd_rpv), | |
| interaction_effect = i) | |
| # add column for revenue outcome of each user | |
| df['revenue'] = apply(df, 1, outcome) | |
| # run regression with interaction term | |
| s <- summary(lm(revenue ~ exp_1*exp_2, data = df)) | |
| return(s$coefficients['exp_1:exp_2',4]) | |
| } | |
| # one run | |
| run(0.1) | |
| # lots of runs | |
| # r <- sapply(rep(.1,1000), run) | |
| # hist(r, breaks=100) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment