Ejemplo n.º 1
0
def mixing_times(H, trials=10):
    """runs the two samplers with the given energy a bunch of times
       reports back their average mixing times
    """
    order = len(H) * 2
    c_tm = np.zeros(trials)
    d_tm = np.zeros(trials)

    for i in xrange(trials):
        # todo: add reset methods
        print "trial: {}".format(i)
        hmc = AlgebraicDiscrete(order, energies=H)
        chmc = AlgebraicContinuous(order, energies=H)
        d_tm[i] = hmc.calculate_mixing_time()
        c_tm[i] = chmc.calculate_mixing_time()

    print "Average mixing time for discrete sampler: {}".format(np.mean(d_tm))
    print "Average mixing time for continuous sampler: {}".format(np.mean(c_tm))
Ejemplo n.º 2
0
def calc_spectral_gaps(order, trials=1, n_sample_step=1000):
    """Approximates the spectral gap for each sampler at a certain order
    returns avg_discrete_sg, discrete_sg_var,  avg_continuous_sg, continuous_sg_var
    """
    assert order % 2 == 0
    # normally distributed?
    H = np.random.randn(order / 2)
    c_sg = np.zeros(trials)
    h_sg = np.zeros(trials)

    print "Order: {}".format(order)

    for i in xrange(trials):
        hmc = AlgebraicDiscrete(order, energies=H)
        chmc = AlgebraicContinuous(order, energies=H)
        # runs until close to equilibrium distribution
        n_hmc = hmc.calculate_mixing_time()
        n_chmc = chmc.calculate_mixing_time()
        h_sg[i] = sg(hmc)
        c_sg[i] = sg(chmc)
        print "{} samplings steps for hmc to approach equilibirium".format(n_hmc)
        print "{} samplings steps for chmc to approach equilibirium".format(n_chmc)

    return np.mean(h_sg), np.std(h_sg), np.mean(c_sg), np.std(c_sg)