<jupyter_start><jupyter_text>Baye's Theorem  IntroductionBefor starting with *Bayes Theorem* we can have a look at some definitions.**Conditional Probability :**Conditional Probability is the Probability of one event occuring with some Relationship to one or more events.Let A and B be the two interdependent event,where A has already occured then the probabilty of B will be  $$ P(B|A) = P(A \cap B)|P(A) $$ **Joint Probability :**Joint Probability is a Statistical measure that Calculates the Likehood of two events occuring together and at the same point in time. $$ P(A \cap B) = P(A|B) * P(B) $$  Bayes TheoremBayes Theorem was named after **<NAME>**,who discovered it in **1763** and worked in the field of Decision Theory.Bayes Theorem is a mathematical formula used to determine the **Conditional Probability** of events without the **Joint Probability**.**Statement**If B$_{1}$, B$_{2}$, B$_{3}$,.....,B$_{n}$ are Mutually exclusive event with P(B$_{i}$) $\not=$  0 ,( i=1,2,3,...,n) of Random Experiment then for any Arbitrary event A of the Sample Space of the above Experiment with P(A)>0,we have$$ P(B_{i}|A) = P(B_{i})P(A|B_{i})/ \sum\limits_{i=1}^{n} P(B_{i})P(A|B_{i}) $$**Proof**Let S be the Sample Space of the Random Experiment.The Event  B$_{1}$, B$_{2}$, B$_{3}$,.....,B$_{n}$ being Exhaustive$$ S = (B_{1} \cup B_{2} \cup ...\cup B_{n})  \hspace{1cm} \hspace{0.1cm}      [\therefore A \subset S] $$$$ A = A \cap S = A \cap ( B_{1} \cup B_{2} \cup B_{3},.....,\cup B_{n}) $$$$   = (A \cap B_{1}) \cup (A \cap B_{2}) \cup ... \cup (A \cap B_{n}) $$$$ P(A) = P(A \cap B_{1}) + P (A \cap B_{2}) + ...+ P(A \cap B_{n}) $$$$  \hspace{3cm} \hspace{0.1cm}    = P(B_{1})P(A|B_{1}) + P(B_{2})P(A|B_{2}) + ... +P(B_{n})P(A|B_{n}) $$$$      = \sum\limits_{i=1}^{n} P(B_{i})P(A|B_{i}) $$Now,$$ P(A \cap B_{i}) = P(A)P(B_{i}|A) $$$$ P(B_{i}|A) = P(A \cap B_{i})/P(A) = P(B_{i})P(A|B_{i})/\sum\limits_{i=1}^{n} P(B_{i})P(A|B_{i}) $$**P(B)** is the Probability of occurence **B**.If we know that the event **A** has already occured.On knowing about the event **A**,**P(B)** is changed to **P(B|A)**.With the help of **Bayes Theorem we can Calculate P(B|A)**.**Naming Conventions :**P(A/B) : Posterior Probability P(A) : Prior ProbabilityP(B/A) : LikelihoodP(B) : Evidence So, Bayes Theorem can be Restated as :$$ Posterior = Likelihood * Prior / Evidence $$  Now we will be looking at some problem examples on Bayes Theorem.    **Example 1** :Suppose that the reliability of a Covid-19 test is specified as follows:Of Population having Covid-19 , 90% of the test detect the desire but 10% go undetected.Of Population free of Covid-19 , 99% of the test are judged Covid-19 -tive but 1% are diagnosed showing Covid-19 +tive.From a large population of which only 0.1% have Covid-19,one person is selected at Random,given the Covid-19 test,and the pathologist Report him/her as Covid-19 positive.What is the Probability that the person actually have Covid-19? **Solution**Let, B$_{1}$ = The Person Selected is Actually having Covid-19.B$_{2}$ = The Person Selected is not having Covid-19.A = The Person Covid-19 Test is Diagnosed as Positive.P(B$_{1}$) = 0.1% = 0.1/100 = 0.001P(B$_{2}$) = 1-P(B$_{1}$) = 1-0.001 = 0.999P(A|B$_{1}$) = Probability that the person tested Covid-19 +tive given that he / she is actually having Covid-19.= 90/100 = 0.9 P(A|B$_{2}$) = Probability that the person tested Covid-19 +tive given that he / she is actually not having Covid-19.= 1/100 = 0.01 Required Probability = P(B$_{1}$|A) = P(B$_{1}$) * P(A|B$_{1}$)/ (((P(B$_{1}$) * P(A|B$_{1}$))+((P(B$_{2}$) * P(A|B$_{2}$)))                     = (0.001 * 0.9)/(0.001 * 0.9+0.999 * 0.01) = 90/1089 =0.08264 We will Now use Python to calculate the same.<jupyter_code>#calculate P(B1|A) given P(B1),P(A|B1),P(A|B2),P(B2)
def bayes_theorem(p_b1,p_a_given_b1,p_a_given_b2,p_b2):
   p_b1_given_a=(p_b1*p_a_given_b1)/((p_b1*p_a_given_b1)+(p_b2*p_a_given_b2))
   return p_b1_given_a

#P(B1)
p_b1=0.001
#P(B2)
p_b2=0.999
#P(A|B1)
p_a_given_b1=0.9
#P(A|B2)
p_a_given_b2=0.01
result=bayes_theorem(p_b1,p_a_given_b1,p_a_given_b2,p_b2)
print('P(B1|A)=% .3f %%'%(result*100))<jupyter_output>P(B1|A)= 8.264 %<jupyter_text>**Example 2 :** In a Quiz,a contestant either guesses or cheat or knows the answer to a multiple choice question with four choices.The Probability that he/she makes a guess is 1/3 and the Probability that he /she cheats the answer is 1/6.The Probability that his answer is correct,given that he cheated it,is 1/8.Find the Probability that he knows the answer to the question,given that he/she correctly answered it. **Solution**Let, B$_{1}$ = Contestant guesses the answer.B$_{2}$ = Contestant cheated the answer.B$_{3}$ = Contestant knows the answer.A = Contestant answer correctly.clearly,P(B$_{1}$) = 1/3 , P(B$_{2}$) =1/6Since B$_{1}$ ,B$_{2}$, B$_{3}$ are mutually exclusive and exhaustive event.P(B$_{1}$) + P(B$_{2}$) + P(B$_{3}$) = 1 => P(B$_{3}$) = 1 - (P(B$_{1}$) + P(B$_{2}$))=1-1/3-1/6=1/2If B$_{1}$ has already occured,the contestant guesses,the there are four choices out of which only one is correct.$\therefore$ the Probability that he answers correctly given that he/she has made a guess is 1/4 i.e. **P(A|B$-{1}$) = 1/4**It is given that he knew the answer = 1By Bayes Theorem,Required Probability = P(B$_{3}$|A)= P(B$_{3}$)P(A|B$_{3}$)/(P(B$_{1}$)P(A|B$_{1}$)+P(B$_{2}$)P(A|B$_{2}$)+P(B$_{3}$)P(A|B$_{3}$))= (1/2 * 1) / ((1/3 * 1/4) + (1/6 * 1/8) + (1/2 * 1))=24/29<jupyter_code>#calculate P(B1|A) given P(B1),P(A|B1),P(A|B2),P(B2),P(B3),P(A|B3)
def bayes_theorem(p_b1,p_a_given_b1,p_a_given_b2,p_b2,p_b3,p_a_given_b3):
   p_b3_given_a=(p_b3*p_a_given_b3)/((p_b1*p_a_given_b1)+(p_b2*p_a_given_b2)+(p_b3*p_a_given_b3))
   return p_b3_given_a

#P(B1)
p_b1=1/3
#P(B2)
p_b2=1/6
#P(B3)
p_b3=1/2
#P(A|B1)
p_a_given_b1=1/4
#P(A|B2)
p_a_given_b2=1/8
#P(A|B3)
p_a_given_b3=1
result=bayes_theorem(p_b1,p_a_given_b1,p_a_given_b2,p_b2,p_b3,p_a_given_b3)
print('P(B3|A)=% .3f %%'%(result*100))<jupyter_output>P(B3|A)= 82.759 %
{'fraction_non_alphanumeric': 0.19445366987711724, 'fraction_numerical': 0.05679176353370973, 'mean_word_length': 4.887585532746823, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '1651', 'license': 'MIT', 'n_tokens_mistral': 2661, 'n_tokens_neox': 2469, 'n_words': 679, 'path': "notebooks/Baye's Theorem Notebook.ipynb", 'repo_name': 'Ritu7683/Statistics-and-Econometrics-for-Data-Science', 'size': '6028'}