Beispiel #1
0
/*tumor and normal should have been sorted before call the following function*/
static void random_sample(double *tumor, int n_tumor, double *normal, int n_nml, int n,  int bin_size ,int *bin, int n_bins, int paired, int insert, int sd)
{	int i,k,pos;
	double U, rn_insert, U1;
	//int n_sampled=0, t_sampled=0;
	int N = n_tumor + n_nml, max_pos, min_pos;
	double tmp, ave_dist, offset;

	tmp = (tumor[n_tumor-1]>normal[n_nml-1]? tumor[n_tumor-1]: normal[n_nml-1]);
	max_pos = (int) tmp;
	if(max_pos/bin_size > n_bins-1) {fprintf(stderr,"Error in random_sample: not enough space for binned data\n");exit(1);}
        tmp = (tumor[0]<normal[0]? tumor[0]: normal[0]);
        min_pos = (int) tmp;
	
	ave_dist = ((double) max_pos - min_pos)/((double) n_nml+n_tumor) + 1;  /*plus 1 to make sure ave_dist at least 2*/
	//ave_dist = 100;

	/*fist initialize n_bins*/
	for(i=0;i<n_bins;i++){
		bin[i] = 0;
		}

	/*random sample and binning*/
	if(paired!=1){
		int pos1, pos2;
		for(i=0;i<n;i++){
			//U = N*rand_lp();
			//U1 = 2*rand_lp()-1;
			U = N*drand48();
			U1 = 2*drand48() -1;
			offset = U1*ave_dist;
			if(U > n_tumor){
				k = ((int) U - n_tumor)%n_nml;
				pos = (int) normal[k];
				pos = pos > 0 ? pos : 1;
				pos = pos < max_pos ? pos : max_pos;
				//bin[pos/bin_size] ++;
				//n_sampled++;
				}else{
				k = ((int) U)%n_tumor;
				pos = (int) tumor[k];
                                pos = pos > 0 ? pos : 1;
                                pos = pos < max_pos ? pos : max_pos;
				//bin[pos/bin_size] ++;
				//t_sampled++;
				}
			pos1 = pos; 
			pos = pos + offset;
                        pos = pos>0 ? pos:1;
                        pos = pos < max_pos ? pos : max_pos;
                        //bin[pos/bin_size] ++;
                        pos2 = pos;
			if(drand48()<0.5){ /*add some noise into the simulated data*/
	                        if(drand48()<0.95) bin[pos1/bin_size] ++;
	                        if(drand48()<0.05) bin[pos2/bin_size] ++;						
				}else{
                                if(drand48()<0.05) bin[pos1/bin_size] ++;
                                if(drand48()<0.95) bin[pos2/bin_size] ++;
				}
			}
		}else{
		//int pos1, pos2;
                for(i=0;i<n/2;i++){
                        U = N*rand_lp();
                        if(U > n_tumor){
                                k = ((int) U - n_tumor)%n_nml;
                                pos = (int) normal[k];
                                bin[pos/bin_size] ++;
				//n_sampled++;
                                }else{
                                k = ((int) U)%n_tumor;
                                pos = (int) tumor[k];
                                bin[pos/bin_size] ++;
				//t_sampled++;
                                }
			//pos1 = pos;
			rn_insert = rnorm((double)insert, (double) sd);
			pos = pos+rn_insert;
			pos = pos>0 ? pos:1;
			pos = pos < max_pos ? pos : max_pos;
			bin[pos/bin_size] ++;
			//pos2 = pos;
			//if(rand_lp()<0.5) bin[pos1/bin_size] --;
			//if(rand_lp()<0.5) bin[pos2/bin_size] --;
                        }

		}
	//fprintf(stderr,"sampled %d from normal\n",n_sampled);
	//fprintf(stderr,"sampled %d from tumor\n", t_sampled);
	return;
}
int main()
{  
   int i, j; double s, t;
   printf("Preparing test: 4 variables, 600000 inequalities\n");
   A[0][0] = 1.0; A[0][1] = 2.0; A[0][2] = 1.0; A[0][3] = 0.0; b[0] = 10000.0;
   A[1][0] = 0.0; A[1][1] = 1.0; A[1][2] = 2.0; A[1][3] = 1.0; b[1] = 10000.0;
   A[2][0] = 1.0; A[2][1] = 0.0; A[2][2] = 1.0; A[2][3] = 3.0; b[2] = 10000.0;
   A[3][0] = 4.0; A[3][1] = 0.0; A[3][2] = 1.0; A[3][3] = 1.0; b[3] = 10000.0;
   c[0]=1.0; c[1]=1.0; c[2]=1.0; c[3]=1.0;
   for( i=4; i< 100000; i++ )
   {  A[i][0] = (12123*i)%104729; 
      A[i][1] = (47*i)%104729; 
      A[i][2] = (2011*i)%104729; 
      A[i][3] = (7919*i)%104729;
      b[i] = A[i][0] + 2*A[i][1] + 3*A[i][2] + 4* A[i][3] + 1 + (i%137);
   }
   A[100000][0] = 0.0; A[100000][1] = 6.0; A[100000][2] = 1.0; 
   A[100000][3] = 1.0; b[100000] = 19.0;
   for( i=100001; i< 200000; i++ )
   {  A[i][0] = (2323*i)%101111; 
      A[i][1] = (74*i)%101111; 
      A[i][2] = (2017*i)%101111; 
      A[i][3] = (7915*i)%101111;
      b[i] = A[i][0] + 2*A[i][1] + 3*A[i][2] + 4* A[i][3] + 2 + (i%89);
   }
   A[200000][0] = 5.0; A[200000][1] = 2.0; A[200000][2] = 0.0; 
   A[200000][3] = 1.0; b[200000] = 13.0;
  for( i=200001; i< 300000; i++ )
   {  A[i][0] = (23123*i)%100003; 
      A[i][1] = (47*i)%100003; 
      A[i][2] = (2011*i)%100003; 
      A[i][3] = (7919*i)%100003;
      b[i] = A[i][0] + 2*A[i][1] + 3*A[i][2] + 4* A[i][3] + 2 + (i%57);
   }
   A[300000][0] = 1.0; A[300000][1] = 2.0; A[300000][2] = 1.0; 
   A[300000][3] = 3.0; b[300000] = 20.0;
   A[300001][0] = 1.0; A[300001][1] = 0.0; A[300001][2] = 5.0; 
   A[300001][3] = 4.0; b[300001] = 32.0;
   A[300002][0] = 7.0; A[300002][1] = 1.0; A[300002][2] = 1.0; 
   A[300002][3] = 7.0; b[300002] = 40.0;
   for( i=300003; i< 400000; i++ )
   {  A[i][0] = (13*i)%103087; 
      A[i][1] = (99*i)%103087; 
      A[i][2] = (2012*i)%103087; 
      A[i][3] = (666*i)%103087;
      b[i] = A[i][0] + 2*A[i][1] + 3*A[i][2] + 4* A[i][3] + 1;
   }
   for( i=400000; i< 500000; i++ )
   {  A[i][0] = 1; 
      A[i][1] = (17*i)%999983; 
      A[i][2] = (1967*i)%444443; 
      A[i][3] = 2;
      b[i] = A[i][0] + 2*A[i][1] + 3*A[i][2] + 4* A[i][3] + (1000000.0/(double)i);
   }
   for( i=500000; i< 600000; i++ )
   {  A[i][0] = (3*i)%111121; 
      A[i][1] = (2*i)%999199; 
      A[i][2] = (2*i)%444443; 
      A[i][3] = i;
      b[i] = A[i][0] + 2*A[i][1] + 3*A[i][2] + 4* A[i][3] + 1.3;
   }
   
   printf("Running test: 600000 inequalities, 4 variables\n");
   j = rand_lp(100000, A, b, c, result);
   printf("Test: extremal point (%lf, %lf, %lf, %lf) after %d recomputation steps\n", 
          result[0], result[1], result[2], result[3], j);
   printf("Answer should be (1,2,3,4)\n End Test\n");
return 0;
}