コード例 #1
0
    def test_repeat_from_should_return_a_sample_with_every_values_the_mad_of_the_array(self):
        # Given
        sample = Sample([1, 2, 3, 3, 4, 4, 4, 5, 5.5, 6, 6, 6.5, 7, 7, 7.5, 8, 9, 12, 52, 90])

        # When
        mad_repeated_sample = self.mad_repeater.repeat_from(sample)

        # Then
        expected_sample = Sample([2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2])
        self.assertEqual(expected_sample, mad_repeated_sample)
コード例 #2
0
    def test_reassemble_should_append_right_part_to_the_left_one(self):
        # Given
        left_part = Sample([1, 1, 1, 1, 1, 1, 1, 1])
        right_part = Sample([2, 2, 2, 2, 2, 2])

        # When
        parts_associated = self.mad.reassemble(left_part, right_part)

        # Then
        self.assertEqual(Sample([1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2]), parts_associated)
コード例 #3
0
    def test_split_on_should_return_two_parts_of_an_array(self):
        # Given
        skewed_sample = Sample([1, 4, 4, 4, 5, 5, 5, 5, 7, 7, 8, 10, 16, 30])

        # When
        left_part, right_part = self.mad.split_on(skewed_sample)

        # Then
        assert_array_equal(Sample([1, 4, 4, 4, 5, 5, 5, 5]), left_part)
        assert_array_equal(Sample([7, 7, 8, 10, 16, 30]), right_part)
コード例 #4
0
    def test_normalize_should_divide_each_sample_element_with_provided_weights(self):
        # Given
        sample = Sample([2, 4, 6, 8])
        weights = Sample([2, 2, 2, 2])
        object_that_implements_on_method = type('identity', (object,), {'on': (lambda x: x)})

        # When
        normalized_sample = MadNormalizer(object_that_implements_on_method).normalize_with(sample, weights)

        # Then
        self.assertEqual(Sample([1, 2, 3, 4]), normalized_sample)
コード例 #5
0
    def test_mad_normalization_should_divide_each_sample_element_with_absolute_deviation_from_the_median(self):
        # Given
        sample = Sample([2, 4, 6, 8])
        weights = Sample([2, 2, 2, 2])
        absolute_deviation_from_the_median_implementation = AbsoluteDeviationFromTheMedian()

        # When
        normalized_sample = MadNormalizer(absolute_deviation_from_the_median_implementation).normalize_with(sample,
                                                                                                            weights)
        # Then
        self.assertEqual([1.5, 0.5, 0.5, 1.5], normalized_sample)
コード例 #6
0
    def test_on_should_calculate_the_absolute_distance_from_center_in_terms_of_the_mad(self):
        # Given
        absolute_deviation_from_the_median = AbsoluteDeviationFromTheMedian()
        sample_with_a_median_of_6 = Sample([1, 2, 3, 3, 4, 4, 4, 5, 5.5, 6, 6, 6.5, 7, 7, 7.5, 8, 9, 12, 52, 90])

        # When
        distances_from_the_centre = absolute_deviation_from_the_median.on(sample_with_a_median_of_6)

        # Then
        expected_distances = Sample(
            [5.0, 4.0, 3.0, 3.0, 2.0, 2.0, 2.0, 1.0, 0.5, 0.0, 0.0, 0.5, 1.0, 1.0, 1.5, 2.0, 3.0, 6.0,
             46.0, 84.0])
        assert_array_equal(expected_distances, distances_from_the_centre)
コード例 #7
0
 def _subtract_part_of(self, sample: Sample, threshold: float,
                       comparison_operator: callable) -> Sample:
     elements_are_on_this_side_of_the_threshold = comparison_operator(
         np.array(sample), threshold)
     return Sample(
         np.array(sample)[np.where(
             elements_are_on_this_side_of_the_threshold)])
コード例 #8
0
    def test_repeat_from_should_return_a_sample_of_same_length(self):
        # Given
        sample_of_length_3 = Sample([1, 2, 3])

        # When
        mad_repeated_sample = self.mad_repeater.repeat_from(sample_of_length_3)

        # Then
        self.assertEqual(3, len(mad_repeated_sample))
コード例 #9
0
    def test_int_on_should_calculate_the_median_of_absolute_deviations(self):
        # Given
        sample = Sample([
            1, 2, 3, 3, 4, 4, 4, 5, 5.5, 6, 6, 6.5, 7, 7, 7.5, 8, 9, 12, 52, 90
        ])
        mock_absolute_deviation_from_the_median = Mock(
            spec=AbsoluteDeviationFromTheMedian)
        distances_from_the_centre = Sample([
            5.0, 4.0, 3.0, 3.0, 2.0, 2.0, 2.0, 1.0, 0.5, 0.0, 0.0, 0.5, 1.0,
            1.0, 1.5, 2.0, 3.0, 6.0, 46.0, 84.0
        ])
        mock_absolute_deviation_from_the_median.on.return_value = distances_from_the_centre

        # When
        computed_distance = mad(mock_absolute_deviation_from_the_median).on(
            sample)

        # Then
        self.assertEqual(2, computed_distance)
コード例 #10
0
    def test_on_should_calculate_the_median_of_absolute_deviations(self):
        # Given
        sample = Sample([
            1, 2, 3, 3, 4, 4, 4, 5, 5.5, 6, 6, 6.5, 7, 7, 7.5, 8, 9, 12, 52, 90
        ])

        # When
        computed_mad = mad().on(sample)

        # Then
        self.assertEqual(2, computed_mad)
コード例 #11
0
    def test_on_should_return_0_when_more_than_50_percent_of_the_values_are_identical(
            self):
        # Given
        sample_with_more_than_fifty_percent_of_identical_values = Sample(
            [0, 1, 1, 1, 1, 1, 1, 1, 0])

        # When
        distance = mad().on(
            sample_with_more_than_fifty_percent_of_identical_values)

        # Then
        self.assertEqual(0, distance)
コード例 #12
0
    def test_on_should_allow_a_consistency_constant(self):
        # Given
        sample = Sample([
            1, 2, 3, 3, 4, 4, 4, 5, 5.5, 6, 6, 6.5, 7, 7, 7.5, 8, 9, 12, 52, 90
        ])

        # When
        consistency_constant_for_normal_distribution = 1.4826
        distance = mad().on(sample,
                            consistency_constant_for_normal_distribution)

        # Then
        self.assertEqual(2.9652, distance)
コード例 #13
0
ファイル: mad_repeater.py プロジェクト: sxhylkl/double_mad
 def repeat_from(self, sample: Sample) -> Sample:
     mad_of_the_sample = self.median_absolute_deviation_from_the_median.on(
         sample)
     return Sample([mad_of_the_sample] * len(sample))