Пример #1
0
Файл: random.c Проект: 50wu/gpdb
/*
 * dbms_random.normal() RETURN NUMBER;
 *
 *     Returns random numbers in a standard normal distribution
 */
Datum
dbms_random_normal(PG_FUNCTION_ARGS)
{
	float8 result;
	
	/* need random value from (0..1) */
	result = ltqnorm(((double) rand() + 1) / ((double) RAND_MAX + 2));

	PG_RETURN_FLOAT8(result);
}
Пример #2
0
ssize_t MeterRandom::read(std::vector<Reading> &rds, size_t n) {
	if(rds.size() < 1) return -1;

	double step = ltqnorm((float) rand() / RAND_MAX);
	double newval = _last + step;

	/* check boundaries */
	_last += (newval > _max || newval < _min) ? -step : step;

	rds[0].value(_last);
	rds[0].time();
	rds[0].identifier(new NilIdentifier());

	return 1;
}
Пример #3
0
void randomvals_int (t_randomvals *x, t_atom_long value)
{		
	t_rand_gen *gen = &x->gen;
	
	double *means = x->means;
	double *devs = x->devs;
	double *weights = x->weights;
	double *lo_bounds = x->lo_bounds;
	double *hi_bounds = x->hi_bounds;
	
	double randval, mean, dev, lo_bound, hi_bound;
	long i;
	long num_params = x->num_params;
	
	if (value >= 2)
	{
		// Summed windowed gaussians random distribution
		
		// Choose a mean and dev pair based on weighting
		
		randval = rand_double_n(gen, weights[num_params - 1]);
		
		for (i = 0; i < num_params - 1; i++)
			if (randval < weights[i])
				break;
		
		// Generate a windowed gaussian number (between 0 and 1) using a fast implementation
		
		mean = means[i];
		dev = devs[i];
		lo_bound = lo_bounds[i];
		hi_bound = hi_bounds[i];
		
		randval =  ltqnorm(0.5 + 0.5 * rand_double_range(gen, lo_bound, hi_bound)) * dev + mean;
		if (randval > 1.)
			randval = 1.;
		if (randval < 0.)
			randval = 0.;
	}
	else 
	{
		// Generate a flat distribution random number between 0 and 1
		
		randval = rand_double(gen);
	}
	
	outlet_float(x->the_outlet, randval);	
}
Пример #4
0
void randomvals_perform64 (t_randomvals *x, t_object *dsp64, double **ins, long numins, double **outs, long numouts, long vec_size, long flags, void *userparam)
{	
	// Set pointers
	
	double *in = ins[0];
	double *out = outs[0];	
	
	t_rand_gen *gen = &x->gen;
	
	double *means = x->means;
	double *devs = x->devs;
	double *weights = x->weights;
	double *lo_bounds = x->lo_bounds;
	double *hi_bounds = x->hi_bounds;
	
	double randval, mean, dev, lo_bound, hi_bound;
	long test, i;
	long num_params = x->num_params;
	
	while (vec_size--) 
	{
		test = *in++;
		
		if (test >= 1)
		{
			if (test >= 2)
			{
				// Summed windowed gaussians random distribution
				
				// Choose a mean and dev pair based on weighting
				
				randval = rand_double_n(gen, weights[num_params - 1]);
				
				for (i = 0; i < num_params - 1; i++)
					if (randval < weights[i])
						break;
				
				// Generate a windowed gaussian number (between 0 and 1) using a fast implementation
				
				mean = means[i];
				dev = devs[i];
				lo_bound = lo_bounds[i];
				hi_bound = hi_bounds[i];
				
				randval =  ltqnorm(0.5 + 0.5 * rand_double_range(gen, lo_bound, hi_bound)) * dev + mean;
				if (randval > 1.)
					randval = 1.;
				if (randval < 0.)
					randval = 0.;
			}
			else 
			{
				// Generate a flat distribution random number between 0 and 1
				
				randval = rand_double(gen);
			}
		}
		else
		{
			// Output zeros
			
			randval = 0;
		}
		
		*out++ = randval;
	}
}
Пример #5
0
t_int *randomvals_perform (t_int *w)
{	
	// Set pointers
	
	float *in = (float *) w[1];
	float *out = (float *) w[2];
	long vec_size = w[3];
	t_randomvals *x = (t_randomvals *) w[4];
	
	
	t_rand_gen *gen = &x->gen;
	
	double *means = x->means;
	double *devs = x->devs;
	double *weights = x->weights;
	double *lo_bounds = x->lo_bounds;
	double *hi_bounds = x->hi_bounds;
	
	double randval, mean, dev, lo_bound, hi_bound;
	long test, i;
	long num_params = x->num_params;
	
	while (vec_size--) 
	{
		test = *in++;
		
		if (test >= 1)
		{
			if (test >= 2)
			{
				// Summed windowed gaussians random distribution
				
				// Choose a mean and dev pair based on weighting
				
				randval = rand_double_n(gen, weights[num_params - 1]);
				
				for (i = 0; i < num_params - 1; i++)
					if (randval < weights[i])
						break;
				
				// Generate a windowed gaussian number (between 0 and 1) using a fast implementation
				
				mean = means[i];
				dev = devs[i];
				lo_bound = lo_bounds[i];
				hi_bound = hi_bounds[i];
				
				randval =  ltqnorm(0.5 + 0.5 * rand_double_range(gen, lo_bound, hi_bound)) * dev + mean;
				if (randval > 1.)
					randval = 1.;
				if (randval < 0.)
					randval = 0.;
			}
			else 
			{
				// Generate a flat distribution random number between 0 and 1
				
				randval = rand_double(gen);
			}
		}
		else
		{
			// Output zeros
			
			randval = 0;
		}
		
		*out++ = randval;
	}
	
	return w + 5;
}
Пример #6
0
// Inverse of the error function erfc().
nr_double_t fspecial::erfcinv (nr_double_t x) {
  return -ltqnorm (x / 2.0) / M_SQRT2;
}
Пример #7
0
vector<double> Plink::calcMantelHaenszel_2x2xK(Perm & perm, bool original)
{

  // Should we perform BD test (K>1)
  if (nk<2) par::breslowday = false;

  ofstream MHOUT;

  if ( original )
    {
      
      //////////////////////////////////
      // Any individual not assigned to a cluster, making missing
      // phenotype (only need to do this once, for original)
      
      vector<Individual*>::iterator person = sample.begin();
      while ( person != sample.end() )
	{
	  if ( (*person)->sol < 0 ) 
	    (*person)->missing = true;
	  person++;
	}
      
      
      string f = par::output_file_name + ".cmh";
      MHOUT.open(f.c_str(),ios::out);
      
      MHOUT << setw(4) << "CHR" << " "
	    << setw(par::pp_maxsnp) << "SNP" << " "
	    << setw(10) << "BP" << " "
	    << setw(4) << "A1" << " " 
	    << setw(8) << "MAF" << " "
	    << setw(4) << "A2" << " "	
	    << setw(10) << "CHISQ" << " "
	    << setw(10) << "P" << " "
	    << setw(10) << "OR" << " "
	    << setw(10) << "SE" << " "
	    << setw(10) << string("L"+dbl2str(par::ci_level*100)) << " "  
	    << setw(10) << string("U"+dbl2str(par::ci_level*100)) << " ";    
      
      
      if (par::breslowday)
	MHOUT << setw(10) << "CHISQ_BD" << " "
	      << setw(10) << "P_BD" << " ";
      
      MHOUT << "\n";
      
      MHOUT.precision(4);
      
      printLOG("Cochran-Mantel-Haenszel 2x2xK test, K = " + int2str( nk) + "\n");

      if (par::breslowday)
	printLOG("Performing Breslow-Day test of homogeneous odds ratios\n");
      
      printLOG("Writing results to [ " + f + " ]\n");
      
      // Warnings, 
      if (par::breslowday && nk>10) 
	printLOG("** Warning ** Breslow-Day statistics require large N per cluster ** \n");
      
    }


  double zt = ltqnorm( 1 - (1 - par::ci_level) / 2  ) ; 
  

  // Cochran-Mantel-Haenszel 2x2xK test
  
  vector<double> results(nl_all);

  vector<CSNP*>::iterator s = SNP.begin();
  int l=0;
  while ( s != SNP.end() )
    {
      
      // Skip possibly
      if (par::adaptive_perm && !perm.snp_test[l])
	{
	  s++;
	  l++;
	  continue;
	}

      // Disease X allele X strata
      
      // Calculate mean of 11 cell for each strata
      vector<double> mean_11(nk,0);
      vector<double> var_11(nk,0);
 
      // Calculate statistic
      vector<double> n_11(nk,0);
      vector<double> n_12(nk,0);
      vector<double> n_21(nk,0);
      vector<double> n_22(nk,0);

      // Disease marginals
      vector<double> n_1X(nk,0); // disease
      vector<double> n_2X(nk,0); // no disease

      vector<double> n_X1(nk,0); // F allele
      vector<double> n_X2(nk,0); // T allele
      
      vector<double> n_TT(nk,0); // Total allele count
      

      ///////////////////////// 	      
      // Autosomal or haploid?
	      
      bool X=false, haploid=false;
      if (par::chr_sex[locus[l]->chr]) X=true;
      else if (par::chr_haploid[locus[l]->chr]) haploid=true;

      ////////////////////////
      // Consider each person
      
      vector<bool>::iterator i1 = (*s)->one.begin();
      vector<bool>::iterator i2 = (*s)->two.begin();
      vector<Individual*>::iterator gperson = sample.begin();

      while ( gperson != sample.end() )
	{
	  Individual * pperson = (*gperson)->pperson;

	  bool s1 = *i1;
	  bool s2 = *i2;
	  
	  // Affected individuals
	  if ( pperson->aff && !pperson->missing )
	    {

		// Haploid?
		if ( haploid || ( X && (*gperson)->sex ) ) 
		{

	      // Allelic marginal
	      if ( ! s1  )
		{
		  // FF hom
		      n_11[ pperson->sol ] ++ ;
		      n_X1[ pperson->sol ] ++ ;		    
		}
	      else 
		{
		  if ( ! s2 ) // FT
		    {
		      gperson++;
		      i1++;
		      i2++;
		      continue;  // skip missing genotypes
		    }
		  else // TT
		    {
		      n_12[ pperson->sol ] ++ ;
		      n_X2[ pperson->sol ] ++ ;		      
		    }
		}

	      // Disease marginal
	      n_1X[ pperson->sol ] ++;
	      n_TT[ pperson->sol ] ++;

		}
		else   // autosomal
		{
		
	      // Allelic marginal
	      if ( ! s1  )
		{
		  if ( ! s2 ) // FF hom
		    {
		      n_11[ pperson->sol ] +=2 ;
		      n_X1[ pperson->sol ] +=2 ;
		    }
		  else
		    {
		      n_11[ pperson->sol ]++ ; // FT het
		      n_12[ pperson->sol ]++ ;
		      n_X1[ pperson->sol ]++ ;
		      n_X2[ pperson->sol ]++ ;		      
		    }
		}
	      else 
		{
		  if ( ! s2 ) // FT
		    {
		      gperson++;
		      i1++;
		      i2++;
		      continue;  // skip missing genotypes
		    }
		  else // TT
		    {
		      n_12[ pperson->sol ] +=2 ;
		      n_X2[ pperson->sol ] +=2 ;		      
		    }
		}

	      // Disease marginal
	      n_1X[ pperson->sol ] += 2;
	      n_TT[ pperson->sol ] += 2;

	      } // end autosomal

	    }
	  else if ( ! pperson->missing ) // Unaffecteds
	    {

		// Haploid?
		if ( haploid || ( X && (*gperson)->sex ) ) 
		{

	      // Allelic marginal
	      if ( ! s1  )
		{
		  // FF hom
		      n_21[ pperson->sol ] ++ ;
		      n_X1[ pperson->sol ] ++ ;		    
		}
	      else 
		{
		  if ( ! s2 ) // FT
		    {
		      gperson++;
		      i1++;
		      i2++;
		      continue;  // skip missing genotypes
		    }
		  else // TT
		    {
		      n_22[ pperson->sol ] ++ ;
		      n_X2[ pperson->sol ] ++ ;		      
		    }
		}

	      // Disease marginal
	      n_2X[ pperson->sol ] ++;
	      n_TT[ pperson->sol ] ++;

		}
		else   // autosomal 
		{
	      // Allelic marginal
	      if ( ! s1 )
		{
		  if ( ! s2 ) // FF
		    {
		      n_X1[ pperson->sol ] +=2 ;
		      n_21[ pperson->sol ] +=2 ;
		    }
		  else
		    {
		      n_X1[ pperson->sol ] ++ ;
		      n_X2[ pperson->sol ] ++ ;		      
		      n_21[ pperson->sol ] ++ ;
		      n_22[ pperson->sol ] ++ ;
		    }
		}
	      else 
		{
		  if ( ! s2 ) // FT
		    {
		      gperson++;
		      i1++;
		      i2++;
		      continue;  // skip missing genotypes
		    }
		  else // TT
		    {
		      n_X2[ pperson->sol ] +=2 ;		      		      
		      n_22[ pperson->sol ] +=2 ;
		    }
		}     

	      // disease marginal
	      n_2X[ pperson->sol ] += 2;
	      n_TT[ pperson->sol ] += 2;

		}  // end autosomal			      
	    } // end unaffected

	  gperson++;
	  i1++;
	  i2++;
	  
	} // count next individual
    
      
      
      

      // Finished iterating over individuals: cluster needs at least 2 
      // nonmissing individuals
      
      vector<bool> validK(nk,false);
      for (int k=0; k<nk; k++)
	if (n_TT[k]>=2) validK[k]=true;
      
      for (int k=0; k<nk; k++)
	{
	  if (validK[k])
	    {
	      mean_11[k] = ( n_X1[k] * n_1X[k] ) / n_TT[k] ;
	      var_11[k] = ( n_X1[k] * n_X2[k] * n_1X[k] * n_2X[k] ) 
		/ ( n_TT[k]*n_TT[k]*(n_TT[k]-1) );

// 	      cout << k << " " 
// 		   << n_11[k] << " " 
// 		   << n_12[k] << " " 
// 		   << n_21[k] << " " 
// 		   << n_22[k] << "\n";
	      
	    }
	}

      
      double CMH = 0;
      double denom = 0;
      for (int k=0; k<nk; k++)
	{
	  if (validK[k])
	    {
	      CMH += n_11[k] - mean_11[k];
	      denom += var_11[k];
	    }
	}

      CMH *= CMH;
      CMH /= denom;
 
      // MH Odds ratio & CI
      double R = 0;
      double S = 0;
      vector<double> r2(nk);
      vector<double> s2(nk);      

      for (int k=0; k<nk; k++)
	{
	  if (validK[k])
	    {
	      r2[k] = (n_11[k]*n_22[k]) / n_TT[k];
	      s2[k] = (n_12[k]*n_21[k]) / n_TT[k];
	      R += r2[k]; 
	      S += s2[k];
	    }
	}
      double OR = R / S ;

      double v1 = 0, v2 = 0, v3 = 0;
      for (int k=0; k<nk; k++)
	{
	  if (validK[k])
	    {
	      v1 += (1/n_TT[k]) * ( n_11[k] + n_22[k] ) * r2[k] ;
	      v2 += (1/n_TT[k]) * ( n_12[k] + n_21[k] ) * s2[k] ;
	      v3 += (1/n_TT[k]) * ( ( n_11[k] + n_22[k] ) * s2[k] 
				+ ( n_12[k] + n_21[k] ) * r2[k] );
	    }
	}
      
      double SE = ( 1/(2*R*R) )  * v1
	+ (1/(2*S*S)) * v2
	+ (1/(2*R*S)) * v3 ;
      SE = sqrt(SE);
      
      double OR_lower = exp( log(OR) - zt * SE );
      double OR_upper = exp( log(OR) + zt * SE );
      
      if ( original )
	{

	  double pvalue = chiprobP(CMH,1);
	  
	  // Skip?, if filtering p-values
	  if ( par::pfilter && ( pvalue > par::pfvalue || pvalue < 0 ) ) 
	    goto skip_p_cmh;
	  

	  MHOUT << setw(4) << locus[l]->chr << " "
		<< setw(par::pp_maxsnp) << locus[l]->name << " "
		<< setw(10) << locus[l]->bp << " "
		<< setw(4) << locus[l]->allele1 << " " 
		<< setw(8) << locus[l]->freq << " " 
		<< setw(4) << locus[l]->allele2 << " ";
	  

	  if (realnum(CMH))
	    MHOUT << setw(10) << CMH << " "
		  << setw(10) << chiprobP(CMH,1) << " ";
	  else
	    MHOUT << setw(10) << "NA" << " "
		  << setw(10) << "NA" << " ";
	  
	  
	  if (realnum(OR))
	    MHOUT << setw(10) << OR << " ";
	  else
	    MHOUT << setw(10) << "NA" << " ";

	  if (realnum(SE))
	    MHOUT << setw(10) << SE << " ";
	  else 
	    MHOUT << setw(10) << "NA" << " ";

	  if (realnum(OR_lower))
	    MHOUT << setw(10) << OR_lower << " ";
	  else
	    MHOUT << setw(10) << "NA" << " ";

	  if (realnum(OR_upper))
	    MHOUT << setw(10) << OR_upper << " ";
	  else
	    MHOUT << setw(10) << "NA" << " ";
	  

	  // Optional Breslow-Day test of homogeneity of odds ratios
	  if (par::breslowday)
	    {
	      
	      double amax;
	      double bb;
	      double determ;
	      double as_plus;
	      double as_minus;
	      double Astar;
	      double Bstar;
	      double Cstar;
	      double Dstar;
	      double Var;
	      double BDX2 = 0;
	      int df = 0;
	      for (int k=0; k<nk; k++)
		{
		  if (validK[k])
		    {
		      df++;
		      amax = (n_1X[k] < n_X1[k]) ? n_1X[k] : n_X1[k];
		      bb = n_2X[k] + n_1X[k] * OR - n_X1[k] * (1-OR);
		      determ = sqrt(bb*bb + 4*(1-OR) * OR * n_1X[k] * n_X1[k]);
		      as_plus = ( -bb + determ ) / ( 2 - 2 * OR );
		      as_minus = ( -bb - determ ) / ( 2 - 2 * OR );		      
		      Astar =  as_minus <= amax && as_minus >= 0 ? as_minus  : as_plus ;
		      Bstar = n_1X[k] - Astar;
		      Cstar = n_X1[k] - Astar;
		      Dstar = n_2X[k] - n_X1[k] + Astar;
		      Var = 1/(1/Astar + 1/Bstar + 1/Cstar + 1/Dstar);
		      BDX2 += ( (n_11[k] - Astar) * ( n_11[k] - Astar ) ) / Var ; 
		    }
		}
	      
	      double BDp = chiprobP( BDX2 , df-1 ); 
	      
	      if ( BDp > -1 )   
		MHOUT << setw(10) << BDX2 << " " 
		      << setw(10) << BDp << " ";	  
	      else
		MHOUT << setw(10) << "NA" << " " 
		      << setw(10) << "NA" << " ";	  
	      
	    }
	  
	  MHOUT << "\n";
	  
	}

    skip_p_cmh:
      
      // Store for permutation procedure, based 2x2xK CMH result
      results[l] = CMH;
      
      // Next SNP
      s++;
      l++;

    }
  

  if (original)
    MHOUT.close();

  return results;
  
}