Inference¶
Package¶
inference
¶
inference¶
Inference engines for WPPM.
This subpackage provides different strategies for fitting model parameters to data and returning posterior objects.
Implementations
- MAPOptimizer : maximum a posteriori fit with Optax optimizers.
- NUTSSampler : full posterior sampling via NUTS (requires blackjax).
- MALASampler : full posterior sampling via MALA, fixed step size (requires blackjax).
- LaplaceApproximation : approximate posterior covariance around MAP (stub).
- LangevinSampler : skeleton for Langevin-based sampling (stub).
Optional dependencies
- NUTSSampler and MALASampler require blackjax: pip install 'psyphy[sampling]'
Classes:
| Name | Description |
|---|---|
InferenceEngine |
Abstract interface for psyphy inference engines. |
LangevinSampler |
Langevin sampler (stub). |
LaplaceApproximation |
Laplace approximation around MAP estimate. |
MALASampler |
MALA (Metropolis-adjusted Langevin algorithm) using BlackJAX. |
MAPOptimizer |
MAP (Maximum A Posteriori) optimizer. |
NUTSSampler |
NUTS (No-U-Turn Sampler) using BlackJAX. |
InferenceEngine
¶
Bases: ABC
Abstract interface for psyphy inference engines.
The constructor of an engine holds the settings of the method, such as the
number of steps. Everything that changes from one run to the next goes to
fit: the data, the starting point and the seed.
Methods:
| Name | Description |
|---|---|
fit |
Fit model parameters to data and return a posterior. |
fit
¶
Fit model parameters to data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
WPPM
|
Psychophysical model to fit. |
required |
data
|
TrialData
|
Observed trials. |
required |
init_params
|
dict | None
|
Starting parameter PyTree, for example |
None
|
seed
|
int | None
|
Seed for every random step of this fit. None means 0, so two fits with no seed give the same result. |
None
|
Returns:
| Type | Description |
|---|---|
Posterior
|
Posterior object wrapping fitted params and model reference. |
Source code in src/psyphy/inference/base.py
LangevinSampler
¶
Bases: InferenceEngine
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, init_params: dict | None = None, seed: int | None = None) -> MAPPosterior
Fit model parameters with Langevin dynamics (stub).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
WPPM
|
Model instance. |
required |
data
|
TrialData
|
Observed trials. |
required |
init_params
|
dict | None
|
Starting parameter PyTree. |
None
|
seed
|
int | None
|
PRNG seed for this fit. None means 0. |
None
|
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
Always. Use MAPOptimizer or NUTSSampler instead. |
Source code in src/psyphy/inference/langevin.py
LaplaceApproximation
¶
Bases: InferenceEngine
Laplace approximation around MAP estimate.
Methods:
| Name | Description |
|---|---|
fit |
Not implemented yet. |
from_map |
Construct a Gaussian approximation centered at MAP. |
fit
¶
fit(model, data, init_params: dict | None = None, seed: int | None = None) -> MAPPosterior
Fit a Laplace approximation to data (stub).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
WPPM
|
Model instance. |
required |
data
|
TrialData
|
Observed trials. |
required |
init_params
|
dict | None
|
Starting parameter PyTree for the MAP fit. |
None
|
seed
|
int | None
|
PRNG seed for this fit. None means 0. |
None
|
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
Always. Use MAPOptimizer or NUTSSampler instead. |
Source code in src/psyphy/inference/laplace.py
from_map
¶
from_map(map_posterior: MAPPosterior) -> MAPPosterior
Return posterior approximation from MAP.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
map_posterior
|
Posterior
|
Posterior object from MAP optimization. |
required |
Returns:
| Type | Description |
|---|---|
MAPPosterior
|
Posterior distribution containing Laplace approximation. |
Source code in src/psyphy/inference/laplace.py
MALASampler
¶
MALASampler(num_warmup: int = 500, num_samples: int = 1000, num_chains: int = 4, *, step_size: float, logdensity_key_seed: int = 0, prior_preconditioning: bool = True, show_progress: bool = True)
Bases: InferenceEngine
MALA (Metropolis-adjusted Langevin algorithm) using BlackJAX.
Draws samples from the full posterior p(θ | data). Returns an
:class:MCMCPosterior with the same layout as :class:NUTSSampler, so
covariance_to_arviz, :class:WPPMCovarianceFieldSamples and the
plotting functions take either.
MALA does not tune itself. The step size is fixed for the whole run, burn-in included, and there is no mass matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_warmup
|
int
|
Burn-in steps per chain. Their samples are thrown away. |
500
|
num_samples
|
int
|
Number of posterior samples per chain (after burn-in). |
1000
|
num_chains
|
int
|
Number of independent chains, run together. |
4
|
step_size
|
float
|
Size of each Langevin step. Each step moves |
required |
logdensity_key_seed
|
int
|
Seed for the fixed Monte Carlo key baked into the log density. See
:class: |
0
|
prior_preconditioning
|
bool
|
If True and the model exposes |
True
|
show_progress
|
bool
|
Display a tqdm progress bar. Requires |
True
|
Notes
sampler_stats has 2 keys, each shaped (num_chains, num_samples):
acceptance_rate: probability of accepting each proposed stepis_accepted: whether the step was accepted
MALA has no diverging, energy or n_steps, so ArviZ's energy
plot and divergence marks are not available. R-hat and effective sample
size work as for NUTS.
Examples:
Methods:
| Name | Description |
|---|---|
fit |
Run MALA and return a posterior over model parameters. |
Attributes:
| Name | Type | Description |
|---|---|---|
logdensity_key_seed |
|
|
num_chains |
|
|
num_samples |
|
|
num_warmup |
|
|
prior_preconditioning |
|
|
show_progress |
|
|
step_size |
|
Source code in src/psyphy/inference/mala.py
fit
¶
fit(model, data, init_params: dict | None = None, seed: int | None = None) -> MCMCPosterior
Run MALA and return a posterior over model parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
WPPM
|
Model instance providing log_posterior_from_data. |
required |
data
|
TrialData
|
Observed trials. |
required |
init_params
|
dict | None
|
Initial parameter PyTree, shared by all chains. If None, one draw
from the model prior with the seed. Starting from the MAP
( |
None
|
seed
|
int | None
|
Seed for chain initialization and sampling. None means 0. |
None
|
Returns:
| Type | Description |
|---|---|
MCMCPosterior
|
Samples shaped (num_chains, num_samples, *param_shape), and
|
Source code in src/psyphy/inference/mala.py
MAPOptimizer
¶
MAPOptimizer(steps: int = 500, learning_rate: float = 5e-05, momentum: float = 0.9, optimizer: GradientTransformation | None = None, *, reduction: Literal['mean', 'sum'] = 'mean', track_history: bool = True, log_every: int = 1, progress_every: int = 10, show_progress: bool = False, max_grad_norm: float | None = None)
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.
- By default the objective is scaled to a per-trial quantity
(
reduction="mean"); see thereductionparameter.
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
|
reduction
|
('mean', 'sum')
|
How to scale the objective before differentiating.
The two objectives differ by the positive constant N, so they have
the same minimizer, and with no gradient clipping they trace an
identical path under
To disable it entirely (e.g. to match a reference implementation
that does no clipping) pass Note that the model is unaffected: Because the recorded loss is per-trial under |
"mean"
|
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: |
False
|
max_grad_norm
|
float | None
|
If set, clip gradients by global norm to this value before applying
optimizer updates: Off by default. Clipping is a guard rail against rare gradient
spikes, and only behaves as one when the threshold sits above the
typical gradient norm so that it binds on a small fraction of steps.
A threshold below the typical norm binds on every step, which fixes
the step length at The typical norm is model- and data-dependent, so there is no safe
universal default; for the WPPM it is O(10^2)-O(10^3). Choose a
threshold from the observed distribution rather than a convention
borrowed from deep-learning recipes, and check
:attr: |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
clip_rate |
float | None
|
Fraction of steps on which gradient clipping bound, populated after
:meth: |
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 |
|---|---|---|
clip_rate |
float | None
|
|
log_every |
|
|
loss_history |
list[float]
|
|
loss_steps |
list[int]
|
|
max_grad_norm |
|
|
n_clipped_steps |
int
|
|
optimizer |
|
|
progress_every |
|
|
reduction |
|
|
show_progress |
|
|
steps |
|
|
track_history |
|
Source code in src/psyphy/inference/map_optimizer.py
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 | |
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, and as the base key for the MC likelihood random stream during optimization. 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
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 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 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 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 | |
get_history
¶
Return (steps, losses) recorded during the last fit when tracking was enabled.
NUTSSampler
¶
NUTSSampler(num_warmup: int = 500, num_samples: int = 1000, num_chains: int = 4, *, logdensity_key_seed: int = 0, target_acceptance_rate: float = 0.8, step_size: float | None = None, max_num_doublings: int = 8, prior_preconditioning: bool = True, show_progress: bool = True)
Bases: InferenceEngine
NUTS (No-U-Turn Sampler) using BlackJAX.
Draws samples from the full posterior p(θ | data) via gradient-based MCMC. Returns an MCMCPosterior with shape (num_chains, num_samples, *param_shape).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_warmup
|
int
|
Number of warmup/burn-in steps per chain. |
500
|
num_samples
|
int
|
Number of posterior draws per chain (after warmup). |
1000
|
num_chains
|
int
|
Number of independent chains to run. |
4
|
logdensity_key_seed
|
int
|
Seed for the fixed JAX key baked into the logdensity closure. Controls which MC noise realization is used throughout sampling. See module docstring for rationale. |
0
|
target_acceptance_rate
|
float
|
Target Metropolis acceptance rate for window adaptation (adaptive warmup only). Typical values: 0.-0.9. |
0.8
|
step_size
|
float | None
|
If None (default), use blackjax.window_adaptation to automatically tune step_size and mass matrix (adaptive warmup). If a float, skip adaptation and use this fixed step_size for all chains; warmup becomes plain burn-in and all chains are vmapped in parallel (fixed step size). The caller is responsible for choosing a suitable value. |
None
|
max_num_doublings
|
int
|
Maximum NUTS tree depth. One draw costs at most
2**max_num_doublings - 1 gradient evaluations (255 at the default;
BlackJAX's own default of 10 allows 1023). Applies to both modes,
warmup and sampling. If |
8
|
prior_preconditioning
|
bool
|
Adaptive warmup only. If True and the model exposes |
True
|
show_progress
|
bool
|
Display a tqdm progress bar during warmup and sampling.
Requires |
True
|
Notes
Two warmup modes:
Adaptive warmup (step_size=None, default): blackjax.window_adaptation adapts step_size and inverse_mass_matrix independently per chain. The adaptation state is all JAX arrays, so warmup is vmapped over chains and JIT-compiled once; sampling is likewise one vmapped, jitted lax.scan using each chain's own adapted parameters.
Fixed step size (step_size=
Examples:
Methods:
| Name | Description |
|---|---|
fit |
Run NUTS sampling and return a posterior over model parameters. |
Attributes:
| Name | Type | Description |
|---|---|---|
logdensity_key_seed |
|
|
max_num_doublings |
|
|
num_chains |
|
|
num_samples |
|
|
num_warmup |
|
|
prior_preconditioning |
|
|
show_progress |
|
|
step_size |
|
|
target_acceptance_rate |
|
Source code in src/psyphy/inference/nuts.py
fit
¶
fit(model, data, init_params: dict | None = None, seed: int | None = None) -> MCMCPosterior
Run NUTS sampling and return a posterior over model parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
WPPM
|
Model instance providing log_posterior_from_data. |
required |
data
|
TrialData
|
Observed trials. |
required |
init_params
|
dict | None
|
Initial parameter PyTree. If None, samples from the model prior using the seed. Providing MAP params as init_params dramatically reduces the warmup needed. |
None
|
seed
|
int | None
|
Master PRNG seed for chain initialization and sampling keys. None means 0. |
None
|
Returns:
| Type | Description |
|---|---|
MCMCPosterior
|
Samples shaped (num_chains, num_samples, *param_shape), sampler_stats with acceptance_rate. |
Source code in src/psyphy/inference/nuts.py
Base¶
base
¶
base.py
Abstract base class for inference engines.
Every psyphy inference engine (MAPOptimizer, NUTSSampler, LangevinSampler,
LaplaceApproximation) subclasses this base and implements
fit(model, data, init_params=None, seed=None), which returns a posterior.
Classes:
| Name | Description |
|---|---|
InferenceEngine |
Abstract interface for psyphy inference engines. |
InferenceEngine
¶
Bases: ABC
Abstract interface for psyphy inference engines.
The constructor of an engine holds the settings of the method, such as the
number of steps. Everything that changes from one run to the next goes to
fit: the data, the starting point and the seed.
Methods:
| Name | Description |
|---|---|
fit |
Fit model parameters to data and return a posterior. |
fit
¶
Fit model parameters to data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
WPPM
|
Psychophysical model to fit. |
required |
data
|
TrialData
|
Observed trials. |
required |
init_params
|
dict | None
|
Starting parameter PyTree, for example |
None
|
seed
|
int | None
|
Seed for every random step of this fit. None means 0, so two fits with no seed give the same result. |
None
|
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.
What it does: - 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, *, reduction: Literal['mean', 'sum'] = 'mean', track_history: bool = True, log_every: int = 1, progress_every: int = 10, show_progress: bool = False, max_grad_norm: float | None = None)
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.
- By default the objective is scaled to a per-trial quantity
(
reduction="mean"); see thereductionparameter.
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
|
reduction
|
('mean', 'sum')
|
How to scale the objective before differentiating.
The two objectives differ by the positive constant N, so they have
the same minimizer, and with no gradient clipping they trace an
identical path under
To disable it entirely (e.g. to match a reference implementation
that does no clipping) pass Note that the model is unaffected: Because the recorded loss is per-trial under |
"mean"
|
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: |
False
|
max_grad_norm
|
float | None
|
If set, clip gradients by global norm to this value before applying
optimizer updates: Off by default. Clipping is a guard rail against rare gradient
spikes, and only behaves as one when the threshold sits above the
typical gradient norm so that it binds on a small fraction of steps.
A threshold below the typical norm binds on every step, which fixes
the step length at The typical norm is model- and data-dependent, so there is no safe
universal default; for the WPPM it is O(10^2)-O(10^3). Choose a
threshold from the observed distribution rather than a convention
borrowed from deep-learning recipes, and check
:attr: |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
clip_rate |
float | None
|
Fraction of steps on which gradient clipping bound, populated after
:meth: |
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 |
|---|---|---|
clip_rate |
float | None
|
|
log_every |
|
|
loss_history |
list[float]
|
|
loss_steps |
list[int]
|
|
max_grad_norm |
|
|
n_clipped_steps |
int
|
|
optimizer |
|
|
progress_every |
|
|
reduction |
|
|
show_progress |
|
|
steps |
|
|
track_history |
|
Source code in src/psyphy/inference/map_optimizer.py
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 | |
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, and as the base key for the MC likelihood random stream during optimization. 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
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 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 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 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 | |
get_history
¶
Return (steps, losses) recorded during the last fit when tracking was enabled.
Langevin Samplers¶
langevin
¶
langevin.py
Langevin samplers for posterior inference.
Will lkely implement: - Overdamped (unadjusted) Langevin Algorithm (ULA) (exists in BlackJax) - Underdamped Langevin (with BAOAB splitting scheme?)
Used for posterior-aware trial placement (InfoGain).
Currently a stub: fit returns an initial posterior and runs no Langevin dynamics.
Classes:
| Name | Description |
|---|---|
LangevinSampler |
Langevin sampler (stub). |
LangevinSampler
¶
Bases: InferenceEngine
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, init_params: dict | None = None, seed: int | None = None) -> MAPPosterior
Fit model parameters with Langevin dynamics (stub).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
WPPM
|
Model instance. |
required |
data
|
TrialData
|
Observed trials. |
required |
init_params
|
dict | None
|
Starting parameter PyTree. |
None
|
seed
|
int | None
|
PRNG seed for this fit. None means 0. |
None
|
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
Always. Use MAPOptimizer or NUTSSampler instead. |
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(n, key=...) cheaply. Useful for InfoGainPlacement when only MAP fit is available.
Currently a stub: from_map returns the MAP posterior and computes no Hessian.
Classes:
| Name | Description |
|---|---|
LaplaceApproximation |
Laplace approximation around MAP estimate. |
LaplaceApproximation
¶
Bases: InferenceEngine
Laplace approximation around MAP estimate.
Methods:
| Name | Description |
|---|---|
fit |
Not implemented yet. |
from_map |
Construct a Gaussian approximation centered at MAP. |
fit
¶
fit(model, data, init_params: dict | None = None, seed: int | None = None) -> MAPPosterior
Fit a Laplace approximation to data (stub).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
WPPM
|
Model instance. |
required |
data
|
TrialData
|
Observed trials. |
required |
init_params
|
dict | None
|
Starting parameter PyTree for the MAP fit. |
None
|
seed
|
int | None
|
PRNG seed for this fit. None means 0. |
None
|
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
Always. Use MAPOptimizer or NUTSSampler instead. |
Source code in src/psyphy/inference/laplace.py
from_map
¶
from_map(map_posterior: MAPPosterior) -> MAPPosterior
Return posterior approximation from MAP.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
map_posterior
|
Posterior
|
Posterior object from MAP optimization. |
required |
Returns:
| Type | Description |
|---|---|
MAPPosterior
|
Posterior distribution containing Laplace approximation. |