intermediate classification-sentiment 40 min read

Linear Text Classifiers: Generative vs. Discriminative

Naive Bayes and logistic regression draw the same kind of boundary through word-count space and disagree only about where to put it. They share a hypothesis class and match different moments; Zipf's long tail breaks the generative shortcut; on separable text the logistic MLE runs off to infinity while its direction settles on the maximum-margin line; and no unigram model of financial tone can see that "costs rose" is bad news

Overview & Motivation

Train two classifiers on 1,5001{,}500 synthetic financial filings labeled negative, neutral, or positive in tone, using nothing but each document’s word counts, and test them on 1,5001{,}500 more. Naive Bayes gets 0.6010.601 of the test documents right. Logistic regression, given exactly the same counts, gets 0.7390.739. Both draw linear boundaries through the same space of count vectors; on this page we work out why they draw them in different places, and when that difference favors which one. Then we look at the one thing neither can see. The word “rose” gets a weight of 0.0600.060 from logistic regression, no more than noise among these weights, because “sales rose” is good news and “costs rose” is bad. A reader who parses every sentence could reach 0.8900.890.

The Zipf topic showed that a vocabulary is mostly rare words. That fact runs through everything below. We develop it in order:

  1. One hypothesis class. Naive Bayes is a softmax of a linear function of the counts, so it and logistic regression choose from the same family of boundaries.
  2. Two estimating equations. They differ in which moments of the training data they reproduce.
  3. How fast the generative side learns. Ng and Jordan’s bound, made explicit, and why it is honest but loose.
  4. The Zipf tail. A multinomial vocabulary cannot meet that bound’s hypothesis, and the cost is polynomial in the vocabulary.
  5. Burstiness and the crossover. When naive Bayes is the true model there is no crossover; when words come in bursts, there is.
  6. Separation. With more words than documents the logistic MLE does not exist, and its direction does.
  7. Naive Bayes features as a prior.

Then the finance corpus, where part of tone turns out to be an exclusive-or.


Two ways to draw one line

Take only two words, “strong” and “weak,” and plot each document as a point: how many times it says “strong” on one axis, how many times it says “weak” on the other. Most documents of every tone land exactly on the origin: 55%55\% of negative, 79%79\% of neutral, and 60%60\% of positive training documents say neither word, and so do 956956 of the 1,5001{,}500 test documents. The rest carry the signal, positive documents leaning along the first axis and negative ones along the second, and a classifier is a line through that plane.

Naive Bayes draws the line by modeling each class’s words. It estimates how often positive documents use “strong” and how often negative ones do, takes the log of the ratio as the word’s weight, and puts the line where the two classes’ models assign equal probability. Every word gets a vote proportional to its log ratio, whatever the other words are doing. Logistic regression draws the line directly. It moves the line until its predicted probabilities fit the labels as well as possible, and a word whose evidence the other words already carry gets less weight.

With 3,2463{,}246 words the plane becomes a 3,2463{,}246-dimensional space, and the line a hyperplane, but the picture is the same. Both methods draw a hyperplane. The rest of this page is about where each one puts it.


One hypothesis class

We write x∈NVx \in \mathbb{N}^V for a document’s count vector over a vocabulary of VV features, L(x)=∑jxjL(x) = \sum_j x_j for its length, and y∈{1,…,K}y \in \{1, \dots, K\} for its class.

Definition 1 (Bag of words).

A document’s count vector xx has xjx_j equal to the number of occurrences of feature jj, a word or, with bigrams, an adjacent pair of words. Its binarization is x^j=1{xj>0}\hat x_j = \mathbf{1}\{x_j \gt 0\}.

Multinomial naive Bayes models a document of class kk as LL independent draws from a distribution θk\theta_k over the vocabulary, so P(x∣y=k)∝∏jθkjxjP(x \mid y = k) \propto \prod_j \theta_{kj}^{x_j}. Bernoulli naive Bayes models each feature’s presence as an independent coin with probability θkj\theta_{kj}. Both are generative: they model xx given yy and invert with Bayes’s rule.

Proposition 1 (Naive Bayes is softmax-linear).

(a) Multinomial naive Bayes has log⁡P(y=k∣x)=log⁡softmax⁡k(Wx+b)\log P(y = k \mid x) = \log \operatorname{softmax}_k(Wx + b) with Wkj=log⁡θkjW_{kj} = \log \theta_{kj} and bk=log⁡πkb_k = \log \pi_k. Bernoulli naive Bayes has the same form in x^\hat x, with Wkj=logit⁡θkjW_{kj} = \operatorname{logit} \theta_{kj} and bk=log⁡πk+∑jlog⁡(1−θkj)b_k = \log \pi_k + \sum_j \log(1 - \theta_{kj}).

(b) For every (W,b)(W, b) and every length LL, some multinomial naive Bayes model has posterior softmax⁡(Wx+b)\operatorname{softmax}(Wx + b) on all documents of length LL. Across lengths,

softmax⁡(Wx+b)=softmax⁡(W~x+b+L(x) λˉ),λˉk=log⁡∑jeWkj,W~k=Wk−λˉk1,\operatorname{softmax}(Wx + b) = \operatorname{softmax}\big(\tilde W x + b + L(x)\,\bar\lambda\big), \qquad \bar\lambda_k = \log \textstyle\sum_j e^{W_{kj}}, \quad \tilde W_k = W_k - \bar\lambda_k \mathbf{1},

with each row of eW~e^{\tilde W} a probability distribution. The softmax-linear family is naive Bayes’s posteriors with one more feature, the length.

Proof.

(a) By Bayes’s rule, log⁡P(y=k∣x)=log⁡πk+log⁡P(x∣y=k)−log⁡P(x)\log P(y = k \mid x) = \log \pi_k + \log P(x \mid y = k) - \log P(x). For the multinomial, log⁡P(x∣k)=log⁡(L!/∏jxj!)+∑jxjlog⁡θkj\log P(x \mid k) = \log\big(L! / \prod_j x_j!\big) + \sum_j x_j \log \theta_{kj}, and the coefficient is the same for every class, so it cancels in the normalization. For the Bernoulli model, log⁡P(x^∣k)=∑j[x^jlog⁡θkj+(1−x^j)log⁡(1−θkj)]=∑jx^jlogit⁡θkj+∑jlog⁡(1−θkj)\log P(\hat x \mid k) = \sum_j \big[\hat x_j \log \theta_{kj} + (1 - \hat x_j)\log(1 - \theta_{kj})\big] = \sum_j \hat x_j \operatorname{logit}\theta_{kj} + \sum_j \log(1 - \theta_{kj}).

(b) Set θk=softmax⁡(Wk)\theta_k = \operatorname{softmax}(W_k), so Wk=log⁡θk+λˉk1W_k = \log \theta_k + \bar\lambda_k \mathbf{1} and Wk⋅x=∑jxjlog⁡θkj+L(x) λˉkW_k \cdot x = \sum_j x_j \log \theta_{kj} + L(x)\,\bar\lambda_k. When every document has length LL, choose log⁡πk=bk+Lλˉk\log \pi_k = b_k + L \bar\lambda_k up to a constant, and the naive Bayes posterior is softmax⁡(Wx+b)\operatorname{softmax}(Wx + b). When lengths vary, L(x) λˉkL(x)\,\bar\lambda_k depends on the document, so it is a feature rather than a bias.

∎

The extra feature matters. With a one-word vocabulary every document is just a length, every naive Bayes posterior is its prior, and a softmax of wkL+bkw_k L + b_k can still use the length. The difference of the two classes’ Bernoulli weights in (a), logit⁡θ1j−logit⁡θ0j\operatorname{logit}\theta_{1j} - \operatorname{logit}\theta_{0j}, is the Robertson–Spärck Jones relevance weight, with relevance as the class (formalRAG BM25 topic, Theorem 1). The harness checks (a) against the literal Bayes rule to 10−910^{-9} and against scikit-learn to 10−810^{-8}.

Definition 2 (Generative–discriminative pair).

A generative classifier fits a model of p(x,y)p(x, y) and predicts arg⁡max⁡yp(y∣x)\arg\max_y p(y \mid x). Its discriminative partner fits p(y∣x)p(y \mid x) directly, in the parametric family that the generative model induces. Multinomial naive Bayes and softmax logistic regression on counts are such a pair.

The pairing is Ng and Jordan’s (2002), building on Rubinstein and Hastie (1997). The formalML naive-Bayes topic develops the three variants and their smoothing; here we take the pair as given and ask what distinguishes its members.


Two estimating equations

Logistic regression fits WW and bb by minimizing the average cross-entropy of pi=softmax⁡(Wxi+b)p_i = \operatorname{softmax}(Wx_i + b), with a ridge penalty on the weights:

F(W,b)=1n∑i=1n[log⁡∑kezik−zi,yi]+λ2∥W∥2,zi=Wxi+b.F(W, b) = \frac{1}{n}\sum_{i=1}^n \Big[\log \sum_k e^{z_{ik}} - z_{i, y_i}\Big] + \frac{\lambda}{2}\lVert W \rVert^2, \qquad z_i = Wx_i + b.

We write yiky_{ik} for the one-hot label, nkn_k for the number of training documents in class kk, and x~i=(xi,1)\tilde x_i = (x_i, 1).

Theorem 1 (Two estimating equations).

(a) FF is convex, with Hessian

1n∑i(diag⁡pi−pipi⊤)⊗x~ix~i⊤  +  λ IK⊗diag⁡(1,…,1,0).\frac{1}{n}\sum_i \big(\operatorname{diag} p_i - p_i p_i^\top\big) \otimes \tilde x_i \tilde x_i^\top \;+\; \lambda\, I_K \otimes \operatorname{diag}(1, \dots, 1, 0).

At λ=0\lambda = 0 its null space contains every 1K⊗v\mathbf{1}_K \otimes v, a subspace of dimension V+1V + 1. At λ>0\lambda \gt 0, when the pip_i are interior, the null space is the single direction that shifts every intercept together.

(b) At any minimizer,

∑i(yik−pik) xij=nλWkjfor all k,j,∑ipik=nkfor all k.\sum_i (y_{ik} - p_{ik})\, x_{ij} = n\lambda W_{kj} \quad \text{for all } k, j, \qquad \sum_i p_{ik} = n_k \quad \text{for all } k.

(c) Multinomial naive Bayes instead sets θ^kj=∑i: yi=kxij/∑i: yi=kLi\hat\theta_{kj} = \sum_{i:\,y_i = k} x_{ij} \big/ \sum_{i:\,y_i = k} L_i, which matches each class’s word totals and in general satisfies neither equation in (b).

Proof.

(a) The function z↦log⁡∑kezkz \mapsto \log\sum_k e^{z_k} is convex with Hessian diag⁡p−pp⊤\operatorname{diag} p - p p^\top, and zi=(IK⊗x~i⊤) θz_i = (I_K \otimes \tilde x_i^\top)\,\theta is linear in θ=(W,b)\theta = (W, b), so the chain rule gives the Kronecker form and each term is positive semidefinite. Since (diag⁡p−pp⊤)1=0(\operatorname{diag} p - p p^\top)\mathbf{1} = 0, every 1⊗v\mathbf{1} \otimes v is in each term’s null space. With λ>0\lambda \gt 0, a direction (U,c)(U, c) has quadratic form at least λ∥U∥2\lambda \lVert U \rVert^2, which vanishes only if U=0U = 0; then the form is 1n∑ic⊤(diag⁡pi−pipi⊤)c\frac1n \sum_i c^\top(\operatorname{diag} p_i - p_i p_i^\top)c, which vanishes only for c∝1c \propto \mathbf{1} when the pip_i are interior.

(b) The gradient is ∂F/∂Wkj=1n∑i(pik−yik)xij+λWkj\partial F / \partial W_{kj} = \frac1n \sum_i (p_{ik} - y_{ik})x_{ij} + \lambda W_{kj} and ∂F/∂bk=1n∑i(pik−yik)\partial F / \partial b_k = \frac1n \sum_i (p_{ik} - y_{ik}). Set both to zero.

(c) Naive Bayes maximizes the joint likelihood ∑i[log⁡πyi+∑jxijlog⁡θyij]\sum_i \big[\log \pi_{y_i} + \sum_j x_{ij}\log \theta_{y_i j}\big] subject to ∑jθkj=1\sum_j \theta_{kj} = 1, and stationarity of the Lagrangian gives the ratio.

∎

The first equation in (b) says that at λ=0\lambda = 0 the fitted model predicts, for every class and every word, exactly as many occurrences of the word in documents of the class as the training data contains. The second says the predicted class totals equal the observed ones, for every λ\lambda: logistic regression is calibrated in the large on its training data. Naive Bayes matches a different set of moments. On the finance unigrams at α=3\alpha = 3 its fitted posteriors miss the neutral and positive class totals by 22.322.3 and 21.921.9 documents out of 1,5001{,}500, and its co-counts by up to 268268. Much of that is the smoothing: at α=0.001\alpha = 0.001 the largest misses are 6.46.4 and 74.874.8. Logistic regression’s class totals hold to 8×10−108 \times 10^{-10}. Its co-counts miss by up to 8.818.81, and that gap is the penalty term nλWn\lambda W of (b) exactly; what vanishes, to 4.5×10−84.5 \times 10^{-8}, is the residual of the penalized equation.

Theorem 1 is the multiclass version of the binary logistic results in the formalStatistics GLM topic (its Theorems 1 and 2). The harness checks the gradient and Hessian against finite differences, the null-space dimensions on random instances (one with more features than documents, since (a) at λ>0\lambda \gt 0 needs no spanning), and agreement with scikit-learn, whose solver stops at an objective no lower than ours.


How fast the generative side learns

Ng and Jordan compare how much data each member of a pair needs to come within ε0\varepsilon_0 of its own asymptotic error. Their generative side is about binary features whose rates are bounded away from 00 and 11. We state it with explicit constants.

Lemma 1 (Uniform concentration of the parameters).

Take VV binary features and suppose each of the two classes has at least m0=⌈ln⁡(4V/δ)/(2ε2)⌉m_0 = \lceil \ln(4V/\delta)/(2\varepsilon^2) \rceil training documents.

(a) Conditional on the labels, with probability at least 1−δ1 - \delta every estimated rate satisfies ∣p^(xj=1∣b)−p(xj=1∣b)∣≤ε\lvert \hat p(x_j = 1 \mid b) - p(x_j = 1 \mid b) \rvert \le \varepsilon.

(b) With m≥ln⁡(4/δ)/(2ε2)m \ge \ln(4/\delta)/(2\varepsilon^2) documents, ∣π^−π∣≤ε\lvert \hat\pi - \pi \rvert \le \varepsilon with probability at least 1−δ1 - \delta; if ρ0≤π≤1−ρ0\rho_0 \le \pi \le 1 - \rho_0, then m≥m0/(ρ0−ε)m \ge m_0 / (\rho_0 - \varepsilon) documents give each class at least m0m_0 on that event.

(c) Add-ll smoothing moves each rate by at most l/mbl / m_b.

Proof.

(a) Each p^jb\hat p_{jb} is a mean of mbm_b independent Bernoulli variables, so Hoeffding’s inequality gives P(∣p^jb−pjb∣>ε)≤2e−2ε2mbP(\lvert \hat p_{jb} - p_{jb} \rvert \gt \varepsilon) \le 2e^{-2\varepsilon^2 m_b}. A union bound over the 2V2V pairs (j,b)(j, b) gives at most 4Ve−2ε2m0≤δ4V e^{-2\varepsilon^2 m_0} \le \delta.

(b) Hoeffding’s inequality for the labels; on its event, mb≥(πb−ε)m≥(ρ0−ε)mm_b \ge (\pi_b - \varepsilon)m \ge (\rho_0 - \varepsilon) m.

(c) ∣(c+l)/(mb+2l)−c/mb∣≤l/mb\big\lvert (c + l)/(m_b + 2l) - c/m_b \big\rvert \le l / m_b for 0≤c≤mb0 \le c \le m_b.

∎

The union bound is where log⁡V\log V comes from: VV parameters cost only log⁡V\log V in the sample size. Ng and Jordan state this as their Lemma 3, with m=O(ε−2log⁡(V/δ))m = O\big(\varepsilon^{-2}\log(V/\delta)\big).

Let l(x)=∑jlog⁡[p(xj∣1)/p(xj∣0)]+log⁡[π/(1−π)]l(x) = \sum_j \log\big[p(x_j \mid 1)/p(x_j \mid 0)\big] + \log\big[\pi/(1 - \pi)\big] be the population discriminant, h∞h_\infty its sign rule, and l^\hat l, h^\hat h the plug-in versions from estimated parameters. For a margin width τ\tau, let

G(τ)=P[(l(x)∈[0,τV]∧y=1)∨(l(x)∈[−τV,0]∧y=0)],G(\tau) = P\big[\big(l(x) \in [0, \tau V] \wedge y = 1\big) \vee \big(l(x) \in [-\tau V, 0] \wedge y = 0\big)\big],

the probability that h∞h_\infty is right but by a margin of at most τV\tau V.

Theorem 2 (The generative error bound).

Suppose every true rate p(xj=1∣b)p(x_j = 1 \mid b), and π\pi, lie in [ρ0,1−ρ0][\rho_0, 1 - \rho_0], and every estimate is within ε≤ρ0/2\varepsilon \le \rho_0/2 of the truth. Then

(a) ∣l^(x)−l(x)∣≤τV\lvert \hat l(x) - l(x) \rvert \le \tau V for every xx, with τV=4(V+1)ε/ρ0\tau V = 4(V + 1)\varepsilon / \rho_0;

(b) err⁡(h^)≤err⁡(h∞)+G(τ)\operatorname{err}(\hat h) \le \operatorname{err}(h_\infty) + G(\tau).

Proof.

(a) Every true probability, for xj=1x_j = 1 and for xj=0x_j = 0, is at least ρ0\rho_0, and every estimate is within ε≤ρ0/2\varepsilon \le \rho_0/2, so both are at least ρ0/2\rho_0/2. By the mean value theorem, ∣log⁡a^−log⁡a∣≤∣a^−a∣/min⁡(a^,a)≤2ε/ρ0\lvert \log \hat a - \log a \rvert \le \lvert \hat a - a \rvert / \min(\hat a, a) \le 2\varepsilon/\rho_0. The discriminant has 2V2V feature log terms and 22 prior log terms, so ∣l^−l∣≤(2V+2)(2ε/ρ0)\lvert \hat l - l \rvert \le (2V + 2)(2\varepsilon/\rho_0).

(b) If h^\hat h errs on a document that h∞h_\infty gets right, the two discriminants have opposite signs, so ∣l(x)∣≤∣l^(x)−l(x)∣≤τV\lvert l(x) \rvert \le \lvert \hat l(x) - l(x) \rvert \le \tau V, and h∞h_\infty was right: that is the event that GG measures.

∎

With Lemma 1, ε=O(log⁡(V/δ)/m)\varepsilon = O\big(\sqrt{\log(V/\delta)/m}\big), so τ=O(log⁡V/m)\tau = O\big(\sqrt{\log V / m}\big), which is Ng and Jordan’s Theorem 4. If G(τ)≤ε0/2+F(τ)G(\tau) \le \varepsilon_0/2 + F(\tau) for some FF with F(τ)→0F(\tau) \to 0, then m=O(log⁡V)m = O(\log V) documents bring naive Bayes within ε0\varepsilon_0 of its asymptotic error (their Corollary 6). The discriminative side we cite. Half-spaces in RV\mathbb{R}^V have VC dimension V+1V + 1 (formalML VC-dimension topic, Theorem 6), which gives their Proposition 2: the discriminative classifier comes within O((V/m)log⁡(m/V))O\big(\sqrt{(V/m)\log(m/V)}\big) of its asymptotic error, and needs m=Ω(V)m = \Omega(V) documents in the worst case. Their Proposition 1 adds that the discriminative classifier’s asymptotic error is never worse. Both concern the classifier that minimizes training 0-1 error over half-spaces; logistic regression’s MLE is a convex stand-in for it.

How loose is it? The harness builds a world where Bernoulli naive Bayes is exactly true: V=200V = 200 features, rates clipped to [0.1,0.9][0.1, 0.9], ε=0.05\varepsilon = 0.05, δ=0.2\delta = 0.2. Lemma 1 asks for m0=1,659m_0 = 1{,}659 documents per class. In 4040 draws of that size no parameter was ever off by more than ε\varepsilon, and the largest deviation’s 0.80.8 quantile was 0.0400.040. Theorem 2’s margin is τV=402\tau V = 402 nats. The largest gap between l^\hat l and ll on any test document in any draw was 2.152.15 nats, and the median ∣l∣\lvert l \rvert is 1.631.63. With a margin of 402402, G(τ)G(\tau) is every correctly classified document, and the bound says nothing. A margin of one nat would take 2.7×1082.7 \times 10^8 documents per class. Most of the slack is in the size of each term, not in their signs. The proof charges every log term 2ε/ρ0=12\varepsilon/\rho_0 = 1 nat, while the real terms have median 0.0130.013 and maximum 0.170.17. With those sizes and every sign aligned, the worst test document reaches 8.08.0 nats, a factor of 5050 below 402402, and the worst document anyone could build reaches 12.312.3; letting the signs cancel takes off a further factor of 4.44.4. The sample size it predicts grows the right way in VV, but the constants make it vacuous at any realistic size.


Multinomial counts and the Zipf tail

Theorem 2 needs every rate to be at least ρ0\rho_0: Lemma 1 has no such hypothesis, but Theorem 2’s log terms blow up near zero. A multinomial vocabulary cannot have that: its rates sum to 11, so some word has θj≤1/V\theta_j \le 1/V, and under Zipf’s law most words are far rarer.

Theorem 3 (Relative accuracy costs the rarest word).

Let a class’s NN training tokens be independent draws from θ∈ΔV\theta \in \Delta^V, and let θ^=C/N\hat\theta = C/N be the empirical rates. Fix ε∈(0,1/2]\varepsilon \in (0, 1/2].

(a) If N≥3ln⁡(2V/δ)/(ε2θmin⁡)N \ge 3\ln(2V/\delta)/(\varepsilon^2 \theta_{\min}), then with probability at least 1−δ1 - \delta every ∣θ^j/θj−1∣≤ε\lvert \hat\theta_j / \theta_j - 1 \rvert \le \varepsilon. Then every ∣log⁡θ^j−log⁡θj∣≤2ε\lvert \log\hat\theta_j - \log\theta_j \rvert \le 2\varepsilon. If each of two classes has that many tokens, then with probability at least 1−2δ1 - 2\delta a document’s discriminant moves by at most 4εL4\varepsilon L.

(b) If N≤1/(2θmin⁡)N \le 1/(2\theta_{\min}), the rarest word is unseen with probability at least 1/21/2, and its relative error is 11. So relative accuracy for every word needs more than 1/(2θmin⁡)≥V/21/(2\theta_{\min}) \ge V/2 tokens.

(c) Under Zipf(ss), θmin⁡=V−s/HV,s\theta_{\min} = V^{-s}/H_{V,s}, so (a) asks for a number of tokens that grows like VsHV,slog⁡VV^s H_{V,s} \log V: polynomially in VV, not logarithmically.

Proof.

(a) The multiplicative Chernoff bound gives P(∣Cj−Nθj∣≥εNθj)≤2e−ε2Nθj/3P\big(\lvert C_j - N\theta_j \rvert \ge \varepsilon N\theta_j\big) \le 2e^{-\varepsilon^2 N\theta_j/3}, and a union bound over VV words with θj≥θmin⁡\theta_j \ge \theta_{\min} gives at most 2Ve−ε2Nθmin⁡/3≤δ2V e^{-\varepsilon^2 N \theta_{\min}/3} \le \delta. For ε≤1/2\varepsilon \le 1/2, −log⁡(1−ε)≤ε/(1−ε)≤2ε-\log(1 - \varepsilon) \le \varepsilon/(1 - \varepsilon) \le 2\varepsilon and log⁡(1+ε)≤ε\log(1 + \varepsilon) \le \varepsilon. A union bound over the two classes’ events gives 1−2δ1 - 2\delta, and on both the discriminant ∑jxj(log⁡θ^1j−log⁡θ^0j)\sum_j x_j(\log\hat\theta_{1j} - \log\hat\theta_{0j}) moves by at most ∑jxj⋅4ε=4εL\sum_j x_j \cdot 4\varepsilon = 4\varepsilon L.

(b) P(Cmin⁡=0)=(1−θmin⁡)N≥1−Nθmin⁡≥1/2P(C_{\min} = 0) = (1 - \theta_{\min})^N \ge 1 - N\theta_{\min} \ge 1/2.

(c) Zipf(ss) gives word jj probability j−s/HV,sj^{-s}/H_{V,s}, the law of the Zipf topic, and the last word has the smallest.

∎

In the long-tailed world of the next section (V=3,071V = 3{,}071 words, rates in proportion to the corpus’s), θmin⁡\theta_{\min} is 4.8×10−64.8 \times 10^{-6}, and part (a) at ε=1/2\varepsilon = 1/2, δ=0.1\delta = 0.1 asks for 27.727.7 million tokens per class. A uniform vocabulary of the same size would need 0.410.41 million, and a pure Zipf(1.1) vocabulary 5.55.5 million. Training on 4,0964{,}096 documents supplies 0.440.44 million tokens per class and leaves about 6767 words never seen.

The bound is sufficient, not necessary, because rare words are rare in test documents too. The share of test tokens whose word occurred fewer than five times in training falls from 0.1040.104 at 6464 training documents to 0.0280.028 at 512512 and 0.00180.0018 at 4,0964{,}096. What the long tail does change is the smoothing. When the smoothing strength α\alpha is chosen on validation data, its median choice in the 3,0713{,}071-word world rises unevenly, from under 11 through n=64n = 64 to 2020 at n=4,096n = 4{,}096, on a grid that stops at 3030, while in a 101101-word world it stays between 0.10.1 and 0.650.65. More data does not mean more words seen once. Pooled across classes, words seen exactly once peak near 650650 at n=256n = 256 and are down to about 3030 at n=4,096n = 4{,}096; within one class they peak near 600600 at n=1,024n = 1{,}024 and fall to 160160, while α\alpha is still rising. What changes is what shrinkage costs. At n=256n = 256, raising α\alpha from 11 to 1010 gains 0.0630.063 of validation accuracy by shrinking the noise words, and loses 0.0520.052 by flattening the informative words and 0.1030.103 because αV\alpha V pseudo-tokens turn a small difference between the classes’ token totals into a bias that grows with a document’s length. At n=4,096n = 4{,}096 those costs are 0.0060.006 and 0.0020.002, and the gain is still 0.0240.024.


Burstiness and the crossover

Ng and Jordan’s two results predict two regimes. Naive Bayes approaches its asymptote faster, so it should lead early. Logistic regression has the better asymptote, so it should lead late if the two asymptotes differ. When naive Bayes is the true model they do not differ, and the prediction is no crossover at all.

Real words also come in bursts: a filing that mentions “impairment” once tends to mention it again. (The synthetic corpus does not do this: “impairment” occurs 3030 times in 2929 of its 1,5001{,}500 training documents. So we build bursts into simulated worlds.) Model it in the simplest way: draw an underlying count vector uu from naive Bayes, and observe x=r⊙ux = r \odot u, where each word’s occurrences repeat rj≥1r_j \ge 1 times.

Proposition 2 (Bursts keep logistic regression right and can move naive Bayes's asymptote).

Let u∼Multinomial⁡(L,θy)u \sim \operatorname{Multinomial}(L, \theta_y) with LL independent of yy, and observe x=r⊙ux = r \odot u for fixed rj≥1r_j \ge 1.

(a) P(y∣x)=softmax⁡y(∑j(xj/rj)log⁡θyj+log⁡πy)P(y \mid x) = \operatorname{softmax}_y\big(\sum_j (x_j/r_j)\log\theta_{yj} + \log\pi_y\big), which is linear in xx. Softmax logistic regression contains the true conditional, and its population error is the Bayes error.

(b) Multinomial naive Bayes fit to infinitely many such documents converges to θ~k=r⊙θk/Zk\tilde\theta_k = r \odot \theta_k / Z_k with Zk=∑jrjθkjZ_k = \sum_j r_j\theta_{kj}. Its weight per observed count is log⁡(θ1j/θ0j)+log⁡(Z0/Z1)\log(\theta_{1j}/\theta_{0j}) + \log(Z_0/Z_1): per underlying occurrence, rjr_j times the Bayes weight, plus a term in the length. Its asymptotic error is generally above the Bayes error.

(c) If rj=cr_j = c for every word with θ1j≠θ0j\theta_{1j} \ne \theta_{0j}, then Z0=Z1Z_0 = Z_1, and naive Bayes’s limiting weights are cc times the Bayes weights per observed count. With π=1/2\pi = 1/2 its limiting rule is the Bayes rule, whatever rr does on the other words.

Proof.

(a) u=x/ru = x / r is a function of xx, so P(y∣x)=P(y∣u)P(y \mid x) = P(y \mid u), which Proposition 1(a) writes as a softmax of ∑jujlog⁡θyj+log⁡πy\sum_j u_j \log\theta_{yj} + \log\pi_y.

(b) By the law of large numbers, θ^kj→E[xj∣k]/E[L(x)∣k]=rjθkjE[L]/(E[L] Zk)\hat\theta_{kj} \to \mathbb{E}[x_j \mid k] / \mathbb{E}[L(x) \mid k] = r_j \theta_{kj}\mathbb{E}[L] / (\mathbb{E}[L]\, Z_k).

(c) Let SS be the set of words with unequal rates. Since ∑j∈Sθkj=1−∑j∉Sθkj\sum_{j \in S} \theta_{kj} = 1 - \sum_{j \notin S} \theta_{kj} is the same for both classes, so is Zk=c∑j∈Sθkj+∑j∉SrjθkjZ_k = c\sum_{j \in S} \theta_{kj} + \sum_{j \notin S} r_j\theta_{kj}. The weight in (b) is then log⁡(θ1j/θ0j)\log(\theta_{1j}/\theta_{0j}), which is 00 off SS and cc times the weight (1/rj)log⁡(θ1j/θ0j)(1/r_j)\log(\theta_{1j}/\theta_{0j}) of (a) on SS. With π=1/2\pi = 1/2 neither discriminant has a prior term, so one is cc times the other and they have the same sign.

∎

The harness builds four worlds from the corpus. In each, 7171 informative words (the sentiment lexicon, the direction words, and “not”) have class-dependent rates taken from the corpus and hold 6.2%6.2\% of every document. The rest of each document is spread over uninformative words in proportion to their corpus frequencies: 3030 of them (V=101V = 101) or 3,0003{,}000 (V=3,071V = 3{,}071). In the bursty worlds a fixed half of the words repeat four times, including 3434 of the 7171 informative ones. All four worlds have one Bayes accuracy, 0.8540.854 on a million documents: the bursts are a deterministic relabeling, and words whose rates do not depend on the class cannot change it. Naive Bayes’s asymptote in the bursty worlds is 0.8160.816 at V=101V = 101 and 0.8150.815 at V=3,071V = 3{,}071. Each method’s hyperparameter, the smoothing α\alpha for naive Bayes and the penalty λ\lambda for logistic regression, is chosen for every fit on a fresh validation draw, and each curve averages four nested training draws.

  • Naive Bayes true, V=101V = 101: naive Bayes leads at every training size and reaches 0.8520.852 at 4,0964{,}096 documents, against 0.8480.848. No crossover.
  • Naive Bayes true, V=3,071V = 3{,}071: again no crossover; at 4,0964{,}096 documents naive Bayes has 0.8310.831 and logistic regression 0.8110.811.
  • Bursty, V=101V = 101: naive Bayes leads until 512512 documents, and logistic regression leads from there on, finishing at 0.8490.849, past naive Bayes’s asymptote; naive Bayes finishes at 0.8170.817.
  • Bursty, V=3,071V = 3{,}071: the crossover moves out to 2,0482{,}048 documents, and at 4,0964{,}096 logistic regression has 0.7900.790 and naive Bayes 0.7720.772.

Do the two regimes appear exactly where the asymptotic errors differ? No. Proposition 2(c) says overcounting alone costs nothing. Repeat every word four times, or all 7171 informative words and nothing else, and naive Bayes’s asymptote stays at the Bayes accuracy, because every weight that matters scales by one factor. That needs balanced classes: at π=0.3\pi = 0.3 the score scales and the prior does not, and uniform bursts cost 0.0100.010. What costs the asymptote is unevenness among informative words. Let every other informative word burst and nothing else, so that some signal words get four votes and others one, and the asymptote falls to 0.8090.809.

The crossover does not need that. Let only the noise words burst. Their log ratios are zero, so by (c) naive Bayes’s asymptote is exactly the Bayes accuracy, and there is no gap. Logistic regression overtakes it anyway, winning all four draws from n=256n = 256 at V=101V = 101 and from n=1,024n = 1{,}024 at V=3,071V = 3{,}071, sooner than the 1,0241{,}024 and 4,0964{,}096 of the half-informative worlds, where the gap exists. A noise word’s fitted weight is small and wrong, and a burst gives it four votes at test time. Naive Bayes’s weight is a log ratio of rates and cannot see the four: at n=256n = 256 the noise words’ part of its score grows fourfold, from a standard deviation of 0.730.73 to 2.922.92. Logistic regression refits. By Proposition 3(a) with r=4r = 4, repeating a word four times divides its weight by about four (3.733.73, 3.963.96, 3.993.99 at λ=0.1\lambda = 0.1, 0.010.01, 0.0010.001, and exactly 44 at λ=0\lambda = 0), so that part of its score barely moves, from 1.211.21 to 1.271.27. At V=101V = 101 the two meet again by n=32,768n = 32{,}768, as equal asymptotes predict; at V=3,071V = 3{,}071 logistic regression still wins all four draws there.

So a gap between asymptotes is one way to get Ng and Jordan’s two regimes, not the only one. The other is a difference in how each method pays for noise it has not yet averaged out. Choose the world and the vocabulary in the laboratory.

A · Crossover — naive Bayes leads early; where words come in bursts, logistic regression overtakes it
512
test accuracy against training documents (mean of 4 draws, band = min to max)0.50.60.70.80.981632641282565121k2k4ktraining documents n (log scale)
naive Bayeslogistic regressionBayes accuracy
naive Bayes at n = 512
0.832
logistic regression at n = 512
0.795
Bayes accuracy
0.854
naive Bayes asymptote
0.854
crossover n
none

When naive Bayes is the true model, its asymptote is the Bayes accuracy and it leads at every n shown. When half the words come in bursts of four, logistic regression stays well specified. Naive Bayes loses asymptote because the bursts hit some informative words and not others (Proposition 2), and its noise words get four votes each until it has averaged them out. A gap in asymptotes is not required: on the page, bursts on noise words alone produce a crossover with no gap at all.


Separation

The finance corpus has 1,5001{,}500 training documents and 3,2463{,}246 unigram features, 17,70617{,}706 with bigrams. With more features than documents, a hyperplane can usually separate the training classes perfectly, on either feature set: the unigram count matrix has rank 1,5001{,}500. That changes what logistic regression’s fit means. We use the homogeneous model here (no intercept), write zi=sixiz_i = s_i x_i for a document times its label si∈{±1}s_i \in \{\pm 1\}, and let

ℓ(w)=1n∑ilog⁡(1+e−zi⋅w),Fλ(w)=ℓ(w)+λ2∥w∥2.\ell(w) = \frac{1}{n}\sum_i \log\big(1 + e^{-z_i \cdot w}\big), \qquad F_\lambda(w) = \ell(w) + \frac{\lambda}{2}\lVert w \rVert^2 .

Theorem 4 (Separation, the missing MLE, and the max-margin limit).

(a) If some wˉ\bar w has zi⋅wˉ>0z_i \cdot \bar w \gt 0 for every ii, then inf⁡ℓ=0\inf \ell = 0 and no ww attains it; ∇ℓ(w)⋅wˉ<0\nabla\ell(w)\cdot\bar w \lt 0 at every ww, so ℓ\ell has no stationary point. For λ>0\lambda \gt 0, FλF_\lambda has a unique minimizer w^λ\hat w_\lambda.

(b) If the xix_i are linearly independent, which needs n≤Vn \le V, every labeling is separable.

(c) Let w^=arg⁡min⁡∥w∥2\hat w = \arg\min \lVert w \rVert^2 subject to zi⋅w≥1z_i \cdot w \ge 1 for all ii, the hard-margin SVM, which is unique, and let u∗=w^/∥w^∥u^* = \hat w / \lVert \hat w \rVert and γ∗=1/∥w^∥\gamma^* = 1/\lVert \hat w \rVert. As λ→0\lambda \to 0, ∥w^λ∥→∞\lVert \hat w_\lambda \rVert \to \infty, min⁡izi⋅w^λ/∥w^λ∥→γ∗\min_i z_i \cdot \hat w_\lambda / \lVert \hat w_\lambda \rVert \to \gamma^*, and w^λ/∥w^λ∥→u∗\hat w_\lambda / \lVert \hat w_\lambda \rVert \to u^*.

Proof.

(a) Along twˉt\bar w every margin grows, so ℓ(twˉ)→0\ell(t\bar w) \to 0 as t→∞t \to \infty, while ℓ>0\ell \gt 0 everywhere. The directional derivative ∇ℓ(w)⋅wˉ=−1n∑iσ(−zi⋅w) zi⋅wˉ\nabla\ell(w)\cdot\bar w = -\frac1n\sum_i \sigma(-z_i\cdot w)\, z_i\cdot\bar w is negative. FλF_\lambda is strictly convex and grows without bound, so it has exactly one minimizer.

(b) The map w↦Xww \mapsto Xw from RV\mathbb{R}^V onto Rn\mathbb{R}^n is surjective, so Xw=sXw = s has a solution for any sign vector ss, and then zi⋅w=si2=1z_i \cdot w = s_i^2 = 1.

(c) Write w^λ=ρλuλ\hat w_\lambda = \rho_\lambda u_\lambda with ∥uλ∥=1\lVert u_\lambda \rVert = 1, and γλ=min⁡izi⋅uλ\gamma_\lambda = \min_i z_i \cdot u_\lambda.

The norm grows without bound. Fix RR. On the ball ∥w∥≤R\lVert w \rVert \le R, Fλ≥cR=min⁡∥w∥≤Rℓ(w)>0F_\lambda \ge c_R = \min_{\lVert w \rVert \le R} \ell(w) \gt 0. Since log⁡(1+e−m)≤e−m\log(1 + e^{-m}) \le e^{-m}, Fλ(R′u∗)≤e−R′γ∗+λR′2/2F_\lambda(R' u^*) \le e^{-R'\gamma^*} + \lambda R'^2/2, which is below cRc_R for R′R' large and then λ\lambda small. So eventually w^λ\hat w_\lambda lies outside the ball.

Compare at equal norm. Fλ(w^λ)≤Fλ(ρλu∗)F_\lambda(\hat w_\lambda) \le F_\lambda(\rho_\lambda u^*) and the penalties are equal, so ℓ(ρλuλ)≤ℓ(ρλu∗)≤e−ρλγ∗\ell(\rho_\lambda u_\lambda) \le \ell(\rho_\lambda u^*) \le e^{-\rho_\lambda \gamma^*}.

The margin is eventually positive. If γλ<0\gamma_\lambda \lt 0, one term of ℓ(ρλuλ)\ell(\rho_\lambda u_\lambda) is at least log⁡2\log 2, so ℓ≥log⁡2/n\ell \ge \log 2 / n, which exceeds e−ρλγ∗e^{-\rho_\lambda\gamma^*} once ρλ\rho_\lambda is large.

The margin approaches the maximum. For m≥0m \ge 0, log⁡(1+e−m)≥e−m/2\log(1 + e^{-m}) \ge e^{-m}/2, so 12ne−ρλγλ≤e−ρλγ∗\frac{1}{2n}e^{-\rho_\lambda\gamma_\lambda} \le e^{-\rho_\lambda\gamma^*}, which gives γλ≥γ∗−ln⁡(2n)/ρλ→γ∗\gamma_\lambda \ge \gamma^* - \ln(2n)/\rho_\lambda \to \gamma^*.

The direction converges. Every limit point uˉ\bar u of uλu_\lambda on the unit sphere has min⁡izi⋅uˉ≥γ∗\min_i z_i \cdot \bar u \ge \gamma^*, so it is a max-margin direction, and by uniqueness uˉ=u∗\bar u = u^*. A bounded sequence whose every convergent subsequence has the same limit converges to it.

∎

Part (c) is Rosset, Zhu, and Hastie’s (2004) Theorem 2.1 for the logistic loss with the squared L2L_2 penalty; their JMLR paper proves the same limit along the constrained path ∥w∥≤c\lVert w \rVert \le c. Albert and Anderson (1984) classify when the MLE exists; part (a) is the complete-separation case. Cover’s (1965) function-counting theorem gives (b) its exact form: nn points in general position in Rd\mathbb{R}^d admit all 2n2^n homogeneous labelings exactly when n≤dn \le d.

The harness takes the 200200 polar tone documents of a 300300-document corpus, with 2,7382{,}738 words. Their count vectors have full rank, so every labeling is separable, and a linear program confirms it for the true labels and three random ones. The SVM normalized margin is γ∗=0.692\gamma^* = 0.692. As λ\lambda falls from 10−110^{-1} to 10−710^{-7} the norm ∥w^λ∥\lVert \hat w_\lambda \rVert runs from 1.5411.541 to 18.00518.005, the cosine to the SVM direction rises from 0.9300.930 to 0.9980.998, and the normalized margin from 0.0320.032 to 0.6450.645. The last decade of λ\lambda adds 3.0473.047 to the norm, near ln⁡10/γ∗=3.326\ln 10 / \gamma^* = 3.326, as ∥w^λ∥≈ln⁡(1/λ)/γ∗\lVert \hat w_\lambda \rVert \approx \ln(1/\lambda)/\gamma^* would predict.

Unregularized gradient descent does the same thing with time in place of λ\lambda. Soudry and colleagues (2018, Theorems 3 and 5) prove that on separable data, from any start and with a small enough step, w(t)w(t) grows like w^log⁡t\hat w \log t and its direction converges to the max-margin direction, at rate O(log⁡log⁡t/log⁡t)O(\log\log t / \log t). From w=0w = 0 on the tone documents, the cosine to the SVM direction is 0.5910.591 after 1010 steps, 0.9060.906 after 1,0001{,}000, and 0.9870.987 after 100,000100{,}000, while the loss falls to 0.00250.0025. The laboratory plots both paths. A logistic regression on separable text that reports its weights is reporting how long it ran; only the direction is a fact about the data, and the margin bound of the formalML generalization topic (Theorem 6) is why that direction is a good one.


Naive Bayes features as a prior

Wang and Manning (2012) reuse naive Bayes inside a discriminative model. Their log-count ratio for a binary task is

r=log⁡p/∥p∥1q/∥q∥1,p=α+∑i: yi=1fi,q=α+∑i: yi=0fi,r = \log\frac{p / \lVert p \rVert_1}{q / \lVert q \rVert_1}, \qquad p = \alpha + \sum_{i:\,y_i = 1} f_i, \quad q = \alpha + \sum_{i:\,y_i = 0} f_i,

naive Bayes’s weight vector for features ff, and their NBSVM fits a linear model to r⊙fr \odot f.

Proposition 3 (Naive Bayes weights as a prior).

(a) Let every rj≠0r_j \ne 0. Logistic regression on the features r⊙xr \odot x with penalty λ2∥v∥2\frac{\lambda}{2}\lVert v \rVert^2 has the same decision function as logistic regression on xx with penalty λ2∑jwj2/rj2\frac{\lambda}{2}\sum_j w_j^2 / r_j^2, through w=r⊙vw = r \odot v.

(b) For raw counts with class totals N1N_1, N0N_0, and word totals cj=c0j+c1jc_j = c_{0j} + c_{1j},

∣rj∣≤ln⁡cj+αα+∣ln⁡N1+αVN0+αV∣.\lvert r_j \rvert \le \ln\frac{c_j + \alpha}{\alpha} + \Big\lvert \ln\frac{N_1 + \alpha V}{N_0 + \alpha V} \Big\rvert .
Proof.

(a) The substitution w=r⊙vw = r \odot v is a bijection that leaves every score x⋅w=(r⊙x)⋅vx \cdot w = (r \odot x)\cdot v unchanged and sends ∥v∥2\lVert v \rVert^2 to ∑jwj2/rj2\sum_j w_j^2/r_j^2.

(b) rj=ln⁡c1j+αc0j+α+ln⁡N0+αVN1+αVr_j = \ln\frac{c_{1j} + \alpha}{c_{0j} + \alpha} + \ln\frac{N_0 + \alpha V}{N_1 + \alpha V}, and since 0≤ckj≤cj0 \le c_{kj} \le c_j, the first ratio lies between α/(cj+α)\alpha / (c_j + \alpha) and (cj+α)/α(c_j + \alpha)/\alpha.

∎

Part (a) is a Gaussian prior with a different variance for each word, set by naive Bayes: FF averages its loss, so against the summed log likelihood the penalty is a prior of variance rj2/(nλ)r_j^2/(n\lambda) (formalStatistics regularization topic). A word that naive Bayes finds uninformative gets a tight prior around zero, and a word it finds strongly polar is free to move. Part (b) is the funnel in the laboratory. On the 1,0001{,}000 polar training documents (6,3076{,}307 words), the most extreme log ratio of any word seen once is 2.4082.408 at α=0.1\alpha = 0.1, 0.7030.703 at α=1\alpha = 1, and 0.1020.102 at α=10\alpha = 10, each exactly the bound. Smoothing caps what a rare word can say, and under Zipf most words are rare. Add-α\alpha smoothing is a posterior mean under a symmetric Dirichlet prior (formalStatistics multivariate topic, Theorem 6). The formalRAG query-likelihood topic smooths toward a background distribution instead.


The aspect exclusive-or

The finance corpus writes its tone into 55 to 88 aspect sentences per document. Each sentence names an aspect (revenue, margins, demand, liquidity, costs, taxes, liabilities) and says what happened to it. For revenue, up is good news; for costs, up is bad.

Proposition 4 (Valence times direction is not linear in unigrams).

On the four one-clause documents “revenue rose” (positive), “revenue fell” (negative), “costs rose” (negative), and “costs fell” (positive), no (w,b)(w, b) separates the labels using unigram counts. Bigram indicators separate them.

Proof.

Suppose (w,b)(w, b) separates them. The positive documents give wrevenue+wrose+b>0w_{\text{revenue}} + w_{\text{rose}} + b \gt 0 and wcosts+wfell+b>0w_{\text{costs}} + w_{\text{fell}} + b \gt 0, whose sum is S=wrevenue+wcosts+wrose+wfell+2b>0S = w_{\text{revenue}} + w_{\text{costs}} + w_{\text{rose}} + w_{\text{fell}} + 2b \gt 0. The negative documents give wrevenue+wfell+b<0w_{\text{revenue}} + w_{\text{fell}} + b \lt 0 and wcosts+wrose+b<0w_{\text{costs}} + w_{\text{rose}} + b \lt 0, whose sum is the same S<0S \lt 0. With bigram indicators, weight +1+1 on “revenue rose” and “costs fell” and −1-1 on the other two separates them.

∎

This is the exclusive-or that a single hyperplane cannot draw, and the corpus shows its signature. In the unigram fit, logistic regression gives “rose” a weight of 0.0600.060 and “fell” 0.0240.024 (positive minus negative). That is noise, not zero: the weights have standard deviation 0.0920.092 and median magnitude 0.0310.031, which puts “rose” in about the top quarter, but no direction word gets past it and their signs follow no pattern, because the words cancel across aspects. The bigram fit puts the sign on the pairs:

bigramweight
costs rose−0.355-0.355
costs fell0.5260.526
taxes rose−0.351-0.351
sales rose0.2650.265
revenue rose0.0010.001
not strong−0.199-0.199
not weak0.3360.336

“Revenue rose” is the row that fails, and instructively: 4545 of its 7171 training occurrences sit inside “cost of revenue rose,” an exclusive-or one word wider than a bigram. Negation is the same kind of interaction: “not” flips the adjective after it, and “not weak” is positive.



Where this sits

Naive Bayes for text goes back decades, and its independence assumption has been examined from many sides: Lewis (1998) surveys its history in retrieval, Domingos and Pazzani (1997) explain why it can classify well with badly wrong probabilities, McCallum and Nigam (1998) compare the multinomial and Bernoulli event models, and Rennie and colleagues (2003) repair several of its biases, including burstiness, with complement classes and log term frequencies. Joachims (1998) showed that text is the high-dimensional, nearly separable setting where margin methods do well, the geometry of Theorem 4. The formalML naive-Bayes topic covers Gaussian naive Bayes, Ng and Jordan for that case, and the Platt and isotonic repairs of its probabilities.

The next topic replaces learned weights with a fixed dictionary, Loughran and McDonald’s (2011), and asks what a lexicon gets wrong about financial words. The same log-linear model, lifted from documents to label sequences, is the linear-chain conditional random field of the sequence-labeling topic. Whether a 0.030.03 accuracy gap between two of these classifiers is real is a question for the significance-testing topic. And a fine-tuned encoder replaces the bag of words with a representation that can see “costs rose” as a phrase.


Honest caveats


Implementation

The companion notebook (notebookPath) imports linear_text_classifiers.py, which owns every number on this page. That module imports the Zipf topic’s module for Zipf’s law and the shared finance corpus through it. It implements multinomial and Bernoulli naive Bayes in Proposition 1’s closed form, softmax and binary logistic regression by trust-region Newton–CG with exact Hessian-vector products, the hard-margin SVM through its dual, the simulated worlds with common random numbers, and the finance classifiers. A harness of 4545 assertion-based tests runs in about four minutes on a laptop CPU, and --slow adds two checks that fit on up to 50,00050{,}000 documents, about ten minutes more. The tests cover:

  • Proposition 1 against the literal Bayes rule and scikit-learn’s naive Bayes, and the length counterexample.
  • Theorem 1’s gradient, Hessian, and null spaces against finite differences, its estimating equations at the fitted optimum, and scikit-learn’s logistic regression.
  • Lemma 1 and Theorem 2 in a Bernoulli world that meets their hypotheses, including how loose the bound is; Theorem 3 in the long-tailed world, with a Monte Carlo check of its Chernoff step.
  • Proposition 2’s identity and its balanced case, the one Bayes accuracy, who bursts, and the learning curves’ regimes in all four worlds; words seen once and what α\alpha pays for.
  • Theorem 4’s rank, separability by linear programming, the missing stationary point, and the monotone path and gradient-descent directions.
  • Proposition 3’s equivalence and bound, Proposition 4 by linear programming and on the corpus, and the finance tables, ceiling, prefixes, and label noise.
  • The case study’s explanations: six splits, wide grids, vocabulary cuts, redrawn words, exact readers, the bigram decomposition, and every phrase the page quotes from the corpus.

The laboratory reads its data from a JSON file that one of those tests requires to equal the module’s viz_payload() exactly, so the figure cannot drift from the code.

References & Further Reading