def get_svm_ws(x, y, c, kernel_function): n = np.shape(x)[0] # print "x data = \n", data[:, :-1] k = kernel_function(x, x) # k = np.dot(x, x.T) # basically the "kernel matrix" # print "kernel = \n", k P = matrix(k * np.multiply.outer(y, y)) # print "P = \n", P q = matrix(-1*np.ones(n)) # print "q = \n", q G = matrix(np.concatenate((np.eye(n), -1*np.eye(n)))) # print "G = \n", G h = matrix(np.concatenate((np.tile(c, n), np.tile(0, n))), tc='d') A = matrix(y, (1, n)) b = matrix(0.0) solvers.options['abstol'] = 1e-10 solvers.options['reltol'] = 1e-10 solvers.options['feastol'] = 1e-10 solvers.options['show_progress'] = False solution = solvers.qp(P, q, G, h, A, b) alphas = np.array(solution['x']) # print alphas w = np.sum((x.T*y)*alphas.flatten(), axis=1) zero_thresh = 1e-6 y = np.reshape(y, (-1, 1)) x_support = np.where(np.abs(alphas - (c + zero_thresh)/2.0) < (c - zero_thresh)/2.0, x, 0) print "Number of support vectors = ", np.sum(np.abs(alphas - (c + zero_thresh)/2.0) < (c - zero_thresh)/2.0) k_alpha = kernel_function(x_support, x) w0 = np.array([(np.sum(np.where(np.abs(alphas - (c + zero_thresh)/2.0) < (c - zero_thresh)/2.0, y, 0)) - np.sum(k_alpha * y * alphas)) / np.sum(np.where(abs(alphas - (c + zero_thresh)/2.0) < (c - zero_thresh)/2.0))]) # print "alphas = ", alphas # print "w0 = ", w0 # print "w = ", w return [w0, alphas, x_support]
def mfeasible(c, lfather=None, ufather=None): #box containing the ball c.lReduced = c.xc - c.r * ones(D) c.uReduced = c.xc + c.r * ones(D) # check if the sub-ball is in the domain reduced of the father def checkAndUpdateBound(): # intersection between the box of the ball and the box reduced of the father for i in range(0,D): if c.lReduced[i]<lfather[i]: c.lReduced[i]=lfather[i] if c.uReduced[i]>ufather[i]: c.uReduced[i]=ufather[i] checkAndUpdateBound() # Solve QP to check feasibility sol = qp(matrix(2*identity(D)),matrix(-2*c.xc),matrix(vstack([-1*identity(D),identity(D),A])),matrix(hstack([-l,u,b])),matrix(E),matrix(d)) mxopt = array(sol['x']).flatten() mr = sol['primal objective'] mr = mr + dot(c.xc,c.xc) # Check if point lies inside domain if(mr <= (c.r**2)): mf = 1 else: mf = 0 mxopt = None return mf, mxopt
def cvxopt_run(infile = 'cvxopt_params.npz', outfile = 'cvxopt.npz'): try: params = np.load(infile); P = matrix(params['P']); q = matrix(params['q']); except IOError: print 'file not found'; raise; except KeyError: print 'parameters P and q are required to solve QP'; raise; else: try: G = matrix(params['G']); h = matrix(params['h']); except KeyError: sol = solvers.qp(P, q); else: try: A = matrix(params['A']); b = matrix(params['b']); except KeyError: sol = solvers.qp(P, q, G, h); else: sol = solvers.qp(P, q, G, h, A, b); finally: print sol['x']; print sol['primal objective']; x = np.array([i for i in sol['x']]); print x; np.savez(outfile, x = x);
def maximize(mu, sigma, q): n = len(last_b) P = matrix(2 * (sigma + ALPHA * np.eye(n))) q = matrix(-q * mu + 2 * ALPHA * np.matrix(last_b).T) G = matrix(-np.eye(n)) h = matrix(np.zeros(n)) if max_leverage is None or max_leverage == float('inf'): sol = solvers.qp(P, q, G, h) else: if self.allow_cash: G = matrix(np.r_[G, matrix(np.ones(n)).T]) h = matrix(np.r_[h, matrix([self.max_leverage])]) sol = solvers.qp(P, q, G, h, initvals=last_b) else: A = matrix(np.ones(n)).T b = matrix(np.array([max_leverage])) sol = solvers.qp(P, q, G, h, A, b, initvals=last_b) if sol['status'] != 'optimal': logging.warning("Solution not found for {}, using last weights".format(last_b.name)) return last_b return np.squeeze(sol['x'])
def portfolio(filename="portfolio.txt"): # Markowitz portfolio optimization data = np.loadtxt('portfolio.txt')[:,1:] n = data.shape[1] mu = 1.13 # calculate covariance matrix Q = np.cov(data.T) # calculate returns R = data.mean(axis=0) P = matrix(Q) q = matrix(np.zeros(n)) b = matrix(np.array([1., mu])) A = np.ones((2,n)) A[1,:] = R A = matrix(A) # calculate optimal portfolio with short selling. sol1 = solvers.qp(P, q, A=A, b=b) x1 = np.array(sol1['x']).flatten() # calculate optimal portfolio without short selling. G = matrix(-np.eye(n)) h = matrix(np.zeros(n)) sol2 = solvers.qp(P, q, G, h, A, b) x2 = np.array(sol2['x']).flatten() return x1, x2
def optimal_portfolio(returns): n = len(returns) returns = np.asmatrix(returns) N = 100 mus = [10**(5.0 * t/N - 1.0) for t in range(N)] # Convert to cvxopt matrices S = opt.matrix(np.cov(returns)) pbar = opt.matrix(np.mean(returns, axis=1)) # Create constraint matrices G = -opt.matrix(np.eye(n)) # negative n x n identity matrix h = opt.matrix(0.0, (n ,1)) A = opt.matrix(1.0, (1, n)) b = opt.matrix(1.0) # Calculate efficient frontier weights using quadratic programming portfolios = [solvers.qp(mu*S, -pbar, G, h, A, b)['x'] for mu in mus] ## CALCULATE RISKS AND RETURNS FOR FRONTIER returns = [np.dot(np.transpose(pbar), x) for x in portfolios] returns = np.concatenate(returns).ravel() risks = [np.sqrt(np.dot(np.transpose(x), S*x)) for x in portfolios] risks = np.concatenate(risks).ravel() sharpes = [this_return / this_risk for this_return,this_risk in zip(returns, risks)] final_portfolio = portfolios[sharpes.index(max(sharpes))] ## CALCULATE THE 2ND DEGREE POLYNOMIAL OF THE FRONTIER CURVE m1 = np.polyfit(np.asarray(returns), np.asarray(risks), 2) x1 = np.sqrt(m1[2] / m1[0]) # CALCULATE THE OPTIMAL PORTFOLIO wt = solvers.qp(opt.matrix(x1 * S), -pbar, G, h, A, b)['x'] #return np.asarray(wt), returns, risks return final_portfolio, returns, risks
def optimal_portfolio(returns): n = len(returns) returns = np.asmatrix(returns) N = 100 mus = [10**(5.0 * t/N - 1.0) for t in range(N)] # Convert to cvxopt matrices S = opt.matrix(np.cov(returns)) pbar = opt.matrix(np.mean(returns, axis=1)) # Create constraint matrices G = -opt.matrix(np.eye(n)) # negative n x n identity matrix h = opt.matrix(0.0, (n ,1)) A = opt.matrix(1.0, (1, n)) b = opt.matrix(1.0) # Calculate efficient frontier weights using quadratic programming portfolios = [solvers.qp(mu*S, -pbar, G, h, A, b)['x'] for mu in mus] ## CALCULATE RISKS AND RETURNS FOR FRONTIER returns = [blas.dot(pbar, x) for x in portfolios] risks = [np.sqrt(blas.dot(x, S*x)) for x in portfolios] ## CALCULATE THE 2ND DEGREE POLYNOMIAL OF THE FRONTIER CURVE m1 = np.polyfit(returns, risks, 2) x1 = np.sqrt(m1[2] / m1[0]) # CALCULATE THE OPTIMAL PORTFOLIO wt = solvers.qp(opt.matrix(x1 * S), -pbar, G, h, A, b)['x'] return np.asarray(wt), returns, risks
def solve_optimization(self, train_x, labels): Q = self.build_Q(train_x, labels) p = [-1.0] * train_x.shape[0] if self.with_slack: h = [0.0] * train_x.shape[0] + [self.C] * train_x.shape[0] G = np.concatenate((np.identity(train_x.shape[0]) * -1, np.identity(train_x.shape[0]))) else: h = [0.0] * train_x.shape[0] G = np.identity(train_x.shape[0]) * -1 optimized = qp(matrix(Q), matrix(p), matrix(G), matrix(h)) if optimized['status'] == 'optimal': alphas = list(optimized['x']) self.support_vector = [(alpha, train_x[i,:],labels[i]) for i,alpha in enumerate(alphas) if alpha>self.epsilon] return True else: print "No valid separating hyperplane found" print "Find a best hyperplane for the mixture data..." h = [0.0] * train_x.shape[0] + [self.C] * train_x.shape[0] G = np.concatenate((np.identity(train_x.shape[0]) * -1, np.identity(train_x.shape[0]))) optimized = qp(matrix(Q), matrix(p), matrix(G), matrix(h)) alphas = list(optimized['x']) self.support_vector = [(alpha, train_x[i,:],labels[i]) for i,alpha in enumerate(alphas) if alpha>self.epsilon] return False
def maximize(mu, sigma, q): n = len(last_b) P = matrix(2 * sigma) qq = matrix(np.zeros(n)) G = matrix(np.r_[-np.eye(n), -mu]) h = matrix(np.r_[np.zeros(n), -q]) try: if max_leverage is None or max_leverage == float('inf'): sol = solvers.qp(P, qq, G, h) else: if self.allow_cash: G = matrix(np.r_[G, matrix(np.ones(n)).T]) h = matrix(np.r_[h, matrix([self.max_leverage])]) sol = solvers.qp(P, qq, G, h, initvals=last_b) else: A = matrix(np.ones(n)).T b = matrix(np.array([max_leverage])) sol = solvers.qp(P, qq, G, h, A, b, initvals=last_b) if sol['status'] == 'unknown': raise ValueError() except ValueError: # no feasible solution - maximize return instead P = P * 0 qq = matrix(-mu.T) G = matrix(np.r_[-np.eye(n), matrix(np.ones(n)).T]) h = matrix(np.r_[np.zeros(n), self.max_leverage]) sol = solvers.qp(P, qq, G, h) return np.squeeze(sol['x'])
def optim_CHT_decen(pb, step, e, user, ratio=0., k_max=1000): """ Returns power distribution u_sol, the lagrangian multiplier, the number of Uzawa iterations and the value of the cost function in the static distributed optimization with a user cheating with his comfort factor. keyword arguments: pb -- dictionary of the problem (nbr of users, time step, max resources, max admissible power, thermal resistance, Thermal capacity, vector of the init temperature, value of the exterior temperature, reference temperature, comfort factor, size of the prediction horizon) step -- step of the Lagrangian in the Uzawa method e -- value of the maxim gap tolerable user -- number of the user cheating ratio -- ratio of the comfort factor in comparison to the one of the other users (default 0.0) k_max -- max number of iteration in the Uzawa method (default 1000) """ Rth = pb['Rth'] Text = pb['Text'] T_id = pb['T_id'] Umax = pb['Umax'] u_m = pb['u_m'] alpha = pb['alpha'] u_id = (T_id - Text) / Rth L = 0 m = len(Rth) u_sol = np.zeros(m) for k in range(k_max): assert L >= 0, "u_id can be reached for all users" for j in range(m): Pj = matrix(2 * alpha[j] * Rth[j] ** 2, tc='d') qj = matrix(1 - 2 * alpha[j] * Rth[j]**2 * u_id[j] + L, tc='d') Gj = matrix([-1, 1], tc='d') hj = matrix([0, u_m[j]], tc='d') solj = solvers.qp(Pj, qj, Gj, hj) u_sol[j] = solj['x'][0] Pj = matrix(2 * alpha[user] * Rth[user] ** 2, tc='d') qj = matrix(1 - 2 * alpha[user] * Rth[user] ** 2 * u_id[user] + (1-ratio)*L, tc='d') Gj = matrix([-1, 1], tc='d') hj = matrix([0, u_m[user]], tc='d') solj = solvers.qp(Pj, qj, Gj, hj) u_sol[user] = solj['x'][0] L = L + step * (u_sol.sum() - Umax) if u_sol.sum() - Umax < e: break return u_sol, L, k, 'tba'
def _local_opt(self, initial_data, sens_matrix): """Soves the quadratic problem for maximization of the log likelihood Args: initial_data(list): The initial data corresonding to simulations from sims sens_matrix(np.array): The sensitivity matrix about the model Return: (np.ndarray): The step direction for greates improvement in log lieklyhood """ constrain = self.get_option('constrain') debug = self.get_option('debug') # Get constraints g_mat, h_vec = self._get_constraints() p_mat, q_vec = self._get_model_pq() tmp = self._get_sim_pq(initial_data, sens_matrix) p_mat += tmp[0] q_vec += tmp[1] p_mat *= 0.5 solvers.options['show_progress'] = False solvers.options['debug'] = False solvers.options['maxiters'] = 100 # 100 default solvers.options['reltol'] = 1e-6 # 1e-6 default solvers.options['abstol'] = 1e-7 # 1e-7 default solvers.options['feastol'] = 1e-7 # 1e-7 default try: if constrain: sol = solvers.qp(matrix(p_mat), matrix(q_vec), matrix(g_mat), matrix(h_vec)) else: sol = solvers.qp(matrix(p_mat), matrix(q_vec)) except ValueError as inst: print(inst) print("G " + str(g_mat.shape)) print("P " + str(p_mat.shape)) print("h " + str(h_vec.shape)) print("q " + str(q_vec.shape)) pdb.post_mortem() if sol['status'] != 'optimal': for key, value in sol.items(): print(key, value) raise RuntimeError('{} The optimization algorithm could not locate' 'an optimal point'.format(self.get_inform(1))) return sol
def optimal_portfolio(returns): n = len(returns) returns = numpy.asmatrix(returns) N = 100 mus = [10**(5.0 * t/N - 1.0) for t in range(N)] # Convert to cvxopt matrices S = matrix(numpy.cov(returns)) print S # extra print stmts print "I am opt related" #######FIXME ABOVE ######################### pbar = matrix(numpy.mean(returns, axis=1)) # Create constraint matrices m=[[-1.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0],[0.0,-1.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0],[0.0,0.0,-1.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0],[0.0,0.0,0.0,-1.0,0.0,0.0,0.0,0.0,1.0,0.0],[0.0,0.0,0.0,0.0,-1.0,0.0,0.0,0.0,0.0,1.0]] G = matrix(m) ab=[0,0,0,0,0,0.3,0.3,0.3,0.3,0.3] h=matrix(ab) # G = -matrix(numpy.eye(n)) # negative n x n identity matrix # h = matrix(0.0, (n ,1)) A = matrix(1.0, (1, n)) b = matrix(1.0) # Calculate efficient frontier weights using quadratic programming portfolios = [solvers.qp(mu*S, -pbar, G, h, A, b)['x'] for mu in mus] ## CALCULATE RISKS AND RETURNS FOR FRONTIER returns = [blas.dot(pbar, x) for x in portfolios] risks = [numpy.sqrt(blas.dot(x, S*x)) for x in portfolios] ## CALCULATE THE 2ND DEGREE POLYNOMIAL OF THE FRONTIER CURVE m1 = numpy.polyfit(returns, risks, 2) x1 = numpy.sqrt(m1[2] / m1[0]) # CALCULATE THE OPTIMAL PORTFOLIO wt = solvers.qp(matrix(x1 * S), -pbar, G, h, A, b)['x'] weights = matrix(wt) printing.options['dformat'] = '%.1f' #rounding up weights to 1 decimal # return numpy.asarray(wt) return weights #changed in matrix """result looks like the follwg-
def fit(self, X, y): assert X.shape[0] == y.shape[0] N, d = X.shape P = np.zeros((N, N)) for i in xrange(N): for j in xrange(N): P[i, j] = y[i] * y[j] * self.kernel(X[i], X[j]) q = -np.ones((N,)) G = -np.eye(N) h = np.zeros((N,)) A = np.array([y]) b = np.array([0.0]) self.sol = solvers.qp(matrix(P), matrix(q), matrix(G), matrix(h), matrix(A, tc='d'), matrix(b)) self.alpha = np.array(self.sol['x']) w = np.zeros((d,)) sv_i = None for i in xrange(len(self.alpha)): a = self.alpha[i] if a > 10e-5: sv_i = i w += a * y[i] * X[i] self.b = y[sv_i] for i in xrange(len(self.alpha)): a = self.alpha[i] if a > 10e-5: self.b -= self.alpha[i] * y[i] * self.kernel(X[i], X[sv_i]) # self.w = np.r_[self.b, w] self.X = X self.y = y
def solve(self, verbose=False): # Optimize old_settings = _apply_options({"show_progress": verbose}) for i in count(-9): try: results = qp(self.P, self.q, self.G, self.h, self.A, self.b, initvals=self.last_results) break except ValueError as e: # Sometimes the hessian isn't full rank, # due to numerical error if self.fix_pd: eps = 10.0 ** i print "Rank error while solving, adjusting to fix..." print "Using epsilon = %.1e" % eps self._ensure_pd(eps) else: raise e _apply_options(old_settings) # Store results self.last_results = results # Check return status status = results["status"] if not status == "optimal": print >> stderr, ("Warning: termination of qp with status: %s" % status) # Convert back to NumPy matrix # and return solution xstar = results["x"] obj = Objective((0.5 * xstar.T * self.P * xstar)[0], (self.q.T * xstar)[0]) return matrix(xstar), obj
def fit_l1reg(self): from cvxopt import matrix, solvers n_params = self.tree_graph.shape[1] HI_sc = self.genetic_params if self.map_to_tree else len(self.relevant_muts) n_sera = len(self.sera) n_v = len(self.HI_strains) # set up the quadratic matrix containing the deviation term (linear xterm below) # and the l2-regulatization of the avidities and potencies P1 = np.zeros((n_params+HI_sc,n_params+HI_sc)) P1[:n_params, :n_params] = self.TgT for ii in xrange(HI_sc, HI_sc+n_sera): P1[ii,ii]+=self.lam_pot for ii in xrange(HI_sc+n_sera, n_params): P1[ii,ii]+=self.lam_avi P = matrix(P1) # set up cost for auxillary parameter and the linear cross-term q1 = np.zeros(n_params+HI_sc) q1[:n_params] = -np.dot( self.HI_dist, self.tree_graph) q1[n_params:] = self.lam_HI q = matrix(q1) # set up linear constraint matrix to regularize the HI parametesr h = matrix(np.zeros(2*HI_sc)) # Gw <=h G1 = np.zeros((2*HI_sc,n_params+HI_sc)) G1[:HI_sc, :HI_sc] = -np.eye(HI_sc) G1[:HI_sc:, n_params:] = -np.eye(HI_sc) G1[HI_sc:, :HI_sc] = np.eye(HI_sc) G1[HI_sc:, n_params:] = -np.eye(HI_sc) G = matrix(G1) W = solvers.qp(P,q,G,h) self.params = np.array([x for x in W['x']])[:n_params] print "rms deviation prior to relax=",np.sqrt(self.fit_func()) return self.params
def kmm(Xtrain, Xtest, sigma): n_tr = len(Xtrain) n_te = len(Xtest) # calculate Kernel print('Computing kernel for training data ...') K_ns = sk.rbf_kernel(Xtrain, Xtrain, sigma) # make it symmetric K = 0.9 * (K_ns + K_ns.transpose()) # calculate kappa print('Computing kernel for kappa ...') kappa_r = sk.rbf_kernel(Xtrain, Xtest, sigma) ones = numpy.ones(shape=(n_te, 1)) kappa = numpy.dot(kappa_r, ones) kappa = -(float(n_tr) / float(n_te)) * kappa # calculate eps eps = (math.sqrt(n_tr) - 1) / math.sqrt(n_tr) # constraints A0 = numpy.ones(shape=(1, n_tr)) A1 = -numpy.ones(shape=(1, n_tr)) A = numpy.vstack([A0, A1, -numpy.eye(n_tr), numpy.eye(n_tr)]) b = numpy.array([[n_tr * (eps + 1), n_tr * (eps - 1)]]) b = numpy.vstack([b.T, -numpy.zeros(shape=(n_tr, 1)), numpy.ones(shape=(n_tr, 1)) * 1000]) print('Solving quadratic program for beta ...') P = matrix(K, tc='d') q = matrix(kappa, tc='d') G = matrix(A, tc='d') h = matrix(b, tc='d') beta = solvers.qp(P, q, G, h) return [i for i in beta['x']]
def optimize(self, mu, syms=None): """Optimize porfolio allocation for given mu. http://abel.ee.ucla.edu/cvxopt/userguide/coneprog.html#quadratic-programming :mu: @todo :syms: symbols. default: self.top() :returns: @todo """ if syms is None: syms = self.top() n = len(syms) mvals = [v for v in self.stats.ix[syms]['mean'].values] pbar = matrix(mvals) G = matrix(0.0, (n, n)) G[::n + 1] = -1.0 h = matrix(0.0, (n, 1)) A = matrix(1.0, (1, n)) b = matrix(1.0) C = matrix(self.panel.r[syms].cov().values) options['show_progress'] = False pf = qp(mu * C, -pbar, G, h, A, b) returns = dot(pbar, pf['x']) std = sqrt(dot(pf['x'], C * pf['x'])) return (pf['x'], returns, std)
def test_pcg(): 'Test function for projected CG.' n = 10 m = 4 H = sprandsym(n,n) A = sp_rand(m,n,0.9) x0 = matrix(1,(n,1)) b = A*x0 c = matrix(1.0,(n,1)) x_pcg = pcg(H,c,A,b,x0) Lhs1 = sparse([H,A]) Lhs2 = sparse([A.T,spmatrix([],[],[],(m,m))]) Lhs = sparse([[Lhs1],[Lhs2]]) rhs = -matrix([c,spmatrix([],[],[],(m,1))]) rhs2 = copy(rhs) linsolve(Lhs,rhs) #print rhs[:10] sol = solvers.qp(H,c,A=A,b=b) print ' cvxopt qp| pCG' print matrix([[sol['x']],[x_pcg]]) print 'Dual variables:' print sol['y'] print 'KKT equation residuals:' print H*sol['x'] + c + A.T*sol['y']
def runQuadOptimizer_globalMinVar(matSigma): vecVola = np.sqrt(matSigma.diagonal()) n = vecVola.shape[0] # Check for postive definite covariance matrix, if not exit if np.all(np.linalg.eigvals(matSigma) > 0) == False: print "Sigma matrix not positive definite, solution may not be unique! \nProceed Checking for semi-definiteness\n" if np.all(np.linalg.eigvals(matSigma) >= 0) == False: print "Sigma matrix not positive semi-definite!" raise SystemExit # Parameterize solver, using standard notation for cvxopt P = matrix(matSigma,tc='d') q = matrix(np.zeros(shape=(n, 1)), tc='d') G = matrix(np.diag(np.repeat(a=-1, repeats=n)), tc='d') g = matrix(np.zeros(shape=(n, 1)), tc='d') A = matrix(np.ones(shape=(1, n)), tc='d') a = matrix(np.array([1]), tc='d') # Invoke solver sol = solvers.qp(A=A, b=a, G=G, h=g, P=P, q=q) # Solution xOpt = np.array(sol['x']) fOpt = np.array(sol['primal objective']) # Solution rescaled normalFactor = sum(xOpt) xOptScaled = np.asarray(xOpt) * (1/normalFactor) return {'xOpt':xOpt, 'xOptScaled':xOptScaled, 'fOpt':fOpt}
def cvx_l1(H, y, p, i, lamda1,lamda2, sig2): h = np.hstack v = np.vstack u = cvx.base.matrix prtrb=np.zeros(n) prtrb[i]=p ydH = np.dot(y.T,H)/(2.*sig2) Hdy = np.dot(H.T,y)/(2.*sig2) HdH= np.dot(H.T,H)/sig2 q = u(h([-ydH-Hdy+lamda1*np.ones(n)-prtrb,ydH+Hdy+lamda1*np.ones(n)+prtrb]).T) #P=u(lamda2*np.eye(2*n)) hor1 = h([HdH+lamda2*np.eye(n),-HdH-lamda2*np.eye(n)]) # hor2 = h([lamda2*np.eye(n),lamda2*np.eye(n)]) P=u(v([hor1,-hor1])) G3 = h([-np.eye(2*n)]) G = u(v([G3])) hh = u(h([np.zeros(2*n).T])) sol = qp(P,q, G, hh) doublex = np.array(sol['x'][:2*n]).T[0] xestimate = np.dot(u(h([np.eye(n),-np.eye(n)])),doublex) return xestimate.T
def fit(self): """ train a BDD """ # number of training samples n = self.n # the kernel matrix K = self.kernel # the covariance matrix C = self.cov_mat # the inverse of the covariance matrix C_inv = np.linalg.inv(C) # the diagonal matrix containing the sum of the rowa of the kernel matrix D = self.diagonal # parameter 0 < nu < 1, controlling the sparsity of the solution nu = self.nu # the mean vector m = -np.dot(D, np.ones(n))**nu # solve the quadratic program P = matrix(n*K + C_inv) q = matrix(-1. * (np.dot(D, np.ones(n)) + np.dot(C_inv, m)).T) sol = qp(P, q) # extract the solution self.alphas = sol['x'] # BDD is trained self.is_trained = True
def findQPSolver(X_Train_Data_, Y_Train_Data_, C=None): no_of_samples = len(X_Train_Data_) # Gram matrix K = np.zeros((no_of_samples, no_of_samples)) for i in range(no_of_samples): for j in range(no_of_samples): K[i,j] = polynomial_kernel(X_Train_Data_[i], X_Train_Data_[j]) P = matrix(np.outer(Y_Train_Data_,Y_Train_Data_) * K) q = matrix(np.ones(no_of_samples) * -1) A = matrix(Y_Train_Data_, (1,no_of_samples)) b = matrix(0.0) tmp1 = np.diag(np.ones(no_of_samples) * -1) tmp2 = np.diag(np.ones(no_of_samples)) G = matrix(np.vstack((tmp1, tmp2))) tmp1 = np.zeros(no_of_samples) tmp2 = np.ones(no_of_samples) * 1 h = matrix(np.hstack((tmp1, tmp2))) sol = solvers.qp(P, q, G, h, A, b) return sol['x']
def Corpus_K_Means(TestSample,num_topic): Theta = TestSample.Theta ThetaPredict = np.zeros(Theta.shape) W = TestSample.Word W = np.array(W,dtype='double') estimators = KMeans(n_clusters=num_topic,n_init=5) estimators.fit(W) BetaPredict=estimators.cluster_centers_ Q = 2*BetaPredict.dot(BetaPredict.transpose()) Q = matrix(Q) P = W.dot(BetaPredict.transpose()) G = -np.eye(num_topic) G = matrix(G) h = np.zeros([num_topic,1]) h = matrix(h) A = np.ones([1,num_topic]) A = matrix(A) b = matrix(1.0) solvers.options['show_progress'] = False for i in range(num_topic): p = matrix(P[[i],:].transpose()) sol=solvers.qp(Q, p, G, h, A, b) ThetaPredict[:,[i]] = np.array(sol['x']) Err = ThetaPredict - Theta return np.square(np.linalg.norm(Err))
def solve(self, A, y, x0, as_signs): print 'cvxopt QP: # of params=%d' % A.shape[1] #flip sign of columns of A that have negative usefulness, a trick #Patrick Gill uses to halve the # of parameters for the QP solver A *= as_signs #the sign of x0 is unflipped for the nonzero elements, flip it x0 *= as_signs p = A.shape[1] H = cvxopt_matrix(np.dot(A.transpose(), A)) f = cvxopt_matrix(self.lambda_val - np.dot(A.transpose(), y)) b = cvxopt_matrix(np.zeros(p)) Q = cvxopt_matrix(-np.eye(p)) stime = time.time() sol = cvxopt_solvers.qp(H, f, Q, b, None, None, None, x0) etime = time.time() - stime print 'QP solver took %d seconds' % int(etime) xnew = np.array(sol['x']).squeeze() #unflip elements of xnew xnew *= as_signs return xnew
def optimize_mvo(returns, target_return, short_sales=False): """ Solves the MVO model: min x'Qx s.t. mu'x >= target_return e'x = 1 {x >= 0} - Short selling constraint """ means = np.mean(returns, axis=1) p = np.cov(returns) q = [0.0 for _ in range(num_stocks)] g = np.zeros((1, num_stocks)) + np.transpose(-1.0 * means) h = [-target_return] if not short_sales: g = np.concatenate((g, -np.eye(num_stocks)), axis=0) h = h + [0.0 for _ in range(num_stocks)] a = np.ones((1, num_stocks)) b = [1.0] solution = solvers.qp( matrix(2 * p), matrix(q), matrix(g), matrix(h), matrix(a), matrix(b) ) return solution['x']
def svm(pts, labels): """ Support Vector Machine using CVXOPT in Python. This example is mean to illustrate how SVMs work. """ n = len(pts[0]) # x is a column vector [w b]^T # set up P P = matrix(0.0, (n+1,n+1)) for i in range(n): P[i,i] = 1.0 # q^t x # set up q q = matrix(0.0,(n+1,1)) q[-1] = 1.0 m = len(pts) # set up h h = matrix(-1.0,(m,1)) # set up G G = matrix(0.0, (m,n+1)) for i in range(m): G[i,:n] = -labels[i] * pts[i] G[i,n] = -labels[i] x = solvers.qp(P,q,G,h)['x'] return P, q, h, G, x
def noshort(self,t): r=[] for i in t: r.append(LookupReturn(i)) Q = matrix(self.cv) p = matrix([0.0]*len(t)) A = matrix([1.0]*len(t),(1,len(t))) b = matrix(1.0) G = matrix(numpy.identity(len(t))) h = matrix([0.0]*len(t)) sol = solvers.qp(Q,p,-G,-h,A,b) ret,i = 0,0 pcts = "[" while i < len(sol['x']): pcov = PairCov(t[i],t[i],PC)[1] rt = round(r[i],2) print str(round(float(sol['x'][i]),4)).rjust(5),t[i].rjust(4) + " Ret",rt, "SD",pcov,"5%,1% VaR on $100K:",round(100000*norm.ppf(0.05,rt/1200.0,pcov),2),round(norm.ppf(0.01,rt/1200.0,pcov)*100000,2) pcts+="["+str(round(float(sol['x'][i]),4))+"]," ret += float(r[i])/100.0*round(float(sol['x'][i]),4) i += 1 pcts = pcts[:-1] + "]" yvec = numpy.transpose(numpy.matrix(pcts)) sd = numpy.transpose(yvec)*self.sigma*yvec self.ret=ret self.sd=sd return ret,yvec,self.sigma,r
def optim_central(pb): """ Returns the power distribution u_sol of the central static QP. keyword arguments: pb -- dictionary of the problem (nbr of users, time step, max resources, max admissible power, thermal resistance, Thermal capacity, vector of the init temperature, value of the exterior temperature, reference temperature, comfort factor, size of the prediction horizon) """ Rth = pb['Rth'] Text = pb['Text'] T_id = pb['T_id'] Umax = pb['Umax'] u_m = pb['u_m'] alpha = pb['alpha'] u_id = (T_id - Text) /Rth print(np.shape(alpha*u_id)) print(np.shape(2*Rth**2)) # Matrix definition P = matrix(2 * np.diag(alpha*Rth ** 2), tc='d') q = matrix(1 - 2*alpha*u_id * (Rth ** 2), tc='d') G = matrix(np.vstack((np.ones(len(Rth)), -np.identity(len(Rth)), np.identity(len(Rth)))), tc='d') h = matrix(np.hstack((Umax, np.zeros(len(Rth)), u_m)), tc='d') # Resolution sol = solvers.qp(P, q, G, h) # Solution u_sol = np.asarray(sol['x']).T[0] return u_sol,
def trainConvexLossModel(feature_matrix, observation_matrix, opttype='ls', weights=None): N = feature_matrix.shape[0] p = feature_matrix.shape[1] if weights == None: weights = np.eye(N) sol = None gradient = np.zeros((N,p+1)) loss = np.zeros((N,1)) #solves least squares problems if opttype == 'ls': weighted_feature_matrix = np.dot(np.transpose(feature_matrix), weights) kernel = matrix( np.dot(weighted_feature_matrix, feature_matrix) ) obs = matrix( -np.dot(weighted_feature_matrix, observation_matrix) ) #cvx do magic! sol = solvers.qp(kernel, obs) for i in range(0,N): residual = (np.dot(np.transpose(sol['x']),feature_matrix[i,:]) - observation_matrix[i]) gradient[i,0:p] = residual*np.transpose(sol['x']) gradient[i,p] = residual*-1 loss[i] = residual ** 2 return (sol,loss,gradient)
def qp_solver(Uk,Rk,param): print "u and gamma length: %d, %d" %(len(Uk),len(Rk)) U = numpy.matrix(sort_by_keys(Uk).values()) # T = numpy.transpose(U) R = sort_by_keys(Rk).values() P = numpy.dot(2,U*U.T) P = P.astype(float) # print "%d" % len(P) print sort_by_keys(Rk).keys()==sort_by_keys(Uk).keys() q = numpy.dot(-param,R) n = len(q) G = matrix(0.0, (n,n)) G[::n+1] = -1.0 # G = matrix(numpy.identity(n)) A = matrix(1.0,(1,n)) h = matrix(0.0,(n,1),tc='d') b = matrix(1.0,tc='d') solver = qp(matrix(P),matrix(q),G,h,A,b) alpha = matrix_to_array(solver['x']) s = two_lists_to_dictionary(Uk.keys(),alpha) L = s.items() L.sort(lambda x, y: -1 if x[1] > y[1] else 1) return L
def fit(self, X, max_iter=-1, center=True, normalize=True): """ :param X: Data matrix is assumed to be feats x samples. :param max_iter: *ignored*, just for compatibility. :return: Alphas and threshold for dual SVDDs. """ self.X = X.copy() dims, self.samples = X.shape if self.samples < 1: print('Invalid training data.') return -1 # number of training examples N = self.samples kernel = get_kernel(X, X, self.kernel, self.kparam) if center: kernel = center_kernel(kernel) if normalize: kernel = normalize_kernel(kernel) norms = np.diag(kernel).copy() if self.nu >= 1.0: print("Center-of-mass solution.") self.alphas = np.ones(self.samples) / float(self.samples) self.radius2 = 0.0 self.svs = np.array(range(self.samples), dtype='i') self.pobj = 0.0 # TODO: calculate real primal objective self.cTc = self.alphas[self.svs].T.dot( kernel[self.svs, :][:, self.svs].dot(self.alphas[self.svs])) return self.alphas, self.radius2 C = 1. / np.float(self.samples * self.nu) # generate a kernel matrix P = 2.0 * matrix(kernel) # this is the diagonal of the kernel matrix q = -matrix(norms) # sum_i alpha_i = A alpha = b = 1.0 A = matrix(1.0, (1, N)) b = matrix(1.0, (1, 1)) # 0 <= alpha_i <= h = C G1 = spmatrix(1.0, range(N), range(N)) G = sparse([G1, -G1]) h1 = matrix(C, (N, 1)) h2 = matrix(0.0, (N, 1)) h = matrix([h1, h2]) sol = qp(P, q, G, h, A, b) # store solution self.alphas = np.array(sol['x'], dtype=np.float) self.pobj = -sol['primal objective'] # find support vectors self.svs = np.where(self.alphas > self.PRECISION)[0] # self.cTc = self.alphas[self.svs].T.dot(kernel[self.svs, :][:, self.svs].dot(self.alphas[self.svs])) self.cTc = self.alphas.T.dot(kernel.dot(self.alphas)) # find support vectors with alpha < C for threshold calculation self.radius2 = 0. thres = self.predict(X[:, self.svs]) self.radius2 = np.min(thres) return self.alphas, thres
def train(self, list_Ktr, labels): ''' list_Ktr : list of kernels of the training examples labels : array of the labels of the training examples ''' self.list_Ktr = list_Ktr for k in self.list_Ktr: self.traces.append(self.traceN(k)) #return self.traces #print self.traces if self.tracenorm: #self.list_Ktr = [k / self.traceN(k) for k in list_Ktr] self.list_Ktr = [ k / self.traces[i] for i, k in enumerate(list_Ktr) ] #return self.list_Ktr[0] set_labels = set(labels) if len(set_labels) != 2: print 'The different labels are not 2' return None elif (-1 in set_labels and 1 in set_labels): self.labels = labels else: poslab = max(set_labels) self.labels = matrix( np.array([1. if i == poslab else -1. for i in labels])) # Sum of the kernels ker_matrix = matrix(self.sum_kernels(self.list_Ktr)) YY = matrix(np.diag(list(matrix(self.labels)))) KLL = (1.0 - self.lam) * YY * ker_matrix * YY LID = matrix(np.diag([self.lam] * len(self.labels))) Q = 2 * (KLL + LID) p = matrix([0.0] * len(self.labels)) G = -matrix(np.diag([1.0] * len(self.labels))) h = matrix([0.0] * len(self.labels), (len(self.labels), 1)) A = matrix([[1.0 if lab == +1 else 0 for lab in self.labels], [1.0 if lab2 == -1 else 0 for lab2 in self.labels]]).T b = matrix([[1.0], [1.0]], (2, 1)) solvers.options['show_progress'] = False #True sol = solvers.qp(Q, p, G, h, A, b) # Gamma: self.gamma = sol['x'] self.g1 = [x for x in self.gamma] # Bias for classification: bias = 0.5 * self.gamma.T * ker_matrix * YY * self.gamma self.bias = bias # Weights evaluation: yg = mul(self.gamma.T, self.labels.T) self.weights = [] for kermat in self.list_Ktr: b = yg * kermat * yg.T self.weights.append(b[0]) norm2 = sum([w for w in self.weights]) self.weights = [w / norm2 for w in self.weights] if self.tracenorm: for idx, val in enumerate(self.traces): self.weights[idx] = self.weights[idx] / val if True: ker_matrix = matrix(self.sum_kernels(self.list_Ktr, self.weights)) self.ker_matrix = ker_matrix YY = matrix(np.diag(list(matrix(self.labels)))) KLL = (1.0 - self.lam) * YY * ker_matrix * YY LID = matrix(np.diag([self.lam] * len(self.labels))) Q = 2 * (KLL + LID) p = matrix([0.0] * len(self.labels)) G = -matrix(np.diag([1.0] * len(self.labels))) h = matrix([0.0] * len(self.labels), (len(self.labels), 1)) A = matrix([[1.0 if lab == +1 else 0 for lab in self.labels], [1.0 if lab2 == -1 else 0 for lab2 in self.labels]]).T b = matrix([[1.0], [1.0]], (2, 1)) solvers.options['show_progress'] = False #True sol = solvers.qp(Q, p, G, h, A, b) # Gamma: self.gamma = sol['x'] return self
def updatesingleW(i): # optimize alpha using qp solver from cvxopt FA = base.matrix(np.float64(np.dot(-self.H, self.data[i, :].T))) al = solvers.qp(HA, FA, INQa, INQb) self.W[i, :] = np.array(al['x']).reshape((1, -1))
for i in range(len(totalH)): totalH_.append(totalH[i]/c_eff) q = matrix(totalH_*n) P = sparse([[spdiag([1/(c_eff*c_eff)]*1440)]*n]*n) for v in range(n): for t in range(1440): A[v,1440*v+t] = 1/60 #A[v+n,1440*v+t] = a_[v][t] G = sparse([spdiag([-1.0]*(n*1440)),spdiag([1.0]*(n*1440))]) h = matrix([0.0]*(n*1440)+[pMax]*(n*1440)) try: sol=solvers.qp(P,q,G,h,A,b) except: continue x = sol['x'] # without V2G if sol['status'] != 'optimal': continue # work out totol power demand and battery throughput total1 = [0.0]*1440 through1 = 0 for t in range(1440): total1[t] += totalH[t] for v in range(n): total1[t] += x[1440*v+t]/c_eff
def OptPort(naData, fTarget, naLower=None, naUpper=None, naExpected=None, s_type="long"): """ @summary Returns the Markowitz optimum portfolio for a specific return. @param naData: Daily returns of the various stocks (using returnize1) @param fTarget: Target return, i.e. 0.04 = 4% per period @param lPeriod: Period to compress the returns to, e.g. 7 = weekly @param naLower: List of floats which corresponds to lower portfolio% for each stock @param naUpper: List of floats which corresponds to upper portfolio% for each stock @return tuple: (weights of portfolio, min possible return, max possible return) """ ''' Attempt to import library ''' try: pass from cvxopt import matrix from cvxopt.blas import dot from cvxopt.solvers import qp, options except ImportError: print 'Could not import CVX library' raise ''' Get number of stocks ''' length = naData.shape[1] b_error = False naLower = deepcopy(naLower) naUpper = deepcopy(naUpper) naExpected = deepcopy(naExpected) # Assuming AvgReturns as the expected returns if parameter is not specified if (naExpected == None): naExpected = np.average(naData, axis=0) na_signs = np.sign(naExpected) indices, = np.where(na_signs == 0) na_signs[indices] = 1 if s_type == "long": na_signs = np.ones(len(na_signs)) elif s_type == "short": na_signs = np.ones(len(na_signs)) * (-1) naData = na_signs * naData naExpected = na_signs * naExpected # Covariance matrix of the Data Set naCov = np.cov(naData, rowvar=False) # If length is one, just return 100% single symbol if length == 1: return (list(na_signs), np.std(naData, axis=0)[0], False) if length == 0: return ([], [0], False) # If we have 0/1 "free" equity we can't optimize # We just use limits since we are stuck with 0 degrees of freedom ''' Special case for None == fTarget, simply return average returns and cov ''' if (fTarget is None): return (naExpected, np.std(naData, axis=0), b_error) # Upper bound of the Weights of a equity, If not specified, assumed to be 1. if (naUpper is None): naUpper = np.ones(length) # Lower bound of the Weights of a equity, If not specified assumed to be 0 (No shorting case) if (naLower is None): naLower = np.zeros(length) if sum(naLower) == 1: fPortDev = np.std(np.dot(naData, naLower)) return (naLower, fPortDev, False) if sum(naUpper) == 1: fPortDev = np.std(np.dot(naData, naUpper)) return (naUpper, fPortDev, False) naFree = naUpper != naLower if naFree.sum() <= 1: lnaPortfolios = naUpper.copy() # If there is 1 free we need to modify it to make the total # Add up to 1 if naFree.sum() == 1: f_rest = naUpper[~naFree].sum() lnaPortfolios[naFree] = 1.0 - f_rest lnaPortfolios = na_signs * lnaPortfolios fPortDev = np.std(np.dot(naData, lnaPortfolios)) return (lnaPortfolios, fPortDev, False) # Double the covariance of the diagonal elements for calculating risk. for i in range(length): naCov[i][i] = 2 * naCov[i][i] # Note, returns are modified to all be long from here on out (fMin, fMax) = getRetRange(False, naLower, naUpper, naExpected, "long") #print (fTarget, fMin, fMax) if fTarget < fMin or fTarget > fMax: print "Target not possible", fTarget, fMin, fMax b_error = True naLower = naLower * (-1) # Setting up the parameters for the CVXOPT Library, it takes inputs in Matrix format. ''' The Risk minimization problem is a standard Quadratic Programming problem according to the Markowitz Theory. ''' S = matrix(naCov) #pbar=matrix(naExpected) naLower.shape = (length, 1) naUpper.shape = (length, 1) naExpected.shape = (1, length) zeo = matrix(0.0, (length, 1)) I = np.eye(length) minusI = -1 * I G = matrix(np.vstack((I, minusI))) h = matrix(np.vstack((naUpper, naLower))) ones = matrix(1.0, (1, length)) A = matrix(np.vstack((naExpected, ones))) b = matrix([float(fTarget), 1.0]) # Optional Settings for CVXOPT options['show_progress'] = False options['abstol'] = 1e-25 options['reltol'] = 1e-24 options['feastol'] = 1e-25 # Optimization Calls # Optimal Portfolio try: lnaPortfolios = qp(S, -zeo, G, h, A, b)['x'] except: b_error = True if b_error == True: print "Optimization not Possible" na_port = naLower * -1 if sum(na_port) < 1: if sum(naUpper) == 1: na_port = naUpper else: i = 0 while (sum(na_port) < 1 and i < 25): naOrder = naUpper - na_port i = i + 1 indices = np.where(naOrder > 0) na_port[indices] = na_port[indices] + ( 1 - sum(na_port)) / len(indices[0]) naOrder = naUpper - na_port indices = np.where(naOrder < 0) na_port[indices] = naUpper[indices] lnaPortfolios = matrix(na_port) lnaPortfolios = (na_signs.reshape(-1, 1) * lnaPortfolios).reshape(-1) # Expected Return of the Portfolio # lfReturn = dot(pbar, lnaPortfolios) # Risk of the portfolio fPortDev = np.std(np.dot(naData, lnaPortfolios)) return (lnaPortfolios, fPortDev, b_error)
def fit(x, y, kernel, C=1000.0): eps = 0.1 # cvxoptの形式にデータを変換 m, n = x.shape y = y.reshape(-1, 1) data_size = len(x) P = np.zeros((m * 2, m * 2)) for i in range(m): for j in range(m): if kernel == None: P[i][j] = np.inner(x[i], x[j]) else: P[i][j] = eval(kernel)(x[i], x[j]) P[m + i][m + j] = P[i][j] P[i + m][j] = -1.0 * P[i][j] P[i][m + j] = -1.0 * P[i][j] P = matrix(P) q = np.zeros(m * 2) for i in range(m): q[i] = 1.0 * y[i] + eps q[i + m] = -1.0 * y[i] + eps q = matrix(q) tmp1 = -1.0 * np.diag(np.ones(m * 2)) tmp2 = np.diag(np.ones(m * 2)) G = matrix(np.vstack((tmp1, tmp2))) tmp1 = np.zeros(m * 2) tmp2 = np.ones(m * 2) * C h = matrix(np.hstack((tmp1, tmp2))) A = matrix(np.append(np.ones(m), -1.0 * np.ones(m)), (1, m * 2)) b = matrix(np.zeros(1)) # solverのパラメータの調整 solvers.options['show_progress'] = False solvers.options['abstol'] = 1e-10 solvers.options['reltol'] = 1e-10 solvers.options['feastol'] = 1e-10 # solverを実行 sol = solvers.qp(P, q, G, h, A, b) alphas = np.array(sol['x']) alphas = np.where(alphas < 1e-6, 0.0, alphas) S = (alphas > 1e-6).flatten() diff = (alphas[m:] - alphas[:m]).ravel() support_vector = np.arange(m)[(diff > eps) | (diff < -eps)] w = np.zeros(x.shape[1]) for i in range(len(x)): tmp_w = (alphas[m + i] - alphas[i]) * x[i] w += tmp_w b = np.zeros(len(x)) for i in range(len(x)): tmp_b_sum = 0 for j in range(len(x)): tmp_b = diff[j] * np.dot(x[i], x[j]) if kernel == None: tmp_b = diff[j] * np.dot(x[i], x[j]) else: tmp_b = diff[j] * eval(kernel)(x[i], x[j]) tmp_b_sum += tmp_b if diff[i] > 0: b[i] = -y[i] + tmp_b_sum + eps else: b[i] = -y[i] + tmp_b_sum - eps b = np.average(b[support_vector]) return alphas, w, b, diff
def p_constrain(list_p,a0=0,v0=0,ak=0,vk=0): global p,M #define constrains: firstly equal constrains #p p=np.ones([4*k+2,1])*0.0 p[0][0]=list_p[0] p[1][0]=a0 p[2][0]=v0 # p.append([list_p[0]]) # p.append([a0]) # p.append([v0]) for i in range(1,k): p[i+2][0]=list_p[i] p[k+2][0]=list_p[k] p[k+3][0]=ak p[k+4][0]=vk # p.append([list_p[k]]) # p.append([ak]) # p.append([vk]) print('p is done') print('k+5的值:',k+5) print('p的总长度:',len(p),'4k+2的值',4*k+2) p=np.array(p) print(p.shape) print('-' * 60) M=np.ones([4*k+2,(n+1)*k])*0.0 #M1 M1=np.ones([k-1+3+3,(n+1)*k])*0.0 #p0,v0,a0 for i in range(n+1): M1[0][i]=t_to[0]**i if i==n: continue M1[1][i+1]=(i+1)*t_to[0]**i if i==n-1: continue M1[2][i+2]=(i+2)*(i+1)*t_to[0]**i #middle p for j in range(3,k+2): for i in range(n + 1): M1[j][i+(j-2)*(n+1)] = t_to[j-2] ** i #pk,vk,ak for i in range(n+1): M1[k+2][i+(k-1)*(n+1)]=t_to[-1]**i if i==n: continue M1[k+3][i+1+(k-1)*(n+1)]=(i+1)*t_to[-1]**i if i==n-1: continue M1[k+4][i+2+(k-1)*(n+1)]=(i+2)*(i+1)*t_to[-1]**i print('M1 is done') print(M1.shape) print('-' * 60) #definew constrains:secondly unequal constrains M2=np.ones([3*k-3,(n+1)*k])*0.0 j=0 l=0 while l<3*(k-1): for i in range(n+1): M2[l][j*(n+1)+i]=t_to[j+1]**i M2[l][j * (n + 1) + i+(n+1)] = -t_to[j+1] ** i if i == n: continue M2[l+1][j*(n+1)+i + 1] = (i + 1) * t_to[j+1] ** i M2[l+1][j*(n+1)+i + 1+(n+1)] = -(i + 1) * t_to[j+1] ** i if i == n - 1: continue M2[l+2][j*(n+1)+i + 2] = (i + 2) * (i + 1) * t_to[j+1] ** i M2[l+2][j*(n+1)+i + 2+(n+1)] = - (i + 2) * (i + 1) * t_to[j+1] ** i j+=1 l+=3 print('M2 is done') print(M2.shape) print('-' * 60) #combine M1 and M2 to M #M=np.ones([4*k+2,(n+1)*k])*0.0 for i in range(4*k+2): for j in range((n+1)*k): if i<k+5: M[i][j]=M1[i][j] else: M[i][j]=M2[i-k-5][j] # for i in range(26): # print(M[i]) # if i<11: # print(M1[i]) # else: # print(M2[i-11]) # print('-'*60) print('M is done') print(M.shape) print('-' * 60) # figure_out() global Q_all q = np.ones([len(Q_all), 1]) q = np.mat(q) # cvxopt_solve_qp(P=Q_all, q=q,A=M, b=p) # q=np.transpose(q) # Q_all=np.transpose(Q_all) # M=np.transpose(M) # p=np.transpose(p) q = matrix(q) Q_all = matrix(Q_all) M = matrix(M) p = matrix(p) # p=matrix([3.0,4.0,0.0,0.0,0.0,0.0,0,0,0,0]) # print(Q_all.shape) # print(M.shape) # print(p.shape) result = solvers.qp(P=Q_all, q=q, A=M, b=p) return result['x']
def tangency_portfolio(cov_mat, exp_rets, allow_short=False): """ Computes a tangency portfolio, i.e. a maximum Sharpe ratio portfolio. Note: As the Sharpe ratio is not invariant with respect to leverage, it is not possible to construct non-trivial market neutral tangency portfolios. This is because for a positive initial Sharpe ratio the sharpe grows unbound with increasing leverage. Parameters ---------- cov_mat: pandas.DataFrame Covariance matrix of asset returns. exp_rets: pandas.Series Expected asset returns (often historical returns). allow_short: bool, optional If 'False' construct a long-only portfolio. If 'True' allow shorting, i.e. negative weights. Returns ------- weights: pandas.Series Optimal asset weights. """ if not isinstance(cov_mat, pd.DataFrame): raise ValueError("Covariance matrix is not a DataFrame") if not isinstance(exp_rets, pd.Series): raise ValueError("Expected returns is not a Series") if not cov_mat.index.equals(exp_rets.index): raise ValueError("Indices do not match") n = len(cov_mat) P = opt.matrix(cov_mat.values) q = opt.matrix(0.0, (n, 1)) # Constraints Gx <= h if not allow_short: # exp_rets*x >= 1 and x >= 0 G = opt.matrix(np.vstack((-exp_rets.values, -np.identity(n)))) h = opt.matrix(np.vstack((-1.0, np.zeros((n, 1))))) else: # exp_rets*x >= 1 G = opt.matrix(-exp_rets.values).T h = opt.matrix(-1.0) # Solve optsolvers.options['show_progress'] = False sol = optsolvers.qp(P, q, G, h) if sol['status'] != 'optimal': warnings.warn("Convergence problem") # Put weights into a labeled series weights = pd.Series(sol['x'], index=cov_mat.index) # Rescale weights, so that sum(weights) = 1 weights /= weights.sum() return weights
for i in range(len(train_data)): for j in range(len(train_data)): P[i][j] = train_data[i][2] * train_data[j][2] * kernal( case, train_data[i][0:2], train_data[j][0:2], t) # P[i][j] = train_data[i][2]*train_data[j][2]*k.radicalbasekernal(train_data[i][0:2], train_data[j][0:2], t) " ------------------------- Without Slack variable -------------------------- " " --- The q, G and h matrix ---" q = -1 * np.ones(len(train_data)) h = np.zeros(len(train_data)) G = -1 * np.eye(len(train_data)) " --- Convex Optimization ---" print("") print("Without Slack") r = qp(matrix(P), matrix(np.transpose(q)), matrix(G), matrix(h)) alpha = list(r['x']) # Variables landmarks = [] non_zero_alpha = [] " --- Indicator function ---" for i in range(len(alpha)): if (alpha[i] > math.pow(10, -5)): non_zero_alpha.append(alpha[i]) landmarks.append(train_data[i][0:3]) " --- Plot Data without slack ---" pylab.figure(1) pylab.xlim(-7, 7) pylab.ylim(-7, 7)
def update(self, measurement, dt_new=None): """ Update (both prediction and correction steps) of Kalman filter, when new measurement comes in. Args: measurement : double measured angle - yaw or pitch dt_new : double (optional) current time step to use, if not supplied then the default time step specified in constructor is used Returns: (a, av, aa) : doubles tuple of new state estimate: angle (a), angular velocity (av), angular acceleration (aa) """ self.n_iter += 1 self.x_est_prior.append(np.zeros(self.x_sz)) self.x_est_post.append(np.zeros(self.x_sz)) self.P_prior.append(np.zeros(self.P_sz)) self.P_post.append(np.zeros(self.P_sz)) self.K.append(np.zeros(self.x_sz)) self.measurements.append(measurement) # log measurement if dt_new == None: F = self.F # transition matrix as in previous step else: F = np.matrix([[1, dt_new, dt_new * dt_new / 2], [0, 1, dt_new], [0, 0, 1]]) # new transition matrix # Time update (prediction) self.P_prior[ self.n_iter] = F * self.P_post[self.n_iter - 1] * F.T + self.Q self.x_est_prior[self.n_iter] = F * self.x_est_post[self.n_iter - 1] # Measurement update (correction) self.K[self.n_iter] = self.P_prior[self.n_iter] * self.H.T / ( self.H * self.P_prior[self.n_iter] * self.H.T + self.R)[0, 0] self.x_est_post[self.n_iter] = self.x_est_prior[ self.n_iter] + self.K[self.n_iter] * ( measurement - self.H * self.x_est_prior[self.n_iter])[0, 0] self.P_post[self.n_iter] = (np.identity( self.N) - self.K[self.n_iter] * self.H) * self.P_prior[self.n_iter] # Constrain the position naively ''' very simple without adjustment of velocity and acceleration if(self.x_min != None): self.x_est_post[self.n_iter][0,0] = max(self.x_est_post[self.n_iter][0,0], self.x_min[0]) if(self.x_max != None): self.x_est_post[self.n_iter][0,0] = min(self.x_est_post[self.n_iter][0,0], self.x_max[0]) ''' # Constrain the position and velocity - quadratic programming if self.x_min != None and self.x_max != None: # inverse of state covariance matrix => max probability estimate subject to constraints W = self.P_post[self.n_iter].I P = 2 * matrix(np.array(W, dtype='d')) q = matrix( np.array(-2 * W.T * self.x_est_post[self.n_iter], dtype='d')) # G = matrix(np.array([ [1,0,0], [-1,0,0] ], dtype='d')) # if only position is constrained # h = matrix(np.array([self.x_max[0], -self.x_min[0]], dtype='d')) # if only position is constrained G = matrix( np.array([[1, 0, 0], [-1, 0, 0], [0, 1, 0], [0, -1, 0]], dtype='d') ) # if both position and velocity are constrained h = matrix( np.array([ self.x_max[0], -self.x_min[0], self.x_max[1], -self.x_min[1] ], dtype='d') ) # if both position and velocity are constrained # Suppressed output from QP solver solvers.options['show_progress'] = False sol = solvers.qp(P, q, G, h) # sol['x'] = x = argmin( x.T*W *x - 2*x_est_post.T*W*x ) # If optimal solution was found, then adjust the state estimate x_est_post if sol['status'] == 'optimal': self.x_est_post[self.n_iter] = np.array(sol['x']) else: print "not optimal ... !" # Equivalent to call to getLastEstState(): # Return new state estimate: angle, angular velocity, angular acceleration return (self.x_est_post[self.n_iter][0, 0], self.x_est_post[self.n_iter][1, 0], self.x_est_post[self.n_iter][2, 0])
def lsqlin(C, d, reg=0, A=None, b=None, Aeq=None, beq=None, \ lb=None, ub=None, x0=None, opts=None): ''' Solve linear constrained l2-regularized least squares. Can handle both dense and sparse matrices. Matlab's lsqlin equivalent. It is actually wrapper around CVXOPT QP solver. min_x ||C*x - d||^2_2 + reg * ||x||^2_2 s.t. A * x <= b Aeq * x = beq lb <= x <= ub Input arguments: C is m x n dense or sparse matrix d is n x 1 dense matrix reg is regularization parameter A is p x n dense or sparse matrix b is p x 1 dense matrix Aeq is q x n dense or sparse matrix beq is q x 1 dense matrix lb is n x 1 matrix or scalar ub is n x 1 matrix or scalar Output arguments: Return dictionary, the output of CVXOPT QP. Dont pass matlab-like empty lists to avoid setting parameters, just use None: lsqlin(C, d, 0.05, None, None, Aeq, beq) #Correct lsqlin(C, d, 0.05, [], [], Aeq, beq) #Wrong! ''' if sparse.issparse(A): #detects both np and cxopt sparse sparse_case = True #We need A to be scipy sparse, as I couldn't find how #CVXOPT spmatrix can be vstacked if isinstance(A, spmatrix): A = spmatrix_sparse_to_scipy(A) else: sparse_case = False C = numpy_to_cvxopt_matrix(C) d = numpy_to_cvxopt_matrix(d) Q = C.T * C q = - d.T * C nvars = C.size[1] if reg > 0: if sparse_case: I = scipy_sparse_to_spmatrix(sparse.eye(nvars, nvars,\ format='coo')) else: I = matrix(np.eye(nvars), (nvars, nvars), 'd') Q = Q + reg * I lb = cvxopt_to_numpy_matrix(lb) ub = cvxopt_to_numpy_matrix(ub) b = cvxopt_to_numpy_matrix(b) if lb is not None: #Modify 'A' and 'b' to add lb inequalities if lb.size == 1: lb = np.repeat(lb, nvars) if sparse_case: lb_A = -sparse.eye(nvars, nvars, format='coo') A = sparse_None_vstack(A, lb_A) else: lb_A = -np.eye(nvars) A = numpy_None_vstack(A, lb_A) b = numpy_None_concatenate(b, -lb) if ub is not None: #Modify 'A' and 'b' to add ub inequalities if ub.size == 1: ub = np.repeat(ub, nvars) if sparse_case: ub_A = sparse.eye(nvars, nvars, format='coo') A = sparse_None_vstack(A, ub_A) else: ub_A = np.eye(nvars) A = numpy_None_vstack(A, ub_A) b = numpy_None_concatenate(b, ub) #Convert data to CVXOPT format A = numpy_to_cvxopt_matrix(A) Aeq = numpy_to_cvxopt_matrix(Aeq) b = numpy_to_cvxopt_matrix(b) beq = numpy_to_cvxopt_matrix(beq) #Set up options if opts is not None: for k, v in opts.items(): solvers.options[k] = v #Run CVXOPT.SQP solver sol = solvers.qp(Q, q.T, A, b, Aeq, beq, None, x0) return sol
def get_exact_PRC_qp(model_name): root_folder = get_project_root() file = os.path.join(root_folder, "data", "model_params", f"{model_name}_params.json") params = json.load(open(file, 'r')) model = eval(f"{model_name}(params)") root_folder = get_project_root() file = os.path.join(root_folder, "data", "limit_cycles", f"{model_name}_limit_cycle.pkl") data_lim_cycle = pickle.load(open(file, "rb+")) T = data_lim_cycle["T"] omega = (2 * np.pi) / T signal = data_lim_cycle["limit_cycle"] dt = data_lim_cycle["dt"] num_points = data_lim_cycle["num_points"] x0 = data_lim_cycle["x0"] params = data_lim_cycle["params"] t = np.arange(num_points + 1) * dt phase = t * omega T = data_lim_cycle["T"] N = signal.shape[1] # number of ``channels'' M = signal.shape[0] # time points # dz / dt = -D^T z is approximated by # z_{n+1} - z_{n} + 0.5 dt D^T(x_{n+1})Z_{n+1} + 0.5 dt D^T(x_{n})Z_{n} = 0 # C z = 0, where z has the shape N * M - vector of Z_{n} # C = (-I + 0.5 dt D^T(x_n)) + cyclically shifted up by one row (I + 0.5 dt D^T(x_{n+1})) # C has (N * M, N*M) dimensions # and the constraints Bz = omega # associated d phi/ dt = (F(t), Z(t)) = w # B is made out from F^T(x_{n}) and has (M, N*M) dimensions C_1 = np.zeros((N * M, N * M)) C_2 = np.zeros((N * M, N * M)) B = np.zeros((M, N * M)) for t_i in range(M): i_start = N * t_i i_finish = N * (t_i + 1) C_1[i_start:i_finish, i_start:i_finish] = -np.eye(N) + 0.5 * dt * model.jac_rhs( signal[t_i, :]).T C_2[i_start:i_finish, i_start:i_finish] = np.eye(N) + 0.5 * dt * model.jac_rhs(signal[ (t_i + 1) % M, :]).T B[t_i, i_start:i_finish] = model.rhs_(signal[t_i, :]) C = C_1 + np.roll(C_2, N, axis=1) # # Least norm solution (no hard constraints) using solver alpha = 1e-9 solvers.options['show_progress'] = True # (Cx - h)^T @ (Cx - h) = x^T C^T C x - 2 h^T C x + h^T h P = C.T @ C q = np.zeros(N * M) sol = solvers.qp(P=matrix(P), q=matrix(q), A=matrix(B), b=matrix(omega * np.ones(M))) x = np.array(sol['x']) Z = x.reshape(M, N) prc_exact_data = dict() prc_exact_data["delta_phi"] = Z[:, 0] prc_exact_data["Z"] = Z prc_exact_data["phi"] = phase prc_exact_data["signal"] = signal prc_exact_data["params"] = params prc_exact_data["omega"] = omega return prc_exact_data
G[m:2 * m, :n] = -A G[m:2 * m, n:n + m] = spmatrix(-1.0, range(m), range(m)) G[m:2 * m, n + m:] = spmatrix(-1.0, range(m), range(m)) h[m:2 * m] = -b # u >= 0 G[2 * m:3 * m, n:n + m] = spmatrix(-1.0, range(m), range(m)) # u <= 1 G[3 * m:4 * m, n:n + m] = spmatrix(1.0, range(m), range(m)) h[3 * m:4 * m] = 1.0 # v >= 0 G[4 * m:, n + m:] = spmatrix(-1.0, range(m), range(m)) xh = solvers.qp(P, q, G, h)['x'][:n] try: import pylab except ImportError: pass else: pylab.figure(1, facecolor='w') pylab.plot(u, v, 'o', [-11, 11], [xh[0] - 11 * xh[1], xh[0] + 11 * xh[1]], '-g', [-11, 11], [xls[0] - 11 * xls[1], xls[0] + 11 * xls[1]], '--r', markerfacecolor='w', markeredgecolor='b') pylab.axis([-11, 11, -20, 25])
n = 4 S = matrix([[4e-2, 6e-3, -4e-3, 0.0], [6e-3, 1e-2, 0.0, 0.0], [-4e-3, 0.0, 2.5e-3, 0.0], [0.0, 0.0, 0.0, 0.0]]) pbar = matrix([.12, .10, .07, .03]) G = matrix(0.0, (n, n)) G[::n + 1] = -1.0 h = matrix(0.0, (n, 1)) A = matrix(1.0, (1, n)) b = matrix(1.0) N = 100 mus = [10**(5.0 * t / N - 1.0) for t in range(N)] options['show_progress'] = False xs = [qp(mu * S, -pbar, G, h, A, b)['x'] for mu in mus] returns = [dot(pbar, x) for x in xs] risks = [sqrt(dot(x, S * x)) for x in xs] try: import pylab except ImportError: pass else: pylab.figure(1, facecolor='w') pylab.plot(risks, returns) pylab.xlabel('standard deviation') pylab.ylabel('expected return') pylab.axis([0, 0.2, 0, 0.15]) pylab.title('Risk-return trade-off curve (fig 4.12)') pylab.yticks([0.00, 0.05, 0.10, 0.15])
def optim_quadrotor(num_horizon, P, Pdot, Pddot, x_init, y_init, vx_init, vy_init, ax_init, ay_init, x_fin, y_fin, vx_fin, vy_fin, ax_fin, ay_fin, ax_max, ay_max, vx_max, vy_max, jx_max, jy_max): num = num_horizon nvar = shape(P)[1] nvar_offset = nvar - 3 cx_1 = x_init cx_2 = vx_init cx_3 = ax_init cy_1 = y_init cy_2 = vy_init cy_3 = ay_init x_offset = cx_1 * P[1:num, 0] + cx_2 * P[1:num, 1] + cx_3 * P[1:num, 2] vx_offset = cx_1 * Pdot[1:num, 0] + cx_2 * Pdot[1:num, 1] + cx_3 * Pdot[1:num, 2] ax_offset = cx_1 * Pddot[1:num, 0] + cx_2 * Pddot[1:num, 1] + cx_3 * Pddot[ 1:num, 2] y_offset = cy_1 * P[1:num, 0] + cy_2 * P[1:num, 1] + cy_3 * P[1:num, 2] vy_offset = cy_1 * Pdot[1:num, 0] + cy_2 * Pdot[1:num, 1] + cy_3 * Pdot[1:num, 2] ay_offset = cy_1 * Pddot[1:num, 0] + cy_2 * Pddot[1:num, 1] + cy_3 * Pddot[ 1:num, 2] A_x_fin = hstack((P[-1, 3:nvar].reshape(1, nvar - 3), zeros( (1, nvar - 3)))) B_x_fin = array([x_fin - x_offset[-1]]) A_y_fin = hstack((zeros((1, nvar - 3)), P[-1, 3:nvar].reshape(1, nvar - 3))) B_y_fin = array([y_fin - y_offset[-1]]) A_pos_fin = vstack((A_x_fin, A_y_fin)) B_pos_fin = hstack((B_x_fin, B_y_fin)) A_vel_fin = block_diag(Pdot[-1, 3:nvar], Pdot[-1, 3:nvar]) B_vel_fin = hstack((vx_fin - vx_offset[-1], vy_fin - vy_offset[-1])) A_acc_fin = block_diag(Pddot[-1, 3:nvar], Pddot[-1, 3:nvar]) B_acc_fin = hstack((ax_fin - ax_offset[-1], ay_fin - ay_offset[-1])) ####################################################################### A_vx_ineq = vstack((Pdot[1:num, 3:nvar], -Pdot[1:num, 3:nvar])) B_vx_ineq = hstack((vx_max * ones(num - 1) - vx_offset, vx_max * ones(num - 1) + vx_offset)) A_ax_ineq = vstack((Pddot[1:num, 3:nvar], -Pddot[1:num, 3:nvar])) B_ax_ineq = hstack((ax_max * ones(num - 1) - ax_offset, ax_max * ones(num - 1) + ax_offset)) A_jx_ineq = vstack((identity(nvar_offset), -identity(nvar_offset))) B_jx_ineq = hstack( (jx_max * ones(nvar_offset), jx_max * ones(nvar_offset))) A_vy_ineq = vstack((Pdot[1:num, 3:nvar], -Pdot[1:num, 3:nvar])) B_vy_ineq = hstack((vy_max * ones(num - 1) - vy_offset, vy_max * ones(num - 1) + vy_offset)) A_ay_ineq = vstack((Pddot[1:num, 3:nvar], -Pddot[1:num, 3:nvar])) B_ay_ineq = hstack((ay_max * ones(num - 1) - ay_offset, ay_max * ones(num - 1) + ay_offset)) A_jy_ineq = vstack((identity(nvar_offset), -identity(nvar_offset))) B_jy_ineq = hstack( (jy_max * ones(nvar_offset), jy_max * ones(nvar_offset))) A_vel_ineq = block_diag(A_vx_ineq, A_vy_ineq) B_vel_ineq = hstack((B_vx_ineq, B_vy_ineq)) A_acc_ineq = block_diag(A_ax_ineq, A_ay_ineq) B_acc_ineq = hstack((B_ax_ineq, B_ay_ineq)) A_jerk_ineq = block_diag(A_jx_ineq, A_jy_ineq) B_jerk_ineq = hstack((B_jx_ineq, B_jy_ineq)) A_ineq = vstack((A_vel_ineq, A_acc_ineq, A_jerk_ineq)) B_ineq = hstack((B_vel_ineq, B_acc_ineq, B_jerk_ineq)) A_eq = vstack((A_vel_fin, A_acc_fin)) B_eq = hstack((B_vel_fin, B_acc_fin)) weight = 100 cost = weight * dot(A_pos_fin.T, A_pos_fin) + 0.01 * identity( 2 * nvar_offset) linccost = -weight * dot(A_pos_fin.T, B_pos_fin) sol = solvers.qp(cvxopt.matrix(cost, tc='d'), cvxopt.matrix(linccost, tc='d'), cvxopt.matrix(A_ineq, tc='d'), cvxopt.matrix(B_ineq, tc='d'), cvxopt.matrix(A_eq, tc='d'), cvxopt.matrix(B_eq, tc='d')) sol = array(sol['x']).squeeze() cx = sol[0:nvar_offset] cy = sol[nvar_offset:2 * nvar_offset] cx_sol = hstack((cx_1, cx_2, cx_3, cx)) cy_sol = hstack((cy_1, cy_2, cy_3, cy)) x = dot(P, cx_sol) xdot = dot(Pdot, cx_sol) xddot = dot(Pddot, cx_sol) y = dot(P, cy_sol) ydot = dot(Pdot, cy_sol) yddot = dot(Pddot, cy_sol) return x, xdot, xddot, y, ydot, yddot
def _coneqp(self, gram, targets, probs): n_quantiles = probs.size # Number of quantiles to predict n_coefs = gram.shape[0] # Number of variables n_samples = n_coefs // n_quantiles # Quantiles levels kronprobs = kron(ones(int(n_coefs / n_quantiles)), probs.squeeze()) solvers.options['show_progress'] = self.verbose if self.tol: solvers.options['reltol'] = self.tol if self.eps == 0: # Quadratic part of the objective gram = matrix(gram) # Linear part of the objective q_lin = matrix(-kron(targets, ones(n_quantiles))) # LHS of the inequality constraint g_lhs = matrix(r_[eye(n_coefs), -eye(n_coefs)]) # RHS of the inequality h_rhs = matrix(r_[self.reg_c_ * kronprobs, self.reg_c_ * (1 - kronprobs)]) # LHS of the equality constraint lhs_eqc = matrix(kron(ones(n_samples), eye(n_quantiles))) # Solve the dual optimization problem self.sol_ = solvers.qp(gram, q_lin, g_lhs, h_rhs, lhs_eqc, matrix(zeros(n_quantiles))) # Set coefs coefs = asarray(self.sol_['x']) else: def build_lhs(m, p): # m: n_samples # p: n_quantiles n = m*p # n_variables # Get the norm bounds (m last variables) A = zeros(p+1) A[0] = -1 A = kron(eye(m), A).T # Get the m p-long vectors B = kron(eye(m), c_[zeros(p), eye(p)].T) # Box constraint C = c_[r_[eye(n), -eye(n)], zeros((2*n, m))] # Set everything together C = r_[C, c_[B, A]] return C # Quadratic part of the objective gram = matrix(r_[c_[gram, zeros((n_coefs, n_samples))], zeros((n_samples, n_coefs+n_samples))]) # Linear part of the objective q_lin = matrix(r_[-kron(targets, ones(n_quantiles)), ones(n_samples)*self.eps]) # LHS of the inequality constraint g_lhs = matrix(build_lhs(n_samples, n_quantiles)) # RHS of the inequality h_rhs = matrix(r_[self.reg_c_ * kronprobs, self.reg_c_ * (1-kronprobs), zeros(n_samples * (n_quantiles+1))]) # LHS of the equality constraint lhs_eqc = matrix(c_[kron(ones(n_samples), eye(n_quantiles)), zeros((n_quantiles, n_samples))]) # Parameters of the optimization problem dims = {'l': 2*n_coefs, 'q': [n_quantiles+1]*n_samples, 's': []} # Solve the dual optimization problem self.sol_ = solvers.coneqp(gram, q_lin, g_lhs, h_rhs, dims, lhs_eqc, matrix(zeros(n_quantiles))) # Set coefs coefs = asarray(self.sol_['x'][:n_coefs]) # Set the intercept # Erase the previous intercept before prediction self.model_ = {'coefs': coefs, 'intercept': 0} predictions = self.predict(self.linop_.X) if predictions.ndim < 2: predictions = predictions.reshape(1, -1) # 2D array intercept = [percentile(targets - pred, 100. * prob) for (pred, prob) in zip(predictions, probs)] intercept = asarray(intercept).squeeze() self.model_ = {'coefs': coefs, 'intercept': intercept}
def Interp_corridor(p0, p1, T, deg): #ti = normalize(p1[0,:] - p0[0,:]) # unit vector delta = 0.05 # corridor width is 0.05m k = deg # highest degree dim = p0[0].size coeff = np.zeros((k + 1, dim)) # scale the derivaties with T p0[1, :] *= T p0[2, :] *= T**2 p0[3, :] *= T**3 p1[1, :] *= T p1[2, :] *= T**2 p1[3, :] *= T**3 # H, f, A is the same for each dimension hh = 1.0 / np.arange(1, 2 * k - 6) # build Hblock from hh Hblock = np.zeros((k - 3, k - 3)) for ii in range(k - 3): Hblock[ii, :] = hh[ii:ii + k - 3] # H is (k-3) by (k-3) Htemp = np.vstack((np.zeros( (4, k + 1)), np.hstack((np.zeros((k - 3, 4)), Hblock)))) kscale = np.zeros((k + 1, )) # scaling for ii in range(k + 1): if (ii == 4): kscale[ii] = 1 * 2 * 3 * 4.0 if (ii > 4): kscale[ii] = kscale[ii - 1] * ii / (ii - 4) H = Htemp H = kscale[:, None] * Htemp * kscale # scaling each dimension, not matrix multiplication f = np.zeros((k + 1, 1)) A = np.zeros((8, k + 1)) # init A matrix for i in range(k + 1): A[0, 0] = 1 A[1, 1] = 1 A[2, 2] = 2 A[3, 3] = 6 A[4, :] = np.ones((1, k + 1)) A[5, :] = np.arange(k + 1) A[6, :] = A[5, :] * (np.arange(k + 1) - 1) A[7, :] = A[6, :] * (np.arange(k + 1) - 2) A = A.astype(float) # convert to double to avoid errors # b is different for each dimension for i in range(dim): # inequality constraint G = np.empty((0, k + 1)) h = np.empty((0, 1)) if (i >= 1): # y/z corridor, yt-p0[1]<=delta, p0[1]-yt<=delta for tt in np.array([0.2, 0.4, 0.6, 0.8]): h = np.vstack((h, np.array([[p0[0, i] + delta]]), np.array([[delta - p0[0, i]]]))) G = np.vstack((G, tt**np.arange(k + 1), -tt**np.arange(k + 1))) b = np.append(p0[:, i], p1[:, i])[:, None] # 0.5x^THx+f^Tx, s.t. Gx<=h, Ax=b sol = solvers.qp(matrix(H), matrix(f), matrix(G), matrix(h), matrix(A), matrix(b)) x = sol['x'] print sol['status'] print sol['primal objective'] coeff[:, i] = np.reshape(x, (k + 1, )) print coeff[:, i] #print 0.5*np.dot(np.dot(H,coeff[:,i]), coeff[:,i]) #print np.dot(A, coeff[:,i])-b.T[0] print H.shape, f.shape, A.shape, b.shape return coeff
length = len(data) q = [[-1.0 for i in range(length)]] h = [[0.0 for i in range(length)]] G = [[0.0 for col in range(length)] for row in range(length)] for i in range(length): G[i][i] = -1.0 P = buildP(data) print(len(P), len(P[0])) r = qp(matrix(P), matrix(q), matrix(G), matrix(h)) alpha = list(r['x']) #print (alpha) points = [] for i in range(length): if alpha[i] > 0.00001: points.append((alpha[i], data[i])) pylab.hold(True) pylab.plot([p[0] for p in classA], [p[1] for p in classA], 'bo') pylab.plot([p[0] for p in classB], [p[1] for p in classB], 'ro') #pylab.show()
def fit(self, X_data, Y_labels, pos_label=1, C=0.5, min_sv_alpha=0.0001): # train with data self.data = np.asarray(X_data) self.Y_labels = np.asarray(Y_labels) self.labels = np.ndarray(shape=self.Y_labels.shape[0]) self.C = C self.min_sv_alpha = min_sv_alpha if type(self.labels) == 'String' and pos_label == 1: print("Error in arguments. Please pass a value for pos_label.") return -1 elif type(self.labels[0] == 'String' and type(pos_label) == 'String'): self.pos_label = pos_label for i in range(self.Y_labels.shape[0]): if self.Y_labels[i] == pos_label: self.labels[i] = float(1) else: self.labels[i] = float(-1) # print(self.rbf_kernel(self.data, Y_labels)) self.K = np.zeros((self.data.shape[0], self.data.shape[0])) for i in range(self.data.shape[0]): for j in range(self.data.shape[0]): self.K[i, j] = np.dot(self.data[i], self.data[j]) Q = matrix( np.matmul(np.outer(self.labels, self.labels), self.K).astype('double')) p = matrix(np.ones(self.K.shape[0]) * -1) #G = matrix(np.diag(np.ones(self.data.shape[0]) * -1)) #h = matrix(np.zeros(self.data.shape[0])) G_x = matrix(np.diag(np.ones(self.K.shape[0]) * -1)) h_x = matrix(np.zeros(self.K.shape[0])) G_slack = matrix(np.diag(np.ones(self.K.shape[0]))) h_slack = matrix(np.ones(self.K.shape[0]) * self.C) G = matrix(np.vstack((G_x, G_slack))) h = matrix(np.vstack((h_x, h_slack))) A = matrix(self.labels.astype('double'), (1, self.K.shape[0])) b = matrix(0.0) solution = solvers.qp(Q, p, G, h, A, b, kktsolver='ldl') print(np.asarray(solution['x']).shape) # the solver outputs the alphas (lagrange multipliers) self.alphas = np.asarray(solution['x'])[:, 0] print('Lagrange multipliers:') print(self.alphas) # save the support vectors (i.e. the ones with non-zero alphas) and their y values sv_index = np.where((self.min_sv_alpha < self.alphas) & (self.alphas < self.C)) self.support_alphas = self.alphas[sv_index] self.support_vectors = self.data[sv_index] self.targets = self.labels[sv_index] print('\nNumber of support vectors:', self.support_vectors.shape[0], end='\n\n') self.b = 0 for i, yi in enumerate(self.targets): m = yi for j, xj in enumerate(self.support_vectors): m -= self.alphas[j] * self.targets[j] * self.support_vectors[i] self.b += m self.b /= self.targets.shape[0] self.w = 0 for i, yi in enumerate(self.targets): self.w += self.alphas[i] * self.targets[i] * self.support_vectors[i] self.w_norm = np.linalg.norm(np.asarray(self.w))
def mkl(km_list, labels, c_type): """Multiple kernel learning with UNIMKL, ALIGN, ALIGNF. The learned weights for kernels and the combined kernel are written out. Parameters: ----------- km_list: list of numpy 2d array, a list of kernel matrix labels: numpy 2d array, the output c_type: str, the type of MKL, must be one of'UNIMKL', 'ALIGN', 'ALIGNF' Returns: -------- train_km, numpy 2d array, combined kernel """ print "Computing combined kernel for", c_type n_km = len(km_list) if c_type == 'UNIMKL': train_km = numpy.zeros(km_list[0].shape) for km in km_list: train_km = train_km + km train_km = normalize_km(train_km) w = np.ones(n_km) / n_km #numpy.savetxt(out_f, train_km) elif c_type == 'ALIGN': """Two stage model, weight as idenpedently centered alignment score""" ky = numpy.dot(labels, labels.T) ky_c = center(ky) #ky_c = normalize_km(ky_c) ky_n = f_norm(ky_c) w = [] train_km = numpy.zeros(km_list[0].shape) for km in km_list: km_c = center(km) #km_c = normalize_km(km_c) s = f_dot(km_c, ky_c) / f_norm(km) / ky_n w.append(s) train_km = train_km + s * km_c train_km = normalize_km(train_km) w = numpy.array(w) #numpy.savetxt('kernel_weights_%s.txt' % c_type, numpy.array(w)) #numpy.savetxt(out_f, train_km) elif c_type == 'ALIGNF': ky = numpy.dot(labels, labels.T) ky_c = center(ky) #ky_c = normalize_km(ky_c) a = [] kmc_list = [] for km in km_list: km_c = center(km) #km_c = normalize_km(km_c) a.append(f_dot(km_c, ky_c)) kmc_list.append(km_c) a = numpy.array(a, dtype='d') n_km = len(kmc_list) M = numpy.zeros((n_km, n_km), dtype='d') for i in range(n_km): for j in range(i, n_km): M[i, j] = f_dot(kmc_list[i], kmc_list[j]) M[j, i] = M[i, j] try: from cvxopt import matrix from cvxopt import solvers except ImportError: raise ImportError("Module <cvxopt> needed for ALIGNF.") # define variable for cvxopt.qp P = matrix(2 * M) q = matrix(-2 * a) G = matrix(numpy.diag([-1.0] * n_km)) h = matrix(numpy.zeros(n_km, dtype='d')) sol = solvers.qp(P, q, G, h) w = sol['x'] w = w / numpy.linalg.norm(w) # compute train_km with best w train_km = numpy.zeros(km_list[0].shape) for i in range(n_km): train_km = train_km + w[i] * kmc_list[i] train_km = normalize_km(train_km) #numpy.savetxt('kernel_weights_%s.txt' % c_type, numpy.array(w)) #numpy.savetxt(out_f, train_km) else: raise Exception("MKL only supports UNIMKL, ALIGN, ALIGNF") return train_km, w
def CFS_arm(self, x_ref, cq=[1, 10, 0], cs=[0, 0, 20], minimal_dis=0, ts=1, maxIter=6, stop_eps=1e-1): # define the robot here robot = RobotProperty() self.robot = RobotProperty() # without obstacle, then collision free n_ob = self.spec['n_ob'] obs_traj = self.obs_traj if n_ob == 0 or len( obs_traj) == 0: # no future obstacle information is provided print(f"{len(obs_traj)} length obstacle, direct exit!!!!") return x_ref # has obstacle, the normal CFS procedure x_rs = np.array(x_ref) # planning parameters h = x_rs.shape[0] dimension = x_rs.shape[1] # # flatten the trajectory to one dimension # flatten to one dimension for applying qp, in the form of x0,y0,x1,y1,... x_rs = np.reshape(x_rs, (x_rs.size, 1)) x_origin = x_rs # objective terms # identity Q1 = np.identity(h * dimension) # distance metric qd = np.array([[1, 0, 0, 0, 0, 0, 0], [0, 0.01, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 4, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 0, 4]]) for i in range(h): Q1[i * dimension:(i + 1) * dimension, i * dimension:(i + 1) * dimension] = qd * 0.1 if i == h - 1: Q1[i * dimension:(i + 1) * dimension, i * dimension:(i + 1) * dimension] = qd S1 = Q1 # velocity term Vdiff = np.identity(h * dimension) - np.diag( np.ones((1, (h - 1) * dimension))[0], dimension) # Q2 = np.matmul(Vdiff.transpose(),Vdiff) Q2 = np.matmul(Vdiff[0:(h - 1) * dimension, :].transpose(), Vdiff[0:(h - 1) * dimension, :]) # Acceleration term Adiff = Vdiff - np.diag( np.ones((1, (h - 1) * dimension))[0], dimension) + np.diag( np.ones((1, (h - 2) * dimension))[0], dimension * 2) Q3 = np.matmul(Adiff[0:(h - 2) * dimension, :].transpose(), Adiff[0:(h - 2) * dimension, :]) # Vdiff = eye(nstep*dim)-diag(ones(1,(nstep-1)*dim),dim); # objective Q = Q1 * cq[0] + Q2 * cq[1] + Q3 * cq[2] S = S1 * cs[0] + Q2 * cs[1] + Q3 * cs[2] # quadratic term H = Q + S # linear term f = -1 * np.dot(Q, x_origin) b = np.ones((h * n_ob, 1)) * (-minimal_dis) H = matrix(H, (len(H), len(H[0])), 'd') f = matrix(f, (len(f), 1), 'd') # b = matrix(b,(len(b),1),'d') # reference trajctory cost J0 = np.dot(np.transpose(x_rs - x_origin), np.dot( Q, (x_rs - x_origin))) + np.dot(np.transpose(x_rs), np.dot(S, x_rs)) J = float('inf') dlt = float('inf') cnt = 0 # equality constraints # start pos and end pos remain unchanged Aeq = np.zeros((dimension * 2, len(x_rs))) for i in range(dimension): Aeq[i, i] = 1 Aeq[dimension * 2 - i - 1, len(x_rs) - i - 1] = 1 beq = np.zeros((dimension * 2, 1)) beq[0:dimension, 0] = x_rs[0:dimension, 0] beq[dimension:dimension * 2, 0] = x_rs[dimension * (h - 1):dimension * h, 0] # transform to convex optimization matrix Aeq = matrix(Aeq, (len(Aeq), len(Aeq[0])), 'd') beq = matrix(beq, (len(beq), 1), 'd') # main CFS loop while dlt > stop_eps: cnt += 1 Lstack, Sstack = [], [] # inequality constraints # l * x <= s Constraint = np.zeros((h * n_ob, len(x_rs))) for i in range(h): # get reference pos at time step i if i < h - 1 and i > 0: x_r = x_rs[i * dimension:(i + 1) * dimension] # get inequality value (distance) # get obstacle at this time step obs = obs_traj[i] dist = self.ineq_arm(x_r, robot.DH, robot.base, obs, robot.cap) # get gradient ref_grad = jac_num_arm(self.ineq_arm, x_r, robot.DH, robot.base, obs, robot.cap) # compute s = dist - robot.margin - np.dot(ref_grad, x_r) l = -1 * ref_grad if i == h - 1 or i == 0: # don't need inequality constraints for lst dimension s = np.zeros((1, 1)) l = np.zeros((1, self.dim)) # update Sstack = vstack_wrapper(Sstack, s) l_tmp = np.zeros((1, len(x_rs))) l_tmp[:, i * dimension:(i + 1) * dimension] = l Lstack = vstack_wrapper(Lstack, l_tmp) # add joint limit # l_lim_u = np.identity(h*dimension) # l_lim_l = -1*np.identity(h*dimension) # Lstack = vstack_wrapper(Lstack, l_lim_u) # Lstack = vstack_wrapper(Lstack, l_lim_l) # Sstack = vstack_wrapper(Sstack, 2*pi*np.ones((h*dimension,1))) # # Sstack = vstack_wrapper(Sstack, -pi*np.ones((60,1))) # Sstack = vstack_wrapper(Sstack, np.zeros((h*dimension,1))) Lstack = matrix(Lstack, (len(Lstack), len(Lstack[0])), 'd') Sstack = matrix(Sstack, (len(Sstack), 1), 'd') # QP solver sol = solvers.qp(H, f, Lstack, Sstack, Aeq, beq) x_ts = sol['x'] x_ts = np.reshape(x_ts, (len(x_rs), 1)) J = np.dot(np.transpose(x_ts - x_origin), np.dot(Q, (x_ts - x_origin))) + np.dot( np.transpose(x_ts), np.dot(S, x_ts)) dlt = min(abs(J - J0), np.linalg.norm(x_ts - x_rs)) J0 = J x_rs = x_ts if cnt >= maxIter: break # return the reference trajectory x_rs = x_rs[:h * dimension] return x_rs.reshape(h, dimension)
##-*- coding: utf-8 -*- # conda install cvxopt from cvxopt import matrix, solvers import numpy as np Q = matrix([[4.0, 1.0], [1.0, 2.0]]) p = matrix([1.0, 1.0], (2, 1)) G = matrix([[-1.0, 0.0], [0.0, -1.0]]) h = matrix([0.0, 0.0]) A = matrix([1.0, 1.0], (1, 2)) #1行2列 b = matrix(1.0) sol = solvers.qp(Q, p, G, h, A, b) print(sol['x']) print(sol['primal objective'])
b[i + pos.shape[0] + neg.shape[0]] = C Aeq = np.zeros((1, pos.shape[0] + neg.shape[0])) Beq = np.zeros((1, 1)) for i in range(pos.shape[0] + neg.shape[0]): Aeq[0, i] = Y[i] f = matrix(f) H = matrix(H) A = matrix(A) b = matrix(b) Aeq = matrix(Aeq) Beq = matrix(Beq) sol = solvers.qp(H, f, A, b, Aeq, Beq) alpha = sol['x'] w = np.zeros(pos.shape[1] + 1) for j in range(pos.shape[1]): for i in range(pos.shape[0] + neg.shape[0]): w[j] += alpha[i] * Y[i] * X[i, j] wx = np.asarray(np.dot(w[0:pos.shape[1]], X.T)) bs = Y - wx w[2] = bs[1] plt.figure(1, figsize=(7, 7)) plt.plot(X[0:pos.shape[0], 0], X[0:pos.shape[0], 1], 'b+', label='positive') plt.plot(X[pos.shape[0]:pos.shape[0] + neg.shape[0], 0], X[pos.shape[0]:pos.shape[0] + neg.shape[0], 1],
def quadratic_solver(P, q, G, h, A, b): sol = solvers.qp(P, q, G, h, A, b) alphas = np.array(sol['x']) return alphas
def fit(self, X, y): self.X_train_ = X self.y_train_ = y self.n_samples_ = y.shape[0] # n_samples self.classes_, self.repartition_ = np.unique(y, return_counts=True) self.classes_ = self.classes_.astype(int) self.n_classes_ = self.classes_.shape[0] self.repartition_ = self.repartition_ / self.n_samples_ self.K_ = self.fit_kernel() self.K_test_ = None if self.mode == 'OVA': for class_ in self.classes_: y_copy = y.copy() y_copy[y_copy != class_] = -1 y_copy[y_copy == class_] = 1 self.fit_dual(y_copy) # Solving the QP if self.intercept is True: sol = solvers.qp(matrix(self.K_ + self.Q_, tc='d'), matrix(self.p_, tc='d'), matrix(self.G_, tc='d'), matrix(self.h_, tc='d'), matrix(self.A_, tc='d'), matrix(self.b_, tc='d')) # Saving the solution else: sol = solvers.qp(matrix(self.K_ + self.Q_, tc='d'), matrix(self.p_, tc='d'), matrix(self.G_), matrix(self.h_, tc='d')) # Saving the solution self.alphas_.append(np.array(sol['x']).reshape(-1, )) elif self.mode == 'OVO': masks_samples = [ ] # empty boolean list, len=45, True if the sample is in the current OVO comparison y_copies = [] # classes of OVO configuration are all stored for class_plus in range(self.n_classes_): for class_minus in range(class_plus + 1, 10): y_copy = y.copy() # Mask select the samples which match the classes considered in the current OVO comparison mask = (y_copy == class_plus) | (y_copy == class_minus) masks_samples.append(mask) # saving the mask y_copy = y_copy[mask] y_copy[y_copy == class_minus] = -1 y_copy[y_copy == class_plus] = 1 y_copies.append(y_copy) # Updating the size of the subproblem for the dual computation # Important otherwise it would be built for n_samples = all the sample self.n_samples_ = y_copy.shape[0] # This_K is the submatrix of K for the current OVO problem this_K = self.K_[mask, :] this_K = this_K[:, mask] self.fit_dual(y_copy) # Solvign the QP if self.intercept is True: sol = solvers.qp(matrix(this_K + self.Q_, tc='d'), matrix(self.p_, tc='d'), matrix(self.G_), matrix(self.h_), matrix(self.A_, tc='d'), matrix(self.b_, tc='d')) else: sol = solvers.qp(matrix(this_K + self.Q_, tc='d'), matrix(self.p_, tc='d'), matrix(self.G_), matrix(self.h_)) # Saving the solution in list attibute alphas_ self.alphas_.append(np.array(sol['x']).reshape(-1, )) # Don't forget to update the n_samples attribute again self.n_samples_ = y.shape[0] self.y_copies_ = y_copies # Saving the masks as attributes self.masks_samples_ = np.array(masks_samples) else: raise Exception('mode should be OVA or OVO') return self
from cvxopt import solvers # Define QP parameters (directly) # min 0.5*x'*P*x + q'*x # x # subject to Gx <= h # Ax = b # # https://courses.csail.mit.edu/6.867/wiki/images/a/a7/Qp-cvxopt.pdf P = matrix([[1.0,0.0],[0.0,0.0]]) q = matrix([3.0,4.0]) G = matrix([[-1.0,0.0,-1.0,2.0,3.0],[0.0,-1.0,-3.0,5.0,4.0]]) h = matrix([0.0,0.0,-15.0,100.0,80.0]) # Define QP parameters (with NumPy) P = matrix(numpy.diag([1,0]), tc='d') q = matrix(numpy.array([3,4]), tc='d') G = matrix(numpy.array([[-1,0],[0,-1],[-1,-3],[2,5],[3,4]]), tc='d') h = matrix(numpy.array([0,0,-15,100,80]), tc='d') # Construct the QP, invoke solver sol = solvers.qp(P,q,G,h) # Extract optimal value and solution sol['x'] # [7.13e-07, 5.00e+00] sol['primal objective'] # 20.0000061731 print(sol['x']) print(sol['primal objective'])
def cvxopt_foopsi(fluor, b, c1, g, sn, p, bas_nonneg, verbosity): """Solve the deconvolution problem using cvxopt and picos packages """ try: from cvxopt import matrix, spmatrix, spdiag, solvers import picos except ImportError: raise ImportError( 'Constrained Foopsi requires cvxopt and picos packages.') T = len(fluor) # construct deconvolution matrix (sp = G*c) G = spmatrix(1., list(range(T)), list(range(T)), (T, T)) for i in range(p): G = G + spmatrix(-g[i], np.arange(i + 1, T), np.arange(T - i - 1), (T, T)) gr = np.roots(np.concatenate([np.array([1]), -g.flatten()])) gd_vec = np.max(gr)**np.arange(T) # decay vector for initial fluorescence gen_vec = G * matrix(np.ones(fluor.size)) # Initialize variables in our problem prob = picos.Problem() # Define variables calcium_fit = prob.add_variable('calcium_fit', fluor.size) cnt = 0 if b is None: flag_b = True cnt += 1 b = prob.add_variable('b', 1) if bas_nonneg: b_lb = 0 else: b_lb = np.min(fluor) prob.add_constraint(b >= b_lb) else: flag_b = False if c1 is None: flag_c1 = True cnt += 1 c1 = prob.add_variable('c1', 1) prob.add_constraint(c1 >= 0) else: flag_c1 = False # Add constraints prob.add_constraint(G * calcium_fit >= 0) res = abs(matrix(fluor.astype(float)) - calcium_fit - b * matrix(np.ones(fluor.size)) - matrix(gd_vec) * c1) prob.add_constraint(res < sn * np.sqrt(fluor.size)) prob.set_objective('min', calcium_fit.T * gen_vec) # solve problem try: prob.solve(solver='mosek', verbose=verbosity) except ImportError: warn('MOSEK is not installed. Spike inference may be VERY slow!') prob.solver_selection() prob.solve(verbose=verbosity) # if problem in infeasible due to low noise value then project onto the # cone of linear constraints with cvxopt if prob.status == 'prim_infeas_cer' or prob.status == 'dual_infeas_cer' or prob.status == 'primal infeasible': warn('Original problem infeasible. Adjusting noise level and re-solving') # setup quadratic problem with cvxopt solvers.options['show_progress'] = verbosity ind_rows = list(range(T)) ind_cols = list(range(T)) vals = np.ones(T) if flag_b: ind_rows = ind_rows + list(range(T)) ind_cols = ind_cols + [T] * T vals = np.concatenate((vals, np.ones(T))) if flag_c1: ind_rows = ind_rows + list(range(T)) ind_cols = ind_cols + [T + cnt - 1] * T vals = np.concatenate((vals, np.squeeze(gd_vec))) P = spmatrix(vals, ind_rows, ind_cols, (T, T + cnt)) H = P.T * P Py = P.T * matrix(fluor.astype(float)) sol = solvers.qp( H, -Py, spdiag([-G, -spmatrix(1., list(range(cnt)), list(range(cnt)))]), matrix(0., (T + cnt, 1))) xx = sol['x'] c = np.array(xx[:T]) sp = np.array(G * matrix(c)) c = np.squeeze(c) if flag_b: b = np.array(xx[T + 1]) + b_lb if flag_c1: c1 = np.array(xx[-1]) sn = old_div(np.linalg.norm(fluor - c - c1 * gd_vec - b), np.sqrt(T)) else: # readout picos solution c = np.squeeze(calcium_fit.value) sp = np.squeeze(np.asarray(G * calcium_fit.value)) if flag_b: b = np.squeeze(b.value) if flag_c1: c1 = np.squeeze(c1.value) return c, b, c1, g, sn, sp
objW = np.identity(4) H, f = getQuadProgCostFunction(objJ, objC, objW) Aeq = dynJ beq = dynC Ain = np.vstack((copJ, -copJ)) bin = np.ones((10, 1)) * dF P = matrix(H, tc='d') q = matrix(f, tc='d') G = matrix(Ain, tc='d') h = matrix(bin, tc='d') A = matrix(Aeq, tc='d') b = matrix(beq, tc='d') soln = solvers.qp(P, q, G, h, A, b) optX = soln['x'] coeffX = optX[0:4] coeffV = optX[4:8] xInitial = coeffX[0] xFinal = coeffX[0] + coeffX[1] * T1 + coeffX[2] * T2 + coeffX[3] * T3 xdInitial = coeffX[1] xdFinal = coeffX[1] + 2 * coeffX[2] * T1 + 3 * coeffX[3] * T2 print(xInitial) print(xFinal) print(xdInitial) print(xdFinal)
A[6][1, :] = (C[1] - C[6]).T b[6][1] = 0.5 * A[6][1, :] * (C[1] + C[6]) A[6][2, :] = (C[5] - C[6]).T b[6][2] = 0.5 * A[6][2, :] * (C[5] + C[6]) # For regions k=1, ..., 6, let pk be the optimal value of # # minimize x'*x # subject to A*x <= b. # # The Chernoff upper bound is 1.0 - sum exp( - pk / (2 sigma^2)). P = matrix([1.0, 0.0, 0.0, 1.0], (2, 2)) q = matrix(0.0, (2, 1)) optvals = matrix( [blas.nrm2(solvers.qp(P, q, A[k], b[k])['x'])**2 for k in range(1, 7)]) nopts = 200 sigmas = 0.2 + (0.5 - 0.2) / nopts * matrix(list(range(nopts)), tc='d') bnds = [1.0 - sum(exp(-optvals / (2 * sigma**2))) for sigma in sigmas] try: import pylab except ImportError: pass else: pylab.figure(facecolor='w') pylab.plot(sigmas, bnds, '-') pylab.axis([0.2, 0.5, 0.9, 1.0]) pylab.title('Chernoff lower bound (fig. 7.8)') pylab.xlabel('sigma') pylab.ylabel('Probability of correct detection')
from cvxopt import matrix, solvers # build K V = np.concatenate((X0.T, -X1.T), axis=1) K = matrix(V.T.dot(V)) p = matrix(-np.ones((2 * N, 1))) # build A, b, G, h G = matrix(np.vstack((-np.eye(2 * N), np.eye(2 * N)))) h = np.vstack((np.zeros((2 * N, 1)), C * np.ones((2 * N, 1)))) h = matrix(np.vstack((np.zeros((2 * N, 1)), C * np.ones((2 * N, 1))))) A = matrix(y.reshape((-1, 2 * N))) b = matrix(np.zeros((1, 1))) solvers.options['show_progress'] = False sol = solvers.qp(K, p, G, h, A, b) l = np.array(sol['x']) print('lambda = \n', l.T) S = np.where(l > 1e-5)[0] S2 = np.where(l < .99 * C)[0] M = [val for val in S if val in S2] # intersection of two lists XT = X.T # we need each col is one data point in this alg VS = V[:, S] # XS = XT[:, S] # yS = y[ S] lS = l[S] # lM = l[M]