def fit(self): ''' Fit a Holt-winters method of type={linear, additive, multiplicative} on data Return ---------- params: Array-like Parameters of the model [alpha, beta, gamma, m] depending on the type RMSE: Root Mean Squared Error of the model when fit to the data ''' rmse = 0.0 y = self.data if self.type == 'linear': initial_values = array([0.3, 0.1]) boundaries = [(0, 1), (0, 1)] parameters = fmin_l_bfgs_b(self.RMSE, x0 = initial_values, args = (y, self.type), bounds = boundaries, approx_grad = True) self.alpha, self.beta = parameters[0] rmse = parameters[1] return [self.alpha, self.beta], rmse elif self.type == 'additive': self.m = tsutils.findDominentSeason(y) initial_values = array([0.3, 0.1, 0.1]) boundaries = [(0, 1), (0, 1), (0, 1)] parameters = fmin_l_bfgs_b(self.RMSE, x0 = initial_values, args = (y, self.type, self.m), bounds = boundaries, approx_grad = True) self.alpha, self.beta, self.gamma = parameters[0] rmse = parameters[1] return [self.alpha, self.beta, self.gamma, self.m], rmse elif self.type == 'multiplicative': self.m = tsutils.findDominentSeason(y) initial_values = array([0.0, 1.0, 0.0]) boundaries = [(0, 1), (0, 1), (0, 1)] parameters = fmin_l_bfgs_b(self.RMSE, x0 = initial_values, args = (y, self.type, self.m), bounds = boundaries, approx_grad = True) self.alpha, self.beta, self.gamma = parameters[0] rmse = parameters[1] return [self.alpha, self.beta, self.gamma, self.m], rmse
def fit(self): ''' Fit a Holt-winters method of type={linear, additive, multiplicative} on data Return ---------- params: Array-like Parameters of the model [alpha, beta, gamma, m] depending on the type RMSE: Root Mean Squared Error of the model when fit to the data ''' rmse = 0.0 y = self.data[:] if self.type == 'linear': initial_values = array([0.3, 0.1]) boundaries = [(0, 1), (0, 1)] parameters = fmin_l_bfgs_b(self.RMSE, x0 = initial_values, args = (y, self.type), bounds = boundaries, approx_grad = True) self.alpha, self.beta = parameters[0] rmse = parameters[1] return [self.alpha, self.beta], rmse elif self.type == 'additive': self.m = tsutils.findDominentSeason(y) initial_values = array([0.3, 0.1, 0.1]) boundaries = [(0, 1), (0, 1), (0, 1)] parameters = fmin_l_bfgs_b(self.RMSE, x0 = initial_values, args = (y, self.type, self.m), bounds = boundaries, approx_grad = True) self.alpha, self.beta, self.gamma = parameters[0] rmse = parameters[1] return [self.alpha, self.beta, self.gamma, self.m], rmse elif self.type == 'multiplicative': self.m = tsutils.findDominentSeason(y) initial_values = array([0.0, 1.0, 0.0]) boundaries = [(0, 1), (0, 1), (0, 1)] parameters = fmin_l_bfgs_b(self.RMSE, x0 = initial_values, args = (y, self.type, self.m), bounds = boundaries, approx_grad = True) self.alpha, self.beta, self.gamma = parameters[0] rmse = parameters[1] return [self.alpha, self.beta, self.gamma, self.m], rmse
def update(self,data): ''' Updates the Holt-winters model by re-estimating the paramters {alpha, beta, gamma, m} depending on the model type ''' self.data = data[:].tolist() y = self.data[:] if self.type == 'linear': initial_values = array([self.alpha, self.beta]) boundaries = [(0, 1), (0, 1)] parameters = fmin_l_bfgs_b(self.RMSE, x0 = initial_values, args = (y, self.type), bounds = boundaries, approx_grad = True) self.alpha, self.beta = parameters[0] rmse = parameters[1] return [self.alpha, self.beta], rmse elif self.type == 'additive': self.m = tsutils.findDominentSeason(y) initial_values = array([self.alpha, self.beta, self.gamma]) boundaries = [(0, 1), (0, 1), (0, 1)] parameters = fmin_l_bfgs_b(self.RMSE, x0 = initial_values, args = (y, self.type, self.m), bounds = boundaries, approx_grad = True) self.alpha, self.beta, self.gamma = parameters[0] rmse = parameters[1] return [self.alpha, self.beta, self.gamma, self.m], rmse elif self.type == 'multiplicative': self.m = tsutils.findDominentSeason(y) initial_values = array([self.alpha, self.beta, self.gamma]) boundaries = [(0, 1), (0, 1), (0, 1)] parameters = fmin_l_bfgs_b(self.RMSE, x0 = initial_values, args = (y, self.type, self.m), bounds = boundaries, approx_grad = True) self.alpha, self.beta, self.gamma = parameters[0] rmse = parameters[1]
def update(self,data): ''' Updates the Holt-winters model by re-estimating the paramters {alpha, beta, gamma, m} depending on the model type ''' self.data = data[:] y = self.data[:] if self.type == 'linear': initial_values = array([self.alpha, self.beta]) boundaries = [(0, 1), (0, 1)] parameters = fmin_l_bfgs_b(self.RMSE, x0 = initial_values, args = (y, self.type), bounds = boundaries, approx_grad = True) self.alpha, self.beta = parameters[0] rmse = parameters[1] return [self.alpha, self.beta], rmse elif self.type == 'additive': self.m = tsutils.findDominentSeason(y) initial_values = array([self.alpha, self.beta, self.gamma]) boundaries = [(0, 1), (0, 1), (0, 1)] parameters = fmin_l_bfgs_b(self.RMSE, x0 = initial_values, args = (y, self.type, self.m), bounds = boundaries, approx_grad = True) self.alpha, self.beta, self.gamma = parameters[0] rmse = parameters[1] return [self.alpha, self.beta, self.gamma, self.m], rmse elif self.type == 'multiplicative': self.m = tsutils.findDominentSeason(y) initial_values = array([self.alpha, self.beta, self.gamma]) boundaries = [(0, 1), (0, 1), (0, 1)] parameters = fmin_l_bfgs_b(self.RMSE, x0 = initial_values, args = (y, self.type, self.m), bounds = boundaries, approx_grad = True) self.alpha, self.beta, self.gamma = parameters[0] rmse = parameters[1]
def fit(self): ''' Fits a model: First smoothes the data using a Median filter. Then determines if a pattern exists by calculating the dominant period and splitting the time series into pattern windows. For each adjacent pair of the pattern widows, the Pearson-Correlation is calculated and compared to the threshold together with the two pattern means. If the pattern exists for more than the match_ratio a Signature pattern is calculated using the original data and pattern windows that match. **Note: If no pattern is found, a 1st order Markov model is trained on the data instead ''' self.markov.fit() # Smooth data and dominant period smooth = signal.medfilt(self.data, self.window) Z = tsutils.findDominentSeason(self.data) # Z=288 # Determine the number of pattern windows and split smoothed data Q = np.int(np.ceil(1.0 * self.N / Z)) if Q > 2: idx = range(Z, Q * Z, Z) P = np.array(np.split(smooth, idx)) self.P = P self.warp = P[-1].shape[0] != P[-2].shape[0] # Check pattern exist PC = [] MR = [] for i in range(Q - 1 - self.warp): PC.append(stats.pearsonr(P[i], P[i + 1])[0]) MR.append(np.mean(P[i]) / np.mean(P[i + 1])) # Test if pattern exist if np.all(PC > self.corr_thres): self.contain_pat = True Pr = np.array(np.split(self.data, idx)) if self.warp: self.sig = np.average(Pr[:-1], axis=0) else: self.sig = np.average(Pr, axis=0)
def fit(self): ''' Fits a model: First smoothes the data using a Median filter. Then determines if a pattern exists by calculating the dominant period and splitting the time series into pattern windows. For each adjacent pair of the pattern widows, the Pearson-Correlation is calculated and compared to the threshold together with the two pattern means. If the pattern exists for more than the match_ratio a Signature pattern is calculated using the original data and pattern windows that match. **Note: If no pattern is found, a 1st order Markov model is trained on the data instead ''' self.markov.fit() # Smooth data and dominant period smooth = signal.medfilt(self.data, self.window) Z = tsutils.findDominentSeason(self.data) # Z=288 # Determine the number of pattern windows and split smoothed data Q = np.int(np.ceil(1.0*self.N/Z)) if Q > 2: idx = range(Z,Q*Z,Z) P = np.array(np.split(smooth, idx)) self.P = P self.warp = P[-1].shape[0] != P[-2].shape[0] # Check pattern exist PC = [] MR = [] for i in range(Q-1-self.warp): PC.append(stats.pearsonr(P[i], P[i+1])[0]) MR.append(np.mean(P[i])/np.mean(P[i+1])) # Test if pattern exist if np.all(PC>self.corr_thres): self.contain_pat = True Pr = np.array(np.split(self.data, idx)) if self.warp: self.sig = np.average(Pr[:-1],axis=0) else: self.sig = np.average(Pr,axis=0)