Beispiel #1
0
    def test_confidence_interval(self):
        """
        Test if given two timeseries and a desired confidence interval,
        regression gives us the correct over and underestimation.
        """
        data_x = zip(range(100), range(100))
        overestimations  = [[90, 90 - 1], [91, 91 - 3], [92, 92 - 1], [93, 93 - 40], [94, 94 - 1]]
        underestimations = [[95, 95 + 5], [96, 96 + 1 ], [97,97 + 4], [98, 98 + 3], [99, 99 + 1]]
        data_y = data_x[:90] + overestimations + underestimations

        ts_x = TimeSeries.from_twodim_list(data_x)
        ts_y = TimeSeries.from_twodim_list(data_y)

        #Mock the random.sample method so that we can use our outliers as samples
        with patch('pycast.common.timeseries.random.sample') as sample_mock:
            sample_mock.return_value = underestimations+overestimations

            reg = Regression()
            n, m, error = reg.calculate_parameters_with_confidence(ts_x, ts_y, .6)

            #Since all values are the same the params should be n=0, m=1
            self.assertEquals(0,n)
            self.assertEquals(1,m)

            #assert under/overestimation
            self.assertEquals(error[0], -1)
            self.assertEquals(error[1], 3)
Beispiel #2
0
    def test_confidence_interval(self):
        """
        Test if given two timeseries and a desired confidence interval,
        regression gives us the correct over and underestimation.
        """
        data_x = zip(range(100), range(100))
        overestimations = [[90, 90 - 1], [91, 91 - 3], [92, 92 - 1],
                           [93, 93 - 40], [94, 94 - 1]]
        underestimations = [[95, 95 + 5], [96, 96 + 1], [97, 97 + 4],
                            [98, 98 + 3], [99, 99 + 1]]
        data_y = data_x[:90] + overestimations + underestimations

        ts_x = TimeSeries.from_twodim_list(data_x)
        ts_y = TimeSeries.from_twodim_list(data_y)

        #Mock the random.sample method so that we can use our outliers as samples
        with patch('pycast.common.timeseries.random.sample') as sample_mock:
            sample_mock.return_value = underestimations + overestimations

            reg = Regression()
            n, m, error = reg.calculate_parameters_with_confidence(
                ts_x, ts_y, .6)

            #Since all values are the same the params should be n=0, m=1
            self.assertEquals(0, n)
            self.assertEquals(1, m)

            #assert under/overestimation
            self.assertEquals(error[0], -1)
            self.assertEquals(error[1], 3)
Beispiel #3
0
    def predict_test(self):
        """Test if given an independent timeseries and parameters the
        right prediction is done"""
        data1 = [[1, 1], [2, 2], [3, 3]]
        data2 = [[1, 3], [2, 5], [3, 7]]

        ts1 = TimeSeries.from_twodim_list(data1)
        ts2 = TimeSeries.from_twodim_list(data2)

        reg = Regression()
        result = reg.predict(ts1, 1, 2)
        self.assertEquals(ts2, result)
Beispiel #4
0
    def predict_test(self):
        """Test if given an independent timeseries and parameters the
        right prediction is done"""
        data1 = [[1, 1],[2,2],[3,3]]
        data2 = [[1, 3],[2,5],[3,7]]

        ts1 = TimeSeries.from_twodim_list(data1)
        ts2 = TimeSeries.from_twodim_list(data2)

        reg = Regression()
        result = reg.predict(ts1, 1, 2)
        self.assertEquals(ts2, result)
Beispiel #5
0
    def calculate_parameters_one_empty_list_test(self):
        """Test for ValueError if one Timeseries are empty"""
        tsOne = TimeSeries.from_twodim_list([[1, 12.34]])
        tsTwo = TimeSeries.from_twodim_list([])

        reg = Regression()
        self.assertRaises(ValueError, reg.calculate_parameters, tsOne, tsTwo)
Beispiel #6
0
    def match_time_series_test(self):
        """Test if two timeseries are matched correctly"""
        # Initialize input
        data1 = [[1, 12.42], [4, 34.23], [7, 12.32], [8, 12.45]]
        data2 = [[2, 32.45], [7, 65.34], [4, 23.12], [5, 32.45]]
        tsSrc1 = TimeSeries.from_twodim_list(data1)
        tsSrc2 = TimeSeries.from_twodim_list(data2)

        # Initialize a correct result
        dstX = [[4, 34.23], [7, 12.32]]
        dstY = [[4, 23.12], [7, 65.34]]

        reg = Regression()
        tsX, tsY = reg.match_time_series(tsSrc1, tsSrc2)

        self.assertEqual(tsX, dstX)
        self.assertEqual(tsY, dstY)
Beispiel #7
0
    def match_time_series_test(self):
        """Test if two timeseries are matched correctly"""
        # Initialize input
        data1  = [[1, 12.42], [4, 34.23], [7, 12.32], [8, 12.45]]
        data2  = [[2, 32.45], [7, 65.34], [4, 23.12], [5, 32.45]]
        tsSrc1 = TimeSeries.from_twodim_list(data1)
        tsSrc2 = TimeSeries.from_twodim_list(data2)

        # Initialize a correct result
        dstX   = [[4, 34.23], [7, 12.32]]
        dstY   = [[4, 23.12], [7, 65.34]]

        reg = Regression()
        tsX, tsY = reg.match_time_series(tsSrc1, tsSrc2)

        self.assertEqual(tsX, dstX)
        self.assertEqual(tsY, dstY)
Beispiel #8
0
    def calculate_parameters_test(self):
        """Test the calculation of the parameters for the regression line."""
        # Initialize the source
        data1 = [[1, 10.00], [7, 22.01], [2, 12.40], [3, 17.38], [4, 16.66],
                 [5, 20.12], [6, 23.45], [8, 24.34], [9, 12.12]]
        data2 = [[1, 13.00], [2, 15.40], [3, 15.38], [4, 16.32], [10, 10.12],
                 [5, 18.14], [6, 20.05], [7, 21.01], [8, 25.34]]
        tsSrc1 = TimeSeries.from_twodim_list(data1)
        tsSrc2 = TimeSeries.from_twodim_list(data2)

        # Initialize a correct result.
        expRes = (5.546941864140029, 0.6850537379535376)

        # Initialize the method
        reg = Regression()
        res = reg.calculate_parameters(tsSrc1, tsSrc2)

        self.assertEquals(res, expRes)
Beispiel #9
0
    def calculate_parameter_with_short_timeseries_test(self):
        """Test for ValueError if Timeseries has only one matching date"""
        # Initialize input
        data1 = [[1, 12.23], [4, 23.34]]
        data2 = [[1, 34.23]]
        tsSrc1 = TimeSeries.from_twodim_list(data1)
        tsSrc2 = TimeSeries.from_twodim_list(data2)

        reg = Regression()
        self.assertRaises(ValueError, reg.calculate_parameters, tsSrc1, tsSrc2)
Beispiel #10
0
    def calculate_parameter_duplicate_dates_test(self):
        """Test for ValueError if dates in timeseries are not distinct"""
        # Initialize input
        data1 = [[1, 12.23], [4, 23.34]]
        data2 = [[1, 34.23], [1, 16.23]]
        tsSrc1 = TimeSeries.from_twodim_list(data1)
        tsSrc2 = TimeSeries.from_twodim_list(data2)

        reg = Regression()
        self.assertRaises(ValueError, reg.calculate_parameters, tsSrc1, tsSrc2)
Beispiel #11
0
    def calculate_parameters_without_match_test(self):
        """Test for ValueError, if the input timeseries have no macthing dates"""
        # Initialize input
        data1 = [[1, 12.42], [6, 12.32], [8, 12.45]]
        data2 = [[2, 32.45], [4, 23.12], [7, 65.34]]
        tsOne = TimeSeries.from_twodim_list(data1)
        tsTwo = TimeSeries.from_twodim_list(data2)

        reg = Regression()

        self.assertRaises(ValueError, reg.calculate_parameters, tsOne, tsTwo)
Beispiel #12
0
    def calculate_parameters_test(self):
        """Test the calculation of the parameters for the regression line."""
        # Initialize the source
        data1 = [
                    [1, 10.00], [7, 22.01], [2, 12.40], [3, 17.38], [4, 16.66],
                    [5, 20.12], [6, 23.45], [8, 24.34], [9, 12.12]
                ]
        data2 = [
                    [1, 13.00], [2, 15.40], [3, 15.38], [4, 16.32], [10, 10.12],
                    [5, 18.14], [6, 20.05], [7, 21.01], [8, 25.34]
                ]
        tsSrc1 = TimeSeries.from_twodim_list(data1)
        tsSrc2 = TimeSeries.from_twodim_list(data2)

        # Initialize a correct result.
        expRes  = (5.546941864140029, 0.6850537379535376)

        # Initialize the method
        reg = Regression()
        res = reg.calculate_parameters(tsSrc1, tsSrc2)

        self.assertEquals(res, expRes)