def detectDoubleExponentialSmoothingAnomalies(series, y, alpha, beta, threshold=2, mae = 0, deviation= 0, bound=IS_UPPER_BOUND, calculateScore=False, debug=False, returnAnomaliesOnly= True): yhat = double_exponential_smoothing(y, alpha, beta) # calculate confidence intervals for smoothed values if( mae==0 and deviation ==0 ): mae = mean_absolute_error(y, yhat) if calculateScore==True or debug == True: mape = mean_absolute_percentage_error(y, yhat) logging.info("Mean absolute error is "+str(mae)+", Mean absolute percentage_error is "+str(mape)+"%") if (mae ==0 and deviation == 0 ): deviation = np.std(y - yhat) lower_bond = yhat - (mae + threshold * deviation) upper_bond = yhat + (mae + threshold * deviation) if debug == True: logging.info(lower_bond) logging.info(upper_bond) logging.info(y) ts=[] adata=[] anomalies=[] myshape = series.shape nrow = myshape[0] i=0 for i in range(nrow): isAnomaly = False if (not returnAnomaliesOnly): ts.append(series.index[i]) adata.append(series.iloc[i,0]) if bound==IS_UPPER_BOUND: if y[i] > upper_bond[i]: if returnAnomaliesOnly: ts.append(series.index[i]) adata.append(y[i]) isAnomaly = True elif bound==IS_LOWER_BOUND: if y[i] < lower_bond[i]: if returnAnomaliesOnly: ts.append(series.index[i]) adata.append(y[i]) isAnomaly = True else: if y[i] > upper_bond[i] or y[i] < lower_bond[i]: if returnAnomaliesOnly: ts.append(series.index[i]) adata.append(y[i]) isAnomaly = True if returnAnomaliesOnly: if isAnomaly: anomalies.append(True) else: anomalies.append(isAnomaly) #return mae, deviation, addHeader(ts,adata) if calculateScore: return mae, deviation, ts, adata, anomalies, mape return mae, deviation, ts, adata, anomalies
def trainDoubleExponentialSmoothingParameters(y, alphas, betas): minmap = -1 alpha = 0 beta = 0 for a in alphas: for b in betas: yhat = double_exponential_smoothing(y, a, b) mape = mean_absolute_percentage_error(y, yhat) if (minmap == -1): minmap = mape alpha = a beta = b elif minmap > mape: minmap = mape alpha = a beta = b return alpha, beta
def calculateDoubleExponentialSmoothingParameters( series, alpha=0.95, beta=0.05,threshold = 2, calculateScore=False): y= series.y yhat = double_exponential_smoothing(y.values, alpha, beta) # calculate confidence intervals for smoothed values mae = mean_absolute_error(y.values, yhat) deviation = np.std(y.values - yhat) mape = 0 if calculateScore: mape = mean_absolute_percentage_error(y.values, yhat) logging.info("Mean absolute error is "+str(mae)+", Mean absolute percentage_error is "+str(mape)+"%") hlower_bound = yhat - (mae + threshold * deviation) hupper_bound = yhat + (mae + threshold * deviation) if calculateScore: return hlower_bound[len(hlower_bound)-1], hupper_bound[len(hupper_bound)-1], mape return hlower_bound[len(hlower_bound)-1], hupper_bound[len(hupper_bound)-1]