kups.mcmc.widom
¶
Widom test-particle method.
A ghost move runs the full propose/patch/log-ratio pipeline and discards the resulting state patch; the log acceptance ratio is accumulated into running statistics.
Contents:
- widom_test: per-system \(\ln\alpha\) for a ghost move
- GhostProbe: propagator wrapper accumulating the ratio via a lens + update callback
- WidomStatistics: running-sum accumulator reduced to \(\mu^\mathrm{ex}\), \(K_H\), \(q_\mathrm{st}\) by the post-hoc analyzer.
- TransitionStatistics: TMMC collection-matrix (C-matrix) accumulator for flat-histogram runs (Witman 2018).
- EnergyMoments / EnergyCumulants: Pébay/Welford online central moments of arbitrary order (default 4) for Taylor expansion of \(\ln Q_c(\beta)\).
References
Widom, B. (1963). J. Chem. Phys., 39, 2808. Vlugt, T. J. H. et al. (2008). J. Chem. Theory Comput., 4, 1107. Witman, M., Mahynski, N. A. & Smit, B. (2018). J. Chem. Theory Comput., 14, 6149--6158. DOI: 10.1021/acs.jctc.8b00534 Pébay, P. (2008). Formulas for Robust, One-Pass Parallel Computation of Covariances and Arbitrary-Order Statistical Moments. Sandia SAND2008-6212.
Energy = Array
¶
Potential energy [energy].
LogAcceptanceRatio = Array
¶
Log Metropolis acceptance ratio \(\ln\alpha\) [dimensionless].
ParticleCount = Array
¶
Macrostate particle count \(N\) [dimensionless, integer].
EnergyCumulants
¶
Finalized cumulants of the potential energy distribution.
Stores the standard cumulants \(\kappa_2, \ldots, \kappa_P\) of the energy
(\(\kappa_2 = \mathrm{Var}\), \(\kappa_3 = \mu_3\),
\(\kappa_4 = \mu_4 - 3\mu_2^2\), ... with \(\mu_k\) the central moments),
plus \(\kappa_1 = \langle E \rangle\) as mean. They determine the
\(\beta\)-derivatives of the configurational partition function
(Witman 2018, eq 10) via
which is what the flat-histogram Taylor extrapolation consumes.
Attributes:
| Name | Type | Description |
|---|---|---|
mean |
Energy
|
\(\kappa_1 = \langle E \rangle\), shape |
cumulants |
Array
|
\(\kappa_2, \ldots, \kappa_P\) stacked along the trailing
axis (leading axes stay per-system, so tables/vmap batch over
them), shape |
Source code in src/kups/mcmc/widom.py
EnergyMoments
¶
Pébay one-pass accumulator for central moments of per-system energy.
Maintains the unnormalized central-moment sums
for an arbitrary maximum order \(P\) (default 4, enough for the third-order
Taylor expansion of \(\ln Q_c(\beta)\)), updated via the single-sample
recurrence of Pébay (2008). Call :meth:finalize to convert to cumulants.
Attributes:
| Name | Type | Description |
|---|---|---|
count |
Array
|
Number of samples accumulated, shape |
mean |
Energy
|
Running sample mean \(\bar{x}_n\), shape |
central_sums |
Array
|
\(M_2, \ldots, M_P\) stacked along the trailing axis
(leading axes stay per-system, so tables/vmap batch over them),
shape |
Source code in src/kups/mcmc/widom.py
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 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 | |
max_order
property
¶
Highest moment order \(P\) accumulated.
finalize()
¶
Normalize \(M_p\) by \(n\) and map central moments to cumulants.
Uses the standard recursion (valid since \(\mu_1 = 0\))
which yields \(\kappa_2 = \mu_2\), \(\kappa_3 = \mu_3\), \(\kappa_4 = \mu_4 - 3\mu_2^2\), ...
Returns:
| Type | Description |
|---|---|
EnergyCumulants
|
Cumulants \(\kappa_1, \ldots, \kappa_P\) of the accumulated samples. |
Source code in src/kups/mcmc/widom.py
reset()
¶
Zero all fields.
Returns:
| Type | Description |
|---|---|
EnergyMoments
|
Fresh accumulator of the same shape and order with all fields zero. |
update(energy)
¶
Incorporate one per-system energy sample (Pébay single-sample update).
With \(\delta = x - \bar{x}_{n-1}\) and \(\delta_n = \delta / n\), the general recurrence for a single new sample is
the one-value specialisation of Pébay (2008), eq 2.9. For \(p \le 4\) this reduces to the familiar Welford-style updates; each order reads only the previous orders' old values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
energy
|
Energy
|
Per-system energy sample, shape |
required |
Returns:
| Type | Description |
|---|---|
EnergyMoments
|
Accumulator with the sample folded into all tracked moments. |
Source code in src/kups/mcmc/widom.py
zeros(n_systems, max_order=4)
staticmethod
¶
Create a zero-initialized accumulator for n_systems macrostates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_systems
|
int
|
Number of macrostates accumulated in parallel. |
required |
max_order
|
int
|
Highest central-moment order to track (at least 2). |
4
|
Returns:
| Type | Description |
|---|---|
EnergyMoments
|
Accumulator with all sums and counts at zero. |
Source code in src/kups/mcmc/widom.py
GhostProbe
¶
Bases: Propagator[State]
Propagator running one ghost move and updating a lens-accessed statistic.
Attributes:
| Name | Type | Description |
|---|---|---|
propose_fn |
/ patch_fn / log_probability_ratio_fn
|
standard MCMC trio
(:class: |
stat_lens |
Lens[State, Stat]
|
where in |
update_fn |
Callable[[State, Stat, Array], Stat]
|
|
Source code in src/kups/mcmc/widom.py
__call__(key, state)
¶
Run one ghost move and fold \(\ln\alpha\) into the accumulator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Array
|
JAX PRNG key. |
required |
state
|
State
|
Current simulation state; only the lens-accessed statistic changes. |
required |
Returns:
| Type | Description |
|---|---|
State
|
State with the updated accumulator written back through the lens. |
Source code in src/kups/mcmc/widom.py
TransitionStatistics
¶
TMMC collection-matrix (C-matrix) accumulator for \(N \to N \pm 1\) moves.
Here \(\alpha\) is the Metropolis acceptance ratio of the ghost move (the exponential of the log-ratio produced by the proposal pipeline). Each ghost evaluation contributes the acceptance probability \(\min(1, \alpha)\) to the corresponding row (Witman 2018, eq 5--7). Downstream, transition probabilities are recovered as
All arrays have shape (n_systems,).
Attributes:
| Name | Type | Description |
|---|---|---|
acceptance_insertion |
Array
|
\(\sum \min(1, \alpha_\text{ins})\). |
acceptance_deletion |
Array
|
\(\sum \min(1, \alpha_\text{del})\). |
n_trials_insertion |
Array
|
Number of ghost insertions evaluated. |
n_trials_deletion |
Array
|
Number of ghost deletions evaluated (incremented even when \(N = 0\); the accepted fraction is zero there). |
Source code in src/kups/mcmc/widom.py
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 257 258 259 260 261 | |
reset()
¶
Zero all fields.
Returns:
| Type | Description |
|---|---|
TransitionStatistics
|
Fresh accumulator of the same shape with all fields at zero. |
update_deletion(ln_alpha, macrostate_n)
¶
Accumulate a ghost deletion; zero contribution when \(N = 0\).
The trial count always increments — the fraction of accepted deletions at \(N = 0\) is zero, but the denominator still counts the trial, so \(P(0 \to 1)\) is not inflated.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ln_alpha
|
LogAcceptanceRatio
|
Per-system log Metropolis ratio \(\ln\alpha\) of the ghost deletion. |
required |
macrostate_n
|
ParticleCount
|
Per-system particle count \(N\); systems at \(N = 0\) contribute nothing to the acceptance sum. |
required |
Returns:
| Type | Description |
|---|---|
TransitionStatistics
|
Accumulator with \(\min(1, \alpha)\) added to the deletion row. |
Source code in src/kups/mcmc/widom.py
update_insertion(ln_alpha)
¶
Accumulate a ghost insertion. Trial count is incremented unconditionally.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ln_alpha
|
LogAcceptanceRatio
|
Per-system log Metropolis ratio \(\ln\alpha\) of the ghost insertion. |
required |
Returns:
| Type | Description |
|---|---|
TransitionStatistics
|
Accumulator with \(\min(1, \alpha)\) added to the insertion row. |
Source code in src/kups/mcmc/widom.py
zeros(n_systems)
staticmethod
¶
Create a zero-initialized accumulator for n_systems macrostates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_systems
|
int
|
Number of macrostates accumulated in parallel. |
required |
Returns:
| Type | Description |
|---|---|
TransitionStatistics
|
Accumulator with all sums and counts at zero, shape |
Source code in src/kups/mcmc/widom.py
WidomStatistics
¶
Online accumulator for plain Widom insertion sums.
Attributes:
| Name | Type | Description |
|---|---|---|
sum_boltzmann |
Array
|
\(\sum \exp(-\beta \Delta U) = \sum W\) [dimensionless]. |
sum_delta_u_boltzmann |
Array
|
\(\sum \Delta U \cdot \exp(-\beta \Delta U)\) [energy], with \(\Delta U\) the ghost insertion (host-guest) energy, not the cell's total potential energy. |
n_samples |
Array
|
Number of evaluations accumulated. |
Source code in src/kups/mcmc/widom.py
reset()
¶
Zero all fields.
Returns:
| Type | Description |
|---|---|
WidomStatistics
|
Fresh accumulator of the same shape with all fields at zero. |
update(ln_alpha, delta_u)
¶
Accumulate one ghost insertion.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ln_alpha
|
LogAcceptanceRatio
|
Per-system log Metropolis ratio. |
required |
delta_u
|
Energy
|
Per-system ghost insertion energy. With a zero-move-log insertion proposal and a bare Boltzmann log-ratio, \(\Delta U = -k_BT \ln\alpha\) exactly. |
required |
Returns:
| Type | Description |
|---|---|
WidomStatistics
|
Accumulator with the sample folded into the running sums. |
Source code in src/kups/mcmc/widom.py
zeros(n_systems)
staticmethod
¶
Create a zero-initialized accumulator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_systems
|
int
|
Number of systems accumulated in parallel. |
required |
Returns:
| Type | Description |
|---|---|
WidomStatistics
|
Accumulator with all sums and counts at zero, shape |
Source code in src/kups/mcmc/widom.py
widom_test(key, state, propose_fn, patch_fn, log_probability_ratio_fn)
¶
Evaluate per-system \(\ln\alpha\) for a ghost move without modifying state.
Runs the full MCMC proposal \(\to\) patch \(\to\) log-ratio pipeline and intentionally discards the resulting state patch. The physical state is untouched --- this is the Widom test-particle method applied as a reusable subroutine. The returned value is raw \(\ln\alpha\), not clamped by \(\min(1, \cdot)\); callers decide how to consume it:
- Excess chemical potential: average \(\exp\ln\alpha\), take \(-k_BT \ln\langle\cdot\rangle\).
- Henry coefficient: same average evaluated at \(N = 0\).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Array
|
JAX PRNG key. |
required |
state
|
State
|
Current simulation state. Not modified. |
required |
propose_fn
|
ChangesFn[State, Changes]
|
Move proposal (e.g. insertion or deletion). |
required |
patch_fn
|
PatchFn[State, Changes, Move]
|
Converts proposal to a state patch. |
required |
log_probability_ratio_fn
|
LogProbabilityRatioFn[State, Move]
|
Evaluates the acceptance log-ratio against the proposed patch. |
required |
Returns:
| Type | Description |
|---|---|
Table[SystemId, LogAcceptanceRatio]
|
Per-system log acceptance ratio as |