Inference¶
Package¶
inference
¶
inference¶
Inference engines for WPPM.
This subpackage provides different strategies for fitting model parameters to data and returning posterior objects.
MVP implementations
- MAPOptimizer : maximum a posteriori fit with Optax optimizers.
- LaplaceApproximation : approximate posterior covariance around MAP.
- LangevinSampler : skeleton for sampling-based inference.
Future extensions
- adjusted MC samplers, e.g., MALA (for Bayesian posterior inference).
Classes:
| Name | Description |
|---|---|
InferenceEngine |
Abstract interface for inference engines. |
LangevinSampler |
Langevin sampler (stub). |
LaplaceApproximation |
Laplace approximation around MAP estimate. |
MAPOptimizer |
MAP (Maximum A Posteriori) optimizer. |
Attributes:
| Name | Type | Description |
|---|---|---|
INFERENCE_ENGINES |
|
INFERENCE_ENGINES
¶
INFERENCE_ENGINES = {
"map": MAPOptimizer,
"laplace": LaplaceApproximation,
"langevin": LangevinSampler,
}
InferenceEngine
¶
Bases: ABC
Abstract interface for inference engines.
Methods:
| Name | Description |
|---|---|
fit |
Fit model parameters to data and return a Posterior object. |
fit
¶
Fit model parameters to data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
WPPM
|
Psychophysical model to fit. |
required |
data
|
ResponseData
|
Observed trials. |
required |
Returns:
| Type | Description |
|---|---|
Posterior
|
Posterior object wrapping fitted params and model reference. |
Source code in src/psyphy/inference/base.py
LangevinSampler
¶
Langevin sampler (stub).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
steps
|
int
|
Number of Langevin steps. |
1000
|
step_size
|
float
|
Integration step size. |
1e-3
|
temperature
|
float
|
Noise scale (temperature). |
1.0
|
Methods:
| Name | Description |
|---|---|
fit |
Fit model parameters with Langevin dynamics (stub). |
Attributes:
| Name | Type | Description |
|---|---|---|
step_size |
|
|
steps |
|
|
temperature |
|
Source code in src/psyphy/inference/langevin.py
fit
¶
fit(model, data) -> Posterior
Fit model parameters with Langevin dynamics (stub).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
WPPM
|
Model instance. |
required |
data
|
ResponseData
|
Observed trials. |
required |
Returns:
| Type | Description |
|---|---|
Posterior
|
Posterior wrapper (MVP: params from init). |
Source code in src/psyphy/inference/langevin.py
LaplaceApproximation
¶
Laplace approximation around MAP estimate.
Methods:
| Name | Description |
|---|---|
from_map |
Construct a Gaussian approximation centered at MAP. |
MAPOptimizer
¶
MAPOptimizer(
steps: int = 500,
learning_rate: float = 5e-05,
momentum: float = 0.9,
optimizer: GradientTransformation | None = None,
*,
track_history: bool = True,
log_every: int = 1,
progress_every: int = 10,
show_progress: bool = False,
max_grad_norm: float | None = 1.0,
)
Bases: InferenceEngine
MAP (Maximum A Posteriori) optimizer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
steps
|
int
|
Number of optimization steps. |
500
|
optimizer
|
GradientTransformation
|
Optax optimizer to use. Default: SGD with momentum. |
None
|
Notes
- Loss function = negative log posterior.
- Gradients computed with jax.grad.
Create a MAP optimizer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
steps
|
int
|
Number of optimization steps. |
500
|
optimizer
|
GradientTransformation | None
|
Optax optimizer to use. |
None
|
learning_rate
|
float
|
Learning rate for the default optimizer (SGD with momentum). |
5e-05
|
momentum
|
float
|
Momentum for the default optimizer (SGD with momentum). |
0.9
|
track_history
|
bool
|
When True, record loss history during fitting for plotting. |
True
|
log_every
|
int
|
Record every N steps (also records the last step). |
1
|
progress_every
|
int
|
Update the progress-bar loss display every N steps (and the last step) when show_progress=True. This is kept separate from log_every so you can record loss at high frequency for plotting (e.g. log_every=1) without forcing a device->host sync for the progress UI every step. |
10
|
show_progress
|
bool
|
When True, display a tqdm progress bar during fitting. This is a UI feature: if tqdm is not installed, fitting proceeds without a progress bar. |
False
|
max_grad_norm
|
float | None
|
If set, clip gradients by global norm to this value before applying optimizer updates. This stabilizes optimization when gradients blow up. |
1.0
|
Methods:
| Name | Description |
|---|---|
fit |
Fit model parameters with MAP optimization. |
get_history |
Return (steps, losses) recorded during the last fit when tracking was enabled. |
Attributes:
| Name | Type | Description |
|---|---|---|
log_every |
|
|
loss_history |
list[float]
|
|
loss_steps |
list[int]
|
|
max_grad_norm |
|
|
optimizer |
|
|
progress_every |
|
|
show_progress |
|
|
steps |
|
|
track_history |
|
Source code in src/psyphy/inference/map_optimizer.py
fit
¶
fit(
model,
data,
init_params: dict | None = None,
seed: int | None = None,
) -> MAPPosterior
Fit model parameters with MAP optimization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
WPPM
|
Model instance. |
required |
data
|
ResponseData
|
Observed trials. |
required |
init_params
|
dict | None
|
Initial parameter PyTree to start optimization from. If provided, this takes precedence over the seed. |
None
|
seed
|
int | None
|
PRNG seed used to draw initial parameters from the model's prior when init_params is not provided. If None, defaults to 0. |
None
|
Returns:
| Type | Description |
|---|---|
MAPPosterior
|
Posterior wrapper around MAP params and model. |
Source code in src/psyphy/inference/map_optimizer.py
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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 | |
get_history
¶
Return (steps, losses) recorded during the last fit when tracking was enabled.
Base¶
base
¶
base.py
Abstract base class for inference engines.
All inference engines must implement a fit(model, data) method
that returns a Posterior object.
All inference engines (MAPOptimizer, LangevinSampler, LaplaceApproximation) subclass from this base.
Classes:
| Name | Description |
|---|---|
InferenceEngine |
Abstract interface for inference engines. |
InferenceEngine
¶
Bases: ABC
Abstract interface for inference engines.
Methods:
| Name | Description |
|---|---|
fit |
Fit model parameters to data and return a Posterior object. |
fit
¶
Fit model parameters to data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
WPPM
|
Psychophysical model to fit. |
required |
data
|
ResponseData
|
Observed trials. |
required |
Returns:
| Type | Description |
|---|---|
Posterior
|
Posterior object wrapping fitted params and model reference. |
Source code in src/psyphy/inference/base.py
MAP Optimizer¶
map_optimizer
¶
map_optimizer.py
MAP (Maximum A Posteriori) optimizer using Optax.
MVP implementation: - Uses gradient ascent on log posterior. - Defaults to SGD with momentum, but any Optax optimizer can be passed in.
Connections
- Calls WPPM.log_posterior_from_data(params, data) as the objective.
- Returns a Posterior object wrapping the MAP estimate.
Classes:
| Name | Description |
|---|---|
MAPOptimizer |
MAP (Maximum A Posteriori) optimizer. |
MAPOptimizer
¶
MAPOptimizer(
steps: int = 500,
learning_rate: float = 5e-05,
momentum: float = 0.9,
optimizer: GradientTransformation | None = None,
*,
track_history: bool = True,
log_every: int = 1,
progress_every: int = 10,
show_progress: bool = False,
max_grad_norm: float | None = 1.0,
)
Bases: InferenceEngine
MAP (Maximum A Posteriori) optimizer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
steps
|
int
|
Number of optimization steps. |
500
|
optimizer
|
GradientTransformation
|
Optax optimizer to use. Default: SGD with momentum. |
None
|
Notes
- Loss function = negative log posterior.
- Gradients computed with jax.grad.
Create a MAP optimizer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
steps
|
int
|
Number of optimization steps. |
500
|
optimizer
|
GradientTransformation | None
|
Optax optimizer to use. |
None
|
learning_rate
|
float
|
Learning rate for the default optimizer (SGD with momentum). |
5e-05
|
momentum
|
float
|
Momentum for the default optimizer (SGD with momentum). |
0.9
|
track_history
|
bool
|
When True, record loss history during fitting for plotting. |
True
|
log_every
|
int
|
Record every N steps (also records the last step). |
1
|
progress_every
|
int
|
Update the progress-bar loss display every N steps (and the last step) when show_progress=True. This is kept separate from log_every so you can record loss at high frequency for plotting (e.g. log_every=1) without forcing a device->host sync for the progress UI every step. |
10
|
show_progress
|
bool
|
When True, display a tqdm progress bar during fitting. This is a UI feature: if tqdm is not installed, fitting proceeds without a progress bar. |
False
|
max_grad_norm
|
float | None
|
If set, clip gradients by global norm to this value before applying optimizer updates. This stabilizes optimization when gradients blow up. |
1.0
|
Methods:
| Name | Description |
|---|---|
fit |
Fit model parameters with MAP optimization. |
get_history |
Return (steps, losses) recorded during the last fit when tracking was enabled. |
Attributes:
| Name | Type | Description |
|---|---|---|
log_every |
|
|
loss_history |
list[float]
|
|
loss_steps |
list[int]
|
|
max_grad_norm |
|
|
optimizer |
|
|
progress_every |
|
|
show_progress |
|
|
steps |
|
|
track_history |
|
Source code in src/psyphy/inference/map_optimizer.py
fit
¶
fit(
model,
data,
init_params: dict | None = None,
seed: int | None = None,
) -> MAPPosterior
Fit model parameters with MAP optimization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
WPPM
|
Model instance. |
required |
data
|
ResponseData
|
Observed trials. |
required |
init_params
|
dict | None
|
Initial parameter PyTree to start optimization from. If provided, this takes precedence over the seed. |
None
|
seed
|
int | None
|
PRNG seed used to draw initial parameters from the model's prior when init_params is not provided. If None, defaults to 0. |
None
|
Returns:
| Type | Description |
|---|---|
MAPPosterior
|
Posterior wrapper around MAP params and model. |
Source code in src/psyphy/inference/map_optimizer.py
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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 | |
get_history
¶
Return (steps, losses) recorded during the last fit when tracking was enabled.
Langevin Samplers¶
langevin
¶
langevin.py
Langevin samplers for posterior inference.
Implements: - Overdamped (unadjusted) Langevin Algorithm (ULA) - Underdamped Langevin (with BAOAB splitting scheme?)
Used for posterior-aware trial placement (InfoGain).
MVP implementation: - Stub that returns an initial Posterior. - Future: implement underdamped Langevin dynamics (e.g. BAOAB integrator).
Classes:
| Name | Description |
|---|---|
LangevinSampler |
Langevin sampler (stub). |
LangevinSampler
¶
Langevin sampler (stub).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
steps
|
int
|
Number of Langevin steps. |
1000
|
step_size
|
float
|
Integration step size. |
1e-3
|
temperature
|
float
|
Noise scale (temperature). |
1.0
|
Methods:
| Name | Description |
|---|---|
fit |
Fit model parameters with Langevin dynamics (stub). |
Attributes:
| Name | Type | Description |
|---|---|---|
step_size |
|
|
steps |
|
|
temperature |
|
Source code in src/psyphy/inference/langevin.py
fit
¶
fit(model, data) -> Posterior
Fit model parameters with Langevin dynamics (stub).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
WPPM
|
Model instance. |
required |
data
|
ResponseData
|
Observed trials. |
required |
Returns:
| Type | Description |
|---|---|
Posterior
|
Posterior wrapper (MVP: params from init). |
Source code in src/psyphy/inference/langevin.py
Laplace Approximation¶
laplace
¶
laplace.py
Laplace approximation to posterior.
Approximates posterior with a Gaussian: N(mean = MAP, covariance = H^-1 at MAP)
Provides posterior.sample() cheaply. Useful for InfoGainPlacement when only MAP fit is available.
MVP implementation: - Stub that just returns the MAP posterior. - Future: compute covariance from Hessian at MAP params.
Classes:
| Name | Description |
|---|---|
LaplaceApproximation |
Laplace approximation around MAP estimate. |
LaplaceApproximation
¶
Laplace approximation around MAP estimate.
Methods:
| Name | Description |
|---|---|
from_map |
Construct a Gaussian approximation centered at MAP. |