Skip to content

Instantly share code, notes, and snippets.

@johnmyleswhite
Created October 7, 2017 12:04
Show Gist options
  • Save johnmyleswhite/b25cd84ab8db7542cddeebdb813bcc55 to your computer and use it in GitHub Desktop.
Save johnmyleswhite/b25cd84ab8db7542cddeebdb813bcc55 to your computer and use it in GitHub Desktop.
Ratios in causal inference
library("ggplot2")
library("dplyr")
# Population size
n <- 2500
# Sessions per user if assigned to test
sessions_test <- as.integer(exp(rnorm(n, 0.5, 1)))
# Sessions per user if assigned to control
sessions_control <- as.integer(exp(rnorm(n, 0.0, 1)))
# Has sessions variables are transformations of these.
has_hessions_test <- as.integer(sessions_test > 0)
has_hessions_control <- as.integer(sessions_control > 0)
# Given assignment, estimate the estimand in a naive way
analyze <- function(
sessions_test,
sessions_control,
assignments
) {
ratio_test <- (
(
sum(sessions_test * assignments)
)
/
(
sum(sessions_test * assignments > 0)
)
)
ratio_control <- (
(
sum(sessions_control * (1 - assignments))
)
/
(
sum(sessions_control * (1 - assignments) > 0)
)
)
estimator <- ratio_test / ratio_control
return(estimator)
}
monte_carlo <- function(
sessions_test,
sessions_control,
n_sims=10000L
) {
estimators <- rep(NA, n_sims)
for (s in 1:n_sims) {
assignments <- rbinom(n, 1, 0.5)
estimators[s] <- analyze(
sessions_test,
sessions_control,
assignments
)
}
return(estimators)
}
estimand <- (
(
sum(sessions_test) / sum(has_hessions_test)
)
/
(
sum(sessions_control) / sum(has_hessions_control)
)
)
estimators <- monte_carlo(sessions_test, sessions_control)
hist(estimators - estimand)
mean(estimators)
estimand
@johnmyleswhite
Copy link
Author

johnmyleswhite commented Oct 7, 2017

> mean(estimators)
[1] 1.350879
>
> estimand
[1] 1.350425

@johnmyleswhite
Copy link
Author

dean

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment