Types
The framework's core types are NamedTuples for JAX pytree
compatibility. ModelSpec is the user contract — the only object a
model needs to populate to be trainable. TrainState carries everything
mutable across a training run (params, opt state, episode state, key,
step counter, replay state, …) so train_step can be a pure function
suitable for @jax.jit.
For the ModelSpec field listing with signatures and shape contracts,
see The user contract in
REFERENCE.md. For the prose walkthrough on populating a ModelSpec,
see Implementing a model.
The remaining types (ReweightState, ReplayState, EpisodeState,
Metrics) are framework-internal data plumbing that you'll see in
type signatures but rarely construct yourself. The exception is
make_reweight_state(n_equations) — used when constructing a
TrainState outside create_train_state (rare; mainly in tests).
deqn_jax.types
Core type definitions for DEQN-JAX.
Uses NamedTuples for pytree compatibility with JAX transformations.
ModelSpec
Bases: NamedTuple
Specification for an economic model.
A ModelSpec defines everything needed to train a DEQN: - Dimensions (states, policies, shocks) - Economic equations (equilibrium conditions) - State transitions (dynamics) - Constants (calibration parameters)
All functions should be pure and JAX-compatible.
Example
MODEL = ModelSpec( name="brock_mirman", n_states=2, n_policies=1, n_shocks=1, state_names=["k", "z"], policy_names=["sav_rate"], constants={"alpha": 1/3, "beta": 0.95, ...}, equations_fn=equations, step_fn=step, steady_state_fn=compute_steady_state, )
Source code in src/deqn_jax/types.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 | |
ReweightState
Bases: NamedTuple
Running statistics for adaptive loss reweighting.
Used by lr_annealing and relobralo strategies to track per-equation loss history for dynamic weight adjustment.
Attributes:
| Name | Type | Description |
|---|---|---|
running_ema |
Array
|
EMA of per-equation losses (for lr_annealing) |
prev_losses |
Array
|
Previous step losses (for relobralo) |
init_losses |
Array
|
First step losses (for relobralo) |
initialized |
Array
|
Whether init_losses has been set |
Source code in src/deqn_jax/types.py
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | |
ReplayState
Bases: NamedTuple
Fixed-shape ring buffer of past states + per-state priorities.
Used by the prioritized state-replay mechanism (RL-style anti-forgetting +
spectral-bias mitigation). Lives on TrainState so it survives
checkpoint resume and is variant-agnostic across the 5 train-step kinds.
Attributes:
| Name | Type | Description |
|---|---|---|
buffer |
Array
|
Past states |
priorities |
Array
|
Per-row scalar priorities ( |
write_idx |
Array
|
Scalar int32 ring cursor. Modulo |
n_filled |
Array
|
Scalar int32, capped at |
Source code in src/deqn_jax/types.py
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 | |
TrainState
Bases: NamedTuple
Immutable training state for JAX-compatible training loops.
All state needed for training is bundled here so that train_step can be a pure function suitable for jax.jit.
Attributes:
| Name | Type | Description |
|---|---|---|
params |
Any
|
Equinox model (pytree of parameters) |
opt_state |
Any
|
Optax optimizer state |
episode_state |
Array
|
Current state batch for episode simulation |
key |
Array
|
JAX PRNG key |
step |
int
|
Current training step |
episode |
int
|
Current episode number |
loss_weights |
Array
|
Per-equation loss weights [n_eq] |
reweight_state |
ReweightState
|
Adaptive reweighting running statistics |
target_params |
Any
|
Frozen policy copy for target-network style training |
aux_params |
Any
|
Slot for a second trainable module (e.g. value network
in actor-critic, critic network, learned expectation operator).
None by default. Default training loop ignores it; only loss
functions that know about |
aux_opt_state |
Any
|
Optimizer state for |
history_state |
Any
|
Sliding history window |
replay_state |
Any
|
Prioritized state-replay buffer ( |
Source code in src/deqn_jax/types.py
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 | |
EpisodeState
Bases: NamedTuple
State carried through episode simulation via lax.scan.
Attributes:
| Name | Type | Description |
|---|---|---|
state |
Array
|
Current economic state [batch, n_states] |
key |
Array
|
PRNG key for shock sampling |
Source code in src/deqn_jax/types.py
334 335 336 337 338 339 340 341 342 343 | |
Metrics
Bases: NamedTuple
Training metrics from a single step/episode.
All three fields hold scalar JAX Arrays at runtime (built inside
JIT'd grad steps). They were previously annotated as plain Python
float / Dict[str, float], which produced ~70 spurious
reportArgumentType errors at every Metrics(...) call site --
consumers cast to float explicitly when they need a Python
scalar (float(metrics.loss)).
Source code in src/deqn_jax/types.py
346 347 348 349 350 351 352 353 354 355 356 357 358 359 | |
make_reweight_state
make_reweight_state(n_equations: int) -> ReweightState
Create initial reweight state for n equations.
Source code in src/deqn_jax/types.py
238 239 240 241 242 243 244 245 | |
make_replay_state
make_replay_state(
capacity: int, n_states: int
) -> ReplayState
Create an empty replay buffer with room for capacity states.
Source code in src/deqn_jax/types.py
273 274 275 276 277 278 279 280 | |