Sha256: 3c78e14bc550ab5fbdfbad277601eababfbf9323dccbd55151fb199c1898b494
Contents?: true
Size: 1.08 KB
Versions: 2
Compression:
Stored size: 1.08 KB
Contents
class RailsDataExplorer module Statistics class RngCategory def initialize(categories, category_probabilities = nil, rng = lambda { Kernel.rand }) @categories, @category_probabilities, @rng = categories, category_probabilities, rng @category_probabilities ||= @categories.map { |e| @rng.call } @category_probabilities = normalize_category_probabilities @category_order = compute_category_order end def rand r_v = @rng.call rnd = @category_order.detect { |e| e[:threshold] >= r_v } rnd[:category] end def normalize_category_probabilities total = @category_probabilities.inject(0) { |m,e| m += e } @category_probabilities.map { |e| e / total.to_f } end def compute_category_order category_order = [] running_sum = 0 @categories.each_with_index { |e, idx| running_sum += @category_probabilities[idx] category_order << { :category => e, :threshold => running_sum } } category_order end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
rails-data-explorer-0.1.0 | lib/rails-data-explorer/statistics/rng_category.rb |
rails-data-explorer-0.0.1 | lib/rails-data-explorer/statistics/rng_category.rb |