tsallis.q = 1.06 posterior.set_q(tsallis.q) logistic.alpha = L_intra.error_model.alpha lowerupper.tau = L_bbone.error_model.k logistic.beta = 0.1 posterior.likelihoods['contacts'].set_lambda(float(logistic.beta)) ## Tsallis ensemble only print '\n--- testing Tsallis ensemble ---\n' L_intra.enabled, prior.enabled, L_bbone.enabled, L_rog.enabled = 0, 1, 0, 0 with take_time('calculating energy with isdhic'): a = tsallis.log_prob() with take_time('calculating energy with isd'): b = posterior.torsion_posterior.energy(coords.get()) report_log_prob(a, -b) with take_time('calculating forces with isdhic'): forces.set(0.) tsallis.update_forces() with take_time('calculating forces with isd'): b = posterior.torsion_posterior.gradient(coords.get()) report_gradient(forces.get(), -b) ## logistic likelihood only
## start from stretched out chromosome structure extended = np.multiply.outer(np.arange(n_particles), np.eye(3)[0]) * diameter coords.set(extended) ## use Hamiltonian Monte Carlo generate X chromosome structures ## from the posterior distribution hmc = HamiltonianMonteCarlo(posterior,stepsize=1e-3) hmc.leapfrog.n_steps = 100 hmc.adapt_until = int(1e6) hmc.activate() samples = [] with take_time('running HMC'): while len(samples) < 1e3: samples.append(hmc.next()) X = np.array([state.positions for state in samples]).reshape(len(samples),-1,3) E = np.array([state.potential_energy for state in samples]) K = np.array([state.kinetic_energy for state in samples]) if False: from isd import utils prior = posterior.priors[0] E_isd = utils.Load('/tmp/E') X = E_isd.torsion_angles.copy()
from isdhic.core import take_time from isdhic.chromosome import ChromosomeSimulation import compare_with_isd as compare ## create posteriors with isd and isdhic with take_time('creating isdhic posterior'): filename = '../scripts/chrX_cell1_50kb.py' with open(filename) as script: exec script with take_time('creating isd posterior'): isd_posterior, _ = compare.create_isd_posterior( run='geo_rosetta_contacts_Rg_highres') ## set nuisance parameters isd_posterior.likelihoods['contacts'].error_model.alpha = posterior[ 'contacts'].alpha isd_posterior.likelihoods['Rg'].error_model.k = posterior['rog'].tau state = isd_posterior.as_state() state.torsion_angles[...] = posterior.params['coordinates'].get() isd_posterior.energy(state) out = '{2}: {0:.5e} / {1:.5e}' print '\n' + out.format(-posterior.log_prob(), state.E, 'posterior')
## create contact data n_data = 100 pairs = random_pairs(universe.n_particles, n_data) data = np.random.random(n_data) * 10. mock = isdhic.ModelDistances(pairs, 'contacts') relu = Relu('contacts', data, mock, params=params) relu2 = isdhic.Relu('contacts2', data, mock, params=params) for param in (coords, forces, mock, relu.steepness): params.add(param) mock.update(params) with take_time('evaluating python version of relu likelihood'): lgp = relu.log_prob() print 'log_prob={0:.3e}'.format(lgp) with take_time('evaluating cython version of relu likelihood'): lgp = relu2.log_prob() print 'log_prob={0:.3e}'.format(lgp) with take_time('evaluating derivatives of python version'): relu.update_derivatives() with take_time('evaluating derivatives of cython version'): relu2.update_derivatives()
contacts = isdhic.ModelDistances(pairs, 'contacts') chain = isdhic.ModelDistances(connectivity, 'chain') distances = isdhic.ModelDistances(connectivity + pairs, 'all') params = isdhic.Parameters() for param in (precision, coords, distances): params.add(param) print 'Getting/setting of Cartesian coordinates works? -', print np.fabs(universe.coords.flatten() - coords.get()).max() < 1e-10 print print 'Comparing cython with numpy implementation\n' with take_time('\tevaluate "{}" using cython'.format(contacts.name)): contacts.update(params) with take_time('\tevaluate "{}" using numpy'.format(contacts.name)): d_numpy = numpy_distances(contacts, universe) print '\n\tmax discrepancy between distances: {0:.1e}\n'.format( np.fabs(contacts.get() - d_numpy).max()) print 'Does it pay off to evaluate all distances at once?\n' with take_time('\tfirst "{0}", then "{1}"'.format(chain.name, contacts.name)): chain.update(params) contacts.update(params)
def update_forces(self): self.params['forces'].set(-np.dot(self.K, self.x)) if __name__ == '__main__': osci = Oscillator(np.diag([10., 1.])) osci = Oscillator(np.array([[10., -2.5], [-2.5, 1.]])) hmc = HamiltonianMonteCarlo(osci, stepsize=1e-3) hmc.leapfrog.n_steps = 100 hmc.adapt_until = int(1e6) hmc.activate() with take_time('running HMC'): samples = [] while len(samples) < 1e4: samples.append(hmc.next()) q = np.array([state.positions for state in samples]) p = np.array([state.momenta for state in samples]) E = np.array([state.potential_energy for state in samples]) sigma = np.diagonal(np.linalg.inv(osci.K)) limits = (-4 * sigma.max(), 4 * sigma.max()) x = 5 * sigma.max() * np.linspace(-1., 1., 1000) kw_hist = dict(normed=True, histtype='stepfilled', bins=30,
boltzmann = isdhic.BoltzmannEnsemble('boltzmann', forcefield, params) tsallis = isdhic.TsallisEnsemble('tsallis', forcefield, params) print forcefield.energy(coords.get()) print boltzmann.log_prob() boltzmann.beta = 0.1 tsallis.E_min = np.floor(tsallis.log_prob()) - 500. tsallis.q = 1.06 tsallis.beta = 0.5 eps = 1e-7 out = ' max discrepancy={0:.1e}, corr={1:.1f}' for prior in (boltzmann, tsallis): forces.set(0.) with take_time('\ncalculate forces {}'.format(prior)): prior.update_forces() a = forces.get().copy() f = lambda x, prior=prior: log_prob(x, prior) x = params['coordinates'].get().copy() b = optimize.approx_fprime(x, f, eps) cc = np.corrcoef(a, b)[0, 1] * 100 d_max = np.max(np.fabs(a - b)) print out.format(d_max, cc)
simulation = ChromosomeSimulation(n_particles, diameter=beadsize) posterior = simulation.create_chromosome(contacts) universe = simulation.universe coords = simulation.params['coordinates'] forces = simulation.params['forces'] posterior['rog'].data[...] = isd_posterior.likelihoods['Rg'].data.values posterior['rog'].tau = isd_posterior.likelihoods['Rg'].error_model.k print posterior for pdf in posterior: print pdf print '\n--- testing conditional posterior ---\n' with take_time('evaluating log probability of {}'.format(posterior)): a = posterior.log_prob() with take_time('evaluating posterior with isd'): b = isd_posterior.torsion_posterior.energy(coords.get()) compare.report_log_prob(a, -b) with take_time('\nevaluating forces of {}'.format(posterior)): forces.set(0.) posterior.update_forces() with take_time('calculating forces with isd'): b = isd_posterior.torsion_posterior.gradient(coords.get()) compare.report_gradient(forces.get(), -b)