leaspy.models.summary#
Model inspection objects for programmatic access to model metadata.
This module provides Summary and Info, returned by
model.summary() and model.info() respectively, along with the
TypedDict schemas for training and dataset metadata.
Both classes auto-print when their return value is discarded (e.g.
model.summary()) and stay silent when stored in a variable
(e.g. s = model.summary()). See AutoPrintMixin for details.
Classes#
Per-subject visit distribution statistics. |
|
Statistics of the training dataset, computed during |
|
Metadata about the training process, captured during |
|
Mixin that auto-prints when the object is discarded. |
|
Model configuration and training context (no parameter values). |
|
Structured summary of a Leaspy model including parameter values. |
Functions#
|
Resolve human-readable labels for a parameter axis. |
|
Calculate the number of free parameters of the model. |
|
Calculate the Bayesian Information Criterion (BIC). |
|
Calculate the Akaike Information Criterion (AIC). |
|
Calculate the Integrated Completed Likelihood (ICL) for a mixture model. |
Module Contents#
- class DatasetInfo[source]#
Bases:
TypedDictStatistics of the training dataset, computed during
fit().- visits_per_subject: VisitsPerSubject#
- class TrainingInfo[source]#
Bases:
TypedDictMetadata about the training process, captured during
fit().
- get_axis_labels(axis_name, size, feature_names=None)[source]#
Resolve human-readable labels for a parameter axis.
- Parameters:
- axis_namestr or None
Semantic axis name (
"feature","source","cluster","basis").- sizeint
Number of elements along the axis.
- feature_nameslist[str], optional
Feature names used when axis_name is
"feature".
- Returns:
- list[str] or None
Labels for the axis, or
Noneif no meaningful labels are available.
- Parameters:
- Return type:
- class AutoPrintMixin[source]#
Mixin that auto-prints when the object is discarded.
Relies on CPython reference counting: when the return value of e.g.
model.summary()is not assigned, the object is immediately garbage-collected, triggering__del__which prints it.When stored (
s = model.summary()), any public attribute access sets_printed = True, suppressing the__del__output.Subclasses must define a
_printed: boolfield (via dataclass) and a__str__method.
- get_number_of_parameters(model)[source]#
Calculate the number of free parameters of the model.
Uses the theoretical formula:
P = 3F + (F-1)*S + S*K + 4Kwhere F = features, S = sources, K = clusters.
- compute_bic(nll, num_params, n_subjects)[source]#
Calculate the Bayesian Information Criterion (BIC).
BIC = 2 * nll + P * log(N)- Parameters:
- nllfloat
Negative log-likelihood (
nll_attach).- num_paramsint
Number of free parameters.
- n_subjectsint
Number of subjects used for model fitting.
- Returns:
- float or None
The computed BIC, or
Noneif inputs are invalid.
- Parameters:
- Return type:
Optional[float]
- compute_aic(nll, num_params, n_subjects)[source]#
Calculate the Akaike Information Criterion (AIC).
AIC = 2 * nll + 2 * P- Parameters:
- nllfloat
Negative log-likelihood (
nll_attach).- num_paramsint
Number of free parameters.
- n_subjectsint
Number of subjects used for model fitting (not used in AIC but included for consistency).
- Returns:
- float or None
The computed AIC, or
Noneif inputs are invalid.
- Parameters:
- Return type:
Optional[float]
- compute_icl(bic, model)[source]#
Calculate the Integrated Completed Likelihood (ICL) for a mixture model.
ICL = BIC - sum_i sum_k pi_ik * log(pi_ik)where
pi_ikis the posterior probability that individual i belongs to cluster k (classification responsibility). The entropy term penalises poorly-separated clusters on top of the BIC complexity penalty, so models with crisp cluster assignments are preferred.Returns
Nonefor non-mixture models (i.e. whenmodel.n_clustersis not set), or whenbicitself isNone.- Parameters:
- bicfloat or None
Pre-computed BIC value for this model.
- modelBaseModel
A fitted Leaspy model. Mixture-specific fields are read from
model.state(per-individualtau,xi,sources) and frommodel.parameters(cluster priorstau_mean,tau_std,xi_mean,xi_std,sources_mean,probs).
- Returns:
- float or None
The computed ICL, or
Noneif not applicable.
- Parameters:
- Return type:
Optional[float]
- class Info[source]#
Bases:
AutoPrintMixinModel configuration and training context (no parameter values).
Returned by
model.info(). Auto-prints when discarded; provides programmatic access when stored in a variable.Examples
>>> model.info() # prints info >>> i = model.info() # store for programmatic access >>> i.n_subjects # 150 >>> i.help() # list available attributes
- training_info: TrainingInfo#
- dataset_info: DatasetInfo#
- classmethod from_model(model)[source]#
Build an
Infofrom a model instance.Works on un-fitted models too: parameter counts and DAG-derived fields are simply omitted when the model state isn’t available yet.
- property n_burn_in_iter: int | None#
Number of burn-in (memory-less) iterations.
- Return type:
Optional[int]
- property hyperparameter: dict#
Model hyperparameters (e.g. source_dimension, obs_model).
- Return type:
- property latent_variable_distributions: dict#
Latent variable prior distributions, grouped by population/individual.
- Return type:
- property n_subjects: int | None#
Number of subjects in the training dataset.
- Return type:
Optional[int]
- property n_observations: int | None#
Total number of observed data points.
- Return type:
Optional[int]
- property visits_per_subject: VisitsPerSubject | None#
Per-subject visit distribution statistics.
- Return type:
Optional[VisitsPerSubject]
- class Summary[source]#
Bases:
AutoPrintMixinStructured summary of a Leaspy model including parameter values.
Returned by
model.summary(). Auto-prints when discarded; provides programmatic access when stored in a variable.Examples
>>> model.summary() # prints the formatted summary >>> s = model.summary() # store for programmatic access >>> s.algorithm # 'mcmc_saem' >>> s.get_param('tau_std') # tensor([10.5]) >>> s.help() # list available attributes
- training_info: TrainingInfo#
- dataset_info: DatasetInfo#
- property n_subjects: int | None#
Number of subjects in the training dataset.
- Return type:
Optional[int]