-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
81 lines (73 loc) · 2.1 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
import os
import numpy as np
# Quality functions
def qualitySSE(x,y):
# Sum squared error
# x,y - pandas structures
# x - real values
# y - forecasts
return ((x-y)**2).sum(), (x-y)**2
def qualityMSE(x,y):
# Mean squared error
# x,y - pandas structures
# x - real values
# y - forecasts
return ((x-y)**2).mean() , (x-y)**2
def qualityRMSE(x,y):
# Root mean squared error
# x,y - pandas structures
# x - real values
# y - forecasts
return (((x-y)**2).mean())**(0.5) , (x-y)**2
def qualityMAE(x,y):
# Mean absolute error
# x,y - pandas structures
# x - real values
# y - forecasts
return (x-y).abs().mean(), (x-y).abs()
def qualityMAPE(x,y):
# Mean absolute percentage error
# x,y - pandas structures
# x - real values
# y - forecasts
qlt = ((x-y).abs()/x).replace([np.inf, -np.inf], np.nan)
return qlt.mean() , (x-y).abs()
def qualityMACAPE(x,y):
# Mean average corrected absolute percentage error
# x,y - pandas structures
# x - real values
# y - forecasts
qlt = (2*(x-y).abs()/(x+y)).replace([np.inf, -np.inf], np.nan)
return qlt.mean() , (x-y).abs()
def qualityMedianAE(x,y):
# Median absolute error
# x,y - pandas structures
# x - real values
# y - forecasts
return ((x-y).abs()).median(), (x-y).abs()
def ExponentialSmoothing(x, h, Params):
T = len(x)
alpha = Params['alpha']
AdaptationPeriod=Params['AdaptationPeriod']
FORECAST = [np.NaN]*(T+h)
if alpha>1:
w.warn('Alpha can not be more than 1')
#alpha = 1
return FORECAST
if alpha<0:
w.warn('Alpha can not be less than 0')
#alpha = 0
return FORECAST
y = x[0]
t0=0
for t in range(0, T):
if not math.isnan(x[t]):
if math.isnan(y):
y=x[t]
t0=t
if (t-t0+1)<AdaptationPeriod:
y = y*(1-alpha*(t-t0+1)/(AdaptationPeriod)) + alpha*(t-t0+1)/(AdaptationPeriod)*x[t]
y = y*(1-alpha) + alpha*x[t]
#else do not nothing
FORECAST[t+h] = y
return FORECAST