// Destroy the pdf void pf_pdf_discrete_free(pf_pdf_discrete_t *pdf) { gsl_ran_discrete_free(pdf->ran); gsl_rng_free(pdf->rng); free(pdf->probs); free(pdf); return; }
void rdist_free(t_rdist *x){ if(x->r_rng){ gsl_rng_free(x->r_rng); } if(x->r_g){ gsl_ran_discrete_free(x->r_g); } if(x->r_arIn){ free(x->r_arIn); } if(x->r_output_buffer){ free(x->r_output_buffer); } }
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; }
GenomicElementType::~GenomicElementType(void) { //EIDOS_ERRSTREAM << "GenomicElementType::~GenomicElementType" << std::endl; if (lookup_mutation_type_) { gsl_ran_discrete_free(lookup_mutation_type_); lookup_mutation_type_ = nullptr; } if (mm_thresholds) { free(mm_thresholds); mm_thresholds = nullptr; } }
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; }
virtual ~DiscreteSelection() { gsl_ran_discrete_free( m_lookup ); }
int main (void) { gsl_ieee_env_setup (); gsl_rng_env_setup (); r_global = gsl_rng_alloc (gsl_rng_default); #define FUNC(x) test_ ## x, "test gsl_ran_" #x #define FUNC2(x) test_ ## x, test_ ## x ## _pdf, "test gsl_ran_" #x test_shuffle (); test_choose (); testMoments (FUNC (ugaussian), 0.0, 100.0, 0.5); testMoments (FUNC (ugaussian), -1.0, 1.0, 0.6826895); testMoments (FUNC (ugaussian), 3.0, 3.5, 0.0011172689); testMoments (FUNC (ugaussian_tail), 3.0, 3.5, 0.0011172689 / 0.0013498981); testMoments (FUNC (exponential), 0.0, 1.0, 1 - exp (-0.5)); testMoments (FUNC (cauchy), 0.0, 10000.0, 0.5); testMoments (FUNC (discrete1), -0.5, 0.5, 0.59); testMoments (FUNC (discrete1), 0.5, 1.5, 0.40); testMoments (FUNC (discrete1), 1.5, 3.5, 0.01); testMoments (FUNC (discrete2), -0.5, 0.5, 1.0/45.0 ); testMoments (FUNC (discrete2), 8.5, 9.5, 0 ); testMoments (FUNC (discrete3), -0.5, 0.5, 0.05 ); testMoments (FUNC (discrete3), 0.5, 1.5, 0.05 ); testMoments (FUNC (discrete3), -0.5, 9.5, 0.5 ); test_dirichlet_moments (); test_multinomial_moments (); testPDF (FUNC2 (beta)); testPDF (FUNC2 (cauchy)); testPDF (FUNC2 (chisq)); testPDF (FUNC2 (dirichlet)); testPDF (FUNC2 (erlang)); testPDF (FUNC2 (exponential)); testPDF (FUNC2 (exppow0)); testPDF (FUNC2 (exppow1)); testPDF (FUNC2 (exppow1a)); testPDF (FUNC2 (exppow2)); testPDF (FUNC2 (exppow2a)); testPDF (FUNC2 (exppow2b)); testPDF (FUNC2 (fdist)); testPDF (FUNC2 (flat)); testPDF (FUNC2 (gamma)); testPDF (FUNC2 (gamma1)); testPDF (FUNC2 (gamma_int)); testPDF (FUNC2 (gamma_large)); testPDF (FUNC2 (gamma_small)); testPDF (FUNC2 (gamma_mt)); testPDF (FUNC2 (gamma_mt1)); testPDF (FUNC2 (gamma_mt_int)); testPDF (FUNC2 (gamma_mt_large)); testPDF (FUNC2 (gamma_mt_small)); testPDF (FUNC2 (gaussian)); testPDF (FUNC2 (gaussian_ratio_method)); testPDF (FUNC2 (gaussian_ziggurat)); testPDF (FUNC2 (ugaussian)); testPDF (FUNC2 (ugaussian_ratio_method)); testPDF (FUNC2 (gaussian_tail)); testPDF (FUNC2 (gaussian_tail1)); testPDF (FUNC2 (gaussian_tail2)); testPDF (FUNC2 (ugaussian_tail)); testPDF (FUNC2 (bivariate_gaussian1)); testPDF (FUNC2 (bivariate_gaussian2)); testPDF (FUNC2 (bivariate_gaussian3)); testPDF (FUNC2 (bivariate_gaussian4)); testPDF (FUNC2 (gumbel1)); testPDF (FUNC2 (gumbel2)); testPDF (FUNC2 (landau)); testPDF (FUNC2 (levy1)); testPDF (FUNC2 (levy2)); testPDF (FUNC2 (levy1a)); testPDF (FUNC2 (levy2a)); testPDF (FUNC2 (levy_skew1)); testPDF (FUNC2 (levy_skew2)); testPDF (FUNC2 (levy_skew1a)); testPDF (FUNC2 (levy_skew2a)); testPDF (FUNC2 (levy_skew1b)); testPDF (FUNC2 (levy_skew2b)); testPDF (FUNC2 (logistic)); testPDF (FUNC2 (lognormal)); testPDF (FUNC2 (pareto)); testPDF (FUNC2 (rayleigh)); testPDF (FUNC2 (rayleigh_tail)); testPDF (FUNC2 (tdist1)); testPDF (FUNC2 (tdist2)); testPDF (FUNC2 (laplace)); testPDF (FUNC2 (weibull)); testPDF (FUNC2 (weibull1)); testPDF (FUNC2 (dir2d)); testPDF (FUNC2 (dir2d_trig_method)); testPDF (FUNC2 (dir3dxy)); testPDF (FUNC2 (dir3dyz)); testPDF (FUNC2 (dir3dzx)); testDiscretePDF (FUNC2 (discrete1)); testDiscretePDF (FUNC2 (discrete2)); testDiscretePDF (FUNC2 (discrete3)); testDiscretePDF (FUNC2 (poisson)); testDiscretePDF (FUNC2 (poisson_large)); testDiscretePDF (FUNC2 (bernoulli)); testDiscretePDF (FUNC2 (binomial)); testDiscretePDF (FUNC2 (binomial0)); testDiscretePDF (FUNC2 (binomial1)); testDiscretePDF (FUNC2 (binomial_knuth)); testDiscretePDF (FUNC2 (binomial_large)); testDiscretePDF (FUNC2 (binomial_large_knuth)); testDiscretePDF (FUNC2 (binomial_huge)); testDiscretePDF (FUNC2 (binomial_huge_knuth)); testDiscretePDF (FUNC2 (geometric)); testDiscretePDF (FUNC2 (geometric1)); testDiscretePDF (FUNC2 (hypergeometric1)); testDiscretePDF (FUNC2 (hypergeometric2)); testDiscretePDF (FUNC2 (hypergeometric3)); testDiscretePDF (FUNC2 (hypergeometric4)); testDiscretePDF (FUNC2 (hypergeometric5)); testDiscretePDF (FUNC2 (hypergeometric6)); testDiscretePDF (FUNC2 (logarithmic)); testDiscretePDF (FUNC2 (multinomial)); testDiscretePDF (FUNC2 (multinomial_large)); testDiscretePDF (FUNC2 (negative_binomial)); testDiscretePDF (FUNC2 (pascal)); gsl_rng_free (r_global); gsl_ran_discrete_free (g1); gsl_ran_discrete_free (g2); gsl_ran_discrete_free (g3); exit (gsl_test_summary ()); }