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.
In this repository55 other files it gives its agents.
- advanced-optimizationSkill
- bayesian-inferenceSkill
- bayesian-pinnSkill
- catalyst-reactionsSkill
- continuum-mechanics-and-rheologySkill
- control-theorySkill
- creating-julia-appSkill
- debugging-toolkitSkill
- differential-equationsSkill
- differentiationSkill
- equation-discoverySkill
- experiment-trackingSkill
- fem-feaSkill
- julia-graph-neural-networksSkill
- julia-languageSkill
- julia-ml-and-dlSkill
- julia-model-deploymentSkill
- julia-paper-implementationSkill
- julia-pro-helpSkill
- julia-pro-modeSkill
- julia-reinforcement-learningSkill
- modeling-toolkitSkill
- neural-pdeSkill
- nonlinear-dynamicsSkill
- numerical-methods-implementationSkill
- package-development-workflowSkill
- performance-tuningSkill
- physical-learning-systemsSkill
- playbook-debugSkill
- playbook-model-validateSkill
- playbook-package-releaseSkill
- playbook-paper-implementSkill
- playbook-perfSkill
- playbook-test-specSkill
- principle-both-sidesSkill
- principle-breakingSkill
- principle-file-is-truthSkill
- principle-gateSkill
- principle-limitsSkill
- principle-loud-failureSkill
- principle-one-stepSkill
- principle-proof-listSkill
- principle-rebuttalSkill
- principle-runtime-surfaceSkill
- principle-three-boundsSkill
- principle-three-clocksSkill
- scientific-visualizationSkill
- sciml-and-diffeqSkill
- setup-julia-proSkill
- signal-processingSkill
- simulation-and-hpcSkill
- statistical-physicsSkill
- symbolic-mathSkill
- web-development-juliaSkill
- zarr-cloud-arraysSkill
The file
Files
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
endremake 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
| Situation | Sampler | Note |
| unimodal, NUTS converged from the MAP | NUTS(0.65) with init_params = vec(map_estimate) | the default path |
| suspected multimodality (UDD identifiability, neuron permutations) | Pigeons NRPT over TuringLogPotential | tempering, not a longer chain |
| high dimension and NUTS too slow | SVI as reconnaissance | its 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
| Backend | Sense algorithm | Use for | Cost |
ForwardDiff | none — Dual numbers bypass it | up to ~100 parameters, stiff systems | linear in parameter count |
Zygote, Enzyme, ReverseDiff | GaussAdjoint() | network weights dominate the dimension | one 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
| Page | Open when |
| turing-ude-stage-code | writing stage 1 or 2: the full OptimizationProblem and @model |
| bayesian-sindy-workflow | the correction is finite and you want intervals on discovered coefficients |
| optimization-patterns | stage 1 in detail: multiple shooting, least squares, OptimizationProblem over a solve |
| differentiation | the gradient itself is suspect |
| bayesian-pinn | the 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. u0fixed to the first observation. That asserts zero noise att = 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.
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
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.

