Step functions¶
Single-step sampling: you own the loop.
These are plain module-level functions. Import them bare, or reach them
through the package namespace with import mcmckit as mc; both are the same
call.
Every function here advances a chain by exactly one step. State goes in as plain arguments and comes back as plain return values, so the recursion is yours to write, stop, inspect and modify.
import numpy as np
from mcmckit import ram_step
x = np.zeros(2)
logp = log_post(x)
S = np.linalg.cholesky(np.eye(2) * 0.1**2)
for i in range(1, 10_001):
x, logp, S, accepted = ram_step(log_post, x, logp, S, i)
Sign convention¶
Every function takes a log-posterior: bigger means a better fit. If your code carries a negative log-posterior, wrap it once:
Auxiliary model output¶
If your callable returns (log_post, aux), the extra payload is threaded back
out with the accepted sample and passed back in via aux=. Use it to keep
whatever your forward model already computed, without re-running it.
def log_post(theta):
freqs = surrogate(theta)
return -0.5 * np.sum(((freqs - measured) / sigma)**2), freqs
x, logp, S, accepted, freqs = ram_step(log_post, x, logp, S, i, aux=freqs)
Return a plain float instead and no aux comes back, so the signature keeps
its usual width.
mh_step ¶
One random-walk Metropolis-Hastings step.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
log_post
|
callable
|
|
required |
x
|
(ndarray, shape(d))
|
Current position. |
required |
logp
|
float
|
|
required |
cov
|
array - like
|
Proposal covariance. Scalar, 1-D diagonal or full |
required |
aux
|
object
|
Auxiliary payload belonging to |
None
|
rng
|
Generator
|
|
None
|
Returns:
| Type | Description |
|---|---|
(x_new, logp_new, accepted)
|
Plus |
Source code in mcmckit/steps.py
ram_step ¶
One Robust Adaptive Metropolis step (Vihola 2012).
The adaptation state is the Cholesky factor S, threaded in and out
explicitly. Seed it with np.linalg.cholesky(as_cov(cov0, d)).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
log_post
|
callable
|
|
required |
x
|
(ndarray, shape(d))
|
Current position. |
required |
logp
|
float
|
|
required |
S
|
(ndarray, shape(d, d))
|
Lower-triangular Cholesky factor of the proposal covariance. |
required |
i
|
int
|
Step number, 1-indexed. Drives the adaptation decay |
required |
gamma
|
float
|
Adaptation decay exponent, in (0.5, 1]. Default 0.51. |
0.51
|
target_rate
|
float
|
Target acceptance rate. Default 0.234. |
0.234
|
aux
|
object
|
Auxiliary payload belonging to |
None
|
rng
|
Generator
|
|
None
|
Returns:
| Type | Description |
|---|---|
(x_new, logp_new, S_new, accepted)
|
Plus |
Source code in mcmckit/steps.py
dram_step ¶
dram_step(log_post, x, logp, state, dr_scale=0.1, adapt_start=100, adapt_interval=10, regularization=1e-06, aux=None, rng=None)
One Delayed Rejection Adaptive Metropolis step.
Two-stage delayed rejection with Haario adaptive-Metropolis covariance
learning. The adaptation state is a :class:DRAMState, threaded in and
out; build the first one with :func:init_dram_state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
log_post
|
callable
|
|
required |
x
|
(ndarray, shape(d))
|
Current position. |
required |
logp
|
float
|
|
required |
state
|
DRAMState
|
Adaptation state carried between steps. |
required |
dr_scale
|
float
|
The second-stage proposal uses |
0.1
|
adapt_start
|
int
|
Steps to take before the covariance starts adapting. Default 100. |
100
|
adapt_interval
|
int
|
Adapt every this many steps. Default 10. |
10
|
regularization
|
float
|
Diagonal jitter keeping the adapted covariance positive definite. |
1e-06
|
aux
|
object
|
|
None
|
rng
|
Generator
|
|
None
|
Returns:
| Type | Description |
|---|---|
(x_new, logp_new, state_new, accepted)
|
Plus |
Source code in mcmckit/steps.py
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 | |
mala_step ¶
One Metropolis-adjusted Langevin step.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
log_post_and_grad
|
callable
|
|
required |
x
|
(ndarray, shape(d))
|
Current position. |
required |
logp
|
float
|
Log posterior at |
required |
grad
|
(ndarray, shape(d))
|
Gradient of the log posterior at |
required |
step_size
|
float
|
Langevin step size. Tune for roughly 0.57 acceptance. |
required |
aux
|
object
|
|
None
|
rng
|
Generator
|
|
None
|
Returns:
| Type | Description |
|---|---|
(x_new, logp_new, grad_new, accepted)
|
Plus |
Source code in mcmckit/steps.py
adaptive_mala_step ¶
adaptive_mala_step(log_post_and_grad, x, logp, grad, log_step, i, gamma=0.51, target_rate=0.574, aux=None, rng=None)
One MALA step with log-space step-size adaptation.
Identical to :func:mala_step except the step size adapts itself. The
adaptation state is log_step, threaded in and out. Seed it with
np.log(initial_step_size).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
log_step
|
float
|
Log of the current Langevin step size. |
required |
i
|
int
|
Step number, 1-indexed. |
required |
gamma
|
float
|
Adaptation decay exponent. Default 0.51. |
0.51
|
target_rate
|
float
|
Target acceptance rate. Default 0.574, optimal for MALA. |
0.574
|
Returns:
| Type | Description |
|---|---|
(x_new, logp_new, grad_new, log_step_new, accepted)
|
Plus |
Source code in mcmckit/steps.py
gibbs_step ¶
One Metropolis-within-Gibbs sweep, updating every block once.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
log_post
|
callable
|
|
required |
x
|
(ndarray, shape(d))
|
Current position. |
required |
logp
|
float
|
|
required |
blocks
|
sequence of sequence of int
|
Index groups updated together. |
required |
proposal_std
|
float or sequence of float
|
Random-walk width, shared or one per block. |
required |
aux
|
object
|
|
None
|
rng
|
Generator
|
|
None
|
Returns:
| Type | Description |
|---|---|
(x_new, logp_new, accepted)
|
|
Source code in mcmckit/steps.py
DRAMState ¶
Bases: NamedTuple
Adaptation state for :func:dram_step.
Plain data, no behaviour. C is the live proposal covariance; mean
and M2 accumulate the empirical covariance recursively (Welford), so a
step costs O(d^2) regardless of how long the chain gets.
Attributes:
| Name | Type | Description |
|---|---|---|
C |
(ndarray, shape(d, d))
|
Current proposal covariance. |
mean |
(ndarray, shape(d))
|
Running mean of the visited states. |
M2 |
(ndarray, shape(d, d))
|
Running sum of squared deviations. Empirical covariance is M2 / (n-1). |
n |
int
|
Number of states folded into mean and M2. |
Source code in mcmckit/steps.py
init_dram_state ¶
Build the starting :class:DRAMState for a chain at x0.
Source code in mcmckit/steps.py
as_cov ¶
Broadcast a scalar, 1-D diagonal or full matrix to a (d, d) covariance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cov
|
float or array - like
|
Scalar (isotropic variance), 1-D (diagonal), or 2-D (full matrix). |
required |
d
|
int
|
Dimension of the parameter space. |
required |
Returns:
| Type | Description |
|---|---|
(ndarray, shape(d, d))
|
|