Example #1
0
    # Set up the 2-D gaussian model
    means_x = [ 0.1, 0.5, 0.9,
                0.1, 0.5, 0.9,
                0.1, 0.5, 0.9]
    means_y = [0.1, 0.1, 0.1,
               0.5, 0.5, 0.5,
               0.9, 0.9, 0.9]
    sd = 0.01
    tdg = TwoDGaussianFit(means_x, means_y, sd ** 2)

    # Create temperature array based on number of workers (excluding master)
    temps = np.logspace(np.log10(min_temp), np.log10(max_temp), num_chains-1)

    # Create the options
    opts = MCMCOpts()
    opts.model = tdg
    opts.estimate_params = tdg.parameters
    opts.initial_values = [1.001, 1.001]
    opts.nsteps = nsteps
    opts.anneal_length = 0 # necessary so cooling does not occur
    opts.T_init = temps[rank - 1] # Use the temperature for this worker
    opts.use_hessian = False
    opts.seed = 1
    opts.norm_step_size = 0.1
    opts.likelihood_fn = tdg.likelihood

    mcmc = MCMC(opts)
    mcmc.initialize()

    # The master coordinates when swaps occur ---------
    swap_period = 5
    # Temperature range
    min_temp = 1
    max_temp = 1e5

    # Create temperature array based on number of workers (excluding master)
    temps = np.logspace(np.log10(min_temp), np.log10(max_temp), num_chains-1)

    rate_step_sizes = np.logspace(np.log10(5e-4), np.log10(5e-2),
                                  num_chains-1)
    scaling_step_sizes = np.logspace(np.log10(1e-4), np.log10(1e-2),
                                     num_chains-1)

    # Initialize the MCMC arguments
    b = args['builder']
    opts = MCMCOpts()
    opts.model = b.model
    opts.tspan = args['time']
    opts.estimate_params = b.estimate_params
    opts.initial_values = b.random_initial_values()
    opts.nsteps = args['nsteps']

    #opts.norm_step_size = np.array([0.01] + \
    #                    ([0.05] * (len(opts.estimate_params)-1)))
    # FIXME not correct ordering
    #opts.norm_step_size = np.array([scaling_step_sizes[rank-1]] + \
    #                    ([rate_step_sizes[rank-1]] *
    #                     (len(opts.estimate_params)-1)))
    #print "Step size: %g" % (opts.norm_step_size[1])
    opts.norm_step_size = 0.05
    Solver._use_inline = True

    # Frequency for proposing swaps
    swap_period = 5
    # Temperature range
    min_temp = 1
    max_temp = 1e5

    # Create temperature array based on number of workers (excluding master)
    temps = np.logspace(np.log10(min_temp), np.log10(max_temp), num_chains-1)

    rate_step_sizes = np.logspace(np.log10(5e-4), np.log10(5e-2), num_chains-1)
    scaling_step_sizes = np.logspace(np.log10(1e-4), np.log10(1e-2), num_chains-1)

    # Initialize the MCMC arguments
    opts = MCMCOpts()
    opts.model = model
    opts.tspan = nbd.time_other
    opts.estimate_params = builder.estimate_params
    opts.initial_values = builder.random_initial_values()
    opts.nsteps = nsteps

    #opts.norm_step_size = np.array([0.01] + \
    #                    ([0.05] * (len(opts.estimate_params)-1)))
    opts.norm_step_size = np.array([scaling_step_sizes[rank-1]] + \
                        ([rate_step_sizes[rank-1]] * (len(opts.estimate_params)-1)))
    print "Step size: %g" % (opts.norm_step_size[1])

    opts.sigma_step = 0 # Don't adjust step size
    opts.sigma_max = 50
    opts.sigma_min = 0.01
Example #4
0
    # Number of chains/workers in the whole pool
    num_chains = comm.Get_size()
    # The rank of this chain (0 is the master, others are workers)
    rank = comm.Get_rank()

    # Set up the 2-D gaussian model
    means_x = [0.1, 0.5, 0.9, 0.1, 0.5, 0.9, 0.1, 0.5, 0.9]
    means_y = [0.1, 0.1, 0.1, 0.5, 0.5, 0.5, 0.9, 0.9, 0.9]
    sd = 0.01
    tdg = TwoDGaussianFit(means_x, means_y, sd**2)

    # Create temperature array based on number of workers (excluding master)
    temps = np.logspace(np.log10(min_temp), np.log10(max_temp), num_chains - 1)

    # Create the options
    opts = MCMCOpts()
    opts.model = tdg
    opts.estimate_params = tdg.parameters
    opts.initial_values = [1.001, 1.001]
    opts.nsteps = nsteps
    opts.anneal_length = 0  # necessary so cooling does not occur
    opts.T_init = temps[rank - 1]  # Use the temperature for this worker
    opts.use_hessian = False
    opts.seed = 1
    opts.norm_step_size = 0.1
    opts.likelihood_fn = tdg.likelihood

    mcmc = MCMC(opts)
    mcmc.initialize()

    # The master coordinates when swaps occur ---------
Example #5
0
def fit_gaussian(nsteps):
    num_dimensions = 1
    # Create the dummy model
    means = 10 ** np.random.rand(num_dimensions)
    variances = np.random.rand(num_dimensions)
    g = GaussianFit(means, variances)

    # Create the options
    opts = MCMCOpts()
    opts.model = g
    opts.estimate_params = g.parameters
    opts.initial_values = [1]
    opts.nsteps = nsteps
    opts.anneal_length = nsteps/10
    opts.T_init = 1
    opts.use_hessian = False
    opts.seed = 1
    opts.norm_step_size = 1
    opts.likelihood_fn = g.likelihood
    opts.step_fn = step

    # Create the MCMC object
    mcmc = MCMC(opts)
    mcmc.initialize()
    mcmc.estimate()
    mcmc.prune(nsteps/10, 1)

    plt.ion()
    for i in range(mcmc.num_estimate):
        mean_i = np.mean(mcmc.positions[:,i])
        var_i = np.var(mcmc.positions[:, i])
        print "True mean: %f" % means[i]
        print "Sampled mean: %f" % mean_i
        print "True variance: %f" % variances[i]
        print "Sampled variance: %f" % var_i
        plt.figure()
        (heights, points, lines) = plt.hist(mcmc.positions[:,i], bins=50,
                                        normed=True)
        plt.plot(points, norm.pdf(points, loc=means[i],
             scale=np.sqrt(variances[i])), 'r')

    mean_err = np.zeros(len(mcmc.positions))
    var_err = np.zeros(len(mcmc.positions))
    for i in range(len(mcmc.positions)):
        mean_err[i] = (means[0] - np.mean(mcmc.positions[:i+1,0]))
        var_err[i] = (variances[0] - np.var(mcmc.positions[:i+1,0]))
    plt.figure()
    plt.plot(mean_err)
    plt.plot(var_err)

    return mcmc
Example #6
0
def fit_twod_gaussians_by_pt(nsteps):
    means_x = [ 0.1, 0.5, 0.9,
                0.1, 0.5, 0.9,
                0.1, 0.5, 0.9]
    means_y = [0.1, 0.1, 0.1,
               0.5, 0.5, 0.5,
               0.9, 0.9, 0.9]
    sd = 0.01
    tdg = TwoDGaussianFit(means_x, means_y, sd ** 2)

    # Create the options
    opts = MCMCOpts()
    opts.model = tdg
    opts.estimate_params = tdg.parameters
    opts.initial_values = [1.001, 1.001]
    opts.nsteps = nsteps
    opts.anneal_length = 0 # necessary so cooling does not occur
    opts.T_init = 1
    opts.use_hessian = False
    opts.seed = 1
    opts.norm_step_size = 0.1
    opts.likelihood_fn = tdg.likelihood
    opts.step_fn = step

    # Create the PT object
    num_temps = 8
    pt = PT_MCMC(opts, num_temps, 100)
    pt.estimate()

    plt.ion()
    for chain in pt.chains:
        fig = plt.figure()
        chain.prune(nsteps/10, 1)
        plt.scatter(chain.positions[:,0], chain.positions[:,1])
        ax = fig.gca()
        for x, y in zip(means_x, means_y):
            circ = plt.Circle((x, y), radius=2*sd, color='r', fill=False)
            ax.add_patch(circ)
        plt.xlim((-0.5, 1.5))
        plt.ylim((-0.5, 1.5))
        plt.title('Temp = %.2f' % chain.options.T_init)
        plt.show()

    return pt
Example #7
0
def fit_twod_gaussians(nsteps):
    means_x = [ 0.1, 0.5, 0.9,
                0.1, 0.5, 0.9,
                0.1, 0.5, 0.9]
    means_y = [0.1, 0.1, 0.1,
               0.5, 0.5, 0.5,
               0.9, 0.9, 0.9]
    sd = 0.01
    tdg = TwoDGaussianFit(means_x, means_y, sd ** 2)

    # Create the options
    opts = MCMCOpts()
    opts.model = tdg
    opts.estimate_params = tdg.parameters
    opts.initial_values = [1.001, 1.001]
    opts.nsteps = nsteps
    opts.anneal_length = nsteps/10
    opts.T_init = 1
    opts.use_hessian = False
    opts.seed = 1
    opts.norm_step_size = 0.1
    opts.likelihood_fn = tdg.likelihood
    opts.step_fn = step

    mcmc = MCMC(opts)
    mcmc.initialize()
    mcmc.estimate()

    plt.ion()
    fig = plt.figure()
    mcmc.prune(0, 20)
    plt.scatter(mcmc.positions[:,0], mcmc.positions[:,1])
    ax = fig.gca()
    for x, y in zip(means_x, means_y):
        circ = plt.Circle((x, y), radius=2*sd, color='r', fill=False)
        ax.add_patch(circ)
    plt.xlim((-0.5, 1.5))
    plt.ylim((-0.5, 1.5))
    plt.show()

    return mcmc
Example #8
0
def fit_beta_by_pt(nsteps):
    """Fit a beta distribution by parallel tempering."""
    num_dimensions = 1
    # Create the dummy model
    b = BetaFit(0.5, 0.5)

    # Create the options
    opts = MCMCOpts()
    opts.model = b
    opts.estimate_params = b.parameters
    opts.initial_values = [10 ** 0.5]
    opts.nsteps = nsteps
    opts.anneal_length = 0
    opts.T_init = 1
    opts.use_hessian = False
    opts.seed = 1
    opts.norm_step_size = 0.5
    opts.likelihood_fn = b.likelihood
    opts.step_fn = step

    # Create the MCMC object
    num_temps = 8
    pt = PT_MCMC(opts, num_temps, 10)
    pt.estimate()

    plt.ion()
    for chain in pt.chains:
        fig = plt.figure()
        chain.prune(nsteps/10, 1)
        (heights, points, lines) = plt.hist(chain.positions, bins=100,
                                            normed=True)
        plt.plot(points, beta.pdf(points, b.a, b.b), 'r')
        plt.ylim((0,10))
        plt.xlim((0, 1))
    return pt
Example #9
0
def fit_beta(nsteps):
    """Fit a beta distribution by MCMC."""
    num_dimensions = 1
    # Create the dummy model
    b = BetaFit(0.5, 0.5)

    # Create the options
    opts = MCMCOpts()
    opts.model = b
    opts.estimate_params = b.parameters
    opts.initial_values = [1.001]
    opts.nsteps = nsteps
    opts.anneal_length = nsteps/10
    opts.T_init = 100
    opts.use_hessian = False
    opts.seed = 1
    opts.norm_step_size = 0.01
    opts.likelihood_fn = b.likelihood
    opts.step_fn = step

    # Create the MCMC object
    mcmc = MCMC(opts)
    mcmc.initialize()
    mcmc.estimate()
    mcmc.prune(nsteps/10, 1)

    plt.ion()
    for i in range(mcmc.num_estimate):
        plt.figure()
        (heights, points, lines) = plt.hist(mcmc.positions[:,i], bins=100,
                                        normed=True)
        plt.plot(points, beta.pdf(points, b.a, b.b), 'r')
    return mcmc