compute_metrics <- function(win_vector, lose_vector) {
  res <- list()

  win_proportion <- mean(win_vector)
  lose_proportion <- mean(lose_vector)

  res$win_proportion$value <- win_proportion

  res$lose_proportion$value <- lose_proportion

  res$win_ratio$value <- win_proportion / lose_proportion

  res$net_benefit$value <- win_proportion - lose_proportion

  # Compute Standard Deviations and Confidence Intervals --------------------

  std_win_proportion <- stats::sd(win_vector)
  std_lose_proportion <- stats::sd(lose_vector)

  res$win_proportion$std <- std_win_proportion
  res$lose_proportion$std <- std_lose_proportion


  win_ratio_CI_res <- win_ratio_CI(win_vector,lose_vector)
  res$win_ratio$lower_CI <- win_ratio_CI_res$lower
  res$win_ratio$upper_CI <- win_ratio_CI_res$upper

  net_benefit_CI_res <- net_benefit_CI(win_vector,lose_vector)
  res$net_benefit$lower_CI <- net_benefit_CI_res$lower
  res$net_benefit$upper_CI <- net_benefit_CI_res$upper

  return(res)
}

win_ratio_CI <- function(win_vector, lose_vector) {
  n <- length(win_vector)

  win_mean <- mean(win_vector)
  lose_mean <- mean(lose_vector)

  win_ratio <- win_mean / lose_mean

  win_var <- stats::var(win_vector)
  lose_var <- stats::var(lose_vector)


  win_lose_cov <- stats::cov(win_vector, lose_vector)

  # Variance of  log(win_ratio)
  var_log_win_ratio <- win_var / (n * win_mean^2) + lose_var / (n * lose_mean^2) - 2 * win_lose_cov / (n * win_mean * lose_mean)

  se_log_win_ratio <- sqrt(var_log_win_ratio)

  # CI of log(win_ratio)

  z <- 1.96 # 95% CI
  log_lower <- log(win_ratio) - z * se_log_win_ratio
  log_upper <- log(win_ratio) + z * se_log_win_ratio

  # Exponentiate to get CI of win_ratio

  res <- list()

  res$lower <- exp(log_lower)
  res$upper <- exp(log_upper)

  return(res)
}

net_benefit_CI <- function(win_vector, lose_vector){

  differences <- win_vector - lose_vector

  net_benefit <- mean(differences)
  net_benefit_std <- stats::sd(differences)

  n <- length(differences)

  std <- net_benefit_std / sqrt(n)

  z <- 1.96 # 95% CI

  res <- list()

  res$lower <- net_benefit - z * std
  res$upper <- net_benefit + z * std

  return(res)
}
