def GCV(Model, Y, lmbda, beta): N = len(Y) X = model2matix(Model) X = np.transpose(np.array(X)) Y_hat = leastsquare.predict(X, beta) rss = np.sum((Y-Y_hat)**2) denominator = (1-float(lmbda)/N)**2 return rss/denominator
def removed_rss(Model, index, Y): X = model2matix(Model) del X[index] X = np.transpose(np.array(X)) if len(X) <= 1: return 999999999., None beta = leastsquare.ls(X, Y) Y_hat = leastsquare.predict(X, beta) rss = np.sum((Y-Y_hat)**2) return rss, beta
def draw_polynomial_pwv(train_in, train_out): p = 3 X = polynomial(train_in) XT = np.transpose(X) beta = ls.ls(X, train_out) Yhat = ls.predict(X, beta) thegsq = ls.thegama2(Yhat, train_out, p) beta_var = np.linalg.inv(XT.dot(X))*thegsq print beta_var PWV = [] for i in range(len(X)): PWV.append(np.transpose(X[i]).dot(beta_var).dot(X[i])) #print PWV plt.plot(train_in, PWV)
def draw_cubic_spline_pwv(train_in, train_out): p = 5 X = cubic(train_in) #print X.shape XT = np.transpose(X) #print X beta = ls.ls(X, train_out) Yhat = ls.predict(X, beta) thegsq = ls.thegama2(Yhat, train_out, p) beta_var = np.linalg.inv(XT.dot(X))*thegsq print beta_var PWV = [] for i in range(len(X)): PWV.append(np.transpose(X[i]).dot(beta_var).dot(X[i])) #print PWV plt.plot(train_in, PWV)
def draw_natural_spline_pwv(train_in, train_out): p = 5 knots = np.arange(0.1, 0.9, 0.16).tolist() knots.append(0.9) X = nscookdata(train_in, 6, knots) #print H #print H.shape XT = np.transpose(X) beta = ls.ls(X, train_out) Yhat = ls.predict(X, beta) thegsq = ls.thegama2(Yhat, train_out, p) beta_var = np.linalg.inv(XT.dot(X))*thegsq print beta_var PWV = [] for i in range(len(X)): PWV.append(np.transpose(X[i]).dot(beta_var).dot(X[i])) #print PWV plt.plot(train_in, PWV)