Announcing a new project I just completed, a Bayesian probability calculator! This is a personal project I began in 2018 and have recently revisited, improved, and finished up. I took an interest in Bayesian probability (learn about Bayes’ theorem here at statisticshowto.com) some years ago when I recognized how powerful of a tool it is to, basically, help calculate how likely things are to be true (based on the information that you have available). It’s all about taking your prior belief (or the “prior probability” as some value between 0% and 100%) about whether some idea or hypothesis is true, and when you come across some new evidence relating to it, revising the belief by properly factoring in the evidence through considering how expected it was assuming the hypothesis is true and how expected it was assuming the hypothesis isn’t true. It’s not too different from how people might generally take new information into account for informing their beliefs, but Bayes’ theorem frames the question precisely, reminding us to pay attention to false positives and our prior beliefs. So by applying Bayes’ theorem, when we make some new observation, we can consider how expected that observation would be if our prior belief was true and how expected it would be if it wasn’t true, and we can consider that given what we previously thought the chance was for it to be true.

The equation itself looks like *P(H|E) = [P(H) × P(E|H)] / [P(H) × P(E|H) + P(¬H) × P(E|¬H)]*, which means that the probability that a belief is true given new evidence (*P(H|E)*) can be calculated by multiplying the prior probability for the hypothesis by the expectation for the evidence given the hypothesis (*P(H) × P(E|H)*), divided by the total expectation for that observation, which is itself the prior probability times the likelihood for the evidence given the hypothesis *plus* the prior belief that the hypothesis was *wrong* times the expectation for the evidence given that the hypothesis was wrong (*P(H) × P(E|H) + P(¬H) × P(E|¬H)*). For a great and intuitive explanation of exactly why this works, I recommend this wonderful video by 3Blue1Brown on YouTube.

To give a simple example of Bayes’ theorem in action, let’s say you’re wondering whether your new friend shares your passion for geology (but you don’t want to, you know, actually ask). “My friend loves geology” may be your hypothesis. And let’s say you initially don’t think it’s very likely to be the case; maybe you think only 20% of people like geology as much as you. So 20% is your prior probability. But then you hear your friend make a geology pun! (“You know, it’s important not to take all igneous rocks for granite!”) Hearing that, you’ll probably automatically think it’s more likely that they do love geology, but how much more likely? In comes the handy Bayesian reasoning, allowing you to take into account the proportion of geology fans who would make a pun like that and the proportion of non-fans who would, in the context of how many people are or aren’t geology fans in the first place. Simply multiply the percent of geology fans (20%) by the percent of geology fans who would make such a pun, which maybe you’ll estimate that at about 80% (since they love their puns!), giving you 16%. Then multiply the percent of non-fans (80%) by the percent of non-fans who would make the pun, which maybe you’ll estimate that at about 10%, giving you 8%. That original 20% vs 80% population split now is whittled down by those who tell puns to become 16% (geology fans) vs 8% (non-fans). With that, you can intuitively recognize now that it’s twice as likely that your friend is a geology fan than that they aren’t. Putting the numbers into the final form of the equation, you can see that the probability is 0.16/(0.16+0.8), which, based on those assumptions, equals a 66.7% chance that your friend loves geology as much as you do.

Bayes’ theorem can be useful when intuitively applied, but I wanted to make a tool that would allow me to quickly and easily calculate exact probabilities. After all, plugging and chugging the numbers into an exact equation like *P(H|E) = [P(H) × P(E|H)] / [P(H) × P(E|H) + P(¬H) × P(E|¬H)]* can be cumbersome. When searching, I found that there were some other Bayesian calculators online, but I felt that they were limited in how much input could be made, or how intuitive they were to use, or how fast they worked. So I set out to create a tool where I could easily calculate probabilities and add multiple pieces of evidence and tweak values, with instant results at every change. Hence, my *Bayesian Calculator for Updating Probability with Multiple Conditionally Independent Variables*.

Link to this entry on my portfolio:

I originally designed the calculator to apply the basic arithmetic of Bayes’ theorem with the standard JavaScript operators, where additional pieces of evidence could be added with ease, where the operation would be able to simply repeat itself, taking the posterior probability from the results of the first evidence to become the new prior for the next, which is perfectly valid, as long as each piece of evidence is distinct. Since it operated on client-side JavaScript, all the math could happen instantly, without having to wait for the inputs to be processed by a web server. I packaged it in an HTML5 webpage with CSS3 styling to have a responsive design that works well across platforms, and I added explanatory text and instructions for anyone else who might want to use the calculator. As I continued to experiment with it and develop it and work on debugging and find any areas that may lead to errors and require custom alert messages, I also observed that there could be certain (albeit pretty rare) inputs that have too many decimal places for JavaScript to properly handle, so I then incorporated the Big.js JavaScript library to perform the math and work with larger numbers of decimal places. Most recently, I added something called the Bayes’ factor to the outputted results, which rather than telling you a final probability which is dependent on your own prior probability input, the Bayes’ factor also tells you how significant the evidence itself is.

When I was satisfied that the calculator was working well and could be a fun and useful tool for others, I finally set it up to be published and accessible online. Check out the Bayesian calculator here!