46A probability kit

mediumpython
40:00

46 A probability kit

Netflix data science candidates report a Bayes' rule calculation in the statistics round (2 reports), and an OpenAI coding round asked for the expected number of iterations of a function that loops at random (1 source). This is practice in that shape, not a leaked question.

Set a 40-minute timer. Read one level at a time, and run only that level's tests until they pass: pytest problems/46_probability_kit -k level1. Earlier levels must keep passing.

Level 1. Bayes' rule. A check flags orders. posterior(prior, sensitivity, false_positive_rate) returns the chance that an order is fraud once it's flagged: prior is the share of orders that are fraud, sensitivity the share of fraud the check flags, and false_positive_rate the share of good orders it flags. update(prior, results) applies several independent results in order, each (sensitivity, false_positive_rate, positive). A positive result weighs sensitivity against false_positive_rate, and a negative one weighs 1 - sensitivity against 1 - false_positive_rate. Each result's answer is the next one's prior.

Level 2. Waiting. expected_tries(p) is the average number of tries until the first success, the success included, when each try succeeds with chance p. Raise ValueError unless 0 < p <= 1. expected_tries_for( successes, p) is the average number of tries until that many successes. coupon_collector(n) is the average number of draws, each equally likely to be any of n kinds, until every kind has shown up.

Level 3. A fair die from another. make_rand7(rand5) takes a function that returns a uniform whole number from 1 to 5 and returns a function that returns a uniform whole number from 1 to 7. Every value must be equally likely, and it should call rand5 at most 2.5 times on average for each number it returns.

Things to get right: the base rate, which rates a negative result uses, counting the try that succeeds, and keeping only outcomes that split evenly into 7. Follow-ups from the lesson: the odds form of Bayes' rule, linearity of expectation, and why a modulo without rejection is biased.

solution.py
Run the tests to see which pass.