Esempio n. 1
0
void Tree::bootstrap() {

  // Use fraction (default 63.21%) of the samples
  size_t num_samples_inbag = (size_t) num_samples * sample_fraction;

  // Reserve space, reserve a little more to be save)
  sampleIDs[0].reserve(num_samples_inbag);
  oob_sampleIDs.reserve(num_samples * (exp(-sample_fraction) + 0.1));

  std::uniform_int_distribution<size_t> unif_dist(0, num_samples - 1);

  // Start with all samples OOB
  inbag_counts.resize(num_samples, 0);

  // Draw num_samples samples with replacement (num_samples_inbag out of n) as inbag and mark as not OOB
  for (size_t s = 0; s < num_samples_inbag; ++s) {
    size_t draw = unif_dist(random_number_generator);
    sampleIDs[0].push_back(draw);
    ++inbag_counts[draw];
  }

  // Save OOB samples
  for (size_t s = 0; s < inbag_counts.size(); ++s) {
    if (inbag_counts[s] == 0) {
      oob_sampleIDs.push_back(s);
    }
  }
  num_samples_oob = oob_sampleIDs.size();

  if (!keep_inbag) {
    inbag_counts.clear();
  }
}
Esempio n. 2
0
FastDMSamplingBase::value_t FastDMSamplingInvNorm::rand(rngeng_t& rngeng)
{
    std::uniform_real_distribution<double> unif_dist;
    while (true) {
        const value_t t = randin(rngeng, invabsmu_, invmu2_);
        const value_t one2t = 0.5 / t;
        if (t < 2.5) {
            // short-time series
            if (acceptt(t, exp(-one2t) * unif_dist(rngeng), one2t)) return t;
        } else {
            // long-time series
            constexpr value_t Cl = -0.6773740579341821; // -log(pi/4)-log(2pi)/2;
            if (acceptt(t, exp(Cl - one2t - 3 / 2 * log(t)) * unif_dist(rngeng), 
                        PI * PI * t / 8)) return t;
        }
    }
}
Esempio n. 3
0
void SpikingOutput::randomize_state(){
    CImg<double>::iterator next_spk_time_it = next_spk_time->begin();

    while(next_spk_time_it < next_spk_time->end()){ // For every spiking output
        // To randomize the state of each output, we set the last next_spk_time
        // to the next firing reduced to a random percentage defined by Random_init.
        *next_spk_time_it = (1.0 - Random_init*unif_dist(rand_gen)) * *next_spk_time_it; // random number in the interval [0,1) seconds from unif. dist. multiplied by previous initial firing period
        next_spk_time_it++;
    }
}
Esempio n. 4
0
FastDMSamplingBase::value_t FastDMSamplingInvNorm::randin(rngeng_t& rngeng, 
    value_t mu, value_t mu2)
{
    std::normal_distribution<value_t> randn;
    std::uniform_real_distribution<double> unif_dist;
    const value_t z = randn(rngeng);
    const value_t y = z * z;
    const value_t x = mu + (mu2 * y - mu * sqrt((4 * mu + mu2 * y) * y)) / 2;
    return unif_dist(rngeng) <= 1 / (1 + x / mu) ? x : mu2 / x;
}
Esempio n. 5
0
FastDMSamplingBase::value_t FastDMSamplingNormExp::rand(rngeng_t& rngeng)
{
    std::uniform_real_distribution<double> unif_dist;
    while (true) {
        const value_t P = F1inf_ * unif_dist(rngeng);
        if (P <= CF1st_) {
            // short-time series
            const value_t erfcinvP = erfcinv(P / Cf1s_);
            const value_t t = 1 / (2 * a_ * erfcinvP * erfcinvP);
            if (acceptt(t, exp(- 0.5/(a_ * t) - sqrtamu_ + 
                               mu2_ * t / 2) * unif_dist(rngeng),
                        0.5 / t)) 
                return t;
        } else {
            // long-time series
            const value_t t = -log1p(- (P - CF1st_) / Cf1l_ - F1lt_) / fourmu2pi_;
            const value_t pi2t8 = PI * PI * t / 8;
            if (acceptt(t, exp(-pi2t8) * unif_dist(rngeng), pi2t8)) return t;
        }
    }
}
Esempio n. 6
0
double mostFrequentValue(std::unordered_map<double, size_t>& class_count, std::mt19937_64 random_number_generator) {
  std::vector<double> major_classes;

  // Find maximum count
  size_t max_count = 0;
  for (auto& class_value : class_count) {
    if (class_value.second > max_count) {
      max_count = class_value.second;
      major_classes.clear();
      major_classes.push_back(class_value.first);
    } else if (class_value.second == max_count) {
      major_classes.push_back(class_value.first);
    }
  }

  if (major_classes.size() == 1) {
    return major_classes[0];
  } else {
    // Choose randomly
    std::uniform_int_distribution<size_t> unif_dist(0, major_classes.size() - 1);
    return major_classes[unif_dist(random_number_generator)];
  }
}
Esempio n. 7
0
DMSample DMConstDriftConstBound::rand(rngeng_t& rngeng)
{
    compute_dm_consts();
    if (!fpt_sampler_) {
        // initialise sampler at first use
        const double smu = fabs(dm_consts_->c3());
        fpt_sampler_.reset(FastDMSamplingBase::create(smu));
    }
    // use sampler with time re-scaling for non-unit bounds
    double t = fpt_sampler_->rand(rngeng);
    std::uniform_real_distribution<double> unif_dist;
    return DMSample(t * dm_consts_->c1() / 4, 
                    unif_dist(rngeng) <= dm_consts_->c5());
}
Esempio n. 8
0
void TreeClassification::bootstrapBalanced() {
  // Find the smallest class we want to align to
  size_t size_smallest = *class_counts->begin();
  for (auto current : *class_counts) {
    size_smallest = (current < size_smallest ? current : size_smallest);
  }
  // Reserve space
  sampleIDs[0].reserve(size_smallest * class_counts->size());
  oob_sampleIDs.reserve(num_samples - size_smallest * class_counts->size());

  // Start with all samples OOB
  inbag_counts.resize(num_samples, 0);

  // Draw size_smallest samples with replacement as inbag and mark as not OOB
  size_t skip = 0;
  for (auto current_class : *class_counts) {
    for (size_t i = 0; i < size_smallest; ++i) {
      std::uniform_int_distribution<size_t> unif_dist(skip, skip + current_class - 1);
      size_t draw = unif_dist(random_number_generator);
      sampleIDs[0].push_back((*sampleIDs_by_class)[draw]);
      ++inbag_counts[(*sampleIDs_by_class)[draw]];
    }
    skip += current_class;
  }
  // Save OOB samples
  for (size_t s = 0; s < inbag_counts.size(); ++s) {
    if (inbag_counts[s] == 0) {
      oob_sampleIDs.push_back(s);
    }
  }
  num_samples_oob = oob_sampleIDs.size();

  if (!keep_inbag) {
    inbag_counts.clear();
  }
}
Esempio n. 9
0
void drawWithoutReplacementSimple(std::vector<size_t>& result, std::mt19937_64& random_number_generator, size_t max,
    std::vector<size_t>& skip, size_t num_samples) {

  result.reserve(num_samples);

  // Set all to not selected
  std::vector<bool> temp;
  temp.resize(max, false);

  std::uniform_int_distribution<size_t> unif_dist(0, max - 1 - skip.size());
  for (size_t i = 0; i < num_samples; ++i) {
    size_t draw;
    do {
      draw = unif_dist(random_number_generator);
      for (auto& skip_value : skip) {
        if (draw >= skip_value) {
          ++draw;
        }
      }
    } while (temp[draw]);
    temp[draw] = true;
    result.push_back(draw);
  }
}
Esempio n. 10
0
DMSample DMConstDriftConstABound::rand(rngeng_t& rngeng)
{
    value_t t = 0.0;
    value_t x = 0.0;
    std::uniform_real_distribution<double> unif_dist;
    while (true) {
        const double xlo = fabs(x - b_lo_);
        const double xup = fabs(x - b_up_);
        if (fabs(xlo - xup) < 1e-20) {
            // symmetric bounds, diffusion model in [x - xup, x + xup]
            const double mutheta = xup * drift_;
            FastDMSamplingBase* fpt_sampler = FastDMSamplingBase::create(mutheta);
            t += xup * xup * fpt_sampler->rand(rngeng);
            delete fpt_sampler;
            return DMSample(t, unif_dist(rngeng) < 1 / (1 + exp(-2 * mutheta)));
        } else if (xlo > xup) {
            // x closer to upper bound, diffusion model in [x - xup, x + xup]
            const double mutheta = xup * drift_;
            FastDMSamplingBase* fpt_sampler = FastDMSamplingBase::create(mutheta);
            t += xup * xup * fpt_sampler->rand(rngeng);
            delete fpt_sampler;
            if (unif_dist(rngeng) < 1 / (1 + exp(-2 * mutheta)))
                return DMSample(t, true);
            x -= xup;
        } else {
            // x closer to lower bound, diffusion model in [x - xlo, x + xlo]
            const double mutheta = xlo * drift_;
            FastDMSamplingBase* fpt_sampler = FastDMSamplingBase::create(mutheta);
            t += xlo * xlo * fpt_sampler->rand(rngeng);
            delete fpt_sampler;
            if (unif_dist(rngeng) > 1 / (1 + exp(-2 * mutheta)))
                return DMSample(t, false);
            x += xlo;
        }
    }
}