Class: Statfeed
- Inherits:
-
Object
- Object
- Statfeed
- Defined in:
- lib/statfeed.rb
Constant Summary collapse
- VERSION =
"1.1.0"
Instance Attribute Summary collapse
-
#accents ⇒ Object
Returns the value of attribute accents.
-
#choices ⇒ Object
Returns the value of attribute choices.
-
#decisions ⇒ Object
Returns the value of attribute decisions.
-
#heterogeneities ⇒ Object
Returns the value of attribute heterogeneities.
-
#options ⇒ Object
Returns the value of attribute options.
-
#randoms ⇒ Object
Returns the value of attribute randoms.
-
#size ⇒ Object
Returns the value of attribute size.
-
#statistics ⇒ Object
Returns the value of attribute statistics.
-
#weights ⇒ Object
Returns the value of attribute weights.
Instance Method Summary collapse
-
#acceptable?(o) ⇒ Boolean
Default is “everything’s okay”.
- #expected_increment(decision, option) ⇒ Object
- #increment_statistics(decision, option) ⇒ Object
-
#initialize(decisions, options, heterogeneity: 0.1, accent: 1.0) ⇒ Statfeed
constructor
A new instance of Statfeed.
- #normalization_value(decision) ⇒ Object
- #normalize_statistics(decision) ⇒ Object
- #normalize_statistics!(decision) ⇒ Object
- #populate_choices ⇒ Object
- #populate_randoms ⇒ Object
- #populate_weights ⇒ Object
- #reset_statistics ⇒ Object
- #sample ⇒ Object
- #scheduling_values(decision) ⇒ Object
-
#sort_option_indices(vals) ⇒ Object
Feed the scheduling values into this (step 2).
-
#sort_options(vals) ⇒ Object
Feed the scheduling values into this (step 2).
- #true_increment(decision, option) ⇒ Object
Constructor Details
#initialize(decisions, options, heterogeneity: 0.1, accent: 1.0) ⇒ Statfeed
Returns a new instance of Statfeed.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/statfeed.rb', line 5 def initialize decisions, , heterogeneity: 0.1, accent: 1.0 @decisions = decisions @options = # This will eventually be the result of applying statistical feedback @choices = Array.new(@decisions.size, nil) # Vector of heterogeneity values for each decision @heterogeneities = Array.new(decisions.size, heterogeneity) # Vector of accent values for each decision @accents = Array.new(decisions.size, accent) # Vector of statistic for each option reset_statistics # Fill matrix of random values populate_randoms # Fill matrix of weights populate_weights end |
Instance Attribute Details
#accents ⇒ Object
Returns the value of attribute accents.
3 4 5 |
# File 'lib/statfeed.rb', line 3 def accents @accents end |
#choices ⇒ Object
Returns the value of attribute choices.
3 4 5 |
# File 'lib/statfeed.rb', line 3 def choices @choices end |
#decisions ⇒ Object
Returns the value of attribute decisions.
3 4 5 |
# File 'lib/statfeed.rb', line 3 def decisions @decisions end |
#heterogeneities ⇒ Object
Returns the value of attribute heterogeneities.
3 4 5 |
# File 'lib/statfeed.rb', line 3 def heterogeneities @heterogeneities end |
#options ⇒ Object
Returns the value of attribute options.
3 4 5 |
# File 'lib/statfeed.rb', line 3 def @options end |
#randoms ⇒ Object
Returns the value of attribute randoms.
3 4 5 |
# File 'lib/statfeed.rb', line 3 def randoms @randoms end |
#size ⇒ Object
Returns the value of attribute size.
3 4 5 |
# File 'lib/statfeed.rb', line 3 def size @size end |
#statistics ⇒ Object
Returns the value of attribute statistics.
3 4 5 |
# File 'lib/statfeed.rb', line 3 def statistics @statistics end |
#weights ⇒ Object
Returns the value of attribute weights.
3 4 5 |
# File 'lib/statfeed.rb', line 3 def weights @weights end |
Instance Method Details
#acceptable?(o) ⇒ Boolean
Default is “everything’s okay”
42 43 44 |
# File 'lib/statfeed.rb', line 42 def acceptable? o true end |
#expected_increment(decision, option) ⇒ Object
62 63 64 |
# File 'lib/statfeed.rb', line 62 def expected_increment decision, option (@accents[decision] + (@heterogeneities[decision] * @randoms[decision][option])).to_f / @weights[decision][option] end |
#increment_statistics(decision, option) ⇒ Object
90 91 92 |
# File 'lib/statfeed.rb', line 90 def increment_statistics decision, option @statistics[option] += true_increment(decision, option) end |
#normalization_value(decision) ⇒ Object
70 71 72 |
# File 'lib/statfeed.rb', line 70 def normalization_value decision @accents[decision].to_f / @weights[decision].inject(0.0, :+) end |
#normalize_statistics(decision) ⇒ Object
94 95 96 97 98 99 100 101 102 |
# File 'lib/statfeed.rb', line 94 def normalize_statistics decision @statistics.each_with_index.map do |statistic, m| if @weights[decision][m] > 0.0 statistic - normalization_value(decision) else statistic end end end |
#normalize_statistics!(decision) ⇒ Object
104 105 106 |
# File 'lib/statfeed.rb', line 104 def normalize_statistics! decision @statistics = normalize_statistics decision end |
#populate_choices ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/statfeed.rb', line 29 def populate_choices @choices.tap do |choices| @decisions.each_index do |decision| = (scheduling_values(decision)) index = .find_index {|o| acceptable? o} choices[decision] = [index] increment_statistics decision, index normalize_statistics! decision end end end |
#populate_randoms ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/statfeed.rb', line 46 def populate_randoms @randoms = Array.new @decisions.size do Array.new @options.size do rand end end end |
#populate_weights ⇒ Object
54 55 56 57 58 59 60 |
# File 'lib/statfeed.rb', line 54 def populate_weights @weights = Array.new @decisions.size do Array.new @options.size do 1.0 / @options.size end end end |
#reset_statistics ⇒ Object
25 26 27 |
# File 'lib/statfeed.rb', line 25 def reset_statistics @statistics = Array.new(.size, 0.0) end |
#sample ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/statfeed.rb', line 108 def sample # Accumulator value acc = 0.0 # Map the cumulative weights cdf = @weights.map { |w| acc += w } # Get a random number between 0.0 and the last value r = rand(cdf.last) # Gets the first number that's less than the random number index = cdf.find_index { |c| c > r } @weights = @weights.map { |w| # Increment the full thing by a total of 1.0 w += (@weights[index].to_f / (@weights.size-1.0)) } @weights[index] = 0 if block_given? yield index else index end end |
#scheduling_values(decision) ⇒ Object
74 75 76 77 78 |
# File 'lib/statfeed.rb', line 74 def scheduling_values decision (0...@options.size).map do |m| @statistics[m] + expected_increment(decision, m) end end |
#sort_option_indices(vals) ⇒ Object
Feed the scheduling values into this (step 2)
86 87 88 |
# File 'lib/statfeed.rb', line 86 def sort_option_indices vals (0...@options.size).to_a.zip(vals).sort_by(&:last).map(&:first) end |
#sort_options(vals) ⇒ Object
Feed the scheduling values into this (step 2)
81 82 83 |
# File 'lib/statfeed.rb', line 81 def vals @options.zip(vals).sort_by(&:last).map(&:first) end |
#true_increment(decision, option) ⇒ Object
66 67 68 |
# File 'lib/statfeed.rb', line 66 def true_increment decision, option @accents[decision].to_f / @weights[decision][option] end |