def main(args_dict): # Extract configuration MK = args_dict['MK'] # Construct Robust Bayesian regression model sigma = 2 * np.ones(1) bounder = robustbayesregr.Bounder() splitter = robustbayesregr.Splitter() proposal = robustbayesregr.IsotropicGaussian(1, sigma) np.random.seed(0) x, y = robustbayesregr.generate_data(1000) target = robustbayesregr.CauchyRegression(x, y, sigma) # Obtain MK samples (and their corresponding MAP values) using A* sampling implementation samples = np.empty((MK)).squeeze() MAPs = [] for i in range(MK): stream = astar.astar_sampling_iterator(target, proposal, bounder, splitter) X, G = stream.next() samples[i] = X MAPs.append(G[0] - EULER) if i % 1 == 0: print_progress('Sampled %d / %d' % (i + 1, MK)) print('') lnZ = float(np.log(target.z())) # Dump true ln(Z) and MAP values to JSON file data = {'lnZ': lnZ, 'MAPs': MAPs} savepath = 'data/astar_rbr_MK%d.json' % (MK) json_dump(data, savepath, indent=None) print('Saved %d samples to %s' % (len(MAPs), savepath))
def main(): import time # bounder bounder = Bounder() # splitter splitter = Splitter() # proposal proposal = Uniform() # target n = 10 w = np.triu(np.random.rand(n, n) * 0.2, 1) f = np.random.rand(n) * 2 - 1 target = CliqueIsingModel(w, f) # ======== RUN =============== M = 1 N = 10 print("\nM samples per run:{0}, N runs:{1}".format(M, N)) start = time.time() for i in range(N): if i % 100 == 0: print i stream = osstar.osstar_sampling_iterator(target, proposal, bounder, splitter) for j in range(M): X, G = stream.next() end = time.time() print("\nOS*") print("splits/run: {0}".format(splitter.counter / float(N))) print("likelihoods/sample: {0}".format(proposal.counter / float(M * N))) print("bounds/sample: {0}".format(bounder.counter / float(M * N))) print("time taken: {0}s".format(end - start)) splitter.counter = 0 proposal.counter = 0 bounder.counter = 0 samples = [] start = time.time() for i in range(N): if i % 100 == 0: print i stream = astar.astar_sampling_iterator(target, proposal, bounder, splitter) for j in range(M): X, G = stream.next() samples.append(X) end = time.time() print("\nA*") print("splits/run: {0}".format(splitter.counter / float(N))) print("likelihoods/sample: {0}".format(proposal.counter / float(M * N))) print("bounds/sample: {0}".format(bounder.counter / float(M * N))) print("time taken: {0}s".format(end - start))
def main(): import time # bounder bounder = Bounder() # splitter splitter = Splitter() # proposal proposal = Uniform() # target n = 10 w = np.triu(np.random.rand(n,n)*0.2, 1) f = np.random.rand(n)*2 - 1 target = CliqueIsingModel(w, f) # ======== RUN =============== M = 1 N = 10 print("\nM samples per run:{0}, N runs:{1}".format(M, N)) start = time.time() for i in range(N): if i % 100 == 0: print i stream = osstar.osstar_sampling_iterator(target, proposal, bounder, splitter) for j in range(M): X, G = stream.next() end = time.time() print("\nOS*") print("splits/run: {0}".format(splitter.counter/float(N))) print("likelihoods/sample: {0}".format(proposal.counter/float(M*N))) print("bounds/sample: {0}".format(bounder.counter/float(M*N))) print("time taken: {0}s".format(end-start)) splitter.counter = 0 proposal.counter = 0 bounder.counter = 0 samples = [] start = time.time() for i in range(N): if i % 100 == 0: print i stream = astar.astar_sampling_iterator(target, proposal, bounder, splitter) for j in range(M): X, G = stream.next() samples.append(X) end = time.time() print("\nA*") print("splits/run: {0}".format(splitter.counter/float(N))) print("likelihoods/sample: {0}".format(proposal.counter/float(M*N))) print("bounds/sample: {0}".format(bounder.counter/float(M*N))) print("time taken: {0}s".format(end-start))
def main(): x = np.array([1, 1, -1, 0.25, 0.25]) target = SortedSoftmax(x, 1) proposal = Uniform() M = 50000 samples, gumbels = np.zeros(M), np.zeros(M) stream = astar.astar_sampling_iterator(target, proposal, bounder, splitter) for j in range(M): X, G = stream.next() samples[j] = X gumbels[j] = G # post process untruncated_gumbels = astar.untruncate(gumbels[1:], gumbels[:-1]) gumbels = np.concatenate(([gumbels[0]], untruncated_gumbels)) empirical_distribution = np.zeros((M, len(x))) empirical_distribution[np.arange(M), samples.astype(int)] = 1 print("true distribution:{0}".format(np.exp(target.x - target.logz))) print("empirical distribution:{0}".format(np.mean(empirical_distribution, axis=0))) print("logz:{0}, hat(logz):{1}".format(target.logz, gumbels.mean() - np.euler_gamma))
def main(): x = np.array([1, 1, -1, 0.25, 0.25]) target = SortedSoftmax(x, 1) proposal = Uniform() M = 50000 samples, gumbels = np.zeros(M), np.zeros(M) stream = astar.astar_sampling_iterator(target, proposal, bounder, splitter) for j in range(M): X, G = stream.next() samples[j] = X gumbels[j] = G # post process untruncated_gumbels = astar.untruncate(gumbels[1:], gumbels[:-1]) gumbels = np.concatenate(([gumbels[0]], untruncated_gumbels)) empirical_distribution = np.zeros((M, len(x))) empirical_distribution[np.arange(M), samples.astype(int)] = 1 print("true distribution:{0}".format(np.exp(target.x - target.logz))) print("empirical distribution:{0}".format( np.mean(empirical_distribution, axis=0))) print("logz:{0}, hat(logz):{1}".format(target.logz, gumbels.mean() - np.euler_gamma))
def main(): import time sigma = 2 * np.ones(1) # bounder bounder = Bounder() # splitter splitter = Splitter() # proposal proposal = IsotropicGaussian(1, sigma) # target x, y = generate_data(1000) target = CauchyRegression(x, y, sigma) # ======== RUN =============== M = 1 N = 1000 print("\nM samples per run:{0}, N runs:{1}".format(M, N)) start = time.time() for i in range(N): if i % 100 == 0: print i stream = osstar.osstar_sampling_iterator(target, proposal, bounder, splitter) for j in range(M): X, G = stream.next() end = time.time() print("\nOS*") print("splits/run: {0}".format(splitter.counter / float(N))) print("likelihoods/sample: {0}".format(proposal.counter / float(M * N))) print("bounds/sample: {0}".format(bounder.counter / float(M * N))) print("time taken: {0}s".format(end - start)) splitter.counter = 0 proposal.counter = 0 bounder.counter = 0 samples = np.empty((N * M)).squeeze() start = time.time() for i in range(N): if i % 100 == 0: print i stream = astar.astar_sampling_iterator(target, proposal, bounder, splitter) for j in range(M): X, G = stream.next() samples[i * M + j] = X end = time.time() print("\nA*") print("splits/run: {0}".format(splitter.counter / float(N))) print("likelihoods/sample: {0}".format(proposal.counter / float(M * N))) print("bounds/sample: {0}".format(bounder.counter / float(M * N))) print("time taken: {0}s".format(end - start)) samples = samples.ravel() plt.hist(samples, bins=25, normed=True) p = np.vectorize(lambda x: np.exp(target.log_density(x))) x = np.linspace(-5, 5, 1000) y = p(x) plt.plot(x, y, "r", linewidth=4) plt.draw() plt.show()
def main(): import time target = Sin(1) proposal = Uniform() bounder = Bounder() splitter = Splitter() M = 1 N = 5000 rej_samples = [] print("\nM samples per run:{0}, N runs:{1}".format(M, N)) start = time.time() for i in range(N): stream = osstar.rejection_sampling_iterator(target, proposal, bounder) for j in range(M): X, G = stream.next() rej_samples.append(X) end = time.time() rej_samples = np.array(rej_samples).ravel() print("\nRejection Sampling") print("likelihoods/sample: {0}".format(proposal.counter / float(M * N))) print("bounds/sample: {0}".format(bounder.counter / float(M * N))) print("time taken: {0}s".format(end - start)) splitter.counter = 0 proposal.counter = 0 bounder.counter = 0 osstar_samples = [] start = time.time() for i in range(N): stream = osstar.osstar_sampling_iterator(target, proposal, bounder, splitter) for j in range(M): X, G = stream.next() osstar_samples.append(X) end = time.time() osstar_samples = np.array(osstar_samples).ravel() print("\nOS*") print("splits/run: {0}".format(splitter.counter / float(N))) print("likelihoods/sample: {0}".format(proposal.counter / float(M * N))) print("bounds/sample: {0}".format(bounder.counter / float(M * N))) print("time taken: {0}s".format(end - start)) splitter.counter = 0 proposal.counter = 0 bounder.counter = 0 astar_iter_samples = [] start = time.time() for i in range(N): stream = astar.astar_sampling_iterator(target, proposal, bounder, splitter) for j in range(M): X, G = stream.next() astar_iter_samples.append(X) end = time.time() astar_iter_samples = np.array(astar_iter_samples).ravel() print("\nA*") print("splits/run: {0}".format(splitter.counter / float(N))) print("likelihoods/sample: {0}".format(proposal.counter / float(M * N))) print("bounds/sample: {0}".format(bounder.counter / float(M * N))) print("time taken: {0}s".format(end - start)) plt.subplot(221) plt.hist(rej_samples, bins=25, normed=True) plt.title("Rejection") p = np.vectorize(lambda x: np.exp(target.log_density(x))) x = np.linspace(0, 2 * pi, 1000) y = p(x) plt.plot(x, y, "r", linewidth=4) plt.subplot(222) plt.hist(osstar_samples, bins=25, normed=True) plt.title("OS*") p = np.vectorize(lambda x: np.exp(target.log_density(x))) x = np.linspace(0, 2 * pi, 1000) y = p(x) plt.plot(x, y, "r", linewidth=4) plt.subplot(223) plt.hist(astar_iter_samples, bins=25, normed=True) plt.title("A*") p = np.vectorize(lambda x: np.exp(target.log_density(x))) x = np.linspace(0, 2 * pi, 1000) y = p(x) plt.plot(x, y, "r", linewidth=4) plt.draw() plt.show()
def main(): import time # bounder bounder = Bounder() # splitter splitter = Splitter() dim = 1 sigma = 2 * np.ones(dim) # proposal proposal = IsotropicGaussian(dim, sigma) # target pi = 0.5 num_points = 3 points = np.concatenate( (np.linspace(-5, -3, num_points), np.linspace(2, 4, num_points))) data = np.zeros((len(points), dim)) for d in range(dim): data[:, d] = points target = ClutterPosterior(sigma, pi, data) # go time! M = 1 N = 500 print("\nM samples per run:{0}, N runs:{1}".format(M, N)) start = time.time() for i in range(N): stream = osstar.osstar_sampling_iterator(target, proposal, bounder, splitter) for j in range(M): X, G = stream.next() end = time.time() print("\nOS*") print("splits/run: {0}".format(splitter.counter / float(N))) print("likelihoods/sample: {0}".format(proposal.counter / float(M * N))) print("bounds/sample: {0}".format(bounder.counter / float(M * N))) print("time taken: {0}s".format(end - start)) splitter.counter = 0 proposal.counter = 0 bounder.counter = 0 samples = np.empty((N * M, dim)).squeeze() start = time.time() for i in range(N): stream = astar.astar_sampling_iterator(target, proposal, bounder, splitter) for j in range(M): X, G = stream.next() samples[i * M + j] = X end = time.time() print("\nA*") print("splits/run: {0}".format(splitter.counter / float(N))) print("likelihoods/sample: {0}".format(proposal.counter / float(M * N))) print("bounds/sample: {0}".format(bounder.counter / float(M * N))) print("time taken: {0}s".format(end - start)) if dim == 1: samples = samples.ravel() plt.hist(samples, bins=25, normed=True) p = np.vectorize(lambda x: np.exp(target.log_density(x))) x = np.linspace(-5, 5, 1000) y = p(x) plt.plot(x, y, "r", linewidth=4) plt.draw() plt.show() if dim == 2: plt.plot(samples[:, 0], samples[:, 1], '.') plt.draw() plt.show()
def main(): import time sigma = 2 * np.ones(1) # bounder bounder = Bounder() # splitter splitter = Splitter() # proposal proposal = IsotropicGaussian(1, sigma) # target x, y = generate_data(1000) target = CauchyRegression(x, y, sigma) # ======== RUN =============== M = 1 N = 1000 print("\nM samples per run:{0}, N runs:{1}".format(M, N)) start = time.time() for i in range(N): if i % 100 == 0: print i stream = osstar.osstar_sampling_iterator(target, proposal, bounder, splitter) for j in range(M): X, G = stream.next() end = time.time() print("\nOS*") print("splits/run: {0}".format(splitter.counter/float(N))) print("likelihoods/sample: {0}".format(proposal.counter/float(M*N))) print("bounds/sample: {0}".format(bounder.counter/float(M*N))) print("time taken: {0}s".format(end-start)) splitter.counter = 0 proposal.counter = 0 bounder.counter = 0 samples = np.empty((N*M)).squeeze() start = time.time() for i in range(N): if i % 100 == 0: print i stream = astar.astar_sampling_iterator(target, proposal, bounder, splitter) for j in range(M): X, G = stream.next() samples[i*M + j] = X end = time.time() print("\nA*") print("splits/run: {0}".format(splitter.counter/float(N))) print("likelihoods/sample: {0}".format(proposal.counter/float(M*N))) print("bounds/sample: {0}".format(bounder.counter/float(M*N))) print("time taken: {0}s".format(end-start)) samples = samples.ravel() plt.hist(samples, bins=25, normed=True) p = np.vectorize(lambda x: np.exp(target.log_density(x))) x = np.linspace(-5, 5, 1000) y = p(x) plt.plot(x,y, "r", linewidth=4) plt.draw() plt.show()
def main(): import time # bounder bounder = Bounder() # splitter splitter = Splitter() dim = 1 sigma = 2 * np.ones(dim) # proposal proposal = IsotropicGaussian(dim, sigma) # target pi = 0.5 num_points = 3 points = np.concatenate((np.linspace(-5, -3, num_points), np.linspace(2, 4, num_points))) data = np.zeros((len(points), dim)) for d in range(dim): data[:,d] = points target = ClutterPosterior(sigma, pi, data) # go time! M = 1 N = 500 print("\nM samples per run:{0}, N runs:{1}".format(M, N)) start = time.time() for i in range(N): stream = osstar.osstar_sampling_iterator(target, proposal, bounder, splitter) for j in range(M): X, G = stream.next() end = time.time() print("\nOS*") print("splits/run: {0}".format(splitter.counter/float(N))) print("likelihoods/sample: {0}".format(proposal.counter/float(M*N))) print("bounds/sample: {0}".format(bounder.counter/float(M*N))) print("time taken: {0}s".format(end-start)) splitter.counter = 0 proposal.counter = 0 bounder.counter = 0 samples = np.empty((N*M, dim)).squeeze() start = time.time() for i in range(N): stream = astar.astar_sampling_iterator(target, proposal, bounder, splitter) for j in range(M): X, G = stream.next() samples[i*M + j] = X end = time.time() print("\nA*") print("splits/run: {0}".format(splitter.counter/float(N))) print("likelihoods/sample: {0}".format(proposal.counter/float(M*N))) print("bounds/sample: {0}".format(bounder.counter/float(M*N))) print("time taken: {0}s".format(end-start)) if dim == 1: samples = samples.ravel() plt.hist(samples, bins=25, normed=True) p = np.vectorize(lambda x: np.exp(target.log_density(x))) x = np.linspace(-5, 5, 1000) y = p(x) plt.plot(x,y, "r", linewidth=4) plt.draw() plt.show() if dim == 2: plt.plot(samples[:,0], samples[:,1], '.') plt.draw() plt.show()
def main(): import time target = Sin(1) proposal = Uniform() bounder = Bounder() splitter = Splitter() M = 1 N = 5000 rej_samples = [] print("\nM samples per run:{0}, N runs:{1}".format(M, N)) start = time.time() for i in range(N): stream = osstar.rejection_sampling_iterator(target, proposal, bounder) for j in range(M): X, G = stream.next() rej_samples.append(X) end = time.time() rej_samples = np.array(rej_samples).ravel() print("\nRejection Sampling") print("likelihoods/sample: {0}".format(proposal.counter/float(M*N))) print("bounds/sample: {0}".format(bounder.counter/float(M*N))) print("time taken: {0}s".format(end-start)) splitter.counter = 0 proposal.counter = 0 bounder.counter = 0 osstar_samples = [] start = time.time() for i in range(N): stream = osstar.osstar_sampling_iterator(target, proposal, bounder, splitter) for j in range(M): X, G = stream.next() osstar_samples.append(X) end = time.time() osstar_samples = np.array(osstar_samples).ravel() print("\nOS*") print("splits/run: {0}".format(splitter.counter/float(N))) print("likelihoods/sample: {0}".format(proposal.counter/float(M*N))) print("bounds/sample: {0}".format(bounder.counter/float(M*N))) print("time taken: {0}s".format(end-start)) splitter.counter = 0 proposal.counter = 0 bounder.counter = 0 astar_iter_samples = [] start = time.time() for i in range(N): stream = astar.astar_sampling_iterator(target, proposal, bounder, splitter) for j in range(M): X, G = stream.next() astar_iter_samples.append(X) end = time.time() astar_iter_samples = np.array(astar_iter_samples).ravel() print("\nA*") print("splits/run: {0}".format(splitter.counter/float(N))) print("likelihoods/sample: {0}".format(proposal.counter/float(M*N))) print("bounds/sample: {0}".format(bounder.counter/float(M*N))) print("time taken: {0}s".format(end-start)) plt.subplot(221) plt.hist(rej_samples, bins=25, normed=True) plt.title("Rejection") p = np.vectorize(lambda x: np.exp(target.log_density(x))) x = np.linspace(0, 2*pi, 1000) y = p(x) plt.plot(x,y, "r", linewidth=4) plt.subplot(222) plt.hist(osstar_samples, bins=25, normed=True) plt.title("OS*") p = np.vectorize(lambda x: np.exp(target.log_density(x))) x = np.linspace(0, 2*pi, 1000) y = p(x) plt.plot(x,y, "r", linewidth=4) plt.subplot(223) plt.hist(astar_iter_samples, bins=25, normed=True) plt.title("A*") p = np.vectorize(lambda x: np.exp(target.log_density(x))) x = np.linspace(0, 2*pi, 1000) y = p(x) plt.plot(x,y, "r", linewidth=4) plt.draw() plt.show()