HierarchicalProblem¶
Joint Bayesian inference over shared hyperparameters and group-level parameters across J nominally identical structures (or any grouped data).
hproblem = mc.HierarchicalProblem(
hyperprior=hyperprior, # log p(phi)
group_prior=group_prior, # log p(theta_j | phi)
group_likelihoods=likelihoods, # list of likelihoods, one per group
n_hyper=2,
n_group=1,
param_names_hyper=["mu_k", "log_sigma_k"],
param_names_group=["k"],
)
# Compatible with all samplers — run as a normal Problem
result = mc.DRAM(n_samples=20_000, initial_cov=np.eye(hproblem.n_params)).run(
hproblem,
x0=hproblem.default_x0(phi0=[10.0, 0.0], group_x0s=[[10.0]] * J),
)
# Extract marginal posteriors
hyper_result = hproblem.extract_hyper(result)
group_results = [hproblem.extract_group(result, j) for j in range(J)]
HierarchicalProblem ¶
Joint Bayesian inference over hyperparameters and group-level parameters.
Models the situation where you have J groups (e.g. J nominally identical
structures) each with local parameters :math:\theta_j, linked through
shared hyperparameters :math:\phi:
.. math::
p(\phi) \quad\text{(hyperprior)}
p(\theta_j \mid \phi) \quad\text{(group-level prior)}
p(y_j \mid \theta_j) \quad\text{(per-group likelihood)}
The joint log-posterior is:
.. math::
\log p(\phi, \theta_1,\ldots,\theta_J \mid y)
= \log p(\phi)
+ \sum_j \log p(\theta_j \mid \phi)
+ \sum_j \log p(y_j \mid \theta_j)
The full parameter vector is laid out as:
.. code-block::
theta = [phi_0, ..., phi_{n_hyper-1},
theta_1_0, ..., theta_1_{n_group-1},
...
theta_J_0, ..., theta_J_{n_group-1}]
This object exposes the same interface as :class:Problem and is
therefore compatible with all samplers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hyperprior
|
callable
|
|
required |
group_prior
|
callable
|
|
required |
group_likelihoods
|
list of callable
|
One callable per group: |
required |
n_hyper
|
int
|
Dimension of |
required |
n_group
|
int
|
Dimension of each group's local parameter vector |
required |
param_names_hyper
|
list of str
|
Names for the hyperparameters. |
None
|
param_names_group
|
list of str
|
Names for one group's parameters; automatically suffixed with the
group index, e.g. |
None
|
Examples:
5 structures sharing a stiffness population::
def hyperprior(phi):
mu, log_s = phi
return -0.5 * ((mu - 10) / 3)**2 - 0.5 * log_s**2
def group_prior(theta_j, phi):
mu, log_s = phi
sigma = np.exp(log_s)
return -0.5 * ((theta_j[0] - mu) / sigma)**2 - log_s
likelihoods = [make_likelihood(y_j) for y_j in datasets]
hproblem = mc.HierarchicalProblem(
hyperprior=hyperprior,
group_prior=group_prior,
group_likelihoods=likelihoods,
n_hyper=2,
n_group=1,
param_names_hyper=["mu_k", "log_sigma_k"],
param_names_group=["k"],
)
result = mc.DRAM(n_samples=20_000, initial_cov=np.eye(hproblem.n_params)).run(
hproblem, x0=hproblem.default_x0([10.0, np.log(1.0)],
[[10.0]] * 5))
hyper_result = hproblem.extract_hyper(result)
group_results = [hproblem.extract_group(result, j) for j in range(5)]
Source code in mcmckit/core/hierarchical.py
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 | |
Attributes¶
Methods:¶
split ¶
Split full theta into (phi, [theta_0, ..., theta_{J-1}]).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
theta
|
(array - like, shape(n_params))
|
|
required |
Returns:
| Name | Type | Description |
|---|---|---|
phi |
(ndarray, shape(n_hyper))
|
|
group_thetas |
list of np.ndarray, each shape (n_group,)
|
|
Source code in mcmckit/core/hierarchical.py
default_x0 ¶
Assemble a starting point from hyper and group initial values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
phi0
|
(array - like, shape(n_hyper))
|
|
required |
group_x0s
|
list of array-like, each shape (n_group,)
|
One entry per group. |
required |
Returns:
| Type | Description |
|---|---|
(ndarray, shape(n_params))
|
|
Source code in mcmckit/core/hierarchical.py
log_prior ¶
log p(phi) + sum_j log p(theta_j | phi).
Source code in mcmckit/core/hierarchical.py
log_likelihood ¶
sum_j log p(y_j | theta_j).
Source code in mcmckit/core/hierarchical.py
log_posterior ¶
extract_hyper ¶
Return a Result containing only the hyperparameter samples.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
Result
|
Full posterior result from running a sampler on this problem. |
required |
Returns:
| Type | Description |
|---|---|
(Result, shape(n_samples, n_hyper))
|
|
Source code in mcmckit/core/hierarchical.py
extract_group ¶
Return a Result containing only group j's parameter samples.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
Result
|
|
required |
j
|
int
|
Group index (0-based). |
required |
Returns:
| Type | Description |
|---|---|
(Result, shape(n_samples, n_group))
|
|