/
Lab 3.py
303 lines (185 loc) · 6.15 KB
/
Lab 3.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#!/usr/bin/env python
# coding: utf-8
# In[2]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# # LAB # 3 (Autocorrelation Function)
# # Using the Python program and using only the “numpy” and “matplotlib” library perform the following tasks:
# ## 1- The equation to estimate autocorrelation is given as: Note: k is the # of lags and T is # of samples.
# ## 2- Let suppose y vectors is given as y(t) = [3, 9, 27, 81,243]. Without use of python or any other computer program, manually calculate the 𝜏0, 𝜏1, 𝜏2, 𝜏3, 𝜏4. Number of lags is 5.
# ## 3- Create a white noise with zero mean and standard deviation of 1 and 1000 samples.
# In[3]:
m=0
o=1
W=o * np.random.randn(1000) + m
# ## 4- Write a python code to estimate Autocorrelation Function. Note: You need to use the equation given in the textbook.
# In[4]:
def autocorrelation(y):
'''It returns the autocorrelation'''
t=len(y)
k=range(0,t)
meany=np.mean(y)
tk=[]
for i in k:
numerator=0
denominator=0
for ti in range(i,t):
numerator+=((y[ti]-meany)*(y[ti-i]-meany))
denominator=np.sum((y-meany)**2)
tk.append(numerator/denominator)
return tk
# In[5]:
Y = [1, 2, 3, 4, 5]
autocorrelation(Y)
# In[6]:
yy = [3, 9, 27, 81,243]
autocorrelation(yy)
# ### a. Plot the ACF for the generated data in step 3. The ACF needs to be plotted using “stem” command.
# In[177]:
plt.rcParams["figure.figsize"] = (30,15)
plt.rcParams['axes.titlesize'] = 36
plt.rcParams['axes.labelsize'] = 30
plt.rcParams['xtick.labelsize']=40
plt.rcParams['ytick.labelsize']=40
plt.rcParams['legend.fontsize']=40
# In[178]:
x2=list()
for i in range(0,21):
x2.append(i)
x1=[-1,-2,-3,-4,-5,-6, -7, -8, -9, -10, -11, -12, -13, -14, -15, -16, -17, -18, -19, -20]
x=x1+x2
# In[179]:
ADFWN1=autocorrelation(W)[1:21]
ADFWN2=autocorrelation(W)[0:21]
ADFWN=ADFWN1+ADFWN2
plt.stem(x,ADFWN, use_line_collection=True)
plt.title('Autocorrelation formula for White noise',pad=20)
plt.ylabel('Autocorrelation')
plt.xlabel('Lag #')
plt.show()
# ### b. Plot both the generated WN in step 3 versus time and plot the histogram.
#
# In[180]:
plt.plot(W)
plt.title('White Noise through time',pad=40)
plt.xlabel('Time')
plt.ylabel('Generated White Noise')
plt.show()
plt.hist(W)
plt.title('White Noise Distribution',pad=40)
plt.xlabel('White Noise value')
plt.ylabel('Number of Observations')
plt.show()
# ### c. Write down your observations about the AFC plot, histogram and the time plot of the generated WN.
# ## 5- Load the time series dataset tute1.csv (from LAB#1)
# ### a. Using python code written in the previous step, plot the ACF for the “Sales” and “Sales” versus time next to each other. You can use subplot command.
# In[181]:
tute1=pd.read_csv('tute1.csv')
tute1.head()
# In[182]:
x2=list()
for i in range(0,21):
x2.append(i)
x1=[-1,-2,-3,-4,-5,-6, -7, -8, -9, -10, -11, -12, -13, -14, -15, -16, -17, -18, -19, -20]
x=x1+x2
# In[183]:
#ADFsales1
# In[184]:
#ADFsales2
# In[185]:
#!pip install statsmodels
from statsmodels.tsa.stattools import adfuller
ADFX= adfuller(tute1['Sales'])
print('ADF Statistic for Sales: %f' % ADFX[0])
print('p-value for Sales: %f' % ADFX[1])
print('Critical Values for Sales:')
for key, value in ADFX[4].items():
print('\t%s: %.3f' % (key, value))
ACFsaless=autocorrelation(tute1['Sales'])
ACFsales2=ACFsaless[0:21]
ACFsales1=ACFsaless[1:21]
ACFsales=ACFsales1+ACFsales2
plt.subplot(2,2,1)
plt.stem(x,ACFsales,use_line_collection=True)
plt.title('Autocorrelation for Sales',pad=20)
plt.ylabel('Autocorrelation')
plt.xlabel('Lag #')
plt.subplot(2,2,2)
plt.plot(tute1['Sales'])
plt.title('Sales Through Time',pad=10)
plt.xlabel('Time')
plt.ylabel('Sales value')
plt.tight_layout()
plt.show()
# ### b. Using python code written in the previous step, plot the ACF for the “AdBudget” and “AdBudegt” versus time next to each other. You can use subplot command.
# In[186]:
x2=list()
for i in range(0,21):
x2.append(i)
x1=[-1,-2,-3,-4,-5,-6, -7, -8, -9, -10, -11, -12, -13, -14, -15, -16, -17, -18, -19, -20]
x=x1+x2
# In[187]:
#ADFAdbug1
# In[188]:
#ADFAdbug2
# In[189]:
ADFX= adfuller(tute1['AdBudget'])
print('ADF Statistic for AdBudget: %f' % ADFX[0])
print('p-value for AdBudget: %f' % ADFX[1])
print('Critical Values for AdBudget:')
for key, value in ADFX[4].items():
print('\t%s: %.3f' % (key, value))
ACFAdbug=autocorrelation(tute1['AdBudget'])
ACFAdbug1=ACFAdbug[0:21]
ACFAdbug2=ACFAdbug[1:21]
ACFAdBudget=ACFAdbug2+ACFAdbug1
plt.subplot(2,2,1)
plt.stem(x,ACFAdBudget,use_line_collection=True)
plt.title('Autocorrelation for AdBudget',pad=20)
plt.ylabel('Autocorrelation')
plt.xlabel('Lag #')
plt.subplot(2,2,2)
plt.plot(tute1['AdBudget'])
plt.title('AdBudget Through Time',pad=10)
plt.xlabel('Time')
plt.ylabel('AdBudget value')
plt.tight_layout()
plt.show()
# ### c. Using python code written in the previous step, plot the ACF for the “GDP” and “GDP” versus time next to each other. You can use subplot command.
#
# In[190]:
x2=list()
for i in range(0,21):
x2.append(i)
x1=[-1,-2,-3,-4,-5,-6, -7, -8, -9, -10, -11, -12, -13, -14, -15, -16, -17, -18, -19, -20]
x=x1+x2
# In[191]:
ADFX= adfuller(tute1['GDP'])
print('ADF Statistic for GDP: %f' % ADFX[0])
print('p-value for GDP: %f' % ADFX[1])
print('Critical Values for GDP:')
for key, value in ADFX[4].items():
print('\t%s: %.3f' % (key, value))
ACFGDPS=autocorrelation(tute1['GDP'])
ACFGDP1=ACFGDPS[0:21]
ACFGDP2=ACFGDPS[1:21]
ACFGDP=ACFGDP2+ACFGDP1
plt.subplot(2,2,1)
plt.stem(x,ACFGDP,use_line_collection=True)
plt.title('Autocorrelation for GDP',pad=20)
plt.ylabel('Autocorrelation')
plt.xlabel('Lag #')
plt.subplot(2,2,2)
plt.plot(tute1['GDP'])
plt.title('GDP Through Time',pad=10)
plt.xlabel('Time')
plt.ylabel('GDP value')
plt.tight_layout()
plt.show()
# ### d. Run the ADF-test for part a , b, and c and display them next to ACF and time plot in each section.
#
# ### e. Write down your observations about the correlation between stationary and non- stationary time series (if there is any) and autocorrelation function?
#
# ### f. The number lags used for this question is 20.
# In[ ]: