Intuition, Vocabulary and Foundations
A recurrence is a compact predictor. Fibonacci satisfies $F_n=F_{n-1}+F_{n-2}$, so two coefficients replace an arbitrarily long table. Berlekamp–Massey reverses that idea: from a sample prefix it infers the shortest linear recurrence consistent with every observed term. Kitamasa then uses the recurrence to jump to a distant index.
For $s_n=c_1s_{n-1}+\cdots+c_Ls_{n-L}$, the characteristic relation is $X^L=c_1X^{L-1}+\cdots+c_L$. Reducing $X^n$ by this relation expresses $s_n$ as a linear combination of the first $L$ terms. Berlekamp–Massey needs a field because each nonzero discrepancy must be invertible; this is automatic modulo a prime but false in general modulo a composite.
Berlekamp–Massey compresses a trusted prefix into its shortest linear predictor, and Kitamasa reduces $X^n$ modulo that predictor's characteristic polynomial. Field division and independent sample validation are correctness requirements, not optional optimizations.
| Concept | Exact example | Non-example or boundary | Question answered |
|---|---|---|---|
| Linear complexity | Fibonacci has complexity $2$ with coefficients $(1,1)$ | A list of terms alone is not a recurrence proof | What is the shortest matching rule? |
| Connection polynomial | $1-X-X^2$ annihilates Fibonacci shifts | $1-X$ fails on the prefix | How are prediction errors represented? |
| Discrepancy | $d_n=s_n+\sum_{i=1}^L C_i s_{n-i}$ | Zero discrepancy on a short prefix proves nothing about unseen data | Does the current rule predict sample $n$? |
| Characteristic reduction | $X^2\equiv X+1$ for Fibonacci | Ordinary numeric division is unrelated | How is a distant term reduced to initial terms? |
| Field requirement | Every nonzero value modulo prime $p$ is invertible | $2$ has no inverse modulo $6$ | When is every update defined? |
| Sample sufficiency | At least $2L$ exact samples normally identify order $L$ | A short accidental fit may fail later | How much evidence supports inference? |
Step-by-Step Derivations and Algorithms
Berlekamp–Massey maintains a connection polynomial $C$, its length $L$, and a saved polynomial $B$ from the last length increase. At index $n$, compute
$$d=s_n+\sum_{i=1}^{L}C_i s_{n-i}.$$
If $d=0$, the current rule already predicts the sample. Otherwise subtract a shifted copy of $B$ scaled by $d/b$, where $b$ is the saved nonzero discrepancy. This cancels the current error. When $2L\le n$, minimality forces the new feasible length $n+1-L$ and the old $C$ becomes the backup.
Kitamasa computes $X^n$ in the quotient by $X^L-c_1X^{L-1}-\cdots-c_L$. Binary exponentiation repeatedly multiplies coefficient vectors, then reduces degrees from $2L-2$ down to $L$. If $X^n\equiv\sum_{i=0}^{L-1}a_iX^i$, then $s_n=\sum a_i s_i$.
flowchart TD
A[Normalize field-valued samples] --> B[Compute discrepancies]
B --> C{Discrepancy zero?}
C -->|Yes| D[Keep connection polynomial]
C -->|No| E[Apply scaled shifted correction]
D --> F[Validate inferred recurrence]
E --> F
F --> G[Exponentiate X modulo characteristic polynomial]
G --> H[Combine with initial terms]
After processing index $n$, the connection polynomial annihilates the observed prefix and has minimal feasible length; every nonzero update cancels the current discrepancy. Classical inference costs $O(NL)$, at worst $O(N^2)$, while Kitamasa evaluates index $n$ in $O(L^2\log n)$ time and $O(L)$ working space.
Implementation, Complexity and Engineering
The implementation requires a prime modulus, normalizes samples, validates the inferred rule against the complete prefix, handles the all-zero sequence, and rejects invalid indices. A prefix fit is only evidence about that prefix; when samples come from measurements or an unknown program, reserve a disjoint suffix for validation.
| Method | Cost | Failure mode | Mitigation |
|---|---|---|---|
| Direct iteration | $O(Ln)$ time, $O(L)$ memory | Huge indices are impractical | Use Kitamasa |
| Berlekamp–Massey | $O(NL)$ time, $O(L)$ state | Too few or corrupted samples yield a false rule | Use at least $2L$ trusted samples and held-out validation |
| Classical Kitamasa | $O(L^2\log n)$ time, $O(L)$ state | Quadratic multiplication dominates for large $L$ | Switch to fast polynomial arithmetic when justified |
| Matrix power | $O(L^3\log n)$ classically | Ignores scalar recurrence structure | Prefer Kitamasa unless the full state is needed |
| Prime-field arithmetic | All nonzero discrepancies invert | Composite moduli contain nonunits | Reject rings or use a ring-aware method |
| Cached model | Amortizes inference across queries | Wrong modulus or initial vector may be reused | Store recurrence, modulus, and initial state together |
Applications, Case Studies and Exercises
From twenty Fibonacci samples modulo $10^9+7$, Berlekamp–Massey returns $(1,1)$; Kitamasa then evaluates $F_{50}\equiv586268941\pmod{10^9+7}$ without generating the intervening terms. In a production pipeline, infer from exact training samples, validate on a disjoint suffix, and only then answer indices as large as $10^{18}$.
Apply Berlekamp-Massey & Kitamasa
Case study 1. Generate twenty Fibonacci terms modulo $101$, trace every nonzero discrepancy, and recover $(1,1)$. Evaluate indices $20$, $50$, and $1000$ and compare with fast doubling. Success means every result agrees.
Case study 2. Hide an order-$8$ recurrence modulo $1{,}000{,}000{,}007$. Generate $32$ training and $16$ validation samples, recover the rule, validate the held-out suffix, and answer $100$ indices up to $10^{18}$. Success means every answer matches the original generator.
Exercises. Define discrepancy and linear complexity; prove that the shifted correction cancels the current error; add batched queries; demonstrate a noninvertible discrepancy modulo a composite; and benchmark an NTT-backed polynomial multiplier against the quadratic version.
Use iteration for nearby indices, matrix powers when full state transitions matter, and Berlekamp–Massey plus Kitamasa when trustworthy field-valued samples reveal a short scalar recurrence. Never extrapolate an unvalidated prefix or perform field division in a composite ring; these boundaries lead directly to fast polynomial and generating-function techniques.