Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

fixed bug with count buys resetting

  • Loading branch information...
commit 5972048b319ace80a5601433ca52a8c8d572b4e7 1 parent f66d342
@rrenaud rrenaud authored
Showing with 72 additions and 10 deletions.
  1. +8 −0 mergeable.py
  2. +8 −1 stats.py
  3. +37 −0 test_count_buys.py
  4. +19 −9 test_stats.py
View
8 mergeable.py
@@ -1,3 +1,11 @@
+""" A mixin to allow merging two objects in an additive fashion.
+
+Given an arbitraryly nested object whose leaf objects consist of numbers,
+provide a merge() operation that sums the numbers when the access paths to the
+leaves are the same and otherwise does a union.
+
+From another angle, support a kind of 'vector addition' for messy, complicated
+objects """
# I't s quite possible that this is too abstract/clever.
class MergeableObjectImpl(object):
View
9 stats.py
@@ -14,7 +14,7 @@
import primitive_util
import mergeable
-# TODO: Make this support a variable prior (rather than the win-rate based
+# 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):
@@ -87,9 +87,16 @@ def from_primitive_object(self, obj):
else:
assert 'Confused by obj %s' % str(obj) and False
+ def merge(self, obj):
+ self.freq += obj.freq
+ self.sum += obj.sum
+ self.sum_sq += obj.sum_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.
View
37 test_count_buys.py
@@ -0,0 +1,37 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+try:
+ import unittest2 as unittest
+except ImportError, e:
+ import unittest
+
+import count_buys
+
+class TestBuyStats(unittest.TestCase):
+ def test_merge_buy_stat(self):
+ a = count_buys.BuyStat()
+ b = count_buys.BuyStat()
+
+ a.available.add_outcome(1)
+ b.available.add_outcome(3)
+ self.assertEquals(a.available.frequency(), 1)
+ self.assertEquals(b.available.frequency(), 1)
+
+ b.merge(a)
+
+ self.assertEquals(b.available.frequency(), 2)
+
+ def test_merge_deck_buy_stats(self):
+ a = count_buys.DeckBuyStats()
+ b = count_buys.DeckBuyStats()
+
+ a['Estate'].available.add_outcome(2)
+ b['Estate'].available.add_outcome(0)
+
+ self.assertEquals(a['Estate'].available.frequency(), 1)
+ self.assertEquals(b['Estate'].available.frequency(), 1)
+ b.merge(a)
+ self.assertEquals(b['Estate'].available.frequency(), 2)
+
+if __name__ == '__main__':
+ unittest.main()
View
28 test_stats.py
@@ -4,16 +4,26 @@
import stats
class RandomVariableStat(unittest.TestCase):
- def testSimple(self):
+ def test_simple(self):
d = stats.MeanVarStat()
- d.AddOutcome(4) # 10 - 4 = 6, 6^2 = 36
- d.AddOutcome(16) # 16 - 10 = 6, 6^2 = 36
- d.AddOutcome(13) # 13 - 10 = 3, 3^2 = 9
- d.AddOutcome(7) # 10 - 7 = 3, 3^2 = 9
- # 36 + 36 + 9 + 9 = 90, 90 / 3 = 30
- self.assertEquals(d.Frequency(), 4)
- self.assertEquals(d.Mean(), 10)
- self.assertEquals(d.Variance(), 30.0)
+ # prior says 1 2p win, 1 2p loss # freq = 2, sum = 2, sum_sq = 4
+ d.add_outcome(2) # freq = 3, sum = 4, sum_sq = 8
+ d.add_outcome(3) # freq = 4, sum = 7, sum_sq = 17
+ self.assertEquals(d.frequency(), 2)
+ self.assertEquals(d.mean(), 7. / 4)
+ self.assertEquals(d.variance(), (17 - 49. / 4) / 3)
+
+ def test_merge(self):
+ a = stats.MeanVarStat()
+ b = stats.MeanVarStat()
+
+ a.add_outcome(1)
+ b.add_outcome(1)
+
+ b.merge(a)
+
+ self.assertEquals(b.frequency(), 2)
+
if __name__ == '__main__':
unittest.main()
Please sign in to comment.
Something went wrong with that request. Please try again.