def main(alpha, beta, gama): series = read_csv('/home/akshit/Documents/Downloads/2.csv', header=0, index_col=0, squeeze=True) series.columns = ['a', 'b', 'c', 'd'] series['e'] = pow((series['a'] * series['a'] + series['b'] * series['b'] + series['c'] * series['c']), 0.5) df = pd.DataFrame({'$a': series['e']}) fd = np.array(df) plt.plot(fd) model = ExponentialSmoothing(fd) alpha = float(alpha) beta = float(beta) gama = float(gama) a = ExponentialSmoothing.fit(model, smoothing_level=alpha, smoothing_slope=beta, use_boxcox=True, remove_bias=True, smoothing_seasonal=gama) c = HoltWintersResults.predict(a, start=0) plt.plot(c) plt.show()
def main(alpha, beta, gama): abspath = os.path.abspath(os.curdir) series = read_csv(abspath + '/2.csv', header=0, index_col=0, squeeze=True) series.columns = ['a', 'b', 'c', 'd'] series['e'] = pow((series['a'] * series['a'] + series['b'] * series['b'] + series['c'] * series['c']), 0.5) df = pd.DataFrame({'$a': series['e']}) fd = np.array(df) plt.plot(fd) model = ExponentialSmoothing(fd) alpha = float(alpha) beta = float(beta) gama = float(gama) a = ExponentialSmoothing.fit(model, smoothing_level=alpha, smoothing_slope=beta, use_boxcox=True, remove_bias=True, smoothing_seasonal=gama) c = HoltWintersResults.predict(a, start=0) plt.plot(c) plt.xlabel('Data', fontsize=12) plt.ylabel('Indices', fontsize=12) plt.show()
def score(self, ts: TestTS, model: HoltWintersResults): predicted_values = model.predict(ts.start, ts.end) return { 'MAE': find_mae(ts.data.values, predicted_values.values), "MAPE": find_mape(ts.data.values, predicted_values.values), 'AIC': model.aic, 'AICC': model.aicc, 'BIC': model.bic, 'SSE': model.sse }
def brutlag_algorithm(ts: TimeSeries, model: HoltWintersResults, tolerance: int = 0.5, period=12, sf=2, gamma=0.3684211): LOG.warning( 'This function in future release will be refactored. ' 'Hence use this with care expecting breaking changes in upcoming versions.' ) LOG.info('Performing brutlag anomaly detection ...') # period - seasonal_period of the given time series # gamma - the seasonality component (0.2 -0.5) # sf - brutlag scaling factor for the confidence bands. (2-3) ub = [] # upper bound or upper confidence band lb = [] # lower bound or lower confidence band prediction = model.predict(ts.start, ts.end) difference_array = [] dt = [] difference_table = { "actual": ts.data, "predicted": prediction, "difference": difference_array, "UB": ub, "LB": lb } """Calculation of confidence bands using brutlag algorithm""" for i in range(len(prediction)): diff = ts.data[i] - prediction[i] if i < period: dt.append(gamma * abs(diff)) else: dt.append(gamma * abs(diff) + (1 - gamma) * dt[i - period]) difference_array.append(diff) ub.append(prediction[i] + sf * dt[i]) lb.append(prediction[i] - sf * dt[i]) print("\nDifference between actual and predicted\n") difference = pd.DataFrame(difference_table) print(difference) """Classification of data points as either normal or anomaly""" normal = [] normal_date = [] anomaly = [] anomaly_date = [] actual = [] for i in range(len(ts.data.index)): if (ub[i] <= ts.data[i] or lb[i] >= ts.data[i] ) and i > period and abs(ts.data[i] - prediction[i]) > tolerance: anomaly_date.append(ts.data.index[i]) anomaly.append(ts.data[i]) actual.append(prediction[i]) else: normal_date.append(ts.data.index[i]) normal.append(ts.data[i]) anomaly = pd.DataFrame({ "date": anomaly_date, "observed": anomaly, 'expected': actual }) anomaly.set_index('date', inplace=True) normal = pd.DataFrame({"date": normal_date, "value": normal}) normal.set_index('date', inplace=True) print("\nThe data points classified as anomaly\n") print(anomaly) plot_anomaly(ts, anomaly, normal, lb, ub) return anomaly
def predict(self, model: HoltWintersResults, start, end): return model.predict(start, end)
plt.show() plt.plot(df_test, predictions,'+') plt.xlabel('Original Values') plt.ylabel('Predicted Values') plt.title('Predicted vs. Original Values') plt.show() print(len(df_train)) #--------------------------------------------------TASK 3---------------------------------------------------# a_arr = [] rmse_arr = [] for a in np.arange(0.1, 1.0, 0.1): a_arr.append(a) ema_model = SimpleExpSmoothing(df_train).fit(smoothing_level=a, optimized=False) result = HoltWintersResults.predict(ema_model,start=0) mse = mean_squared_error(df_train, result) rmse = np.sqrt(mean_squared_error(df_train, result)) rmse_arr.append(rmse) plt.plot(df_train,label='Original') plt.plot(result, color='red', label='Predictions') plt.legend(loc='best') plt.xlabel('t') plt.show() plt.plot(df_train[0:100], label='Original') plt.plot(result[0:100], color='red', label='Predictions') plt.legend(loc='best') plt.xlabel('t') plt.show() print('a:',a) print("RMSE:", rmse)