intermediate embedding-geometry 30 min read

Chunking as a Segmentation and Optimization Problem

Where to cut a document, posed as a coherence-maximizing segmentation with an exact dynamic-programming optimum — and the proxy it secretly optimizes

Overview & motivation

Every retrieval pipeline begins by cutting documents into chunks, and the standard recipe — split every 512512 tokens — is the one step in the stack with no mathematics behind it. Yet the choice matters: a boundary dropped in the middle of an argument splits the evidence a query needs across two chunks, and a chunk that spans two topics dilutes the embedding of both. The question where to cut deserves to be posed precisely, and when it is, it turns out to be a clean optimization problem with an exact solution.

The framing is segmentation. Represent the document as a sequence of sentence embeddings e1,,ene_1, \dots, e_n, L2-normalized so they live on the unit sphere, and choose boundaries that maximize within-chunk coherence — keep each chunk’s sentences pointing the same way. We will see that this coherence is exactly the mean resultant length of the chunk, the von Mises–Fisher concentration statistic, so an optimal chunk is a tight cluster on the sphere; that the resulting additive objective has a globally optimal O(n2)O(n^2) dynamic-programming solution; and that the heuristics in common use — TextTiling and fixed-size splitting — cannot beat it. Then the honest part: the coherence we optimize is a proxy, and optimizing it past the true number of sections makes the chunks more coherent and the segmentation worse.

Before the algebra, drag the chunk-count slider and watch the optimal boundaries find the topic shifts:

sentences (colored by topic) · dashed = true boundariesgap dissimilarity · accent = DP optimum · dashed orange = greedy
The document as a sentence strip colored by its planted topic, with the adjacent-gap dissimilarity below. At the true k = 5 the DP-optimal boundaries (accent) land exactly on the topic shifts; the greedy boundaries (dashed orange) chase local dissimilarity peaks and the fixed-size cuts ignore meaning.
DP boundary F1
100%
greedy boundary F1
75%
true # sections
5

The first panel is the document as a colored strip with the adjacent-gap dissimilarity beneath it; at the true count of five sections the dynamic program lands its boundaries exactly on the topic shifts, while fixed-size cuts fall wherever the arithmetic puts them. The second panel is the catch — coherence cost falls forever as you add chunks, but boundary-recovery accuracy peaks at the true count and then declines. The third compares the methods head to head.

What we cover

  1. Why within-chunk coherence is the mean resultant length, and the closed form it gives.
  2. The optimal segmentation as a dynamic program, proved optimal.
  3. TextTiling and fixed-size chunking, and the gap to the optimum.
  4. The proxy problem: coherence is not retrieval quality.
  5. A finance case study on a synthetic 10-K filing.

Coherence is the mean resultant length

Fix a candidate chunk — the sentences ei,,ej1e_i, \dots, e_{j-1}, written [i,j)[i, j) — and measure its incoherence by how far its sentences point from the chunk’s average direction. Let μ\mu be the unit vector along the sum t[i,j)et\sum_{t \in [i,j)} e_t. The cost is the total angular spread, t(1et,μ)\sum_t (1 - \langle e_t, \mu\rangle), which is zero when every sentence aligns with μ\mu and grows as they disagree.

Proposition 1 (The chunk cost is its length minus its resultant length).

For L2-normalized embeddings, the chunk cost has the closed form

c(i,j)=t[i,j)(1et,μ)=(ji)t[i,j)et=(ji)(1Rˉ),c(i, j) = \sum_{t \in [i,j)} \bigl(1 - \langle e_t, \mu\rangle\bigr) = (j - i) - \Bigl\lVert \sum_{t \in [i,j)} e_t \Bigr\rVert = (j - i)\,(1 - \bar{R}),

where Rˉ=1jitet\bar{R} = \lVert \frac{1}{j-i}\sum_t e_t\rVert is the mean resultant length of the chunk — the von Mises–Fisher concentration statistic.

Proof.

Because μ=(tet)/tet\mu = \bigl(\sum_t e_t\bigr) / \lVert \sum_t e_t\rVert is the unit vector along the sum, the total alignment is

t[i,j)et,μ=tet, μ=tet, tettet=tet.\sum_{t \in [i,j)} \langle e_t, \mu\rangle = \Bigl\langle \sum_t e_t,\ \mu \Bigr\rangle = \Bigl\langle \sum_t e_t,\ \frac{\sum_t e_t}{\lVert \sum_t e_t\rVert} \Bigr\rangle = \Bigl\lVert \sum_t e_t \Bigr\rVert .

Subtracting from the chunk length jij - i (each of the jij-i unit embeddings contributes 11 to t1\sum_t 1) gives c(i,j)=(ji)tetc(i,j) = (j-i) - \lVert\sum_t e_t\rVert. Dividing the sum by jij-i turns the norm into the mean resultant length Rˉ\bar{R}, so c(i,j)=(ji)(1Rˉ)c(i,j) = (j-i)(1 - \bar{R}). \blacksquare

This is the bridge to the prerequisite topic. There, Rˉ\bar{R} was the maximum-likelihood estimate of a von Mises–Fisher distribution’s concentration κ\kappa: a tight topical cluster has Rˉ\bar{R} near 11, a diffuse one near 00. Here the same quantity is a chunk’s coherence, and minimizing the total cost over a segmentation is maximizing the total resultant length — carving the document into tight vMF clusters on the sphere. The cost is also cheap: with prefix sums of the embeddings, [i,j)et\lVert\sum_{[i,j)} e_t\rVert and hence c(i,j)c(i,j) is computed in time proportional to the embedding dimension, independent of the chunk’s length.


The optimal segmentation is a dynamic program

A segmentation into kk chunks is a choice of k1k-1 internal boundaries, and its total cost is the sum of the per-chunk costs — an additive objective over contiguous pieces. Additivity is exactly the structure dynamic programming exploits.

Theorem 1 (Optimal segmentation by dynamic programming).

Let OPTk(j)\mathrm{OPT}_k(j) be the minimum total cost of segmenting the prefix [0,j)[0, j) into kk contiguous chunks. Then

OPTk(j)=mink1i<j OPTk1(i)+c(i,j),\mathrm{OPT}_k(j) = \min_{k-1 \le i < j}\ \mathrm{OPT}_{k-1}(i) + c(i, j),

and the globally optimal kk-segmentation of the whole document is recovered from OPTk(n)\mathrm{OPT}_k(n) in O(kn2)O(k n^2) time. With a per-chunk penalty in place of a fixed kk, the single recurrence OPT(j)=mini<jOPT(i)+c(i,j)+λ\mathrm{OPT}(j) = \min_{i<j} \mathrm{OPT}(i) + c(i,j) + \lambda runs in O(n2)O(n^2).

Proof.

The argument is optimal substructure. Consider any optimal kk-segmentation of [0,j)[0, j) and let its last chunk be [i,j)[i, j). The remaining chunks form a (k1)(k-1)-segmentation of [0,i)[0, i), and it must itself be optimal: if some other (k1)(k-1)-segmentation of [0,i)[0, i) had strictly smaller cost, swapping it in — the last chunk [i,j)[i,j) is unchanged, so its cost c(i,j)c(i,j) is unchanged — would give a kk-segmentation of [0,j)[0, j) with smaller total cost, contradicting optimality. Hence the optimal cost decomposes as OPTk1(i)+c(i,j)\mathrm{OPT}_{k-1}(i) + c(i,j) for the true split point ii, and since we do not know ii we minimize over all valid ii, which is the recurrence. Filling the table over s=1,,ks = 1, \dots, k and j=1,,nj = 1, \dots, n, each entry a minimization over ii, is O(kn2)O(k n^2); back-pointers reconstruct the boundaries. \blacksquare

The companion harness checks this the only way that settles it: against brute force. For small documents it enumerates every way to place the boundaries, and the dynamic program matches the exhaustive optimum at every kk — it is the global minimum, not a good guess. That is the dividing line between this and the methods practitioners actually use.


What practitioners use, and the gap

Two heuristics dominate real pipelines, and neither optimizes the objective above. Fixed-size chunking cuts every ww tokens; it is semantics-free, and on a document whose sections have uneven lengths — every real document — its boundaries land on the topic shifts only by accident. TextTiling (Hearst 1997) is smarter: it slides a window across the document, scores each gap by the dissimilarity of the text before and after, and places boundaries at the gaps of locally greatest dissimilarity. That greedy, local rule often finds the obvious shifts, but it cannot see the global trade-off the dynamic program optimizes, so at the same number of chunks its total coherence is no better — and usually worse — than the optimum.

On the synthetic document in the laboratory, at the true count of five chunks, the dynamic program places its boundaries exactly on the planted topic shifts (boundary F1=100%F_1 = 100\%), the greedy heuristic recovers three of four (F1=75%F_1 = 75\%), and fixed-size chunking, fooled by the uneven section lengths, recovers two (F1=50%F_1 = 50\%) — while the optimum’s coherence cost is strictly the lowest of the three, as the theorem guarantees. The “Boundary recovery” panel shows the gap; it is the value of optimizing globally rather than locally.


Coherence is a proxy

Here is the catch that keeps this honest, and it is visible the moment you keep splitting. The optimal coherence cost is monotone decreasing in the number of chunks — more boundaries can only reduce within-chunk spread, all the way down to singletons of cost zero. So the objective, taken literally, wants to cut the document into individual sentences. What stops it is that coherence is a proxy for the thing we actually care about, which is whether the chunks are good units of retrieval, and that target is not monotone in the chunk count at all.

The “Granularity tradeoff” panel makes the divergence concrete: as kk grows, the coherence cost slides down forever, but the boundary-recovery F1F_1 rises to a peak at the true number of sections and then falls, because past that point the optimizer is splitting coherent sections into even-more-coherent fragments — lowering the cost while destroying the structure. Minimizing the proxy past the true kk is actively harmful. This is the segmentation analog of every proxy-objective problem in the field: the dynamic program will give you the exact optimum of what you asked for, and what you asked for is not quite what you wanted. The practical consequence is that the penalty λ\lambda, or the chunk count kk, is the load-bearing hyperparameter, and it must be set by the downstream retrieval metric, not by the coherence the algorithm optimizes.


Finance case study


Honest caveats


Implementation

The companion module chunking_as_segmentation.py owns every number this page and the laboratory cite. Its six assertions encode the claims in order: the chunk cost equals the sum of one-minus-cosine to the mean direction, hence the mean resultant length (to the decimal); the dynamic program matches an exhaustive brute-force optimum at every kk; neither the greedy nor the fixed-size heuristic beats the DP’s objective value; the optimal cost is monotone in the chunk count; the DP recovers planted boundaries better than the baselines on a document with uneven sections; and the finance filing. A trap worth flagging, since it shaped the experiment: with equal-length sections, fixed-size chunking hits the boundaries by accident and looks as good as the optimum, so the documents are deliberately uneven — which is also what makes them realistic. Both the module and the narrative notebook run in well under a second and must exit cleanly before the topic ships.

Connections

  • the within-segment coherence this topic minimizes is exactly the mean resultant length developed there — the norm of the average of unit embeddings, the maximum-likelihood concentration estimate of a von Mises-Fisher cluster — so an optimal chunk is a tight vMF cluster on the sphere, and segmentation is the problem of cutting the document where the mean direction shifts hypersphere-vmf-geometry

References & Further Reading