EMModel & FitResult¶
EMModel is the high-level, scikit-learn-style wrapper around the hierarchical
EM fit. You give it a list of per-subject data, an objective (fit_func), the
parameter names, and (optionally) the Gaussian→natural transforms and a
simulator. EMModel.fit() returns a FitResult dataclass. Parameters are fit in
Gaussian (unbounded) space; subject_params() returns natural-space estimates
when param_xform is provided.
EMModel ¶
EMModel(all_data: Sequence[Sequence[Any]] | None, fit_func: Callable[..., float], param_names: Sequence[str], param_xform: Sequence[Callable] | None = None, simulate_func: Callable[..., Any] | None = None)
A high-level, sklearn-like interface: model = EMModel(all_data, fit_func, param_names, param_xform=[norm2beta, norm2alpha], simulate_func=...) result = model.fit(...) sim = model.simulate(...)
# Access parameter transformations: beta_transform = model.get_param_transform("beta") # or model.param_xform[0] transformed_value = beta_transform(0.5)
Source code in pyem/api.py
outfit
property
¶
Lazily-computed, cached dict of fit_func outputs for each subject.
Computed on first access after a fit/recover and cached; the cache is
invalidated whenever a new fit() or recover() runs.
get_param_transform ¶
Get parameter transformation function by parameter name or index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
param_name_or_index
|
Parameter name (str) or index (int) |
required |
Returns:
| Type | Description |
|---|---|
|
Transformation function for the specified parameter |
Raises:
| Type | Description |
|---|---|
ValueError
|
If param_xform was not provided or parameter not found |
Source code in pyem/api.py
EMfit ¶
Deprecated legacy alias for :meth:fit; returns fit(**kwargs).__dict__
(a plain dict view of the :class:FitResult). Prefer :meth:fit.
compute_integrated_bic ¶
compute_integrated_bic(nsamples: int = 500, func_output: str = 'all', nll_key: str = 'nll', ntrials_total: int | None = None) -> float
Compute integrated Bayesian Information Criterion (BICint).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nsamples
|
int
|
Number of samples for Monte Carlo integration |
500
|
func_output
|
str
|
Output type to request from fit function |
'all'
|
nll_key
|
str
|
Key to extract negative log-likelihood from fit function output |
'nll'
|
ntrials_total
|
int | None
|
Number of trials per subject used for the BIC complexity
penalty. If omitted, auto-detected from |
None
|
Returns:
| Type | Description |
|---|---|
float
|
Integrated BIC value |
Source code in pyem/api.py
compute_lme ¶
Compute Laplace approximation for log model evidence (LME).
Returns:
| Type | Description |
|---|---|
tuple[ndarray, float, ndarray]
|
Tuple of (Laplace approximation per subject, total LME, good Hessian flags) |
Source code in pyem/api.py
get_outfit ¶
Return a dictionary of outputs from the fit_func for each subject.
Source code in pyem/api.py
scipy_minimize ¶
Fit each subject independently using :func:scipy.optimize.minimize.
This is a non-hierarchical baseline (no population-level prior/M-step; each subject's initial guess and objective are independent of every other subject) — useful for comparing against the full EM fit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seed
|
int | None
|
Seed for the :func: |
None
|
Source code in pyem/api.py
recover ¶
recover(true_params: ndarray, pr_inputs: List[str], simulate_func: Callable = None, fit_kwargs: dict | None = None, **sim_kwargs) -> dict[str, Any]
Parameter recovery analysis given true parameters and simulation function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
true_params
|
ndarray
|
True parameter values (nsubjects x nparams) |
required |
pr_inputs
|
List[str]
|
Required. Names of the simulation output keys (from
|
required |
simulate_func
|
Callable
|
Simulation function (uses self.simulate_func if None) |
None
|
fit_kwargs
|
dict | None
|
Additional keyword arguments forwarded to the recovery
model's |
None
|
**sim_kwargs
|
Additional arguments for simulation |
{}
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary containing true params, estimated params, and recovery metrics. |
dict[str, Any]
|
The |
dict[str, Any]
|
each parameter (array of length |
Source code in pyem/api.py
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 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 | |
plot_recovery ¶
plot_recovery(recovery_dict: dict, show_line: bool = True, figsize: tuple | None = None, show: bool = True) -> plt.Figure
Plot parameter recovery as scatter plots of simulated vs estimated parameters. Creates 3 columns with as many rows as needed, with compact spacing and subplot sizes that scale with the grid.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
recovery_dict
|
dict
|
Output from recover() method, containing: - 'true_params' (array-like, shape [n_sims, n_params]) - 'estimated_params' (array-like, shape [n_sims, n_params]) |
required |
show_line
|
bool
|
Whether to draw x=y line |
True
|
figsize
|
tuple | None
|
Figure size |
None
|
show
|
bool
|
Call :func: |
True
|
Returns:
| Type | Description |
|---|---|
Figure
|
matplotlib Figure object |
Source code in pyem/api.py
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 | |
FitResult
dataclass
¶
FitResult(m: ndarray, inv_h: ndarray, posterior_mu: ndarray, posterior_sigma: ndarray, NPL: ndarray, NLPrior: ndarray, NLL: ndarray, convergence: bool)