In [7]:
from numpy import *
from scipy import *
from sklearn import *
In [10]:
T0 = 100000
T1 = 100000
In [11]:
N = 50
alpha = 0.1
maxSVD = 0.9
In [51]:
def legendre(n, x):
if n==0:
return 1
elif n==1:
return x * (2*n+1)/2
elif n==2:
return (1.5*x*x-0.5) * (2*n+1)/2
elif n==3:
return (2.5*x*x*x-1.5*x) * (2*n+1)/2
In [79]:
a = loadtxt("input.txt")
b = loadtxt("output.txt")
In [80]:
figure()
plot(b[:1000,0])
plot(b[:1000,1])
plot(b[:1000,2])
plot(b[:1000,3])
Out[80]:
In [40]:
def calc_cap(ds):
y = zeros(T1, dtype=float)
for i in range(T1):
tmp = 1.0
for j in range(len(ds)):
tmp *= legendre(ds[j], a[T0+i-j])
y[i] = tmp
lin = linear_model.LinearRegression()
lin.fit(b[T0:T0+T1,:], y[:T1])
z = lin.predict(b[T0:,:])
mse = 0
yy = 0
for i in range(T1):
mse += (y[i] - z[i])**2
yy += y[i]**2
return 1.0 - mse/yy
In [81]:
memo = zeros(25, dtype=float)
for i in range(25):
ds = zeros(i+1)
ds[i] = 1
memo[i] = calc_cap(ds)
print i, memo[i]
In [24]:
clf()
plot(memo)
Out[24]:
In [72]:
memo2 = zeros(100, dtype=float)
cnt = 0
for i in range(10):
for j in range(i, 10):
ds = zeros(10)
ds[i] += 1
ds[j] += 1
memo2[cnt] = calc_cap(ds)
print i, j, memo2[cnt], ds
cnt+=1
In [68]:
memo3 = zeros(100, dtype=float)
cnt = 0
for i in range(10):
for j in range(i, 10):
for k in range(j, 10):
ds = zeros(10)
ds[i] += 1
ds[j] += 1
ds[k] += 1
memo3[cnt] = calc_cap(ds)
print memo3[cnt], ds
cnt+=1
In [57]:
print sum(memo[memo>0])
#print sum(memo2[memo2>0])
print sum(memo3[memo3>0])
In [42]:
Out[42]:
In [ ]: