Beispiel #1
0
def iter_forward_algorithm(model, n, verbose=False):

    # calc first position
    nstates = model.get_num_states(0)
    col1 = [
        model.prob_prior(0, j) + model.prob_emission(0, j)
        for j in xrange(nstates)
    ]

    if n > 20:
        step = (n // 20)
    else:
        step = 1

    # loop through positions
    nstates1 = nstates
    for i in xrange(1, n):
        if verbose and i % step == 0:
            print " forward iter=%d/%d, lnl=%f" % (i + 1, n, max(col1))

        nstates2 = model.get_num_states(i)

        # find total transition and emission
        col2 = []
        for k in xrange(nstates2):
            tot = -util.INF
            emit = model.prob_emission(i, k)
            for j in xrange(nstates1):
                p = col1[j] + model.prob_transition(i - 1, j, i, k) + emit
                tot = logadd(tot, p)
            col2.append(tot)

        yield col2
        col1 = col2
        nstates1 = nstates2
def iter_forward_algorithm(model, n, verbose=False):
    
    # calc first position
    nstates = model.get_num_states(0)
    col1 = [model.prob_prior(0, j) + model.prob_emission(0, j)
            for j in xrange(nstates)]
    
    if n > 20:
        step = (n // 20)
    else:
        step = 1
    
    # loop through positions
    nstates1 = nstates
    for i in xrange(1, n):
        if verbose and i % step == 0:
            print " forward iter=%d/%d, lnl=%f" % (i+1, n, max(col1))

        nstates2 = model.get_num_states(i)

        # find total transition and emission
        col2 = []
        for k in xrange(nstates2):
            tot = -util.INF
            emit = model.prob_emission(i, k)
            for j in xrange(nstates1):
                p = col1[j] + model.prob_transition(i-1, j, i, k) + emit
                tot = logadd(tot, p)
            col2.append(tot)

        yield col2
        col1 = col2
        nstates1 = nstates2
Beispiel #3
0
def sample_hmm_next_state(model, pos, state):
    nstates = model.get_num_states(pos)
    state2 = 0
    p = model.prob_transition(pos - 1, state, pos, state2)
    pick = log(random.random())
    while pick > p and state2 < nstates:
        state2 += 1
        p = logadd(p, model.prob_transition(pos - 1, state, pos, state2))
    return state2
Beispiel #4
0
def sample_hmm_first_state(model):
    state = 0
    nstates = model.get_num_states(0)
    p = model.prob_prior(0, state)
    pick = log(random.random())
    while pick > p and state < nstates:
        state += 1
        p = logadd(p, model.prob_prior(0, state))
    return state
def sample_hmm_next_state(model, pos, state):
    nstates = model.get_num_states(pos)
    state2 = 0
    p = model.prob_transition(pos-1, state, pos, state2)
    pick = log(random.random())
    while pick > p and state2 < nstates:
        state2 += 1
        p = logadd(p, model.prob_transition(pos-1, state, pos, state2))
    return state2
def sample_hmm_first_state(model):
    state = 0
    nstates = model.get_num_states(0)
    p = model.prob_prior(0, state)
    pick = log(random.random())
    while pick > p and state < nstates:
        state += 1
        p = logadd(p, model.prob_prior(0, state))
    return state
Beispiel #7
0
def get_posterior_probs(model, n, verbose=False):

    probs_forward = forward_algorithm(model, n, verbose=verbose)
    probs_backward = backward_algorithm(model, n, verbose=verbose)

    total_prob = -util.INF
    for j in xrange(model.get_num_states(0)):
        total_prob = logadd(
            total_prob,
            model.prob_prior(0, j) + model.prob_emission(0, j) +
            probs_backward[0][j])

    probs_post = [[
        probs_forward[i][j] + probs_backward[i][j] - total_prob
        for j in xrange(model.get_num_states(i))
    ] for i in xrange(n)]

    return probs_post
def get_posterior_probs(model, n, verbose=False):

    probs_forward = forward_algorithm(model, n, verbose=verbose)
    probs_backward = backward_algorithm(model, n, verbose=verbose)

    total_prob = -util.INF
    for j in xrange(model.get_num_states(0)):
        total_prob = logadd(total_prob,
                            model.prob_prior(0, j) +
                            model.prob_emission(0, j) +
                            probs_backward[0][j])

    probs_post = [
        [probs_forward[i][j] + probs_backward[i][j] - total_prob
         for j in xrange(model.get_num_states(i))]
        for i in xrange(n)]

    return probs_post
Beispiel #9
0
def backward_algorithm(model, n, verbose=False):

    probs = []

    # calc last position
    nstates = model.get_num_states(n - 1)
    for i in xrange(n):
        probs.append(None)
    probs[n - 1] = [
        model.prob_prior(n - 1, j) + model.prob_emission(n - 1, j)
        for j in xrange(nstates)
    ]

    if n > 20:
        step = (n // 20)
    else:
        step = 1

    # loop through positions
    for i in xrange(n - 2, -1, -1):
        if verbose and i % step == 0:
            print " backward iter=%d/%d, lnl=%f" % (i + 1, n, max(
                probs[i + 1]))

        nstates1 = model.get_num_states(i)
        nstates2 = model.get_num_states(i + 1)
        col2 = probs[i + 1]

        # find total transition and emission
        col1 = []
        emit = [model.prob_emission(i + 1, k) for k in xrange(nstates2)]
        for j in xrange(nstates1):
            tot = -util.INF
            for k in xrange(nstates2):
                p = col2[k] + emit[k] + model.prob_transition(i, j, i + 1, k)
                tot = logadd(tot, p)
            col1.append(tot)

        probs[i] = col1

    return probs
def backward_algorithm(model, n, verbose=False):

    probs = []

    # calc last position
    nstates = model.get_num_states(n-1)
    for i in xrange(n):
        probs.append(None)
    probs[n-1] = [model.prob_prior(n-1, j) + model.prob_emission(n-1, j)
                  for j in xrange(nstates)]
    
    if n > 20:
        step = (n // 20)
    else:
        step = 1
    
    # loop through positions
    for i in xrange(n-2, -1, -1):
        if verbose and i % step == 0:
            print " backward iter=%d/%d, lnl=%f" % (i+1, n, max(probs[i+1]))

        nstates1 = model.get_num_states(i)
        nstates2 = model.get_num_states(i+1)
        col2 = probs[i+1]

        # find total transition and emission
        col1 = []
        emit = [model.prob_emission(i+1, k) for k in xrange(nstates2)]
        for j in xrange(nstates1):
            tot = -util.INF
            for k in xrange(nstates2):
                p = col2[k] + emit[k] + model.prob_transition(i, j, i+1, k)
                tot = logadd(tot, p)
            col1.append(tot)
                
        probs[i] = col1

    return probs