2

Bayes Theorem

Unsolved
Fundamentals
Prob. and Stats

Difficulty: 1 | Problem written by anish-lakkapragada
Problem reported in interviews at

Amazon
Apple
Facebook
Google
Netflix

Bayes theorem is ubiquitous in machine learning and probabilistic models. It is built on the idea that we can use new information to update probabilities that were based on old information. For example, if we want to predict whether a given book is nonfiction or fiction, and we know that 65% of books are fiction and 35% of books are nonfiction, we can predict based on our current information (priors) that since there are almost 2x more fiction books than nonfiction, this book will be nonfiction. However, if we receive more information that this book has an appendix, and we know that 90% of nonfiction books have an appendix and 20% of fiction books have an appendix- we can use this new information to update our prior belief that the book was fiction. We should not forget the old information and use this new information, but instead, keep both in mind when making a decision. 

Here our hypothesis (H) would be that the book was nonfiction, and the evidence (E) was that it has an appendix. P(H | E) would mean the probability of the book being nonfiction given the evidence. The priors are P(N) = 0.35 and P(F) = 0.65, N for nonfiction and F for fiction. The probability of seeing the evidence given the hypothesis, or P(E | H), equals 0.9 because 90% of nonfiction books (our hypothesis) have an appendix. P(E -| H) here is the probability of a book having the appendix as a fiction book. Similarly, P(-| H) would be the probability of the book not being nonfiction, given no more information (which is 65%.) In the Bayes() function you will be given P(H), P(E | H), and P(E -| H).

You should return P(H | E).

Note: These statistics were completely made up, but hopefully they helped you learn about this amazing theorem!

 

 

Sample Input:
<class 'float'>
h_prior: 0.9
<class 'float'>
e_given_h: 0.2
<class 'float'>
e_not_given_h: 0.3

Expected Output:
<class 'float'>
0.8571428571428572

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Ab aliquam voluptatum dolores exercitationem, inventore architecto reprehenderit beatae repellat blanditiis odit sed facilis maiores saepe laborum, aperiam delectus rem architecto ducimus quam, repellat quod itaque, minus et harum veniam molestiae inventore minima error praesentium atque? Obcaecati ad officiis voluptatem quasi assumenda quod tenetur saepe ipsa ullam ab, sequi cum deleniti fuga eveniet delectus illo, dignissimos eligendi quas vel laudantium commodi iusto debitis ea voluptatem, velit possimus necessitatibus deleniti eveniet aliquid soluta vel quidem, iste error ad necessitatibus.

Est consectetur aut laboriosam ducimus, reiciendis animi voluptatibus praesentium iure cum iusto minima est, nesciunt temporibus quod officiis soluta illum molestias tempore, facilis voluptate eaque. Repudiandae laboriosam fugiat corrupti cupiditate obcaecati sint fuga. Architecto autem ab. Quis enim amet quos iure delectus velit?

Necessitatibus rerum molestiae exercitationem dolores accusantium harum quo odit tenetur nihil quisquam, vel eius natus enim aspernatur totam omnis debitis voluptas, officiis laborum expedita quam aliquam, qui distinctio suscipit ab voluptate eos doloremque perspiciatis voluptatum deserunt, cupiditate culpa tempora nisi maiores omnis dignissimos magnam sed doloribus esse? Odio possimus debitis quas quis neque dolore ad aliquid asperiores ipsam amet, illum sunt debitis ipsum, pariatur non sapiente aperiam nesciunt, facere ratione laudantium exercitationem nam vel? Mollitia a debitis similique et adipisci unde delectus eligendi.

This is a premium feature.
To access this and other such features, click on upgrade below.

Ready.

Input Test Case

Please enter only one test case at a time
numpy has been already imported as np (import numpy as np)