]> gitweb.michael.orlitzky.com - dead/census-tools.git/commitdiff
Added the (Uniform)DistributionTest modules.
authorMichael Orlitzky <michael@orlitzky.com>
Sat, 15 May 2010 03:57:54 +0000 (23:57 -0400)
committerMichael Orlitzky <michael@orlitzky.com>
Sat, 15 May 2010 03:57:54 +0000 (23:57 -0400)
src/Tests/Unit/DistributionTest.py [new file with mode: 0644]
src/Tests/Unit/UniformDistributionTest.py [new file with mode: 0644]

diff --git a/src/Tests/Unit/DistributionTest.py b/src/Tests/Unit/DistributionTest.py
new file mode 100644 (file)
index 0000000..8ab3d0d
--- /dev/null
@@ -0,0 +1,32 @@
+import unittest
+
+import probability.Distribution
+
+
+class DistributionTest(unittest.TestCase):
+
+    def testSampledValuesWithinComponentBounds(self):
+        """
+        Create a distribution from the sum of two uniforms, and make
+        sure that the values sampled from the sum lie in the interval
+        (min1+min2, max1+max2).
+        """
+        min1 = 0.1515
+        max1 = 22
+        min2 = 14
+        max2 = 100
+
+        ud1 = probability.Distribution.Uniform(min1, max1)
+        ud2 = probability.Distribution.Uniform(min2, max2)
+        distro = ud1 + ud2
+
+        for x in range(0,1000):
+            s = distro.sample()
+            self.assertTrue(s >= (min1+min2))
+            self.assertTrue(s <= (max1+max2))
+
+
+def suite():
+    suite = unittest.TestSuite()
+    suite.addTest(unittest.makeSuite(DistributionTest))
+    return suite
diff --git a/src/Tests/Unit/UniformDistributionTest.py b/src/Tests/Unit/UniformDistributionTest.py
new file mode 100644 (file)
index 0000000..d69d643
--- /dev/null
@@ -0,0 +1,55 @@
+import unittest
+
+import probability.Distribution
+
+
+class UniformDistributionTest(unittest.TestCase):
+
+    def testBoundsOrdered(self):
+        """
+        The constructor for Uniform() takes two numbers, 'a' and 'b'
+        which provide the upper and lower bounds for the
+        distribution. The constructor should place the lesser of the
+        two in self.min, and the greater in self.max.
+        """
+        ud = probability.Distribution.Uniform(0,0)
+        self.assertTrue(ud.min == 0)
+        self.assertTrue(ud.max == 0)
+
+        ud = probability.Distribution.Uniform(0,10)
+        self.assertTrue(ud.min == 0)
+        self.assertTrue(ud.max == 10)
+
+        ud = probability.Distribution.Uniform(0.558, 0.23)
+        self.assertTrue(ud.min == 0.23)
+        self.assertTrue(ud.max == 0.558)
+
+
+    def testSampledValuesWithinBounds(self):
+        """
+        Sample the distribution a few times, and make sure the values
+        we get are within the (min,max) we supplied.
+        """
+        ud = probability.Distribution.Uniform(0.558, 0.23)
+        for x in range(0,1000):
+            s = ud.sample()
+            self.assertTrue(s >= ud.min)
+            self.assertTrue(s <= ud.max)
+
+
+    def testKnownCdfValues(self):
+        """
+        The CDF of a uniform distribution is easy to calculate.
+        Check a few known values.
+        """
+        ud = probability.Distribution.Uniform(0, 1)
+        delta = 0.1
+        for i in range(0,11):
+            x = i*delta
+            self.assertEqual(x, ud.cdf(x))
+
+
+def suite():
+    suite = unittest.TestSuite()
+    suite.addTest(unittest.makeSuite(UniformDistributionTest))
+    return suite