Hey Jordan,
I’m just trying to use the polyfit function, but I’m not getting the results i’m expecting
Any ideas before I try to step into the frying pan?
x = [1552920192, 1552938624, 1552961664] # datetimes in unix timestamp
y = [12.85043621, 12.4644804, 11.92448521]
a,b1,b2,b3 = shared.math.polyfit(x,y,order=3,stringReturn=False)
print r
def y(x, a,b1,b2,b3):
return b3*x**3 + b2*x**2 + b1*x + a
print y(1552920192, a,b1,b2,b3)
>>>
[10608.0, -4.2691826820373535e-06, 6.775829897165408e-15, -5.4057545500047475e-24]
74.2827997627 ## This should return ~12.85043621
This should return approximately y[0]
FYI I have removed your line that was rounding the coefficients in lines 349-360ish:
def polyfit(x,y,order=2,stringReturn=False):
assert(len(x)==len(y)), "Number of X ordinates do not match number of Y ordinates"
assert(order > 0 and order < 6), "Order must be between 1 and 5"
order += 1
sumX=shared.math.sum(*x)
x2=[]
for i in range(len(x)):
x2.append(x[i] * x[i])
sumX2=shared.math.sum(*x2)
x3=[]
for i in range(len(x)):
x3.append(x[i] * x2[i])
sumX3=shared.math.sum(*x3)
x4=[]
for i in range(len(x)):
x4.append(x2[i] * x2[i])
sumX4=shared.math.sum(*x4)
x5=[]
for i in range(len(x)):
x5.append(x2[i] * x3[i])
sumX5=shared.math.sum(*x5)
x6=[]
for i in range(len(x)):
x6.append(x3[i] * x3[i])
sumX6=shared.math.sum(*x6)
x7=[]
for i in range(len(x)):
x7.append(x3[i] * x4[i])
sumX7=shared.math.sum(*x7)
x8=[]
for i in range(len(x)):
x8.append(x4[i] * x4[i])
sumX8=shared.math.sum(*x8)
x9=[]
for i in range(len(x)):
x9.append(x4[i] * x5[i])
sumX9=shared.math.sum(*x9)
x10=[]
for i in range(len(x)):
x10.append(x5[i] * x5[i])
sumX10=shared.math.sum(*x10)
sumY=shared.math.sum(*y)
xy=[]
for i in range(len(x)):
xy.append(x[i] * y[i])
sumXY=shared.math.sum(*xy)
x2y=[]
for i in range(len(x)):
x2y.append(x2[i] * y[i])
sumX2Y=shared.math.sum(*x2y)
x3y=[]
for i in range(len(x)):
x3y.append(x3[i] * y[i])
sumX3Y=shared.math.sum(*x3y)
x4y=[]
for i in range(len(x)):
x4y.append(x4[i] * y[i])
sumX4Y=shared.math.sum(*x4y)
x5y=[]
for i in range(len(x)):
x5y.append(x5[i] * y[i])
sumX5Y=shared.math.sum(*x5y)
matrix=[]
matrix.append([float(len(x)), sumX, sumX2, sumX3, sumX4, sumX5])
matrix.append([ sumX, sumX2, sumX3, sumX4, sumX5, sumX6])
matrix.append([sumX2, sumX3, sumX4, sumX5, sumX6, sumX7])
matrix.append([sumX3, sumX4, sumX5, sumX6, sumX7, sumX8])
matrix.append([sumX4, sumX5, sumX6, sumX7, sumX8, sumX9])
matrix.append([sumX5, sumX6, sumX7, sumX8, sumX9, sumX10])
vector=[sumY, sumXY, sumX2Y, sumX3Y, sumX4Y, sumX5Y]
inverse=[]
inverse.append([1.0,0.0,0.0,0.0,0.0,0.0])
inverse.append([0.0,1.0,0.0,0.0,0.0,0.0])
inverse.append([0.0,0.0,1.0,0.0,0.0,0.0])
inverse.append([0.0,0.0,0.0,1.0,0.0,0.0])
inverse.append([0.0,0.0,0.0,0.0,1.0,0.0])
inverse.append([0.0,0.0,0.0,0.0,0.0,1.0])
del matrix[order:]
del vector[order:]
del inverse[order:]
for i in range(order):
del matrix[i][order:]
del inverse[i][order:]
assert(matrix[i][i]!=0), "Invalid Matrix"
x=matrix[0][0]
for i in range(order):
matrix[0][i] = matrix[0][i] / x
inverse[0][i] = inverse[0][i] / x
for z in range(1,order):
for i in range(z,order):
x=matrix[i][z-1]
for j in range(order):
matrix[i][j]=matrix[i][j]-x*matrix[z-1][j]
inverse[i][j]=inverse[i][j]-x*inverse[z-1][j]
y=matrix[z][z]
for j in range(order):
matrix[z][j]=matrix[z][j] / y
inverse[z][j]=inverse[z][j] / y
for z in range(order-1,0,-1):
for i in range(0,z):
x=matrix[i][z]
for j in range(order):
matrix[i][j]=matrix[i][j]-x*matrix[z][j]
inverse[i][j]=inverse[i][j]-x*inverse[z][j]
y=matrix[z][z]
for j in range(order):
matrix[z][j]=matrix[z][j] / y
inverse[z][j]=inverse[z][j] / y
coefficient=[]
for i in range(order):
x = 0.0
for j in range(order):
x += inverse[i][j]*vector[j]
# Note: removed rounding from this next line to make more accurate
coefficient.append(x)
if stringReturn==False:
return coefficient
else:
s=''
for c in range(len(coefficient)-1,-1,-1):
if coefficient[c] != 0:
if c > 1:
s+= "%+.2fx^" % coefficient[c]+str(c)
elif c>0:
s += "%+.2fx" % coefficient[c]
else:
s += "%+.2f" % coefficient[c]
return s