-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimeSeriesPrediction
More file actions
211 lines (171 loc) · 6.54 KB
/
Copy pathTimeSeriesPrediction
File metadata and controls
211 lines (171 loc) · 6.54 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
from sklearn import linear_model
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import csv
from pandas import Series
from sklearn.metrics import mean_squared_error
from statsmodels.tsa.api import ExponentialSmoothing, SimpleExpSmoothing, Holt
import os
from timeit import default_timer as timer
#df = pd.read_excel('Original.xlsx')
#input_var = input("Enter Values for TransmissionTime,UpdateTime,ConsecutiveMessageLost\nPlease separate them with ',': ")
#print ("you entered " + input_var)
TTVValue = 0.7
UTVValue = 0.5
CMLVValue = 0.3
global UpStateFunction
def MakeUpStateFunction():
global UpStateFunction
UpStateFunction = []
TT = []
UT = []
CML = []
TI = []
AbsTTTI = []
NTT=[]
NUT=[]
NCML=[]
with open('Original.csv','r',encoding="UTF8") as csvfile:
csvreader = csv.reader(csvfile,delimiter=',')
csvreader = csv.reader((x.replace('\0', '') for x in csvfile))
#str = str.unicode(str, errors='ignore')
next(csvreader, None)
for row in csvreader:
if(row):
TT.append(row[0])
UT.append(row[1])
CML.append(row[2])
TI.append(row[3])
AbsTTTI.append(np.absolute(float(row[1]) - float(row[3])))
x = np.asarray(TT, dtype='float64')
TTAlpha = min(TT)
TTBeta = np.percentile(x, 99)
UTAlpha = 0
UTBeta = np.var(AbsTTTI)
CMLAlpha = 0
CMLBeta = 3
#---------------------Normalize Transmission Time----------------------------------------------------------------
for i in range(0,len(x)):
if(x[i] <= float(TTAlpha)):
NTT.append(1)
if(x[i] > float(TTAlpha) and x[i] < float(TTBeta)):
NTT.append(1-(x[i]-float(TTAlpha))/(float(TTBeta)-float(TTAlpha)))
if(x[i] >= float(TTBeta)):
NTT.append(0)
#-----------------------Normalize Update Time---------------------------------------------------------------------
for i in range(0, len(AbsTTTI)):
if (AbsTTTI[i] <= float(UTAlpha)):
NUT.append(1)
if (AbsTTTI[i] > float(UTAlpha) and AbsTTTI[i] < float(UTBeta)):
NUT.append(1 - (AbsTTTI[i] - float(UTAlpha)) / (float(UTBeta) - float(UTAlpha)))
if (AbsTTTI[i] >= float(UTBeta)):
NUT.append(0)
#-------------------------Normalize Consecutive Message Lost--------------------------------------------------------
for i in range(0, len(CML)):
if (float(CML[i]) <= float(CMLAlpha)):
NCML.append(1)
if (float(CML[i]) > float(CMLAlpha) and float(CML[i]) < float(CMLBeta)):
NCML.append(1 - (float(CML[i]) - float(CMLAlpha)) / (float(CMLBeta) - float(CMLAlpha)))
if (float(CML[i]) >= float(CMLBeta)):
NCML.append(0)
#-------------------------Generating Up-State-Function----------------------------------------------------------------
for i in range(0,len(NTT)):
UpStateFunction.append(1/(TTVValue+UTVValue+CMLVValue)*((TTVValue*float(NTT[i]))+(UTVValue*float(NUT[i]))+(CMLVValue*float(NCML[i]))))
ls = []
st = 'Up-State-Value'
# st=st.encode('utf-8')
ls.append([st])
st = ''
with open('UpStateFunction.csv', 'w', encoding='UTF8', newline='') as csvfile:
wr = csv.writer(csvfile)
for k in range(0, len(UpStateFunction) - 1):
st = UpStateFunction[k]
ls.append([st])
wr.writerows(ls)
#-------------------------------------------------End of MakeUpStateFunction------------------------------------------------
def MovingAverageMethod():
global UpStateFunction
PredictionList = []
data = pd.read_csv('UpStateFunction.csv',sep='\t')
df = pd.DataFrame(data)
arr=df.rolling(window=3).mean()
for i in range(2,len(arr)):
PredictionList.append(arr.iloc[i][0])
ls = []
st = 'MovingAverageValue'
# st=st.encode('utf-8')
ls.append([st])
st = ''
with open('MovingAverageMethod.csv', 'w', encoding='UTF8', newline='') as csvfile:
wr = csv.writer(csvfile)
for k in range(0, len(arr)):
st = arr.iloc[k][0]
ls.append([st])
wr.writerows(ls)
expected = UpStateFunction
del expected[0]
del expected[1]
del expected[2]
mse = mean_squared_error(expected, PredictionList)
rmse = np.sqrt(mse)
print('RMSE of moving average method: %f' % rmse)
#-----------------------------------------------------------------------------------------------------------------------------
def MakeSimpleExponentialSmoothing():
# prepare data
data = pd.read_csv('UpStateFunction.csv', sep='\t')
# create class
model = SimpleExpSmoothing(data)
# fit model
model = model.fit(smoothing_level=0.3)
# make prediction
result = model.predict(0, len(data))
PredictionList = []
ls = []
st = 'SimpleExponencialSmoothingValue'
# st=st.encode('utf-8')
ls.append([st])
st = ''
with open('SimpleExponencialSmoothing.csv', 'w', encoding='UTF8', newline='') as csvfile:
wr = csv.writer(csvfile)
for k in range(0, len(result)-1):
st = result[k]
#print(st)
PredictionList.append(st)
ls.append([st])
wr.writerows(ls)
mse = mean_squared_error(data, PredictionList)
rmse = np.sqrt(mse)
print('RMSE of simple exponencial smoothing: %f' % rmse)
#------------------------------------------------------------------------------------------------------------------------------
def MakelinearExponentialSmoothing():
# prepare data
data = pd.read_csv('UpStateFunction.csv', sep='\t')
# create class
model = ExponentialSmoothing(data)
# fit model
model = model.fit()
# make prediction
result = model.predict(0, len(data))
PredictionList = []
ls = []
st = 'LinearExponencialSmoothingValue'
# st=st.encode('utf-8')
ls.append([st])
st = ''
with open('LinearExponencialSmoothing.csv', 'w', encoding='UTF8', newline='') as csvfile:
wr = csv.writer(csvfile)
for k in range(0, len(result) - 1):
st = result[k]
# print(st)
PredictionList.append(st)
ls.append([st])
wr.writerows(ls)
mse = mean_squared_error(data, PredictionList)
rmse = np.sqrt(mse)
print('RMSE of linear exponencial smoothing: %f' % rmse)
#------------------------------------------------------------------------------------------------------------------------------
MakeUpStateFunction()
MovingAverageMethod()
MakeSimpleExponentialSmoothing()
MakelinearExponentialSmoothing()