AI: Apply Bayes' rule

Turn a test's accuracy into the probability that matters.

What to build

A program that computes the probability of the evidence and the posterior probability of a hypothesis.

Requirements

  • Inputs: P(H), P(E | H) and P(E | not H).
  • Print P(E) = X and P(H|E) = Y, each with four decimal places.

Input

One line: three probabilities.

Output

Two lines.

Constraints

P(E) is greater than 0.

Example

Input:

0.01 0.9 0.05

Output:

P(E) = 0.0585
P(H|E) = 0.1538

Why it works

Evidence can arise with or without the hypothesis, so the posterior is the share of all the ways to see E that come from H.

Examples

Example 1 · Input
0.01 0.9 0.05
Expected output
P(E) = 0.0585
P(H|E) = 0.1538
Example 2 · Input
0.5 0.8 0.2
Expected output
P(E) = 0.5000
P(H|E) = 0.8000

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

Hints

Hint 1

P(E) = P(E | H) P(H) + P(E | not H) (1 − P(H)).

Hint 2

Then P(H | E) = P(E | H) P(H) / P(E).

Solution

Show one correct solution (try it yourself first)
h, e_h, e_not_h = map(float, input().split())
p_e = e_h * h + e_not_h * (1 - h)
print(f"P(E) = {p_e:.4f}")
print(f"P(H|E) = {e_h * h / p_e:.4f}")

Lesson: Probability and Bayes' rule →