fhat. Workspace

DynGMA

Recent

Learn stochastic dynamics from finite-lag observations.

Zhu and Li 2024Also known as Dynamical Gaussian Mixture Approximation

Full skillAll clearChecks: 3 verification scripts passed when this page was builtNo full-scale reproduction is recorded. Most methods need none: their checks cover the claims.v0.1.1

Use with your agent

Use the fhat method "dyngma" (https://fhat.dev/methods/dyngma/SKILL.md) for this problem. Check first whether it applies.

1. What this solves

When measurements are far apart, the drift and diffusion can change substantially before the next measurement. DynGMA approximates what happens within the gap by composing short Gaussian transition models. Fit the drift and noise by evaluating the resulting mixture at each recorded endpoint.

2. When to use

Use timed pairs in coordinates that plausibly form a time-homogeneous Markov Itô diffusion. Lags may differ across pairs. The drift needs a usable state Jacobian. The likelihood assumes a nonsingular covariance in the modeled coordinates. Start with a low-dimensional state and a few mixture stages. Initial states can be away from equilibrium.

Compare a one-step likelihood and a refined approximation on whole held-out trajectories or independent transitions, using their actual lags. Look for material differences in conditional mean, covariance or density. At matched states and lags, test whether older observations still predict the residuals. Persistent history dependence calls for revisiting the coordinates or observation model.

3. When not to use (and what to use instead)

Situation Use instead
Only unordered samples are available Estimate a static density or obtain timed transitions before fitting an evolution law.
Matching the observed state still leaves systematic dependence on its history Identify additional coordinates or fit an observation model. Stochastic OnsagerNet can learn closure when suitable microscopic trajectories are available.
A single Euler–Maruyama Gaussian already predicts transitions adequately at the observed lags Use a one-step likelihood. For an Onsager-structured drift, use Stochastic OnsagerNet.
A stationary landscape, irreversible current or EPR is the primary result Use UIDD for a diffusion with a supported unique invariant density and a specified reversal.
Constraints or degenerate noise make the endpoint law singular in these coordinates Work in suitable reduced coordinates or use a transition model defined on the actual support.

4. Algorithm

  1. Model a dd-dimensional column vector by dX=fθ(X) dt+σθ(X) dWdX=f_\theta(X)\,dt+\sigma_\theta(X)\,dW, with independent standard Brownian components in WW. For example, use an ff head with dd outputs and build a square σ\sigma from a trainable strict lower triangle and a diagonal (1+si2+si)/2(\sqrt{1+s_i^2}+s_i)/2 from another head ss. The likelihood uses the full covariance rate σσ⊤\sigma\sigma^\top. Paper, Eq. (16)

    Evaluate the diagonal in the working precision using r = 0.5 * hypot(s, 1) and t = 0.5 * s. Use r + t for s≥0s\ge0 and 0.25 / (r - t) for s<0s<0. The reciprocal branch avoids cancellation. Evaluate only the applicable branch for each entry, including under autodiff. Check covariance conditioning separately because a positive diagonal can still be too small for a reliable factorization.

  2. For each pair (x,y,τ)(x,y,\tau) with τ>0\tau>0, choose K≥1K\ge1 equal mixture stages of length h=τ/Kh=\tau/K, for example K=⌈τ/hmax⁡⌉K=\lceil\tau/h_{\max}\rceil with hmax⁡>0h_{\max}>0. Choose L≥1L\ge1 inner steps of length δ=h/L\delta=h/L per stage. A stage starting at a point zz sets m0=zm_0=z and C0=0C_0=0. For j=0,…,L−1j=0,\dots,L-1, set a=mj+δfθ(mj)/2a=m_j+\delta f_\theta(m_j)/2 and let JJ have entries J[i,k]=∂fθ,i/∂x[k]J[i,k]=\partial f_{\theta,i}/\partial x[k] evaluated at aa. With A=I+δJA=I+\delta J and B=I+δJ/2B=I+\delta J/2, compute mj+1=mj+δfθ(a)m_{j+1}=m_j+\delta f_\theta(a) and Cj+1=ACjA⊤+δ(Bσθ(a))(Bσθ(a))⊤C_{j+1}=AC_jA^\top+\delta(B\sigma_\theta(a))(B\sigma_\theta(a))^\top. The stage output is the mean and covariance after the last inner step, mm and CC with index LL. Paper, Eqs. (13)–(14)

  3. Apply one stage to xx and give the resulting Gaussian weight 1. For each later stage and each parent (w,m,C)(w,m,C), factor C=SS⊤C=SS^\top with lower triangular SS. Use the point mm with weight 1/(d+1)1/(d+1) and the points m±d+1 Sejm\pm\sqrt{d+1}\,Se_j with weight 1/[2(d+1)]1/[2(d+1)] each, for j=1,…,dj=1,\dots,d. Here eje_j is coordinate vector jj, so SejSe_j takes a column of SS. Advance each point by the stage update from zero covariance. Give each output the parent weight times its point weight. Visit parents in order. Within each, the checker expects the center, then positive axes, then negative axes, both in increasing coordinate order. Paper, Algorithm 1

  4. For each final component ii, factor Ci=TiTi⊤C_i=T_iT_i^\top and solve Tiui=y−miT_iu_i=y-m_i. Its log contribution is

    ai=log⁡wi−dlog⁡(2π)/2−∑jlog⁡Ti[j,j]−ui⊤ui/2.a_i=\log w_i-d\log(2\pi)/2-\sum_j\log T_i[j,j]-u_i^\top u_i/2.

    Let amax⁡=max⁡iaia_{\max}=\max_i a_i. The endpoint log density is amax⁡+log⁡∑iexp⁡(ai−amax⁡)a_{\max}+\log\sum_i\exp(a_i-a_{\max}). Minimize the mean of its negative over recorded pairs. Differentiate through the stage updates, Cholesky factors, cubature points and Gaussian terms, holding the observed endpoints and stage counts fixed. Paper, Eq. (17)

5. Parameters

Parameter How to choose it
KK and hh Try K=1K=1 first. Raise KK when smaller stages improve held-out predictions. With a point start the unpruned mixture contains (2d+1)K−1(2d+1)^{K-1} components. Keep KK fixed for each pair during differentiation.
LL Check approximation error by refining the inner step while holding KK fixed. The mean uses a midpoint evaluation even for L=1L=1.
Diffusion factor σθ\sigma_\theta Use a full-row-rank factor. The paper builds a triangular factor with a positive diagonal for state-dependent noise. Condition the covariance in the data's working units.
Drift parameterization Fixed known terms can be kept in the drift. Use an analytic Jacobian or autodiff, including its effect on the covariance.

6. Applying it to your own code

Keep the distinction between σ\sigma, the covariance rate σσ⊤\sigma\sigma^\top, and the propagated covariance CC. With the lower Cholesky factor, the weighted outer products of cubature displacements recover CC. Taking rows instead would recover S⊤SS^\top S and change a correlated covariance. The portable reference implements equal stage lengths from Algorithm 1 and dense linear algebra. Its first stage starts from a point, so all cubature points of that stage coincide and it is advanced once; the component count above already reflects this. The pinned authors' code differs from the paper in ways that matter when comparing with it: its cubature helper takes rows of the lower Cholesky factor (so its spread is S⊤SS^\top S rather than CC), it uses one stage of length τ\tau if τ\tau is shorter than the nominal stage length, or nominal stages followed by a final stage of at least one and less than two nominal lengths otherwise (it counts stages with int(τ/h−1) in floating point, so a lag that is an exact multiple of hh can end with a final stage of exactly 2h2h), its training criterion conditions each later observation on the first point of the series, and it adds 10−4510^{-45} to the covariance before factorization. Authors' stage scheduling · Authors' cubature helper

For larger models use batch linear algebra and autodiff. The portable two-weight fit uses centered differences. Keep the data split and recorded lags fixed when comparing likelihoods. To predict an endpoint sample at a specified lag, choose a final component with its normalized weight and return mi+Tiξm_i+T_i\xi with independent standard normal components in ξ\xi.

When you write code using this method, add the comment dyngma v0.1.1, Zhu and Li (2024).

7. Verification

Step 1. Export affine_mixture, affine_logpdf, benes_logpdf and training_loss_gradient in port_adapter.py. The scalar Beneš test model used by the last two is defined in the checks/cases.json interface: f(x)=a⋅tanh⁡(x)f(x)=a\cdot\tanh(x) and σ=exp⁡(b)\sigma=\exp(b) for weights [a,b][a,b]. Follow the interface in checks/cases.json and run python3 /path/to/dyngma/checks/check_port.py from the directory containing the adapter. Show its output to the user. The fixtures include correlated state-dependent noise, a two-dimensional mixture log density, a surviving tail log density and a fixed two-stage likelihood with two unequal observation lags. The last case checks both weight derivatives. The analytic Brownian moment check exercises the cubature axes and weights.

Step 2. Check the component weights, covariance conditioning and held-out conditional predictions by lag. Inspect the fit at smaller hh and δ\delta before trusting estimates of fθf_\theta or σθ\sigma_\theta. For the paper's unit-noise Beneš density comparison, at x0=0.5x_0=0.5 and τ=1\tau=1 the plotted DynGMA density near y=0y=0 is about 0.18. The implementation gives 0.176908 with h=0.5h=0.5 and L=2L=2. This point check is coarse: the exact density there, 0.1894, is also within 0.01 of the plotted value. The paper shows the curves but reports no integrated errors; computed by this reference against the exact transition on [−12,12][-12,12], the integrated absolute density errors at that lag are 0.05989 for DynGMA and 0.32182 for the one-step Euler baseline. Figure 2

The separate small fit uses exact transition quantiles from the unit-noise SDE. It compares coefficient recovery and conditional likelihood at unseen starts and lags with a one-step Euler model fitted to the same data. The authors' neural training result in Appendix C was not reproduced. It uses noise amplitude 0.3 and much larger training data. Appendix C, Table 4

8. Failure signatures

Symptom Check
Correlated directions spread incorrectly while scalar cases pass Check the columns of the lower Cholesky factor and the ACA⊤ACA^\top and BσB\sigma factors.
A finite tail log density underflows Combine normalized Gaussian log terms with log-sum-exp before projecting to density.
A component covariance is singular despite full-row-rank noise Reduce the inner step δ\delta and check the conditioning of B=I+δJ/2B=I+\delta J/2.
Likelihood rewards a noise scale close to zero Inspect rank and conditioning of the data and the fitted covariance. Check whether observed noise, hidden state or deterministic coordinates require a different model.
Cost grows quickly on longer intervals Count stages and cubature branches. Check K=1K=1 before increasing the mixture size.

9. Files and commands

From the method folder run python3 examples/fit_benes.py. The reference kernel and mixture are in reference/dyngma.py. See reference/models.py for the fixed models and loss and reference/benes.py for exact transitions and the toy fit.

Run python3 checks/make_cases.py for the portable fixtures. Run python3 verify/check_cases.py for fixture and derivative checks, python3 verify/check_figure2.py for known-coefficient densities, and python3 verify/check_fit.py for coefficient recovery.

10. Sources

A. Zhu and Q. Li, DynGMA: A robust approach for learning stochastic differential equations from data, Journal of Computational Physics 513, 113200 (2024). See Algorithm 1, Eqs. (13), (14) and (17), Figure 2 and Appendix C. Published paper · Open manuscript · Pinned authors' implementation

Evidence

Output of the verification scripts, run on the reference implementation of version 0.1.1 when this page was built.

Passed verify/check_cases.py: Checks covariance transport, cubature axes, the multi-dimensional mixture log density and likelihood, including both weight derivatives against an independent scalar complex-step calculation.
PASS 6 fixed cases and the independent property checks
PASS loss and both weight derivatives against independent complex-step differentiation
PASS caught discarded covariance between inner steps: midpoint and transported correlated covariance
PASS caught rows of the Cholesky factor: two-dimensional cubature after the first covariance
PASS caught uniform cubature weights: two-dimensional cubature after the first covariance
PASS caught loss evaluated in density space: log density in an underflowing tail (math domain error)
PASS caught transposed triangular solve: two-dimensional mixture log density with correlated covariance
PASS caught one-dimensional Gaussian normalizer: two-dimensional mixture log density with correlated covariance
PASS caught detached cubature centers: finite-lag training loss and both weight derivatives
Passed verify/check_figure2.py: Recreates the known-coefficient Beneš density comparison in Figure 2 and checks density mass, a coarse curve point read from the figure and integrated error against the exact transition.
lag 1, integrated absolute error 0.05989, Euler 0.32182
Figure 2 density at y=0: 0.176908, plotted value about 0.18
lag 2, integrated absolute error 0.04885, Euler 0.55515
Passed verify/check_fit.py: Fits two coefficients to exact unit-noise Beneš quantiles and compares recovery and held-out likelihood with an independently fitted Euler baseline.
fitted drift amplitude 0.9616, noise 0.9883, Euler noise 1.1156
held-out negative log density 1.0664, Euler 1.0732, initial 1.4201

More methods are coming.