Metropolis Hastings
MetropolisHastings module.
This module provides a class definition of the MetropolisHastings class an abstract base class for implementation of Metropolis-Hastings-type sampling algorithms for a model.
AcceptRate
dataclass
Class for dealing with calculation of acceptance rates.
Called from MetropolisHastings-type samplers.
Attributes:
Name | Type | Description |
---|---|---|
count |
counters of current number of proposals and accepted proposals from a MH chain |
Source code in src/openmcmc/sampler/metropolis_hastings.py
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 |
|
acceptance_rate: float
property
Acceptance rate property, as calculated from counters.
Returns:
Type | Description |
---|---|
float
|
percentage proposals accepted in chain |
get_acceptance_rate()
Return acceptance rate formatted as string.
Returns:
Type | Description |
---|---|
str
|
acceptance rate string print out |
Source code in src/openmcmc/sampler/metropolis_hastings.py
49 50 51 52 53 54 55 56 57 58 |
|
increment_accept()
Increment acceptance count.
Source code in src/openmcmc/sampler/metropolis_hastings.py
60 61 62 |
|
increment_proposal()
Increment proposal count.
Source code in src/openmcmc/sampler/metropolis_hastings.py
64 65 66 |
|
MetropolisHastings
dataclass
Bases: MCMCSampler
Abstract base class for implementation of Metropolis-Hastings-type sampling algorithms for a model.
Subclasses include RandomWalk and ManifoldMALA.
https://en.wikipedia.org/wiki/Metropolis%E2%80%93Hastings_algorithm
Attributes:
Name | Type | Description |
---|---|---|
step |
ndarray
|
step size for Metropolis-Hastings proposals. Should either have shape=(p, 1) or shape=(p, n), where p is the dimension of the parameter, and n is the number of replicates. |
accept_rate |
AcceptRate
|
Acceptance Rate counter to keep track of proposals. |
Source code in src/openmcmc/sampler/metropolis_hastings.py
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 |
|
proposal(current_state, param_index=None)
abstractmethod
Method which generates proposed state from current state, and computes corresponding transition probabilities.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
current_state |
dict
|
current state |
required |
param_index |
int
|
subset of parameter used in proposal, If none all parameters are used |
None
|
Returns:
Name | Type | Description |
---|---|---|
prop_state |
dict
|
updated proposal_state dictionary. |
logp_pr_g_cr |
float
|
log-density of proposed state given current state. |
logp_cr_g_pr |
float
|
log-density of current state given proposed state. |
Source code in src/openmcmc/sampler/metropolis_hastings.py
87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
|
sample(current_state)
Generate a sample from the specified Metropolis-Hastings-type method.
https://en.wikipedia.org/wiki/Metropolis%E2%80%93Hastings_algorithm
generate proposal state x' from current_state x and accept or reject proposal according to the probability: A(x',x) = min(1, (P(x')g(x|x'))/(P(x)g(x'|x))) where: - P(x) is the probability of the state x - g(x|x') is the probability of moving from state x to x'
The exact method for the proposal (and therefore the form of the proposal distribution) is determined by the specific type of MetropolisHastings Sampler used.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
current_state |
dict
|
dictionary containing the current sampler state. |
required |
Returns:
Name | Type | Description |
---|---|---|
current_state |
dict
|
with updated sample for self.param. |
Source code in src/openmcmc/sampler/metropolis_hastings.py
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
|
accept_proposal(log_accept)
staticmethod
Decide to accept or reject proposal based on log acceptance probability.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
log_accept |
float64
|
log acceptance probability. |
required |
Returns:
Type | Description |
---|---|
bool
|
True for accept, False for Reject. |
Source code in src/openmcmc/sampler/metropolis_hastings.py
162 163 164 165 166 167 168 169 170 171 172 173 |
|
RandomWalk
dataclass
Bases: MetropolisHastings
Subtype of MetropolisHastings sampler that uses Gaussian random Walk proposals.
Supports both non-truncated and truncated Gaussian proposals: specifying self.domain limits leads to a truncated proposal mechanism.
Allows for the possibility that other elements of the model state have a dependence on the value of self.param, and if so should change when this value changes. If supplied, the self.state_update_function() property is called by the proposal function to update any other elements of the state as required.
Attributes:
Name | Type | Description |
---|---|---|
domain_limits |
ndarray
|
array with shape=(p, 2), where p is the dimensionality of the parameter being sampled. The first column gives the lower limits for the proposal, the second column gives the upper limits. |
state_update_function |
Callable
|
function which updates other elements of proposed state based on the proposed value for param. |
Source code in src/openmcmc/sampler/metropolis_hastings.py
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 257 258 259 260 261 262 263 264 |
|
__post_init__()
Derive conditional model instead of storing all distributions where things are simple.
However, this should not be done in the case where a state_update_function is provided as we don't know in general what/how parameters might change so need to keep full model to avoid incorrect conditioning.
Source code in src/openmcmc/sampler/metropolis_hastings.py
198 199 200 201 202 203 204 205 206 207 |
|
proposal(current_state, param_index=None)
Updates the current value of self.param using a (truncated) Gaussian random walk proposal.
In the non-truncated case, the proposal mechanism is symmetric, i.e. logp_pr_g_cr = logp_cr_g_pr. In this instance, the function simply returns logp_pr_g_cr = logp_cr_g_pr = 0, since these terms would anyway cancel in the calculation of the acceptance ratio.
Introducing a truncation into the proposal distribution means that the proposal is no longer symmetric, and so the log-proposal densities are computed in these cases.
Enables 3 different possibilities for the step size
1) shape=(1, 1): scalar step size, identical for every element of the parameter. 2) shape=(p, 1): step size with the same shape as the parameter being sampled (for one or many replicates). 3) shape=(p, n): a p-dimensional step size for each of n-replicates.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
current_state |
dict
|
dictionary containing current parameter values. |
required |
param_index |
int
|
subset of parameter used in proposal, If none all parameters are used |
None
|
Returns:
Name | Type | Description |
---|---|---|
prop_state |
dict
|
updated proposal_state dictionary. |
logp_pr_g_cr |
float
|
log-density of proposed state given current state. |
logp_cr_g_pr |
float
|
log-density of current state given proposed state. |
Source code in src/openmcmc/sampler/metropolis_hastings.py
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 257 258 259 260 261 262 263 264 |
|
RandomWalkLoop
dataclass
Bases: RandomWalk
Subtype of MetropolisHastings sampler which updates each of n replicates of a parameter one-at-a-time, rather than all simultaneously.
Source code in src/openmcmc/sampler/metropolis_hastings.py
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
|
sample(current_state)
Update each of n replicates of a given parameter in a loop, rather than simultaneously.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
current_state |
dict
|
dictionary containing the current sampler state. |
required |
Returns:
Name | Type | Description |
---|---|---|
current_state |
dict
|
with updated sample for self.param. |
Source code in src/openmcmc/sampler/metropolis_hastings.py
271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
|
ManifoldMALA
dataclass
Bases: MetropolisHastings
Class implementing manifold Metropolis-adjusted Langevin algorithm (mMALA) proposal mechanism.
Reference: Riemann manifold Langevin and Hamiltonian Monte Carlo methods, Mark Girolami, Ben Calderhead, 03 March 2011 https://doi.org/10.1111/j.1467-9868.2010.00765.x
Source code in src/openmcmc/sampler/metropolis_hastings.py
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 |
|
proposal(current_state, param_index=None)
Generate mMALA proposed state from current state using gradient and hessian, and compute corresponding log-transition probabilities.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
current_state |
dict
|
dictionary containing current parameter values. |
required |
param_index |
int
|
required input from superclass. Not used; defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
prop_state |
dict
|
updated proposal_state dictionary. |
logp_pr_g_cr |
ndarray
|
log-density of proposed state given current state. |
logp_cr_g_pr |
ndarray
|
log-density of current state given proposed state. |
Source code in src/openmcmc/sampler/metropolis_hastings.py
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 |
|