Skip to content

Commit

Permalink
Modify change factor calculation to favor large distances changes
Browse files Browse the repository at this point in the history
Since it's more benefitial to allow big changes that cost low positive or high negative prediction changes, this commit makes this modification. Where small positive changes that greatly change the distance are favored and large negative changes that largely change the distance are also favored. The change in distance must be interpreted as how much closer the CF is from a factual, therefore, larger changes are better.
  • Loading branch information
rmazzine committed Aug 12, 2022
1 parent 1d2f921 commit 7a307a0
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
5 changes: 2 additions & 3 deletions cfnow/_fine_tune.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,12 @@ def _calculate_change_factor(c_cf, changes_back_factual, feat_distances, changes
change_factor_feat = []
for f_pc, f_d in zip(prediction_dif, feat_distances[changes_back_original_idxs]):
if f_pc >= 0:
change_factor_feat.append(f_pc*f_d)
else:
# Avoid division by zero
if f_d == 0:
change_factor_feat.append(0)
else:
change_factor_feat.append(f_pc/f_d)
else:
change_factor_feat.append(f_pc*f_d)

return np.array(change_factor_feat)

Expand Down
10 changes: 5 additions & 5 deletions tests/test__fine_tune.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def test__calculate_change_factor(self):
change_factor_feat = _calculate_change_factor(c_cf, changes_back_factual, feat_distances,
changes_back_original_idxs, mp1c, c_cf_c)

self.assertListEqual([0.1, 0.08, 0.04, 0.02], list([round(n, 3) for n in change_factor_feat]))
self.assertListEqual([0.1, 0.125, 0.25, 0.5], list([round(n, 3) for n in change_factor_feat]))
self.assertIsInstance(change_factor_feat, np.ndarray)

def test__calculate_change_factor_negative(self):
Expand All @@ -61,23 +61,23 @@ def test__calculate_change_factor_negative(self):
change_factor_feat = _calculate_change_factor(c_cf, changes_back_factual, feat_distances,
changes_back_original_idxs, mp1c, c_cf_c)

self.assertListEqual([-0.1, round(-0.1/0.8, 3), 0.04, 0.02], list([round(n, 3) for n in change_factor_feat]))
self.assertListEqual([-0.1, -0.08, 0.25, 0.5], list([round(n, 3) for n in change_factor_feat]))
self.assertIsInstance(change_factor_feat, np.ndarray)

def test__calculate_change_factor_negative_zero_distance(self):
def test__calculate_change_factor_positive_zero_distance(self):
# This should not happen as the objective function should not return zero values
c_cf = np.array([1, 1, 1, 1])
changes_back_factual = np.array([[0.1, 0., 0., 0.], [0., 0.1, 0., 0.], [0., 0., 0.1, 0.], [0., 0., 0., 0.1]])
feat_distances = np.array([1, 0.8, 0, 0.2])
changes_back_original_idxs = [0, 1, 2, 3]
mp1c = MagicMock()
mp1c.return_value = np.array([0.7, 0.7, 0.7, 0.5])
mp1c.return_value = np.array([0.7, 0.7, 0.5, 0.5])
c_cf_c = 0.6

change_factor_feat = _calculate_change_factor(c_cf, changes_back_factual, feat_distances,
changes_back_original_idxs, mp1c, c_cf_c)

self.assertListEqual([-0.1, round(-0.1/0.8, 3), 0, 0.02], list([round(n, 3) for n in change_factor_feat]))
self.assertListEqual([-0.1, -0.08, 0, 0.5], list([round(n, 3) for n in change_factor_feat]))
self.assertIsInstance(change_factor_feat, np.ndarray)

def test__generate_change_vectors_all_num_no_tabu(self):
Expand Down

0 comments on commit 7a307a0

Please sign in to comment.