def intersection(*cons): if not all(isinstance(c, constraint) for c in cons): t = type([c for c in cons if not isinstance(c, constraint)][0]) raise TypeError("Not a constraint : "+ repr(t)) cons = [c for c in cons if not isinstance(c, noConstraint)] if all(isinstance(c, quadratic.quad_constraints) for c in cons): q = np.vstack([c.quad_part for c in cons]) l = np.vstack([c.lin_part for c in cons]) o = np.hstack([c.offset for c in cons]) intersection = quadratic.quad_constraints(q, l, o) return intersection intersection = cons_op() intersection._cons_list = list(cons) intersection._op = 'I' return intersection
def intersection(*cons): if not all(isinstance(c, constraint) for c in cons): t = type([c for c in cons if not isinstance(c, constraint)][0]) raise TypeError("Not a constraint : " + repr(t)) cons = [c for c in cons if not isinstance(c, noConstraint)] if all(isinstance(c, quadratic.quad_constraints) for c in cons): q = np.vstack([c.quad_part for c in cons]) l = np.vstack([c.lin_part for c in cons]) o = np.hstack([c.offset for c in cons]) intersection = quadratic.quad_constraints(q, l, o) return intersection intersection = cons_op() intersection._cons_list = list(cons) intersection._op = 'I' return intersection
def distr_norm(self, X_s, y, sigma = 1.): """ Return the value of the norm of X_s.T*y and an instance of truncated : the distribution of X_s.T*y This is implementing the forward stepwise paper in the general case Parameters ---------- X_s : np.float(p, k): X_s is a full ranked matrix y : np.float(p): y is the data, and satisfies the constraints sigma: float sigma is the variance of the normal distribution under wich y is chosen Returns ------- distr : truncated_chi distr is an object used to study the distribution of np.linalg.norm(np.dot(X_s.T, y)), when y is a gaussian vector, chosen under the constraints and on the slice given by nu """ p, _ = y.shape # P_s = projection(X_s) k = min(X_s.shape) z = np.dot(X_s.T, y) z_norm = np.linalg.norm(z) eta = z / z_norm nu = np.dot(np.linalg.pinv(X_s).T, eta) # print "nu : ", nu # Computation of the intervals q = np.zeros((1, p, p)) lin = (-nu).reshape((1, p)) off = np.array([float( \ - np.dot(nu.T, y) \ + np.linalg.norm(nu)**2 * z_norm) \ ]) cons_eta = quadratic.quad_constraints(q, lin, off) cons_inter = cons_op.intersection(self, cons_eta) I = cons_inter.bounds(nu, y) I = I + float(z_norm) # Computation of theta Sig_s = np.dot(X_s.T, X_s) Sig_s_inv = np.linalg.inv(Sig_s) theta_s = float(sigma / np.sqrt(np.dot(eta.T, np.dot(Sig_s_inv, eta)))) distr = truncated_chi(I._U, k, theta_s) return distr
def distr_norm(self, X_s, y, sigma=1.): """ Return the value of the norm of X_s.T*y and an instance of truncated : the distribution of X_s.T*y This is implementing the forward stepwise paper in the general case Parameters ---------- X_s : np.float(p, k): X_s is a full ranked matrix y : np.float(p): y is the data, and satisfies the constraints sigma: float sigma is the variance of the normal distribution under wich y is chosen Returns ------- distr : truncated_chi distr is an object used to study the distribution of np.linalg.norm(np.dot(X_s.T, y)), when y is a gaussian vector, chosen under the constraints and on the slice given by nu """ p, _ = y.shape # P_s = projection(X_s) k = min(X_s.shape) z = np.dot(X_s.T, y) z_norm = np.linalg.norm(z) eta = z / z_norm nu = np.dot(np.linalg.pinv(X_s).T, eta) # print "nu : ", nu # Computation of the intervals q = np.zeros((1, p, p)) lin = (-nu).reshape((1, p)) off = np.array([float( \ - np.dot(nu.T, y) \ + np.linalg.norm(nu)**2 * z_norm) \ ]) cons_eta = quadratic.quad_constraints(q, lin, off) cons_inter = cons_op.intersection(self, cons_eta) I = cons_inter.bounds(nu, y) I = I + float(z_norm) # Computation of theta Sig_s = np.dot(X_s.T, X_s) Sig_s_inv = np.linalg.inv(Sig_s) theta_s = float(sigma / np.sqrt(np.dot(eta.T, np.dot(Sig_s_inv, eta)))) distr = truncated_chi(I._U, k, theta_s) return distr