-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquestion3.py
151 lines (132 loc) · 4.04 KB
/
question3.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
#%%
from statsmodels.tsa.arima.model import ARIMA
import pandas as pd
from statsmodels.graphics.tsaplots import pacf, acf
from statsmodels.tsa.stattools import adfuller, kpss
import math
import plotly.graph_objs as go
import numpy as np
def genPiAppxDigits(numdigits,appxAcc):
import numpy as np
from decimal import getcontext, Decimal
getcontext().prec = numdigits
mypi = (Decimal(4) * sum(-Decimal(k%4 - 2) / k for k in range(1, 2*appxAcc+1, 2)))
return mypi
numdigits = 1000
appxAcc = 10000
series = genPiAppxDigits(numdigits,appxAcc)
#convert and split data
df = list(str(series))
df.remove('.')
df_float = []
for item in df:
df_float.append(float(item))
size = int(len(df) * 0.75)
train, test = df[0:size], df[size:]
#calculate ACF and PACF
df_pacf = pacf(df)
df_acf = acf(df)
lower95 = -1.96/math.sqrt(len(df))
upper95 = -lower95
#visualize PACF and ACF to determine order for ARIMA model
fig2= go.Figure()
fig2.add_trace(go.Bar(
x= np.arange(len(df_acf)),
y= df_acf,
name= 'ACF',
width=[0.2]*len(df_acf),
showlegend=False
))
fig2.add_hrect(y0=lower95, y1=upper95,line_width=0,fillcolor='green',opacity=0.1)
fig2.add_trace(go.Scatter(
mode='markers',
x=np.arange(len(df_acf)),
y= df_acf,
marker=dict(color='blue',size=8),
showlegend=False
))
fig2.update_layout(
title="Autocorrelation",
xaxis_title="Lag",
yaxis_title="Autocorrelation",
height=500,
)
fig3 = go.Figure()
fig3.add_trace(go.Bar(
x= np.arange(len(df_pacf)),
y= df_pacf,
name= 'PACF',
width=[0.2]*len(df_pacf),
showlegend=False
))
fig3.add_hrect(y0=lower95, y1=upper95,line_width=0,fillcolor='green',opacity=0.1)
fig3.add_trace(go.Scatter(
mode='markers',
x=np.arange(len(df_pacf)),
y= df_pacf,
marker=dict(color='blue',size=8),
showlegend=False
))
fig3.update_layout(
title="Partial Autocorrelation",
xaxis_title="Lag",
yaxis_title="Partial Autocorrelation",
height=500,
)
fig2.show()
fig3.show()
#define function to create ARIMA model and record predictions/residuals
def CreatePredictions(p,q,d,test,train):
predictions = list()
for t in range(len(test)):
model = ARIMA(train, order=(p,q,d))
model_fit = model.fit()
output = model_fit.forecast()
residuals = pd.DataFrame(model_fit.resid)
yhat = output[0]
predictions.append(yhat)
try:
obs = test.iloc[t]
train = train.append(obs)
except:
pass
pred = pd.DataFrame([test,predictions])
residuals = residuals.iloc[1:]
return pred, residuals
#convert lists of strings to floats
test_float = []
for item in test:
test_float.append(float(item))
train_float = []
for item in train:
train_float.append(float(item))
pred, residuals = CreatePredictions(1,1,0,test_float,train_float)
#visualize prediction against actual digits of pi
fig4 = go.Figure()
fig4.add_trace(go.Scatter(
x= np.arange(len(pred.T)),
y= pred.T[0],
name= 'Digits of pi'))
fig4.add_trace(go.Line(x=np.arange(len(pred.T)),y=pred.T[1],name='Prediction'))
fig4.show()
#determine accuracy based on the number of correct predictions
difference_in_predictions = round(pred.T[0]-pred.T[1],3)
difference_in_predictions[difference_in_predictions==0]
print('''Since the prediction is a constant, there is no predictive power and pi is irrational since by
definition irrational numbers require an infinite number of digits to write. There is no discerable pattern
which the digits follow. The accuracy here is 0 since there are no correct predictions''')
# %%
#bonus to calculate the pairwise correlation for each series of various accuracy
list_of_appxAcc = [1000,5000,10000,50000,100000]
df_series = []
for i in list_of_appxAcc:
series = genPiAppxDigits(numdigits, i)
df = list(str(series))
df.remove('.')
df_float = []
for item in df:
df_float.append(float(item))
df_series.append(df_float)
correlation_matrix = pd.DataFrame(df_series).T.corr()
print('''For each time series there is no correlation (less than 0.1)''')
# %%