Differentiating Wright–Fisher evolution¤
Here we derive the sensitivities of Wright–Fisher evolution and the numerical rules drifft uses to compute them. The derivation proceeds from the first variation of a transition to adjoints of matrix powers, their Krylov approximations, and implicit derivatives of occupation equations. Notation follows the shared conventions; practical usage and validation are covered in the user guide.
Problem and assumptions¤
Differentiation acts on model parameters and states while population sizes, generation counts, and numerical dimensions remain fixed. We distinguish the exact biological operator from its numerical representation so that the target of each derivative is explicit.
Let \(\theta\in\mathbb R^{n_\theta}\) collect the \(n_\theta\) differentiable inputs. The exact one-generation expectation matrix is \(P_t(\theta)\), and its numerical representation is \(\widetilde P_t(\theta)\). Both act on observables; their transposes propagate population mass. With a source vector \(b_t\) added after each generation,
The matrices may be rectangular at population-size changes. All first-variation identities below assume differentiability at the parameters considered. A dot means differentiation along a direction \(\dot\theta\), including any induced changes in \(v_0\) and \(b_t\). Transposes and inner products are real.
Prepared grids, ranks, quadrature nodes, and interpolation settings are held fixed. Selection-dependent operator entries and transforms are differentiated. The identities are exact for the represented operators; this alone does not establish accuracy relative to the exact Wright–Fisher process.
First variation and matrix powers¤
The first variation separates perturbations already present in the state from those introduced by the next transition. Iterating this identity gives the sensitivity of a whole history without a stationarity or mixing assumption.
Expanding one step to first order,
Hence
Sequential implements the corresponding coupled state/tangent recurrence.
The action \(\dot{\widetilde P}_t^{\,T}v_t\) comes from differentiating the operator action with
its vector argument held fixed; no explicit derivative matrix is required.
Constant-operator power¤
Within a constant-size interval, repeated application becomes a matrix power. Its first variation sums the contributions from perturbing each factor in the product; this is the derivative subsequently approximated by Krylov methods.
The operator being powered depends on the coordinates used for evolution. In population coordinates it is \(\widetilde P^T\). With Jacobi's factorization \(\widetilde P=DE\), evolution instead powers the smaller matrix \(H_{\mathrm J}^T=(ED)^T\) between encoding and reconstruction.
We use \(A\) only for this working-coordinate operator, with the following explicit correspondence for an interval without immigration:
| Coordinates | Operator \(A\) | Input \(z\) | Exponent \(k\) | Population output |
|---|---|---|---|---|
| Population | \(\widetilde P^T\) | \(v_0\) | \(g\) | \(v_g=y\) |
| Jacobi, \(g\ge1\) | \(H_{\mathrm J}^T\) | \(D^Tv_0\) | \(g-1\) | \(v_g=E^Ty\) |
The common power calculation is \(y=A^kz\). Thus \(P\) always denotes the population expectation matrix, whereas \(A\) denotes the operator in the coordinates actually evolved; it may even have a different dimension. The coordinate-transform derivation explains the Jacobi row. The product rule for this common calculation gives
This linear map in \(\Delta A\) is the Fréchet derivative of the power function. Including the input-state perturbation gives
For \(k=0\), this reduces to \(\dot y=\dot z\). No inverse of \(A\) appears, so singular transitions and absorbing states do not invalidate the identity.
Coupled block representation¤
The tangent recurrence can itself be evaluated as a power action. This lets drifft apply its evolution algorithms to the derivative without constructing the full Fréchet derivative as a linear operator on matrices.
Define the coupled operator and its input and output states by
Block multiplication and induction give
Indeed, the upper-right block at step \(k+1\) is \(A\mathcal L_k(A,\dot A)+\dot A A^k=\mathcal L_{k+1}(A,\dot A)\). For a pure state perturbation, \(\dot A=0\), only the action \(A^k\dot z\) is needed.
Adjoint and parameter gradients¤
An adjoint contracts the first variation with a scalar objective, avoiding a separate tangent calculation for every parameter. The same pairing yields both the demographic-history gradient and the matrix cotangent used in the power rule.
Set \(\bar v_g=\nabla_{v_g}\ell\) and define \(\bar v_t=\widetilde P_t\bar v_{t+1}\). Pairing the tangent recurrence with \(\bar v_{t+1}\) gives
Summing telescopes, so
Consequently, for parameter \(\theta_a\),
This also covers rectangular boundary transitions: the transpose maps each adjoint back to the preceding state's dimension.
Matrix cotangent of a power¤
For a constant operator, contributions from all steps can be collected into one matrix cotangent. This expression is useful for deriving a factored parameter pullback even when forming the matrix would be too expensive.
For \(y=A^kz\) and incoming cotangent \(\bar y\), define \(z_j=A^jz\) and \(\bar z_j=(A^T)^{k-j}\bar y\). Thus \(z_0=z\), \(z_k=y\), \(\bar z_k=\bar y\), and \(\bar z_0=\bar z\): the bars consistently identify cotangents of the corresponding states. Then
Each operator contribution satisfies \(\bar z_{j+1}^T\dot A z_j=\langle\bar z_{j+1}z_j^T,\dot A\rangle_F\), where \(\langle X,Y\rangle_F=\operatorname{tr}(X^TY)\). Therefore
Thus \(\bar A\) is the cotangent of \(A\). For \(k=0\), \(\bar A=0\) and \(\bar z=\bar y\). Sequential reverse mode follows by transposing its coupled derivative rule.
Krylov sensitivity approximations¤
Krylov methods replace large power actions by projected ones. Forward and reverse sensitivities use their own projection spaces, rather than derivatives of the basis selected during the primal calculation.
Derivative target
drifft approximates \(\mathrm D(A^kz)\), not the derivative of the executed approximation \(V_m(V_m^TAV_m)^kV_m^Tz\) with its input-dependent basis. At finite dimension, these derivatives need not agree. The custom rule's target is the mathematical power of the represented operator \(A\).
Projected forward sensitivity¤
Applying Krylov projection to the coupled block operator approximates the state and tangent together. The projection can depend on the supplied perturbation, so this numerical calculation needs a dedicated AD rule.
Let \(V_{2m,\mathrm{blk}}\) be the basis constructed for \(A_{\mathrm{blk}}\) with seed \(z_{\mathrm{blk}}\), and define
The projected action is
The basis has at most \(2m\) columns, where \(m\) is the configured dimension.
The blk label distinguishes this coupled projection from the primal
\(V_m,H_m\) pair. Polynomial and rational methods use their respective
basis constructions from the Krylov chapter.
For dense rational block solves, write \(S=I+\tau(I-A)\), \(\tau=k/8\). Then
Assuming \(S\) is invertible, the inverse has the block form
The implementation applies these inverses by solves, processing the lower block first and reusing one LU factorization of \(S\).
Two-sided low-rank adjoint¤
The sum defining \(\bar A\) contains a backward orbit under \(A^T\) and a forward orbit under \(A\). Approximating each orbit in its own space gives a low-rank matrix cotangent without storing a population-sized square matrix.
Choose orthonormal bases \(V_{m,\mathrm L},V_{m,\mathrm R}\) seeded by \(\bar y,z\), respectively, and define
Each basis has at most \(m\) columns; their actual dimensions may differ. The labels \(\mathrm L,\mathrm R\) identify the backward and forward spaces; \(\bar y_{m,\mathrm L},z_{m,\mathrm R}\) are their projected seeds. Substitute the orbit approximations \((A^T)^i\bar y\approx V_{m,\mathrm L}H_{m,\mathrm L}^i\bar y_{m,\mathrm L}\) and \(A^jz\approx V_{m,\mathrm R}H_{m,\mathrm R}^jz_{m,\mathrm R}\) into the cotangent sum:
The same block-power identity shows that \(C\) is the upper-right block of
The transpose on the right block follows from transposing the forward orbit in \(\bar A\); omitting it would give a different matrix cotangent. With \(F=V_{m,\mathrm L}C\),
The operator pullback holds \(F_i,(V_{m,\mathrm R})_i\) fixed during these contractions. Differentiating their construction would introduce derivatives of the approximation algorithm instead of evaluating this adjoint. The result is returned in the operator's parameter structure without materializing \(\bar A\).
Encoding, reconstruction, and sources¤
A reduced-coordinate power is only part of the population calculation. Parameter-dependent transforms and affine source terms contribute additional chain-rule terms, even when the power derivative itself is evaluated exactly.
Write the prepared calculation as
Its first variation is
For incoming population cotangent \(\bar v_{\mathrm{out}}\), the corresponding reverse contributions are \(\bar y=R^T\bar v_{\mathrm{out}}\), \(\bar R=\bar v_{\mathrm{out}}y^T\), and \(\bar c=\bar v_{\mathrm{out}}\), followed by the power pullback. Ordinary JAX differentiation propagates these contributions through the maps defining \(A,z,R,c\).
Jacobi coordinates¤
Jacobi's one-generation factorization determines both the reduced exponent and the transforms surrounding it. Differentiating only the reduced matrix would omit changes in how population mass enters and leaves coefficient space.
For \(\widetilde P=DE\) and \(H_{\mathrm J}=ED\),
Thus \(z=D^Tv_0\), \(A=H_{\mathrm J}^T\), \(k=g-1\), \(R=E^T\), and \(c=0\) without immigration. Their variations include
Any parameter-independent transform has zero variation. Zero generations is handled separately as an identity; population-coordinate evolution instead uses \(z=v_0\), \(A=\widetilde P^T\), \(k=g\), and \(R=I\).
Affine sources¤
A constant source can be included in a linear power by adding a constant coordinate. This preserves the post-transition timing of immigration and allows source sensitivities to use the same power rule.
The augmented operator's tangent is \(\left(\begin{smallmatrix}\dot{\widetilde P}^{\,T}&\dot b\\0&0\end{smallmatrix}\right)\); the added constant coordinate has zero tangent.
In Jacobi coordinates, the encoded source is \(D^Tb\). The population output for \(g\ge1\) is
with an empty sum for \(g=1\). Thus the source contributes through both the augmented reduced operator and the final offset \(c=b\). For changing sizes or parameters, the first-variation and adjoint recurrences above compose the interval and boundary operations directly.
Implicit derivatives of linear solves¤
Expected visits are defined by a linear system, rather than a finite-time power. Differentiating that defining equation avoids differentiating a factorization or an iterative solver trajectory.
Write \(\widetilde Q\) for the represented interior transition, retaining \(Q\) for the exact restriction of \(P\). Expected visits satisfy
Since the initial unit vector \(e_1\) is fixed, the product rule gives
Given the incoming cotangent \(\bar u\), define the adjoint solve variable \(\eta_u\) by \((I-\widetilde Q)\eta_u=\bar u\). Pairing with \(\dot u\) yields
Therefore \(\overline{\widetilde Q}=u\eta_u^T\). The variable \(\eta_u\) is the solution of an adjoint linear system, distinct from the incoming cotangent \(\bar u\); \(u\) remains the expected-visits vector throughout. The occupation interpretation requires transience, and the represented system must be invertible. The derivative inherits solve and representation errors.
Lineax supplies these implicit rules. Shifted solves also occur in rational Krylov construction, but the outer power derivative uses the custom sensitivity rules above, not differentiation of the Arnoldi basis.
Implementation correspondence¤
The mathematical power rules enter JAX through custom primitives, while ordinary differentiation connects them to biological parameters and coordinate transforms. This boundary keeps adaptive numerical construction separate from the derivative target.
All drifft-owned custom differentiation uses
jax.experimental.hijax.HiPrim. HiJAX derives linearization and reverse mode
from JVP rules, with explicit transposes for linear inputs. The Krylov Fréchet
primitive supplies its operator pullback explicitly and rejects unsupported
higher derivatives. Dense's binomial operation uses ordinary JAX AD.
The primal power is computed with convergence diagnostics and passed through a cached-power primitive. Its derivative is attached to \(A\) and \(z\); the cached value and primal Arnoldi basis are not differentiated. Preparation and reconstruction remain connected to parameters through ordinary JAX operations.
Accuracy and limitations¤
Exact derivative identities do not make their projected evaluations exact. The supported derivative orders and convergence checks depend on the evolution method, and must be distinguished from backend approximation error.
| Calculation | Derivative target | Higher-order support |
|---|---|---|
| Sequential | Repeated numerical transition, including its backend approximation | Supported through the recurrence and supported backend rules |
| PolynomialKrylov / RationalKrylov | Approximate sensitivity of the prepared power \(A^kz\) | Higher-order differentiation through the Fréchet rule raises explicitly |
For the coupled forward action, the combined error estimate must satisfy both state and tangent tolerances. Each block uses \(\mathtt{atol}+\mathtt{rtol}\,\|\cdot\|_1\); the smaller tolerance is applied to the combined estimate so a large state cannot mask a small sensitivity. The reverse operator rule compares nested projected cotangents in Frobenius norm and checks intermediate orbit recurrence defects. These checks can detect unresolved propagation even when nested approximations agree.
Diagnostic estimates, not error bounds
Recurrence-defect estimates use unit amplification rather than bounds on subsequent operator powers. For general nonnormal operators, they are heuristic. Neither these estimates nor primal convergence certifies derivative accuracy for the exact Wright–Fisher process.
Zero seeds and invariant-subspace breakdown are supported when sensitivity
checks pass. A failed sensitivity or pullback raises even with throw=False;
a failing member under vmap can fail the batch. There is no automatic fallback.
Numerical error estimates and status fields are not differentiation targets.
The accuracy guide covers validation. In particular, finite differences of an underresolved Krylov primal can disagree with its custom derivative because they differentiate different finite approximations.
References¤
The implementation follows the matrix-function sensitivity framework above; the references provide the low-rank approximation background and the AD interfaces used to express it.