Bayes' rule for a medical test

A positive test is evidence, not proof. Work out how likely a person who tests positive really is to be sick.

What to build

A program that applies Bayes' rule to a diagnostic test.

Requirements

  • Print P(positive) A with 4 decimal places.
  • Print P(sick | positive) B as a percentage with 1 decimal place, such as 9.2%.

Input

One line: the prior (share of people who are sick), the sensitivity (share of sick people who test positive) and the false-positive rate (share of healthy people who test positive).

Output

Two lines.

Constraints

All three are between 0 and 1, and P(positive) is greater than 0.

Example

Input:

0.01 0.9 0.09

Output:

P(positive) 0.0981
P(sick | positive) 9.2%

Why it works

The positives come from two groups, sick and healthy; Bayes' rule asks what share of all the positives comes from the sick group.

Examples

Example 1 · Input
0.01 0.9 0.09
Expected output
P(positive) 0.0981
P(sick | positive) 9.2%
Example 2 · Input
0.5 1 0
Expected output
P(positive) 0.5000
P(sick | positive) 100.0%

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

P(positive) = sensitivity × prior + false-positive rate × (1 − prior).

Hint 2

P(sick | positive) = sensitivity × prior / P(positive).

Solution

Show one correct solution (try it yourself first)
prior, sens, fpr = map(float, input().split())
positive = sens * prior + fpr * (1 - prior)
print(f"P(positive) {positive:.4f}")
print(f"P(sick | positive) {sens * prior / positive:.1%}")

Lesson: Bayes' rule and Naive Bayes →