Random effects (m.effects)

Building blocks for varying (random) effects.

m.effects.diag_pre_multiply

m.effects.diag_pre_multiply(v, m)

Dot product of a diagonal matrix with a matrix.

Argument Type Description
v 1D jax array Vector of diagonal elements.
m 2D jax array Matrix to be multiplied.

Returns

  • 2D jax array: Dot product of a diagonal matrix with a matrix.

m.effects.nested_varying_effects

m.effects.nested_varying_effects(
    N_vars,
    names,
    N_groups,
    group_ids,
    mu=None,
    sigma=None,
    L_corr=None,
    corr=None,
    centered=False,
    sample=False,
)

Nested varying effects model for arbitrary hierarchy depth and variable count.

N_vars is the number of modelled variables (intercept + slopes) and is independent of the number of levels. You can have 4 variables across 2 levels, or 2 variables across 5 levels — they are separate dimensions.

Each level shares the same N_vars because effects at child levels are deviations from the parent on the same set of variables: child_effect = parent_effect[parent_id] + deviation # both shape (N_vars,)

Parent structure is derived automatically from group_ids: for each unit g at level i, its parent = group_ids[i-1] at any observation where group_ids[i] == g. This works for balanced and unbalanced nested designs with no extra arguments.

Argument Type Description
N_vars int Number of variables per level (intercept + number of slopes). Independent of the number of levels — e.g., N_vars=4 with 2 levels is valid.
names list[str] Name for each level, top to bottom.
N_groups list[int] Number of units at each level, top to bottom.
group_ids list[array] One obs-level index array per level, top to bottom. group_ids[i][obs] = which level-i group observation obs belongs to.
mu list[array|None] | None Top-level global mean override (only element 0 is used). If None, global intercept and beta are auto-sampled.
sigma list[dist|None] | None Per-level scale priors, length = n_levels. None elements fall back to Exponential(1).
L_corr list[dist|None] | None Per-level Cholesky-factor priors (non-centered only), length = n_levels. None → LKJCholesky(N_vars, 2).
corr list[dist|None] | None Per-level correlation-matrix priors (centered only), length = n_levels. None → LKJ(N_vars, 2).
centered bool Use centered parameterization. Defaults to False.
sample bool Sample from posterior wrapper. Defaults to False.

Returns

  • (varying_intercepts, varying_slopes) arrays, one value per observation.

Examples

2 levels, 2 variables (intercept + 1 slope) — most common case a, b = m.effects.nested_varying_effects( N_vars=2, names=[“region”, “group”], N_groups=[5, 20], group_ids=[region_id, group_id], )

3 levels, 2 variables — N_vars unchanged as levels increase a, b = m.effects.nested_varying_effects( N_vars=2, names=[“country”, “region”, “group”], N_groups=[3, 10, 40], group_ids=[country_id, region_id, group_id], )

2 levels, 4 variables (intercept + 3 slopes) — N_vars > n_levels is valid a, b1, b2, b3 = m.effects.nested_varying_effects( N_vars=4, names=[“region”, “group”], N_groups=[5, 20], group_ids=[region_id, group_id], )

centered parameterization a, b = m.effects.nested_varying_effects( N_vars=2, names=[“region”, “group”], N_groups=[5, 20], group_ids=[region_id, group_id], centered=True, )

custom priors per level a, b = m.effects.nested_varying_effects( N_vars=2, names=[“region”, “group”], N_groups=[5, 20], group_ids=[region_id, group_id], sigma=[ m.dist.exponential(2, shape=(2,)), # region level m.dist.exponential(0.5, shape=(2,)), # group level ], )

m.effects.random_centered

m.effects.random_centered(sigma, cor_mat, offset_mat)

Generate the centered matrix of random factors

Argument Type Description
sigma vector Prior, vector of length N
cor_mat 2D array correlation matrix, cholesky_factor_corr of dim N, N
offset_mat 2D array matrix of offsets, matrix of dim N*k

Returns

  • 2D array: Random factors, shape (k, N).

m.effects.varying_effects

m.effects.varying_effects(
    N_vars,
    N_group,
    group_id,
    group_name='age',
    alpha_bar=None,
    beta_bar=None,
    sd_intercept=None,
    sd_beta=None,
    corr=None,
    centered=False,
    sample=False,
)

Models a varying intercept and varying slope for a continuous predictor.

This uses a “centered” parameterization to sample a correlated [intercept, slope] pair for each group from a Multivariate Normal distribution.

Argument Type Description
N_vars int The total number of covariates for the model.
N_group int The total number of unique groups (e.g., 7 subjects).
group_id array An array of integer indices for the group of each observation.
group_name str A descriptive name for the grouping factor.
alpha_bar array or None Mean for the intercept dimension. Pass jnp.zeros(1) to produce zero-centered random effects when fixed effects are specified externally. Defaults to None (samples global_intercept ~ N(0,1)).
beta_bar array or None Mean for the slope dimensions. Pass jnp.zeros(N_vars) to produce zero-centered random effects. Defaults to None (samples global_beta ~ N(0,1)).
sd_intercept distribution The hyperprior for the standard deviation of the intercepts.
sd_beta distribution The hyperprior for the standard deviation of the slopes.
corr distribution The prior for the correlation matrix.
centered bool Whether to use a centered parameterization. Defaults to False.
sample bool Whether to sample from the posterior. Defaults to False.

Returns

  • A tuple containing two arrays: - varying_intercepts (array): The intercept for each observation. - varying_slopes (array): The slope for each observation.

m.effects.varying_intercept

m.effects.varying_intercept(
    N_groups,
    group_id,
    a_bar=None,
    sigma=None,
    group_name='group',
    sample=False,
)

Varying intercepts model using a non-centered parameterization.

Argument Type Description
N_groups int The total number of unique groups.
group_id array An array of integer indices specifying the group for each observation.
a_bar distribution The hyperprior for the mean of the group-level intercepts.
sigma distribution The hyperprior for the standard deviation of the group-level intercepts.

Returns

  • An array of the appropriate group-level intercepts for each observation.

m.effects.varying_intercept_non_centered

m.effects.varying_intercept_non_centered(
    N_groups,
    group_idx,
    a_bar=None,
    sigma=None,
    group_name='group',
    sample=False,
)

Varying intercepts model using a NON-CENTERED parameterization.

Argument Type Description
N_groups int The total number of unique groups.
group_idx array An array of integer indices specifying the group for each observation.
a_bar distribution The hyperprior for the mean of the group-level intercepts.
sigma distribution The hyperprior for the standard deviation of the group-level intercepts.

Returns

  • An array of the appropriate group-level intercepts for each observation.

m.effects.varying_intercept_slope

m.effects.varying_intercept_slope(
    N_group,
    group,
    global_intercept,
    global_slope,
    group_name='group',
    group_std=None,
    L_corr=None,
    sample=False,
)

Models a varying intercept and varying slope for a continuous predictor.

This uses a “centered” parameterization to sample a correlated [intercept, slope] pair for each group from a Multivariate Normal distribution.

Argument Type Description
N_groups int The total number of unique groups (e.g., 20 schools).
group_idx array An array of integer indices for the group of each observation.
global_intercept_mean float or distribution The hyperprior for the mean of the intercepts.
global_slope_mean float or distribution The hyperprior for the mean of the slopes.
group_name str A descriptive name for the grouping factor.
group_std distribution Prior for the standard deviations of the intercept and slope. Must have shape (2,). Defaults to Exponential(1).
L_corr distribution Prior for the Cholesky factor of the 2x2 correlation matrix between the intercept and slope. Defaults to LKJ(2, 2).

Returns

  • A tuple containing two arrays: - varying_intercepts (array): The intercept for each observation. - varying_slopes (array): The slope for each observation.

m.effects.varying_intercept_slope_non_centered

m.effects.varying_intercept_slope_non_centered(
    N_groups,
    group_idx,
    global_intercept_mean,
    global_slope_mean,
    group_name='group',
    sigma=None,
    L_Rho=None,
    sample=False,
)

Models a varying intercept and varying slope using a NON-CENTERED parameterization.

Argument Type Description
N_groups int The total number of unique groups.
group_idx array An array of integer indices for the group of each observation.
global_intercept_mean float or distribution The hyperprior for the mean of the intercepts.
global_slope_mean float or distribution The hyperprior for the mean of the slopes.
group_name str A descriptive name for the grouping factor.
sigma distribution Prior for the standard deviations of the intercept and slope. Must have shape (2,). Defaults to Exponential(1).
L_Rho distribution Prior for the Cholesky factor of the 2x2 correlation matrix. Defaults to LKJCholesky(2, 2).

Returns

  • A tuple containing two arrays: - varying_intercepts (array): The intercept for each observation. - varying_slopes (array): The slope for each observation.

m.effects.varying_slope

m.effects.varying_slope(
    category_idx,
    grouping_idx,
    num_categories,
    num_groups,
    L_Rho=None,
    sigma=None,
    group_name='group',
    sample=False,
)

Generates hierarchical varying effects using a non-centered parameterization.

This is a general-purpose component for building hierarchical models. It models a set of related effects (e.g., slopes or intercepts) that vary by a grouping factor. The effects for each group are “pooled” together, meaning the model learns a shared distribution for them, allowing information to be shared among groups.

This is useful for modeling: - The effect of different treatments (‘categories’) varying across subjects (‘groups’). - The performance of students (‘groups’) across different school subjects (‘categories’). - The changing approval rating (‘category’) of a policy over time in different countries (‘groups’).

Argument Type Description
category_idx array An array of integer indices for the category of the effect for each observation. For example, if you have 4 treatments, this would be an array of numbers from 0 to 3.
grouping_idx array An array of integer indices for the grouping factor for each observation (e.g., subject ID, school ID, country ID).
num_categories int The total number of unique categories for the effects (e.g., 4 treatments). This defines the dimensionality of the effects.
num_groups int The total number of unique groups (e.g., 7 subjects).
L_Rho distribution The prior for the Cholesky factor of the correlation matrix between the effects. If None, defaults to a weakly informative LKJCholesky(num_categories, 2) prior.
sigma distribution The prior for the standard deviations (scales) of the effects for each category. If None, defaults to a weakly informative Exponential(1) prior.
group_name str A descriptive name for the grouping factor, used to create unique parameter names in the model trace. Defaults to ‘group’.

Returns

  • An array containing the specific varying effect for each corresponding observation in the input arrays.

m.effects.varying_slope_non_centered

m.effects.varying_slope_non_centered(
    category_idx,
    grouping_idx,
    num_categories,
    num_groups,
    L_Rho=None,
    sigma=None,
    group_name='group',
    sample=False,
)

Generates hierarchical varying effects for CATEGORIES using a non-centered parameterization.