Permalink
Browse files

fix up whiplash math again

  • Loading branch information...
1 parent 3c130ca commit 9357cdbb3fef092774cd55f6f3da99497b90f46f @aaronsw aaronsw committed May 30, 2012
Showing with 10 additions and 4 deletions.
  1. +10 −4 lib/whiplash.rb
View
@@ -11,11 +11,17 @@ def to_1if0
module Bandit
def arm_guess(observations, victories)
+ nonvictories = [0, (observations - victories)].max
+ df = observations-1
+ df = 1 if df <= 0
+ df = df.to_f
mean = victories.to_f/observations.to_f.to_1if0
- stddev = victories * ((1-mean)**2)
- stddev += [0, (observations - victories)].max * ((0-mean)**2)
- stddev = Math.sqrt(stddev * (1.0/(([0.0, observations.to_f-1].max).to_1if0)))
- stddev = stddev.to_1if0/Math.sqrt(observations.to_f.to_1if0)
+
+ stddev = 0
+ stddev += victories * ((1-mean)**2)
+ stddev += nonvictories * ((0-mean)**2)
+ stddev = 1.0/observations if stddev == 0
+ stddev = Math.sqrt(stddev/df)
out = [0, Distribution::Normal.rng(mean, stddev).call].max
return out + (FAIRNESS_CONSTANT * (1.0/observations.to_f.to_1if0))
end

0 comments on commit 9357cdb

Please sign in to comment.