def distances_sum(point, point_array, distance_method=('euclidean', 2)): distances = float(0) m = np.shape(point_array)[0] point = np.asarray(point)[0] for j in range(m): if distance_method[0] == 'euclidean': distances += np.power( distance.euclidean_distance(point, np.asarray(point_array[j])[0]), distance_method[1]) elif distance_method[0] == 'manhattan': distances += np.power( distance.manhattan_distance(point, np.asarray(point_array[j])[0]), distance_method[1]) elif distance_method[0] == 'minkowski': distances += np.power( distance.minkowski_distance(point, np.asarray(point_array[j])[0], distance_method[1]), distance_method[1]) else: distances += np.power( distance.euclidean_distance(point, point_array[j]), distance_method[1]) '''or distance_j = distance.minkowski_distance(point, centroid_j, distance_method[1]) ''' return distances
def find_distance_min(point, point_array, distance_method=('euclidean', 2)): min_distance = float('inf') min_centroid_index = -1 k = np.shape(point_array)[0] point = np.asarray(point)[0] for j in range(k): centroid_j = point_array[j] if distance_method[0] == 'euclidean': distance_j = distance.euclidean_distance(point, centroid_j) elif distance_method[0] == 'manhattan': distance_j = distance.manhattan_distance(point, centroid_j) elif distance_method[0] == 'minkowski': distance_j = distance.minkowski_distance(point, centroid_j, distance_method[1]) else: distance_j = distance.euclidean_distance(point, centroid_j) '''or distance_j = distance.minkowski_distance(point, centroid_j, distance_method[1]) ''' if distance_j < min_distance: min_distance = distance_j min_centroid_index = j return min_centroid_index, min_distance
def test_manhattan_distance_corner(self): self.assertEqual(4, manhattan_distance(13))
def test_manhattan_distance_base(self): self.assertEqual(0, manhattan_distance(1))
def test_manhattan_distance_far(self): self.assertEqual(31, manhattan_distance(1024))
def test_manhattan_distance_straight(self): self.assertEqual(2, manhattan_distance(23))
def test_ABC(k1, k2, k3): #Generate a model model = MM_SSA(k1, k2, k3, max_time=10) #Set threshold epsilon = 1200 #Generate a random trajectory - considered real trajectory model.SSA() T = model.get_trajectory() #Get number of samples n_samples = len(model.time) #Initialize index i = 0 #Initialize first prior mutlivariate Gaussian distribution (mean, covariance) # theta_guess = np.random.uniform(0, 1, (3,3)) theta_guess = np.array([[k1 - 0.1, k2 - 0.002, k3 - 0.03], [k1 - 0.04, k2 - 0.05, k3], [k1 - 0.7, k2 - 0.01, k3 + 0.2]]) #theta_guess = np.array([k1-0.01, k2-0.02, k3-0.1]).reshape(1,3) # distribution = { 'mean': np.mean(theta_guess, axis=0), 'cov': np.cov(theta_guess, rowvar=True) } #Create the theta_list theta = theta_guess print('Starting example') print( multivariate_normal.rvs(size=1, mean=distribution['mean'], cov=distribution['cov']).reshape(1, 3)) while (i < 400): #Generate a random set (k1,k2,k3) by prior distribution which is GOOD trails_500 = 0 trails_1000 = 0 while (True): #print('Entered Loop') theta_guess = multivariate_normal.rvs( size=1, mean=distribution['mean'], cov=distribution['cov']).reshape(1, 3) if (trails_500 > 100): theta_guess = distribution['mean'].reshape(1, 3) #print('Blocked here 1') model_guess = MM_SSA(*theta_guess[0], max_time=n_samples + 1) #print('Blocked here 2') model_guess.SSA() #print('Blocked here 3') T_star = model_guess.get_trajectory() #print('Blocked here 4') if (epsilon_test(np.array(T), np.array(T_star), epsilon)): theta = np.append(theta, theta_guess, axis=0) epsilon = (manhattan_distance(np.array(T), np.array(T_star)) + 12 * epsilon) / 13 break if (trails_500 > 3999): trails_500 = 0 epsilon = ( 2 * manhattan_distance(np.array(T), np.array(T_star)) + epsilon) / 3 elif (trails_1000 > 4000): return distribution trails_500, trails_1000 = trails_500 + 1, trails_1000 + 1 #print(f'{trails} trails') print(f'Accepted with epsilon = {epsilon}') #Create the new posterior distribution #Prior Distribution prior_distribution = distribution #Likelihood D try: mean = np.mean(theta, axis=0) cov = np.cov(theta, rowvar=0) likelihood_distribution = {'mean': mean, 'cov': cov} except: likelihood_distribution = {'mean': mean, 'cov': cov} finally: #Update new posterior distribution distribution = { 'mean': Prod_Multivar_Normal(prior_distribution, likelihood_distribution)[0], 'cov': Prod_Multivar_Normal(prior_distribution, likelihood_distribution)[1] } print(f'i= {i}') #Update index i = i + 1 return distribution
def epsilon_test(T1, T2, epsilon): if (manhattan_distance(T1, T2) < epsilon): return True else: return False