Models
Two registration paths share one _MODELS dict:
- In-tree — add an import + entry to
_MODELSand_DESCRIPTIONSinsrc/deqn_jax/models/__init__.py. Right thing for models that ship with the library. - Programmatic — call
register_model(spec, description=...)at runtime. Right thing for agent-codegen'd models in user projects, notebook prototypes, or external plugins. See Adding a model for the contract.
Both paths feed load_model(name) and list_models() identically.
VariableSpec (in variable_spec.py) is a small helper that gives
named attribute access to state and policy arrays (s.k, p.sav_rate)
across both batched [batch, n] and unbatched [n] shapes — cleaner
than state[:, 0] everywhere, and traces through jax.vmap unchanged.
make_init_state_fn is a declarative builder for initial-state
samplers; supports uniform, normal, lognormal, truncated_normal,
constant per variable.
deqn_jax.models
Economic models for DEQN-JAX.
Each model subpackage exports a MODEL: ModelSpec constant. Two
registration paths, both supported and orthogonal:
- In-tree (this file's
_MODELS/_DESCRIPTIONSdicts): add an import + entry. Right thing for models that ship with the library and are version-controlled here. - Programmatic (
register_model(spec, description=...)): add at runtime from anywhere. Right thing for agent-codegen'd models in user projects, notebook prototypes, or pluggable extensions that don't want to fork the library.
Both paths feed the same _MODELS dict, so load_model and
list_models see them identically.
load_model
load_model(name: str) -> ModelSpec
Return the registered model named name.
Raises ValueError with the available names if not found.
Source code in src/deqn_jax/models/__init__.py
59 60 61 62 63 64 65 66 | |
list_models
list_models() -> List[Tuple[str, str]]
Return [(name, description), ...] for every registered model.
Sees both in-tree and runtime-registered entries.
Source code in src/deqn_jax/models/__init__.py
69 70 71 72 73 74 | |
register_model
register_model(
spec: ModelSpec,
*,
description: Optional[str] = None,
overwrite: bool = False,
) -> None
Register a model at runtime so load_model(spec.name) finds it.
Equivalent to adding an entry to _MODELS and _DESCRIPTIONS
in this file, but at import / instantiation time. Intended for:
- agent-codegen'd models in user projects (no fork required),
- notebook prototyping,
- plugin packages that add models to deqn-jax via
register_modelon import.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
ModelSpec
|
A populated |
required |
description
|
Optional[str]
|
Human-readable one-liner for |
None
|
overwrite
|
bool
|
If True, an existing entry with the same name is
replaced. If False (default) and |
False
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
TypeError
|
If |
Example::
from deqn_jax.api import ModelSpec, register_model
MY_MODEL = ModelSpec(name="my_model", ...)
register_model(MY_MODEL, description="My agent-built model")
# Now usable everywhere:
from deqn_jax.api import load_model, train_from_config, TrainConfig
cfg = TrainConfig(model="my_model", ...)
state, history = train_from_config(cfg)
Source code in src/deqn_jax/models/__init__.py
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 | |
unregister_model
unregister_model(name: str) -> None
Remove a runtime-registered model. No-op if not present.
Mainly useful in tests so a leaked register_model from one test
case doesn't bleed into another. Note: in-tree models can also be
removed this way, but doing so is rarely a good idea.
Source code in src/deqn_jax/models/__init__.py
137 138 139 140 141 142 143 144 145 | |
deqn_jax.models.variable_spec
Variable access helpers - named views over state/policy arrays.
Instead of fragile index slicing like state[:, 0], use:
s = unpack_state(state, MODEL)
s.k # capital
s.z # TFP
JAX traces through NamedTuples efficiently.
VariableSpec
Specification for model variables with named access.
Usage
spec = VariableSpec( state_names=("k", "z"), policy_names=("sav_rate",), )
Unpack
s = spec.unpack_state(state_array) p = spec.unpack_policy(policy_array)
Access by name
capital = s.k savings = p.sav_rate
Pack back
new_state = spec.pack_state(s._replace(k=new_k))
Source code in src/deqn_jax/models/variable_spec.py
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 | |
unpack_state
unpack_state(arr: Array) -> Any
Unpack state array into named fields.
Source code in src/deqn_jax/models/variable_spec.py
112 113 114 | |
unpack_policy
unpack_policy(arr: Array) -> Any
Unpack policy array into named fields.
Source code in src/deqn_jax/models/variable_spec.py
116 117 118 | |
pack_state
pack_state(state: Any) -> Array
Pack state NamedTuple back into array.
Source code in src/deqn_jax/models/variable_spec.py
120 121 122 | |
pack_policy
pack_policy(policy: Any) -> Array
Pack policy NamedTuple back into array.
Source code in src/deqn_jax/models/variable_spec.py
124 125 126 | |
get_state_idx
get_state_idx(name: str) -> int
Get index for state variable by name.
Source code in src/deqn_jax/models/variable_spec.py
128 129 130 | |
get_policy_idx
get_policy_idx(name: str) -> int
Get index for policy variable by name.
Source code in src/deqn_jax/models/variable_spec.py
132 133 134 | |
make_state_type
make_state_type(names: Tuple[str, ...]) -> Type[NamedTuple]
Dynamically create a NamedTuple type for state variables.
Source code in src/deqn_jax/models/variable_spec.py
18 19 20 21 22 23 | |
make_policy_type
make_policy_type(
names: Tuple[str, ...],
) -> Type[NamedTuple]
Dynamically create a NamedTuple type for policy variables.
Source code in src/deqn_jax/models/variable_spec.py
26 27 28 | |
unpack_array
unpack_array(
arr: Array, names: Tuple[str, ...], nt_type: Type
) -> NamedTuple
Unpack array columns into named fields.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arr
|
Array
|
Array of shape [batch, n_vars] or [n_vars] |
required |
names
|
Tuple[str, ...]
|
Tuple of variable names |
required |
nt_type
|
Type
|
NamedTuple type to create |
required |
Returns:
| Type | Description |
|---|---|
NamedTuple
|
NamedTuple with named fields |
Source code in src/deqn_jax/models/variable_spec.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 | |
pack_array
pack_array(nt: NamedTuple) -> Array
Pack NamedTuple fields back into array.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nt
|
NamedTuple
|
NamedTuple with array fields |
required |
Returns:
| Type | Description |
|---|---|
Array
|
Array of shape [batch, n_vars] or [n_vars] |
Source code in src/deqn_jax/models/variable_spec.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | |
make_init_state_fn
make_init_state_fn(
state_names: Tuple[str, ...],
init_specs: Dict[str, Dict[str, Any]],
) -> Callable[..., Array]
Build an init_state_fn from per-variable distributional specs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state_names
|
Tuple[str, ...]
|
Ordered tuple of state variable names. |
required |
init_specs
|
Dict[str, Dict[str, Any]]
|
Dict mapping state name to a spec
|
required |
Returns:
| Type | Description |
|---|---|
Callable[..., Array]
|
A function with signature |
Callable[..., Array]
|
producing a |
Callable[..., Array]
|
Constants are not consumed by default (distributions fix their |
Callable[..., Array]
|
kwargs at spec time) but the signature matches ModelSpec's |
Callable[..., Array]
|
|
Unknown distribution names raise ValueError at build time.
Source code in src/deqn_jax/models/variable_spec.py
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 217 218 219 220 221 222 223 | |