def sobbynorm(k1,k2, lnorm,theta,delta1,delta2,m): D1 = [] D2 = [] norm_list = [] for i in range(len(k1)): D1.append(utils.form_Dk(n=m,k=k1[i])*(delta1**(1/lnorm-k1[i]))) D2.append(utils.form_Dk(n=m,k=k2[i])*(delta2**(1/lnorm-k2[i]))) norm_list.append(cvx.norm(D1[i]*theta*D2[i].T,lnorm)) return np.sum(norm_list)
def lars_one(x,y,k=1,tune=50,eps=0.01,cvx_solver=0): # possible solvers to choose from: typically CVXOPT works better for simulations thus far. solvers = [cvx.SCS,cvx.CVXOPT] # 0 is SCS, 1 CVXOPT default_iter = [160000,3200][cvx_solver] # defaults are 2500 and 100 # Create T: the knots T = knots(x=x,k=k) D = utils.form_Dk(n=x.size,k=k) # Create G-matrix (truncated power basis): specify n and k G = tpb(x=x,t=T,k=k) # Solve convex problem. theta = cvx.Variable(x.size) obj = cvx.Minimize(0.5 * cvx.sum_squares(y - G*theta) + tune * cvx.norm(D*theta, 1) ) prob = cvx.Problem(obj) prob.solve(solver=solvers[cvx_solver],verbose=False,max_iters = default_iter) # Check for error. # This while loop only works for SCS. CVXOPT terminates after 1 iteration. counter = 0 while prob.status != cvx.OPTIMAL: maxit = 2*default_iter prob.solve(solver=solvers[cvx_solver],verbose=False,max_iters=maxit) default_iter = maxit counter = counter +1 if counter>4: raise Exception("Solver did not converge with %s iterations! (N=%s,d=%s,k=%s)" % (default_iter,n,ncuts,k) ) output = {'theta.hat':np.array(theta.value),'fitted': G.dot(np.array(theta.value)),'x':x,'y':y,'eps':eps} return output
def disc_one(x,y,ncuts,k=1,segs=None,lnorm=1,tune=50,constant=True,eps=0.01,cvx_solver=2): # possible solvers to choose from: typically CVXOPT works better for simulations thus far. solvers = [cvx.SCS,cvx.ECOS,cvx.CVXOPT] # 0 is SCS, 1 is ECOS, 2 CVXOPT n = x.size # Create D-matrix: specify n and k if segs==None: segs = np.linspace(min(x)-eps,max(x)+eps,ncuts) D = utils.form_Dk(n=ncuts,k=k) # Create O-matrix: specify x and number of desired cuts O = utils.Omatrix(x,ncuts,constant=constant,eps=eps,segs=segs) # Solve convex problem. theta = cvx.Variable(ncuts) obj = cvx.Minimize(0.5 * cvx.sum_squares(y - O*theta) + tune * cvx.norm(D*theta, lnorm) ) prob = cvx.Problem(obj) # Use CVXOPT as the solver prob.solve(solver=solvers[cvx_solver],verbose=False) print 'Solver status: ', prob.status # Check for error. if prob.status != cvx.OPTIMAL: raise Exception("Solver did not converge!") if constant==True: return [segs, np.array(theta.value),x,y,segs,constant,eps] if constant==False: return [x, O.dot(np.array(theta.value)),x,y,segs,constant,eps]
def meshy_one(x,y,m,k=1,interp=1,mesh=None,lnorm=1,tune=50,eps=0.01,cvx_solver=0): # interp = 0 for natural splines # interp = 1 for banded piecewise polynomials # possible solvers to choose from: typically CVXOPT works better for simulations thus far. solvers = [cvx.SCS,cvx.CVXOPT] # 0 is SCS, 1 CVXOPT default_iter = [160000,3200][cvx_solver] # defaults are 2500 and 100 n = x.size # Create D-matrix: specify n and k if mesh==None: mesh = np.linspace(min(x)-eps,max(x)+eps,m) delta = np.diff(mesh)[0] D = utils.form_Dk(n=m,k=k)*(delta**(1/lnorm-k)) # Create O-matrix: specify x and number of desired cuts O = utils.interpO(data=x,mesh=mesh,k=k,key=interp) # Solve convex problem. theta = cvx.Variable(m) obj = cvx.Minimize(0.5 * cvx.sum_squares(y - O*theta) + tune * cvx.norm(D*theta, lnorm) ) prob = cvx.Problem(obj) prob.solve(solver=solvers[cvx_solver],verbose=False,max_iters = default_iter) counter = 0 while prob.status != cvx.OPTIMAL: maxit = 2*default_iter prob.solve(solver=solvers[cvx_solver],verbose=False,max_iters=maxit) default_iter = maxit counter = counter +1 if counter>4: raise Exception("Solver did not converge with %s iterations! (N=%s,d=%s,k=%s)" % (default_iter,n,m,k) ) output = {'mesh': mesh, 'theta.hat': np.array(theta.value),'fitted':O.dot(np.array(theta.value)),'x':x,'y':y,'k':k,'interp':interp,'eps':eps,'m':m} return output
def l1tf_one(x,y,k=1,tune=50,eps=0.01,cvx_solver=0): # possible solvers to choose from: typically CVXOPT works better for simulations thus far. solvers = [cvx.SCS,cvx.CVXOPT] # 0 is SCS, 1 CVXOPT default_iter = [160000,3200][cvx_solver] # defaults are 2500 and 100 n = x.size # Create D-matrix: specify n and k D = utils.form_Dk(n=n,k=k) # Create falling factorial basis H = fallfac(x=x, k=k) print H.shape # Solve convex problem. theta = cvx.Variable(n) obj = cvx.Minimize(0.5 * cvx.sum_squares(y - H*theta) + tune * cvx.norm(D*theta, 1) ) prob = cvx.Problem(obj) prob.solve(solver=solvers[cvx_solver],verbose=False,max_iters = default_iter) #print 'Solver status: ', prob.status # Check for error. #if prob.status != cvx.OPTIMAL: # raise Exception("Solver did not converge!") # This while loop only works for SCS. CVXOPT terminates after 1 iteration. counter = 0 while prob.status != cvx.OPTIMAL: maxit = 2*default_iter prob.solve(solver=solvers[cvx_solver],verbose=False,max_iters=maxit) default_iter = maxit counter = counter +1 if counter>4: raise Exception("Solver did not converge with %s iterations! (N=%s,d=%s,k=%s)" % (default_iter,n,ncuts,k) ) output = {'theta.hat':np.array(theta.value),'fitted': H.dot(np.array(theta.value)),'x':x,'y':y,'eps':eps} return output