コード例 #1
0
 def testAllSequences(self):
     event = probability.FiniteProductEvent([
         probability.DiscreteEvent({1, 2}),
         probability.DiscreteEvent({3})
     ])
     all_sequences = [i for i in event.all_sequences()]
     self.assertEqual(all_sequences, [(1, 3), (2, 3)])
コード例 #2
0
    def testProbability_FiniteProductEvent(self):
        # 5 coin flips of a biased coin with heads prob = 1/3.
        base_space = probability.DiscreteProbabilitySpace({'h': 1, 't': 2})
        space = probability.FiniteProductSpace([base_space] * 5)

        heads = probability.DiscreteEvent({'h'})
        tails = probability.DiscreteEvent({'t'})
        event = probability.FiniteProductEvent(
            [heads, heads, tails, tails, heads])
        self.assertEqual(space.probability(event), sympy.Rational(4, 3**5))
コード例 #3
0
 def testInverse_FiniteProductEvent(self):
     rv = self._random_variable()
     event1 = probability.DiscreteEvent({'a', 'b'})
     event2 = probability.DiscreteEvent({'x'})
     event = probability.FiniteProductEvent((event1, event2))
     result = rv.inverse(event)
     self.assertIsInstance(result, probability.FiniteProductEvent)
     self.assertLen(result.events, 2)
     self.assertEqual(result.events[0].values, {1, 2})
     self.assertEqual(result.events[1].values, {1, 3})
コード例 #4
0
 def testBasic(self):
     space = probability.DiscreteProbabilitySpace({0: 1, 1: 2, 2: 3})
     p = space.probability(probability.DiscreteEvent([0]))
     self.assertEqual(p, sympy.Rational(1, 6))
     p = space.probability(probability.DiscreteEvent([0, 1]))
     self.assertEqual(p, sympy.Rational(1, 2))
     p = space.probability(probability.DiscreteEvent([0, 1, 2]))
     self.assertEqual(p, 1)
     p = space.probability(probability.DiscreteEvent([0, 1, 2, 3]))
     self.assertEqual(p, 1)
     p = space.probability(probability.DiscreteEvent([3]))
     self.assertEqual(p, 0)
コード例 #5
0
    def testInverse(self):
        random_variable = probability.DiscreteRandomVariable({
            1: 1,
            2: 3,
            3: 4
        })
        inverse = random_variable.inverse(probability.DiscreteEvent({1, 3}))
        self.assertEqual(inverse.values, {1, 2})

        random_variable = probability.DiscreteRandomVariable({1: 1, 2: 1})
        inverse = random_variable.inverse(probability.DiscreteEvent({1, 5}))
        self.assertEqual(inverse.values, {1, 2})
コード例 #6
0
 def testCall(self):
     random_variable = probability.DiscreteRandomVariable({
         1: 1,
         2: 3,
         3: 4
     })
     forwards = random_variable(probability.DiscreteEvent({1, 3}))
     self.assertEqual(forwards.values, {1, 4})
コード例 #7
0
    def testBasic(self):
        space = probability.SampleWithoutReplacementSpace({0: 1, 1: 1}, 2)
        event_0_0 = probability.FiniteProductEvent(
            [probability.DiscreteEvent({0}),
             probability.DiscreteEvent({0})])
        event_0_1 = probability.FiniteProductEvent(
            [probability.DiscreteEvent({0}),
             probability.DiscreteEvent({1})])
        p_0_0 = space.probability(event_0_0)
        p_0_1 = space.probability(event_0_1)
        self.assertEqual(p_0_0, 0)
        self.assertEqual(p_0_1, sympy.Rational(1, 2))

        space = probability.SampleWithoutReplacementSpace({0: 1, 1: 0}, 1)
        event_0 = probability.FiniteProductEvent(
            [probability.DiscreteEvent({0})])
        event_1 = probability.FiniteProductEvent(
            [probability.DiscreteEvent({1})])
        event_2 = probability.FiniteProductEvent(
            [probability.DiscreteEvent({2})])
        p_0 = space.probability(event_0)
        p_1 = space.probability(event_1)
        p_2 = space.probability(event_2)
        self.assertEqual(p_0, 1)
        self.assertEqual(p_1, 0)
        self.assertEqual(p_2, 0)
コード例 #8
0
def _sequence_event(values, length, verb):
    """Returns sequence (finite product) event.

  Args:
    values: List of values to sample from.
    length: Length of the sequence to generate.
    verb: Verb in infinitive form.

  Returns:
    Instance of `probability.FiniteProductEvent`, together with a text
    description.
  """
    del verb  # unused
    samples = [random.choice(values) for _ in range(length)]
    events = [probability.DiscreteEvent([sample]) for sample in samples]
    event = probability.FiniteProductEvent(events)
    sequence = ''.join(str(sample) for sample in samples)
    event_description = 'последовательности {sequence}'.format(
        sequence=sequence)
    return event, event_description