<jupyter_start><jupyter_text>Bayes Theorem Bayes Theorem is still a kind of "non intuitive" concept to grasp. But to clarify it's  usefulness, it's best to explain it through examples. We first gonna introduce the **Tree diagrams** concept, to help better visualize the problems  Tree Diagrams **Tree diagram** is a tool to better organize **conditioned probabilities**. Here we take The example 2.52 (page 96) registering the midterm and final grades of a class. It shows the proportion of Students getting an A in their finals considering if they got an A in their midterms<jupyter_code>from IPython.display import Image
Image(filename="img/midterm_final.png")<jupyter_output><empty_output><jupyter_text>** Question : ** Picking up a random final exam knowing it got an  A, what's the probability this sudent had an A in his midterm ? In mathematical notation the goal is to find the conditional probability : $P(midterm  =  A \space |  \space final  =  A) $ which translates to : $P(midterm  =  A \space |  \space final  =  A)$ = $ \frac{P(midterm = A \space and \space final = A)}{P(final=A)}$ we can know find each part in the tree diagram * $P(midterm = A \space and \space final = A)= 0.0611$<jupyter_code>from IPython.display import Image
Image(filename="img/mf_A.png")<jupyter_output><empty_output><jupyter_text>* $P(final=A) = P(midterm = other \space and \space final = A) \space + \space P(midterm = A \space and \space final = A) = 0.0611 + 0.0957 = 0.1568$marginal probability<jupyter_code>from IPython.display import Image
Image(filename="img/mf_final.png")<jupyter_output><empty_output><jupyter_text>* $P(midterm  =  A \space |  \space final  =  A)$ = $ \frac{P(midterm = A \space and \space final = A)}{P(final=A)}$ = $\frac{0.0611}{0.1568} = 0.3897$ The probability the student who got an A in his finals also got an A in his midterm is  $0,39$  Bayes Theorem In some cases, given the probability $P(A|B)$, we sould like to find the inverted conditional probability $P(B|A)$ Basing on the **exercise 2.54** (page 98) of he probability a woman can develop a cancer depending on the mammogram<jupyter_code>from IPython.display import Image
Image(filename="img/breast_cancer.png")<jupyter_output><empty_output><jupyter_text>Tree diagram showing the result of a mammogram following if the patient has cancer or not. $P(Mammogram | Cancer)$ following this tree diagram data, we would like to answer the following **question :** if we tested a woman over 40 for breast cancer with a mammogram, and it returned positive; what is the probability the patient actually has breast cancer. Answering this question means finding the opposite of $P(Mammogram | Cancer)$; which means $P(Cancer | Mammogram)$ Let's set the following nomination : * **P(C)  :** Probability of having cancer* **P(7C) :** Probability of not having cancer* **P(M+) :** Probability the mammogram is positive* **P(M-) :** Probability the mammogram is negative Which means we'll have to solve the following :$P(C|M+) = \frac{P(C \space and \space M+)}{P(M+)}$ - **Finding $P(C \space and \space M+)$ : **<jupyter_code>from IPython.display import Image
Image(filename="img/C_and_M+.png")<jupyter_output><empty_output><jupyter_text>$P(C \space and \space M+) = P(M+ \space | \space C) . P(C) = 0.00312$ * **Finding $P(M+)$ : **<jupyter_code>from IPython.display import Image
Image(filename="img/M+.png")<jupyter_output><empty_output><jupyter_text>$ P(M+) = P(M+ and \space C) + P(M+ and \space 7C)$$   = P(C).P(M+|\space C) + P(7C).P(M+|\space 7C)$$ = 0.0035 \space X \space 0.89 + 0.9965 \space X \space 0.07$$= 0.07288$ * Which gives :$P(C \space|\space M+) = \frac{P(C\space and \space M+ )}{P(M+)} = \frac{0.00312}{0.07288} = 0.0428$ The result is : even if a patient has a positive mammogram screening,  the probability that she has breast cancer is 4%  Direct Application of Bayes Rule $ P(C \space|\space M+) = \frac{P(M+|\space C).P(C)}{P(M+|\space 7C).P(7C)+P(M+|\space C).P(C)} $  * **Guided Practice 2.56 : ** Based on the Tree Diagram of the Parking lot exercise<jupyter_code>from IPython.display import Image
Image(filename="img/parking_lot.png")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13133253301320527, 'fraction_numerical': 0.025210084033613446, 'mean_word_length': 4.909219858156028, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '63596', 'license': 'MIT', 'n_tokens_mistral': 1462, 'n_tokens_neox': 1356, 'n_words': 503, 'path': 'Chapter 2/Chap2_Bayes_Theorem.ipynb', 'repo_name': 'chikoungoun/OpenIntro', 'size': '4165'}