|
| 1 | +import numpy as np |
| 2 | + |
| 3 | +from bayesmbar import CBayesMBAR |
| 4 | +from numpy import ndarray |
| 5 | + |
| 6 | + |
| 7 | +class OffsetMBAR: |
| 8 | + def __init__( |
| 9 | + self, |
| 10 | + energies: list[np.ndarray], |
| 11 | + nums_conf: list[np.ndarray], |
| 12 | + offsets: list[float], |
| 13 | + sample_size: int = 1000, |
| 14 | + warmup_steps: int = 500, |
| 15 | + method: str = "Newton", |
| 16 | + random_seed: int = None, |
| 17 | + verbose: bool = True, |
| 18 | + ) -> None: |
| 19 | + """ |
| 20 | + Offset-constrained Coupled BayesMBAR. |
| 21 | +
|
| 22 | + This variant of Coupled BayesMBAR enforces a simple constraint: for each |
| 23 | + coupled system i, the free-energy difference computed from its energies |
| 24 | + plus a provided scalar offset must be identical across all systems. In |
| 25 | + other words, the solution to the MBAR equation satisfies |
| 26 | + mbar(energies[i]) + offsets[i] = constant for every system i. |
| 27 | +
|
| 28 | + Parameters |
| 29 | + ---------- |
| 30 | + energies : List[numpy.ndarray] |
| 31 | + Per-system arrays of reduced potentials (in units of kT) used by |
| 32 | + MBAR. One array per coupled system. |
| 33 | + nums_conf : List[numpy.ndarray] |
| 34 | + Per-system arrays of counts (number of configurations) corresponding |
| 35 | + to the provided energies. |
| 36 | + offsets : List[float] |
| 37 | + One scalar offset per system. The constraint is that |
| 38 | + mbar(energies[i]) + offsets[i] is the same across all systems. |
| 39 | + sample_size : int, optional |
| 40 | + Number of samples to draw from the likelihood. Default: 1000. |
| 41 | + warmup_steps : int, optional |
| 42 | + Number of warmup steps for the HMC sampler. Default: 500. |
| 43 | + method : str, optional |
| 44 | + Optimization method to find the likelihood mode. Either "Newton" or |
| 45 | + "L-BFGS-B". Default: "Newton". |
| 46 | + random_seed : int, optional |
| 47 | + Random seed. If None, a seed is generated from the current time. |
| 48 | + Default: None. |
| 49 | + verbose : bool, optional |
| 50 | + If True, print sampling progress. Default: True. |
| 51 | + """ |
| 52 | + new_energies = [] |
| 53 | + new_nums_conf = [] |
| 54 | + first_state = [] |
| 55 | + last_state = [] |
| 56 | + connecting_states = [] |
| 57 | + index = 0 |
| 58 | + for energy, n_conf, offset in zip(energies, nums_conf, offsets, strict=True): |
| 59 | + # Add the original reduced potential |
| 60 | + states, n_samples = energy.shape |
| 61 | + new_energies.append(energy) |
| 62 | + new_nums_conf.append(n_conf) |
| 63 | + first_state.append((index, 0)) |
| 64 | + # Generate the reduced potential for offset |
| 65 | + index += 1 |
| 66 | + # Make it divisible |
| 67 | + slice = energy[0, :n_samples] |
| 68 | + new_energy = np.linspace(0, offset, states).reshape( |
| 69 | + (states, 1) |
| 70 | + ) + slice.reshape((1, n_samples)) |
| 71 | + new_energies.append(new_energy) |
| 72 | + new_nums_conf.append(n_conf) |
| 73 | + last_state.append((index, states - 1)) |
| 74 | + connecting_states.append([(index - 1, states - 1), (index, 0)]) |
| 75 | + index += 1 |
| 76 | + |
| 77 | + self.cbmbar = CBayesMBAR( |
| 78 | + new_energies, |
| 79 | + new_nums_conf, |
| 80 | + identical_states=[first_state, last_state, *connecting_states], |
| 81 | + method=method, |
| 82 | + sample_size=sample_size, |
| 83 | + warmup_steps=warmup_steps, |
| 84 | + random_seed=random_seed, |
| 85 | + verbose=verbose, |
| 86 | + ) |
| 87 | + |
| 88 | + @property |
| 89 | + def F_mode(self) -> list[ndarray]: |
| 90 | + F_mode_list = [] |
| 91 | + for real_f, offset_f in zip(self.cbmbar.F_mode[::2], self.cbmbar.F_mode[1::2]): |
| 92 | + F_mode = np.append(real_f, real_f[-1] + offset_f[-1] - offset_f[0]) |
| 93 | + F_mode_list.append(F_mode) |
| 94 | + return F_mode_list |
| 95 | + |
| 96 | + @property |
| 97 | + def F_mean(self) -> list[ndarray]: |
| 98 | + F_mean_list = [] |
| 99 | + for real_f, offset_f in zip(self.cbmbar.F_mean[::2], self.cbmbar.F_mean[1::2]): |
| 100 | + F_mean = np.append(real_f, real_f[-1] + offset_f[-1] - offset_f[0]) |
| 101 | + F_mean_list.append(F_mean) |
| 102 | + return F_mean_list |
0 commit comments