def test_l1_fit_runs_correctly(self): # how low can we shrink this tolerance and still pass? tol = 0.03 index = np.arange(self.num) result = l1_fit(index, self.y, seasonality_matrix=self.seasonality_matrix) model = result['model'] self.assertEquals(len(model), self.num) for x_value, y_value, model_value in zip(index, self.y, model): diff = model_value - y_value print x_value, y_value, model_value, diff self.assertLess(abs(diff), tol) assert True
def fit(self): self.solution = l1_fit(self.x, self.y, beta_d1=self.params['beta_d1'], beta_d2=self.params['beta_d2'], beta_seasonal=self.params['beta_seasonal'], beta_step=self.params['beta_step'], growth=self.params['growth'], seasonality_matrix=self.seasonality_matrix) y2 = self.solution['base'][-1] y1 = self.solution['base'][-2] x2 = self.x[-1] x1 = self.x[-2] self.slope = (y2 - y1) / float(x2 - x1) self.offset = y2 - self.slope * x2 # fill in the seasonal parameters # remember that the last one is 1-sum_of_rest # as they sum to zero seasonal_params = self.solution['seasonal_parameters'] sum_seasonal = seasonal_params.sum() self.seasonal = np.array(list(seasonal_params) + [-sum_seasonal]) # define this function to gracefully handle # unconstrained seasonal params def date_to_seasonal_component_function(the_date): the_index = self.seasonality_function(the_date) if the_index not in self.compression_dict: return 0.0 compressed_index = self.compression_dict[the_index] return self.seasonal[compressed_index] self.date_to_seasonal_component = date_to_seasonal_component_function # set this function for interpolating self.interpolate = interp1d(self.x, self.solution['model']) self.extrapolate_without_seasonal = lambda new_x: self.offset + self.slope * new_x
def test_l1_fit_runs(self): index = np.arange(self.num) result = l1_fit(index, self.y, seasonality_matrix=self.seasonality_matrix) self.assertEquals(len(result['model']), self.num)