| 1 |
import numpy as np |
| 2 |
|
| 3 |
import csb.test as test |
| 4 |
import csb.numeric |
| 5 |
|
| 6 |
from csb.statistics.pdf import Normal, BaseDensity |
| 7 |
|
| 8 |
from csb.numeric.integrators import AbstractGradient, VelocityVerlet, LeapFrog, FastLeapFrog |
| 9 |
from csb.numeric import InvertibleMatrix |
| 10 |
|
| 11 |
from csb.statistics.samplers import State |
| 12 |
from csb.statistics.samplers.mc import Trajectory |
| 13 |
from csb.statistics.samplers.mc.multichain import MDRENSSwapParameterInfo, MDRENS |
| 14 |
from csb.statistics.samplers.mc.multichain import ThermostattedMDRENSSwapParameterInfo |
| 15 |
from csb.statistics.samplers.mc.multichain import RESwapParameterInfo, AlternatingAdjacentSwapScheme |
| 16 |
from csb.statistics.samplers.mc.multichain import ReplicaExchangeMC, ThermostattedMDRENS |
| 17 |
from csb.statistics.samplers.mc.multichain import HMCStepRENS, HMCStepRENSSwapParameterInfo |
| 18 |
from csb.statistics.samplers.mc.multichain import AbstractSwapCommunicator, AbstractExchangeMC |
| 19 |
from csb.statistics.samplers.mc.multichain import AbstractSwapParameterInfo, ReplicaHistory |
| 20 |
from csb.statistics.samplers.mc.singlechain import HMCSampler, RWMCSampler, AbstractNCMCSampler |
| 21 |
from csb.statistics.samplers.mc.singlechain import AbstractSingleChainMC, SimpleProposalCommunicator |
| 22 |
from csb.statistics.samplers.mc.propagators import RWMCPropagator, HMCPropagator, MDPropagator |
| 23 |
from csb.statistics.samplers.mc.propagators import AbstractNCMCPropagator, AbstractPropagator |
| 24 |
from csb.statistics.samplers.mc.neqsteppropagator import ReducedHamiltonian, HamiltonianSysInfo |
| 25 |
from csb.statistics.samplers.mc.neqsteppropagator import PlainMDPropagation, PlainMDPropagationParam |
| 26 |
from csb.statistics.samplers.mc.neqsteppropagator import AbstractMDPropagation, HMCPropagation |
| 27 |
from csb.statistics.samplers.mc.neqsteppropagator import Protocol, Step, AbstractPerturbation |
| 28 |
from csb.statistics.samplers.mc.neqsteppropagator import ReducedHamiltonianPerturbation |
| 29 |
from csb.statistics.samplers.mc.neqsteppropagator import AbstractPropagation |
| 30 |
from csb.statistics.samplers.mc.neqsteppropagator import NonequilibriumStepPropagator |
| 31 |
from csb.statistics.samplers.mc.neqsteppropagator import NonequilibriumTrajectory |
| 32 |
from csb.statistics.samplers.mc.neqsteppropagator import HMCPropagationParam |
| 33 |
|
| 34 |
class SamplePDF(Normal): |
| 35 |
|
| 36 |
def log_prob(self, x): |
| 37 |
return sum(map(super(SamplePDF, self).log_prob, x)) |
| 38 |
|
| 39 |
def grad(self, x, t): |
| 40 |
return x / (self.sigma ** 2) |
| 41 |
|
| 42 |
class MultimodalPDF(BaseDensity): |
| 43 |
|
| 44 |
def log_prob(self, x): |
| 45 |
return sum(-2.5 * np.cos(2.5 * x) - 0.04 * x ** 2) |
| 46 |
|
| 47 |
def grad(self, x, t): |
| 48 |
return -6.25 * np.sin(2.5 * x) + 0.08 * x |
| 49 |
|
| 50 |
class Multimodal2DPDF(BaseDensity): |
| 51 |
|
| 52 |
k = 0.5 |
| 53 |
|
| 54 |
def _E1(self, x): |
| 55 |
return 2.5 * np.cos(2.5 * x[0]) + 0.04 * x[0] ** 2 |
| 56 |
|
| 57 |
def _E2(self, x): |
| 58 |
return self.k * x[1] ** 2 |
| 59 |
|
| 60 |
def log_prob(self, x): |
| 61 |
return -self._E1(x) - self._E2(x) |
| 62 |
|
| 63 |
def grad(self, x, t): |
| 64 |
return np.array([(-6.25 * np.sin(2.5 * x[0]) + 0.08 * x[0]) * self._E2(x), |
| 65 |
self._E1(x) * self.k * x[1]]) |
| 66 |
|
| 67 |
|
| 68 |
@test.functional |
| 69 |
class TestMCPropagators(test.Case): |
| 70 |
|
| 71 |
def setUp(self): |
| 72 |
|
| 73 |
super(TestMCPropagators, self).setUp() |
| 74 |
|
| 75 |
self.pdf = SamplePDF() |
| 76 |
self.gradient = self._createGradient(1.) |
| 77 |
self.timestep = 1.2 |
| 78 |
self.stepsize = 1.2 |
| 79 |
self.nsteps = 15 |
| 80 |
self.nits = 10000 |
| 81 |
self.state = State(np.random.normal(size=1)) |
| 82 |
|
| 83 |
def _createGradient(self, sigma): |
| 84 |
|
| 85 |
class Grad(AbstractGradient): |
| 86 |
def evaluate(self, q, t): |
| 87 |
return q / (sigma ** 2) |
| 88 |
return Grad() |
| 89 |
|
| 90 |
def checkResult(self, trajectory): |
| 91 |
|
| 92 |
dim = len(trajectory[0].position) |
| 93 |
for i in range(dim): |
| 94 |
states = [state.position[i] for state in trajectory] |
| 95 |
self.assertAlmostEqual(np.array(states).mean(), 0., delta=0.15) |
| 96 |
self.assertAlmostEqual(np.array(states).var(), 1., delta=0.15) |
| 97 |
|
| 98 |
def testRWMCPropagator(self): |
| 99 |
|
| 100 |
gen = RWMCPropagator(self.pdf, self.stepsize) |
| 101 |
|
| 102 |
self.checkResult(gen.generate(self.state, self.nits)) |
| 103 |
|
| 104 |
def testHMCPropagator(self): |
| 105 |
|
| 106 |
gen = HMCPropagator(self.pdf, self.gradient, self.timestep, self.nsteps) |
| 107 |
|
| 108 |
self.checkResult(gen.generate(self.state, self.nits)) |
| 109 |
|
| 110 |
def testHMCPropagatorMM(self): |
| 111 |
|
| 112 |
mm = InvertibleMatrix(np.array([[1., 0.], [0., 2.]])) |
| 113 |
init_state = State(np.random.normal(size=2)) |
| 114 |
gen = HMCPropagator(self.pdf, self.gradient, self.timestep * 1.5, self.nsteps, mass_matrix=mm) |
| 115 |
|
| 116 |
self.checkResult(gen.generate(init_state, self.nits)) |
| 117 |
|
| 118 |
@test.skip("Takes quite a long time to run.") |
| 119 |
def testNCMCPropagator(self): |
| 120 |
|
| 121 |
Nhalf = 5 |
| 122 |
dt = 0.1 |
| 123 |
md_tl = 5 |
| 124 |
|
| 125 |
ks = np.linspace(1.0, 0.2, Nhalf).tolist() |
| 126 |
sigmas = [1/np.sqrt(k) for k in ks] |
| 127 |
sigmas += sigmas[::-1][1:] |
| 128 |
N = len(sigmas) |
| 129 |
pdfs = [SamplePDF(sigma=s) for s in sigmas] |
| 130 |
hamiltonians = [ReducedHamiltonian(pdfs[i].log_prob, pdfs[i].grad) for i in range(N)] |
| 131 |
sys_infos = [HamiltonianSysInfo(hamiltonians[i]) for i in range(N)] |
| 132 |
|
| 133 |
steps = [Step(ReducedHamiltonianPerturbation(sys_infos[i], sys_infos[i+1], |
| 134 |
evaluate_work=False), |
| 135 |
PlainMDPropagation(sys_infos[i+1], |
| 136 |
PlainMDPropagationParam(dt, md_tl, pdfs[i+1].grad), |
| 137 |
evaluate_heat=False)) |
| 138 |
for i in range(N - 1)] |
| 139 |
rv_steps = [Step(ReducedHamiltonianPerturbation(sys_infos[i], sys_infos[i+1], |
| 140 |
evaluate_work=False), |
| 141 |
PlainMDPropagation(sys_infos[i], |
| 142 |
PlainMDPropagationParam(dt, md_tl, pdfs[i].grad), |
| 143 |
evaluate_heat=False)) |
| 144 |
for i in range(N - 1)] |
| 145 |
|
| 146 |
for s in rv_steps: |
| 147 |
s.set_propagation_first() |
| 148 |
protocol = Protocol(steps) |
| 149 |
rv_protocol = Protocol(rv_steps) |
| 150 |
|
| 151 |
class MDProbStepNCMCSampler(AbstractNCMCSampler): |
| 152 |
def _calc_pacc(self, proposal_communicator): |
| 153 |
return np.exp(-proposal_communicator.traj.deltaH) |
| 154 |
|
| 155 |
class MDPropStepNCMCPropagator(AbstractNCMCPropagator): |
| 156 |
def _init_sampler(self, init_state): |
| 157 |
self._sampler = MDProbStepNCMCSampler(init_state, self.protocol, |
| 158 |
self.reverse_protocol) |
| 159 |
|
| 160 |
gen = MDPropStepNCMCPropagator(protocol, rv_protocol) |
| 161 |
|
| 162 |
init_state = State(np.array([1.0])) |
| 163 |
traj = gen.generate(init_state, self.nits, return_trajectory=True) |
| 164 |
self.checkResult(traj) |
| 165 |
|
| 166 |
|
| 167 |
@test.functional |
| 168 |
class TestMultichain(test.Case): |
| 169 |
|
| 170 |
def setUp(self): |
| 171 |
super(TestMultichain, self).setUp() |
| 172 |
|
| 173 |
self.samplers = None |
| 174 |
|
| 175 |
def set1pParams(self): |
| 176 |
init_state = State(np.random.uniform(low=-3.0, high=3.0, size=1)) |
| 177 |
self.temperatures = [0.4, 2.0] |
| 178 |
self.samplers = [RWMCSampler(MultimodalPDF(), init_state, 0.5, |
| 179 |
temperature=self.temperatures[0]), |
| 180 |
RWMCSampler(MultimodalPDF(), init_state, 5.5, |
| 181 |
temperature=self.temperatures[1])] |
| 182 |
self.grad = self.samplers[0]._pdf.grad |
| 183 |
self.nits = 10000 |
| 184 |
self.Ts = [lambda l: l * self.temperatures[i+1] + (1. - l) * self.temperatures[i] |
| 185 |
for i in range(len(self.samplers) - 1)] |
| 186 |
|
| 187 |
def set2pParams(self): |
| 188 |
init_state = State(np.random.uniform(low=-3.0, high=3.0, size=2)) |
| 189 |
pdf = Multimodal2DPDF() |
| 190 |
self.temperatures = [0.4, 1.0, 2.0] |
| 191 |
self.samplers = [RWMCSampler(pdf, init_state, 0.2, |
| 192 |
temperature=self.temperatures[0]), |
| 193 |
RWMCSampler(pdf, init_state, .8, |
| 194 |
temperature=self.temperatures[1]), |
| 195 |
RWMCSampler(pdf, init_state, 2., |
| 196 |
temperature=self.temperatures[2])] |
| 197 |
self.grad = self.samplers[0]._pdf.grad |
| 198 |
self.nits = 20000 |
| 199 |
self.Ts = [lambda l: l * self.temperatures[i+1] + (1. - l) * self.temperatures[i] |
| 200 |
for i in range(len(self.samplers) - 1)] |
| 201 |
|
| 202 |
def _run(self, algorithm): |
| 203 |
|
| 204 |
xmin1 = -2.5 |
| 205 |
xmax1 = 0.0 |
| 206 |
xmin2 = 0.0 |
| 207 |
xmax2 = 2.5 |
| 208 |
p_occ = 0.382 |
| 209 |
|
| 210 |
swapper = AlternatingAdjacentSwapScheme(algorithm) |
| 211 |
|
| 212 |
n_occ1 = 0 |
| 213 |
n_occ2 = 0 |
| 214 |
|
| 215 |
for i in range(self.nits): |
| 216 |
if i % 5 == 0: |
| 217 |
swapper.swap_all() |
| 218 |
|
| 219 |
else: |
| 220 |
algorithm.sample() |
| 221 |
|
| 222 |
x = self.samplers[0].state.position[0] |
| 223 |
|
| 224 |
if x > xmin1 and x < xmax1: |
| 225 |
n_occ1 += 1 |
| 226 |
if x > xmin2 and x < xmax2: |
| 227 |
n_occ2 += 1 |
| 228 |
|
| 229 |
p_occ_sampled1 = float(n_occ1) / float(self.nits) |
| 230 |
p_occ_sampled2 = float(n_occ2) / float(self.nits) |
| 231 |
|
| 232 |
# Assert by comparison with real occupation probabilities and a tolerance of |
| 233 |
# four standard deviations of a run with n=15000 samples and 100 iterations |
| 234 |
|
| 235 |
self.assertAlmostEqual(p_occ_sampled1, p_occ, delta=4.0 * 0.035) |
| 236 |
self.assertAlmostEqual(p_occ_sampled2, p_occ, delta=4.0 * 0.035) |
| 237 |
|
| 238 |
@test.skip("Takes some time, rendered optional by a unit test.") |
| 239 |
def testReplicaExchangeMC(self): |
| 240 |
self.set1pParams() |
| 241 |
params = [RESwapParameterInfo(self.samplers[0], self.samplers[1])] |
| 242 |
algorithm = ReplicaExchangeMC(self.samplers, params) |
| 243 |
self._run(algorithm) |
| 244 |
|
| 245 |
def testMDRENS(self): |
| 246 |
|
| 247 |
self.set1pParams() |
| 248 |
params = [MDRENSSwapParameterInfo(self.samplers[0], self.samplers[1], |
| 249 |
0.025, 15, self.grad)] |
| 250 |
algorithm = MDRENS(self.samplers, params, integrator=VelocityVerlet) |
| 251 |
self._run(algorithm) |
| 252 |
|
| 253 |
def testThermostattedMDRens(self): |
| 254 |
|
| 255 |
self.set1pParams() |
| 256 |
params = [ThermostattedMDRENSSwapParameterInfo(self.samplers[0], self.samplers[1], |
| 257 |
0.05, 15, self.grad, |
| 258 |
temperature=self.Ts[0])] |
| 259 |
algorithm = ThermostattedMDRENS(self.samplers, params) |
| 260 |
self._run(algorithm) |
| 261 |
|
| 262 |
def testThermostattedMDRensMM(self): |
| 263 |
|
| 264 |
self.set2pParams() |
| 265 |
mm1 = InvertibleMatrix(np.array([[1.0, 0.0], [0.0, 5.0]])) |
| 266 |
mm2 = InvertibleMatrix(np.array([[.5, 0.0], [0.0, 10.0]])) |
| 267 |
pdf = Multimodal2DPDF() |
| 268 |
params = [ThermostattedMDRENSSwapParameterInfo(self.samplers[0], self.samplers[1], |
| 269 |
0.01, 15, pdf.grad, |
| 270 |
temperature=self.Ts[0], |
| 271 |
mass_matrix=mm1), |
| 272 |
ThermostattedMDRENSSwapParameterInfo(self.samplers[1], self.samplers[2], |
| 273 |
0.1, 15, pdf.grad, |
| 274 |
temperature=self.Ts[1], |
| 275 |
mass_matrix=mm2)] |
| 276 |
algorithm = ThermostattedMDRENS(self.samplers, params) |
| 277 |
|
| 278 |
self._run(algorithm) |
| 279 |
|
| 280 |
def testHMCStepRENS(self): |
| 281 |
|
| 282 |
self.set1pParams() |
| 283 |
params = [HMCStepRENSSwapParameterInfo(self.samplers[0], self.samplers[1], 0.05, 3, 1, |
| 284 |
self.grad, 5)] |
| 285 |
|
| 286 |
algorithm = HMCStepRENS(self.samplers, params) |
| 287 |
|
| 288 |
self._run(algorithm) |
| 289 |
|
| 290 |
class MockSwapCommunicator(AbstractSwapCommunicator): |
| 291 |
|
| 292 |
pass |
| 293 |
|
| 294 |
class MockSwapParameterInfo(AbstractSwapParameterInfo): |
| 295 |
|
| 296 |
pass |
| 297 |
|
| 298 |
class MockSampler(AbstractSingleChainMC): |
| 299 |
|
| 300 |
def __init__(self, pdf, state, temperature=1.0): |
| 301 |
|
| 302 |
super(MockSampler, self).__init__(pdf, state, temperature) |
| 303 |
|
| 304 |
def _propose(self): |
| 305 |
|
| 306 |
pcom = SimpleProposalCommunicator(self._state, State(self._state.position * 2.0)) |
| 307 |
|
| 308 |
return pcom |
| 309 |
|
| 310 |
def _calc_pacc(self, proposal_communicator): |
| 311 |
|
| 312 |
return 0.42 |
| 313 |
|
| 314 |
class MockedAbstractExchangeMC(AbstractExchangeMC): |
| 315 |
|
| 316 |
def _propose_swap(self, param_info): |
| 317 |
|
| 318 |
return MockSwapCommunicator(param_info, Trajectory([State(np.array([1.0])), |
| 319 |
State(np.array([2.0]))]), |
| 320 |
Trajectory([State(np.array([2.0])), |
| 321 |
State(np.array([1.0]))])) |
| 322 |
|
| 323 |
def _calc_pacc_swap(self, swapcom): |
| 324 |
|
| 325 |
swapcom.acceptance_probability = 0.75 |
| 326 |
|
| 327 |
return swapcom |
| 328 |
|
| 329 |
@test.unit |
| 330 |
class TestAbstractExchangeMC(test.Case): |
| 331 |
|
| 332 |
def setUp(self): |
| 333 |
|
| 334 |
self.samplers = [MockSampler(None, State(np.array([3.0]))), |
| 335 |
MockSampler(None, State(np.array([5.0])))] |
| 336 |
|
| 337 |
self.param_info = MockSwapParameterInfo(self.samplers[0], self.samplers[1]) |
| 338 |
|
| 339 |
self.algo = MockedAbstractExchangeMC(self.samplers, [self.param_info]) |
| 340 |
|
| 341 |
|
| 342 |
def testAcceptSwap(self): |
| 343 |
|
| 344 |
swapcom = MockSwapCommunicator(self.param_info, |
| 345 |
Trajectory([State(np.array([1.0])), |
| 346 |
State(np.array([2.0]))]), |
| 347 |
Trajectory([State(np.array([2.0])), |
| 348 |
State(np.array([1.0]))])) |
| 349 |
|
| 350 |
np.random.seed(5) |
| 351 |
|
| 352 |
swapcom.acceptance_probability = 0.75 |
| 353 |
res = self.algo._accept_swap(swapcom) |
| 354 |
assert(res) |
| 355 |
|
| 356 |
swapcom.acceptance_probability = 0.15 |
| 357 |
res = self.algo._accept_swap(swapcom) |
| 358 |
assert(not res) |
| 359 |
|
| 360 |
def testSwap(self): |
| 361 |
|
| 362 |
np.random.seed(5) |
| 363 |
|
| 364 |
res = self.algo.swap(0) |
| 365 |
|
| 366 |
assert(res) |
| 367 |
self.assertEqual(self.samplers[0].state.position[0], 1.0) |
| 368 |
self.assertEqual(self.samplers[1].state.position[0], 2.0) |
| 369 |
self.assertEqual(self.algo.statistics.stats[0].total_swaps, 1) |
| 370 |
self.assertEqual(self.algo.statistics.stats[0].accepted_swaps, 1) |
| 371 |
|
| 372 |
np.random.seed(4) |
| 373 |
|
| 374 |
res = self.algo.swap(0) |
| 375 |
|
| 376 |
assert(not res) |
| 377 |
self.assertEqual(self.samplers[0].state.position[0], 1.0) |
| 378 |
self.assertEqual(self.samplers[1].state.position[0], 2.0) |
| 379 |
self.assertEqual(self.algo.statistics.stats[0].total_swaps, 2) |
| 380 |
self.assertEqual(self.algo.statistics.stats[0].accepted_swaps, 1) |
| 381 |
|
| 382 |
@test.unit |
| 383 |
class TestReplicaExchangeMC(test.Case): |
| 384 |
|
| 385 |
def setUp(self): |
| 386 |
|
| 387 |
pdf1 = HO() |
| 388 |
pdf2 = HO(k1=2.0, k2=2.0) |
| 389 |
|
| 390 |
self.samplers = [MockSampler(pdf1, State(np.array([3.0]))), |
| 391 |
MockSampler(pdf2, State(np.array([5.0])))] |
| 392 |
|
| 393 |
self.param_info = RESwapParameterInfo(self.samplers[0], self.samplers[1]) |
| 394 |
|
| 395 |
self.algo = ReplicaExchangeMC(self.samplers, [self.param_info]) |
| 396 |
|
| 397 |
def testProposeSwap(self): |
| 398 |
|
| 399 |
res = self.algo._propose_swap(self.param_info) |
| 400 |
self.assertEqual(res.traj12.initial.position[0], 3.0) |
| 401 |
self.assertEqual(res.traj12.final.position[0], 3.0) |
| 402 |
self.assertEqual(res.traj21.initial.position[0], 5.0) |
| 403 |
self.assertEqual(res.traj21.final.position[0], 5.0) |
| 404 |
|
| 405 |
def testCalcPaccSwap(self): |
| 406 |
|
| 407 |
swapcom = self.algo._propose_swap(self.param_info) |
| 408 |
res = self.algo._calc_pacc_swap(swapcom) |
| 409 |
|
| 410 |
self.assertEqual(res.acceptance_probability, csb.numeric.exp(-12.5 + 4.5 - 9.0 + 25.0)) |
| 411 |
|
| 412 |
class HO(object): |
| 413 |
|
| 414 |
def __init__(self, k1=1.0, k2=1.0, x1=0.0, x2=0.0, tau=1.0): |
| 415 |
self.k1 = k1 |
| 416 |
self.k2 = k2 |
| 417 |
self.x1 = x1 |
| 418 |
self.x2 = x2 |
| 419 |
self.tau = tau |
| 420 |
self.kt = lambda t: self.k2 * t / self.tau + (1 - t / self.tau) * self.k1 |
| 421 |
self.xt = lambda t: self.x2 * t / self.tau + (1 - t / self.tau) * self.x1 |
| 422 |
|
| 423 |
def log_prob(self, x, t=0.0): |
| 424 |
return -0.5 * self.kt(t) * sum((x - self.xt(t)) ** 2) |
| 425 |
|
| 426 |
def gradient(self, x, t): |
| 427 |
return self.kt(t) * (x - self.xt(t)) |
| 428 |
|
| 429 |
class MockPropagator(AbstractPropagator): |
| 430 |
|
| 431 |
def __init__(self): |
| 432 |
pass |
| 433 |
|
| 434 |
def generate(self, init_state, length, return_trajectory=False): |
| 435 |
|
| 436 |
final_state = State(init_state.position * 2, init_state.momentum * 2) |
| 437 |
|
| 438 |
return Trajectory([init_state, final_state]) |
| 439 |
|
| 440 |
class PlainMDPropagationMocked(PlainMDPropagation): |
| 441 |
|
| 442 |
def _propagator_factory(self): |
| 443 |
|
| 444 |
return MockPropagator() |
| 445 |
|
| 446 |
class HMCPropagationMocked(HMCPropagation): |
| 447 |
|
| 448 |
def _propagator_factory(self): |
| 449 |
|
| 450 |
return MockPropagator() |
| 451 |
|
| 452 |
class MockPerturbation(AbstractPerturbation): |
| 453 |
|
| 454 |
@property |
| 455 |
def sys_before(self): |
| 456 |
|
| 457 |
pdf = HO() |
| 458 |
|
| 459 |
return HamiltonianSysInfo(ReducedHamiltonian(pdf.log_prob, pdf.gradient)) |
| 460 |
|
| 461 |
@property |
| 462 |
def sys_after(self): |
| 463 |
|
| 464 |
pdf = HO() |
| 465 |
|
| 466 |
return HamiltonianSysInfo(ReducedHamiltonian(pdf.log_prob, pdf.gradient)) |
| 467 |
|
| 468 |
def __init__(self): |
| 469 |
pass |
| 470 |
|
| 471 |
def _run_perturbator(self, state): |
| 472 |
|
| 473 |
final = State(state.position * 2, state.momentum * 2) |
| 474 |
|
| 475 |
return Trajectory([state, final]) |
| 476 |
|
| 477 |
def _calculate_work(self, traj): |
| 478 |
|
| 479 |
return 42.0 |
| 480 |
|
| 481 |
def _calculate_jacobian(self, traj): |
| 482 |
|
| 483 |
return 1.1 |
| 484 |
|
| 485 |
|
| 486 |
class MockPropagation(AbstractPropagation): |
| 487 |
|
| 488 |
def __init__(self): |
| 489 |
pass |
| 490 |
|
| 491 |
@property |
| 492 |
def sys(self): |
| 493 |
|
| 494 |
pdf = HO() |
| 495 |
|
| 496 |
return HamiltonianSysInfo(pdf.log_prob, pdf.gradient) |
| 497 |
|
| 498 |
def _run_propagator(self, state): |
| 499 |
|
| 500 |
final = State(state.position * 2, state.momentum * 2) |
| 501 |
|
| 502 |
return Trajectory([state, final]) |
| 503 |
|
| 504 |
def _calculate_heat(self, traj): |
| 505 |
|
| 506 |
return -42.0 |
| 507 |
|
| 508 |
def _propagator_factory(self): |
| 509 |
|
| 510 |
return None |
| 511 |
|
| 512 |
|
| 513 |
class MockStep(Step): |
| 514 |
|
| 515 |
def __init__(self, return_momentum=True): |
| 516 |
|
| 517 |
self._return_momentum = return_momentum |
| 518 |
self._perform = None |
| 519 |
self.perform = self._perform_pert_prop |
| 520 |
|
| 521 |
@property |
| 522 |
def perturbation(self): |
| 523 |
|
| 524 |
return MockPerturbation() |
| 525 |
|
| 526 |
def _perform_pert_prop(self, state, extra_info=None): |
| 527 |
|
| 528 |
if self._return_momentum == True: |
| 529 |
final = State(state.position * 2, state.momentum * 2) |
| 530 |
else: |
| 531 |
final = State(state.position * 2) |
| 532 |
|
| 533 |
res = NonequilibriumTrajectory([state, final], heat=-42.0, work=42.0, jacobian=1.1) |
| 534 |
|
| 535 |
return res, None, None |
| 536 |
|
| 537 |
def _perform_prop_pert(self, state, extra_info=None): |
| 538 |
|
| 539 |
if self._return_momentum == True: |
| 540 |
final = State(state.position * 2, state.momentum * 2) |
| 541 |
else: |
| 542 |
final = State(state.position * 2) |
| 543 |
|
| 544 |
res = NonequilibriumTrajectory([state, final], heat=42.0, work=-42.0, jacobian=1.1) |
| 545 |
|
| 546 |
return res, None, None |
| 547 |
|
| 548 |
|
| 549 |
class MockProtocol(Protocol): |
| 550 |
|
| 551 |
def __init__(self, momentum=True): |
| 552 |
|
| 553 |
self._momentum = momentum |
| 554 |
self.steps = [MockStep(self._momentum), MockStep(self._momentum)] |
| 555 |
|
| 556 |
|
| 557 |
@test.unit |
| 558 |
class TestNeqsteppropagator(test.Case): |
| 559 |
|
| 560 |
def testReducedHamiltonian(self): |
| 561 |
pdf = HO(k1=2.0, k2=2.0) |
| 562 |
init = State(np.array([2.0]), np.array([-2.0])) |
| 563 |
ham = ReducedHamiltonian(lambda x: pdf.log_prob(x, 0.0), pdf.gradient, temperature=4.0) |
| 564 |
|
| 565 |
self.assertEqual(4.0, ham.E(init.position)) |
| 566 |
self.assertEqual(2.0, ham.kinetic_energy(init.momentum)) |
| 567 |
self.assertEqual(0.0, ham.kinetic_energy(None)) |
| 568 |
self.assertEqual(-1.0, ham.rlog_prob(init.position)) |
| 569 |
self.assertEqual(0.5, ham.rkinetic_energy(init.momentum)) |
| 570 |
self.assertEqual(1.5, ham(init)) |
| 571 |
|
| 572 |
def testHMCPropagation(self): |
| 573 |
|
| 574 |
pdf = HO() |
| 575 |
sys = HamiltonianSysInfo(ReducedHamiltonian(pdf.log_prob, pdf.gradient)) |
| 576 |
param = HMCPropagationParam(None, None, None) |
| 577 |
hmcprop = HMCPropagationMocked(sys, param) |
| 578 |
|
| 579 |
init = State(np.array([2.0]), np.array([2.0])) |
| 580 |
|
| 581 |
## Test _set_mass_matrix |
| 582 |
d = len(init.position) |
| 583 |
param = HMCPropagationParam(None, None, None, mass_matrix=InvertibleMatrix(np.eye(d))) |
| 584 |
hmcprop = HMCPropagationMocked(sys, param) |
| 585 |
hmcprop._set_mass_matrix(init) |
| 586 |
self.assertEqual(hmcprop.param.mass_matrix, |
| 587 |
InvertibleMatrix(np.eye(len(init.position)))) |
| 588 |
|
| 589 |
|
| 590 |
param = HMCPropagationParam(None, None, None) |
| 591 |
hmcprop = HMCPropagationMocked(sys, param) |
| 592 |
hmcprop._set_mass_matrix(init) |
| 593 |
self.assertEqual(hmcprop.param.mass_matrix, |
| 594 |
InvertibleMatrix(np.eye(len(init.position)))) |
| 595 |
|
| 596 |
## Test _calculate_heat |
| 597 |
final = State(init.position * 2, init.momentum * 2) |
| 598 |
traj = Trajectory([init, final]) |
| 599 |
self.assertEqual(hmcprop._calculate_heat(traj), 6.0) |
| 600 |
|
| 601 |
## Test __call__ |
| 602 |
result = hmcprop(init) |
| 603 |
self.assertEqual(init.position, result.initial.position) |
| 604 |
self.assertEqual(init.momentum, result.initial.momentum) |
| 605 |
self.assertEqual(result.final.position, init.position * 2) |
| 606 |
self.assertEqual(result.final.momentum, init.momentum * 2) |
| 607 |
self.assertEqual(result.heat, 6.0) |
| 608 |
|
| 609 |
def testPlainMDPropagation(self): |
| 610 |
|
| 611 |
pdf = HO() |
| 612 |
sys = HamiltonianSysInfo(ReducedHamiltonian(pdf.log_prob, pdf.gradient)) |
| 613 |
|
| 614 |
init = State(np.array([2.0]), np.array([2.0])) |
| 615 |
|
| 616 |
## Test _set_mass_matrix |
| 617 |
d = len(init.position) |
| 618 |
param = PlainMDPropagationParam(None, None, None, |
| 619 |
mass_matrix=InvertibleMatrix(np.eye(d))) |
| 620 |
mdprop = PlainMDPropagationMocked(sys, param) |
| 621 |
mdprop._set_mass_matrix(init) |
| 622 |
self.assertEqual(mdprop.param.mass_matrix, |
| 623 |
InvertibleMatrix(np.eye(d))) |
| 624 |
|
| 625 |
param = PlainMDPropagationParam(None, None, None) |
| 626 |
mdprop = PlainMDPropagationMocked(sys, param) |
| 627 |
mdprop._set_mass_matrix(init) |
| 628 |
self.assertEqual(mdprop.param.mass_matrix, |
| 629 |
InvertibleMatrix(np.eye(d))) |
| 630 |
|
| 631 |
## Test _calculate_heat |
| 632 |
final = State(init.position * 2, init.momentum * 2) |
| 633 |
traj = Trajectory([init, final]) |
| 634 |
self.assertEqual(mdprop._calculate_heat(traj), 12.0) |
| 635 |
|
| 636 |
## Test __call__ |
| 637 |
result = mdprop(init) |
| 638 |
self.assertEqual(init.position, result.initial.position) |
| 639 |
self.assertEqual(init.momentum, result.initial.momentum) |
| 640 |
self.assertEqual(result.final.position, init.position * 2) |
| 641 |
self.assertEqual(result.final.momentum, init.momentum * 2) |
| 642 |
self.assertEqual(result.heat, 12.0) |
| 643 |
|
| 644 |
|
| 645 |
def testReducedHamiltonianPerturbation(self): |
| 646 |
|
| 647 |
pdf = HO(k1=1.0, k2=2.0) |
| 648 |
redham1 = ReducedHamiltonian(lambda x: pdf.log_prob(x, 0.0)) |
| 649 |
redham2 = ReducedHamiltonian(lambda x: pdf.log_prob(x, 1.0)) |
| 650 |
sys1 = HamiltonianSysInfo(redham1) |
| 651 |
sys2 = HamiltonianSysInfo(redham2) |
| 652 |
init = State(np.array([2.0]), np.array([2.0])) |
| 653 |
traj = Trajectory([init, init]) |
| 654 |
|
| 655 |
hampert = ReducedHamiltonianPerturbation(sys1, sys2) |
| 656 |
|
| 657 |
## Test _calculate_work |
| 658 |
self.assertEqual(hampert._calculate_work(traj), 2.0) |
| 659 |
|
| 660 |
## Test __call__ |
| 661 |
result = hampert(init) |
| 662 |
self.assertEqual(result.initial.position[0], init.position[0]) |
| 663 |
self.assertEqual(result.initial.momentum[0], init.momentum[0]) |
| 664 |
self.assertEqual(result.initial.position[0], result.final.position[0]) |
| 665 |
self.assertEqual(result.initial.momentum[0], result.final.momentum[0]) |
| 666 |
self.assertEqual(result.work, 2.0) |
| 667 |
self.assertEqual(result.jacobian, 1.0) |
| 668 |
|
| 669 |
|
| 670 |
def testStep(self): |
| 671 |
|
| 672 |
step = Step(MockPerturbation(), MockPropagation()) |
| 673 |
init = State(np.array([2.0]), np.array([2.0])) |
| 674 |
|
| 675 |
## Test step with first perturbation, then propagation |
| 676 |
res = step.perform(init)[0] |
| 677 |
|
| 678 |
self.assertEqual(res.final.position, init.position * 4) |
| 679 |
self.assertEqual(res.final.momentum, init.momentum * 4) |
| 680 |
self.assertEqual(res.heat, -42.0) |
| 681 |
self.assertEqual(res.work, 42.0) |
| 682 |
self.assertEqual(res.jacobian, 1.1) |
| 683 |
|
| 684 |
## Test step with first perturbation, then propagation |
| 685 |
step.set_propagation_first() |
| 686 |
res = step.perform(init)[0] |
| 687 |
|
| 688 |
self.assertEqual(step.perform, step._perform_prop_pert) |
| 689 |
self.assertEqual(res.final.position, init.position * 4) |
| 690 |
self.assertEqual(res.final.momentum, init.momentum * 4) |
| 691 |
self.assertEqual(res.heat, -42.0) |
| 692 |
self.assertEqual(res.work, 42.0) |
| 693 |
self.assertEqual(res.jacobian, 1.1) |
| 694 |
|
| 695 |
def testNonequilibriumStepPropagator(self): |
| 696 |
|
| 697 |
protocol = Protocol([MockStep(True) for i in range(10)]) |
| 698 |
|
| 699 |
gen = NonequilibriumStepPropagator(protocol) |
| 700 |
|
| 701 |
## Test generate() |
| 702 |
init = State(np.array([2.0]), np.array([2.0])) |
| 703 |
|
| 704 |
res = gen.generate(init) |
| 705 |
|
| 706 |
self.assertEqual(res.final.position, init.position * (2 ** 10)) |
| 707 |
self.assertEqual(res.final.momentum, init.momentum * (2 ** 10)) |
| 708 |
self.assertEqual(res.work, 10 * 42) |
| 709 |
self.assertEqual(res.heat, -10 * 42) |
| 710 |
self.assertEqual(res.jacobian, 1.1 ** 10) |
| 711 |
|
| 712 |
|
| 713 |
class MockedNCMCSampler(AbstractNCMCSampler): |
| 714 |
|
| 715 |
def _calc_pacc(self, proposal_communicator): |
| 716 |
|
| 717 |
return proposal_communicator.traj.final.position[0] |
| 718 |
|
| 719 |
|
| 720 |
@test.unit |
| 721 |
class TestNCMCSampler(test.Case): |
| 722 |
|
| 723 |
def testProposeWithMomentum(self): |
| 724 |
|
| 725 |
self.protocol = MockProtocol(True) |
| 726 |
self.reverse_protocol = MockProtocol(True) |
| 727 |
for s in self.reverse_protocol.steps: |
| 728 |
s.set_propagation_first() |
| 729 |
|
| 730 |
## Test with momentum |
| 731 |
init = State(np.array([2.0]), np.array([2.0])) |
| 732 |
|
| 733 |
sampler = MockedNCMCSampler(init, self.protocol, self.reverse_protocol) |
| 734 |
|
| 735 |
## Test _propose |
| 736 |
# Make sure the first random number is < 0.5 |
| 737 |
np.random.seed(5) |
| 738 |
result = sampler._propose() |
| 739 |
|
| 740 |
self.assertEqual(result.traj.heat, - 2 * 42) |
| 741 |
self.assertEqual(result.traj.work, 2 * 42) |
| 742 |
self.assertEqual(result.traj.initial.position[0], init.position[0]) |
| 743 |
self.assertEqual(result.traj.final.position[0], init.position[0] * 4) |
| 744 |
self.assertEqual(result.traj.initial.momentum[0], init.momentum[0]) |
| 745 |
self.assertEqual(result.traj.final.momentum[0], init.momentum[0] * 4) |
| 746 |
|
| 747 |
# Make sure the first random number is > 0.5 |
| 748 |
np.random.seed(4) |
| 749 |
result = sampler._propose() |
| 750 |
|
| 751 |
self.assertEqual(result.traj.heat, 2 * 42) |
| 752 |
self.assertEqual(result.traj.work, - 2 * 42) |
| 753 |
self.assertEqual(result.traj.initial.position[0], init.position[0]) |
| 754 |
self.assertEqual(result.traj.final.position[0], init.position[0] * 4) |
| 755 |
self.assertEqual(result.traj.initial.momentum[0], init.momentum[0]) |
| 756 |
self.assertEqual(result.traj.final.momentum[0], init.momentum[0] * 4) |
| 757 |
|
| 758 |
|
| 759 |
def testProposeWithoutMomentum(self): |
| 760 |
|
| 761 |
self.protocol = MockProtocol(False) |
| 762 |
self.reverse_protocol = MockProtocol(False) |
| 763 |
for s in self.reverse_protocol.steps: |
| 764 |
s.set_propagation_first() |
| 765 |
|
| 766 |
## Test without momentum |
| 767 |
init = State(np.array([2.0])) |
| 768 |
|
| 769 |
sampler = MockedNCMCSampler(init, self.protocol, self.reverse_protocol) |
| 770 |
|
| 771 |
## Test _propose |
| 772 |
# Make sure the first random number is < 0.5 |
| 773 |
np.random.seed(5) |
| 774 |
result = sampler._propose() |
| 775 |
|
| 776 |
self.assertEqual(result.traj.heat, - 2 * 42) |
| 777 |
self.assertEqual(result.traj.work, 2 * 42) |
| 778 |
self.assertEqual(result.traj.initial.position[0], init.position[0]) |
| 779 |
self.assertEqual(result.traj.final.position[0], init.position[0] * 4) |
| 780 |
self.assertEqual(result.traj.initial.momentum, None) |
| 781 |
self.assertEqual(result.traj.final.momentum, None) |
| 782 |
|
| 783 |
# Make sure the first random number is > 0.5 |
| 784 |
np.random.seed(4) |
| 785 |
result = sampler._propose() |
| 786 |
|
| 787 |
self.assertEqual(result.traj.heat, 2 * 42) |
| 788 |
self.assertEqual(result.traj.work, - 2 * 42) |
| 789 |
self.assertEqual(result.traj.initial.position[0], init.position[0]) |
| 790 |
self.assertEqual(result.traj.final.position[0], init.position[0] * 4) |
| 791 |
self.assertEqual(result.traj.initial.momentum, None) |
| 792 |
self.assertEqual(result.traj.final.momentum, None) |
| 793 |
|
| 794 |
|
| 795 |
class HState(State): |
| 796 |
|
| 797 |
def clone(self): |
| 798 |
s = super(HState, self).clone() |
| 799 |
s.history = self.history |
| 800 |
|
| 801 |
return s |
| 802 |
|
| 803 |
|
| 804 |
@test.functional |
| 805 |
class TestReplicaHistory(test.Case): |
| 806 |
|
| 807 |
def setUp(self): |
| 808 |
|
| 809 |
pass |
| 810 |
|
| 811 |
def _runSimulation(self, n_replicas, swap_interval, first_swap): |
| 812 |
|
| 813 |
temperatures = np.linspace(1.0, 5.0, n_replicas) |
| 814 |
|
| 815 |
init_states = [HState(np.array([1.0])) for T in temperatures] |
| 816 |
|
| 817 |
for i, x in enumerate(init_states): |
| 818 |
x.history = [] |
| 819 |
|
| 820 |
samplers = [RWMCSampler(SamplePDF(), init_states[i], stepsize=1.0, temperature=T) |
| 821 |
for i, T in enumerate(temperatures)] |
| 822 |
|
| 823 |
params = [RESwapParameterInfo(samplers[i], samplers[i+1]) for i in range(len(samplers) - 1)] |
| 824 |
|
| 825 |
algo = ReplicaExchangeMC(samplers, params) |
| 826 |
swapper = AlternatingAdjacentSwapScheme(algo) |
| 827 |
samples = [] |
| 828 |
|
| 829 |
for i in range(500): |
| 830 |
if (i - first_swap) % swap_interval == 0 and i > 0 and i >= first_swap: |
| 831 |
swapper.swap_all() |
| 832 |
else: |
| 833 |
algo.sample() |
| 834 |
|
| 835 |
for j, s in enumerate(algo.state): |
| 836 |
s.history.append(j) |
| 837 |
|
| 838 |
samples.append(algo.state) |
| 839 |
|
| 840 |
return samples |
| 841 |
|
| 842 |
|
| 843 |
def _assertIdenticalHistories(self, samples, interval, first_swap=None): |
| 844 |
|
| 845 |
rh = ReplicaHistory(samples, interval, first_swap) |
| 846 |
|
| 847 |
for i in range(len(samples[0])): |
| 848 |
h = rh.calculate_history(i) |
| 849 |
for j, x in enumerate(samples[-1]): |
| 850 |
if x.history == h: |
| 851 |
return True |
| 852 |
|
| 853 |
return False |
| 854 |
|
| 855 |
def _assertIdenticalProjTrajs(self, samples, interval, first_swap=None): |
| 856 |
|
| 857 |
rh = ReplicaHistory(samples, interval, first_swap) |
| 858 |
|
| 859 |
## Calculate projected trajectories directly from test data history |
| 860 |
trajs1 = [Trajectory([x for x in [y[j] for y in samples] if x.history[0] == j]) |
| 861 |
for j in range(len(samples[0]))] |
| 862 |
|
| 863 |
ok = [] |
| 864 |
for i in range(len(samples[0])): |
| 865 |
trajs2 = rh.calculate_projected_trajectories(i) |
| 866 |
ok.append(True in [np.all(np.array(t1) == np.array(t2)) for t1 in trajs1 |
| 867 |
for t2 in trajs2]) |
| 868 |
|
| 869 |
return np.all(ok) |
| 870 |
|
| 871 |
def testTwoReplicas(self): |
| 872 |
|
| 873 |
swap_interval = 5 |
| 874 |
first_swap = 5 |
| 875 |
|
| 876 |
samples = self._runSimulation(2, swap_interval, first_swap) |
| 877 |
|
| 878 |
assert(self._assertIdenticalHistories(samples, swap_interval)) |
| 879 |
assert(self._assertIdenticalProjTrajs(samples, swap_interval)) |
| 880 |
|
| 881 |
def testFourReplicas(self): |
| 882 |
|
| 883 |
swap_interval = 5 |
| 884 |
first_swap = 5 |
| 885 |
|
| 886 |
samples = self._runSimulation(4, swap_interval, first_swap) |
| 887 |
|
| 888 |
assert(self._assertIdenticalHistories(samples, swap_interval)) |
| 889 |
assert(self._assertIdenticalProjTrajs(samples, swap_interval)) |
| 890 |
|
| 891 |
def testFiveReplicas(self): |
| 892 |
|
| 893 |
swap_interval = 5 |
| 894 |
first_swap = 5 |
| 895 |
|
| 896 |
samples = self._runSimulation(5, swap_interval, first_swap) |
| 897 |
|
| 898 |
assert(self._assertIdenticalHistories(samples, swap_interval)) |
| 899 |
assert(self._assertIdenticalProjTrajs(samples, swap_interval)) |
| 900 |
|
| 901 |
def testFiveReplicasOffset(self): |
| 902 |
|
| 903 |
swap_interval = 6 |
| 904 |
first_swap = 7 |
| 905 |
|
| 906 |
samples = self._runSimulation(5, swap_interval, first_swap) |
| 907 |
|
| 908 |
assert(self._assertIdenticalHistories(samples, swap_interval, first_swap)) |
| 909 |
assert(self._assertIdenticalProjTrajs(samples, swap_interval, first_swap)) |
| 910 |
|
| 911 |
|
| 912 |
@test.unit |
| 913 |
class TestAbstractSingleChainMC(test.Case): |
| 914 |
|
| 915 |
def setUp(self): |
| 916 |
|
| 917 |
self._sampler = MockSampler(pdf=SamplePDF(), state=State(np.array([1.0]))) |
| 918 |
|
| 919 |
def testAcceptProposal(self): |
| 920 |
|
| 921 |
proposal_state = State(np.array([1.234])) |
| 922 |
res = self._sampler._accept_proposal(proposal_state) |
| 923 |
|
| 924 |
assert(self._sampler.state == proposal_state) |
| 925 |
|
| 926 |
def testUpdateStatistics(self): |
| 927 |
|
| 928 |
nmoves_old = self._sampler._nmoves |
| 929 |
accepted_old = self._sampler._accepted |
| 930 |
|
| 931 |
self._sampler._update_statistics(True) |
| 932 |
|
| 933 |
assert(self._sampler._nmoves == nmoves_old + 1) |
| 934 |
assert(self._sampler._accepted == accepted_old + 1) |
| 935 |
|
| 936 |
nmoves_old = self._sampler._nmoves |
| 937 |
accepted_old = self._sampler._accepted |
| 938 |
|
| 939 |
self._sampler._update_statistics(False) |
| 940 |
|
| 941 |
assert(self._sampler._nmoves == nmoves_old + 1) |
| 942 |
assert(self._sampler._accepted == accepted_old) |
| 943 |
|
| 944 |
def testEnergy(self): |
| 945 |
|
| 946 |
E = -self._sampler._pdf.log_prob(self._sampler.state.position) |
| 947 |
|
| 948 |
assert(E == self._sampler.energy) |
| 949 |
|
| 950 |
def testAcceptanceRate(self): |
| 951 |
|
| 952 |
self._sampler._nmoves = 6 |
| 953 |
self._sampler._accepted = 3 |
| 954 |
|
| 955 |
assert(self._sampler.acceptance_rate == 0.5) |
| 956 |
|
| 957 |
self._sampler._nmoves = 0 |
| 958 |
self._sampler._accepted = 0 |
| 959 |
|
| 960 |
assert(self._sampler.acceptance_rate == 0.0) |
| 961 |
|
| 962 |
def testLastMoveAccepted(self): |
| 963 |
|
| 964 |
self._sampler._last_move_accepted = False |
| 965 |
|
| 966 |
np.random.seed(5) |
| 967 |
|
| 968 |
self._sampler.sample() |
| 969 |
|
| 970 |
assert(self._sampler.last_move_accepted == True) |
| 971 |
|
| 972 |
def testTemperature(self): |
| 973 |
|
| 974 |
assert(self._sampler.temperature == self._sampler._temperature) |
| 975 |
|
| 976 |
def testSample(self): |
| 977 |
|
| 978 |
np.random.seed(5) |
| 979 |
|
| 980 |
ipos = np.array([2.0]) |
| 981 |
self._sampler.state = State(ipos) |
| 982 |
|
| 983 |
res = self._sampler.sample() |
| 984 |
|
| 985 |
assert(res.position == ipos * 2.0) |
| 986 |
assert(res.momentum == None) |
| 987 |
|
| 988 |
assert(self._sampler.state.position == ipos * 2.0) |
| 989 |
assert(self._sampler.state.momentum == None) |
| 990 |
|
| 991 |
|
| 992 |
class MockedHMCSampler(HMCSampler): |
| 993 |
|
| 994 |
def _propagator_factory(self): |
| 995 |
|
| 996 |
return MockPropagator() |
| 997 |
|
| 998 |
# def _propose(self): |
| 999 |
|
| 1000 |
# print np.random.normal(size=2) |
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
@test.unit |
| 1005 |
class TestHMCSampler(test.Case): |
| 1006 |
|
| 1007 |
def setUp(self): |
| 1008 |
|
| 1009 |
self._mass_matrix = InvertibleMatrix(np.array([[2.0, 0.0], [1.0, 3.0]])) |
| 1010 |
self._initstate = State(np.array([1.0, 2.0])) |
| 1011 |
self._sampler = MockedHMCSampler(pdf=SamplePDF(), state=self._initstate.clone(), |
| 1012 |
gradient=SamplePDF().grad, |
| 1013 |
timestep=0.3, nsteps=25, mass_matrix=self._mass_matrix) |
| 1014 |
|
| 1015 |
def testPropose(self): |
| 1016 |
|
| 1017 |
np.random.seed(5) |
| 1018 |
initmom = np.random.multivariate_normal(mean=np.zeros(len(self._initstate.position)), |
| 1019 |
cov=self._mass_matrix) |
| 1020 |
|
| 1021 |
self._sampler.state = self._initstate.clone() |
| 1022 |
|
| 1023 |
np.random.seed(5) |
| 1024 |
res = self._sampler._propose() |
| 1025 |
|
| 1026 |
assert(np.all(res.current_state.position == self._initstate.position)) |
| 1027 |
assert(np.all(res.current_state.momentum == initmom)) |
| 1028 |
|
| 1029 |
assert(np.all(res.proposal_state.position == self._initstate.position * 2.0)) |
| 1030 |
assert(np.all(res.proposal_state.momentum == initmom * 2)) |
| 1031 |
|
| 1032 |
def testHamiltonian(self): |
| 1033 |
|
| 1034 |
state = State(np.array([1.0, 2.0]), np.array([2.0, 1.0])) |
| 1035 |
|
| 1036 |
assert(self._sampler._hamiltonian(state) == 3.5 - np.log(1.0 / (2.0 * np.pi))) |
| 1037 |
|
| 1038 |
def testCalcPacc(self): |
| 1039 |
|
| 1040 |
istate = State(np.array([1.0, 2.0]), np.array([2.0, 1.0])) |
| 1041 |
fstate = State(np.array([1.0, 2.0]) * 2.0, np.array([2.0, 1.0]) * 2.0) |
| 1042 |
pcom_fstate = fstate.clone() |
| 1043 |
pcom = SimpleProposalCommunicator(istate.clone(), pcom_fstate) |
| 1044 |
self._sampler.state.momentum = None |
| 1045 |
|
| 1046 |
pacc = self._sampler._calc_pacc(pcom) |
| 1047 |
|
| 1048 |
dH = self._sampler._hamiltonian(fstate) - self._sampler._hamiltonian(istate) |
| 1049 |
|
| 1050 |
assert(csb.numeric.exp(-dH / self._sampler.temperature) == pacc) |
| 1051 |
assert(pcom_fstate.momentum == None) |
| 1052 |
|
| 1053 |
|
| 1054 |
pcom_fstate = fstate.clone() |
| 1055 |
pcom = SimpleProposalCommunicator(istate.clone(), pcom_fstate) |
| 1056 |
self._sampler.state.momentum = np.array([1.0, 4.0]) |
| 1057 |
|
| 1058 |
pacc = self._sampler._calc_pacc(pcom) |
| 1059 |
|
| 1060 |
dH = self._sampler._hamiltonian(fstate) - self._sampler._hamiltonian(istate) |
| 1061 |
|
| 1062 |
assert(csb.numeric.exp(-dH / self._sampler.temperature) == pacc) |
| 1063 |
assert(np.all(pcom_fstate.momentum == np.array([1.0, 4.0]))) |
| 1064 |
|
| 1065 |
|
| 1066 |
if __name__ == '__main__': |
| 1067 |
|
| 1068 |
test.Console() |