General Principles
To model hierarchies where groups are themselves nested within larger units (e.g., students within classes, which are within schools), we use Nested Varying Effects . This allows the model to share information across different levels of the hierarchy, improving estimates through multilevel pooling.
Note
We have the same considerations as for Varying Intercepts and Varying Slopes .
In a nested model, a parameter at one level (e.g., a group-level intercept) becomes the mean for the distribution of parameters at a lower level (e.g., individual-level intercepts).
This structure is often represented using multiple indices: j for the group and k(j) for the super-group (e.g., region) that group j belongs to.
To capture the correlation between multiple varying effects (e.g., intercept and slope) at any level of the hierarchy, we use a Multivariate Normal (MVN) distribution.
Non-centered parameterization is highly recommended for nested models to avoid βfunnelβ geometries that can hinder MCMC sampling.
Example
Below is an example of a nested model with both varying intercepts and varying slopes . We model the outcome y for individuals in groups nested within regions. The relationship between x and y varies at both levels.
Code
from BayesForge import bf
import jax.numpy as jnp
import numpy as np
# Setup device -----------------------------------------------
m = bf(platform= 'cpu' )
# Load data β column args are mapped automatically from the DataFrame.
# N_groups and N_regions are dataset-specific; provide them as defaults.
data_path = m.load.sim_nested_effects(only_path= True )
m.data(data_path)
# Define model ------------------------------------------------
def model_nested(y, x, group_id, region_id, N_groups= 20 , N_regions= 5 ):
sigma = m.dist.exponential(1 , name= 'sigma' )
# 1. Region level
mu_global = jnp.stack([m.dist.normal(5 , 2 , name= 'global_intercept' ),
m.dist.normal(- 1 , 1 , name= 'global_beta' )])
sigma_reg = m.dist.exponential(1 , shape= (2 ,), name= 'sigma_region' )
corr_reg = m.dist.lkj(2 , 2 , name= 'corr_region' )
cov_reg = jnp.diag(sigma_reg) @ corr_reg @ jnp.diag(sigma_reg)
region_effects = m.dist.multivariate_normal(
mu_global, cov_reg, shape= (N_regions,), name= 'region_effects'
)
# 2. Group level β parent mapping via JAX scatter (traceable under pmap)
group_to_region = jnp.zeros(N_groups, dtype= region_id.dtype).at[group_id].set (region_id)
sigma_grp = m.dist.exponential(1 , shape= (2 ,), name= 'sigma_group' )
corr_grp = m.dist.lkj(2 , 2 , name= 'corr_group' )
cov_grp = jnp.diag(sigma_grp) @ corr_grp @ jnp.diag(sigma_grp)
group_effects = m.dist.multivariate_normal(
region_effects[group_to_region], cov_grp, name= 'group_effects'
)
mu_est = group_effects[group_id, 0 ] + group_effects[group_id, 1 ] * x
m.dist.normal(mu_est, sigma, obs= y)
# Run sampler ------------------------------------------------
m.fit(model_nested, num_samples= 1000 , num_warmup= 500 , num_chains= 1 )
m.summary()
An NVIDIA GPU may be present on this machine, but a CUDA-enabled jaxlib is not installed. Falling back to cpu.
bf v 0.0.60 package loaded
jax.local_device_count(cpu) = 32
0%| | 0/1500 [00:00<?, ?it/s]warmup: 5%|β | 75/1500 [00:01<00:37, 37.89it/s, 15 steps of size 9.43e-02. acc. prob=0.77]warmup: 10%|β | 154/1500 [00:02<00:15, 88.46it/s, 63 steps of size 1.28e-01. acc. prob=0.77]warmup: 18%|ββ | 275/1500 [00:02<00:06, 186.78it/s, 7 steps of size 6.26e-02. acc. prob=0.78]warmup: 29%|βββ | 432/1500 [00:02<00:03, 340.61it/s, 15 steps of size 2.57e-01. acc. prob=0.79]sample: 38%|ββββ | 569/1500 [00:02<00:01, 481.41it/s, 15 steps of size 3.23e-01. acc. prob=0.88]sample: 48%|βββββ | 715/1500 [00:02<00:01, 641.09it/s, 15 steps of size 3.23e-01. acc. prob=0.85]sample: 55%|ββββββ | 819/1500 [00:02<00:00, 715.99it/s, 15 steps of size 3.23e-01. acc. prob=0.85]sample: 64%|βββββββ | 955/1500 [00:02<00:00, 852.32it/s, 15 steps of size 3.23e-01. acc. prob=0.83]sample: 73%|ββββββββ | 1093/1500 [00:02<00:00, 975.23it/s, 15 steps of size 3.23e-01. acc. prob=0.84]sample: 82%|βββββββββ | 1232/1500 [00:02<00:00, 1077.80it/s, 15 steps of size 3.23e-01. acc. prob=0.84]sample: 92%|ββββββββββ| 1383/1500 [00:02<00:00, 1190.41it/s, 15 steps of size 3.23e-01. acc. prob=0.84]sample: 100%|ββββββββββ| 1500/1500 [00:03<00:00, 490.94it/s, 15 steps of size 3.23e-01. acc. prob=0.84]
corr_group[0, 0]
1.00
0.00
1.00
1.00
1000.00
1000.00
NaN
0.00
NaN
corr_group[0, 1]
0.42
0.23
0.00
0.76
856.82
764.04
NaN
0.01
0.01
corr_group[1, 0]
0.42
0.23
0.00
0.76
856.82
764.04
NaN
0.01
0.01
corr_group[1, 1]
1.00
0.00
1.00
1.00
1128.06
967.16
NaN
0.00
0.00
corr_region[0, 0]
1.00
0.00
1.00
1.00
1000.00
1000.00
NaN
0.00
NaN
...
...
...
...
...
...
...
...
...
...
sigma
0.49
0.02
0.46
0.52
2001.70
771.88
NaN
0.00
0.00
sigma_group[0]
0.42
0.09
0.30
0.58
819.39
483.68
NaN
0.00
0.00
sigma_group[1]
0.26
0.07
0.17
0.37
537.01
648.04
NaN
0.00
0.00
sigma_region[0]
1.02
0.40
0.55
1.79
978.02
778.40
NaN
0.01
0.02
sigma_region[1]
0.48
0.24
0.22
0.90
696.84
583.35
NaN
0.01
0.01
65 rows Γ 9 columns
Code
# Data and m object carry over from the Centered tab above.
def model_nested_noncentered(y, x, group_id, region_id, N_groups= 20 , N_regions= 5 ):
sigma = m.dist.exponential(1 , name= 'sigma' )
# 1. Region level
mu_global = jnp.stack([m.dist.normal(5 , 2 , name= 'global_intercept' ),
m.dist.normal(- 1 , 1 , name= 'global_beta' )])
sigma_region = m.dist.exponential(1 , shape= (2 ,), name= 'sigma_region' )
L_region = m.dist.lkj_cholesky(2 , 2 , name= 'L_region' )
z_region = m.dist.normal(0 , 1 , name= 'z_region' , shape= (2 , N_regions))
region_effects = mu_global + ((sigma_region[..., None ] * L_region) @ z_region).T
# 2. Group level β parent mapping via JAX scatter (traceable under pmap)
pid = jnp.zeros(N_groups, dtype= region_id.dtype).at[group_id].set (region_id)
sigma_group = m.dist.exponential(1 , shape= (2 ,), name= 'sigma_group' )
L_group = m.dist.lkj_cholesky(2 , 2 , name= 'L_group' )
z_group = m.dist.normal(0 , 1 , name= 'z_group' , shape= (2 , N_groups))
group_effects = region_effects[pid] + ((sigma_group[..., None ] * L_group) @ z_group).T
mu_est = group_effects[group_id, 0 ] + group_effects[group_id, 1 ] * x
m.dist.normal(mu_est, sigma, obs= y)
m.fit(model_nested_noncentered, num_samples= 1000 , num_warmup= 500 , num_chains= 1 )
m.summary()
0%| | 0/1500 [00:00<?, ?it/s]warmup: 5%|β | 75/1500 [00:02<00:47, 30.11it/s, 255 steps of size 2.03e-02. acc. prob=0.76]warmup: 11%|β | 163/1500 [00:02<00:17, 76.18it/s, 255 steps of size 3.32e-02. acc. prob=0.77]warmup: 19%|ββ | 279/1500 [00:02<00:07, 154.05it/s, 255 steps of size 3.41e-02. acc. prob=0.78]warmup: 27%|βββ | 411/1500 [00:02<00:04, 262.56it/s, 63 steps of size 1.18e-01. acc. prob=0.79] sample: 36%|ββββ | 537/1500 [00:02<00:02, 379.63it/s, 127 steps of size 4.07e-02. acc. prob=0.91]sample: 44%|βββββ | 655/1500 [00:02<00:01, 494.15it/s, 127 steps of size 4.07e-02. acc. prob=0.93]sample: 52%|ββββββ | 775/1500 [00:03<00:01, 613.67it/s, 127 steps of size 4.07e-02. acc. prob=0.92]sample: 60%|ββββββ | 901/1500 [00:03<00:00, 739.61it/s, 127 steps of size 4.07e-02. acc. prob=0.92]sample: 69%|βββββββ | 1029/1500 [00:03<00:00, 857.40it/s, 127 steps of size 4.07e-02. acc. prob=0.92]sample: 77%|ββββββββ | 1158/1500 [00:03<00:00, 959.96it/s, 127 steps of size 4.07e-02. acc. prob=0.92]sample: 85%|βββββββββ | 1278/1500 [00:03<00:00, 1019.04it/s, 127 steps of size 4.07e-02. acc. prob=0.92]sample: 94%|ββββββββββ| 1409/1500 [00:03<00:00, 1095.40it/s, 127 steps of size 4.07e-02. acc. prob=0.92]sample: 100%|ββββββββββ| 1500/1500 [00:03<00:00, 409.12it/s, 127 steps of size 4.07e-02. acc. prob=0.92]
L_group[0, 0]
1.00
0.00
1.00
1.00
1000.00
1000.00
NaN
0.00
NaN
L_group[0, 1]
0.00
0.00
0.00
0.00
1000.00
1000.00
NaN
0.00
NaN
L_group[1, 0]
0.43
0.23
0.03
0.77
590.42
643.52
NaN
0.01
0.01
L_group[1, 1]
0.86
0.12
0.64
1.00
575.43
643.52
NaN
0.00
0.00
L_region[0, 0]
1.00
0.00
1.00
1.00
1000.00
1000.00
NaN
0.00
NaN
...
...
...
...
...
...
...
...
...
...
z_region[1, 0]
-0.69
0.61
-1.72
0.24
662.78
677.24
NaN
0.02
0.02
z_region[1, 1]
1.03
0.68
-0.04
2.13
865.79
687.28
NaN
0.02
0.02
z_region[1, 2]
-0.57
0.64
-1.58
0.45
712.00
699.23
NaN
0.02
0.02
z_region[1, 3]
0.38
0.77
-0.90
1.53
785.38
741.84
NaN
0.03
0.02
z_region[1, 4]
0.01
0.70
-1.03
1.20
611.67
645.79
NaN
0.03
0.02
65 rows Γ 9 columns
Code
from BayesForge import bf
import jax.numpy as jnp
# Setup device -----------------------------------------------
m = bf(platform= 'cpu' )
# Load data β column args are mapped automatically from the DataFrame.
# N_groups and N_regions are dataset-specific; provide them as defaults.
data_path = m.load.sim_nested_effects(only_path= True )
m.data(data_path)
# group_ids: one obs-level index array per level, top to bottom.
# Parent structure is derived automatically from these arrays.
def model_nested_builtin(y, x, group_id, region_id, N_groups= 20 , N_regions= 5 ):
sigma = m.dist.exponential(1 , name= 'sigma' )
a_g_est, b_g_est = m.effects.nested_varying_effects(
N_vars= 2 ,
names= ["region" , "group" ],
N_groups= [N_regions, N_groups],
group_ids= [region_id, group_id],
centered= False ,
)
mu_est = a_g_est + b_g_est * x
m.dist.normal(mu_est, sigma, obs= y)
m.fit(model_nested_builtin, num_samples= 1000 , num_warmup= 500 , num_chains= 1 )
m.summary()
jax.local_device_count(cpu) = 32
0%| | 0/1500 [00:00<?, ?it/s]warmup: 5%|β | 75/1500 [00:02<00:48, 29.63it/s, 255 steps of size 1.71e-02. acc. prob=0.76]warmup: 11%|β | 165/1500 [00:02<00:17, 76.05it/s, 1023 steps of size 1.22e-02. acc. prob=0.77]warmup: 20%|ββ | 295/1500 [00:02<00:07, 162.70it/s, 127 steps of size 7.14e-02. acc. prob=0.78]warmup: 29%|βββ | 439/1500 [00:02<00:03, 280.04it/s, 127 steps of size 7.22e-02. acc. prob=0.78]sample: 38%|ββββ | 576/1500 [00:02<00:02, 406.36it/s, 127 steps of size 3.83e-02. acc. prob=0.89]sample: 47%|βββββ | 712/1500 [00:03<00:01, 541.70it/s, 255 steps of size 3.83e-02. acc. prob=0.85]sample: 57%|ββββββ | 856/1500 [00:03<00:00, 692.83it/s, 127 steps of size 3.83e-02. acc. prob=0.88]sample: 66%|βββββββ | 986/1500 [00:03<00:00, 810.30it/s, 63 steps of size 3.83e-02. acc. prob=0.89] sample: 76%|ββββββββ | 1138/1500 [00:03<00:00, 964.28it/s, 63 steps of size 3.83e-02. acc. prob=0.89]sample: 86%|βββββββββ | 1289/1500 [00:03<00:00, 1091.86it/s, 127 steps of size 3.83e-02. acc. prob=0.90]sample: 95%|ββββββββββ| 1421/1500 [00:03<00:00, 1149.28it/s, 127 steps of size 3.83e-02. acc. prob=0.90]sample: 100%|ββββββββββ| 1500/1500 [00:03<00:00, 417.85it/s, 255 steps of size 3.83e-02. acc. prob=0.90]
L_corr_group[0, 0]
1.00
0.00
1.00
1.00
1000.00
1000.00
NaN
0.00
NaN
L_corr_group[0, 1]
0.00
0.00
0.00
0.00
1000.00
1000.00
NaN
0.00
NaN
L_corr_group[1, 0]
0.41
0.24
-0.01
0.75
422.80
654.12
NaN
0.01
0.01
L_corr_group[1, 1]
0.87
0.11
0.67
1.00
449.41
712.07
NaN
0.01
0.00
L_corr_region[0, 0]
1.00
0.00
1.00
1.00
1000.00
1000.00
NaN
0.00
NaN
...
...
...
...
...
...
...
...
...
...
z_region[1, 0]
-0.76
0.66
-1.82
0.22
722.08
479.90
NaN
0.02
0.03
z_region[1, 1]
0.98
0.69
-0.07
2.06
640.51
567.35
NaN
0.03
0.02
z_region[1, 2]
-0.69
0.67
-1.72
0.40
796.26
723.29
NaN
0.02
0.02
z_region[1, 3]
0.33
0.82
-0.98
1.57
590.17
398.79
NaN
0.03
0.04
z_region[1, 4]
-0.07
0.70
-1.14
1.11
826.55
677.83
NaN
0.02
0.02
65 rows Γ 9 columns
Code
from BayesForge import bf
import jax.numpy as jnp
# Setup device -----------------------------------------------
m = bf(platform= 'cpu' )
# Load data β column args are mapped automatically from the DataFrame.
# N_groups and N_regions are dataset-specific; provide them as defaults.
data_path = m.load.sim_nested_effects(only_path= True )
m.data(data_path)
def model_nested_builtin_centered(y, x, group_id, region_id, N_groups= 20 , N_regions= 5 ):
sigma = m.dist.exponential(1 , name= 'sigma' )
a_g_est, b_g_est = m.effects.nested_varying_effects(
N_vars= 2 ,
names= ["region" , "group" ],
N_groups= [N_regions, N_groups],
group_ids= [region_id, group_id],
centered= True ,
)
mu_est = a_g_est + b_g_est * x
m.dist.normal(mu_est, sigma, obs= y)
m.fit(model_nested_builtin_centered, num_samples= 1000 , num_warmup= 500 , num_chains= 1 )
m.summary()
jax.local_device_count(cpu) = 32
0%| | 0/1500 [00:00<?, ?it/s]warmup: 5%|β | 75/1500 [00:01<00:36, 38.61it/s, 15 steps of size 3.35e-02. acc. prob=0.76]warmup: 14%|ββ | 211/1500 [00:02<00:09, 129.06it/s, 15 steps of size 2.10e-01. acc. prob=0.78]warmup: 24%|βββ | 362/1500 [00:02<00:04, 251.99it/s, 15 steps of size 3.37e-01. acc. prob=0.78]sample: 33%|ββββ | 502/1500 [00:02<00:02, 380.68it/s, 15 steps of size 2.20e-01. acc. prob=0.98]sample: 39%|ββββ | 591/1500 [00:02<00:02, 450.46it/s, 15 steps of size 2.20e-01. acc. prob=0.94]sample: 45%|βββββ | 678/1500 [00:02<00:01, 519.36it/s, 15 steps of size 2.20e-01. acc. prob=0.95]sample: 53%|ββββββ | 795/1500 [00:02<00:01, 642.91it/s, 15 steps of size 2.20e-01. acc. prob=0.95]sample: 62%|βββββββ | 924/1500 [00:02<00:00, 780.87it/s, 15 steps of size 2.20e-01. acc. prob=0.95]sample: 69%|βββββββ | 1038/1500 [00:02<00:00, 863.82it/s, 15 steps of size 2.20e-01. acc. prob=0.95]sample: 77%|ββββββββ | 1155/1500 [00:02<00:00, 939.15it/s, 15 steps of size 2.20e-01. acc. prob=0.94]sample: 84%|βββββββββ | 1260/1500 [00:02<00:00, 966.77it/s, 15 steps of size 2.20e-01. acc. prob=0.94]sample: 93%|ββββββββββ| 1390/1500 [00:03<00:00, 1056.88it/s, 15 steps of size 2.20e-01. acc. prob=0.94]sample: 100%|ββββββββββ| 1500/1500 [00:03<00:00, 478.16it/s, 15 steps of size 2.20e-01. acc. prob=0.94]
corr_group[0, 0]
1.00
0.00
1.00
1.00
1000.00
1000.00
NaN
0.00
NaN
corr_group[0, 1]
0.41
0.24
-0.02
0.74
419.70
514.28
NaN
0.01
0.01
corr_group[1, 0]
0.41
0.24
-0.02
0.74
419.70
514.28
NaN
0.01
0.01
corr_group[1, 1]
1.00
0.00
1.00
1.00
1064.88
975.03
NaN
0.00
0.00
corr_region[0, 0]
1.00
0.00
1.00
1.00
1000.00
1000.00
NaN
0.00
NaN
...
...
...
...
...
...
...
...
...
...
sigma
0.49
0.02
0.46
0.52
1097.02
704.00
NaN
0.00
0.00
sigma_group[0]
0.42
0.09
0.30
0.57
600.60
566.37
NaN
0.00
0.00
sigma_group[1]
0.25
0.06
0.17
0.35
318.94
374.90
NaN
0.00
0.00
sigma_region[0]
1.16
0.55
0.59
2.11
420.50
354.07
NaN
0.03
0.06
sigma_region[1]
0.47
0.24
0.21
0.87
438.67
485.38
NaN
0.01
0.02
65 rows Γ 9 columns
using PyBayesForge
# Setup device ------------------------------------------------
m = importBF (platform= "cpu" )
# Load data β column args mapped automatically from the DataFrame.
# N_groups and N_regions are dataset-specific; provide them as defaults.
data_path = m.load.sim_nested_effects (only_path= true )
m.data (data_path)
# The Julia wrapper exposes no Python-visible defaults, so pass the model
# data (DataFrame columns + scalar sizes) explicitly.
m.data_on_model = pydict (
y = jnp.array (m.df["y" ]),
x = jnp.array (m.df["x" ]),
group_id = jnp.array (m.df["group_id" ]),
region_id = jnp.array (m.df["region_id" ]),
N_groups = 20 ,
N_regions = 5 ,
)
# Define model ------------------------------------------------
@BF function model (y, x, group_id, region_id, N_groups= 20 , N_regions= 5 )
sigma = m.dist.exponential (1 , name= "sigma" )
veff = m.effects.nested_varying_effects (
N_vars = 2 ,
names = ["region" , "group" ],
N_groups = [N_regions, N_groups],
group_ids = [region_id, group_id],
centered = false ,
)
a_g_est, b_g_est = veff
mu = a_g_est + b_g_est * x
m.dist.normal (mu, sigma, obs= y, name= "y" )
end
# Run mcmc ------------------------------------------------
m.fit (model, num_samples= 1000 , num_warmup= 500 , num_chains= 1 )
m.summary ()
library (BayesForge)
# Setup platform ------------------------------------------------
m = importBF (platform= 'cpu' )
jnp = reticulate:: import ("jax.numpy" )
# Load data β column args mapped automatically from the DataFrame.
# N_groups and N_regions are dataset-specific; provide them as defaults.
data_path = m$ load$ sim_nested_effects (only_path= TRUE )
m$ data (data_path)
# The R wrapper exposes no Python-visible defaults, so pass the model
# data (DataFrame columns + scalar sizes) explicitly.
m$ data_on_model = list (
y = jnp$ array (m$ df$ y),
x = jnp$ array (m$ df$ x),
group_id = jnp$ array (as.integer (m$ df$ group_id)),
region_id = jnp$ array (as.integer (m$ df$ region_id)),
N_groups = 20 L,
N_regions = 5 L
)
# Define model ------------------------------------------------
model <- function (y, x, group_id, region_id, N_groups= 20 L, N_regions= 5 L) {
sigma = m$ dist$ exponential (1 , name= 'sigma' )
veff = m$ effects$ nested_varying_effects (
N_vars = 2 L,
names = list ('region' , 'group' ),
N_groups = list (as.integer (N_regions), as.integer (N_groups)),
group_ids = list (region_id, group_id),
centered = FALSE ,
)
a_g_est = veff[[1 ]]
b_g_est = veff[[2 ]]
mu = a_g_est + b_g_est * x
m$ dist$ normal (mu, sigma, obs= y, name= 'y' )
}
# Run MCMC ------------------------------------------------
m$ fit (model, num_samples= 1000 L, num_warmup= 500 L, num_chains= 1 L)
m$ summary ()
Mathematical Details
We model the outcome Y_i for observation i . The intercept \alpha_j and slope \beta_j for group j(i) are nested within region k(j) .
Y_{i} \sim \text{Normal}(\mu_i, \sigma)
\mu_{i} = \alpha_{[j(i)]} + \beta_{[j(i)]} X_i
Centered Parameterization
The vector of group-level effects follows a Multivariate Normal distribution centered on the region-level effects:
\begin{bmatrix} \alpha_j \\ \beta_j \end{bmatrix} \sim \text{MVNormal}\left(\begin{bmatrix} \gamma_{\alpha, [k(j)]} \\ \gamma_{\beta, [k(j)]} \end{bmatrix}, \Sigma_{group}\right)
The region-level effects themselves follow their own Multivariate Normal distribution:
\begin{bmatrix} \gamma_{\alpha, k} \\ \gamma_{\beta, k} \end{bmatrix} \sim \text{MVNormal}\left(\begin{bmatrix} \bar{\gamma}_\alpha \\ \bar{\gamma}_\beta \end{bmatrix}, \Sigma_{region}\right)
Where: - \Sigma = \mathbf{S} \mathbf{R} \mathbf{S} (diagonal matrix of standard deviations \mathbf{S} and correlation matrix \mathbf{R} ). - \bar{\gamma}_\alpha, \bar{\gamma}_\beta are the global average intercept and slope. - \Sigma_{region}, \Sigma_{group} are the covariance matrices for each hierarchical level.
Non-Centered Parameterization
To avoid funnel geometries, the non-centered formulation instead uses Cholesky factors L_{region} and L_{group} alongside standardized, non-correlated normal variables:
\begin{bmatrix} z_{\alpha, k} \\ z_{\beta, k} \end{bmatrix} \sim \text{Normal}(0, 1), \quad \begin{bmatrix} z_{\alpha, j} \\ z_{\beta, j} \end{bmatrix} \sim \text{Normal}(0, 1)
The region-level and group-level distributions are linearly transformed:
\begin{bmatrix} \gamma_{\alpha, k} \\ \gamma_{\beta, k} \end{bmatrix} = \begin{bmatrix} \bar{\gamma}_\alpha \\ \bar{\gamma}_\beta \end{bmatrix} + \text{diag}(\mathbf{S}_{region}) L_{region} \begin{bmatrix} z_{\alpha, k} \\ z_{\beta, k} \end{bmatrix}
\begin{bmatrix} \alpha_j \\ \beta_j \end{bmatrix} = \begin{bmatrix} \gamma_{\alpha, [k(j)]} \\ \gamma_{\beta, [k(j)]} \end{bmatrix} + \text{diag}(\mathbf{S}_{group}) L_{group} \begin{bmatrix} z_{\alpha, j} \\ z_{\beta, j} \end{bmatrix}
Model validation tests
Parameter recovery : builtin_function.py simulates nested data with known parameters, fits the built-in nested model and checks that the parameters are recovered.
Documentation examples : the Python, R and Julia examples of this page are run by the documentation tests (Python/test_model_pages.py, R/test_r_model_pages.py, Julia/test_julia_model_pages.py).
Notes
By nesting the MVN prior, we allow the model to learn how the correlation between intercepts and slopes persists across different levels of the hierarchy.
The non-centered parameterization (used by default in the m.effects.nested_varying_effects built-in function) is critical for convergence in these complex models.
This approach can be generalized to N levels and M variables by increasing the dimensionality of the vectors and matrices.