def test_graphnet_wts(X, Xlist, Y, l1=500.0, l2=2, l3=3.5, tol=1e-4): print "graphnet_wts", l1, l2, l3 n = len(Xlist) wts = np.random.normal(0, 1, n) Xlist2 = [Xlist[i] * wts[i] for i in range(n)] A, Afull = gen_adj(X.shape[1]) l = regreg.cwpath(regreg.graphnet_wts((Xlist, Y, A), rowweights=wts)) # initial_coefs = np.array([7.]*10)) l.problem.assign_penalty(l1=l1, l2=l2, l3=l3) l.fit(tol=tol, max_its=400) beta = l.problem.coefficients def f(beta): return ( np.linalg.norm(Y - np.dot(np.vstack(Xlist2), beta)) ** 2 / (2 * len(Y)) + np.fabs(beta).sum() * l1 + l2 * np.linalg.norm(beta) ** 2 / 2 + l3 * np.dot(beta, np.dot(Afull, beta)) / 2 ) v = scipy.optimize.fmin_powell(f, np.zeros(X.shape[1]), ftol=1.0e-10, xtol=1.0e-10, maxfun=100000) v = np.asarray(v) print np.round(100 * v) / 100, "\n", np.round(100 * beta) / 100 print f(beta), f(v) if f(v) < f(beta) + 1e-10: assert np.fabs(f(v) - f(beta)) / np.fabs(f(v) + f(beta)) < 1.0e-04 if np.linalg.norm(v) > 1e-8: assert np.linalg.norm(v - beta) / np.linalg.norm(v) < 1.0e-04 else: assert np.linalg.norm(beta) < 1e-8
def test_graphnet_wts(X, Xlist, Y, l1=500., l2=2, l3=3.5, tol=1e-4): print "graphnet_wts", l1, l2, l3 n = len(Xlist) wts = np.random.normal(0, 1, n) Xlist2 = [Xlist[i] * wts[i] for i in range(n)] A, Afull = gen_adj(X.shape[1]) l = regreg.cwpath(regreg.graphnet_wts( (Xlist, Y, A), rowweights=wts)) # initial_coefs = np.array([7.]*10)) l.problem.assign_penalty(l1=l1, l2=l2, l3=l3) l.fit(tol=tol, max_its=400) beta = l.problem.coefficients def f(beta): return np.linalg.norm(Y - np.dot(np.vstack(Xlist2), beta))**2 / ( 2 * len(Y)) + np.fabs(beta).sum() * l1 + l2 * np.linalg.norm( beta)**2 / 2 + l3 * np.dot(beta, np.dot(Afull, beta)) / 2 v = scipy.optimize.fmin_powell(f, np.zeros(X.shape[1]), ftol=1.0e-10, xtol=1.0e-10, maxfun=100000) v = np.asarray(v) print np.round(100 * v) / 100, '\n', np.round(100 * beta) / 100 print f(beta), f(v) if f(v) < f(beta) + 1e-10: assert (np.fabs(f(v) - f(beta)) / np.fabs(f(v) + f(beta)) < 1.0e-04) if np.linalg.norm(v) > 1e-8: assert (np.linalg.norm(v - beta) / np.linalg.norm(v) < 1.0e-04) else: assert (np.linalg.norm(beta) < 1e-8)
def test_graphnet(X, Xlist, Y, l1=500.0, l2=2, l3=3.5, tol=1e-4): print "GraphNet", l1, l2, l3 A, Afull = gen_adj(X.shape[1]) # l = regreg.regreg((Xlist, Y, A),regreg.graphnet,regreg.cwpath)#, initial_coefs= np.array([7.]*10)) l = regreg.cwpath(regreg.graphnet((Xlist, Y, A))) l.problem.assign_penalty(l1=l1, l2=l2, l3=l3) l.fit(tol=tol, inner_its=50, max_its=5000) beta = l.problem.coefficients def f(beta): return ( np.linalg.norm(Y - np.dot(X, beta)) ** 2 / (2 * len(Y)) + np.fabs(beta).sum() * l1 + l2 * np.linalg.norm(beta) ** 2 / 2 + l3 * np.dot(beta, np.dot(Afull, beta)) / 2 ) v = scipy.optimize.fmin_powell(f, np.zeros(X.shape[1]), ftol=1.0e-10, xtol=1.0e-10, maxfun=100000) v = np.asarray(v) # print np.round(100*v)/100,'\n', np.round(100*beta)/100 # print f(v), f(beta) if f(v) < f(beta): assert np.fabs(f(v) - f(beta)) / np.fabs(f(v) + f(beta)) < 1.0e-04 if np.linalg.norm(v) > 1e-8: assert np.linalg.norm(v - beta) / np.linalg.norm(v) < 1.0e-04 else: assert np.linalg.norm(beta) < 1e-8
def test_graphnet(X, Xlist, Y, l1=500., l2=2, l3=3.5, tol=1e-4): print "GraphNet", l1, l2, l3 A, Afull = gen_adj(X.shape[1]) #l = regreg.regreg((Xlist, Y, A),regreg.graphnet,regreg.cwpath)#, initial_coefs= np.array([7.]*10)) l = regreg.cwpath(regreg.graphnet((Xlist, Y, A))) l.problem.assign_penalty(l1=l1, l2=l2, l3=l3) l.fit(tol=tol, inner_its=50, max_its=5000) beta = l.problem.coefficients def f(beta): return np.linalg.norm(Y - np.dot(X, beta))**2 / (2 * len(Y)) + np.fabs( beta).sum() * l1 + l2 * np.linalg.norm(beta)**2 / 2 + l3 * np.dot( beta, np.dot(Afull, beta)) / 2 v = scipy.optimize.fmin_powell(f, np.zeros(X.shape[1]), ftol=1.0e-10, xtol=1.0e-10, maxfun=100000) v = np.asarray(v) #print np.round(100*v)/100,'\n', np.round(100*beta)/100 #print f(v), f(beta) if f(v) < f(beta): assert (np.fabs(f(v) - f(beta)) / np.fabs(f(v) + f(beta)) < 1.0e-04) if np.linalg.norm(v) > 1e-8: assert (np.linalg.norm(v - beta) / np.linalg.norm(v) < 1.0e-04) else: assert (np.linalg.norm(beta) < 1e-8)
def test_v_graphnet(X, Xlist, Y, l1=500., l2=2, l3=3.5, tol=1e-4): print "v_graphnet", l1, l2, l3 A, Afull = gen_adj(X.shape[1]) vec = np.dot(Y, X) l = regreg.cwpath(regreg.v_graphnet((vec, A))) l.problem.assign_penalty(l1=l1, l2=l2, l3=l3) l.fit(tol=tol, max_its=5000) beta = l.problem.coefficients lv2 = regreg.cwpath(regreg.lin_graphnet((Xlist, Y, A))) lv2.problem.assign_penalty(l1=l1, l2=l2, l3=l3) lv2.fit(tol=tol, max_its=5000) beta2 = lv2.problem.coefficients if np.linalg.norm(beta) > 1e-8: assert (np.linalg.norm(beta - beta2) / np.linalg.norm(beta) < 1.0e-04) else: assert (np.linalg.norm(beta2) < 1e-8) def f(beta): return -np.dot(vec, beta) + np.fabs( beta).sum() * l1 + l2 * np.linalg.norm(beta)**2 / 2 + l3 * np.dot( beta, np.dot(Afull, beta)) / 2 v = scipy.optimize.fmin_powell(f, np.zeros(X.shape[1]), ftol=1.0e-10, xtol=1.0e-10, maxfun=100000) v = np.asarray(v) #print np.round(100*v)/100,'\n', np.round(100*beta)/100 if f(v) < f(beta): assert (np.fabs(f(v) - f(beta)) / np.fabs(f(v) + f(beta)) < 1.0e-04) if np.linalg.norm(v) > 1e-8: assert (np.linalg.norm(v - beta) / np.linalg.norm(v) < 1.0e-04) else: assert (np.linalg.norm(beta) < 1e-8)
def test_v_graphnet(X, Xlist, Y, l1=500.0, l2=2, l3=3.5, tol=1e-4): print "v_graphnet", l1, l2, l3 A, Afull = gen_adj(X.shape[1]) vec = np.dot(Y, X) l = regreg.cwpath(regreg.v_graphnet((vec, A))) l.problem.assign_penalty(l1=l1, l2=l2, l3=l3) l.fit(tol=tol, max_its=5000) beta = l.problem.coefficients lv2 = regreg.cwpath(regreg.lin_graphnet((Xlist, Y, A))) lv2.problem.assign_penalty(l1=l1, l2=l2, l3=l3) lv2.fit(tol=tol, max_its=5000) beta2 = lv2.problem.coefficients if np.linalg.norm(beta) > 1e-8: assert np.linalg.norm(beta - beta2) / np.linalg.norm(beta) < 1.0e-04 else: assert np.linalg.norm(beta2) < 1e-8 def f(beta): return ( -np.dot(vec, beta) + np.fabs(beta).sum() * l1 + l2 * np.linalg.norm(beta) ** 2 / 2 + l3 * np.dot(beta, np.dot(Afull, beta)) / 2 ) v = scipy.optimize.fmin_powell(f, np.zeros(X.shape[1]), ftol=1.0e-10, xtol=1.0e-10, maxfun=100000) v = np.asarray(v) # print np.round(100*v)/100,'\n', np.round(100*beta)/100 if f(v) < f(beta): assert np.fabs(f(v) - f(beta)) / np.fabs(f(v) + f(beta)) < 1.0e-04 if np.linalg.norm(v) > 1e-8: assert np.linalg.norm(v - beta) / np.linalg.norm(v) < 1.0e-04 else: assert np.linalg.norm(beta) < 1e-8
def test_lin_graphnet(X, Xlist, Y, l1=500.0, l2=2, l3=3.5, tol=1e-4): print "lin_graphnet", l1, l2, l3 A, Afull = gen_adj(X.shape[1]) orth = np.random.normal(0, 1, X.shape[1]) eta = np.random.normal(0, 1, 1)[0] # l = regreg.regreg((Xlist, Y, A),regreg.lin_graphnet,regreg.cwpath)#, initial_coefs= np.array([7.]*10)) l = regreg.cwpath(regreg.lin_graphnet((Xlist, Y, A))) l.problem.assign_penalty(l1=l1, l2=l2, l3=l3, eta=eta) l.problem.orth = orth l.fit(tol=tol, max_its=5000) beta = l.problem.coefficients def f(beta): q = np.dot(X, beta) return ( -np.dot(Y, q) + np.fabs(beta).sum() * l1 + l2 * np.linalg.norm(beta) ** 2 / 2 + l3 * np.dot(beta, np.dot(Afull, beta)) / 2 + eta * np.dot(beta, orth) ) v = scipy.optimize.fmin_powell(f, np.zeros(X.shape[1]), ftol=1.0e-10, xtol=1.0e-10, maxfun=100000) v = np.asarray(v) print np.round(100 * v) / 100, "\n", np.round(100 * beta) / 100 print eta, np.sum(beta), np.sum(v) if f(v) < f(beta): assert np.fabs(f(v) - f(beta)) / np.fabs(f(v) + f(beta)) < 1.0e-04 if np.linalg.norm(v) > 1e-8: assert np.linalg.norm(v - beta) / np.linalg.norm(v) < 1.0e-04 else: assert np.linalg.norm(beta) < 1e-8
def test_lin_graphnet(X, Xlist, Y, l1=500., l2=2, l3=3.5, tol=1e-4): print "lin_graphnet", l1, l2, l3 A, Afull = gen_adj(X.shape[1]) orth = np.random.normal(0, 1, X.shape[1]) eta = np.random.normal(0, 1, 1)[0] #l = regreg.regreg((Xlist, Y, A),regreg.lin_graphnet,regreg.cwpath)#, initial_coefs= np.array([7.]*10)) l = regreg.cwpath(regreg.lin_graphnet((Xlist, Y, A))) l.problem.assign_penalty(l1=l1, l2=l2, l3=l3, eta=eta) l.problem.orth = orth l.fit(tol=tol, max_its=5000) beta = l.problem.coefficients def f(beta): q = np.dot(X, beta) return -np.dot(Y, q) + np.fabs( beta).sum() * l1 + l2 * np.linalg.norm(beta)**2 / 2 + l3 * np.dot( beta, np.dot(Afull, beta)) / 2 + eta * np.dot(beta, orth) v = scipy.optimize.fmin_powell(f, np.zeros(X.shape[1]), ftol=1.0e-10, xtol=1.0e-10, maxfun=100000) v = np.asarray(v) print np.round(100 * v) / 100, '\n', np.round(100 * beta) / 100 print eta, np.sum(beta), np.sum(v) if f(v) < f(beta): assert (np.fabs(f(v) - f(beta)) / np.fabs(f(v) + f(beta)) < 1.0e-04) if np.linalg.norm(v) > 1e-8: assert (np.linalg.norm(v - beta) / np.linalg.norm(v) < 1.0e-04) else: assert (np.linalg.norm(beta) < 1e-8)
def test_lasso(X, Xlist, Y, l1=500.0, tol=1e-4, M=0.0): print "LASSO", l1 # l = regreg.regreg((Xlist, Y),regreg.lasso,regreg.cwpath)#, initial_coefs= np.array([7.]*10)) p1 = regreg.lasso((Xlist, Y)) p1.assign_penalty(l1=l1) p2 = regreg.lasso((Xlist, Y)) p2.assign_penalty(l1=l1) p3 = regreg.lasso((Xlist, Y)) p3.assign_penalty(l1=l1) t1 = time.time() o1 = regreg.cwpath(p1) o1.fit(tol=tol, max_its=50) beta1 = o1.problem.coefficients t2 = time.time() print "CWPATH", t2 - t1 t1 = time.time() o2 = regreg.gengrad(p2) o2.fit(M, tol=1e-10, max_its=1500) beta2 = o2.problem.coefficients t2 = time.time() print "GENGRAD", t2 - t1 epsvec = [1e-0, 1e-3, 1e-6] t1 = time.time() o3 = regreg.nesterov(p3) for eps in epsvec: f_s = o3.fit(M, tol=1e-10, max_its=150, epsilon=eps) f_s = o3.fit(M, tol=1e-10, max_its=5000, epsilon=eps) beta3 = o3.problem.coefficients t2 = time.time() print "NEST", t2 - t1 def f(beta): return np.linalg.norm(Y - np.dot(X, beta)) ** 2 / (2 * len(Y)) + np.fabs(beta).sum() * l1 v = scipy.optimize.fmin_powell(f, np.zeros(X.shape[1]), ftol=1.0e-10, xtol=1.0e-10, maxfun=100000) v = np.asarray(v) vs = scipy.optimize.fmin_powell(f_s, np.zeros(X.shape[1]), ftol=1.0e-10, xtol=1.0e-10, maxfun=100000) vs = np.asarray(vs) print np.round(10000 * beta1) / 10000 print np.round(10000 * beta2) / 10000 print np.round(10000 * beta3) / 10000 print "\n", np.round(10000 * v) / 10000 print np.round(10000 * vs) / 10000 print f_s(beta3), f_s(vs) assert np.fabs(f(beta1) - f(beta2)) / np.fabs(f(beta1) + f(beta1)) < 1.0e-04 if np.linalg.norm(beta1) > 1e-8: assert np.linalg.norm(beta2 - beta1) / np.linalg.norm(beta1) < 1.0e-04 else: assert np.linalg.norm(beta2) < 1e-8 if f(v) < f(beta1): assert np.fabs(f(v) - f(beta1)) / np.fabs(f(v) + f(beta1)) < 1.0e-04 if np.linalg.norm(v) > 1e-8: assert np.linalg.norm(v - beta1) / np.linalg.norm(v) < 1.0e-04 else: assert np.linalg.norm(beta1) < 1e-8
def test_lasso(X, Xlist, Y, l1=500., tol=1e-4, M=0.): print "LASSO", l1 #l = regreg.regreg((Xlist, Y),regreg.lasso,regreg.cwpath)#, initial_coefs= np.array([7.]*10)) p1 = regreg.lasso((Xlist, Y)) p1.assign_penalty(l1=l1) p2 = regreg.lasso((Xlist, Y)) p2.assign_penalty(l1=l1) p3 = regreg.lasso((Xlist, Y)) p3.assign_penalty(l1=l1) t1 = time.time() o1 = regreg.cwpath(p1) o1.fit(tol=tol, max_its=50) beta1 = o1.problem.coefficients t2 = time.time() print "CWPATH", t2 - t1 t1 = time.time() o2 = regreg.gengrad(p2) o2.fit(M, tol=1e-10, max_its=1500) beta2 = o2.problem.coefficients t2 = time.time() print "GENGRAD", t2 - t1 epsvec = [1e-0, 1e-3, 1e-6] t1 = time.time() o3 = regreg.nesterov(p3) for eps in epsvec: f_s = o3.fit(M, tol=1e-10, max_its=150, epsilon=eps) f_s = o3.fit(M, tol=1e-10, max_its=5000, epsilon=eps) beta3 = o3.problem.coefficients t2 = time.time() print "NEST", t2 - t1 def f(beta): return np.linalg.norm(Y - np.dot(X, beta))**2 / ( 2 * len(Y)) + np.fabs(beta).sum() * l1 v = scipy.optimize.fmin_powell(f, np.zeros(X.shape[1]), ftol=1.0e-10, xtol=1.0e-10, maxfun=100000) v = np.asarray(v) vs = scipy.optimize.fmin_powell(f_s, np.zeros(X.shape[1]), ftol=1.0e-10, xtol=1.0e-10, maxfun=100000) vs = np.asarray(vs) print np.round(10000 * beta1) / 10000 print np.round(10000 * beta2) / 10000 print np.round(10000 * beta3) / 10000 print "\n", np.round(10000 * v) / 10000 print np.round(10000 * vs) / 10000 print f_s(beta3), f_s(vs) assert (np.fabs(f(beta1) - f(beta2)) / np.fabs(f(beta1) + f(beta1)) < 1.0e-04) if np.linalg.norm(beta1) > 1e-8: assert (np.linalg.norm(beta2 - beta1) / np.linalg.norm(beta1) < 1.0e-04) else: assert (np.linalg.norm(beta2) < 1e-8) if f(v) < f(beta1): assert (np.fabs(f(v) - f(beta1)) / np.fabs(f(v) + f(beta1)) < 1.0e-04) if np.linalg.norm(v) > 1e-8: assert (np.linalg.norm(v - beta1) / np.linalg.norm(v) < 1.0e-04) else: assert (np.linalg.norm(beta1) < 1e-8)