R/bayesian_simulator.R

Defines functions bayesian_simulation

Documented in bayesian_simulation

#' Bayesian A/B Test Performance using Monte-Carlo Simulation
#'
#'This program will perform a monte-carlo simulation over the posterior distrubtions of an A/B test and calculate relative
#'performance measures.
#'
#' @param x a data frame from the bayesian_test output including posterior alpha and beta values for the A/B cells
#' @param trials an integer value representing the number of trails for the monte-carlo simulation
#' @param a_is_champion logical, indicating is A is the champion cell.
#' @return An output string identifying the outperformance probability from monte carlo simulations and the relative
#' outperformance of A over B. Plots generated are the relative performance histogram and a cumulative density function of the results.
#' @author Dean Marchiori
#' @details
#' This function accepts the output of the bayesian_test function. Relative performance is measured by performing monte-carlo simulations of both distributions.
#' @import ggplot2
#' @export
#'

###  Bayesian A/B Testing  ###
bayesian_simulation <- function(x = bayesian_test_output, trials = 10000, a_is_champion = TRUE) {


  #==================== Monte Carlo Simulation ========================================#

  a.sim <- rbeta(n = trials, shape1 = x[1,1], shape2 = x[2,1])
  b.sim <- rbeta(n = trials, shape1 = x[3,1], shape2 = x[4,1])

  #==================== Outputs========================================#

  if (a_is_champion == TRUE) {

    outperf.prob <- sum(a.sim > b.sim)/trials
    outperf.ratio <- data.frame(outperformance = a.sim/b.sim)
    print(paste("Outperformance probability (A vs B): ",outperf.prob))
    print(paste("A vs B relative performance ratio: ", round(mean(outperf.ratio$outperformance),2)))

  } else {

    outperf.prob <- sum(b.sim > a.sim)/trials
    outperf.ratio <- data.frame(outperformance = b.sim/a.sim)
    print(paste("Outperformance probability (B over A) ",outperf.prob))
    print(paste("B vs A relative performance ratio: ", round(mean(outperf.ratio$outperformance),2)))

  }

  #==================== Plots ========================================#

  # Plotting historgram of simulated outperformance ratios
  p1 <- ggplot(data = outperf.ratio,
               aes(outperformance)
  ) +
    geom_histogram(fill = "#007DBA",
                   alpha = 0.6
    ) +
    geom_vline(xintercept = 1,
               colour = "Dark Grey",
               lty = "dashed",
               lwd = 0.8
    ) +
    geom_vline(xintercept = mean(outperf.ratio$outperformance),
               colour = "Red"
    ) +
    theme_light(base_size = 16) +
    labs(title = "Outperformance Ratio (A vs B))",
         x = "Outperformance Ratio"
    )

  # Plotting CDF of outperformance ratios
  p2 <- plot.ecdf(outperf.ratio$outperformance,
                  main = "Cumulative Distribution Function of Outperformance Ratio",
                  ylab = "Cumulative Probability",
                  xlab = "Outperformance Ratio",
                  col = "Dark Blue")
  abline(v = 1,col = "Dark Grey", lty = "dashed", lwd = 0.8)

 return(list(p1,p2))

}
deanmarchiori/UpliftTestR documentation built on May 12, 2017, 9:53 p.m.