Beispiel #1
0
    def test_get_seasonality_matrix_month(self):
        """
        Test that seasonality matrix works with month
        :return: None
        """
        dates = date_range(2013, 1, 2015, 8)
        #This maps Jan -> 0, Dec -> 11 etc
        seasonality_function = lambda date: date.month-1
        matrix, compression = get_seasonality_matrix(dates, seasonality_function)
        for key, val in compression.items():
            self.assertEquals(val, key)

        n_dates = len(dates)
        n_months = 12
        self.assertEquals(matrix.size, (n_dates, n_months))
        # the first one which is Jan should be 1 for month index 0 and
        # 0 for every other month index
        self.assertEquals(matrix[0, 0], 1)
        for month_num in xrange(n_months):
            self.assertEquals(matrix[0, month_num], int(month_num == 0))
        # Test that the whole matrix is as expected
        for index in xrange(n_dates):
            for month_num in xrange(n_months):
                month_num_from_index = index % 12
                self.assertEquals(matrix[index, month_num],
                                  int(month_num == month_num_from_index))
Beispiel #2
0
 def setUp(self):
     self.mock = make_mock(do_plot=False)
     self.y = self.mock['y']
     self.num = len(self.y)
     self.dates = date_range(2015, 1, 2029, 12)[0:self.num]
     self.assertEquals(len(self.y), len(self.dates))
     seas_func = lambda date: date.month-1
     self.seasonality_matrix, self.compression_dict = \
         get_seasonality_matrix(self.dates, seasonality_function=seas_func)
Beispiel #3
0
 def test_get_seasonality_matrix_year(self):
     """
     Test that seasonality matrix works with year
     :return: None
     """
     dates = date_range(2013, 1, 2015, 8)
     #This maps 2013->0, 2014->1, 2015->2
     seasonality_function = lambda date: date.year-2013
     matrix, compression = get_seasonality_matrix(dates, seasonality_function)
     expected_compression = {0: 0, 1: 1, 2: 2}
     self.assertDictEqual(compression, expected_compression)
     n_dates = len(dates)
     n_years = 3
     self.assertEquals(matrix.size, (n_dates, n_years))
     # Test that the whole matrix is as expected
     for index in xrange(n_dates):
         for year_num in xrange(n_years):
             #integer division
             year_num_from_index = index / 12
             self.assertEquals(matrix[index, year_num],
                               int(year_num == year_num_from_index))
Beispiel #4
0
    def __init__(self, dates, y,
                 timescale=None,
                 seasonality_function=None,
                 params=None):
        """
        Instantiate and initialize the object
        :param dates: iterable of datetime.date or datetime.datetime objects
        :param y: iterable, the y-values for the time series
        :param timescale: sets the time scale for scaling dates to numbers
                          allows the regularization parameters to stay scale
                          independent
        :param seasonality_function: a function that maps dates to an index
               pertaining to seasonality variables (zero indexed)
               example: lambda date: date.month-1
        :param params: a dictionary of overrides for default parameters,
               mostly regularization parameters, see default_params
        :return: an object instance
        """

        if timescale is None:
            # timescale = (1, 'month')
            timescale = default_timescale(dates)

        self.dates = np.array(dates)
        self.y = np.array(y)
        # scale the dates to an index
        self.timescale = timescale
        self.x = np.array([scale_date(date, self.timescale) for date in dates])
        self.x_min = min(self.x)
        self.x_max = max(self.x)
        self.params = default_params()
        if seasonality_function is None:
            # does it make sense to have a default?
            # only with seasonality turned off
            seasonality_function = lambda the_date: the_date.month - 1
            # some largish number
            self.params['beta_seasonal'] = 13796.0

        self.seasonality_function = seasonality_function

        # calculate the seasonality matrix, this combined with the
        # index 'x' allows us to forget about dates and call the
        # date agnostic fit function

        mat, compress = get_seasonality_matrix(dates, seasonality_function)

        self.seasonality_matrix = mat
        self.compression_dict = compress
        self.solution = None
        self.slope = None
        self.offset = None
        self.seasonal = None

        # dummy functions for now, replace after fit
        self.interpolate = lambda x: None
        self.extrapolate_without_seasonal = lambda x: None
        self.date_to_seasonal_component = lambda x: None

        if params is not None:
            # override any parameters handed in with
            # params dictionary
            for key, value in params.items():
                self.params[key] = value