Skip to content

Derived parameters for dynamics - #21

Merged
mapi1 merged 5 commits into
mainfrom
feat/derived-params
Nov 11, 2026
Merged

mapi1 merged 5 commits into
mainfrom
feat/derived-params

Conversation

@mapi1

@mapi1 mapi1 commented Nov 5, 2026 •

Copy link
Copy Markdown
Collaborator

Lets a dynamics model declare values computed from its other parameters, the counterpart of TVB's update_derived_parameters and of coupling precompute. Includes the TVB ReducedSetFitzHughNagumo port that motivates it, and a coupling channel fix it surfaced.

class Model(AbstractDynamics):
    DEFAULT_PARAMS = Bunch(tau=2.0, k=0.7)
    DERIVED_PARAMS = ("rate", "gain")

    def derived_params(self, params):
        rate = 1.0 / params.tau
        return Bunch(rate=rate, gain=jnp.tanh(params.k) * rate)

    def dynamics(self, t, state, params, coupling, external):
        ...  # params.tau and params.rate alike

resolved = model.resolve_params(config.dynamics)  # inspection / direct calls

How it works

  • The hook returns only derived values; they are merged into the params that dynamics() receives.
  • It runs once per solve, before the time loop, on every path (bare / Network / HeterogeneousNetwork, native and Diffrax, observe= probe). Gradients flow to the primary parameters.
  • Models without derived parameters are unaffected.

Design decisions to discuss

  1. The hook sees only the model's own parameters, not the graph or other components. Network-dependent rules (e.g. analytic FIC) would need a separate network-level hook.
  2. Derived values are never set, swept or fitted. config.dynamics holds only primary parameters, so nothing can go stale. To make a quantity free, promote it to DEFAULT_PARAMS in a subclass. Fitting a function of one parameter is a reparameterization (TransformedParameter).
  3. DERIVED_PARAMS uses ordinary inheritance: omit to inherit, define to replace (() removes all). This differs from STATE_NAMES etc. on purpose, so promotion works.

Misuse (wrong declaration, overlap with DEFAULT_PARAMS, missing hook, wrong hook output, Model(rate=...), config.dynamics.rate = ...) raises errors that name the model, field and config location.

Performance

XLA already hoists invariant parameter math out of the forward scan, but not out of the backward pass. Cheap scalar derivations can stay inline; expensive (e.g. matrix-valued) derivations under gradients benefit substantially.

Example model: ReducedSetFitzHughNagumo

Port of TVB's model, whose mode coefficients (Aik, Bik, Cik, e_i, ...) are integrals over the input distribution N(mu, sigma). They are now derived parameters, so mu and sigma can be swept or fitted by gradient descent, which TVB cannot do. Derived arrays, dfun and network trajectories (instant and delayed) match TVB. Its network input is the coupled sum of the three xi modes, provided by the new SumLinearCoupling.

Coupling channels

The port surfaced two gaps in the coupling contract:

  • Built-in couplings had a fixed single output channel, so LinearCoupling could not transport several states.
  • A plain Network never compared a coupling's channels with the model's COUPLING_INPUTS. JAX clamps out-of-range rows silently, so e.g. MontbrioPazoRoxin with LinearCoupling(source="r") received the r coupling as its V coupling.

Changes:

  • Elementwise couplings (Linear, Difference, Sigmoid, Tanh, Kuramoto, delayed variants) produce one channel per source state (N_OUTPUT_STATES = PER_SOURCE). Single-source use is unchanged.
  • New SumLinearCoupling / DelayedSumLinearCoupling sum the source states before transport (one channel, one transport).
  • Network checks channel counts against COUPLING_INPUTS. Heterogeneous routes check against the readout width, at prepare() for callable readouts.
  • Breaking for subclasses of these couplings whose pre() combines states: they must now declare N_OUTPUT_STATES, and the error says so.
  • The TVB comparison now wires MontbrioPazoRoxin and Epileptor with both coupled states and routes WilsonCowan's network term to delayed (its instant input is TVB's local coupling). The tests' tiny G had hidden the old wiring.

Changes

  • dynamics/base.py: DERIVED_PARAMS, derived_params(), resolve_params(), validation
  • solve.py: resolve once per solve on all dispatch paths; route channel check
  • utils/printer.py: show derived names and hook source
  • dynamics/tvb/reduced_set.py: ReducedSetFitzHughNagumo
  • coupling/: per-source channels, SumLinearCoupling, clearer pre() channel errors; core/network.py, core/heterogeneous.py: channel checks
  • Docs: "Derived Parameters" in dynamics.qmd, "Output Channels" in coupling.qmd, skill references
  • Tests: test_derived_params.py, test_reduced_set.py, test_coupling_channels.py, TVB comparison entries

@mapi1
mapi1 merged commit a73932a into main Nov 11, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant