Book a free demo

Coding Grandmaster ยท project 22 of 46 ยท premium, free for now

Bayes and the medical test

Write posterior(prior, sensitivity, specificity) returning P(sick | positive) rounded to 4 decimal places. Print it for prior 0.01, sensitivity 0.9, specificity 0.9.

Learn

A test that is right 90% of the time sounds reliable, yet if only 1 person in 100 has the condition, most positive results are false alarms. Bayes' rule combines the three numbers: P(sick | positive) = prior ร— sensitivity รท (prior ร— sensitivity + (1 โˆ’ prior) ร— (1 โˆ’ specificity)). Picture 1,000 people: 10 are sick (9 test positive), 990 are healthy (99 test positive anyway), so only 9 of 108 positives are truly sick.

Your task

Write posterior(prior, sensitivity, specificity) returning P(sick | positive) rounded to 4 decimal places. Print it for prior 0.01, sensitivity 0.9, specificity 0.9.

Stretch challenge

A second positive test on the same person: use the answer as the new prior. What do you get?

โ† Given thatโ€ฆThe binomial distribution โ†’

Python
Press Run to see what your program prints.

More in this module

Keep going in Coding Grandmaster

Practise the idea

๐Ÿšช Play Monty Hall Lab

Pick a door, then stay or switch. Play ten games, then run a thousand to see the surprising answer.

Play now

FAQ

Bayes and the medical test: common questions

I am stuck. What can I do?

Press Hint for a nudge, read the message next to each goal, and compare your code with the lesson text. If you still need it, Show answer reveals a model solution. Try to type it yourself rather than copy it.

Does my work get saved?

Your ticks and XP are saved in this browser, and in your account if you log in. The code you type is not stored on a server.

Start a conversation

Enjoying coding? Meet a teacher.

Tell us the learner's age, country and what they would like to build, and we will suggest a class. A parent or guardian can make the enquiry.

  • Tell us the learnerโ€™s age or grade and goals
  • Weโ€™ll suggest a suitable class or learning path
  • Weโ€™ll confirm format, timing and fees with a parent or guardian

For a childโ€™s class, please have a parent or guardian send the enquiry. Avoid sharing passwords or sensitive personal details.

Weโ€™ll use your details only to respond to this request.

Play it, don't just read it

Write some code right now

Free games that need no sign-up. Pick one and play for five minutes.

All free games โ†’
Chat on WhatsApp
Esc

Try