def to_cvx(self, f, scale=1.0): yi = f(self.x[0]) yj = f(self.x[1]) yk = f(self.x[2]) diff1 = yk - yj + eps #diff2 = yk - yi + eps diff2 = yk + yj - 2 * yi exp1 = 1 - cvx.exp(-scale * diff1) exp2 = 1 - cvx.exp(-scale * diff2) val = cvx.min_elemwise(exp1, exp2) log_val = -cvx.log(val) return log_val, [diff1 > eps, diff2 > eps]
def _res_update(self, f, pi, j, rho): K = f.size beta = self.p[j] / rho u0 = f + pi / rho u = np.sort(u0)[::-1] cumsum = 0 for k in range(K - 1): cumsum += u[k] t = (cumsum - beta) / (k + 1) if u[k + 1] <= t: break f = cvx.min_elemwise(u0, t).value return np.array(f)
def generate_cvx(cls, constraints, f, transform=None, scale=1.0): x, x_low, x_high = ConvexNeighborConstraint.generate_neighbors_for_scipy_optimize( constraints, transform) yi = f(x) yj = f(x_low) yk = f(x_high) diff1 = yk - yj #diff2 = yk - yi diff2 = yk + yj - 2 * yi exp1 = 1 - cvx.exp(-scale * diff1) + eps exp2 = 1 - cvx.exp(-scale * diff2) + eps val = cvx.min_elemwise(exp1, exp2) log_val = -cvx.log(val) sum_val = cvx.sum_entries(log_val) return sum_val, [diff1 > eps, diff2 > eps]
def objective(t, y, u): R = co.matrix(diag(reference(t))) return cp.norm(R * cp.min_elemwise(y-Tobj, 0)) \ + cp.norm(u, 1) * args.cost
def get_constraint_list_cov(x_train, y_train, x_control_train, sensitive_attrs_to_cov_thresh, cons_type, w): constraints = [] for attr in sensitive_attrs_to_cov_thresh.keys(): attr_arr = x_control_train[attr] print(attr_arr) attr_arr_transformed, index_dict = get_one_hot_encoding( attr_arr.astype(int)) if index_dict is None: # binary attribute, in this case, the attr_arr_transformed is the same as the attr_arr s_val_to_total = { ct: {} for ct in [0, 1, 2] } # constrain type -> sens_attr_val -> total number s_val_to_avg = {ct: {} for ct in [0, 1, 2]} cons_sum_dict = { ct: {} for ct in [0, 1, 2] } # sum of entities (females and males) in constraints are stored here for v in set(attr_arr): s_val_to_total[0][v] = np.sum(x_control_train[attr] == v) s_val_to_total[1][v] = np.sum( np.logical_and(x_control_train[attr] == v, y_train == -1) ) # FPR constraint so we only consider the ground truth negative dataset for computing the covariance s_val_to_total[2][v] = np.sum( np.logical_and(x_control_train[attr] == v, y_train == +1)) for ct in [0, 1, 2]: s_val_to_avg[ct][0] = s_val_to_total[ct][1] / float( s_val_to_total[ct][0] + s_val_to_total[ct][1] ) # N1/N in our formulation, differs from one constraint type to another s_val_to_avg[ct][1] = 1.0 - s_val_to_avg[ct][0] # N0/N for v in set(attr_arr): idx = x_control_train[attr] == v ################################################################# # #DCCP constraints dist_bound_prod = cvxpy.mul_elemwise( y_train[idx], x_train[idx] * w) # y.f(x) cons_sum_dict[0][v] = cvxpy.sum_entries( cvxpy.min_elemwise(0, dist_bound_prod)) * ( s_val_to_avg[0][v] / len(x_train) ) # avg misclassification distance from boundary cons_sum_dict[1][v] = cvxpy.sum_entries( cvxpy.min_elemwise( 0, cvxpy.mul_elemwise( (1 - y_train[idx]) / 2.0, dist_bound_prod))) * ( s_val_to_avg[1][v] / sum(y_train == -1)) cons_sum_dict[2][v] = cvxpy.sum_entries( cvxpy.min_elemwise( 0, cvxpy.mul_elemwise( (1 + y_train[idx]) / 2.0, dist_bound_prod))) * ( s_val_to_avg[2][v] / sum(y_train == +1)) ################################################################# if cons_type == 4: cts = [1, 2] elif cons_type in [0, 1, 2]: cts = [cons_type] else: raise Exception("Invalid constraint type") ################################################################# # DCCP constraints for ct in cts: thresh = abs(sensitive_attrs_to_cov_thresh[attr][ct][1] - sensitive_attrs_to_cov_thresh[attr][ct][0]) constraints.append( cons_sum_dict[ct][1] <= cons_sum_dict[ct][0] + thresh) constraints.append( cons_sum_dict[ct][1] >= cons_sum_dict[ct][0] - thresh) ################################################################# else: # otherwise, its a categorical attribute, so we need to set the cov thresh for each value separately # need to fill up this part raise Exception( "Fill the constraint code for categorical sensitive features... Exiting..." ) sys.exit(1) return constraints
def cvx_util(self, r): return cvx.min_elemwise(r, self.β * r)
def cvx_util(self,r): return cvx.min_elemwise(r, self.beta*r, self.beta*self.x0)