def test_kernel_X_alone_dimension(self): k = HypercubeKernel(1.) n = 3 d = 2 X = zeros((n, d), dtype=numpy.bool8) K = k.kernel(X) self.assertEqual(K.shape, (n, n))
def test_kernel_X_Y_one_point_different(self): gamma = .2 k = HypercubeKernel(gamma) X = asarray([[1]], dtype=numpy.bool8) Y = asarray([[0]], dtype=numpy.bool8) K = k.kernel(X, Y) self.assertEqual(K[0, 0], tanh(gamma))
def test_kernel_X_Y_one_point_different(self): gamma = .2 k = HypercubeKernel(gamma) X = asarray([[1]], dtype=numpy.bool8) Y = asarray([[0]], dtype=numpy.bool8) K = k.kernel(X, Y) self.assertEqual(K[0, 0], tanh(gamma))
def test_kernel_X_alone_dimension(self): k = HypercubeKernel(1.) n = 3 d = 2 X = zeros((n, d), dtype=numpy.bool8) K = k.kernel(X) self.assertEqual(K.shape, (n, n))
def test_kernel_X_alone_type(self): k = HypercubeKernel(1.) n = 3 d = 2 X = zeros((n, d), dtype=numpy.bool8) K = k.kernel(X) self.assertEqual(type(K), numpy.ndarray)
def test_kernel_X_alone_type(self): k = HypercubeKernel(1.) n = 3 d = 2 X = zeros((n, d), dtype=numpy.bool8) K = k.kernel(X) self.assertEqual(type(K), numpy.ndarray)
def test_kernel_X_Y_dimension(self): k = HypercubeKernel(1.) n_X = 3 n_Y = 4 d = 2 X = zeros((n_X, d), dtype=numpy.bool8) Y = zeros((n_Y, d), dtype=numpy.bool8) K = k.kernel(X, Y) self.assertEqual(K.shape, (n_X, n_Y))
def test_kernel_X_Y_dimension(self): k = HypercubeKernel(1.) n_X = 3 n_Y = 4 d = 2 X = zeros((n_X, d), dtype=numpy.bool8) Y = zeros((n_Y, d), dtype=numpy.bool8) K = k.kernel(X, Y) self.assertEqual(K.shape, (n_X, n_Y))
def test_kernel_X_two_points_fixed(self): gamma = .2 k = HypercubeKernel(gamma) X = asarray([[1, 0], [1, 1]], dtype=numpy.bool8) K = zeros((2, 2)) for i in range(2): for j in range(2): dist = sum(X[i] != X[j]) K[i, j] = tanh(gamma)**dist self.assertAlmostEqual(norm(K - k.kernel(X)), 0)
def test_kernel_X_two_points_fixed(self): gamma = .2 k = HypercubeKernel(gamma) X = asarray([[1, 0], [1, 1]], dtype=numpy.bool8) K = zeros((2, 2)) for i in range(2): for j in range(2): dist = sum(X[i] != X[j]) K[i, j] = tanh(gamma) ** dist self.assertAlmostEqual(norm(K - k.kernel(X)), 0)
def test_kernel_X_many_points_random(self): gamma = .2 n_X = 4 d = 5 num_runs = 100 k = HypercubeKernel(gamma) for _ in range(num_runs): X = randint(0, 2, (n_X, d)).astype(numpy.bool8) K = zeros((n_X, n_X)) for i in range(n_X): for j in range(n_X): dist = sum(X[i] != X[j]) K[i, j] = tanh(gamma) ** dist self.assertAlmostEqual(norm(K - k.kernel(X)), 0)
def test_kernel_X_many_points_random(self): gamma = .2 n_X = 4 d = 5 num_runs = 100 k = HypercubeKernel(gamma) for _ in range(num_runs): X = randint(0, 2, (n_X, d)).astype(numpy.bool8) K = zeros((n_X, n_X)) for i in range(n_X): for j in range(n_X): dist = sum(X[i] != X[j]) K[i, j] = tanh(gamma)**dist self.assertAlmostEqual(norm(K - k.kernel(X)), 0)
def main(): d = 5 ps = rand(d) ps /= norm(ps) distribution = Bernoulli(ps) num_history = 100 Z = distribution.sample(num_history).samples threshold = 0.8 spread = 0.2 gamma = 0.2 kernel = HypercubeKernel(gamma) mcmc_sampler = DiscreteKameleon(distribution, kernel, Z, threshold, spread) start = zeros(distribution.dimension, dtype=numpy.bool8) mcmc_params = MCMCParams(start=start, num_iterations=1000) chain = MCMCChain(mcmc_sampler, mcmc_params) chain.append_mcmc_output(StatisticsOutput(plot_times=True)) chain.append_mcmc_output(DiscretePlottingOutput(plot_from=0, lag=100)) chain.run() print "ps", ps print "empirical", mean(chain.samples, 0)
def main(): d = 5 b = randn(d) V = randn(d, d) W = V + V.T fill_diagonal(W, zeros(d)) hopfield = Hopfield(W, b) current_state = [rand() < 0.5 for _ in range(d)] distribution = HopfieldFullConditionals(full_target=hopfield, current_state=current_state) print("Running Gibbs to produce chain history") Z = sample_gibbs(distribution, num_samples=2000)[1500:].astype(numpy.bool8) inds = permutation(len(Z)) Z = Z[inds[:500]] print("done") threshold = 0.8 spread = 0.2 gamma = 0.2 kernel = HypercubeKernel(gamma) mcmc_sampler = DiscreteKameleon(hopfield, kernel, Z, threshold, spread) start = zeros(distribution.dimension, dtype=numpy.bool8) mcmc_params = MCMCParams(start=start, num_iterations=10000) chain = MCMCChain(mcmc_sampler, mcmc_params) chain.append_mcmc_output(StatisticsOutput(plot_times=True)) chain.append_mcmc_output(DiscretePlottingOutput(plot_from=0, lag=500)) chain.run()
def test_chain_bernoulli(self): # runs the sampler on a distribution of infdependent bernoulli variables # and compares the mean d = 5 ps = rand(d) ps /= norm(ps) distribution = Bernoulli(ps) num_history = 100 Z = distribution.sample(num_history).samples threshold = 0.8 spread = 0.2 gamma = 0.2 kernel = HypercubeKernel(gamma) mcmc_sampler = DiscreteKameleon(distribution, kernel, Z, threshold, spread) start = zeros(distribution.dimension, dtype=numpy.bool8) mcmc_params = MCMCParams(start=start, num_iterations=1000) chain = MCMCChain(mcmc_sampler, mcmc_params) chain.run() self.assertAlmostEqual(norm(mean(chain.samples, 0) - ps), 0, delta=0.2)
def test_contructor_wrong_threshold_type(self): dimension = 3 ps = rand(dimension) distribution = Bernoulli(ps) kernel = HypercubeKernel(1.) Z = zeros((2, distribution.dimension)) spread = 0.5 self.assertRaises(TypeError, DiscreteKameleon, distribution, kernel, Z, None, spread)
def test_contructor_wrong_Z_array_dimension_too_large(self): dimension = 3 ps = rand(dimension) distribution = Bernoulli(ps) kernel = HypercubeKernel(1.) Z = zeros((2, distribution.dimension, 3)) threshold = 0.5 spread = 0.5 self.assertRaises(ValueError, DiscreteKameleon, distribution, kernel, Z, threshold, spread)
def run_kameleon_chain(Z, hopfield, start, num_iterations): threshold = 0.8 spread = 0.03 gamma = 0.2 kernel = HypercubeKernel(gamma) sampler = DiscreteKameleon(hopfield, kernel, Z, threshold, spread) params = MCMCParams(start=start, num_iterations=num_iterations) chain = MCMCChain(sampler, params) chain.run() return chain
def test_contructor(self): dimension = 3 ps = rand(dimension) distribution = Bernoulli(ps) kernel = HypercubeKernel(1.) Z = zeros((2, distribution.dimension)) threshold = 0.5 spread = 0.5 sampler = DiscreteKameleon(distribution, kernel, Z, threshold, spread) self.assertEqual(sampler.distribution, distribution) self.assertEqual(sampler.kernel, kernel) self.assertTrue(sampler.Z is Z) self.assertEqual(sampler.threshold, threshold) self.assertEqual(sampler.spread, spread)
def test_construct_proposal(self): dimension = 3 num_history = 4 ps = rand(dimension) distribution = Bernoulli(ps) kernel = HypercubeKernel(1.) Z = randint(0, 2, (num_history, dimension)).astype(numpy.bool8) threshold = 0.5 spread = 0.5 sampler = DiscreteKameleon(distribution, kernel, Z, threshold, spread) y = randint(0, 2, dimension).astype(dtype=numpy.bool8) p = sampler.construct_proposal(y) self.assertTrue(isinstance(p, DiscreteRandomWalkProposal)) self.assertEqual(p.spread, spread)
def test_adapt_does_nothing(self): dimension = 3 ps = rand(dimension) distribution = Bernoulli(ps) kernel = HypercubeKernel(1.) Z = zeros((2, distribution.dimension)) threshold = 0.5 spread = .5 sampler = DiscreteKameleon(distribution, kernel, Z, threshold, spread) # serialise, call adapt, load, compare f = NamedTemporaryFile() dump(sampler, f) f.seek(0) sampler_copy = load(f) f.close() sampler.adapt(None, None) # rough check for equality, dont do a proper one here self.assertEqual(type(sampler_copy.kernel), type(sampler.kernel)) self.assertEqual(sampler_copy.kernel.gamma, sampler.kernel.gamma) self.assertEqual(type(sampler_copy.distribution), type(sampler.distribution)) self.assertEqual(sampler_copy.distribution.dimension, sampler.distribution.dimension) self.assertEqual(type(sampler_copy.Z), type(sampler.Z)) self.assertEqual(sampler_copy.Z.shape, sampler.Z.shape) self.assertAlmostEqual(norm(sampler_copy.Z - sampler.Z), 0) self.assertEqual(sampler_copy.spread, sampler.spread) # this is none, so just compare self.assertEqual(sampler.Q, sampler_copy.Q)
def test_kernel_X_one_point_zero(self): gamma = .2 k = HypercubeKernel(gamma) X = asarray([[0]], dtype=numpy.bool8) K = k.kernel(X) self.assertEqual(K[0, 0], 1.)
def test_kernel_X_alone_dtype(self): gamma = .2 k = HypercubeKernel(gamma) X = asarray([[0]], dtype=numpy.bool8) K = k.kernel(X) self.assertEqual(K.dtype, numpy.float)
def test_kernel_X_one_point_zero(self): gamma = .2 k = HypercubeKernel(gamma) X = asarray([[0]], dtype=numpy.bool8) K = k.kernel(X) self.assertEqual(K[0, 0], 1.)
def main(): Z, hopfield = create_ground_truth() d = hopfield.dimension print("Number of ground truth samples: %d" % len(Z)) num_iterations = 200000 warm_up = 1000 thin = 100 start = randint(0, 2, d).astype(numpy.bool8) timestring = time.strftime("%Y-%m-%d_%H:%M:%S") print("Running SM for %d iterations" % num_iterations) sm_chain = run_sm_chain(hopfield, start, num_iterations) try: fname = "temp_sm_result_" + timestring + ".bin" f = open(fname, "w") dump(sm_chain, f) f.close() except IOError: print("Could not save this SM chain") print("Running Gibbs for %d iterations" % (num_iterations * d)) gibbs_chain = run_gibbs_chain(hopfield, start, num_iterations) try: fname = "temp_gibbs_result_" + timestring + ".bin" f = open(fname, "w") dump(gibbs_chain, f) f.close() except IOError: print("Could not save this Gibbs chain") print("Running Discrete Kameleon for %d iterations" % num_iterations) kameleon_chain = run_kameleon_chain(Z, hopfield, start, num_iterations) try: fname = "temp_kameleon_result_" + timestring + ".bin" f = open(fname, "w") dump(kameleon_chain, f) f.close() except IOError: print("Could not save this Kameleon chain") # remove warm up and thin print("Removing warm up and thinning") S_g = gibbs_chain.samples[warm_up:] S_g = S_g[arange(len(S_g), step=thin * d)].astype(numpy.bool8) S_k = kameleon_chain.samples[warm_up:] S_k = S_k[arange(len(S_k), step=thin)].astype(numpy.bool8) S_sm = sm_chain.samples[warm_up:] S_sm = S_sm[arange(len(S_sm), step=thin)].astype(numpy.bool8) print("Gibbs samples: %d" % len(S_g)) print("Kameleon samples: %d" % len(S_k)) print("SM samples: %d" % len(S_sm)) print("MMDs:") kernel = HypercubeKernel(0.2) num_evaluations = 10 inds_g = linspace(0, len(S_g), num_evaluations).astype(numpy.int) inds_k = linspace(0, len(S_k), num_evaluations).astype(numpy.int) inds_sm = linspace(0, len(S_sm), num_evaluations).astype(numpy.int) mmds = zeros((3, num_evaluations - 1)) for i in arange(num_evaluations - 1): mmds[0, i - 1] = sqrt(kernel.estimateMMD(S_g[:inds_g[i + 1]], Z)) mmds[1, i - 1] = sqrt(kernel.estimateMMD(S_k[:inds_k[i + 1]], Z)) mmds[2, i - 1] = sqrt(kernel.estimateMMD(S_sm[:inds_sm[i + 1]], Z)) print(mmds) plot(inds_g[1:], mmds[0, :]) plot(inds_k[1:], mmds[1, :]) plot(inds_sm[1:], mmds[2, :]) legend(["Gibbs", "Kameleon", "SM"]) show()
def test_kernel_wrong_Y_array_dimension2(self): k = HypercubeKernel(1.) X = zeros((2, 3)) Y = zeros((2, 3, 4)) self.assertRaises(ValueError, k.kernel, X, Y)
def test_kernel_wrong_Y_array_dtype1(self): k = HypercubeKernel(1.) X = zeros(2, dtype=numpy.bool8) Y = zeros((2, 2), dtype=numpy.float64) self.assertRaises(ValueError, k.kernel, X, Y)
def test_kernel_wrong_Y_type(self): k = HypercubeKernel(1.) X = zeros((2, 2)) Y = 3 self.assertRaises(ValueError, k.kernel, X, Y)
def test_kernel_wrong_X_type(self): k = HypercubeKernel(1.) X = None Y = zeros((2, 2)) self.assertRaises(TypeError, k.kernel, X, Y)
def test_contructor_gamma(self): gamma = 1.2 k = HypercubeKernel(gamma=gamma) self.assertEqual(gamma, k.gamma)
def test_kernel_X_alone_dtype(self): gamma = .2 k = HypercubeKernel(gamma) X = asarray([[0]], dtype=numpy.bool8) K = k.kernel(X) self.assertEqual(K.dtype, numpy.float)
def main(): Z, hopfield = create_ground_truth() d = hopfield.dimension print("Number of ground truth samples: %d" % len(Z)) num_iterations = 200000 warm_up = 1000 thin = 100 start = randint(0, 2, d).astype(numpy.bool8) timestring = time.strftime("%Y-%m-%d_%H:%M:%S") print("Running SM for %d iterations" % num_iterations) sm_chain = run_sm_chain(hopfield, start, num_iterations) try: fname = "temp_sm_result_" + timestring + ".bin" f = open(fname, "w") dump(sm_chain, f) f.close() except IOError: print("Could not save this SM chain") print("Running Gibbs for %d iterations" % (num_iterations * d)) gibbs_chain = run_gibbs_chain(hopfield, start, num_iterations) try: fname = "temp_gibbs_result_" + timestring + ".bin" f = open(fname, "w") dump(gibbs_chain, f) f.close() except IOError: print("Could not save this Gibbs chain") print("Running Discrete Kameleon for %d iterations" % num_iterations) kameleon_chain = run_kameleon_chain(Z, hopfield, start, num_iterations) try: fname = "temp_kameleon_result_" + timestring + ".bin" f = open(fname, "w") dump(kameleon_chain, f) f.close() except IOError: print("Could not save this Kameleon chain") # remove warm up and thin print("Removing warm up and thinning") S_g = gibbs_chain.samples[warm_up:] S_g = S_g[arange(len(S_g), step=thin * d)].astype(numpy.bool8) S_k = kameleon_chain.samples[warm_up:] S_k = S_k[arange(len(S_k), step=thin)].astype(numpy.bool8) S_sm = sm_chain.samples[warm_up:] S_sm = S_sm[arange(len(S_sm), step=thin)].astype(numpy.bool8) print("Gibbs samples: %d" % len(S_g)) print("Kameleon samples: %d" % len(S_k)) print("SM samples: %d" % len(S_sm)) print("MMDs:") kernel = HypercubeKernel(0.2) num_evaluations = 10 inds_g = linspace(0, len(S_g), num_evaluations).astype(numpy.int) inds_k = linspace(0, len(S_k), num_evaluations).astype(numpy.int) inds_sm = linspace(0, len(S_sm), num_evaluations).astype(numpy.int) mmds = zeros((3, num_evaluations - 1)) for i in arange(num_evaluations - 1): mmds[0, i - 1] = sqrt(kernel.estimateMMD(S_g[:inds_g[i + 1]], Z)) mmds[1, i - 1] = sqrt(kernel.estimateMMD(S_k[:inds_k[i + 1]], Z)) mmds[2, i - 1] = sqrt(kernel.estimateMMD(S_sm[:inds_sm[i + 1]], Z)) print(mmds) plot(inds_g[1:], mmds[0, :]) plot(inds_k[1:], mmds[1, :]) plot(inds_sm[1:], mmds[2, :]) legend(["Gibbs", "Kameleon", "SM"]) show()