def testSetVariationalParameters(self):
     gc = gaussian_contributors.GaussianContributors()
     gc.SetVariationalParameters(TEST_DATA)
     # Check that gc was set to something sane:
     contributors = set(['c1', 'c2', 'c3'])
     self.assertSetEqual(contributors, set(gc.bias.keys()))
     self.assertSetEqual(contributors, set(gc.precision.keys()))
 def testResolveQuestion(self):
     gc = gaussian_contributors.GaussianContributors()
     gc.bias = {'worker1': -0.5, 'worker2': 0.8}
     gc.precision = {'worker1': 1.0 / 0.3, 'worker2': 1.0 / 0.2}
     expected = {
         gaussian_contributors.MEAN: 2.32,
         gaussian_contributors.VARIANCE: 0.12
     }
     resolution_map = gc.ResolveQuestion([('worker1', 2.0, {}),
                                          ('worker2', 3.0, {})])
     test_util.AssertMapsAlmostEqual(self,
                                     expected,
                                     resolution_map,
                                     label='variable')
     # Now try with infinite-precision contributors:
     gc.bias = {'worker1': 1.0, 'worker2': 2.0, 'worker3': 3.0}
     gc.precision = {
         'worker1': 1.0,
         'worker2': gaussian_contributors.INFINITY,
         'worker3': gaussian_contributors.INFINITY
     }
     expected = {
         gaussian_contributors.MEAN: -2.5,
         gaussian_contributors.VARIANCE: 0.25
     }
     resolution_map = gc.ResolveQuestion([('worker1', 0.0, {}),
                                          ('worker2', 0.0, {}),
                                          ('worker3', 0.0, {})])
     test_util.AssertMapsAlmostEqual(self,
                                     expected,
                                     resolution_map,
                                     label='variable')
     # Check that non-numeric judgments cause a TypeError:
     self.assertRaises(TypeError, gc.ResolveQuestion,
                       [('worker1', 'WTF', {})])
예제 #3
0
  def testIterateUntilConvergence(self):
    # Initialize a Gaussian model and an EM object:
    gc = gaussian_contributors.GaussianContributors()
    em = alternating_resolution.ExpectationMaximization()

    # Use the judgments from the Dawid & Skene example:
    data = test_util.DS_DATA
    gc.InitializeResolutions(data)
    # The algorithm converges slowly because EPSILON is small:
    self.assertFalse(em.IterateUntilConvergence(data, gc, max_iterations=1000))
    expected = {}
    for question in data:
      expected[question] = dict([
          (gaussian_contributors.MEAN, EXPECTED_MEAN[question]),
          (gaussian_contributors.VARIANCE, EXPECTED_VARIANCE)])
    result = gc.ExtractResolutions(data)
    test_util.AssertResolutionsAlmostEqual(self, expected, result)