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(): 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()