def test_nudge_individual_correct_nudge_size(self): input_dist = np.array([[ [0.2, 0.05, 0.1], [0.05, 0.1, 0.025], ], [[0.1, 0.08, 0.02], [0.05, 0.025, 0.2]]]) nudge_size = 0.01 for _ in range(100): new_dist = nudge.nudge_individual(input_dist, 0.01) self.assertAlmostEqual(nudge_size, np.sum(np.absolute(new_dist - input_dist))) for _ in range(100): input_dist = np.random.dirichlet(np.array([6, 5, 3, 4, 8])) new_dist = nudge.nudge_individual(input_dist, 0.01) self.assertAlmostEqual(nudge_size, np.sum(np.absolute(new_dist - input_dist)))
def test_nudge_individual_create_new_probability_dist(self): input_dist = np.array([[ [0.2, 0.05, 0.1], [0.05, 0.1, 0.025], ], [[0.1, 0.08, 0.02], [0.05, 0.025, 0.2]]]) nudge_size = 0.01 for _ in range(100): new_dist = nudge.nudge_individual(input_dist, 0.01) self.assertAlmostEqual(1, np.sum(new_dist)) self.assertTrue(np.all(new_dist >= 0)) nudge_size = 0.01 for _ in range(100): input_dist = np.random.dirichlet(np.array([5, 5, 5, 5, 5])) new_dist = nudge.nudge_individual(input_dist, 0.01) self.assertAlmostEqual(1, np.sum(new_dist)) self.assertTrue(np.all(new_dist >= 0))
def test_nudge_individual_makes_copy(self): input_dist = np.array([[ [0.2, 0.05, 0.1], [0.05, 0.1, 0.025], ], [[0.1, 0.08, 0.02], [0.05, 0.025, 0.2]]]) input_dist_copy = np.copy(input_dist) new_dist = nudge.nudge_individual(input_dist, 0.01) self.assertTrue(np.all(input_dist == input_dist_copy)) print(new_dist) print(np.sum(np.absolute(new_dist - input_dist)))
def test_nudge_individual_correct_nudge_with_noise_vector(self): input_dist = np.array([[ [0.2, 0.05, 0.1], [0.05, 0.1, 0.025], ], [[0.1, 0.09, 0.01], [0.05, 0.025, 0.2]]]) noise_vector = np.array([0.1, -0.025, -0.075]) nudge_size = 0.2 new_dist = nudge.nudge_individual(input_dist, 0.01, noise_vector) expected_new_input_dist = np.array([[ [0.235, 0.04125, 0.07375], [0.0675, 0.095625, 0.011875], ], [[0.115, 0.085, 0], [0.0775, 0.018125, 0.179375]]]) self.assertTrue(np.allclose(new_dist, expected_new_input_dist)) shape = [5, 6, 6, 4, 3] total_number_of_states = reduce(lambda x, y: x * y, shape) for _ in range(100): input_dist = np.random.dirichlet( np.array(total_number_of_states * [1])) input_dist = np.reshape(input_dist, shape) new_dist = nudge.nudge_individual(input_dist, 0.2, noise_vector) self.assertAlmostEqual(np.sum(new_dist), 1) self.assertTrue(np.all(new_dist >= 0))
def evaluate(self, start_distribution, cond_output): """ Find the impact of the nudge on the output distribution and set the score. Parameters: ---------- start_distribution: an nd-array Representing a probability distribution cond_output: an nd-array The output conditioned on all input variables """ new_input_dist = nudge.nudge_individual(start_distribution, self.nudge_size, self.genes) nudge_impact = nudge.find_nudge_impact(start_distribution, new_input_dist, cond_output, measure="absolute") #to make it a minimization rather than a maximization problem self.score = -nudge_impact