////////DECAY FUNCTIONS///////////
/// There is a single decay rate depending on the sign of the running sum
//Decrement running sum based on decay probability eta * filter state (running sum)
//As we approach the threshold the decay increases proportionally
int synapseSingleFilterUnifiedWithDecayReflecting::doStochasticDecay()
{

	double p;
	unsigned int rDecaySteps;

	//g_rng_r = getRandGeneratorInstance();
	if (miRFilterValue == 0) return 0;

	if (miRFilterValue > 0) //p side decay
	{
		p = 1.0-exp(-mdDecayRate*miTimeSinceLastInduction);
		rDecaySteps = gsl_ran_binomial(mprng,p,miRFilterValue);
		miRFilterValue -= rDecaySteps; //Subtract to move to zero
	}
	else //Increment Sum - q side is decaying back to zero
	{
		p = 1.0-exp(-mdDecayRate*miTimeSinceLastInduction);
		rDecaySteps = gsl_ran_binomial(mprng,p,-miRFilterValue);
		miRFilterValue += rDecaySteps; //Add To move to zero
	}


	return rDecaySteps;
}
コード例 #2
0
ファイル: GSLAlgebra.cpp プロジェクト: awarematics/SECONDO
int gslalg_rng_binomial_VM ( Word* args, Word& result,
                         int message, Word& local, Supplier s )
{
  result = qp->ResultStorage( s );
  CcInt *res = ((CcInt*) result.addr);
  long resL = 0;
  CcInt  *cN = (CcInt*) args[0].addr;
  CcReal *cP = (CcReal*) args[1].addr;
  if( cN->IsDefined() && cP->IsDefined() )
  {
    double p = cP->GetRealval();
    long n = cN->GetIntval();
    if( n >= 0 && p >= 0 && p <= 1.0 )
    {
      resL = gsl_ran_binomial(the_gsl_randomgenerator.GetGenerator(), p, n);
      res->Set(true, resL);
    }
    else
    {
      res->Set(false, 0);
    }
  }
  else
    res->Set(false, 0);
  return (0);
}
コード例 #3
0
ファイル: GSLRNG.c プロジェクト: B-Rich/breve
int GSLRNG_binomial(stEval *args, stEval *result, void *i) {
    gsl_rng *r = STPOINTER(&args[0]);
    double p = STDOUBLE(&args[1]);
	int n = STINT(&args[2]);
    STINT(result) = gsl_ran_binomial(r,p,n);
    return EC_OK;
}
コード例 #4
0
ファイル: apop_multinomial.c プロジェクト: b-k/apophenia
static int multinomial_rng(double *out, gsl_rng *r, apop_model* est){
    Nullcheck_mp(est, 1);
    double * p = est->parameters->vector->data;
    //the trick where we turn the params into a p-vector
    int N = p[0];

    if (est->parameters->vector->size == 2) {
        *out = gsl_ran_binomial_knuth(r, 1-gsl_vector_get(est->parameters->vector, 1), N);
        out[1] = N-*out;
        goto done;
    }
    //else, multinomial
    //cut/pasted/modded from the GSL. Copyright them.
    p[0] = 1 - (apop_sum(est->parameters->vector)-N);
    double sum_p = 0.0;
    int sum_n = 0;

    for (int i = 0; i < est->parameters->vector->size; i++) {
        out[i] = (p[i] > 0)
                ? gsl_ran_binomial (r, p[i] / (1 - sum_p), N - sum_n)
                : 0;
        sum_p += p[i];
        sum_n += out[i];
    }
    done:
    p[0] = N;
    return 0;
}
コード例 #5
0
ファイル: moduleRandom.c プロジェクト: dcf21/pyxplot9
void pplfunc_frandombin(ppl_context *c, pplObj *in, int nArgs, int *status, int *errType, char *errText)
 {
  char *FunctionDescription = "binomial(p,n)";
  if (rndgen==NULL) { rndgen = gsl_rng_alloc(gsl_rng_default); gsl_rng_set(rndgen, 0); }
  CHECK_NEEDINT(in[1], "n", "function's second argument must be an integer in the range");
  OUTPUT.real = gsl_ran_binomial(rndgen, in[0].real, (unsigned int)in[1].real);
  CHECK_OUTPUT_OKAY;
 }
コード例 #6
0
long
librandom::GSL_BinomialRandomDev::ldev( RngPtr rng ) const
{
  GslRandomGen* gsr_rng = dynamic_cast< GslRandomGen* >( &( *rng ) );
  if ( !gsr_rng )
    throw UnsuitableRNG(
      "The gsl_binomial RDV can only be used with GSL RNGs." );
  return gsl_ran_binomial( gsr_rng->rng_, p_, n_ );
}
コード例 #7
0
ファイル: libranddist.c プロジェクト: CNMAT/CNMAT-Externs
void librdist_binomial(gsl_rng *rng, int argc, void *argv, int bufc, float *buf){
	t_atom *av = (t_atom *)argv;
	if(argc != librdist_getnargs(ps_binomial)){
		return;
	}
	const double p = librdist_atom_getfloat(av);
	const unsigned int n = (unsigned int)librdist_atom_getlong(av + 1);
	int i;
	for(i = 0; i < bufc; i++)
	       	buf[i] = (float)gsl_ran_binomial(rng, p, n);
}
コード例 #8
0
ファイル: poisson.c プロジェクト: tommyliu/visionPJ1
unsigned int
gsl_ran_poisson (const gsl_rng * r, double mu)
{
  double emu;
  double prod = 1.0;
  unsigned int k = 0;

  while (mu > 10)
    {
      unsigned int m = mu * (7.0 / 8.0);

      double X = gsl_ran_gamma_int (r, m);

      if (X >= mu)
        {
          return k + gsl_ran_binomial (r, mu / X, m - 1);
        }
      else
        {
          k += m;
          mu -= X; 
        }
    }

  /* This following method works well when mu is small */

  emu = exp (-mu);

  do
    {
      prod *= gsl_rng_uniform (r);
      k++;
    }
  while (prod > emu);

  return k - 1;

}
コード例 #9
0
ファイル: channel.c プロジェクト: PhDP/mackay2003
int main(int argc, char **argv) {
  if (argc != 3) {
    printf("Provide two arguments: a probability of failure per bit and a message.\n");
    printf("More than one error can hit the same bit, and a character is made of 1 byte (8 bits).\n");
  }
  const size_t size = strlen(argv[2]);
  char *received = malloc(size + 1);
  strcpy(received, argv[2]);
  const double f = atof(argv[1]);

  gsl_rng *rng = gsl_rng_alloc(gsl_rng_taus2);
  gsl_rng_set(rng, time(NULL)); // Seed with time
  const int errors = gsl_ran_binomial(rng, f, size * 8);
  for (int i = 0; i < errors; ++i) {
    unsigned int toflip = (int)(gsl_rng_uniform(rng) * size);
    bitflip(received + toflip, rng);
  }
  printf("Message sent:\n%s\n", argv[2]);
  printf("Message received:\n%s\n", received);
  printf("Bits flipped: %d\n", errors);
  free(received);
  return EXIT_SUCCESS;
}
コード例 #10
0
ファイル: multinomial.c プロジェクト: CNMAT/CNMAT-Externs
void
gsl_ran_multinomial (const gsl_rng * r, const size_t K,
                     const unsigned int N, const double p[], unsigned int n[])
{
  size_t k;
  double norm = 0.0;
  double sum_p = 0.0;

  unsigned int sum_n = 0;

  /* p[k] may contain non-negative weights that do not sum to 1.0.
   * Even a probability distribution will not exactly sum to 1.0
   * due to rounding errors. 
   */

  for (k = 0; k < K; k++)
    {
      norm += p[k];
    }

  for (k = 0; k < K; k++)
    {
      if (p[k] > 0.0)
        {
          n[k] = gsl_ran_binomial (r, p[k] / (norm - sum_p), N - sum_n);
        }
      else
        {
          n[k] = 0;
        }

      sum_p += p[k];
      sum_n += n[k];
    }

}
コード例 #11
0
ファイル: pidrtpcrsim.cpp プロジェクト: cbg-ethz/PrimerID
void run_simulation(int thread_id, intVector& rank_abundance, intVector& rank_occurrence)
{
    gsl_rng* r = gsl_rng_alloc(gsl_rng_mt19937);
    gsl_rng_set(r, random_seed());
    std::default_random_engine generator(random_seed());

    // RT
    intVector sample_after_RT(bins);

    // PCR
    uint64_t no_PCR_molecules;
    std::vector<uint64_t> sample_after_PCR(bins);

    // Sequencing sample
    intVector sampler_after_seq(bins);

    for (int i = 1; i <= (N_trials - 1 - thread_id) / number_threads + 1; ++i) {
        if ((thread_id == 0) && (i * number_threads % 10 == 0))
            std::cout << "Trial no.: " << i* number_threads << '\n';

        // 1.) simulate RT
        gsl_ran_multinomial(r, bins, N_RT, prob_RT.data(), sample_after_RT.data());

        // 2.) simulate PCR (branching process)
        no_PCR_molecules = 0;
        for (int j = 0; j < bins; ++j) {
            if (sample_after_RT[j]) {
                sample_after_PCR[j] = sample_after_RT[j];

                // stochastic amplification
                for (int k = 0; k < cycles; ++k) {
                    sample_after_PCR[j] += gsl_ran_binomial(r, p, sample_after_PCR[j]);
                }
                no_PCR_molecules += sample_after_PCR[j];
            }
            else {
                sample_after_PCR[j] = 0;
            }
        }

        if (thread_id == 0) {
            average_PCR_molecules += no_PCR_molecules;
        }

        // 3.) sequencing sample
        if (no_PCR_molecules > 100 * N_reads) {
            // multinomial approximation is good
            std::sort(sample_after_PCR.begin(), sample_after_PCR.end(), std::greater<intType>());
            sampler_after_seq = ran_mult_multinomial(sample_after_PCR, N_reads, r, min_coverage);
            ++used_with_replacement;
        }
        else {
            // multinomial approximation is not good enough!
            sampler_after_seq = ran_mult_hypergeometric(sample_after_PCR, N_reads, r, generator, min_coverage);
            ++used_without_replacement;
        }

        std::sort(sampler_after_seq.begin(), sampler_after_seq.end(), std::greater<intType>());

        // 4.) enter rankings
        for (int j = 0; j < rank_cutoff; ++j) {
            if (sampler_after_seq[j]) {
                rank_abundance[j] += sampler_after_seq[j];
                ++rank_occurrence[j];
            }
        }
    }

    gsl_rng_free(r);
}
コード例 #12
0
long
librandom::GSL_BinomialRandomDev::ldev()
{
  return gsl_ran_binomial( rng_, p_, n_ );
}
コード例 #13
0
ファイル: probability.cpp プロジェクト: mlabadi2/code
unsigned int GslRandGen::binomial(const double & p, unsigned int n)
{
  return gsl_ran_binomial(r, p, n);
}
コード例 #14
0
double
test_binomial_large (void)
{
  return gsl_ran_binomial (r_global, 0.3, 55);
}
コード例 #15
0
void form_offspring(
		const  gsl_rng *rand, 
		int    k, 
		int    picked_father, 
		int    counter, 
		int  **ptr_matrix_del, 
		int  **ptr_matrix_env, 
		int  **ptr_offspring_matrix_del, 
		int  **ptr_offspring_matrix_env
		){
	
	int i;	
	
	/*I think that this works both for selfing and for sexual reproduction*/	

	for(i=0;i<LOCI_ENV;i++){
		
		ptr_offspring_matrix_env[k][i]=0;


                if(ptr_matrix_env[k][i]+ptr_matrix_env[picked_father][i]==0){
                        ptr_offspring_matrix_env[counter][i]=0;
                }

                if(ptr_matrix_env[k][i]+ptr_matrix_env[picked_father][i]==4){
                        ptr_offspring_matrix_env[counter][i]=2;
                }

                if(ptr_matrix_env[k][i]+ptr_matrix_env[picked_father][i]==1){
                        ptr_offspring_matrix_env[counter][i]=gsl_ran_bernoulli(rand, 0.5);
                }

                if(ptr_matrix_env[k][i]+ptr_matrix_env[picked_father][i]==3){
                        ptr_offspring_matrix_env[counter][i]=1+gsl_ran_bernoulli(rand,0.5);
                }

                if(ptr_matrix_env[k][i]+ptr_matrix_env[picked_father][i]==2){
                        if(ptr_matrix_env[k][i]==0 || ptr_matrix_env[picked_father][i]==0){
                                ptr_offspring_matrix_env[counter][i]=1;
                        }
                        else{
                                ptr_offspring_matrix_env[counter][i]=gsl_ran_binomial(rand,0.5,2);
                        }
                }

	}


	for(i=0;i<LOCI_DEL;i++){
	
		ptr_offspring_matrix_del[k][i]=0;
		

		if(ptr_matrix_del[k][i]+ptr_matrix_del[picked_father][i]==0){
			ptr_offspring_matrix_del[counter][i]=0;
		}

		if(ptr_matrix_del[k][i]+ptr_matrix_del[picked_father][i]==4){
			ptr_offspring_matrix_del[counter][i]=2;
		}

		if(ptr_matrix_del[k][i]+ptr_matrix_del[picked_father][i]==1){
			ptr_offspring_matrix_del[counter][i]=gsl_ran_bernoulli(rand, 0.5);
		}

		if(ptr_matrix_del[k][i]+ptr_matrix_del[picked_father][i]==3){
			ptr_offspring_matrix_del[counter][i]=1+gsl_ran_bernoulli(rand,0.5);
		}

		if(ptr_matrix_del[k][i]+ptr_matrix_del[picked_father][i]==2){
			if(ptr_matrix_del[k][i]==0 || ptr_matrix_del[picked_father][i]==0){
				ptr_offspring_matrix_del[counter][i]=1;
			}
			else{
				ptr_offspring_matrix_del[counter][i]=gsl_ran_binomial(rand,0.5,2);
			}
		}

	}

}
コード例 #16
0
ファイル: qapprox.c プロジェクト: kbullaughey/polyevol
main(int argc, char **argv )
{

int  **genos[2] ;
FILE *fp1, *palpha  ;
int popsize, nloci, ngen , gen  ;
int paroff, matpat, ind, loc , nfix ;
double ran1() ;
double s,  alpha, sum , sumsq,  env, initfreq1, *freqinit  ;
double *effects,  opt, sig , fitmax, mutrate, *freqsp, *freqsoff , *ptemp ;
double var, meanphen, sumpa, suma, delta, pprime ;

if( argc < 7 ) { fprintf( stderr, " qapprox  popsize nloci optimum  s(strength of selection) ngen  mutrate \n"); exit(1); }
popsize = strtol( argv[1],NULL,10);
nloci = strtol( argv[2],NULL,10);
opt = strtod( argv[3],NULL);
s = strtod( argv[4],NULL);
ngen = strtol( argv[5],NULL,10);
	mutrate = strtod( argv[6],NULL);
/*	alpha = strtod( argv[7],NULL); */
	/*	nfix = strtol( argv[8],NULL,10); */
/*		initfreq1  = strtod( argv[9],NULL); */

	gsl_rng *rng = gsl_rng_alloc(gsl_rng_taus2);
    gsl_rng_set(rng, 123 );

/* to gen a binomial: 		nwt = gsl_ran_binomial( rng, epwt,(unsigned int)n) ;   */

	env = 0. ;

fp1 = fopen("phenout", "w");
	
/* vectors to store  effect size at each locus,  phenotypes of individuals, and fitnesses of individuals */
	effects = (double *)malloc( (unsigned)nloci*sizeof(double) ) ;
	freqsp = (double *)malloc( (unsigned)nloci*sizeof(double) ) ;
	freqsoff = (double *)malloc( (unsigned)nloci*sizeof(double) ) ;
	freqinit = (double *)malloc( (unsigned)nloci*sizeof(double) ) ;

palpha = fopen( "alphas","r");
for( loc = 0 ; loc<nloci; loc++)   fscanf(palpha," %lf", effects+loc)   ;
for( loc = 0; loc <nloci ; loc++) fscanf(stdin," %lf", freqinit+loc ) ;



for( loc = 0; loc < nloci; loc++) printf(" %lf", freqinit[loc] );
printf("\n");
for( loc = 0; loc < nloci; loc++) freqsp[loc] = freqinit[loc] ;
	
suma = 0.0 ;
for( loc=0; loc <nloci; loc++) suma += effects[loc] ;

/* main loop */
for( gen = 0 ; gen < ngen ; gen++){
/*   if( gen == 3000 ) opt += 2.0 ; */ 

	sumpa = var =  0. ;
	for( loc=0; loc<nloci; loc++) { 
	       sumpa +=  freqsp[loc]*effects[loc] ; 
		   var += 2.*effects[loc]*effects[loc]*freqsp[loc]*(1.0-freqsp[loc] ) ;
		   } 
	meanphen = 2.*sumpa - suma ;
	 delta = meanphen - opt ;
	 fprintf(fp1,"%lf\t%lf\n",meanphen, var ) ;

 /*   pprime =    */
    for( loc = 0; loc< nloci; loc++){
		pprime = freqsp[loc]  + 0.5*s*effects[loc]*effects[loc]*freqsp[loc]*(1.0-freqsp[loc] )*( (2.0*freqsp[loc]-1.0) - 2.0*delta/effects[loc] ) - mutrate*(2.0*freqsp[loc] -1.0 ) ;
	     freqsoff[loc] = gsl_ran_binomial( rng, pprime, (unsigned int)(2*popsize) )/(2.0*popsize) ;
	}
	for( loc = 0; loc < nloci; loc++) printf(" %lf", freqsoff[loc] );
	printf("\n");
	
/*		fprintf(fp1,"%lf\t%lf\n",sum/popsize, sumsq/popsize - (sum/popsize)*(sum/popsize) ) ; */
	ptemp = freqsp;
	freqsp = freqsoff ;
	freqsoff = ptemp ;
  }
/* end of main loop */
}
コード例 #17
0
unsigned long librandom::GSL_BinomialRandomDev::uldev(RngPtr rng) const
{
  GslRandomGen* gsr_rng = dynamic_cast<GslRandomGen*>(&(*rng));
  assert (gsr_rng && "rng needs to be a GSL RNG");
  return gsl_ran_binomial(gsr_rng->rng_, p_, n_);
}
コード例 #18
0
ファイル: SEIR_Ebola.cpp プロジェクト: lucymli/EpiGenMCMC
void Model::simulate(std::vector<double> & model_params, std::vector<std::string> & param_names, Trajectory * traj, int start_dt, int end_dt, double step_size, int total_dt,  gsl_rng * rng) {
    if ((traj->get_state(1)+traj->get_state(2)) < 1.0) {
        return;
    }
    double Beta = model_params[0];
    double k=model_params[1];
    double rateE2I=model_params[2];
    double rateI2R=model_params[3];
    double R0_reduce=model_params[6];
    int change_T=model_params[7]/step_size;
    double Rt = Beta/rateI2R*traj->get_state(0);
    double total_infectious=0.0;
    double new_infections=0.0;
    double divisions = 3.0;
    double p1 = rateE2I*step_size/divisions;
    double p2 = rateI2R*step_size/divisions;
    double S2E = 0.0;
    double E2I = 0.0;
    double I2R = 0.0;
    double currS;
    double latent;
    double infectious;
    double Tg = 1.0/rateE2I + 1.0/rateI2R;
    // */
    for (int t=start_dt; t<end_dt; ++t) {
        double sub_t_I2R = 0.0;
        for (int sub_t=0; sub_t<(int) divisions; ++sub_t) {
            //
            // Transitions
            //
            // Recoveries: I --> R
            currS = traj->get_state(0);
            latent = traj->get_state(1);
            infectious = traj->get_state(2);
            if (infectious > 0) {
                if (p2 >= 1.0) I2R = infectious;
                else if (use_deterministic) {
                    I2R = infectious*p2;
                }
                else {
                    I2R = gsl_ran_binomial(rng, p2, infectious); // Recoveries
                }
            }
            // Becoming infectious: E --> I
            if (latent > 0) {
                if (p1 >= 1.0) E2I = latent;
                else if (use_deterministic) {
                    E2I = latent * p1;
                }
                else {
                    E2I = gsl_ran_binomial(rng, p1, latent); // Becoming infectious
                }
            }
            traj->set_state(infectious-I2R+E2I, 2); // Infectious
            if (use_deterministic) {
                S2E = Beta * currS * step_size * infectious;
            } else {
                // Infections: S --> E
                if (I2R > 0.0) {
                    total_infectious += I2R;
                    sub_t_I2R += I2R;
                    //                traj->set_traj(I2R, t-start_dt); // People are sampled, i.e. appear in the time-series at the time of recovery
                    if (currS > 0) {
                        // Current reproductive number
                        Rt = Beta / rateI2R * currS;
                        if (change_T>=t) Rt *= R0_reduce;
                        // Draw from the negative binomial distribution (gamma-poisson mixture) to determine
                        // number of secondary infections
                        S2E = gsl_ran_negative_binomial(rng, k/(k+Rt), k*I2R);  // New infections
                        if (S2E > 0) {
                            S2E = std::min(currS, S2E);
                            new_infections += S2E;
                            traj->set_state(currS-S2E, 0); // Susceptible
                        }
                    }
                }
            }
            traj->set_state(latent+S2E-E2I, 1); // Latent
            S2E=0.0;
            E2I=0.0;
            I2R=0.0;
        }
        traj->set_traj(0, sub_t_I2R, t-start_dt);
        double N = traj->get_state(1)+traj->get_state(2);
        // Record 1/N for coalescent rate calculation
        if (N > 0.0) {
            traj->set_traj(1, N, t-start_dt);
            traj->set_traj(2, Rt/Tg*(1.0+1.0/k), t-start_dt);
        }
        else {
            traj->set_traj(1, 0.0, t-start_dt);
            traj->set_traj(2, 0.0, t-start_dt);
            break;
        }
    }
}
コード例 #19
0
double
test_binomial_huge (void)
{
  return gsl_ran_binomial (r_global, 0.3, 5500);
}
コード例 #20
0
double
test_binomial1 (void)
{
  return gsl_ran_binomial (r_global, 1, 8);
}
コード例 #21
0
unsigned int
gsl_ran_binomial_tpe (const gsl_rng * rng, double p, unsigned int n)
{
  return gsl_ran_binomial (rng, p, n);
}
コード例 #22
0
ファイル: gsl-randist.c プロジェクト: hongjiedai/svmheavy.net
int
main (int argc, char *argv[])
{
  size_t i,j;
  size_t n = 0;
  double mu = 0, nu = 0, nu1 = 0, nu2 = 0, sigma = 0, a = 0, b = 0, c = 0;
  double zeta = 0, sigmax = 0, sigmay = 0, rho = 0;
  double p = 0;
  double x = 0, y =0, z=0  ;
  unsigned int N = 0, t = 0, n1 = 0, n2 = 0 ;
  unsigned long int seed = 0 ;
  const char * name ;
  gsl_rng * r ;

  if (argc < 4) 
    {
      printf (
"Usage: gsl-randist seed n DIST param1 param2 ...\n"
"Generates n samples from the distribution DIST with parameters param1,\n"
"param2, etc. Valid distributions are,\n"
"\n"
"  beta\n"
"  binomial\n"
"  bivariate-gaussian\n"
"  cauchy\n"
"  chisq\n"
"  dir-2d\n"
"  dir-3d\n"
"  dir-nd\n"
"  erlang\n"
"  exponential\n"
"  exppow\n"
"  fdist\n"
"  flat\n"
"  gamma\n"
"  gaussian-tail\n"
"  gaussian\n"
"  geometric\n"
"  gumbel1\n"
"  gumbel2\n"
"  hypergeometric\n"
"  laplace\n"
"  landau\n"
"  levy\n"
"  levy-skew\n"
"  logarithmic\n"
"  logistic\n"
"  lognormal\n"
"  negative-binomial\n"
"  pareto\n"
"  pascal\n"
"  poisson\n"
"  rayleigh-tail\n"
"  rayleigh\n"
"  tdist\n"
"  ugaussian-tail\n"
"  ugaussian\n"
"  weibull\n") ;
      exit (0);
    }

  argv++ ; seed = atol (argv[0]); argc-- ;
  argv++ ; n = atol (argv[0]); argc-- ;
  argv++ ; name = argv[0] ; argc-- ; argc-- ;

  gsl_rng_env_setup() ;

  if (gsl_rng_default_seed != 0) {
    fprintf(stderr, 
            "overriding GSL_RNG_SEED with command line value, seed = %ld\n", 
            seed) ;
  }
  
  gsl_rng_default_seed = seed ;

  r = gsl_rng_alloc(gsl_rng_default) ;


#define NAME(x) !strcmp(name,(x))
#define OUTPUT(x) for (i = 0; i < n; i++) { printf("%g\n", (x)) ; }
#define OUTPUT1(a,x) for(i = 0; i < n; i++) { a ; printf("%g\n", x) ; }
#define OUTPUT2(a,x,y) for(i = 0; i < n; i++) { a ; printf("%g %g\n", x, y) ; }
#define OUTPUT3(a,x,y,z) for(i = 0; i < n; i++) { a ; printf("%g %g %g\n", x, y, z) ; }
#define INT_OUTPUT(x) for (i = 0; i < n; i++) { printf("%d\n", (x)) ; }
#define ARGS(x,y) if (argc != x) error(y) ;
#define DBL_ARG(x) if (argc) { x=atof((++argv)[0]);argc--;} else {error( #x);};
#define INT_ARG(x) if (argc) { x=atoi((++argv)[0]);argc--;} else {error( #x);};

  if (NAME("bernoulli"))
    {
      ARGS(1, "p = probability of success");
      DBL_ARG(p)
      INT_OUTPUT(gsl_ran_bernoulli (r, p));
    }
  else if (NAME("beta"))
    {
      ARGS(2, "a,b = shape parameters");
      DBL_ARG(a)
      DBL_ARG(b)
      OUTPUT(gsl_ran_beta (r, a, b));
    }
  else if (NAME("binomial"))
    {
      ARGS(2, "p = probability, N = number of trials");
      DBL_ARG(p)
      INT_ARG(N)
      INT_OUTPUT(gsl_ran_binomial (r, p, N));
    }
  else if (NAME("cauchy"))
    {
      ARGS(1, "a = scale parameter");
      DBL_ARG(a)
      OUTPUT(gsl_ran_cauchy (r, a));
    }
  else if (NAME("chisq"))
    {
      ARGS(1, "nu = degrees of freedom");
      DBL_ARG(nu)
      OUTPUT(gsl_ran_chisq (r, nu));
    }
  else if (NAME("erlang"))
    {
      ARGS(2, "a = scale parameter, b = order");
      DBL_ARG(a)
      DBL_ARG(b)
      OUTPUT(gsl_ran_erlang (r, a, b));
    }
  else if (NAME("exponential"))
    {
      ARGS(1, "mu = mean value");
      DBL_ARG(mu) ;
      OUTPUT(gsl_ran_exponential (r, mu));
    }
  else if (NAME("exppow"))
    {
      ARGS(2, "a = scale parameter, b = power (1=exponential, 2=gaussian)");
      DBL_ARG(a) ;
      DBL_ARG(b) ;
      OUTPUT(gsl_ran_exppow (r, a, b));
    }
  else if (NAME("fdist"))
    {
      ARGS(2, "nu1, nu2 = degrees of freedom parameters");
      DBL_ARG(nu1) ;
      DBL_ARG(nu2) ;
      OUTPUT(gsl_ran_fdist (r, nu1, nu2));
    }
  else if (NAME("flat"))
    {
      ARGS(2, "a = lower limit, b = upper limit");
      DBL_ARG(a) ;
      DBL_ARG(b) ;
      OUTPUT(gsl_ran_flat (r, a, b));
    }
  else if (NAME("gamma"))
    {
      ARGS(2, "a = order, b = scale");
      DBL_ARG(a) ;
      DBL_ARG(b) ;
      OUTPUT(gsl_ran_gamma (r, a, b));
    }
  else if (NAME("gaussian"))
    {
      ARGS(1, "sigma = standard deviation");
      DBL_ARG(sigma) ;
      OUTPUT(gsl_ran_gaussian (r, sigma));
    }
  else if (NAME("gaussian-tail"))
    {
      ARGS(2, "a = lower limit, sigma = standard deviation");
      DBL_ARG(a) ;
      DBL_ARG(sigma) ;
      OUTPUT(gsl_ran_gaussian_tail (r, a, sigma));
    }
  else if (NAME("ugaussian"))
    {
      ARGS(0, "unit gaussian, no parameters required");
      OUTPUT(gsl_ran_ugaussian (r));
    }
  else if (NAME("ugaussian-tail"))
    {
      ARGS(1, "a = lower limit");
      DBL_ARG(a) ;
      OUTPUT(gsl_ran_ugaussian_tail (r, a));
    }
  else if (NAME("bivariate-gaussian"))
    {
      ARGS(3, "sigmax = x std.dev., sigmay = y std.dev., rho = correlation");
      DBL_ARG(sigmax) ;
      DBL_ARG(sigmay) ;
      DBL_ARG(rho) ;
      OUTPUT2(gsl_ran_bivariate_gaussian (r, sigmax, sigmay, rho, &x, &y), 
              x, y);
    }
  else if (NAME("dir-2d"))
    {
      OUTPUT2(gsl_ran_dir_2d (r, &x, &y), x, y);
    }
  else if (NAME("dir-3d"))
    {
      OUTPUT3(gsl_ran_dir_3d (r, &x, &y, &z), x, y, z);
    }
  else if (NAME("dir-nd"))
    {
      double *xarr;  
      ARGS(1, "n1 = number of dimensions of hypersphere"); 
      INT_ARG(n1) ;
      xarr = (double *)malloc(n1*sizeof(double));

      for(i = 0; i < n; i++) { 
        gsl_ran_dir_nd (r, n1, xarr) ; 
        for (j = 0; j < n1; j++) { 
          if (j) putchar(' '); 
          printf("%g", xarr[j]) ; 
        } 
        putchar('\n'); 
      } ;

      free(xarr);
    }  
  else if (NAME("geometric"))
    {
      ARGS(1, "p = bernoulli trial probability of success");
      DBL_ARG(p) ;
      INT_OUTPUT(gsl_ran_geometric (r, p));
    }
  else if (NAME("gumbel1"))
    {
      ARGS(2, "a = order, b = scale parameter");
      DBL_ARG(a) ;
      DBL_ARG(b) ;
      OUTPUT(gsl_ran_gumbel1 (r, a, b));
    }
  else if (NAME("gumbel2"))
    {
      ARGS(2, "a = order, b = scale parameter");
      DBL_ARG(a) ;
      DBL_ARG(b) ;
      OUTPUT(gsl_ran_gumbel2 (r, a, b));
    }
  else if (NAME("hypergeometric"))
    {
      ARGS(3, "n1 = tagged population, n2 = untagged population, t = number of trials");
      INT_ARG(n1) ;
      INT_ARG(n2) ;
      INT_ARG(t) ;
      INT_OUTPUT(gsl_ran_hypergeometric (r, n1, n2, t));
    }
  else if (NAME("laplace"))
    {
      ARGS(1, "a = scale parameter");
      DBL_ARG(a) ;
      OUTPUT(gsl_ran_laplace (r, a));
    }
  else if (NAME("landau"))
    {
      ARGS(0, "no arguments required");
      OUTPUT(gsl_ran_landau (r));
    }
  else if (NAME("levy"))
    {
      ARGS(2, "c = scale, a = power (1=cauchy, 2=gaussian)");
      DBL_ARG(c) ;
      DBL_ARG(a) ;
      OUTPUT(gsl_ran_levy (r, c, a));
    }
  else if (NAME("levy-skew"))
    {
      ARGS(3, "c = scale, a = power (1=cauchy, 2=gaussian), b = skew");
      DBL_ARG(c) ;
      DBL_ARG(a) ;
      DBL_ARG(b) ;
      OUTPUT(gsl_ran_levy_skew (r, c, a, b));
    }
  else if (NAME("logarithmic"))
    {
      ARGS(1, "p = probability");
      DBL_ARG(p) ;
      INT_OUTPUT(gsl_ran_logarithmic (r, p));
    }
  else if (NAME("logistic"))
    {
      ARGS(1, "a = scale parameter");
      DBL_ARG(a) ;
      OUTPUT(gsl_ran_logistic (r, a));
    }
  else if (NAME("lognormal"))
    {
      ARGS(2, "zeta = location parameter, sigma = scale parameter");
      DBL_ARG(zeta) ;
      DBL_ARG(sigma) ;
      OUTPUT(gsl_ran_lognormal (r, zeta, sigma));
    }
  else if (NAME("negative-binomial"))
    {
      ARGS(2, "p = probability, a = order");
      DBL_ARG(p) ;
      DBL_ARG(a) ;
      INT_OUTPUT(gsl_ran_negative_binomial (r, p, a));
    }
  else if (NAME("pareto"))
    {
      ARGS(2, "a = power, b = scale parameter");
      DBL_ARG(a) ;
      DBL_ARG(b) ;
      OUTPUT(gsl_ran_pareto (r, a, b));
    }
  else if (NAME("pascal"))
    {
      ARGS(2, "p = probability, n = order (integer)");
      DBL_ARG(p) ;
      INT_ARG(N) ;
      INT_OUTPUT(gsl_ran_pascal (r, p, N));
    }
  else if (NAME("poisson"))
    {
      ARGS(1, "mu = scale parameter");
      DBL_ARG(mu) ;
      INT_OUTPUT(gsl_ran_poisson (r, mu));
    }
  else if (NAME("rayleigh"))
    {
      ARGS(1, "sigma = scale parameter");
      DBL_ARG(sigma) ;
      OUTPUT(gsl_ran_rayleigh (r, sigma));
    }
  else if (NAME("rayleigh-tail"))
    {
      ARGS(2, "a = lower limit, sigma = scale parameter");
      DBL_ARG(a) ;
      DBL_ARG(sigma) ;
      OUTPUT(gsl_ran_rayleigh_tail (r, a, sigma));
    }
  else if (NAME("tdist"))
    {
      ARGS(1, "nu = degrees of freedom");
      DBL_ARG(nu) ;
      OUTPUT(gsl_ran_tdist (r, nu));
    }
  else if (NAME("weibull"))
    {
      ARGS(2, "a = scale parameter, b = exponent");
      DBL_ARG(a) ;
      DBL_ARG(b) ;
      OUTPUT(gsl_ran_weibull (r, a, b));
    }
  else
    {
      fprintf(stderr,"Error: unrecognized distribution: %s\n", name) ;
    }

  return 0 ;
}
コード例 #23
0
ファイル: test.c プロジェクト: CNMAT/gsl
double
test_binomial_max (void)
{
  return gsl_ran_binomial (r_global, 1e-8, 1<<31);
}