示例#1
0
double CLocalRBFRegression::doRegression(ColumnVector *vector, DataSubset *subset)
{
//	cout << "NNs for Input " << vector->t() << endl;
	
	DataSubset::iterator it = subset->begin();
/*	for (int i = 0; it != subset->end(); it ++, i++)
	{
		ColumnVector dist(*vector);
		dist = dist - *(*input)[*it];
		printf("(%d %f) ", *it, dist.norm_Frobenius());
	}
	printf("\n"); */

	ColumnVector *rbfFactors = getRBFFactors(vector, subset);
	it = subset->begin();
	double value = 0;
	for (int i = 0; it != subset->end(); it ++, i++)
	{
		value += rbfFactors->element(i) * (*output)[*it];
	}
	double sum = rbfFactors->sum();
	if (sum > 0)
	{
		value = value / sum;
	}
	//printf("Value: %f %f ", value ,sum);
	//cout << rbfFactors->t();
	return value;
}
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; 
}
示例#3
0
double CParam::calculate_log_cond_norm(CData &Data, int i_original, ColumnVector &item_by_rnorm, ColumnVector &tilde_y_i, ColumnVector &y_q, bool is_q, LowerTriangularMatrix &LSigma_1_i, ColumnVector &s_q) { // MODIFIED 2015/02/16

  double log_cond_norm;

  if ( item_by_rnorm.sum() >= 1 ) {

    ColumnVector mu_z_i = Mu.column(z_in(i_original));
    ColumnVector s_1_compact = Data.get_compact_vector(item_by_rnorm); 
    ColumnVector Mu_1 = subvector(mu_z_i,s_1_compact);
    Matrix Sigma_1 = Submatrix_elem_2(SIGMA[z_in(i_original)-1],s_1_compact,s_1_compact);

    // ADDED 2015/01/27

    ColumnVector s_q_compact = Data.get_compact_vector(s_q) ; // MODIFIED 2015/02/16 
    ColumnVector VectorOne = s_q_compact ; VectorOne = 1 ; // MODIFIED 2015/02/16
    ColumnVector s_0_compact = VectorOne - s_q_compact ; // MODIFIED 2015/02/16
    int sum_s_0_comp = s_0_compact.sum() ;
    LowerTriangularMatrix LSigma_cond ;
    ColumnVector Mu_cond ;

    if ( sum_s_0_comp>0 ){

      ColumnVector Mu_0 = subvector(mu_z_i,s_0_compact); // (s_1_compact.sum()) vector
      Matrix Sigma_0 = Submatrix_elem_2(SIGMA[z_in(i_original)-1],s_0_compact,s_0_compact);
      Matrix Sigma_10 = Submatrix_elem_2(SIGMA[z_in(i_original)-1],s_1_compact,s_0_compact);
      ColumnVector y_tilde_compact = Data.get_compact_vector(tilde_y_i) ;
      ColumnVector y_tilde_0 = subvector(y_tilde_compact,s_0_compact) ;
      SymmetricMatrix Sigma_0_symm ; Sigma_0_symm << Sigma_0 ;

      LowerTriangularMatrix LSigma_0 = Cholesky(Sigma_0_symm) ;
      Mu_cond = Mu_1 + Sigma_10 * (LSigma_0.i()).t()*LSigma_0.i() * ( y_tilde_0-Mu_0 ) ;
      Matrix Sigma_cond = Sigma_1 - Sigma_10 * (LSigma_0.i()).t()*LSigma_0.i() * Sigma_10.t() ;
      SymmetricMatrix Sigma_cond_symm ; Sigma_cond_symm << Sigma_cond ;
      int sum_s_1_comp = s_1_compact.sum() ;
      DiagonalMatrix D(sum_s_1_comp) ; Matrix V(sum_s_1_comp,sum_s_1_comp) ;
      Jacobi(Sigma_cond_symm,D,V) ;
      int is_zero_exist = 0 ;
      for (int i_var=1; i_var<=sum_s_1_comp; i_var++){
    	if ( D(i_var) < 1e-9 ){
    	  D(i_var) = 1e-9 ;
    	  is_zero_exist = 1 ;
    	}
      } // for (int i_var=1; i_var<=sum_s_1_comp; i_var++)
      if ( is_zero_exist == 1 ){
    	Sigma_cond_symm << V * D * V.t() ;
    	if ( msg_level >= 1 ) {
		  Rprintf( "   Warning: When generating y_j from conditional normal(Mu_-j,Sigma_-j), Sigma_-j is non-positive definite because of computation precision. The eigenvalues D(j,j) smaller than 1e-9 is replaced with 1e-9, and let Sigma_-j = V D V.t().\n");
    	}
      } //
      LSigma_cond = Cholesky(Sigma_cond_symm);
      // y_part = rMVN_fn(Mu_cond,LSigma_cond);
      // log_cond_norm = log_MVN_fn(y_part,Mu_cond,LSigma_cond) ;

    } else {

      Mu_cond = Mu_1 ;
      SymmetricMatrix Sigma_1_symm = Submatrix_elem(SIGMA[z_in(i_original)-1],s_1_compact);
      LSigma_cond = Cholesky(Sigma_1_symm) ;
      // SymmetricMatrix Sigma_1_symm ; Sigma_1_symm << Sigma_1 ;
      // LowerTriangularMatrix LSigma_1 = Cholesky_Sigma_star_symm(Sigma_1_symm);
      // y_part = rMVN_fn(Mu_1,LSigma_1);
      // log_cond_norm = log_MVN_fn(y_part,Mu_1,LSigma_1) ;

    } // if ( sum_s_0_comp>0 ) else ...

    // ADDED 2015/01/26

    LowerTriangularMatrix LSigma_cond_i = LSigma_cond.i() ;
    // LowerTriangularMatrix LSigma_1 = Cholesky(Sigma_1);
    // LSigma_1_i = LSigma_1.i();

    ColumnVector y_part;
    if (is_q) {
      y_part = rMVN_fn(Mu_cond,LSigma_cond);
    } else {
      ColumnVector y_i = (Y_in.row(i_original)).t();
      y_part = subvector(y_i,item_by_rnorm);
    }
    
    log_cond_norm = log_MVN_fn(y_part,Mu_cond,LSigma_cond_i);

    if (is_q) {
      y_q = tilde_y_i;
      for ( int temp_j = 1,temp_count1 = 0; temp_j<=n_var; temp_j++ ){
        if ( item_by_rnorm(temp_j)==1 ){ 
          y_q(temp_j) = y_part(++temp_count1);
	}
      }
    } // if (is_q)

  } else {

    log_cond_norm = 0;
    if (is_q) { y_q = tilde_y_i;}

  } // if ( item_by_rnorm.sum() > = 1 ) else ..

  return log_cond_norm;
}
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; 
}