int CFeasibilityMap::s_to_tau_fn( ColumnVector &s_i ){
  int tau_i=0; 
  for (int i_var = 0; i_var<s_i.nrows(); i_var++){
    if (s_i(i_var+1)==1) tau_i += 1 << i_var;
	}
  return tau_i;
}
Example #2
0
// Multiply X by n-1 x n matrix to give n-1 contrasts
// Return a ColumnVector
ReturnMatrix Helmert(const ColumnVector& X, bool full)
{
   REPORT
   Tracer et("Helmert * CV");
   int n = X.nrows();
   if (n == 0) Throw(ProgramException("X Vector of length 0", X));
   Real sum = 0.0; ColumnVector Y;
   if (full) Y.resize(n); else Y.resize(n-1);
   for (int i = 1; i < n; ++i)
      { sum += X(i); Y(i) = (i * X(i+1) - sum) / sqrt((Real)i * (i+1)); }
   if (full) { sum += X(n); Y(n) = sum / sqrt((Real)n); }
   Y.release(); return Y.for_return();
} 
Example #3
0
// same as above but want only j-th element
Real Helmert_transpose(const ColumnVector& Y, int j, bool full)
{
   REPORT
   Tracer et("Helmert_transpose:single element ");
   int n = Y.nrows(); Real sum;
   if (full) sum = Y(n) / sqrt((Real)n); else { sum = 0.0; ++n; }
   if (j > n || j <= 0) Throw(IndexException(j, Y));
   for (int i = n-1; i > 0; --i)
   {
      Real Yi = Y(i) / sqrt((Real)i * (i+1));
      if (i+1 == j) return i * Yi + sum;
      sum -= Yi;
   }
   return sum;
}
Example #4
0
ReturnMatrix Helmert_transpose(const ColumnVector& Y, bool full)
{
   REPORT
   Tracer et("Helmert_transpose * CV ");
   int n = Y.nrows(); Real sum;
   if (full) sum = Y(n) / sqrt((Real)n); else { sum = 0.0; ++n; }
   ColumnVector X(n);
   for (int i = n-1; i > 0; --i)
   {
      Real Yi = Y(i) / sqrt((Real)i * (i+1));
      X(i+1) = i * Yi + sum; sum -= Yi;
   }
   X(1) = sum;
   X.release(); return X.for_return();
}
int CFeasibilityMap::test_feasible_test_fn(CData &Data, ColumnVector &x_tilde_i, ColumnVector &s_i){
  int is_feasible = 0 ;
  
  int n_var = x_tilde_i.nrows() ; 
  int n_EditVec = Data.EditVec.nrows() ;
  int sum_s_1 = s_i.sum() ; int sum_s_0 = n_var - sum_s_1 ;   
  ColumnVector x_0(sum_s_0); x_0 = 0;
  Matrix A_0(n_EditVec,sum_s_0) ; A_0 = 0;
	Matrix A_1(n_EditVec,sum_s_1) ; A_1 = 0;
	
  int count0 = 0;
  int count1 = 0;
	for (int j_var=1; j_var<=n_var; j_var++){
		if ((int)s_i(j_var)==0){
			count0++; 
			x_0(count0) = x_tilde_i(j_var) ; 
			A_0.column(count0) = Data.EditMat.column(j_var) ; 
		} else {
      count1++;
			A_1.column(count1) = Data.EditMat.column(j_var) ; 
		}
	} 
  
	ColumnVector which_rows(n_EditVec); which_rows = 0 ; 
	for (int i_row=1; i_row<=n_EditVec; i_row++){
		int n_zero = 0 ; 
		for (int j=1; j<=sum_s_1; j++){
			if (A_1(i_row,j)==0) n_zero++; 
		}
		if (n_zero<sum_s_1) which_rows(i_row) = 1 ; 
	}

	Matrix A_1_red(which_rows.sum(),sum_s_1); 
	ColumnVector b_1_red(which_rows.sum()); 
	Matrix A_0_red(which_rows.sum(),sum_s_0); 
	for (int i_row=1, count_row = 0; i_row<=n_EditVec; i_row++){
		if (which_rows(i_row)==1){
			count_row++; 
			A_1_red.row(count_row) = A_1.row(i_row) ;  	// Constraints
			b_1_red.row(count_row) = Data.EditVec.row(i_row) ; 
			A_0_red.row(count_row) = A_0.row(i_row) ; 
		} 
	}
  ColumnVector EditVec_shr = b_1_red - A_0_red * x_0 ;
  is_feasible = SolveLP(A_1_red, EditVec_shr);
	return is_feasible?1:0; 
}
int CFeasibilityMap::feasible_test_fn(CData &Data, ColumnVector &x_tilde_i, 
    ColumnVector &s_i, int i_original, bool initD_S, float epsilon, ColumnVector &x){
  int is_feasible = 0 ;
  if (initD_S) {
    if (!Data.PassStep0_for_init(x_tilde_i,s_i,epsilon)) { return is_feasible;}
  } else {
    // Step 0 - check balance edits hold, but only one s_ij has the value 1. 
    if (!Data.PassStep0(s_i,i_original)) { return is_feasible;}
  }
  
  // Step 1
  int n_var = x_tilde_i.nrows() ; 
  int n_EditVec = Data.EditVec.nrows() ;
  int sum_s_1 = s_i.sum() ; int sum_s_0 = n_var - sum_s_1 ;   
	ColumnVector x_0(sum_s_0); x_0 = 0;
  Matrix A_0(n_EditVec,sum_s_0) ; A_0 = 0;
	Matrix A_1(n_EditVec,sum_s_1) ; A_1 = 0;
	
  int count0 = 0;
  int count1 = 0;
	for (int j_var=1; j_var<=n_var; j_var++){
		if ((int)s_i(j_var)==0){
			count0++; 
			x_0(count0) = x_tilde_i(j_var) ; 
			A_0.column(count0) = Data.EditMat.column(j_var) ; 
		} else {
      count1++;
			A_1.column(count1) = Data.EditMat.column(j_var) ; 
		}
	} 
 
  Data.Debug = Debug;
  if (!Data.PassStep1(s_i, A_0, x_0)) { return is_feasible;}
	
  // Step 2: lpSolve to check whether s_i has a feasible solution of x_i
	ColumnVector which_rows(n_EditVec); which_rows = 0 ; 
	for (int i_row=1; i_row<=n_EditVec; i_row++){
		int n_zero = 0 ; 
		for (int j=1; j<=sum_s_1; j++){
			if (A_1(i_row,j)==0) n_zero++; 
		}
		if (n_zero<sum_s_1) which_rows(i_row) = 1 ; 
	}

	Matrix A_1_red(which_rows.sum(),sum_s_1); 
	ColumnVector b_1_red(which_rows.sum()); 
	Matrix A_0_red(which_rows.sum(),sum_s_0); 
	for (int i_row=1, count_row = 0; i_row<=n_EditVec; i_row++){
		if (which_rows(i_row)==1){
			count_row++; 
			A_1_red.row(count_row) = A_1.row(i_row) ;  	// Constraints
			b_1_red.row(count_row) = Data.EditVec.row(i_row) ; 
			A_0_red.row(count_row) = A_0.row(i_row) ; 
		} 
	}
  ColumnVector EditVec_shr = b_1_red - A_0_red * x_0 ;
  
  if (initD_S) {
    is_feasible = SolveLP(A_1_red, EditVec_shr,x);    
  } else {
    is_feasible = SolveLP(A_1_red, EditVec_shr);
  }
	return is_feasible?1:0; 
}