Published: By: Per Spetz Reading time: 10 min

Why you should use Bayesian methods for A/B testing

Bayesian statistics has gone from being an academic bone of contention to a given in modern A/B testing, where market-leading tools from vendors such as Google, VWO and AB Tasty are based on Bayesian methods. But what is it, why is it such a talking point in A/B testing, and what benefits can it give you?

In this article I give an introduction to Bayesian A/B testing and share my three main reasons why I prefer it.

Part 1: What is “Bayesian statistics”?

Let’s start with what Bayesian statistics is about. It is perhaps easiest to describe in relation to the traditionally dominant paradigm in statistics, frequentism. Frequentist statistics is what most people recognise from school and what we most often come across in opinion polls and similar everyday contexts where statistics and probability appear. So how do the two perspectives differ?

An illustrative example

Here is the example I have come across that I think best describes the difference:

Imagine you find a coin on the street, and you want to find out whether the coin is evenly weighted, i.e. has a 50% probability of landing heads or tails. You flip the coin 5 times and get heads 5 times in a row. This feels unlikely, and you suspect there is something fishy about the coin and want to find out if that is the case.
Instead of flipping the coin thousands of times to prove that the coin is not evenly weighted, we can use a statistical model that, with a small measure of controlled uncertainty, can help us draw a conclusion faster. But in choosing a model, we can look at the problem in slightly different ways.

  • Scenario 1: You think 5 “heads” in a row is an unlikely result, but at the same time you know that almost all coins in the world are evenly weighted. What are the odds that this particular coin is some kind of weighted coin? You decide to flip the coin a few more times to be safe, and after another five heads you conclude that it must be a weighted coin.
  • Scenario 2: Now imagine that a baby finds the same coin and does the same test with the same 5 outcomes. As mentioned, the probability of this happening by chance is very small, and a newborn baby has no previous experience of coins or coin flipping, and therefore no knowledge that there is almost always a 50/50 probability of heads versus tails. In other words, the baby has no reason to draw any conclusion other than that this is a coin that is not evenly weighted.

Both ways of reasoning sound quite reasonable, don’t they? Neither of them is wrong either, and yet they ended up with two different decisions.

A simplified mathematical explanation

Frequentist statistics:
The baby starts from the frequentist perspective, where we note the outcome through frequent trials to answer the specific question:

If the coin is evenly weighted, what is the probability of getting such an extreme outcome by chance?

If the probability p for our measured outcome is small enough (often p ≤ 0.05), we reject the hypothesis that it is an evenly weighted coin. Somewhat simplified, we can say that in frequentist statistics we let the data from the experiment speak for itself, and we calculate the probability of what we see in the outcome.

Bayesian statistics:
In the Bayesian perspective, we include information about the probability from a previous state of knowledge, a so-called prior probability, and combine it with the data we collect during our experiment to make a statement about our updated state of knowledge, a so-called posterior probability. Our prior can be based on historical data from similar sequences of events, or something else we believe we know about the process in question. In this case, our prior is simply our general knowledge that the vast majority of coins are evenly weighted.

Using the formula that makes up Bayes’ theorem, we can calculate a conditional probability, roughly:

Given what we already know, what is the probability of the outcome we see?

Bayes’ theorem, “the probability of A, given B” (image credit: Wikipedia)

A fundamental difference is that Bayesian methods treat the parameter we are interested in as a random variable, where some values are more or less likely. Frequentists generally think this sounds insane, since there must be a real value for this parameter that is unquestionably true.

Take the example of the coin: surely, in the end, there must be a correct value for how the coin falls if it is flipped with exact millimetre precision thousands and thousands of times? Whether there is a true value can be argued, but a perhaps more relevant question is whether it actually matters.

My rule of thumb (formulated by someone else smart) is that Bayesian methods can be described as a framework for continuously updating our belief about the probability of a certain outcome with the help of data, while frequentist methods can be described as a framework for using data to rule out whether a certain outcome is different from another.

It sounds as if both perspectives work for analysing A/B tests, and that is of course true, but there are two small mathematical treats in Bayesian methods that give them additional value:

    • Since Bayesian methods calculate the probability that our test parameter takes different values, we can also express our “answer” as the probability that B > A. We cannot do this with frequentist methods.
    • A Bayesian model has a so-called loss function, which is similar to the probability function, but can instead be said to describe the difference between the result we have (our posterior) and the value if it turns out we choose the wrong variant.

    I understand if you still have not fallen off your chair from the wow factor, but it turns out that these small differences have valuable practical consequences.

    Part 2: Why do A/B testers need to relate to Bayesian statistics?

    The short explanation is that Bayesian methods have become standard in several commercial A/B testing tools, and that the market continues to move in that direction. The main reasons for this are:

    • It enables statistically significant results faster/with less data
      In the coin example above, the Bayesian perspective made us more sceptical of our results, but of course it works the other way too: if the outcome of our experiment is in line with previous experience (our prior), we can reach a statistically significant result with less data, and our experiment does not need to spend as long out in the wild. This can increase the pace of experimentation, but can also be an enabler for small samples or volatile data (though not the only way to test hypotheses with little or unruly data).
    • It gives the user a bit more slack
      The Bayesian hallmark of iteratively updating what we believe means that, in general, the demands on method and experiment design are not as high. One example is the problem of “peeking”, which can largely be avoided, and this reduction in complexity is particularly valuable in a self-service tool aimed at people who are not necessarily statisticians. (Again, there are also other non-Bayesian methods that can address these problems.)

    There are also a couple of classic drawbacks to Bayesian methods, but these are not usually major problems for digital A/B testing tools either:

    • It is arbitrary number juggling
      The most common objection is that Bayesian hypothesis testing somewhat arbitrarily throws in information that is not part of the experiment in order to draw conclusions. “Minimal priors” are exactly this: a prior that says all possible outcomes are exactly equally likely, and therefore adds no information, but via Bayes a posterior probability is still calculated that may differ from what the same experiment data would show with a frequentist method. However, A/B testing is rarely done on completely new and unknown outcomes, and we often have an idea of the result that can guide us to some extent, for example from similar tests or industry benchmarks. As soon as data starts coming in, we can immediately adjust and use our new data as the prior, thereby adjusting a minimal prior “straight away”, which is also a big difference with today’s automated data collection and algorithms compared with Bayes’ time, when this was done with pen and paper.
    • It is labour-intensive
      A practical drawback, which is also the reason most of us were not allowed to calculate with Bayesian methods at school, is that it is very labour-intensive to calculate a probability distribution using Bayes’ theorem, especially for continuous values. But this is not a major problem either with today’s computing power in the device you are reading this article on, not to mention the computing power that is a couple of clicks away in the cloud.

    You could say that Bayesian methods move complexity from the methods chapter to the calculation chapter, which makes them attractive for tools that are not necessarily aimed at statisticians or experienced analysts. The biggest advantage of frequentist methods, however, is that they have simply been the traditional go-to choice for hypothesis testing for a long time, and anyone who has worked with CRO knows how strong the default choice is just by virtue of being the default. Despite this, Bayesian methods have emerged as at least an equally recognised and obvious alternative in a relatively short time, and if I had to put everything on one card for the future of A/B testing, my money would be on Bayes.

    Part 3: If tools can do this for me, why do I need to care?

    When A/B testing tools are marketed, the statistical engine becomes one of the most important competitive advantages, since it dictates how and which results are presented, but also how fast and reliable the tool is. As a result, the statistical analysis often becomes a trade secret and something of a black box, and as an A/B tester you are simply forced to trust the conclusions the tool draws, something you should in my opinion avoid because:

    • They require flawless implementation in several steps
      The conditions for a correct statistical analysis require the tracking layer (the input data) and the metrics layer (the experiment configuration) to be implemented without errors, and they also require the new variant/code to be completely bug-free to give the tool the right conditions for the analysis. I think everyone who has run A/B tests can agree that this is rare, and that in the analysis step you often need to do additional hands-on work of various kinds to produce a credible analysis.
    • You get locked into a specific configuration
      Even if everything is implemented correctly, your needs may change during or after the experiment. Perhaps you realise that a particular segment of users is more interesting so you want to extend or shorten the test period, or that a different metric should really be evaluated, and so on. In general, this means you have to reconfigure the experiment from scratch if you want the automated analysis to be correct.
    • You need to be healthily suspicious
      As an A/B tester, validated learnings and recommendations are your reason for being. In the end you need to be able to judge which experiments are reliable, not least to understand when you can use diagnostics and analysis out of the box, and when you need to roll up your sleeves yourself to check that your recommendations hold.

    In my experience, successful A/B testing requires that you can, to some extent, take on diagnostics and analysis of experiments yourself, outside the framework of a specific tool. Combined with the fact that large parts of the commercial ecosystem are based on Bayesian methods, the consequence is that sooner or later, if you work with A/B testing, you will find yourself in a Bayesian context that requires manual work in the analysis. The choice of method affects the statistical language, which parameters are used and what the analysis actually says, which in turn affects how analysts present results and stakeholders’ expectations and understanding of the conclusions. It is hard enough to establish an understanding of one statistical school in an organisation, and a teaching nightmare to establish an understanding of two at the same time.

    In my opinion, you need to make a conscious and well-founded choice between using or not using Bayesian methods when you are going to stand on your own two feet in the analysis.

    Part 4: Why I prefer Bayesian methods

    At this point, most A/B testers (myself included) glance towards what they are most familiar with: safe and reliable frequentist statistics. But instead of being lured by the sirens’ song of p-values, t-tests and confidence intervals, you should, like Odysseus, lash yourself to the mast and stay the course towards Bayesian A/B testing. In addition to the statistical pros and cons we have already gone through, Bayesian methods have three advantages when it comes to A/B testing in practice.

    1. Share results with stakeholders in a clearer and more convincing way

    In my book, by far the biggest advantage of Bayesian analysis is that you formulate your conclusions in terms of probability rather than significance. This means you never again have to tangle yourself up in explanations of what a p-value is, what a 95 per cent confidence level means or what it means when a positive result is not significant. It is simply very awkward to communicate frequentist results to someone who does not have a statistical background, and it paves the way for misunderstandings.

    With Bayesian methods, the result of your analysis is expressed in terms of “the probability that B is better than A is x%”. No more explanation than that is needed to make a business decision, and in my experience it is infinitely easier for your stakeholders to understand. In the end, A/B testing is about creating and communicating decision support, and there, in my opinion, Bayesian statistics is superior.

    2. Reduce the risk of leaving winning variants on the drawing board

    A further upside of the Bayesian result being expressed in terms of probability is that you do not get locked into the division between significant and non-significant results. In the Bayesian world we talk about a sliding scale of probability between 0 and 100%. The business decision is usually whether or not to implement a new variant, and in that situation all variants in an A/B test with a probability of more than 50% can be said to be winners. Sure, you cannot be completely sure that you actually have a winning variant unless you reach 100% probability, but in many cases you have to choose one or the other.

    To apply a little probability theory, the law of large numbers will make you right more often in the long run, and you will have more experiments that can actually serve as decision support. Anyone who has been involved in digital product development will probably also recognise the desire to give a newly developed feature or product change the benefit of the doubt after the team has spent precious time on it, so the slightest support that it probably improves the user experience can be used for more data-driven decision-making even when we are not completely sure.

    An image that conveys the same amount of knowledge as my 20,000 words.
    (image credit: Jakub Linowski)

    3. Enables a more intuitive risk analysis

    Similar to the reasoning in the point above, A/B tests can often be about minimising the risk of a change in the product, i.e. ruling out a negative impact on our metrics. Imagine, for example, that in your Bayesian analysis of an A/B test you conclude that variant B, with 90% probability, performs better than A on a certain conversion rate. This of course means that there is also a 10% risk that A is actually better, and that we were just unlucky in our measurement.

    90% feels promising, but at the same time 10% is not a very unlikely scenario. Do we dare to take the chance anyway?

    Without going into the practical details, with our previously mentioned “loss function” we can estimate the average loss in conversion rate from not choosing variant B even though it was best in our measurement, and in that way get decision support that also describes the potential downside of each choice if we choose the wrong variant.

    (You can find a more detailed guide to what expected loss is and how to calculate it here.)

    Some made-up data to illustrate “expected loss”

    So we can see that B has an uplift of about 4.3% compared with A, but there is still a 10% risk that A is actually better. We calculate the expected loss from wrongly choosing B as 0.0024, which means the negative effect on the conversion rate would be 0.24% and give a “negative uplift” of only 0.4%.

    As part of our analysis, we can set thresholds for what we are comfortable with in terms of risk, for example that the probability of being best must exceed 90%, and that the expected loss must not give a negative uplift greater than 1%, and we can adjust risk levels depending on the type of KPI, e.g. paying customers vs new leads. In this way we can fairly easily identify experiments that risk too great a negative impact on important KPIs, while letting promising variants through even if they have not reached 100% probability of being best.

    Summary

    Bayesian methods are now a given in modern A/B testing and something that, in my opinion, you simply need to relate to as an A/B tester. My impression is that this has largely been driven by several heavyweight commercial A/B testing tools adopting it, but that it also offers several advantages for us ordinary mortals, especially for what A/B testing is ultimately about: getting an organisation to make the right decisions under uncertain conditions with the help of data.

    I hope this article contributed some new perspectives, or that you even learned something brand new! 🙂

    If you have input, questions or just want to talk more about A/B testing, add me on LinkedIn or send an email.