Maths in computer science: Bayes' Theorem
Tags: beginners, tutorial, learning
Published: 2024-01-21 21:01:25

Bayes’ Theorem 
Bayes’ theorem is a theorem that is useful when the conditional probability of the thing we are interested in is hard or impossible to estimate but its converse is available. This sounds complicated but I will try an explain this through the course of this article. 
First off I think It would be useful to know what Bayes’ theorem has to do with computer science. This theorem is very useful when used in AI, a few examples of which are Bayesian networks( a way to model and make predictions is a world of events) and Naïve bayes.

Now to understand what Bayes’ theorem is we must first understand what conditional probability is. Conditional probability is the probability of an event occurring , based on the probability that a previous event has occurred. It is often portrayed as “Probability of event A given event B”, and its notation is P(A|B). To calculate P(A|B) we can use a mathematical formula: P(A|B) = P(A and B) / P(B).
Now let us start to understand Bayes’ theorem through an example: If there is a patient with a set of symptoms, say S, then the doctor concludes that they could have condition, say C. So if the probability of having condition C, when showing symptoms S: P(C|S), is high then we must start treating the patient for what condition we can assume they have. However when trying to find P(C|S) we run into a problem, in medical records patients are sorted according to their treatments and not by the symptoms they showed. Thus we now can’t find P(C|S), but we realise that we could find the probability that a patient has symptoms S given that they have condition C: P(S|C). This is the inverse of what we want but we can use Bayes’ theorem to find out what we actually want from it’s inverse.

Below I have shown the formula for Bayes’ Theorem, and the derivation to this formula is really simple. We already know that :

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cgroe8d01emug44majw1.png)


P(A|B) = P(A and B) / P(A).
And if you know this or not I want you to just accept for now that:
P(A and B) = P(B|A) x P(A)
Now we just substitute equation 2 into equation 1, and we have Bayes’ theorem. Right now this may just look like you have spent all your time reading just to find out about this one formula that doesn’t even seem that useful, but let me tell you that the uses of this are widespread and not only in the field of computing but also in other fields such as economics and medicine.

To give you a proper idea as for what this theorem is used for I will list out a few examples:
1.	Fraud detection: The theorem can help in predicting whether a transaction form a credit card is a fraud or not, using past records on what the customer buys, and other risk factors that the credit card company decides. 
2.	Weather forecasting: We all check the weather before leaving for school, and you can thank Bayes’ theorem for updating the probability of certain weather conditions from new data that is gathered from satellites and weather stations. It like that patient example we saw earlier.
3.	Machine learning: Bayes’ theorem is used in machine learning for a multitude of things such as classifying items, anomaly detection, etc. 
Overall this is a very important part of statistics that plays a major role in the computer science field, mainly through its implication when it comes to software being able to more accurately understand it’s environments and be able to predict things that could happen in the future given past data. This help’s AI models to grow and become even better, and all this from just a simple maths equation that you have just learnt.