def calculateMovingAverageParameters(series, window=0, threshold=2.0, calculateScore=False): if window == 0: window = len(series) rolling_mean = series.rolling(window=window).mean() mae = mean_absolute_error(series.iloc[window - 1:, :], rolling_mean.iloc[window - 1:, :]) deviation = np.std(series.iloc[window - 1:, :] - rolling_mean.iloc[window - 1:, :]) if (calculateScore): mape = mean_absolute_percentage_error( series.iloc[window - 1:, :], rolling_mean.iloc[window - 1:, :]) logging.info("Mean absolute error is " + str(mae) + ", Mean absolute percentage_error is " + str(mape) + "%") llower_bound = rolling_mean - (mae + threshold * deviation) hupper_bound = rolling_mean + (mae + threshold * deviation) mean_upper, std_upper = calculateHistoricalParameters(hupper_bound) mean_lower, std_lower = calculateHistoricalParameters(llower_bound) #add other models for different use case other_models = {} other_models[MEAN_UPPER] = mean_upper other_models[MEAN_LOWER] = mean_lower other_models[STD_UPPER] = std_upper other_models[STD_LOWER] = std_lower other_models[PREDIT_UPPERS] = llower_bound other_models[PREDIT_LOWERS] = hupper_bound # return hlower_bound.y.values[len(llower_bound)-1], hupper_bound.y.values[len(hupper_bound)-1], other_models return llower_bound.y.values[len(llower_bound) - 1], hupper_bound.y.values[len(hupper_bound) - 1] '''
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 trainMovingAverageParameters(series, windowlist, calculateScore=True ): minmape = -1 windowno = 0 for window in windowlist: rolling_mean = series.rolling(window=window).mean() mape = mean_absolute_percentage_error(series.iloc[window-1:,:], rolling_mean.iloc[window-1:,:]) if (minmape == -1): minmape = mape windowno = window elif (minmape> mape): minmape = mape windowno = window return windowno
def calculateExponentialSmoothingParameters(series, alpha=0.9, threshold=2, calculateScore=False): y= series.y yhat = exponential_smoothing(y.values, alpha) # 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]
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 calculateMovingAverageParameters(series, window=0, threshold=2.0, calculateScore=False): if window == 0: window = len(series) rolling_mean = series.rolling(window=window).mean() mae = mean_absolute_error(series.iloc[window - 1:, :], rolling_mean.iloc[window - 1:, :]) deviation = np.std(series.iloc[window - 1:, :] - rolling_mean.iloc[window - 1:, :]) if (calculateScore): mape = mean_absolute_percentage_error( series.iloc[window - 1:, :], rolling_mean.iloc[window - 1:, :]) logging.info("Mean absolute error is " + str(mae) + ", Mean absolute percentage_error is " + str(mape) + "%") hlower_bound = rolling_mean - (mae + threshold * deviation) hupper_bound = rolling_mean + (mae + threshold * deviation) return hlower_bound.y.values[len(hlower_bound) - 1], hupper_bound.y.values[len(hupper_bound) - 1] '''
def retrieveHW_MAPE(y, model): error = mean_absolute_percentage_error(y.values, model.result[:len(y)]) return error