Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Chapter 22 — Assignment Project: A Triage Assistant for a Clinic

Open in Colab

Overview

A clinic wants a small assistant that, from a few questions at the reception desk, estimates how likely a patient is to have flu (to be seen by a doctor quickly, and kept away from other patients). Doctors want to understand why it says what it says, to ask it any question, and to use it even when some answers are missing.

You will build a Bayesian network from the clinic’s records, query it like a doctor would, let it choose the most useful next question, and compare it with the classifiers of Chapter 8.

Data. 4,000 simulated patient records (they are generated by the notebook; no real patient data is used). Each record has 8 yes/no variables: Winter (the visit is in winter), the confirmed diagnoses Flu, Cold, Allergy, and the symptoms Fever, Cough, Sneezing, Fatigue. The first 3,000 records are for training, the last 1,000 for testing.

The structure (agreed with the doctors):

PartTaskPoints
ALearn the tables from the records20
BAnswer the doctors’ questions25
CChoose the next question20
DCompare with Chapter 8’s classifiers, with and without missing answers20
EReport for the clinic15

Part A — Learn the Tables (20 points)

A1 (checked). Complete learn_network(df, structure, pseudo=1.0). For every variable and every combination of its parents’ values, estimate P(variable=True∣parents)P(\text{variable} = \text{True} \mid \text{parents}) by counting, with Laplace smoothing: #(True, parents)+pseudo#(parents)+2⋅pseudo\dfrac{\#(\text{True, parents}) + \text{pseudo}}{\#(\text{parents}) + 2\cdot\text{pseudo}}. Return a dict name -> (parents, table) in the format of the lab, with tables keyed by tuples of parent values.

A2. For the table of Fever, print how many training records support each entry. Which entries would you trust least, and why?

Your answer:


Part B — The Doctors’ Questions (25 points)

B1 (checked). Complete query(net, q, evidence), returning P(q=True∣evidence)P(q = \text{True} \mid \text{evidence}) by enumeration (your lab function, adapted to this network format, is fine). If q is already in the evidence, return 1.0 or 0.0.

B2. Answer, in a table, the doctors’ questions: P(Flu∣fever, cough)P(\text{Flu} \mid \text{fever, cough}); the same in winter; the same with sneezing; the same with fatigue. Then P(Allergy∣sneezing)P(\text{Allergy} \mid \text{sneezing}) and P(Allergy∣sneezing, cold)P(\text{Allergy} \mid \text{sneezing, cold}). Explain each change in one sentence (which of the patterns of Section 22.3 is at work?).

Your explanations:


Part C — The Next Question (20 points)

At the desk, the assistant can ask one more question. The best one is the one that is expected to make it most certain about flu. Uncertainty is measured by the entropy H(p)=−plog⁡2p−(1−p)log⁡2(1−p)H(p) = -p\log_2 p - (1 - p)\log_2(1 - p) (Chapter 8, decision trees).

C1 (checked). Complete expected_entropy(net, evidence, question): with pyes=P(question=True∣evidence)p_{yes} = P(\text{question} = \text{True} \mid \text{evidence}), return

pyes H(P(Flu∣evidence,yes))+(1−pyes) H(P(Flu∣evidence,no))p_{yes}\, H\big(P(\text{Flu} \mid \text{evidence}, \text{yes})\big) + (1 - p_{yes})\, H\big(P(\text{Flu} \mid \text{evidence}, \text{no})\big)

C2. A patient says they cough. Rank the four other questions (Fever, Sneezing, Fatigue, Winter) by expected entropy. Which should the receptionist ask? Then simulate a full interview: starting from {'Cough': True}, repeatedly ask the best remaining question for the first test patient who has flu and coughs, using their real answers, and print the probability of flu after each answer.


Part D — Compared with Chapter 8’s Classifiers (20 points)

D1. On the 1,000 test patients, predict flu from the five things known at the desk (Winter, Fever, Cough, Sneezing, Fatigue) with: your network; Bernoulli Naive Bayes; and logistic regression (both trained on the same five columns of the training records). Report the AUC and the log loss of each.

D2. Patients often cannot say whether they have a fever (no thermometer at hand). Hide the Fever answer for a random half of the test patients (np.random.default_rng(0).random(1000) < 0.5). The network simply leaves it out of the evidence; for logistic regression, fill it in with the training mean. Compare the AUCs.

Your observations:


Part E — Report for the Clinic (15 points)

Write 250–350 words for the clinic’s doctors and managers:

  1. What the assistant is (in plain words) and what it was built from.

  2. How well it works (test results), and what it does better than a standard classifier.

  3. One example of its reasoning a doctor would find sensible, and one that surprised you.

  4. Its limits: simulated data, the structure fixed by hand, rare combinations, and what must be checked before any real use (validation on real patients, approval, the doctor stays responsible).

Your report:


Grading Rubric

PartFull marks if…
A (20)tables learnt correctly (checked); unreliable entries identified with their counts
B (25)queries correct (checked); every change explained with the right pattern (chain, fork, collider)
C (20)expected entropy correct (checked); questions ranked; an interview simulated
D (20)a fair comparison on AUC and log loss; missing answers handled properly; an honest discussion
E (15)clear, accurate, honest about the limits and the doctor’s role