def testThermostattedMDRensMM(self): self.set2pParams() mm1 = InvertibleMatrix(np.array([[1.0, 0.0], [0.0, 5.0]])) mm2 = InvertibleMatrix(np.array([[.5, 0.0], [0.0, 10.0]])) pdf = Multimodal2DPDF() params = [ ThermostattedMDRENSSwapParameterInfo(self.samplers[0], self.samplers[1], 0.01, 15, pdf.grad, temperature=self.Ts[0], mass_matrix=mm1), ThermostattedMDRENSSwapParameterInfo(self.samplers[1], self.samplers[2], 0.1, 15, pdf.grad, temperature=self.Ts[1], mass_matrix=mm2) ] algorithm = ThermostattedMDRENS(self.samplers, params) self._run(algorithm)
def testHMCPropagatorMM(self): mm = InvertibleMatrix(np.array([[1., 0.], [0., 2.]])) init_state = State(np.random.normal(size=2)) gen = HMCPropagator(self.pdf, self.gradient, self.timestep * 1.5, self.nsteps, mass_matrix=mm) self.checkResult(gen.generate(init_state, self.nits))
def _setup_matrices(self, mass_matrix): self._d = len(self.state.position) self._mass_matrix = mass_matrix if self.mass_matrix is None: self.mass_matrix = InvertibleMatrix(numpy.eye(self._d), numpy.eye(self._d)) self._momentum_covariance_matrix = self._temperature * self.mass_matrix
def setUp(self): self._mass_matrix = InvertibleMatrix(np.array([[2.0, 0.0], [1.0, 3.0]])) self._initstate = State(np.array([1.0, 2.0])) self._sampler = MockedHMCSampler(pdf=SamplePDF(), state=self._initstate.clone(), gradient=SamplePDF().grad, timestep=0.3, nsteps=25, mass_matrix=self._mass_matrix)
def _set_mass_matrix(self, state): """ Sets the mass matrix in the param object. @param state: The initial state which is used to determine the dimension of the mass matrix @type state: L{State} """ if self.param.mass_matrix is None: d = len(state.position) self.param.mass_matrix = InvertibleMatrix(numpy.eye(d))
def testHMCPropagation(self): pdf = HO() sys = HamiltonianSysInfo(ReducedHamiltonian(pdf.log_prob, pdf.gradient)) param = HMCPropagationParam(None, None, None) hmcprop = HMCPropagationMocked(sys, param) init = State(np.array([2.0]), np.array([2.0])) ## Test _set_mass_matrix d = len(init.position) param = HMCPropagationParam(None, None, None, mass_matrix=InvertibleMatrix(np.eye(d))) hmcprop = HMCPropagationMocked(sys, param) hmcprop._set_mass_matrix(init) self.assertEqual(hmcprop.param.mass_matrix, InvertibleMatrix(np.eye(len(init.position)))) param = HMCPropagationParam(None, None, None) hmcprop = HMCPropagationMocked(sys, param) hmcprop._set_mass_matrix(init) self.assertEqual(hmcprop.param.mass_matrix, InvertibleMatrix(np.eye(len(init.position)))) ## Test _calculate_heat final = State(init.position * 2, init.momentum * 2) traj = Trajectory([init, final]) self.assertEqual(hmcprop._calculate_heat(traj), 6.0) ## Test __call__ result = hmcprop(init) self.assertEqual(init.position, result.initial.position) self.assertEqual(init.momentum, result.initial.momentum) self.assertEqual(result.final.position, init.position * 2) self.assertEqual(result.final.momentum, init.momentum * 2) self.assertEqual(result.heat, 6.0)
def generate(self, init_state, length, return_trajectory=False): if self._first_run == True and self.mass_matrix is None: d = len(init_state.position) self.mass_matrix = InvertibleMatrix(numpy.eye(d), numpy.eye(d)) integrator = self._integrator(self.timestep, self.gradient) builder = TrajectoryBuilder.create(full=return_trajectory) builder.add_initial_state(init_state) heat = 0. state = init_state.clone() d = len(state.position) n_randoms = int(1.5 * length * self._collision_probability / float(self._update_interval)) if n_randoms < 5: n_randoms = 5 if not self.mass_matrix.is_unity_multiple: randoms = numpy.random.multivariate_normal(mean=numpy.zeros(d), cov=self.mass_matrix, size=n_randoms).T else: randoms = numpy.random.normal(scale=numpy.sqrt( self.mass_matrix[0][0]), size=(d, n_randoms)) self._random_loopers = [Looper(x) for x in randoms] for i in range(length - 1): state, heat = self._step(i, state, heat, integrator) builder.add_intermediate_state(state) state, heat = self._step(length - 1, state, heat, integrator) builder.add_final_state(state) traj = builder.product traj.heat = heat return traj
def testCaching(self): from csb.numeric import InvertibleMatrix # Initialize with matrix testmatrix = InvertibleMatrix(self.m_general) # Check if it worked self.assertListEqual(testmatrix._matrix.flatten().tolist(), self.m_general.flatten().tolist()) # Because the testmatrix.inverse hasn't been accessed yet, testmatrix._inverse should be None self.assertEqual(testmatrix._inverse_matrix, None) # Now we access testmatrix.inverse, which onyl now actually calculates the inverse self.assertListEqual(testmatrix.inverse.flatten().tolist(), self.m_general_inv.flatten().tolist()) # Let's change testmatrix via testmatrix.__imul__ testmatrix *= 2. # Check if that worked self.assertListEqual(testmatrix._matrix.flatten().tolist(), (2.0 * self.m_general.flatten()).tolist()) # This operation should not have changed the testmatrix._inverse_matrix field, as # we didn't access testmatrix.inverse again self.assertListEqual(testmatrix._inverse_matrix.flatten().tolist(), self.m_general_inv.flatten().tolist()) # New we access testmatrix.inverse, which calculates the inverse and updates the field self.assertListEqual(testmatrix.inverse.flatten().tolist(), (self.m_general_inv / 2.0).flatten().tolist()) # The same again for testmatrix.__idiv__ testmatrix /= 2. # Check if that worked self.assertListEqual(testmatrix._matrix.flatten().tolist(), (self.m_general.flatten()).tolist()) # This operation should not have changed the testmatrix._inverse_matrix field, as # we didn't access testmatrix.inverse again self.assertListEqual(testmatrix._inverse_matrix.flatten().tolist(), (self.m_general_inv / 2.0).flatten().tolist()) # New we access testmatrix.inverse, which calculates the inverse and updates the field self.assertListEqual(testmatrix.inverse.flatten().tolist(), self.m_general_inv.flatten().tolist()) # Initialize with inverse matrix testmatrix = InvertibleMatrix(inverse_matrix=self.m_general_inv) # Let's see if that worked, e.g. if the testmatrix._inverse_matrix field has been # set correctly self.assertListEqual(testmatrix._inverse_matrix.flatten().tolist(), self.m_general_inv.flatten().tolist()) # Check if the property returns what it's supposed to be self.assertListEqual(testmatrix.inverse.flatten().tolist(), self.m_general_inv.flatten().tolist()) # We didn't call testmatrix.__getitem__() yet, so testmatrix._matrix should be None self.assertEqual(testmatrix._matrix, None) # To include the numerical error invinv = np.linalg.inv(self.m_general_inv) # Now we access testmatrix by its __getitem__ method, which calculates the # testmatrix._matrix field from the testmatrix._inverse_matrix by inversion for i in range(len(testmatrix)): self.assertListEqual(testmatrix[i].tolist(), invinv[i].tolist()) testmatrix = InvertibleMatrix(inverse_matrix=self.m_general_inv) # Let's change testmatrix via testmatrix.__imul__ testmatrix *= 2. # That shouldn't have changed the testmatrix._matrix field (which currently # should be None), but the testmatrix._inverse_matrix field by a factor of 1/2.0 = 0.5 self.assertEqual(testmatrix._matrix, None) self.assertListEqual(testmatrix._inverse_matrix.flatten().tolist(), (self.m_general_inv / 2.0).flatten().tolist()) # Now we access testmatrix by __getitem__, which calculates the matrix # from the inverse and updates the field testmatrix._matrix invinv *= 2.0 for i in range(len(testmatrix)): self.assertListEqual(testmatrix[i].tolist(), invinv[i].tolist()) # The same again for testmatrix.__idiv__ testmatrix = InvertibleMatrix(inverse_matrix=self.m_general_inv) testmatrix /= 2. # Check if testmatrix._matrix is None and if the testmatrix._inverse field # has been multiplied by a factor of 2.0 self.assertEqual(testmatrix._matrix, None) self.assertListEqual(testmatrix.inverse.flatten().tolist(), (self.m_general_inv * 2.0).flatten().tolist()) # All that is supposed to leave testmatrix._matrix with None: self.assertEqual(testmatrix._matrix, None) # Now we access testmatrix by __getitem__ again, which calculates the matrix from # its inverse and updates the field invinv /= 4.0 for i in range(len(testmatrix)): self.assertListEqual(testmatrix[i].tolist(), invinv[i].tolist())
def invertMatrix(self, matrix): from csb.numeric import InvertibleMatrix for i in range(1000): testmatrix = InvertibleMatrix(matrix) dummy = testmatrix.inverse