Exemplo n.º 1
0
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
Exemplo n.º 2
0
def meshy_predict(meshy_one_object,x):
	O = utils.interpO(data=x,mesh=meshy_one_object['mesh'],k=meshy_one_object['k'],key=meshy_one_object['interp'])
	return O.dot(meshy_one_object['theta.hat'])