dynamirt.gllvm package

Subpackages

Submodules

dynamirt.gllvm.families module

dynamirt.gllvm.families.Bernoulli() → Callable

Family factory for binary responses using a logit link.

Returns:

A family function with signature

family(eta, ctx) -> dist.Bernoulli where eta is the linear predictor on the logit scale.

dynamirt.gllvm.families.Gaussian(sigma_prior: Distribution | None = None) → Callable

Family factory for continuous responses with a Gaussian likelihood.

A per-variable standard deviation sigma is sampled from sigma_prior.

Parameters:

sigma_prior – Prior on the observation-level standard deviation. Expanded to shape (1, n_var). Defaults to HalfNormal(1) if None.

Returns:

A family function with signature

family(eta, ctx) -> dist.Normal where eta is the conditional mean.

dynamirt.gllvm.families.NegBinom(conc_prior: Distribution | None = None) → Callable

Family factory for overdispersed count responses using a log link.

A per-variable concentration (inverse overdispersion) parameter is sampled from conc_prior.

Parameters:

conc_prior – Prior on the concentration parameter. Expanded to shape (1, n_var). Defaults to HalfNormal(1) if None.

Returns:

A family function with signature

family(eta, ctx) -> dist.NegativeBinomial2 where eta is the linear predictor on the log scale.

dynamirt.gllvm.loadings module

dynamirt.gllvm.loadings.Confirmatory(Q: Array | ndarray | bool | number | bool | int | float | complex, positive_anchors: Array | ndarray | bool | number | bool | int | float | complex = None, free_prior: Distribution | None = None, positive_prior: Distribution | None = None) → Callable

Loading matrix factory for confirmatory factor analysis.

Parameters:
  • Q – Binary (n_var, n_latent) array. Q[i, j] = 1 means item i is allowed to load on factor j; 0 means the loading is fixed at 0.

  • positive_anchors – Optional binary array of the same shape as Q. Marks a subset of Q’s nonzero entries as positive-only anchor loadings. If None, all free loadings are unconstrained in sign. Defaults to None.

  • free_prior – Prior distribution for the unconstrained loadings. Defaults to Normal(0, 1) if None.

  • positive_prior – Prior distribution for the positive anchor loadings. Defaults to LogNormal(0, 0.5) if None.

Returns:

A loadings(ctx) function that returns the (n_var, n_latent) loading matrix

dynamirt.gllvm.loadings.Fixed() → Callable

Loading matrix factory that fixes all loadings to 1 so every item loads equally on every latent factor.

Returns:

A loadings(ctx) function that returns a (n_var, n_latent) array of ones.

dynamirt.gllvm.loadings.Full(prior: Distribution | None = None) → Callable

Loading matrix factory with all loadings drawn from prior. For the default Normal(0, 1) this results in an unconstrained loading matrix and is equivalent to Confirmatory with Q of all ones and no positive anchors and will lead to rotational and signed-permutation invariances in most cases.

Parameters:

prior – Prior distribution for each loading. Defaults to Normal(0, 1).

Returns:

A loadings(ctx) function that returns a (n_var, n_latent) array sampled entrywise from prior.

dynamirt.gllvm.loadings.Sparsity(tau0=1.0, slab_scale: float = 1.0, slab_df: float = 4.0) → Callable

Loading matrix factory with regularized horseshoe sparsity prior.

Applies the regularised horseshoe prior of Piironen & Vehtari (2017) to the loading matrix, encouraging most loadings toward zero while allowing a sparse subset to remain large.

Reference:

Juho Piironen. Aki Vehtari (2017). Electron. J. Statist. 11 (2) 5018 - 5051 https://doi.org/10.1214/17-EJS1337SI

Parameters:
  • tau0 – Global shrinkage scale. A sensible default follows Eq. 3.12: p0 / (D - p0) * sigma / sqrt(n) where p0 is the expected number of non-zero loadings and D is the total number of loadings. Defaults to 1.0.

  • slab_scale – Typical magnitude of a loading that escapes shrinkage. Controls the width of the regularising slab. Defaults to 1.0.

  • slab_df – Degrees of freedom of the Student-t slab controlling how heavy-tailed the non-zero loadings may be. Defaults to 4.0.

Returns:

A loadings(ctx) function that returns the (n_var, n_latent) loading matrix

dynamirt.gllvm.model module

dynamirt.gllvm.model.gllvm(responses: None | Array | ndarray | bool | number | bool | int | float | complex, covariates: Mapping[str, Array | ndarray | bool | number | bool | int | float | complex], family: Callable, n_latent: int = 1, full_rank_regression: List[Callable] = None, latent_regression: List[Callable] = None, loadings: Callable | None = None, n_obs: int = None, n_var: int = None, latent_site_name: str = 'u')

Generalized linear latent variable model.

Regresses n_var responses jointly on a full-rank linear predictor plus a reduced-rank (n_latent) term:

mu = eta + u @ loadings.T

where eta is the sum of full-rank regression terms, u collects the latent scores, and loadings is a (n_var, n_latent) matrix. NaN entries in responses are masked out of the likelihood.

Parameters:
  • responses – Observation matrix of shape (n_obs, n_var). NaN marks missing data. Pass None for prior-predictive sampling, in which case n_obs and n_var must be given explicitly.

  • covariates – Mapping from covariate name to array of length n_obs (or broadcastable scalar). Two keys are added automatically: "one_" (constant 1, for intercepts) and "row_" (observation index, a stand-in for a respondent ID when n_obs equals the number of respondents).

  • family – Called as family(mu, ctx) and must return a numpyro.distributions.Distribution over responses.

  • n_latent – Dimensionality of the latent space. Defaults to 1.

  • full_rank_regression – List of term callables, each with signature term(ctx, n_var) -> (n_obs, n_var). Their outputs are summed into the full-rank predictor eta.

  • latent_regression – List of term callables, each with signature term(ctx, n_latent) -> (n_obs, n_latent). Their outputs are summed into the latent scores u.

  • loadings – A loading-matrix factory loadings(ctx) -> (n_var, n_latent). Defaults to Full() (unconstrained).

  • n_obs – Number of observations. Required when responses is None.

  • n_var – Number of response variables. Required when responses is None.

  • latent_site_name – Name of the NumPyro deterministic site that stores the latent scores u. Defaults to "u".

dynamirt.gllvm.parameters module

class dynamirt.gllvm.parameters.Param(prior: Distribution | float, by_group: Literal['shared', 'free'] | Pool = 'shared', by_variable: Literal['shared', 'free'] | Pool = 'shared')

Bases: object

Broadcastable parameter with optional non-centered partial pooling.

Wraps a scalar constant or a prior distribution and manages its expansion along the group and variable/latent axes of a term. When either axis is set to a Pool instance, a non-centered parameterisation is used: a shared hyper-mean and hyper-scale are sampled, and the raw draw is shifted and scaled accordingly.

Variables:
  • prior (numpyro.distributions.distribution.Distribution | float) – Either a fixed scalar value (no sampling) or a numpyro.distributions.Distribution used as the prior.

  • by_group (Literal['shared', 'free'] | dynamirt.gllvm.parameters.Pool) – "shared" broadcasts a single value across groups, "free" samples independently per group, and a Pool instance applies non-centered partial pooling across groups. Defaults to "shared".

  • by_variable (Literal['shared', 'free'] | dynamirt.gllvm.parameters.Pool) – Same as by_group but along the variable/latent axis. Defaults to "shared".

apply_partial_pooling(name, raw, n_groups, n_target, trailing=())
by_group: Literal['shared', 'free'] | Pool = 'shared'
by_variable: Literal['shared', 'free'] | Pool = 'shared'
prior: Distribution | float
sample_raw(name, n_groups, n_target, trailing=())
class dynamirt.gllvm.parameters.Pool(loc: Param = Param(prior=<numpyro.distributions.continuous.Normal object at 0x7c32059c9af0 with batch shape () and event shape ()>, by_group='shared', by_variable='shared'), scale: Param = Param(prior=<numpyro.distributions.continuous.HalfNormal object at 0x7c32059c9eb0 with batch shape () and event shape ()>, by_group='shared', by_variable='shared'), transform: Transform = <factory>)

Bases: object

Hyperparameters for non-centered partial pooling in Param.

Variables:
  • loc (dynamirt.gllvm.parameters.Param) – Param for the hyper-mean. Defaults to Normal(0, 1).

  • scale (dynamirt.gllvm.parameters.Param) – Param for the hyper-scale. Defaults to HalfNormal(0.5).

  • transform (numpyro.distributions.transforms.Transform) – Bijective transform applied after the affine loc + scale * raw step (e.g. ExpTransform to constrain the result to be positive). Defaults to the identity transform.

loc: Param = Param(prior=<numpyro.distributions.continuous.Normal object at 0x7c32059c9af0 with batch shape () and event shape ()>, by_group='shared', by_variable='shared')
scale: Param = Param(prior=<numpyro.distributions.continuous.HalfNormal object at 0x7c32059c9eb0 with batch shape () and event shape ()>, by_group='shared', by_variable='shared')
transform: Transform

Module contents