def test_compute_attachment_raises_when_not_matching_opinions_influence(
         self):
     x1 = [0.1, 0.2, 0.6, 0.4]
     x2 = [0.9, 0.4, 0.7, 0.5]
     w12 = [0.1, 0.0]
     with self.assertRaises(ValueError):
         gll.compute_attachment(x1, x2, w12, gll.AttachmentType.TO_INITIAL)
 def test_compute_attachment_bef_disc_raises_when_not_matching_op_influence(
         self):
     x1 = [0.1, 0.2, 0.6, 0.4]
     x2 = [0.9, 0.4, 0.7, 0.5]
     w12 = [0.1, 0.0]
     with self.assertRaises(ValueError):
         gll.compute_attachment(x1,
                                x2,
                                w12,
                                to_opinion=gll.AttachmentType.TO_PREVIOUS)
 def test_compute_attachment_raises_when_start_k_was_not_0_or_1(self):
     x1 = [0.1, 0.2, 0.6, 0.4]
     x2 = [0.9, 0.4, 0.7, 0.5]
     w12 = [0.1, 0.0]
     with self.assertRaises(ValueError):
         gll.compute_attachment(x1,
                                x2,
                                w12,
                                start_k=-1,
                                to_opinion=gll.AttachmentType.TO_INITIAL)
 def test_compute_attachment_when_div_by_zero(self):
     x1 = [0.2, 0.2, 0.2]
     x2 = [0.4, 0.4, 0.4]
     w12 = [0.1, 0.0]
     expected_a11 = [0, np.nan]
     computed_a11, _ = gll.compute_attachment(
         xi=x1,
         xj=x2,
         wij=w12,
         eps=0,
         to_opinion=gll.AttachmentType.TO_PREVIOUS)
     np_testing.assert_array_almost_equal(expected_a11, computed_a11)
 def test_compute_attachment_when_start_k_equals_1(self):
     x1 = [0.1, 0.2, 0.6, 0.4]
     x2 = [0.9, 0.4, 0.7, 0.5]
     w12 = [0.1, 0.0, 0.2]
     expected_a11 = [0.5 / 0.1, 0.3 / 0.52]
     computed_a11, _ = gll.compute_attachment(
         xi=x1,
         xj=x2,
         wij=w12,
         start_k=1,
         eps=0,
         to_opinion=gll.AttachmentType.TO_INITIAL)
     np_testing.assert_array_almost_equal(expected_a11, computed_a11)
 def test_compute_attachment_to_opinion_bef_disc_when_div_by_zero_with_eps(
         self):
     x1 = [0.2, 0.2, 0.2]
     x2 = [0.4, 0.4, 0.4]
     w12 = [0.1, 0.0]
     eps = 0.01
     expected_a11 = [(0.2 - 0.2) / (0.1 * (0.4 - 0.2) + eps), 0 / eps]
     computed_a11, _ = gll.compute_attachment(
         xi=x1,
         xj=x2,
         wij=w12,
         eps=eps,
         to_opinion=gll.AttachmentType.TO_PREVIOUS)
     np_testing.assert_array_almost_equal(expected_a11, computed_a11)
 def test_compute_attachment_to_initial_when_division_by_zero_with_eps(
         self):
     x1 = [0.2, 0.2, 0.2]
     x2 = [0.4, 0.4, 0.4]
     w12 = [0.1, 0.0]
     eps = 0.01
     expected_a11 = [0 / (0.02 + eps), 0 / (0 + eps)]
     computed_a11, _ = gll.compute_attachment(
         xi=x1,
         xj=x2,
         wij=w12,
         eps=eps,
         to_opinion=gll.AttachmentType.TO_INITIAL)
     np_testing.assert_array_almost_equal(expected_a11, computed_a11)
 def test_compute_attachment_to_initial_opinion(self):
     x1 = [0.1, 0.2, 0.6, 0.4]
     x2 = [0.9, 0.4, 0.7, 0.5]
     w12 = [0.1, 0.0, 0.2]
     expected_a11 = [0.1 / 0.08, 0.5 / 0.1, 0.3 / 0.52]
     expected_details = [{}, {}, {}]
     computed_a11, details = gll.compute_attachment(
         xi=x1,
         xj=x2,
         wij=w12,
         eps=0,
         to_opinion=gll.AttachmentType.TO_INITIAL)
     np_testing.assert_array_almost_equal(expected_a11, computed_a11)
     utils.assert_dict_equals({'1': expected_details}, {'1': details})
 def test_compute_attachment_to_opinion_before_disc_when_start_k_equals_1(
         self):
     x1 = [0.1, 0.2, 0.6, 0.4]
     x2 = [0.9, 0.4, 0.7, 0.5]
     w12 = [0.1, 0.0, 0.2]
     expected_a11 = [(0.6 - 0.1) / (0.2 - 0.1),
                     (0.4 - 0.2) / (0.6 - 0.2 + 0.2 * (0.7 - 0.6))]
     computed_a11, _ = gll.compute_attachment(
         xi=x1,
         xj=x2,
         wij=w12,
         start_k=1,
         eps=0,
         to_opinion=gll.AttachmentType.TO_PREVIOUS)
     np_testing.assert_array_almost_equal(expected_a11, computed_a11)
 def test_compute_attachment_to_previous_opinion(self):
     x1 = [0.1, 0.2, 0.6, 0.4]
     x2 = [0.9, 0.4, 0.7, 0.5]
     w12 = [0.1, 0.0, 0.2]
     expected_a11 = [
         0.1 / 0.08, (0.6 - 0.1) / (0.2 - 0.1),
         (0.4 - 0.2) / (0.6 - 0.2 + 0.2 * (0.7 - 0.6))
     ]
     computed_a11, _ = gll.compute_attachment(
         xi=x1,
         xj=x2,
         wij=w12,
         eps=0,
         to_opinion=gll.AttachmentType.TO_PREVIOUS)
     np_testing.assert_array_almost_equal(expected_a11, computed_a11)
 def test_compute_attachment_to_initial_op_when_denom_to_sum_almost_zero(
         self):
     x1 = [0.8, 0.85, 0.92, 0.92]
     x2 = [0.6, 0.6, 0.7, 0.7]
     w12 = [0.1, 0.2, 0.1]
     expected_a11 = [
         (0.85 - 0.8) / (0.1 * (0.6 - 0.8)),
         np.nan,  # (0.92 - 0.8) / (0.85 - 0.8 + 0.2 * (0.6 - 0.85)),
         (0.92 - 0.8) / (0.92 - 0.8 + 0.1 * (0.7 - 0.92))
     ]
     expected_details = [{}, {'n/0': 1}, {}]
     computed_a11, details = gll.compute_attachment(
         xi=x1,
         xj=x2,
         wij=w12,
         eps=0,
         to_opinion=gll.AttachmentType.TO_INITIAL)
     np_testing.assert_array_almost_equal(expected_a11, computed_a11)
     utils.assert_dict_equals({'1': expected_details}, {'1': details})