コード例 #1
0
def demo_rejection_sampling():
    path_length = 2
    jukes_cantor_rate_matrix = RateMatrix.get_jukes_cantor_rate_matrix()
    states = 'ACGT'
    n = 100000
    nielsen_event_count = 0
    nielsen_path_count = 0
    nielsen_first_time_sum = 0
    nielsen_dwell = dict((c, 0) for c in states)
    rejection_event_count = 0
    rejection_path_count = 0
    rejection_first_time_sum = 0
    rejection_dwell = dict((c, 0) for c in states)
    for i in range(n):
        initial_state = 'A'
        terminal_state = 'C'
        events = get_rejection_sample(initial_state, terminal_state, states, path_length, jukes_cantor_rate_matrix)
        if events is not None:
            assert events
            rejection_path_count += 1
            rejection_event_count += len(events)
            t, state = events[0]
            rejection_first_time_sum += t
            extended = [(0, initial_state)] + events + [(path_length, terminal_state)]
            for (t0, state0), (t1, state1) in zip(extended[:-1], extended[1:]):
                rejection_dwell[state0] += t1 - t0
        events = get_nielsen_sample(initial_state, terminal_state, states, path_length, jukes_cantor_rate_matrix)
        if events is not None:
            assert events
            nielsen_path_count += 1
            nielsen_event_count += len(events)
            t, state = events[0]
            nielsen_first_time_sum += t
            extended = [(0, initial_state)] + events + [(path_length, terminal_state)]
            for (t0, state0), (t1, state1) in zip(extended[:-1], extended[1:]):
                nielsen_dwell[state0] += t1 - t0
    expected_fraction = RateMatrix.get_jukes_cantor_transition_matrix(path_length)[(initial_state, terminal_state)]
    print 'testing the rejection sampling:'
    print 'expected fraction:', expected_fraction
    print 'observed fraction:', rejection_path_count / float(n)
    print 'comparing rejection sampling and nielsen sampling:'
    rejection_method_fraction = rejection_event_count / float(rejection_path_count)
    nielsen_method_fraction = nielsen_event_count / float(nielsen_path_count)
    print 'rejection method fraction:', rejection_method_fraction
    print 'nielsen method fraction:', nielsen_method_fraction
    print 'comparing time of first event:'
    print 'rejection method first event time mean:', rejection_first_time_sum / float(rejection_path_count)
    print 'nielsen method first event time mean:', nielsen_first_time_sum / float(nielsen_path_count)
    print 'comparing the duration spent in each state:'
    print 'rejection:'
    for state, t in rejection_dwell.items():
        print '\t%s: %f' % (state, t/float(rejection_path_count))
    print 'nielsen:'
    for state, t in nielsen_dwell.items():
        print '\t%s: %f' % (state, t/float(nielsen_path_count))
コード例 #2
0
 def test_jukes_cantor_rejection(self):
     path_length = 1
     jukes_cantor_rate_matrix = RateMatrix.get_jukes_cantor_rate_matrix()
     states = 'ACGT'
     n = 200
     observed = 0
     for i in range(n):
         events = get_rejection_sample('A', 'C', states, path_length, jukes_cantor_rate_matrix)
         if events is not None:
             observed += 1
     p = RateMatrix.get_jukes_cantor_transition_matrix(path_length)[('A', 'C')]
     expected = n*p
     variance = n*p*(1-p)
     errstr = 'observed: %f  expected: %f' % (observed, expected)
     self.failUnless(abs(observed - expected) < 3*math.sqrt(variance), errstr)