def test_update_covariance(self): """Check proposal covariance is updated.""" constraints = hs.MockedConstraints() distribution = pr.ConstrainedDistribution(constraints) x = distribution.get_example() cov = np.identity(2) sampler = sr.MetroSampler(distribution, x, cov, 200, 0, 0) samples, _, _ = sampler.sample(200, 1) samples = np.append(samples[:-1], x.reshape(1, len(x)), 0) x_mean = np.mean(samples, 0) x2_sum = np.zeros(cov.shape) for val in samples: x2_sum += np.outer(val, val) nlen = samples.shape[0] fact = float(nlen - 1.0) x_covariance = (x2_sum - float(nlen) * np.outer(x_mean, x_mean)) / fact sd = sampler.sd eps = sampler.eps delta = sd * eps * np.identity(x_covariance.shape[0]) covariance = sd * x_covariance + delta assert (abs(covariance[0, 0] - sampler.covariance[0, 0])) <= 1.0e-5 assert (abs(covariance[1, 0] - sampler.covariance[1, 0])) <= 1.0e-5 assert (abs(covariance[1, 1] - sampler.covariance[1, 1])) <= 1.0e-5
def test_get_example(self): """Test get_example method.""" constraints = hp.MockedConstraints() distribution = sp.ConstrainedDistribution(constraints) x = distribution.get_example() assert x[0] == .2 assert x[1] == .5
def test_prob_invalid_x(self): """Test prob method with a state of incompatible size.""" constraints = hp.MockedConstraints() distribution = sp.ConstrainedDistribution(constraints) x = np.array([.1, .1, .1]) with pytest.raises(ValueError): distribution.prob(x)
def test_prob_valid_x_size_feasible(self): """Test prob method with a feasible state.""" constraints = hp.MockedConstraints() distribution = sp.ConstrainedDistribution(constraints) x = np.array([.2, .9]) valid = distribution.prob(x) assert abs(valid - 1.0) <= 1.0e-5
def test_running_mean(self): """Check correctness of the running mean computation.""" constraints = hs.MockedConstraints() distribution = pr.ConstrainedDistribution(constraints) x = distribution.get_example() cov = np.identity(2) sampler = sr.MetroSampler(distribution, x, cov, 0, 0, 0) samples, _, _ = sampler.sample(200, 1) samples = np.append(samples[:-1], x.reshape(1, len(x)), 0) x_mean = np.mean(samples, 0) assert abs(x_mean[0] - sampler.x_mean[0]) <= 1.0e-5 assert abs(x_mean[1] - sampler.x_mean[1]) <= 1.0e-5
def generate_samples(): # Create distribution and initial covariance matrix for proposal constraints = sc.Constraint('./Data/mixture.txt') posterior = sp.ConstrainedDistribution(constraints) covariance = np.identity(2) # Sampler's parameters t0 = 1000 tb = 50000 # Generate samples x0 = posterior.get_example() sampler = sr.MetroSampler(posterior, x0, covariance, 200, t0, tb, 0.1) vals, accepted, total = sampler.sample(4000, 200) # Generate scatter plot plt.scatter(vals[:, 0], vals[:, 1], s = 0.2) plt.show()
def test_running_covariance(self): """Check correctness of the running covariance computation.""" constraints = hs.MockedConstraints() distribution = pr.ConstrainedDistribution(constraints) x = distribution.get_example() cov = np.identity(2) sampler = sr.MetroSampler(distribution, x, cov, 0, 0, 0) samples, _, _ = sampler.sample(200, 1) samples = np.append(samples[:-1], x.reshape(1, len(x)), 0) x_mean = np.mean(samples, 0) x2_sum = np.zeros(cov.shape) for val in samples: x2_sum += np.outer(val, val) nlen = samples.shape[0] fact = float(nlen - 1.0) x_covariance = (x2_sum - float(nlen) * np.outer(x_mean, x_mean)) / fact assert abs(x_covariance[0, 0] - sampler.x_covariance[0, 0]) <= 1.0e-5 assert abs(x_covariance[1, 1] - sampler.x_covariance[1, 1]) <= 1.0e-5 assert abs(x_covariance[0, 1] - sampler.x_covariance[0, 1]) <= 1.0e-5
def gendist(): # Arguments descriptions descA = 'Generate samples from high-dimensional uniform distribution' descI = 'path to file with constraints specification' descO = 'path to file where sample will be saved' descS = 'number of samples to generate' # Initialize parser and parse arguments parser = argparse.ArgumentParser(description=descA) parser.add_argument('inpfile', help=descI) parser.add_argument('outfile', help=descO) parser.add_argument('samples', help=descS, type=int) args = parser.parse_args() # Check that constraints file exists input_file = args.inpfile if not os.path.isfile(input_file): print 'Error: the specified input file does not exist !!!' exit(0) # Check that output file can be written try: with open(args.outfile, 'w') as f: pass except IOError as err: print 'Cannot write to output file: ', err.errno, ',', err.strerror exit(0) # Create constrained distribution constraints = cons.Constraint(input_file) posterior = post.ConstrainedDistribution(constraints) # Initial state of Markov chain and initial covariance matrix x0 = posterior.get_example() cov0 = np.identity(x0.shape[0]) # Initialize sampler parameters t0 = 1000 tb = 10000 ratio = .0 gamma = .1 steps = 0 # Tune the sampler step size print "Adjusting the step size...\n" while steps < 8 and (ratio < .20 or ratio > .35): sampler = samp.MetroSampler(posterior, x0, cov0, 200, t0, tb, gamma) vals, accepted, total = sampler.sample(2000, 10) print 'With gamma = %f, the number of accepted and total ' \ 'samples is %d %d' % (gamma, accepted, total) ratio = float(accepted) / float(total) gamma = adjust_gamma(ratio, gamma) steps += 1 # Start sampling print '\nThe optimal step size is gamma = %f' % gamma, '. Start sampling...' sampler = samp.MetroSampler(posterior, x0, cov0, 200, t0, tb * 5, gamma) vals, accepted, total = sampler.sample(args.samples, 200) # Store samples to file print 'Sampling completed. The number of accepted and total ' \ 'samples is: %d %d' % (accepted, total), '. Storing data to file..' np.savetxt(args.outfile, vals, delimiter=' ', fmt='%1.4e')