Problem 328626 · medium · Level 03 Linear Management & Searching

Listening for Leaks

Bayes' rule · prior and posterior · sensitivity · false positives · updating

A water company listens to pipe sections with an acoustic sensor. From past digs it knows:

  • a section has a leak with probability prior before any listening (the base rate);
  • if there is a leak, the sensor beeps with probability sensitivity;
  • if there is no leak, it still beeps with probability false_alarm (traffic, pumps, ...).

A section is listened to several times, and given whether it leaks, the listenings are independent. results is a string of "+" (beep) and "-" (no beep) in the order they happened. Write leak_chances(prior, sensitivity, false_alarm, results) that returns the list of probabilities that the section leaks after each result, in order.

Examples

Input:  prior = 0.02, sensitivity = 0.9, false_alarm = 0.1, results = "++-"
Output: [0.15517241379310345, 0.6230769230769231, 0.15517241379310343]
Explanation: in 1000 sections, 20 leak and 18 of those beep; 980 do not, and 98 of
those beep too. So after one beep 18 / (18 + 98) = 0.155. A second beep raises it
to 0.62, and a silence brings it back down to exactly where one beep had left it.

Input:  prior = 0.5, sensitivity = 0.8, false_alarm = 0.2, results = ""
Output: []

Constraints

  • 0 < prior < 1, 0 < false_alarm < sensitivity < 1
  • 0 <= len(results) <= 1000
  • floats are compared with a tolerance of 1e-6

Goals

  • Apply Bayes' rule to a positive and to a negative result
  • Use each posterior as the prior for the next, independent result
  • See how the base rate and a negative result change the conclusion
Starting Python…