Bayes' Rule

~12 mincode completion

Implement returning as a float.

false_positive_rate is .

Examples

The rare-disease case: a positive test still leaves you probably fine

Input
posterior(0.01, 0.99, 0.05)
Output
0.16667

A perfect test with no false positives confirms the hypothesis outright

Input
posterior(0.3, 0.9, 0)
Output
1

Evidence that is equally likely either way leaves the prior untouched

Input
posterior(0.4, 0.5, 0.5)
Output
0.4

Hints

Hint 1

Work directly with the arguments , , false_positive_rate and return the result rather than printing it.

Hint 2

A common slip here: divides by the likelihood instead of the evidence.

Requirements

  • : P(H)

  • : P(E | H)

  • false_positive_rate: P(E | not H)

  • Return float: P(H | E)

Constraints

  • Standard library only, no imports required

  • Time limit: 200 ms, Memory: 64 MB

Where this shows up

~12 min

8 employers weight this skill

4 quant funds, 2 health and bio companies, 2 big tech firms. Top match scores 93.

Python
def posterior(prior, likelihood, false_positive_rate):
    """
    Bayes' rule for a binary hypothesis.

    Args:
        prior:               P(H)
        likelihood:          P(E | H)
        false_positive_rate: P(E | not H)

    Returns:
        float: P(H | E)
    """
    # YOUR CODE HERE
    pass
Loading docs…

The AI Mentor needs an account

It reads your code and the failing tests and nudges you toward the fix without handing you the answer. Free accounts get it on every problem you're working on today.