Exemplo n.º 1
0
int GSLRNG_negative_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_negative_binomial(r,p,n);
    return EC_OK;
}
Exemplo n.º 2
0
inline
void CNPLCM_CR_Basic_Freq::sam_n0(){
	//GSL: "This function returns a random integer from the negative binomial distribution, 
	// the number of failures occurring before n successes in independent trials with probability 
	// p of success. The probability distribution for negative binomial variates is,
	// p(k) = {\Gamma(n + k) \over \Gamma(k+1) \Gamma(n) } p^n (1-p)^k NOTE THE +1 IN GAMMA(K+1)!!!!
	par->n0 = gsl_ran_negative_binomial(r, 1.0 - par->prob_zero, data->n); //<-=-this one is the correct.
	//This corresponds to an (improper) prior P(Nmis|n) \propto n/(Nmis+n). This ensures that after integrating Nmis
	// the joint probability correspond to the correct truncated distribution.
}
Exemplo n.º 3
0
void librdist_negative_binomial(gsl_rng *rng, int argc, void *argv, int bufc, float *buf){
	t_atom *av = (t_atom *)argv;
	if(argc != librdist_getnargs(ps_negative_binomial)){
		return;
	}
	const double p = librdist_atom_getfloat(av);
	const double n = librdist_atom_getfloat(av + 1);
	int i;
	for(i = 0; i < bufc; i++)
	       	buf[i] = (float)gsl_ran_negative_binomial(rng, p, n);
}
Exemplo n.º 4
0
unsigned int
gsl_ran_pascal (const gsl_rng * r, double p, unsigned int n)
{
  /* This is a separate interface for the pascal distribution so that
     it can be optimized differently from the negative binomial in
     future.
     
     e.g. if n < 10 it might be faster to generate the Pascal
     distributions as the sum of geometric variates directly.  */
  
  unsigned int k = gsl_ran_negative_binomial (r, p, (double) n);
  return k;
}
Exemplo n.º 5
0
rcount nulldist::rand()
{
    if( gsl_ran_bernoulli( rng, a ) ) return 0;

    /* This uses a rejection sampling scheme worked out by Charles Geyer,
     * detailed in his notes "Lower-Truncated Poisson and Negative Binomial
     * Distributions".
     */
    rcount x;
    double accp;
    while( true ) {
        x = gsl_ran_negative_binomial( rng, p, r + 1.0 ) + 1;

        accp = gsl_sf_lnfact( x - 1 ) - gsl_sf_lnfact( x );

        if( gsl_ran_bernoulli( rng, exp( accp ) ) ) break;
    }

    return x;
}
Exemplo n.º 6
0
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 ;
}
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(0)+traj->get_state(1)) < 1.0) {
        return;
    }
    double R0_0=1.0;
    double R0_1=1.0;
    double R0_2=1.0;
    double R0_3=1.0;
    double R0_4=1.0;
    double R0_5=1.0;
    double R0_T0=0.0;
    double R0_T1=100000.0;
    double R0_T2=100000.0;
    double R0_T3=100000.0;
    double R0_T4=100000.0;
    double k=1.0;
    double alpha=1.0;
    double scale=1.0;
    // /* For slightly faster implementation, call parameters by index
    for (int i=0; i!=param_names.size(); ++i) {
        if (param_names[i]=="R0_0") R0_0 = model_params[i];
        if (param_names[i]=="R0_1") R0_1 = model_params[i];
        if (param_names[i]=="R0_2") R0_2 = model_params[i];
        if (param_names[i]=="R0_3") R0_0 = model_params[i];
        if (param_names[i]=="R0_4") R0_0 = model_params[i];
        if (param_names[i]=="R0_5") R0_0 = model_params[i];
        if (param_names[i]=="R0_T0") R0_T0 = model_params[i];
        if (param_names[i]=="R0_T1") R0_T1 = model_params[i];
        if (param_names[i]=="R0_T2") R0_T2 = model_params[i];
        if (param_names[i]=="R0_T3") R0_T3 = model_params[i];
        if (param_names[i]=="R0_T4") R0_T4 = model_params[i];
        if (param_names[i]=="k") k = model_params[i];
        if (param_names[i]=="alpha") alpha = model_params[i];
        if (param_names[i]=="scale") scale = model_params[i];
    }
    double R0_now = 0.0;
    double recoveries=0.0;
    double new_infections=0.0;
    double durI = 0.0;
    double ran_unif_num1, ran_unif_num2;
    if (custom_prob.size()==0) set_custom_prob(alpha, scale);
    if (start_dt < step_size) {  // Set initial number of infected
        traj->resize_recoveries(total_dt);
        int init_inf = (int)round(model_params[14]);
        traj->set_state(init_inf, 0);
        for (int i=0; i!=init_inf; ++i) {
//            durI = gsl_ran_gamma(rng, alpha, scale);
            ran_unif_num1 = gsl_ran_flat(rng, 0.0000001, 1);
            ran_unif_num2 = gsl_ran_flat(rng, 0.0000001, 1);
            durI = -log(ran_unif_num1)*scale-log(ran_unif_num2)*scale;
            durI = (int)(durI/365.0/step_size);
            traj->add_recovery_time(durI);
        }
    }
    double num_infected = traj->get_state(0);
    for (int t=start_dt; t<end_dt; ++t) {
        //
        // Transitions
        //
        // Recoveries: I --> R
        recoveries = traj->num_recover_at(t-start_dt);
        if (recoveries > 100000.0) { // If the epidemic is too large, set num_infected to 0, so that likelihood is 0.
            num_infected = 0.0;
            traj->set_traj(0.0, t-start_dt);
        }
        else if (recoveries > 0) {
            traj->set_traj(recoveries, t-start_dt);
            if (t*step_size<(R0_T0)) R0_now = R0_0;
            else if (t*step_size<(R0_T1)) R0_now = R0_1;
            else if (t*step_size<(R0_T2)) R0_now = R0_2;
            else if (t*step_size<(R0_T3)) R0_now = R0_3;
            else if (t*step_size<(R0_T4)) R0_now = R0_4;
            else {
                R0_now = R0_5;
            }
            new_infections = gsl_ran_negative_binomial(rng, k/(k+R0_now), k*recoveries);
            if (new_infections > 1000) {
                std::vector <unsigned int> a (106, 0);
                gsl_ran_multinomial(rng, 106, (unsigned int)new_infections, &custom_prob[0], &a[0]);
                for (int i=0; i!=a.size(); ++i) {
//                    durI = gsl_ran_gamma(rng, alpha, scale);

//                    ran_unif_num1 = gsl_rng_uniform_pos(rng);
//                    ran_unif_num2 = gsl_rng_uniform_pos(rng);
//                    durI = -log(ran_unif_num1)*scale-log(ran_unif_num2)*scale;
//                    durI = (int)(durI/365.0/step_size);
                    
//                    durI = durI_vec[gsl_rng_uniform_int(rng, 1000)];
//                    traj->add_recovery_time(durI+t-start_dt);
                    traj->add_recovery_time(i+t-start_dt, a[i]);
                }
            }
            else if (new_infections > 0) {
                for (int i=0; i!=new_infections; ++i) {
                    ran_unif_num1 = gsl_rng_uniform_pos(rng);
                    ran_unif_num2 = gsl_rng_uniform_pos(rng);
                    durI = -log(ran_unif_num1)*scale-log(ran_unif_num2)*scale;
                    durI = (int)(durI/365.0/step_size);
                    traj->add_recovery_time(durI+t-start_dt);
                }
            }
            num_infected += new_infections - recoveries;
        }
        double curr_coal_rate = 1.0/num_infected;
        // Record 1/N for coalescent rate calculation
        if (num_infected > 0.0) {
            traj->set_traj2(curr_coal_rate*R0_now/(alpha*scale)*(1.0+1.0/k), t-start_dt);
        }
        else {
            traj->set_traj2(0.0, t-start_dt);
            break;
        }
    }
    traj->set_state(num_infected, 0);
    traj->delete_recoveries_before(end_dt-start_dt);
}
Exemplo n.º 8
0
double
test_negative_binomial (void)
{
  return gsl_ran_negative_binomial (r_global, 0.3, 20.0);
}
Exemplo n.º 9
0
void SimulEpidNegBin(double VarDivMean, parameters *param, gsl_matrix *Incid, gsl_vector *Incid0)
{
	// VarDivMean = Variance/Mean of the negative binomial considered
	int k, t, g, i, s;
	int tMin, tMax;
	double lambda;
	double prov;
	int poiss;
	
	for (k=0 ; k<param->NbGroups ; k++)
	{
		gsl_matrix_set(Incid,k,0,gsl_vector_get(Incid0,k));	
	}

	for (i=0 ; i<param->p+1 ; i++)
	{
		if(i==0)
		{
			tMin=1;
		}else
		{
			tMin=gsl_vector_get(param->tau,i-1);
		}
		if(i==param->p)
		{
			tMax=param->T;
		}else
		{
			tMax=gsl_vector_get(param->tau,i);
		}
		for (t=tMin ; t<tMax ; t++)
		{
			//printf("t %d\n",t);
			//fflush(stdout);
			for(k=0 ; k<param->NbGroups ; k++)
			{
				//printf("k %d\n",k);
				//fflush(stdout);
				lambda=0;
				for(g=0 ; g<param->NbGroups ; g++)	
				{
					//printf("g %d\n",g);
					//fflush(stdout);
					prov=0;
					for(s=1 ; s<=GSL_MIN(t,param->S) ; s++)
					{
						
						prov+=gsl_matrix_get(Incid,g,t-s)*gsl_matrix_get(param->GTdistr,g,s-1);
						//printf("s %d %lg\n",s,prov);
						//fflush(stdout);
					}
					prov*=gsl_matrix_get(param->K[i],g,k);
					//printf("prov %lg\n",prov);
					//fflush(stdout);
        			lambda+=prov;
        			//printf("lambda %lg\n",lambda);
					//fflush(stdout);
        			
				}
				poiss=gsl_ran_negative_binomial(rng,1/VarDivMean,lambda/(VarDivMean-1));
				//printf("lambda %lg inci %d\n",lambda,poiss);
				//fflush(stdout);
				gsl_matrix_set(Incid,k,t,poiss);
			}
		}
			
	}
			
}
Exemplo n.º 10
0
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;
        }
    }
}
Exemplo n.º 11
0
int sock_sim(double prior_r,double prior_p,double alpha,double beta,double obs_paired,double obs_odd,double *BigVector,int iter){
int nthreads;

 //variables, gsl rng initiation
 int i,j,match_count,n_picked;
 unsigned int n_socks;
 double prop_pairs,n_pairs,n_odd,prior_n;
 double obs_total = obs_paired + obs_odd;
 prior_n = prior_r - 1;
 match_count = 0; 

 double temp_paired,temp_odd,temp_pairs;

 //setup gsl random seed
 const gsl_rng_type * T;
 gsl_rng_env_setup();

 T = gsl_rng_default;
 r = gsl_rng_alloc(T); 

#pragma omp parallel for 
 for(i=0;i<iter;i++){  

   //sample, get n_pairs and n_odd
   n_socks = gsl_ran_negative_binomial(r,prior_p,prior_n);
   prop_pairs = gsl_ran_beta(r,alpha,beta);
   n_pairs = round(floor(.5*n_socks)*prop_pairs);
   n_odd = n_socks - 2*n_pairs;

   //make generated population
   double *gen_pop = (double *)malloc(sizeof(double)*n_socks);
   for(j=0;j<n_pairs;j++){

     gen_pop[2*j] = (double) j;
     gen_pop[(2*j)+1] = (double) j;

   }
   for(j=2*n_pairs;j<n_socks;j++){

     gen_pop[j]= (double) j;

   }

   //get generated sample size
   if(obs_total <= n_socks){

     n_picked = (int) obs_total;

   }else{ n_picked = n_socks; }


   //get sample vector
   double *gen_samp = (double *)malloc(sizeof(double)*n_picked);

   //count pairs

   //sample from generated population
   gsl_ran_choose(r,gen_samp,n_picked,gen_pop,n_socks,sizeof(double));
 
   //sort sample
   gsl_sort(gen_samp,1,n_picked);

   //count the number of pairs/odd in sample
   temp_pairs = 0.;
   temp_odd = 1.;
   for(j=1;j<n_picked;j++){

     if(gen_samp[j] == gen_samp[j-1]){

       temp_pairs = temp_pairs + 1;
       temp_odd = temp_odd - 1;
       continue;

     }else{ temp_odd = temp_odd + 1; }

   }
   temp_paired = 2*temp_pairs;


   //allocate big vector
   BigVector[5*i] = (double) n_socks;
   BigVector[(5*i) + 1] = n_pairs;
   BigVector[(5*i) + 2] = n_odd;
   BigVector[(5*i) + 3] = prop_pairs;

   //counter
   if(temp_odd==obs_odd && temp_paired==obs_paired){ 

       match_count = match_count + 1;
       BigVector[(5*i) + 4] = 1.;
       continue;
 
   }
   else{
   
      BigVector[(5*i) + 4] = 0.;
      continue;
  
    }

   //free the temp allocated things
   free(gen_pop);
   free(gen_samp);
 }

 gsl_rng_free(r);
 return(match_count);

}