-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.py
165 lines (125 loc) · 4.63 KB
/
script.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
import pandas as pd
import pandas
import numpy as np
#provide local path
testfile='../input/test.csv'
data = open(testfile).readlines()
sequences={} #(key, value) = (id , sequence)
for i in range(1,len(data)):
line=data[i]
line =line.replace('"','')
line = line[:-1].split(',')
id = int(line[0])
sequence=[int(x) for x in line[1:]];
sequences[id]=sequence
# In[ ]:
def checkRecurrence(seq, order= 2, minlength = 7):
"""
:type seq: List[int]
:type order: int
:type minlength: int
:rtype: List[int]
Check whether the input sequence is a recurrence sequence with given order.
If it is, return the coefficients for the recurrenec relation.
If not, return None.
"""
if len(seq)< max((2*order+1), minlength):
return None
################ Set up the system of equations
A,b = [], []
for i in range(order):
A.append(seq[i:i+order])
b.append(seq[i+order])
A,b =np.array(A), np.array(b)
try:
if np.linalg.det(A)==0:
return None
except TypeError:
return None
############# Solve for the coefficients (c0, c1, c2, ...)
coeffs = np.linalg.inv(A).dot(b)
############ Check if the next terms satisfy recurrence relation
for i in range(2*order, len(seq)):
predict = np.sum(coeffs*np.array(seq[i-order:i]))
if abs(predict-seq[i])>10**(-2):
return None
return list(coeffs)
def predictNextTerm(seq, coeffs):
"""
:type seq: List[int]
:type coeffs: List[int]
:rtype: int
Given a sequence and coefficienes, compute the next term for the sequence.
"""
order = len(coeffs)
predict = np.sum(coeffs*np.array(seq[-order:]))
return int(round(predict))
# ## Example: ##
# * Given a sequence [1,5,11,21,39,73,139,269,527].
# * We verify if it's 3rd order recurrence sequence and find the coefficients (2,-5,4).
# * We then predict the next term using the last 3 terms and the relation $a_{n+3} = 2a_{n}-5a_{n+1}+4a_{n+2}$.
# In[ ]:
seq = [1,5,11,21,39,73,139,269,527]
print (checkRecurrence(seq,3))
print (predictNextTerm(seq, [2,-5,4]))
# # Find 2nd order sequeneces in the test set #
# In[ ]:
order2Seq={} #(key, value) = (sequence id, [prediction, coefficients])
for id in sequences:
seq = sequences[id]
coeff = checkRecurrence(seq,2)
if coeff!=None:
predict = predictNextTerm(seq, coeff)
order2Seq[id]=(predict,coeff)
print ("We found %d sequences\n" %len(order2Seq))
print ("Some examples\n")
print ("ID, Prediction, Coefficients")
for key in sorted(order2Seq)[0:5]:
value = order2Seq[key]
print ("%s, %s, %s" %(key, value[0], [int(round(x)) for x in value[1]]))
# # Find 3rd order sequeneces in the test set #
# In[ ]:
order3Seq={}
for id in sequences:
if id in order2Seq:
continue
seq = sequences[id]
coeff = checkRecurrence(seq,3)
if coeff!=None:
predict = predictNextTerm(seq, coeff)
order3Seq[id]=(predict,coeff)
print ("We found %d sequences\n" %len(order3Seq))
print ("Some examples\n")
print ("ID, Prediction, Coefficients")
for key in sorted(order3Seq)[0:5]:
value = order3Seq[key]
print ("%s, %s, %s" %(key, value[0], [int(round(x)) for x in value[1]]))
# # Find 4th order sequeneces in the test set #
# In[ ]:
order4Seq={}
for id in sequences:
if id in order2Seq or id in order3Seq:
continue
seq = sequences[id]
coeff = checkRecurrence(seq,4)
if coeff!=None:
predict = predictNextTerm(seq, coeff)
order4Seq[id]=(predict,coeff)
print ("We found %d sequences \n" %len(order4Seq))
print ("Some examples\n")
print ("ID, Prediction, Coefficients")
for key in sorted(order4Seq)[4:5]:
value = order4Seq[key]
print ("%s, %s, %s" %(key, value[0], [int(round(x)) for x in value[1]]))
print (sequences[239][0:17])
# ## Recurrence relations not included in OEIS ##
# In the previous cells,
# * We find that Sequence 239 is a 4th order sequence and predict the next term as 5662052980.
# * We check OEIS https://oeis.org/A000773, which confirms the prediction is correct.
# * We observe that this recurrence relation is not described in OEIS. (There are more such sequences.)
# In[ ]:
print("Conclusion:")
print("Number of sequences in the test set:", len(sequences))
print("Number of 2nd order sequences:", len(order2Seq))
print("Number of 3rd order sequences:", len(order3Seq))
print("Number of 4th order sequences:", len(order4Seq))