/* DISCRETE */ CAMLprim value ml_gsl_ran_discrete_preproc(value p) { gsl_ran_discrete_t *G; value r; G = gsl_ran_discrete_preproc(Double_array_length(p), Double_array_val(p)); Abstract_ptr(r, G); return r; }
double test_discrete1 (void) { static double P[3]={0.59, 0.4, 0.01}; if (g1==NULL) { g1 = gsl_ran_discrete_preproc(3,P); } return gsl_ran_discrete(r_global,g1); }
double test_discrete2 (void) { static double P[10]={ 1, 9, 3, 4, 5, 8, 6, 7, 2, 0 }; if (g2==NULL) { g2 = gsl_ran_discrete_preproc(10,P); } return gsl_ran_discrete(r_global,g2); }
qtrait_mloc_rules(const qtrait_mloc_rules &rhs) : wbar(rhs.wbar), sigE(rhs.sigE), optimum(rhs.optimum), VS(rhs.VS), fitnesses(rhs.fitnesses), lookup( KTfwd::fwdpp_internal::gsl_ran_discrete_t_ptr(nullptr)) { if (!fitnesses.empty()) lookup = KTfwd::fwdpp_internal::gsl_ran_discrete_t_ptr( gsl_ran_discrete_preproc(fitnesses.size(), &fitnesses[0])); }
double test_discrete3 (void) { static double P[20]; if (g3 == NULL) { int i; for (i=0; i<20; ++i) P[i]=1.0/20; g3 = gsl_ran_discrete_preproc (20, P); } return gsl_ran_discrete (r_global, g3); }
void rdist_nonparametric(t_rdist *x, t_symbol *msg, short argc, t_atom *argv){ double f[argc]; int i; x->r_dist = msg; for(i = 0; i < argc; i++){ f[i] = librdist_atom_getfloat(argv + i); //post("%d, %f", i, f[i]); } if(x->r_g){ gsl_ran_discrete_free(x->r_g); } x->r_g = gsl_ran_discrete_preproc(argc, f); x->r_function = librdist_nonparametric; }
virtual void w(const dipvector_t &diploids, gcont_t &gametes, const mcont_t &mutations) { auto N_curr = diploids.size(); if (fitnesses.size() < N_curr) fitnesses.resize(N_curr); wbar = 0.; for (size_t i = 0; i < N_curr; ++i) { gametes[diploids[i].first].n = gametes[diploids[i].second].n = 0; fitnesses[i] = diploids[i].w; wbar += diploids[i].w; } wbar /= double(N_curr); lookup = KTfwd::fwdpp_internal::gsl_ran_discrete_t_ptr( gsl_ran_discrete_preproc(N_curr, &fitnesses[0])); }
// Create a discrete pdf pf_pdf_discrete_t *pf_pdf_discrete_alloc(int count, double *probs) { pf_pdf_discrete_t *pdf; pdf = calloc(1, sizeof(pf_pdf_discrete_t)); pdf->prob_count = count; pdf->probs = malloc(count * sizeof(double)); memcpy(pdf->probs, probs, count * sizeof(double)); // Initialize the random number generator pdf->rng = gsl_rng_alloc(gsl_rng_taus); gsl_rng_set(pdf->rng, ++pf_pdf_seed); // Initialize the discrete distribution generator pdf->ran = gsl_ran_discrete_preproc(count, probs); return pdf; }
void w(const dipcont_t &diploids, gcont_t &gametes, const mcont_t &mutations) const { unsigned N_curr = diploids.size(); if (fitnesses.size() < N_curr) fitnesses.resize(N_curr); wbar = 0.; for (unsigned i = 0; i < N_curr; ++i) { for (auto region : diploids[i]) { gametes[region.first].n = gametes[region.second].n = 0; } // the g/e/w fields will be populated via update() fitnesses[i] = diploids[i][0].w; wbar += fitnesses[i]; } wbar /= double(diploids.size()); /*! Black magic alert: fwdpp_internal::gsl_ran_discrete_t_ptr contains a std::unique_ptr wrapping the GSL pointer. This type has its own deleter, which is convenient, because operator= for unique_ptrs automagically calls the deleter before assignment! Details: http://www.cplusplus.com/reference/memory/unique_ptr/operator= This only works b/c the rhs of the expression below may be treated as an rvalue reference. */ lookup = KTfwd::fwdpp_internal::gsl_ran_discrete_t_ptr( gsl_ran_discrete_preproc(N_curr, &fitnesses[0])); }
std::vector<intType> ran_mult_multinomial(const std::vector<uint64_t>& pop, intType sample_size, const gsl_rng* r, intType min_coverage = 0) { std::vector<intType> sample; sample.resize(bins); // 1.) first generate N_reads samples, not all of which will satisfy >= min_coverage std::vector<double> p_PCR(pop.begin(), pop.end()); gsl_ran_multinomial(r, bins, sample_size, p_PCR.data(), sample.data()); uint64_t valid_samples = 0; for (const auto& i : sample) { if (i >= min_coverage) { valid_samples += i; } } // 2.) proceed to add remaining samples if (valid_samples < sample_size) { gsl_ran_discrete_t* categorical_distrib = gsl_ran_discrete_preproc(bins, p_PCR.data()); intType ind; while (valid_samples < sample_size) { ind = gsl_ran_discrete(r, categorical_distrib); ++sample[ind]; if (sample[ind] > min_coverage) { ++valid_samples; } else { if (sample[ind] == min_coverage) valid_samples += min_coverage; } } gsl_ran_discrete_free(categorical_distrib); } return sample; }
void GenomicElementType::InitializeDraws(void) { size_t mutation_type_count = mutation_type_ptrs_.size(); if (mutation_type_count != mutation_fractions_.size()) EIDOS_TERMINATION << "ERROR (GenomicElementType::InitializeDraws): mutation types and fractions have different sizes." << EidosTerminate(); if (lookup_mutation_type_) { gsl_ran_discrete_free(lookup_mutation_type_); lookup_mutation_type_ = nullptr; } // We allow an empty mutation type vector initially, because people might want to add mutation types in script. // However, if DrawMutationType() is called and our vector is still empty, that will be an error. if (mutation_type_count) { // Prepare to randomly draw mutation types std::vector<double> A(mutation_type_count); bool nonzero_seen = false; for (unsigned int i = 0; i < mutation_type_count; i++) { double fraction = mutation_fractions_[i]; if (fraction > 0.0) nonzero_seen = true; A[i] = fraction; } // A mutation type vector with all zero proportions is treated the same as an empty vector: we allow it // on the assumption that it will be fixed later, but if it isn't, that will be an error. if (nonzero_seen) lookup_mutation_type_ = gsl_ran_discrete_preproc(mutation_type_count, A.data()); } }
// Resample void pmap_resample(pmap_t *self, int scan_count) { int i, n; double e, p, norm=0.0; gsl_ran_discrete_t *dist=NULL; pmap_sample_t *oldset=NULL, *newset=NULL; pmap_sample_t *old=NULL, *newsample=NULL; double *sample_probs; sample_probs = new double[self->samples_len]; // Work out old and new sample sets oldset = self->samples + self->sample_set * self->samples_len; newset = self->samples + ((self->sample_set + 1) % 2) * self->samples_len; // Convert to probability for (i = 0; i < self->samples_len; i++) { old = oldset + i; e = old->w / self->num_ranges / scan_count; p = exp(-e / (self->resample_s * self->resample_s)); norm += p; sample_probs[i] = p; } // Normalize probabiities for (i = 0; i < self->samples_len; i++) { sample_probs[i] /= norm; #ifdef __QNXNTO__ assert(std::finite(sample_probs[i])); #else assert(finite(sample_probs[i])); #endif } dist = gsl_ran_discrete_preproc(self->samples_len, sample_probs); assert(dist); // Create discrete distribution for (i = 0; i < self->samples_len; i++) { assert(self); assert(self->rng); n = gsl_ran_discrete(self->rng, dist); old = oldset + n; newsample = newset + i; newsample->w = 0.0; newsample->err = old->err; newsample->pose = old->pose; memcpy(newsample->cells, old->cells, self->grid_size); memcpy(newsample->poses, old->poses, self->traj_size); } gsl_ran_discrete_free(dist); delete [] sample_probs; self->sample_set = (self->sample_set + 1) % 2; return; }
DiscreteSelection( gsl_rng * r, double * fitnesses, size_t s ) : m_rng( r ), m_lookup( NULL ) { m_lookup = gsl_ran_discrete_preproc( s, fitnesses ); }
int main(int argc, char *argv[]) { /*no_mc == #MC ODEs; tstep = time step */ int i, j, no_mc = 27; double tstep=1; int par_index, sim_index; gsl_matrix *pars_mat; int prior = 0; if(prior==1) { pars_mat = readMatrix("../data/prior.csv"); } else { pars_mat = readMatrix("../data/post.csv"); } gsl_matrix *sim_mat = readMatrix("../data/sim.csv"); double *par_wts, *sim_wts; par_wts = calloc(sizeof(double), pars_mat->size1); sim_wts = calloc(sizeof(double), sim_mat->size1); double *sps, *pars, *sps_gil; sps = malloc(no_mc*sizeof(double)); sps_gil = malloc(6*sizeof(double)); /*Add in (fixed) k1 and k2 to pars*/ pars = malloc((pars_mat->size2+1)*sizeof(double)); /*total g & i*/ pars[pars_mat->size2-1] = 10; pars[pars_mat->size2] = 2; gsl_ran_discrete_t *sim_sample, *par_sample; gsl_rng *r = gsl_rng_alloc(gsl_rng_mt19937); gsl_rng_set (r, 1); par_sample = gsl_ran_discrete_preproc(pars_mat->size1, (const double *) par_wts); sim_sample = gsl_ran_discrete_preproc(sim_mat->size1, (const double *) sim_wts); /* 1. Select parameters from post (select row) 2. Select time point of interest (select column) 3. Simulate from MC and Gillespie one time unit 4. Compare 5. Profit */ for(i=0; i<10000; i++) { if(prior == 1) { par_index = i; } else { par_index = gsl_ran_discrete(r, par_sample); } sim_index = gsl_ran_discrete(r, sim_sample); /*Reset MC */ for(j=0; j<no_mc;j++) { sps[j] = 0; } /* Sps order: rg, ri, g, i, G, I column zero iteration number */ sps[0] = MGET(sim_mat, sim_index, 1); sps[2] = MGET(sim_mat, sim_index, 2); sps[5] = MGET(sim_mat, sim_index, 3); sps[9] = MGET(sim_mat, sim_index, 4); sps[14] = MGET(sim_mat, sim_index, 5); sps[20] = MGET(sim_mat, sim_index, 6); for(j=0; j<(pars_mat->size2-1); j++) { pars[j] = MGET(pars_mat, par_index, j+1); } sps_gil[0] = sps[0]; sps_gil[1] = sps[2]; sps_gil[2] = sps[5]; sps_gil[3] = sps[9]; sps_gil[4] = sps[14]; sps_gil[5] = sps[20]; /* simulate from MC */ ode(pars, tstep, sps); gillespie(sps_gil, pars, tstep, r); printf("%f, %f, %f, %f, %f, %f, %d, %d\n", (sps[0] - sps_gil[0])/(pow(sps[1], 0.5)), (sps[2] - sps_gil[1])/(pow(sps[4], 0.5)), (sps[5] - sps_gil[2])/(pow(sps[8], 0.5)), (sps[9] - sps_gil[3])/(pow(sps[13], 0.5)), (sps[14] - sps_gil[4])/(pow(sps[19], 0.5)), (sps[20] - sps_gil[5])/(pow(sps[26], 0.5)), par_index, sim_index ); } return(EXIT_SUCCESS); }
void test(int max, int size) { std::vector<double> a; std::vector<int> b; double d[size]; for(int i = 0; i<size; i++) { a.push_back(1.0/size); b.push_back(1); d[i] = rand();; } //s += std::to_string(max); //s += "\t"; s += std::to_string(size); s += "\t"; int sum = 0; std::cout << std::endl << "Max: " << max << ";\t Size: " << size << std::endl << std::endl; boost::mt19937 gen; const gsl_rng_type * T; gsl_rng * r; gsl_rng_env_setup(); T = gsl_rng_default; r = gsl_rng_alloc(T); RandomLib::Random r2; sum = 0; clock_t startTime = clock(); //RandomLib sum = 0; startTime = clock(); RandomLib::RandomSelect<int> sel(b.begin(), b.end()); s += std::to_string(clock() - startTime); s += "\t"; std::cout << "\t RandomLib: " << std::endl << "\t\t Initiate: " << clock() - startTime << std::endl; startTime = clock(); for(int i = 0; i < max; i++) { sum += sel(r2); } s += std::to_string(clock() - startTime); s += "\t"; std::cout << "\t\t Generate: " << clock() - startTime << std::endl << "\t\t Sum: " << sum << std::endl; //gnu science sum = 0; startTime = clock(); gsl_ran_discrete_t * table = gsl_ran_discrete_preproc(size, d); s += std::to_string(clock() - startTime); s += "\t"; std::cout << "\t Gnu Science: " << std::endl << "\t\t Initiate: " << clock() - startTime << std::endl; startTime = clock(); for(int i = 0; i < max; i++) { sum += gsl_ran_discrete(r, table); } s += std::to_string(clock() - startTime); s += "\t"; std::cout << "\t\t Generate: " << clock() - startTime << std::endl << "\t\t Sum: " << sum << std::endl; gsl_rng_free(r); //boost sum = 0; startTime = clock(); boost::random::discrete_distribution<> dist(a.begin(), a.end()); //boost::random::discrete_distribution<> dist(b, b+size); s += std::to_string(clock() - startTime); s += "\t"; std::cout << "\t Boost: " << std::endl << "\t\t Initiate: " << clock() - startTime << std::endl; startTime = clock(); for(int i = 0; i < max; i++) { sum += dist(gen); } s += std::to_string(clock() - startTime); s += "\t"; std::cout << "\t\t Generate: " << clock() - startTime << std::endl << "\t\t Sum: " << sum << std::endl; //std::dsicrete_distribution sum = 0; startTime = clock(); std::discrete_distribution<int> std_dist(b.begin(), b.end()); s += std::to_string(clock() - startTime); s += "\t"; std::cout << "\t std::discrete_distribution: " << std::endl << "\t\t Initiate: " << clock() - startTime << std::endl; startTime = clock(); for(int i = 0; i < max; i++) { sum += std_dist(gen); } s += std::to_string(clock() - startTime); s += "\t"; std::cout << "\t\t Generate: " << clock() - startTime << std::endl << "\t\t Sum: " << sum << std::endl; /* //table-lookup sum = 0; startTime = clock(); random_distribution q(a); s += std::to_string(clock() - startTime); s += "\t"; std::cout << "\t Method 1: " << std::endl << "\t\t Initiate: " << clock() - startTime << std::endl; startTime = clock(); for(int i = 0; i < max; i++) { sum += q.dist(); } s += std::to_string(clock() - startTime); s += "\t"; std::cout << "\t\t Generate: " << clock() - startTime << std::endl << "\t\t Sum: " << sum << std::endl; //std::cout << "\t\t i0: " << q.i0 << "\t i1: " << q.i1 << "\t i2: " << q.i2 << "\t i3: " << q.i3 << std::endl; //table-lookup + histogram sum = 0; startTime = clock(); random_distribution q2(a,2); s += std::to_string(clock() - startTime); s += "\t"; std::cout << "\t Method 2: " << std::endl << "\t\t Initiate: " << clock() - startTime << std::endl; startTime = clock(); for(int i = 0; i < max; i++) { sum += q2.dist2(); } s += std::to_string(clock() - startTime); s += "\t"; std::cout << "\t\t Generate: " << clock() - startTime << std::endl << "\t\t Sum: " << sum << std::endl; //binary search sum = 0; startTime = clock(); random_distribution q3(a,3); s += std::to_string(clock() - startTime); s += "\t"; std::cout << "\t Method 3: " << std::endl << "\t\t Initiate: " << clock() - startTime << std::endl; startTime = clock(); for(int i = 0; i < max; i++) { sum += q3.dist3(); } s += std::to_string(clock() - startTime); s += "\t"; std::cout << "\t\t Generate: " << clock() - startTime << std::endl << "\t\t Sum: " << sum << std::endl; */ s += "\n"; }
int main() { gsl_rng * r; const gsl_rng_type * T; gsl_rng_env_setup(); T = gsl_rng_default; r = gsl_rng_alloc (T); // printf ("generator type: %s\n", gsl_rng_name (r)); // printf ("seed = %lu\n", gsl_rng_default_seed); // printf ("first value = %lu\n", gsl_rng_get (r)); // Step 1 int k = 3; // k is the # of communities const double alpha = double (1.0) / k; // printf ("k = %d and alpha = %f\n", k, alpha); double eta = 1; // eta is the hidden variable of the Beta distribution double comm_str [k]; for (uint32_t i = 0; i < k; ++i) comm_str[i] = gsl_ran_beta(r, eta, eta); // Step 2 int n = 10; // Number of nodes double theta = 1; // Used for gsl_ran_dirichlet double alpha_array [k]; // Array for gsl_ran_dirichlet for (uint32_t i = 0; i < k; ++i) alpha_array[i] = alpha; double pi [n][k]; // Array to store pi // Populate pi for (uint32_t i = 0; i < n; ++i) gsl_ran_dirichlet(r, k, alpha_array, pi[i]); // // Convert to binary // for (uint32_t i = 0; i < n; ++i){ // for (uint32_t j = 0; j < k ; ++j){ // if (pi[i][j] > 0.5){ // pi[i][j] = 1; // } // else{ // pi[i][j] = 0; // } // } // } // for (uint32_t i = 0; i < n; ++i){ // for (uint32_t j = 0; j < k ; ++j){ // printf("%d, %d, %f\n", i, j ,pi[i][j]); // } // } // Step 4 double mean = 0.0; // Set mean double var = 1; // Set var double prob = 0.5; // Set probability for Bernoulli // Save input values std::ofstream inputs ("inputs.txt"); if (inputs.is_open()){ inputs << "k = " << k << "\n"; inputs << "alpha = " << alpha << "\n"; inputs << "eta = " << eta << "\n"; inputs << "mean = " << mean << "\n"; inputs << "var = " << var << "\n"; inputs << "prob = " << prob << "\n"; inputs << "comm_str = "; for (uint32_t i = 0; i < k; ++i) inputs << comm_str[i] << " "; inputs << "\n" << "n = " << n << "\n" << "pi = " << "\n"; for (uint32_t i = 0; i < n; ++i){ for (uint32_t j = 0; j < k ; ++j){ inputs << pi[i][j] << "\t"; } inputs << "\n"; } inputs.close(); } // Save attribute matrix std::ofstream attributes ("attributes.txt"); if (attributes.is_open()){ for (uint32_t i = 0; i < n; ++i){ attributes << gsl_ran_gaussian(r, var) << "\t"; attributes << gsl_ran_gaussian(r, var) << "\t"; attributes << gsl_ran_bernoulli(r, prob) << "\t"; attributes << gsl_ran_bernoulli(r, prob) << "\t"; attributes << "\n"; } attributes.close(); } // Step 3 int num_edge = 20; // Define number of edges double epsilon = 1e-30; int adj_matrix [n][n]; // Populate adjancency matrix with 0s for (uint32_t i = 0; i < n; ++i) for (uint32_t j = 0; j < n ; ++j) adj_matrix[i][j] = 0; int count = 0; while (count < num_edge){ // printf("count %d\n", count); int a = gsl_rng_uniform_int(r, n); int b = gsl_rng_uniform_int(r, n); // printf("%d, %d\n", a, b); if (a == b){ continue; } double a_probs [k]; double b_probs [k]; for (uint32_t i = 0; i < k; ++i){ a_probs[i] = pi[a][i]; b_probs[i] = pi[b][i]; } int a_val = gsl_ran_discrete(r, gsl_ran_discrete_preproc(k, a_probs)); int b_val = gsl_ran_discrete(r, gsl_ran_discrete_preproc(k, b_probs)); if (a_val == b_val){ // printf("%d, %d\n", a_val, b_val); int x = gsl_ran_bernoulli(r, comm_str[a_val]); if (x == 1){ // printf("x = 1\n"); if (adj_matrix[a][b] == 0){ adj_matrix[a][b] = 1; ++count; } } } else{ int x = gsl_ran_bernoulli(r, epsilon); if (x == 1){ // printf("x = 1\n"); if (adj_matrix[a][b] == 0){ adj_matrix[a][b] = 1; ++count; } } } // for (uint32_t j = 0; j < k ; ++j){ // if (std::pi[a][j] == pi[b][j]){ // int x = gsl_ran_bernoulli(r, comm_str[j]); // // printf("Match 1 %d\n", x); // if (x == 1){ // if (adj_matrix[a][b]) // continue; // else // adj_matrix[a][b] = 1; // run_eps = 0; // ++count; // continue; // } // } // if (run_eps == 1){ // int x = gsl_ran_bernoulli(r, epsilon); // // printf("0 %d\n", x); // if (x == 1){ // if (adj_matrix[a][b]) // continue; // else // adj_matrix[a][b] = 1; // ++count; // continue; // } // } // } } std::ofstream matrix ("matrix.txt"); if (matrix.is_open()){ for (uint32_t i = 0; i < n; ++i){ for (uint32_t j = 0; j < n ; ++j){ if (adj_matrix [i][j]) matrix << i << "\t" << j << '\n'; } } matrix.close(); } }