Permalink
Browse files

fixed bug with turn length counts on popular buys

  • Loading branch information...
1 parent 395a655 commit 2e2e79dab4d29a58d869474a9872830962b81925 @rrenaud rrenaud committed Jun 26, 2011
Showing with 39 additions and 25 deletions.
  1. +7 −3 count_buys.py
  2. +32 −22 stats.py
View
@@ -20,6 +20,9 @@
NO_INFO = MVS().mean_diff(MVS())
+PROV_SMOOTH = 21.4
+COLONY_SMOOTH = 23.5
+
class BuyStat(primitive_util.PrimitiveConversion, mergeable.MergeableObject):
""" A bunch of MeanVar statistics about card buys/game length, etc """
@@ -30,9 +33,10 @@ def __init__(self):
self.returns = MVS()
self.any_gained = MVS()
self.available = MVS()
- # Turn length stats are wrong, need different prior for mean var stat
- self.game_length = MVS()
- self.game_length_colony = MVS()
+
+ self.game_length = MVS(1.0, PROV_SMOOTH, PROV_SMOOTH * PROV_SMOOTH)
+ self.game_length_colony = MVS(1.0, COLONY_SMOOTH,
+ COLONY_SMOOTH * COLONY_SMOOTH)
@property
def none_gained(self):
View
@@ -14,52 +14,58 @@
import primitive_util
import mergeable
-# TODO: Make this support a variable prior, rather than the win-rate based
-# prior of 1 win, 1 loss in 2 2p games.
class MeanVarStat(primitive_util.PrimitiveConversion,
mergeable.MergeableObject):
- __slots__ = ('freq', 'sum', 'sum_sq')
+ __slots__ = ('freq', 'sum', 'sum_sq', 'pfreq', 'psum', 'psum_sq')
- def __init__(self):
- self.freq = 0
- self.sum = 0.0
- self.sum_sq = 0.0
+ def __init__(self, prior_freq=2.0, prior_sum=2.0, prior_sum_sq=4.0):
+ self.freq = prior_freq
+ self.sum = prior_sum
+ self.sum_sq = prior_sum_sq
+ self.pfreq = prior_freq
+ self.psum = prior_sum
+ self.psum_sq = prior_sum_sq
def add_outcome(self, val):
self.freq += 1
self.sum += val
self.sum_sq += val * val
+ def real_frequency(self):
+ return self.freq - self.pfreq
+
def frequency(self):
return self.freq
def mean(self):
- return (self.sum + 2) / (self.freq + 2)
+ return self.sum / self.freq
def variance(self):
if self.freq <= 1:
return 1e10
- return (((self.sum_sq + 4) - ((self.sum + 2) ** 2) / (self.freq + 2)) /
- (self.freq + 1))
+ return (((self.sum_sq) - ((self.sum) ** 2) / (self.freq)) /
+ (self.freq - 1))
def std_dev(self):
return self.variance() ** .5
def sample_std_dev(self):
- return (self.variance() / (self.freq + 2)) ** .5
+ return (self.variance() / (self.freq or 1)) ** .5
def __add__(self, o):
+ self._assert_priors_match(o)
ret = MeanVarStat()
- ret.freq = self.freq + o.freq
- ret.sum = self.sum + o.sum
- ret.sum_sq = self.sum_sq + o.sum_sq
+ ret.freq = self.freq + o.freq - o.pfreq
+ ret.sum = self.sum + o.sum - o.psum
+ ret.sum_sq = self.sum_sq + o.sum_sq - o.psum_sq
return ret
def __sub__(self, o):
+ self._assert_priors_match(o)
ret = MeanVarStat()
- ret.freq = self.freq - o.freq
- ret.sum = self.sum - o.sum
- ret.sum_sq = self.sum_sq - o.sum_sq
+ ret.freq = self.freq - o.freq + o.pfreq
+ ret.sum = self.sum - o.sum + o.psum
+ ret.sum_sq = self.sum_sq - o.sum_sq + o.psum_sq
return ret
def mean_diff(self, o):
@@ -87,16 +93,20 @@ def from_primitive_object(self, obj):
else:
assert 'Confused by obj %s' % str(obj) and False
+ def _assert_priors_match(self, obj):
+ assert self.pfreq == obj.pfreq
+ assert self.psum == obj.psum
+ assert self.psum_sq == obj.psum_sq
+
def merge(self, obj):
- self.freq += obj.freq
- self.sum += obj.sum
- self.sum_sq += obj.sum_sq
+ self._assert_priors_match(obj)
+ self.freq += obj.freq - obj.pfreq
+ self.sum += obj.sum - obj.psum
+ self.sum_sq += obj.sum_sq - obj.psum_sq
def __str__(self):
return '%s, %s, %s' % (self.freq, self.sum, self.sum_sq)
-
-
class DiffStat(object):
"""
Statistics about the difference in means of two distributions.

0 comments on commit 2e2e79d

Please sign in to comment.