agentleFS
90184479018447/julia-pro-p/skills/bayesian-ude-workflow/SKILL.md

bayesian-ude-workflow

Skill0 starsChanged 2 days agoMITView on GitHub

Fit a universal differential equation with uncertainty: MAP warm start, Turing model with an embedded ODE solve, sampler choice, diagnostics, and a posterior predictive check on the neural correction. Use when a hybrid physics-ML model needs credible intervals.

The file

Files
SKILL.md

Bayesian UDE

A UDE puts a neural correction beside known physics, and the Bayesian version asks for a posterior over the physical parameters and the network weights at once. Four facts make vanilla NUTS a poor first attempt: the dimension is the network plus the physics, every log-density evaluation costs a full ODE solve and an adjoint pass, the geometry is curved along stiff directions, and neuron permutation symmetries create identical likelihoods at different parameter vectors. The staged order below exists for those four facts.

Stage 1 — deterministic warm start

Fit a MAP point with Optimization.jl over a ComponentArray, so the optimiser sees a flat vector while the ODE function keeps the structured (α, β, nn) view. This surfaces the failures cheaply: a stiff solver that will not converge, an unbounded gradient, a parameter unidentifiable at the scale you chose. Keep the ComponentArray and the remake-based problem construction — stage 2 reuses both.

Stage 2 — the Turing model

using Turing, ComponentArray

@model function ude(y, t, prob, θ0)
    θ ~ MvNormal(zeros(length(θ0)), 2.0)
    ps = ComponentArray(θ)
    sol = solve(remake(prob; p = ps), Tsit5(); saveat = t, sensealg = GaussAdjoint())
    for i in eachindex(y)
        y[i] ~ Normal(sol.u[i][1], 0.05)
    end
end

remake inside the model is the whole performance story: rebuilding the ODEProblem per leapfrog step re-pays construction and can recompile. Sample over the flat vector, reshape inside the solve.

Stage 3 — sampler

SituationSamplerNote
unimodal, NUTS converged from the MAPNUTS(0.65) with init_params = vec(map_estimate)the default path
suspected multimodality (UDD identifiability, neuron permutations)Pigeons NRPT over TuringLogPotentialtempering, not a longer chain
high dimension and NUTS too slowSVI as reconnaissanceits variance is too narrow for a published interval

Stage 4 — diagnostics and the posterior predictive

R-hat below 1.01, ESS in the hundreds per parameter, no trend in the trace, divergences reported rather than hidden. For a UDE the decisive check is different: sample parameter vectors from the chain, solve the ODE, and check that the credible band covers held-out trajectories. Then plot the neural correction against state: if its band is wider than the physics term, the data does not constrain the correction, and the posterior is the prior in disguise.

AD backend and sense algorithm

BackendSense algorithmUse forCost
ForwardDiffnone — Dual numbers bypass itup to ~100 parameters, stiff systemslinear in parameter count
Zygote, Enzyme, ReverseDiffGaussAdjoint()network weights dominate the dimensionone linear solve per reverse pass

ForwardDiff ignores sensealg; reverse-mode backends respect it. BacksolveAdjoint is cheapest in memory and worst on stiff systems, where the backward solve loses accuracy — the failure looks like a wrong gradient, not a solver error.

Depth

PageOpen when
turing-ude-stage-codewriting stage 1 or 2: the full OptimizationProblem and @model
bayesian-sindy-workflowthe correction is finite and you want intervals on discovered coefficients
optimization-patternsstage 1 in detail: multiple shooting, least squares, OptimizationProblem over a solve
differentiationthe gradient itself is suspect
bayesian-pinnthe unknown is the whole field, not a correction term

Traps

  • Stiff system under NUTS with loose tolerances. Solver noise enters the gradient and the posterior geometry becomes a function of reltol; tighten it or reformulate.
  • u0 fixed to the first observation. That asserts zero noise at t = 0; sample it when it is uncertain.
  • Neuron permutation symmetry. NUTS cannot cross label-switching modes; report the multimodality or break it by architecture, and do not average chains that sit in different permutations.
  • Rebuilding the problem inside the model. Compilation per leapfrog step instead of per run.
  • A MAP point reported as the posterior. A mode with no interval answers a different question, and it is the question the readout most easily suggests.

Done when

A MAP estimate exists before any sampling and its ComponentArray is reused inside the model; the problem is built once and remaked per evaluation; the sensealg matches the AD direction in use; NUTS started from the MAP reaches R-hat below 1.01 with ESS in the hundreds per parameter, or the run moved to tempered sampling and says so; the posterior predictive covers held-out data; the neural correction's band is narrower than the physics term's; and priors, solver tolerances, and backend are recorded next to the chain.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.