Foundations to Frontier
Tests
D. Classical Machine Learning › D3 Probabilistic and Bayesian Classifiers

Bernoulli Naive Bayes

Bernoulli naive Bayes models binary features (an alarm on or off, a word present or absent) as independent coin flips with class-specific probabilities. Its key difference from the multinomial model is that absent features count as evidence too: a missing alarm that a fault would almost always trigger argues against that fault. This page covers the model, smoothing with Beta priors, scoring in log space, the role of absence evidence, the comparison with multinomial NB on text, and its linear form. In the exercises you implement Bernoulli NB and verify a hand calculation in which silence is informative; then you diagnose spacecraft faults from 40 alarm bits, including faults whose alarm signatures are subsets of others, and compare it with multinomial NB and softmax regression across training-set sizes and smoothing levels.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

The dog that did not bark

A stuck valve lights alarms 1, 2 and 3. A stuck valve plus a failed regulator lights alarms 1 to 5. If you see alarms 1, 2 and 3 lit, which fault is it? Looking only at what is lit cannot tell. The key clue is that alarms 4 and 5 are dark, which a regulator failure would almost certainly have triggered. Bernoulli naive Bayes treats every alarm as on or off, and both states count as evidence.

Alarm panel: evidence from lit and dark alarms
Bernoulli NB (uses dark alarms)
A: valve0.989
B: valve + regulator0.011
C: power0.000
Multinomial NB (lit alarms only)
A: valve0.796
B: valve + regulator0.203
C: power0.000
log-evidence A vs B from each alarm (Bernoulli): 1: +0.0 · 2: +0.0 · 3: +0.0 · 4: +2.3 · 5: +2.3 · 6: +0.0 · 7: +0.0 · 8: +0.0
With alarms 1–3 lit and 4–5 dark, Bernoulli NB is confident in A: alarms 4 and 5 being dark is strong evidence against B. Multinomial NB ignores the dark alarms; it still leans to A, but only because B spreads its probability over five alarms instead of three, so it is less confident. Light alarms 4 and 5 and both models switch to B.
Why engineers care

Fault diagnosis from discrete alarms, built-in-test flags and status bits is a binary-feature problem. Missing symptoms are as diagnostic as present ones, and Bernoulli NB uses both, with a model so simple it can be checked by hand and stored as a table of probabilities.

📝 Quick check (Level 0)
0/3 answered
  1. 1. Bernoulli NB features are…
  2. 2. Absent features in Bernoulli NB…
  3. 3. In the demo, what separates fault A from fault B when alarms 1–3 are lit?

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

Diagnosis by hand

Training events have alarms [pressure_low, valve_stuck, voltage_low, temp_high]:

  • propulsion faults: (1,1,0,0), (1,0,0,0), (1,1,0,1);
  • power faults: (0,0,1,0), (0,0,1,1), (0,0,1,0).

The smoothed alarm probabilities (count + 1)/(n + 2) are:

| Alarm | P(on | propulsion) | P(on | power) | |—|—|—|—| | pressure_low | 4/5 = 0.8 | 1/5 = 0.2 | | valve_stuck | 3/5 = 0.6 | 1/5 = 0.2 | | voltage_low | 1/5 = 0.2 | 4/5 = 0.8 | | temp_high | 2/5 = 0.4 | 2/5 = 0.4 |

Only pressure_low lit, x = (1, 0, 0, 0):

  • propulsion: 0.8 × (1 − 0.6) × (1 − 0.2) × (1 − 0.4) = 0.1536;
  • power: 0.2 × 0.8 × 0.2 × 0.6 = 0.0192;
  • P(propulsion) = 8/9 = 0.889.

No alarms at all, x = (0, 0, 0, 0):

  • propulsion: 0.2 × 0.4 × 0.8 × 0.6 = 0.0384;
  • power: 0.8 × 0.8 × 0.2 × 0.6 = 0.0768;
  • P(power) = 2/3.

Silence is informative: propulsion faults usually raise pressure_low, so its absence favours power. A multinomial model has no words to count here and would just return the prior.

The steps in plain English:

  1. Binarise the features (present/absent, alarm on/off).
  2. For each class and feature, estimate P(on) = (number of class examples with it on + α)/(class size + 2α).
  3. Score a new case: log πₖ + Σⱼ [xⱼ log pₖⱼ + (1 − xⱼ) log(1 − pₖⱼ)].
  4. Normalise and pick the most probable class.
  5. Tune α. With very few examples per class, use a small α so that rare spurious alarms are not inflated.
📝 Quick check (Level 1)
0/3 answered
  1. 1. Smoothed P(on) for 0 of 3 examples, α = 1?
  2. 2. An alarm that is off contributes…
  3. 3. With no alarms lit, Bernoulli NB returns…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 30 minUndergraduate students and practising engineers.

The model

Likelihood: p(x | y = k) = ∏ⱼ pₖⱼ^xⱼ (1 − pₖⱼ)^(1 − xⱼ), with each xⱼ equal to 0 or 1.

Smoothed estimate: p̂ₖⱼ = (nₖⱼ + α)/(nₖ + 2α). This is the posterior mean under a Beta(α, α) prior, where nₖⱼ is the number of class-k examples with feature j on.

Linear form. The score is log πₖ + Σⱼ log(1 − pₖⱼ) + Σⱼ xⱼ log[pₖⱼ/(1 − pₖⱼ)]. It is linear in x, with weights equal to the log-odds of each feature being on, plus a class bias that collects every “off” term. This bias is what makes absence count.

Bernoulli versus multinomial for text (McCallum and Nigam, 1998):

  • Bernoulli models presence/absence over the whole vocabulary, so every unseen word contributes a (1 − p) term. Repeats do not matter, and long documents are handled poorly.
  • Multinomial models counts, ignores absent words, and does better with large vocabularies and longer documents.
  • Bernoulli can do well with short texts and small vocabularies (for example, alarm codes).
from sklearn.naive_bayes import BernoulliNB
clf = BernoulliNB(alpha=0.1, binarize=0.0).fit(X, y)      # binarize: threshold for 'on' (None if already 0/1)
clf.predict_proba(X_new); np.exp(clf.feature_log_prob_)    # P(on | class)

Use it when the features are naturally binary (alarms, flags, presence of symptoms or keywords) and absences are informative. Prefer multinomial NB for long texts with counts. Consider logistic regression when there is enough data and features interact.

🐍 Exercise 1: Bernoulli naive Bayes
Implement Bernoulli naive Bayes with Beta smoothing, log-likelihoods including the absent-feature terms, posteriors and predictions; verify the hand example (only pressure_low lit gives 8/9 for propulsion; no alarms gives 2/3 for power), and compare with the true Bayes classifier on synthetic alarm data.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Bernoulli NB smoothing corresponds to a…
  2. 2. The 'off' terms of Bernoulli NB act like…
  3. 3. BernoulliNB's binarize parameter…

Level 3: Advanced

★★★★☆ Advanced⏱ 30 minGraduate level, with full derivations and proofs.

Theory and failure modes

Absence evidence, quantified. If a fault lights an alarm with probability 0.9 and the alternative lights it with probability 0.03, then seeing the alarm dark gives a log ratio of log(0.97/0.1) = 2.27 against the fault, about 10 to 1. Two such dark alarms give about 94 to 1. This is how Bernoulli NB separates subset signatures.

Smoothing with tiny samples. With nₖ examples per class, a never-seen alarm gets p = α/(nₖ + 2α). With nₖ = 5 and α = 1, that is 1/7 = 0.14, far above a true spurious rate of 0.03. All those inflated “off” probabilities blur the classes. In exercise 2, α = 1 cut accuracy from 0.93 to 0.80 with 30 events, while with 120 or more events it was slightly better than α = 0.1. Choose α relative to the class sizes.

Independence violations. Alarms in a fault cascade fire together. Naive Bayes double-counts them (D3.31), giving overconfident posteriors. Mitigations:

  • group correlated alarms into one feature;
  • use tree-augmented NB or Bayesian networks (D3.35);
  • calibrate the probabilities.

Relation to logistic regression. Both are linear in x. Logistic regression fits the weights discriminatively and can learn that two co-firing alarms carry less joint evidence. With few examples, the generative estimates are more stable.

Interview questions.

  • When does Bernoulli beat multinomial NB? When features are binary by nature, vocabularies are small, or absence is diagnostic.
  • What goes wrong with Bernoulli NB on long documents? Every one of thousands of absent words contributes a term, so its scores become dominated by absences and by document length.
📝 Quick check (Level 3)
0/3 answered
  1. 1. Log evidence of a dark alarm with p = 0.9 (fault) vs 0.03 (other)?
  2. 2. With 5 examples per class and α = 1, an unseen alarm gets probability…
  3. 3. Co-firing alarms make naive Bayes…

Level 4: Expert

★★★★★ Expert⏱ 25 minResearchers: original papers and open problems.

Diagnosing faults from alarm bits

History.

  • Early computer-aided medical diagnosis (Warner et al., 1961; de Dombal et al., 1972) used naive Bayes over binary symptoms, often matching clinicians on acute abdominal pain.
  • The binary-independence model of information retrieval (Robertson and Spärck Jones, 1976) is Bernoulli NB.
  • McCallum and Nigam (1998) compared the Bernoulli and multinomial event models for text.
  • Bernoulli-style symptom models still underlie many diagnostic expert systems and fault-tree-to-Bayes conversions.

Exercise 1. The smoothed probabilities match the table. With only pressure_low lit, P(propulsion) = 0.889; with no alarms, P(power) = 0.667. On synthetic alarm data (30 alarms, 3 classes), Bernoulli NB reaches accuracy 0.890, against 0.892 for the true Bayes rule.

Exercise 2: 40 alarm bits, 6 fault modes. Setup: two faults have signatures that are subsets of two others; 20 repetitions. Each cell shows overall accuracy / accuracy on the four subset-related faults.

Training events Bernoulli NB (α = 0.1) Bernoulli NB (α = 1) Multinomial NB Softmax regression
30 0.934 / 0.906 0.803 / 0.746 0.892 / 0.842 0.921 / 0.889
120 0.949 / 0.928 0.957 / 0.938 0.896 / 0.853 0.956 / 0.937
600 0.966 / 0.951 0.969 / 0.955 0.944 / 0.920 0.968 / 0.953
  • Absence evidence matters. Multinomial NB, which sees only lit alarms, is 3–8 points worse on the subset faults at every size.
  • With 5 events per class, Bernoulli NB (small α) is the best model, beating softmax regression: the generative model and its fixed structure need little data.
  • Heavy smoothing (α = 1) is disastrous with tiny classes but slightly better once classes have 20 or more events. Match α to the data.
  • With enough data, Bernoulli NB and softmax regression converge to the same accuracy.
🐍 Exercise 2 (research): alarm-based fault diagnosis
Simulate 40 binary alarms for 6 fault modes, two of whose signatures are subsets of others; compare Bernoulli NB (α = 0.1 and 1), multinomial NB on the same bits and softmax regression by overall accuracy and accuracy on the subset faults, for 30, 120 and 600 training events.

Bernoulli naive Bayes in AI, robotics and aerospace

  • Alarm and BIT-flag diagnosis: fault isolation from discrete status bits.
  • Symptom-based expert systems: maintenance troubleshooting from checklists.
  • Short-text classification: keyword-presence filters for alerts and commands.
  • Binary sensor networks: occupancy or event detection from on/off detectors.

References

  1. A. McCallum, K. Nigam (1998). “A comparison of event models for naive Bayes text classification.” AAAI Workshop on Learning for Text Categorization.
  2. S. E. Robertson, K. Spärck Jones (1976). “Relevance weighting of search terms.” Journal of the American Society for Information Science 27(3).
  3. F. T. de Dombal, D. J. Leaper, J. R. Staniland, A. P. McCann, J. C. Horrocks (1972). “Computer-aided diagnosis of acute abdominal pain.” BMJ 2(5804).
  4. H. R. Warner, A. F. Toronto, L. G. Veasey, R. Stephenson (1961). “A mathematical approach to medical diagnosis.” JAMA 177(3).
  5. K. P. Murphy (2012). Machine Learning: A Probabilistic Perspective, section 3.5. MIT Press.