-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
92 lines (70 loc) · 2.8 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from functools import reduce
import os
import keras as ks
from sklearn.metrics import mean_absolute_error
import tensorflow as tf
from IPython.display import clear_output
from sklearn.utils import resample
from sklearn.metrics import mean_squared_error
from sklearn.preprocessing import MinMaxScaler
from statsmodels.tsa.api import SimpleExpSmoothing, Holt
from statsmodels.tsa.holtwinters import ExponentialSmoothing , HoltWintersResults
# function to implement the optimum "a" for exponential smoothing
# as described in the paper
def optimum_al(series):
opt_a =(series.max() - series.min() - series.mean() ) / (series.max() - series.min())
return opt_a
# function to pick the seasonal periods based on the frequency dataset selected
# this is in case someone uses the holt winters exponential smoothing method (capture seasonality,level,..)
def seasonals(freq_name):
if(freq_name == "daily"):
return 5
elif(freq_name == "weekly"):
return 4
elif(freq_name == "monthly"):
return 12
elif(freq_name == 'quarterly'):
return 4
else :
return 1
# exponential smooth with the optimum alpha calculated from the suggested
# formula , also it offers the option for wolt winters smoothing
# we tested it , it did not improve the results
def exponential_smooth(series,optimum_a,freq_name,Hw=False):
temp = np.zeros((series.shape[0],series.shape[1]))
for i,c in enumerate(series.columns):
if(Hw==False):
#--------------------------- simple smoothing --------------------------- #
sm = SimpleExpSmoothing(series[c], initialization_method="estimated").fit(
smoothing_level=optimum_a[c], optimized=False)
#-------------------------------------------------------------------------#
temp[:,i] = sm.fittedvalues
mod = sm
else :
hw = ExponentialSmoothing(
series[c], trend="add", seasonal="add"
, initialization_method='estimated',
seasonal_periods = seasonals(freq_name)
).fit(optimized=True)
temp[:,i] = hw.fittedvalues
mod = hw
# transform to dataframe again and return it
smoothed_series = pd.DataFrame(temp,index=series.index , columns = series.columns)
return mod,smoothed_series
# function to calculate frequency for dataframes
def frequencyCalc(freq_name):
if(freq_name == "daily"):
return 'B'
elif(freq_name == "weekly"):
return 'W'
elif(freq_name == "monthly"):
return 'M'
elif(freq_name == 'quarterly'):
return 'Q'
else :
return 'A'
if __name__ =="__main__":
pass