Class: Spark::Sampler::Poisson

Inherits:
Base
  • Object
show all
Defined in:
lib/spark/sampler.rb

Overview

Poisson Sampler


Instance Attribute Summary

Attributes inherited from Base

#fraction, #seed

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Spark::Sampler::Base

Instance Method Details

#lazy_sample(iterator) ⇒ Object



57
58
59
60
61
62
# File 'lib/spark/sampler.rb', line 57

def lazy_sample(iterator)
  Enumerator::Lazy.new(iterator) do |yielder, value|
    count = rng.rand
    count.times { yielder << value }
  end
end

#rngObject



64
65
66
# File 'lib/spark/sampler.rb', line 64

def rng
  @rng ||= Spark::RandomGenerator::Poisson.new(fraction, seed)
end

#sample(iterator) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/spark/sampler.rb', line 47

def sample(iterator)
  iterator.map! do |item|
    count = rng.rand
    Array.new(count) { item }
  end
  iterator.flatten!
  iterator.compact!
  iterator
end