Ejemplo n.º 1
0
def test_isotropic_zero_mean_equals_log_gaussian_pdf():
    D = 2
    x = np.random.randn(D)
    g = IsotropicZeroMeanGaussian(sigma=np.sqrt(2))
    log_pdf = log_gaussian_pdf(x,
                               mu=np.zeros(D),
                               Sigma=np.eye(D) * 2,
                               is_cholesky=False,
                               compute_grad=False)
    assert_close(log_pdf, g.log_pdf(x))
Ejemplo n.º 2
0
def sample_banana2(N, D, target):
    momentum = IsotropicZeroMeanGaussian(D=D, sigma=1.0)
    num_steps = 1000
    step_size = 0.2
    hmc = HMCBase(target, momentum, num_steps, num_steps, step_size, \
                  step_size, adaptation_schedule=None)
    start_samples = sample_banana(N, D)
    # simulate trajectory from starting point, note _proposal_trajectory is a "hidden" method
    Qs_total = []
    for i in xrange(N):
        current = start_samples[i]
        current_log_pdf = target.log_pdf(current)
        Qs, acc_probs, log_pdf_q = hmc._proposal_trajectory(
            current, current_log_pdf)
        Qs_total.append(Qs[-1])
    return np.asarray(Qs_total)
Ejemplo n.º 3
0
def get_kmc_instance(target):
    step_size_min = 0.01
    step_size_max = 0.1
    num_steps_min = 1
    num_steps_max = 10
    momentum = IsotropicZeroMeanGaussian(D=D)
    schedule = standard_sqrt_schedule
    acc_star = 0.7
    
    # fully automatic parameter tuning in every fit call
    surrogate = KernelExpLiteGaussianAdaptive(sigma=1., lmbda=.001, D=D, N=200,
                                        n_initial=3, n_iter=3, minimum_size_learning=100,
                                        n_initial_relearn=3, n_iter_relearn=3,
                                        param_bounds={'sigma': [-3, 3]}
                                        )
    
    return KMC(surrogate, target,
               momentum, num_steps_min, num_steps_max, step_size_min, step_size_max,
               adaptation_schedule=schedule, acc_star=acc_star)
Ejemplo n.º 4
0
def get_target_momentum():
    D = 2
    target = IsotropicZeroMeanGaussian(D=D)
    momentum = IsotropicZeroMeanGaussian(D=D)

    return target, momentum
Ejemplo n.º 5
0
def test_isotropic_zero_mean_equals_log_gaussian_pdf():
    D = 2
    x = np.random.randn(D)
    g = IsotropicZeroMeanGaussian(sigma=np.sqrt(2))
    log_pdf = log_gaussian_pdf(x, mu=np.zeros(D), Sigma=np.eye(D) * 2, is_cholesky=False, compute_grad=False)
    assert_close(log_pdf, g.log_pdf(x))
Ejemplo n.º 6
0
    """
    This example samples from the marginal posterior over hyper-parameters of a
    Gaussian Process classification model.
    
    All samplers in the paper are used.
    
    Note this is an illustrative demo and the number of iterations are set very low.
    """
    
    # Glass posterior has 9 dimensions
    D = 9
    if glass_available:
        target = GlassPosterior()
        target.set_up()
    else:
        target = IsotropicZeroMeanGaussian(D=D)

    # transition kernel, pick any
    samplers = [
                get_am_instance(target),
                get_mh_instance(target),
                get_kam_instance(target),
                get_kmc_instance(target)
               ]
    
    for sampler in samplers:
        
        # MCMC parameters
        # small number of iterations here to keep runtime short, feel free to increase
        start = np.zeros(D)
        num_iter = 50
Ejemplo n.º 7
0
    # for D=2, the fitted log-density is plotted, otherwise trajectory only
    D = 2
    N = 200
    seed = int(sys.argv[1])
    np.random.seed(seed)
    # target is banana density, fallback to Gaussian if theano is not present
    if banana_available:
        b = 0.03
        V = 100
        target = Banana(bananicity=b, V=V)
        X = sample_banana(5000, D, bananicity=b, V=V)
        ind = np.random.permutation(range(X.shape[0]))[:N]
        X = X[ind]
        print 'sampling from banana distribution...', X.shape
    else:
        target = IsotropicZeroMeanGaussian(D=D)
        X = sample_gaussian(N=N)
        print 'sampling from gaussian distribution'

    # compute sigma
    sigma = np.median(squareform(pdist(X))**2) / np.log(N + 1.0) * 2
    M = 200
    if N < M:
        start_samples = np.tile(
            X, [int(M / N) + 1, 1])[:M] + np.random.randn(M, 2) * 2
    else:
        start_samples = X[:M] + np.random.randn(M, 2) * 2
    #start_samples[:, 0] *= 4; start_samples[:, 1] *= 2

    # plot trajectories for both KMC lite and finite, parameters are chosen for D=2
    results = []
Ejemplo n.º 8
0
    It uses a fixed instance of KMC that receives a number of oracle samples as
    input.
    
    Note this is an illustrative demo and the number of iterations are set very low.
    """
    # possible to change
    # for D=2, the fitted log-density is plotted, otherwise trajectory only
    D = 2
    N = 1000

    # target is banana density, fallback to Gaussian if theano is not present
    if banana_available:
        target = Banana(D=D)
        X = sample_banana(N, D)
    else:
        target = IsotropicZeroMeanGaussian(D=D)
        X = sample_gaussian(N=N)

    # plot trajectories for both KMC lite and finite, parameters are chosen for D=2
    for surrogate in [
            KernelExpFiniteGaussian(sigma=2, lmbda=0.001, m=N, D=D),
            KernelExpLiteGaussian(sigma=20., lmbda=0.001, D=D, N=N),
            KernelExpLiteGaussianLowRank(sigma=20,
                                         lmbda=0.1,
                                         D=D,
                                         N=N,
                                         cg_tol=0.01),
    ]:
        # try uncommenting this line to illustrate KMC's ability to mix even
        # when no (or incomplete) samples from the target are available
        surrogate.fit(X)
Ejemplo n.º 9
0
if __name__ == '__main__':
    """
    This example samples from the Banana target (if theano is installed).
    It uses here uses an adaptive instance of KMC lite to start with, then (optionally) 
    switches to KMC finite using the KMC exploration as initial sketch of the target
    
    Note this is an illustrative demo and the number of iterations are set very low.
    """
    D = 2
    N = 500

    # target is banana density, with fallback option
    if banana_available:
        target = Banana(D=D)
    else:
        target = IsotropicZeroMeanGaussian(D=D)

    # KMC lite is geometrically ergodic on this target, use it if nothing about target is known
    # KMC finite can be used after burn-in, i.e. if some oracle samples are available
    # see below
    # this surrogate automatically learns parameters in every fit call
    surrogate = KernelExpLiteGaussianAdaptive(sigma=20., lmbda=0.001, D=D, N=N)

    # HMC parameters, step size will be adapted
    momentum = IsotropicZeroMeanGaussian(D=D)
    num_steps_min = 10
    num_steps_max = 50
    step_size_min = .1
    step_size_max = .1

    # kmc sampler instance, schedule here also controls updating the surrogate