def main(options):
    """
    The main function.
    """
    mpi, comm, rank, size = init_mpi(options.use_mpi)
    model = initialize_pymc_model(options.model, comm=comm)
    mcmc = pm.MCMC(model)
    for rv in mcmc.stochastics:
        mcmc.use_step_method(ps.RandomWalk, rv)
    if options.num_particles is None:
        options.num_particles = size
    if options.db_filename is None:
        db_filename = (os.path.splitext(options.model)[0] + '_N=' +
                       str(options.num_particles) + '_M=' +
                       str(options.num_mcmc) + '.pcl')
    if rank == 0 and os.path.exists(db_filename):
        print '-', db_filename, 'exists'
        print '- I am removing it'
        os.remove(db_filename)
    smc_sampler = ps.SMC(mcmc,
                         num_particles=options.num_particles,
                         num_mcmc=options.num_mcmc,
                         verbose=4,
                         db_filename=db_filename,
                         gamma_is_an_exponent=True,
                         mpi=mpi,
                         update_db=True)
    smc_sampler.initialize(0.)
    smc_sampler.move_to(1.)
예제 #2
0
import simple_model
import pymc
import sys
import os
sys.path.insert(0, os.path.abspath('..'))
import pysmc
import matplotlib.pyplot as plt
import pickle
import numpy as np

if __name__ == '__main__':
    # Construct the SMC sampler
    model = simple_model.make_model()
    mcmc = pymc.MCMC(model)
    mcmc.use_step_method(pysmc.RandomWalk, model['mixture'])
    smc_sampler = pysmc.SMC(mcmc, num_particles=1000, num_mcmc=1, verbose=4)
    # Initialize SMC at gamma = 0.01
    smc_sampler.initialize(0.001)
    # Move the particles to gamma = 1.0
    smc_sampler.move_to(1.)
    print(smc_sampler.log_Zs)
    # Get a particle approximation
    p = smc_sampler.get_particle_approximation()
    print(p.mean)
    print(p.variance)
    # Plot a histogram
    data = [
        p.particles[i]['stochastics']['mixture']
        for i in range(p.num_particles)
    ]
    data = np.array(data)
예제 #3
0
import mpi4py.MPI as mpi

if __name__ == '__main__':
    model = diffusion_inverse_model.make_model()
    mcmc = pm.MCMC(model)
    mcmc.use_step_method(ps.GaussianMixtureStep, model['location'])
    mcmc.use_step_method(ps.LognormalRandomWalk,
                         model['alpha'],
                         proposal_sd=1.)
    mcmc.use_step_method(ps.LognormalRandomWalk, model['beta'], proposal_sd=1.)
    mcmc.use_step_method(ps.LognormalRandomWalk, model['tau'], proposal_sd=1.)
    try:
        smc_sampler = ps.SMC(mcmc,
                             num_particles=64,
                             num_mcmc=1,
                             verbose=3,
                             mpi=mpi,
                             db_filename='diffusion_inverse.pickle',
                             update_db=True,
                             gamma_is_an_exponent=True)
        smc_sampler.initialize(0.)
        smc_sampler.move_to(1.)
    except Exception as e:
        print str(e)
        mpi.COMM_WORLD.Abort(1)
    p = smc_sampler.get_particle_approximation()
    m = p.mean
    v = p.variance
    if mpi.COMM_WORLD.Get_rank() == 0:
        print m
        print v
예제 #4
0
sys.path.insert(0, os.path.abspath('..'))
import pysmc as ps
import mpi4py.MPI as mpi
import matplotlib.pyplot as plt
import numpy as np


if __name__ == '__main__':
    do_init = not os.path.exists('test_db.pickle')
    # Construct the SMC sampler
    model = simple_model.make_model()
    mcmc = pm.MCMC(model)
    mcmc.use_step_method(ps.RandomWalk, model['mixture'])
    smc_sampler = ps.SMC(mcmc, num_particles=512,
                         num_mcmc=1, verbose=1,
                         mpi=mpi, gamma_is_an_exponent=True,
                         ess_reduction=0.9,
                         db_filename='test_db.pickle',
                         update_db=True)
    # Initialize SMC at gamma = 0.01
    if do_init:
        smc_sampler.initialize(0.001, num_mcmc_per_particle=100)
    # Move the particles to gamma = 1.0
    smc_sampler.move_to(1.)
    # Get a particle approximation
    p = smc_sampler.get_particle_approximation().gather()
    # Plot a histogram
    #if mpi.COMM_WORLD.Get_rank() == 0:
    #    x = np.linspace(-5, 5, 200)
    #    y = simple_model.eval_stochastic_variable(model['mixture'], x)
    #    plt.plot(x, y, linewidth=2)
    #    ps.hist(p, 'mixture')
예제 #5
0
import pysmc
import pymc
sys.path.insert(0, os.path.abspath('..'))
import matplotlib.pyplot as plt
import cPickle as pickle


if __name__ == '__main__':
    model = reaction_kinetics_model.make_model()
    # Construct the SMC sampler
    mcmc = pymc.MCMC(model)
    mcmc.use_step_method(pysmc.LognormalRandomWalk, model['k1'])
    mcmc.use_step_method(pysmc.LognormalRandomWalk, model['k2'])
    mcmc.use_step_method(pysmc.LognormalRandomWalk, model['sigma'])
    smc_sampler = pysmc.SMC(mcmc, num_particles=100,
                            num_mcmc=1, verbose=1,
                            gamma_is_an_exponent=True)
    # Initialize SMC at gamma = 0.01
    smc_sampler.initialize(0.0)
    # Move the particles to gamma = 1.0
    smc_sampler.move_to(1.)
    # Get a particle approximation
    p = smc_sampler.get_particle_approximation()
    print p.mean
    print p.variance

    data = [p.particles[i]['stochastics']['k1'] for i in xrange(p.num_particles)]
    data = np.array(data)
    plt.plot(data, np.zeros(data.shape), 'ro', markersize=10)
    pysmc.hist(p, 'mixture')
    y = data[:, 1:]
    y = y.reshape((1, y.shape[0] * y.shape[1])) / 500.
    f = CatalysisModelDMNLESS()

    @pm.deterministic
    def model_output(kappa=kappa):
        return f(kappa)['f']

    @pm.stochastic(observed=True)
    def output(value=y, model_output=model_output, sigma2=sigma2, gamma=gamma):
        return gamma * pm.normal_like(y, model_output, 1. / sigma2)

    return locals()


if __name__ == '__main__':
    model = make_model()
    mcmc = pm.MCMC(model)
    mcmc.use_step_method(ps.RandomWalk, model['kappa'])
    mcmc.use_step_method(ps.RandomWalk, model['sigma2'])
    smc_sampler = ps.SMC(mcmc,
                         num_particles=10240,
                         num_mcmc=1,
                         verbose=4,
                         db_filename='demos/smc_catalysis.pcl',
                         gamma_is_an_exponent=True,
                         mpi=mpi,
                         update_db=True)
    smc_sampler.initialize(0.)
    smc_sampler.move_to(1.)