-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcomparing3models.py
197 lines (151 loc) · 5.09 KB
/
comparing3models.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# -*- coding: utf-8 -*-
"""
Created on Tue Apr 03 16:49:59 2018
Comparing EWMA, WCMA and QLSEP using the optimum parameters
- Plot MAPE of each day?
@author: zckoh
"""
import numpy as np
import matplotlib.pyplot as plt
import itertools
execfile("./QLSEP_class.py")
np.set_printoptions(threshold=np.nan)
def sum_list(d,D,y,myList = [], *args):
sum = 0
for x in range(1,D+1):
sum += float(myList[d-x][y])
return sum
#Get the original 48 slots data
slot = 30
day_counter = 1
lux_original = []
lux = []
tmp = []
with open("./NREL_data/20160901.csv", 'r') as f:
fifthlines = itertools.islice(f, 0, None, slot)
for lines in fifthlines:
tmp.append(lines.split(',')[2])
if(day_counter == (1440/slot)):
day_counter = 0
tmp = [w.replace('\n', '') for w in tmp]
lux_original.append([float(i) for i in tmp])
tmp = []
day_counter += 1
days = len(lux_original)
"""EWMA"""
a=QLSEP_node(22,0.1,2,slot,days,2)
for x in range(0,days):
for y in range(0,(1440/slot)):
a.EWMA(x,y,lux_original[x-1][y])
time48 = np.linspace(0,1440,num=1440/slot)
index = 78
"""WCMA"""
D = 4 #past days
K = 3 #past samples
alpha_WCMA = 0.7
#obtain P
P = []
for i in range(K):
P.append(float(i+1)/K)
M = np.array([[float(0)]*(1440/slot)]*days)
pre_WCMA = np.array([[float(0)]*(1440/slot)]*days)
GAP = np.array([[float(0)]*(1440/slot)]*days)
V = np.array([[[float(0)]*K]*(1440/slot)]*days)
for x in range(0,days):
for y in range(0,(1440/slot)):
M[x][y] = sum_list(x,D,y,lux_original)/D
for k in range(0,K):
V[x][y][k] = safe_div(float(lux_original[x][y-K+k]),M[x][y-K+k])
GAP[x][y] = np.dot(V[x][y],P)/np.sum(P)
pre_WCMA[x][y] = alpha_WCMA*float(lux_original[x][y-1]) + (1-alpha_WCMA)*GAP[x][y]*(M[x][y])
"""QL-SEP"""
node1 = QLSEP_node(0.001,0.1,3,slot,days,50)
for x in range(0,days):
for y in range(0,1440/slot):
node1.EWMA(x,y,lux_original[x-1][y])
node1.Calculate_PER(x,y,lux_original[x][y-1],(np.amax(lux_original[x])*0.03))
node1.Q_val_update(x,y)
node1.QLSEP_prediction(x,y)
"""Remove first day"""
pre_WCMA = pre_WCMA[1:]
lux_original = lux_original[1:]
a.EWMA_val = a.EWMA_val[1:]
node1.QLSEP_val = node1.QLSEP_val[1:]
days = days-1
"""calculate the MAPE"""
[mape_EWMA,no_EWMA] = MAPE_overall(lux_original,a.EWMA_val,days)
print "MAPE = %s%% , N = %s (using EWMA)" % (mape_EWMA,no_EWMA)
[mape_WCMA,no_WCMA] = MAPE_overall(lux_original,pre_WCMA,days)
print "MAPE = %s%% , N = %s (using WCMA)" % (mape_WCMA,no_WCMA)
[mape_QL,no_QL] = MAPE_overall(lux_original,node1.QLSEP_val,days)
print "MAPE = %s%% , N = %s (using QL-SEP)" % (mape_QL,no_QL)
EWMA_lst = []
WCMA_lst = []
QLSEP_lst = []
for d in range(days):
EWMA_lst.append(MAPE_oneday(lux_original[d],a.EWMA_val[d]))
WCMA_lst.append(MAPE_oneday(lux_original[d],pre_WCMA[d]))
QLSEP_lst.append(MAPE_oneday(lux_original[d],node1.QLSEP_val[d]))
time_day = np.linspace(1,90, num = days)
print EWMA_lst[76]
plt.figure(1)
fig, ax = plt.subplots(figsize=(7.5,4))
ax.plot(time_day,EWMA_lst,'g',label = 'EWMA')
ax.plot(time_day ,WCMA_lst,'r',label = 'WCMA')
ax.plot(time_day ,QLSEP_lst,'b',label = 'QLSEP')
legend = ax.legend(loc='upper right', shadow=True)
frame = legend.get_frame()
frame.set_facecolor('1.0')
for label in legend.get_texts():
label.set_fontsize('medium')
for label in legend.get_lines():
label.set_linewidth(1.5) # the legend line width
plt.xlabel('Day')
plt.ylabel('MAPE (%)')
plt.title('90 days NREL data')
plt.grid()
plt.savefig('mape_vs_days.png', dpi = 200)
plt.show()
"""Huge Spike"""
plt.figure(1)
fig, ax = plt.subplots(figsize=(7.5,4))
ax.plot(time48,lux_original[77],'g',label = 'Actual (48 slots)')
ax.plot(time48 ,a.EWMA_val[77],'r',label = 'EWMA')
ax.plot(time48 ,node1.QLSEP_val[76],'b',label = 'QLSEP')
legend = ax.legend(loc='upper right', shadow=True)
frame = legend.get_frame()
frame.set_facecolor('1.0')
for label in legend.get_texts():
label.set_fontsize('medium')
for label in legend.get_lines():
label.set_linewidth(1.5) # the legend line width
plt.xlabel('Time(Min)')
plt.ylabel('Light Intensity (klux)')
plt.title('Box day %s (NREL data)' % str(index+1))
plt.grid()
plt.show()
print EWMA_lst[index-1]
print WCMA_lst[index-1]
print QLSEP_lst[index-1]
print EWMA_lst[index]
print WCMA_lst[index]
print QLSEP_lst[index]
plt.figure(1)
fig, ax = plt.subplots(figsize=(7.5,4))
ax.plot(time48,lux_original[index],'g',label = 'Actual (48 slots)')
ax.plot(time48 ,a.EWMA_val[index],'r',label = 'EWMA')
ax.plot(time48 ,pre_WCMA[index],'orange',label = 'WCMA')
ax.plot(time48 ,node1.QLSEP_val[index],'b',label = 'QLSEP')
legend = ax.legend(loc='upper right', shadow=True)
frame = legend.get_frame()
frame.set_facecolor('1.0')
for label in legend.get_texts():
label.set_fontsize('medium')
for label in legend.get_lines():
label.set_linewidth(1.5) # the legend line width
plt.xlabel('Time(Min)')
plt.ylabel('Light Intensity (klux)')
plt.title('Day %s (NREL data)' % str(index+1))
plt.grid()
plt.savefig('profiles_day79.png', dpi = 200)
plt.show()