def non_sphere_rejection(partitionZ): # generate heights and their corresponding probabilities until a height passes unrejected orientation = Quaternion([0.,0.,0.,0.]) new_location = np.array([0., 0., 0.]) while True: orientation.random_orientation() new_location[2] = np.random.uniform(A, max_height-A) acceptance_prob = non_sphere_GB(new_location, orientation) / partitionZ if acceptance_prob > 1: raise InvalidProbability('Acceptance Probability is greater than 1') if np.random.uniform(0., 1.) < acceptance_prob: # the rejection part of the algorithm. return [new_location, orientation]
def generate_non_sphere_partition(partition_steps): partitionZ = 0. #for i in range(100): # new_location = [0., 0., np.random.uniform(A, max_height)] # partitionZ += non_sphere_GB(location,new_location) orientation = Quaternion([0,0,0,0]) new_location = np.array([0., 0., 0.]) for i in range(partition_steps): orientation.random_orientation() new_location[2] = np.random.uniform(A, max_height) sample = non_sphere_GB(new_location, orientation) if sample > partitionZ: partitionZ = sample return partitionZ
def generate_non_sphere_partition(partition_steps): partitionZ = 0. #for i in range(100): # new_location = [0., 0., np.random.uniform(A, max_height)] # partitionZ += non_sphere_GB(location,new_location) orientation = Quaternion([0, 0, 0, 0]) new_location = np.array([0., 0., 0.]) for i in range(partition_steps): orientation.random_orientation() new_location[2] = np.random.uniform(A, max_height) sample = non_sphere_GB(new_location, orientation) if sample > partitionZ: partitionZ = sample return partitionZ
def non_sphere_rejection(partitionZ): # generate heights and their corresponding probabilities until a height passes unrejected orientation = Quaternion([0., 0., 0., 0.]) new_location = np.array([0., 0., 0.]) while True: orientation.random_orientation() new_location[2] = np.random.uniform(A, max_height - A) acceptance_prob = non_sphere_GB(new_location, orientation) / partitionZ if acceptance_prob > 1: raise InvalidProbability( 'Acceptance Probability is greater than 1') if np.random.uniform( 0., 1.) < acceptance_prob: # the rejection part of the algorithm. return [new_location, orientation]
def analytical_distribution_non_sphere(num_points): # heights are sampled evenly from the chosen bounds, using linspace # because linspace includes starting value A, the first index in x is ignored # if x[0] is included, then in the calculation of potential energy U, h-A = 0 # and an exception will be thrown # x = np.linspace(A, max_height, num_points) # orientations = [] # for i in range(num_points): # theta = np.random.normal(0., 1., 4) # orientations.append(Quaternion(theta/np.linalg.norm(theta))) # y = [] # deltaX = x[1] - x[0] # numerator, denominator = 0., 0. # # # add the bounds to the integral value # # ignore x[0] = A # integral = 0.5*(non_sphere_GB([0., 0., x[1]], orientations[0]) + # non_sphere_GB([0., 0., max_height], orientations[num_points-1])) # # iterate over the rest of the heights # for k in range(2, num_points): # integral += non_sphere_GB([0., 0., x[k]], orientations[k]) # # multiply by the change in x to complete the integral calculation # integral *= deltaX # # # now that we have the partition function that the integral represents # # we can calculate all the y positions of the distribution # # again ignore x[0] = A # j = 0 # for h in x[1:]: # numerator = non_sphere_GB([0., 0., h], orientations[j]) # y.append(numerator/integral) # j+=1 x = np.linspace(A, max_height, num_points) y = np.zeros(num_points-1, dtype = float) num_angles = 1000 deltaX = x[1] - x[0] integral = .0 firstBar, lastBar = 0., 0. orientation = Quaternion([0,0,0,0]) # create a quaternion object for i in range(num_angles): orientation.random_orientation() firstBar += non_sphere_GB([0, 0, x[1]], orientation) firstBar /= num_angles for i in range(num_angles): orientation.random_orientation() lastBar += non_sphere_GB([0, 0, x[num_points-1]], orientation) lastBar /= num_angles integral += (firstBar + lastBar) *.5 sample_GB = np.zeros(num_angles, dtype = float) for i in range(2, num_points-1): for j in range(num_angles): orientation.random_orientation() sample_GB[j] = non_sphere_GB(np.array([0, 0, x[i]]), orientation) integral += np.average(sample_GB) integral *= deltaX for i in range(x[1:].size): numerator = 0. for j in range(num_angles): orientation.random_orientation() numerator += non_sphere_GB([0., 0., x[i+1]], orientation) numerator /= num_angles y[i] = (numerator/integral) return x[1:], y
def analytical_distribution_non_sphere(num_points): # heights are sampled evenly from the chosen bounds, using linspace # because linspace includes starting value A, the first index in x is ignored # if x[0] is included, then in the calculation of potential energy U, h-A = 0 # and an exception will be thrown # x = np.linspace(A, max_height, num_points) # orientations = [] # for i in range(num_points): # theta = np.random.normal(0., 1., 4) # orientations.append(Quaternion(theta/np.linalg.norm(theta))) # y = [] # deltaX = x[1] - x[0] # numerator, denominator = 0., 0. # # # add the bounds to the integral value # # ignore x[0] = A # integral = 0.5*(non_sphere_GB([0., 0., x[1]], orientations[0]) + # non_sphere_GB([0., 0., max_height], orientations[num_points-1])) # # iterate over the rest of the heights # for k in range(2, num_points): # integral += non_sphere_GB([0., 0., x[k]], orientations[k]) # # multiply by the change in x to complete the integral calculation # integral *= deltaX # # # now that we have the partition function that the integral represents # # we can calculate all the y positions of the distribution # # again ignore x[0] = A # j = 0 # for h in x[1:]: # numerator = non_sphere_GB([0., 0., h], orientations[j]) # y.append(numerator/integral) # j+=1 x = np.linspace(A, max_height, num_points) y = np.zeros(num_points - 1, dtype=float) num_angles = 1000 deltaX = x[1] - x[0] integral = .0 firstBar, lastBar = 0., 0. orientation = Quaternion([0, 0, 0, 0]) # create a quaternion object for i in range(num_angles): orientation.random_orientation() firstBar += non_sphere_GB([0, 0, x[1]], orientation) firstBar /= num_angles for i in range(num_angles): orientation.random_orientation() lastBar += non_sphere_GB([0, 0, x[num_points - 1]], orientation) lastBar /= num_angles integral += (firstBar + lastBar) * .5 sample_GB = np.zeros(num_angles, dtype=float) for i in range(2, num_points - 1): for j in range(num_angles): orientation.random_orientation() sample_GB[j] = non_sphere_GB(np.array([0, 0, x[i]]), orientation) integral += np.average(sample_GB) integral *= deltaX for i in range(x[1:].size): numerator = 0. for j in range(num_angles): orientation.random_orientation() numerator += non_sphere_GB([0., 0., x[i + 1]], orientation) numerator /= num_angles y[i] = (numerator / integral) return x[1:], y