%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
n=100
x = np.arange(1,n+1,1)
x = x/n
y = 2.3 + 20*x + 100*x**2 + 100*x**3 +np.random.randn(n)*10
plt.plot(x,y)
A = np.vander(x,increasing=True)[:,:4]
plt.imshow(A,aspect="auto")
U, s, Vs = np.linalg.svd(A, full_matrices=False)
V = Vs.T
U.shape,s.shape,Vs.shape
plt.imshow(U,aspect="auto")
plt.imshow(V,aspect="auto")
plt.imshow(U.T.dot(U))
plt.imshow(U.dot(U.T))
U_y = U.T.dot(y)
U_y_div_sigma = U_y*1./s
c = V.dot(U_y_div_sigma)
plt.plot(c)
plt.plot(A.dot(c))
n=200
x_ = np.arange(1,n+1,1)
x_ = x_/n
x_ = x_[np.logical_and(x_>=x.min(), x_<=x.max())]
A_ = np.vander(x_,increasing=True)
A_ = A_[:,:len(c)]
plt.imshow(A_,aspect="auto")
plt.plot(x_,A_.dot(c))
plt.plot(x,y,"o")