kups.mcmc.probability
¶
Log probability ratio functions for Monte Carlo acceptance criteria.
This module provides acceptance probability calculations for different statistical ensembles: canonical (NVT) and grand canonical (μVT). These functions compute the log probability ratios needed for Metropolis-Hastings acceptance/rejection.
Key components:
- BoltzmannLogProbabilityRatio: Canonical ensemble energy criterion
- LogFugacityRatio: Grand canonical chemical potential term
- MuVTLogProbabilityRatio: Combined μVT ensemble acceptance
The acceptance probability in Metropolis-Hastings is:
where the log probability ratio includes contributions from the target distribution and proposal probabilities.
NVTLogLikelihoodRatio = BoltzmannLogProbabilityRatio
module-attribute
¶
Alias for BoltzmannLogProbabilityRatio (canonical ensemble).
BoltzmannLogProbabilityRatio
¶
Bases: LogProbabilityRatioFn[State, Move]
Boltzmann acceptance criterion for canonical (NVT) ensemble.
Computes the log probability ratio based on energy differences:
This corresponds to the Metropolis criterion for constant N, V, T simulations.
Class Type Parameters:
| Name | Bound or Constraints | Description | Default |
|---|---|---|---|
State
|
Simulation state type |
required | |
Move
|
Patch
|
Patch type for state updates |
required |
Attributes:
| Name | Type | Description |
|---|---|---|
temperature |
View[State, Table[SystemId, Array]]
|
Lens to system temperature [K] |
potential |
CachedPotential[State, Any, Any, Move]
|
Cached potential for efficient energy evaluations |
Example
Source code in src/kups/mcmc/probability.py
IsBoltzmannState
¶
Bases: Protocol
State protocol for Boltzmann acceptance probability computation.
Source code in src/kups/mcmc/probability.py
IsFugacityState
¶
Bases: Protocol
State protocol for fugacity acceptance probability computation.
Source code in src/kups/mcmc/probability.py
IsMuVTState
¶
Bases: Protocol
State protocol for μVT (grand canonical) acceptance probability computation.
Source code in src/kups/mcmc/probability.py
LogFugacityRatio
¶
Bases: LogProbabilityRatioFn[State, Move]
Chemical potential contribution for grand canonical (μVT) moves.
Computes the log probability ratio from particle number changes in GCMC:
where \(f_i\) is the fugacity of species \(i\), \(V\) is the volume, and \(N_i\) is the particle count. This implements the acceptance criterion for insertion/deletion moves in the grand canonical ensemble.
Class Type Parameters:
| Name | Bound or Constraints | Description | Default |
|---|---|---|---|
State
|
Simulation state type |
required | |
Move
|
Patch
|
Patch type for state updates |
required |
Attributes:
| Name | Type | Description |
|---|---|---|
log_activity |
View[State, Table[SystemId, Array]]
|
Lens to log(fugacity/k_B T) per species, shape |
volume |
View[State, Table[SystemId, Array]]
|
Lens to system volumes, shape |
particle_counts |
View[State, Table[SystemId, Array]]
|
Lens to current particle counts, shape |
patched_particle_counts |
Probe[State, Move, Table[SystemId, Array]]
|
Probe for particle counts after applying patch |
Note
Activity is fugacity divided by k_B T: \(a = f/(k_B T)\). The log_activity should be computed from equation of state (e.g., Peng-Robinson).
Source code in src/kups/mcmc/probability.py
MuVTLogProbabilityRatio
¶
Bases: LogProbabilityRatioFn[State, Move]
Combined acceptance criterion for grand canonical (μVT) ensemble.
Combines chemical potential and Boltzmann factors for GCMC acceptance:
This is the full acceptance criterion for grand canonical Monte Carlo simulations, accounting for both particle number fluctuations (fugacity) and energy changes (Boltzmann factor).
Class Type Parameters:
| Name | Bound or Constraints | Description | Default |
|---|---|---|---|
State
|
Simulation state type |
required | |
Move
|
Patch
|
Patch type for state updates |
required |
Attributes:
| Name | Type | Description |
|---|---|---|
log_fugacity_ratio |
LogFugacityRatio[State, Move]
|
Chemical potential term for particle insertions/deletions |
boltzmann_log_likelihood_ratio |
BoltzmannLogProbabilityRatio[State, Move]
|
Energy term for configuration changes |
Example
Source code in src/kups/mcmc/probability.py
make_boltzmann_probability_ratio(state, potential)
¶
Build a Boltzmann acceptance criterion for NVT (canonical) Monte Carlo.
Wraps potential in a
CachedPotential so that the previous
energy is read from state.previous_energy and the new energy is evaluated
on demand, then assembles a
BoltzmannLogProbabilityRatio.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
Lens[State, IsBoltzmannState]
|
Lens into the sub-state satisfying
IsBoltzmannState (needs
|
required |
potential
|
Potential[State, Any, Any, Move]
|
Potential to evaluate on the proposed configuration. |
required |
Returns:
| Type | Description |
|---|---|
CachedPotential[State, EmptyType, EmptyType, Move]
|
|
BoltzmannLogProbabilityRatio[State, Move]
|
ready to be called with |
Source code in src/kups/mcmc/probability.py
make_fugacity_probability_ratio(state)
¶
Build the fugacity (chemical potential) acceptance criterion for GCMC.
Constructs a LogFugacityRatio that
computes the grand-canonical acceptance factor arising from particle-number
changes. Current and proposed motif counts are derived from
state.groups.data.species via motif_counts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
Lens[State, IsFugacityState]
|
Lens into the sub-state satisfying
IsFugacityState (needs
|
required |
Returns:
| Type | Description |
|---|---|
LogFugacityRatio[State, Any]
|
|
LogFugacityRatio[State, Any]
|
ready to be called with |
Source code in src/kups/mcmc/probability.py
make_muvt_probability_ratio(state, potential)
¶
Build the combined acceptance criterion for grand canonical (μVT) Monte Carlo.
Composes make_fugacity_probability_ratio and make_boltzmann_probability_ratio into a single MuVTLogProbabilityRatio whose log-ratio is:
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
Lens[State, IsMuVTState]
|
Lens into the sub-state satisfying
IsMuVTState (needs |
required |
potential
|
Potential[State, Any, Any, Move]
|
Potential to evaluate on the proposed configuration. |
required |
Returns:
| Type | Description |
|---|---|
CachedPotential[State, EmptyType, EmptyType, Move]
|
|
MuVTLogProbabilityRatio[State, Move]
|
ready to be called with |
Source code in src/kups/mcmc/probability.py
motif_counts(groups)
¶
Count the number of motifs of each species per system.
Accumulates per-group species assignments into a 2D count matrix, where
entry [i, j] is the number of groups of species j in system i.
Groups whose species index falls outside [0, num_motifs) are silently
ignored (mode="drop").
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
groups
|
Table[GroupId, HasMotifAndSystemIndex]
|
Indexed collection of groups. Each group carries a |
required |
Returns:
| Type | Description |
|---|---|
Array
|
Integer array of shape |
Array
|
per system. |