0% found this document useful (0 votes)
2 views3 pages

Practical 8 - Implement Bayes Theorem Using Python

The document outlines the implementation of Bayes' Theorem in Python, focusing on its application in evaluating cancer test results. It provides a theoretical background on the theorem, including definitions of prior, likelihood, and posterior probabilities, followed by a practical example using a cancer screening scenario. The Python code demonstrates how to calculate the posterior probability of having cancer given a positive test result, with specific values provided for prior and likelihood probabilities.

Uploaded by

Dinesh Deore
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Practical 8 - Implement Bayes Theorem Using Python

The document outlines the implementation of Bayes' Theorem in Python, focusing on its application in evaluating cancer test results. It provides a theoretical background on the theorem, including definitions of prior, likelihood, and posterior probabilities, followed by a practical example using a cancer screening scenario. The Python code demonstrates how to calculate the posterior probability of having cancer given a positive test result, with specific values provided for prior and likelihood probabilities.

Uploaded by

Dinesh Deore
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Practical 3 1

Implement Bayes Theorem using


Python
Aim: To implement Bayes Theorem using Python

Theory:
Bayes' theorem, named after the 18th-century statistician and philosopher Thomas Bayes, is a
fundamental theorem in probability theory and statistics. It provides a way to update the probability for a
hypothesis (an event or proposition) based on new evidence or information. Bayes' theorem is
particularly useful in fields such as machine learning, statistics, and Bayesian inference.
The theorem is expressed mathematically as:

P(A|B) : This is the posterior probability, which represents the probability of hypothesis A being true
given the evidence B. It's what we want to compute.
P(A): This is the prior probability, which represents our initial belief or probability that hypothesis A is
true before considering any new evidence. It's based on our prior knowledge or assumptions.
P(B): This is the marginal likelihood or evidence, which represents the probability of observing evidence
B regardless of whether hypothesis A is true or false. It's the total probability of B over all possible
values of A.
P(B|A): This is the likelihood, which represents the probability of observing evidence
B if hypothesis A is true. It quantifies how well the evidence supports the hypothesis.
The following Python code shows the implementation of the Bayes Theorem

APPLIED ARTIFICIAL INTELLIGENCE PROF. ISMAIL H. POPATIA


Practical 3 2

Problem Statement: Consider a medical laboratory that conducts cancer screenings. The task is
evaluating the results of a cancer test. The test is designed to identify the presence of cancer in patients.
We have the following information:
1. The baseline probability that a randomly selected patient has cancer is 1%.
2. Among patients with cancer, 95% will test positive for the disease.
3. Among patients without cancer, 10% will still test positive for cancer.
Now, given a patient who has tested positive for cancer, we can use the above information to determine
the probability that the patient actually has cancer using Bayes Theorem as follows

Given:
priori_prob: 0.01
likelihood_cancer = 0.95
likelihood_no_cancer = 0.10
posterior_prob =?

The following Python code is used to solve the given problem


def bayes_theorem(prior_prob, likelihood, evidence):
# Calculate the posterior probability using Bayes' theorem
posterior_prob = (likelihood * prior_prob) / evidence
return posterior_prob

if name == " main ":


# Given values
prior_prob = 0.01 # Prior probability of having cancer
likelihood_cancer = 0.95 # Likelihood of getting a positive test
result given cancer
likelihood_no_cancer = 0.10 # Likelihood of getting a positive test
result given no cancer

# Calculate the evidence using the law of total probability


evidence = (likelihood_cancer * prior_prob) + (likelihood_no_cancer *
(1 - prior_prob))

# Calculate the posterior probability


posterior_prob = bayes_theorem(prior_prob, likelihood_cancer,
evidence)

print("Prior Probability of Cancer:", prior_prob)


print("Likelihood of Positive Test Given Cancer:", likelihood_cancer)
print("Likelihood of Positive Test Given No Cancer:",
likelihood_no_cancer)
print("Posterior Probability of Cancer Given Positive Test:",
round(posterior_prob, 2))

APPLIED ARTIFICIAL INTELLIGENCE PROF. ISMAIL H. POPATIA


Practical 3 3

Output:
Prior Probability of Cancer: 0.01
Likelihood of Positive Test Given Cancer: 0.95
Likelihood of Positive Test Given No Cancer: 0.1
Posterior Probability of Cancer Given Positive Test: 0.09

For the video demonstration of the practical click on the link below or scan the QR-code

https://youtu.be/Qsi1g7c89n4

APPLIED ARTIFICIAL INTELLIGENCE PROF. ISMAIL H. POPATIA

You might also like