def get_OracleKernelAdaptiveLangevin_instance(D, target_log_pdf): step_size = 1. m = 500 N = 5000 Z = sample_banana(N, D, bananicity, V) surrogate = KernelExpFiniteGaussian(sigma=10, lmbda=.001, m=m, D=D) surrogate.fit(Z) if False: param_bounds = {'sigma': [-2, 3]} bo = BayesOptSearch(surrogate, Z, param_bounds) best_params = bo.optimize() surrogate.set_parameters_from_dict(best_params) if False: sigma = 1. / gamma_median_heuristic(Z) surrogate.set_parameters_from_dict({'sigma': sigma}) logger.info("kernel exp family uses %s" % surrogate.get_parameters()) if False: import matplotlib.pyplot as plt Xs = np.linspace(-30, 30, 50) Ys = np.linspace(-20, 40, 50) visualise_fit_2d(surrogate, Z, Xs, Ys) plt.show() instance = OracleKernelAdaptiveLangevin(D, target_log_pdf, surrogate, step_size) return instance
def get_OracleKameleon_instance(D, target_log_pdf): step_size = 30. schedule = one_over_sqrt_t_schedule acc_star = 0.234 gamma2 = 0.1 n = 500 Z = sample_banana(N=n, D=D, bananicity=0.03, V=100) kernel_sigma = 1. / gamma_median_heuristic(Z) instance = OracleKameleon(D, target_log_pdf, n, kernel_sigma, step_size, gamma2, schedule, acc_star) instance.set_batch(Z) return instance
def get_OracleKernelAdaptiveLangevin_instance(D, target_log_pdf, grad): step_size = 1. schedule = one_over_sqrt_t_schedule acc_star = 0.574 N = 500 m = 500 Z = sample_banana(N=N, D=D, bananicity=0.03, V=100) surrogate = KernelExpFiniteGaussian(sigma=10, lmbda=1., m=m, D=D) surrogate.fit(Z) instance = OracleKernelAdaptiveLangevin(D, target_log_pdf, surrogate, step_size, schedule, acc_star) return instance
V = 100 true_mean = np.zeros(D) true_var = np.ones(D) true_var[0] = 100 true_var[1] = 20 true_cov = np.diag(true_var) num_iter_per_particle = 200 population_sizes = [5, 10, 25, 50, 100] num_repetitions = 30 num_benchmark_samples = 1000 rng_state = np.random.get_state() np.random.seed(0) benchmark_sample = sample_banana(num_benchmark_samples, D, bananicity, V) np.random.set_state(rng_state) for _ in range(num_repetitions): for population_size in population_sizes: num_iter = population_size * num_iter_per_particle start = np.zeros(D) target_log_pdf = lambda x: log_banana_pdf(x, bananicity, V, compute_grad=False) target_grad = lambda x: log_banana_pdf(x, bananicity, V, compute_grad=True) samplers = [ get_StaticMetropolis_instance(D, target_log_pdf), get_AdaptiveMetropolis_instance(D, target_log_pdf), get_AdaptiveIndependentMetropolis_instance(D, target_log_pdf), get_StaticLangevin_instance(D, target_log_pdf, target_grad),
gamma2 = 0.1 instance = AdaptiveLangevin(D, target_log_pdf, grad, step_size, one_over_4th_root_t_schedule, acc_star) instance.do_preconditioning = prec instance.manual_gradient_step_size = 1 return instance if __name__ == '__main__': Log.set_loglevel(20) max_moment = 8 D = 10 pop_size = 10 bananicity = 0.03 V = 100 Z = sample_banana(3000, D, bananicity, V) moments = np.array([(Z**i).mean(0) for i in range(1, max_moment)]) pk = PolynomialKernel(3) print(moments) true_correct = np.linalg.inv(np.cov(Z.T)) target_log_pdf = lambda x: log_banana_pdf( x, bananicity, V, compute_grad=False) target_grad = lambda x: log_banana_pdf(x, bananicity, V, compute_grad=True) samplers = [ # ] sampler_is = get_StaticLangevin( D, target_log_pdf, target_grad) #get_AdaptiveLangevin(D, target_log_pdf, target_grad)
V = 100 true_mean = np.zeros(D) true_var = np.ones(D) true_var[0] = 100 true_var[1] = 20 true_cov = np.diag(true_var) num_iter_per_particle = 200 population_sizes = [5, 10, 25, 50, 100] num_repetitions = 30 num_benchmark_samples = 1000 rng_state = np.random.get_state() np.random.seed(0) benchmark_sample = sample_banana(num_benchmark_samples, D, bananicity, V) np.random.set_state(rng_state) for _ in range(num_repetitions): for population_size in population_sizes: num_iter = population_size * num_iter_per_particle start = np.zeros(D) target_log_pdf = lambda x: log_banana_pdf( x, bananicity, V, compute_grad=False) target_grad = lambda x: log_banana_pdf( x, bananicity, V, compute_grad=True) samplers = [ get_StaticMetropolis_instance(D, target_log_pdf), get_AdaptiveMetropolis_instance(D, target_log_pdf),
step_size = 5. acc_star = 0.234 gamma2 = 0.1 instance = AdaptiveMetropolis(D, target_log_pdf, step_size, gamma2, one_over_4th_root_t_schedule, acc_star) return instance if __name__ == '__main__': Log.set_loglevel(20) D = 2 pop_size=10 bananicity = 0.03 V = 100 Z = sample_banana(700, D, bananicity, V) moments = np.array([(Z**i).mean(0) for i in range(1,4)]) target_log_pdf = lambda x: log_banana_pdf(x, bananicity, V, compute_grad=False) target_grad = lambda x: log_banana_pdf(x, bananicity, V, compute_grad=True) samplers = [ # get_StaticMetropolis_instance(D, target_log_pdf), # get_AdaptiveMetropolis_instance(D, target_log_pdf), # get_OracleKameleon_instance(D, target_log_pdf), # get_Kameleon_instance(D, target_log_pdf), # get_StaticLangevin_instance(D, target_log_pdf, target_grad), get_AM_5(D, target_log_pdf), get_AM_1(D, target_log_pdf), get_AM_2(D, target_log_pdf), get_AM_05(D, target_log_pdf), ]
from old import KameleonRKSGaussian.KameleonRKSGaussian from kameleon_rks.densities.banana import sample_banana from kameleon_rks.gaussian_rks import sample_basis, gamma_median_heuristic import matplotlib.pyplot as plt import numpy as np np.random.seed(0) # fix RKS basis D = 2 m = 500 kernel_gamma = gamma_median_heuristic(sample_banana(N=1000, D=D)) kernel_gamma = 0.5 print "Using kernel_gamma=%.2f" % kernel_gamma omega, u = sample_basis(D, m, kernel_gamma) # storing all oracle samples fed to Kameleon Z_all = [] # Kameleon parameters eta2 = 300 gamma2 = 0.1 # Schudule for adaptation, toy problem: always update schedule = lambda t: 1. # sampler instance kameleon_rks = KameleonRKSGaussian(D, kernel_gamma, m, gamma2, eta2, schedule) kameleon_rks._initialise()
feature_map_single, feature_map_grad_single import matplotlib.pyplot as plt import numpy as np np.random.seed(0) # fix basis for now D = 2 m = 1000 gamma = .3 omega, u = sample_basis(D, m, gamma) # sample points in input space N = 2000 Z = sample_banana(N, D) # fit Gaussian in feature space Phi = feature_map(Z, omega, u) # mean and covariance, batch version mu = np.mean(Phi, 0) eta = 0.01 C = np.cov(Phi.T) + eta ** 2 * np.eye(m) L = np.linalg.cholesky(C) # step size eta = 50. plt.plot(Z[:, 0], Z[:, 1], 'bx')
acc_star = 0.574 gamma2 = 0.1 instance = AdaptiveLangevin(D, target_log_pdf, grad, step_size, one_over_4th_root_t_schedule, acc_star) instance.do_preconditioning = prec instance.manual_gradient_step_size = 1 return instance if __name__ == '__main__': Log.set_loglevel(20) max_moment = 8 D = 10 pop_size=10 bananicity = 0.03 V = 100 Z = sample_banana(3000, D, bananicity, V) moments = np.array([(Z**i).mean(0) for i in range(1, max_moment)]) pk = PolynomialKernel(3) print(moments) true_correct = np.linalg.inv(np.cov(Z.T)) target_log_pdf = lambda x: log_banana_pdf(x, bananicity, V, compute_grad=False) target_grad = lambda x: log_banana_pdf(x, bananicity, V, compute_grad=True) samplers = [ # ] sampler_is = get_StaticLangevin(D, target_log_pdf, target_grad)#get_AdaptiveLangevin(D, target_log_pdf, target_grad) sampler_mh = get_StaticLangevin(D, target_log_pdf, target_grad)#get_AdaptiveLangevin(D, target_log_pdf, target_grad, prec=True, step_size=1.) start = np.zeros(D)