Ejemplo n.º 1
0
trans, emissions = model.forward_backward(sequence)
print trans
print emissions

print "Viterbi"
prob, states = model.viterbi(sequence)
print "Prob: {}".format(prob)
print "\n".join(state[1].name for state in states)
print
print "MAP"
prob, states = model.maximum_a_posteriori(sequence)
print "Prob: {}".format(prob)
print "\n".join(state[1].name for state in states)

print "Showing that sampling can reproduce the original transition probs."
print "Should produce a matrix close to the following: "
print " [ [ 0.60, 0.10, 0.30 ] "
print "   [ 0.40, 0.40, 0.20 ] "
print "   [ 0.05, 0.15, 0.80 ] ] "
print
print "Tranition Matrix From 100000 Samples:"
sample, path = model.sample(100000, path=True)
trans = np.zeros((3, 3))

for state, n_state in it.izip(path[1:-2], path[2:-1]):
    state_name = float(state.name[1:]) - 1
    n_state_name = float(n_state.name[1:]) - 1
    trans[state_name, n_state_name] += 1

trans = (trans.T / trans.sum(axis=1)).T
print trans
Ejemplo n.º 2
0
silent = State(None, name="silent")
model.add_state(state)
model.add_state(state2)

model.add_transition(state, state, 0.4)
model.add_transition(state, state2, 0.4)
model.add_transition(state2, state2, 0.4)
model.add_transition(state2, state, 0.4)

model.add_transition(model.start, state, 0.5)
model.add_transition(model.start, state2, 0.5)
model.add_transition(state, model.end, 0.2)
model.add_transition(state2, model.end, 0.2)

model.bake()
sequence = model.sample()

print sequence
print
print model.forward(sequence)[ len(sequence), model.end_index ]
print model.backward(sequence)[0,model.start_index]
print
trans, ems =  model.forward_backward(sequence)
print trans
print ems
print
model.train( [ sequence ] )

print
print model.forward(sequence)[ len(sequence), model.end_index ]
print model.backward(sequence)[0,model.start_index]
Ejemplo n.º 3
0
# Transition matrix, with 0.05 subtracted from each probability to add to
# the probability of exiting the hmm
model.add_transition(rainy, rainy, 0.65)
model.add_transition(rainy, sunny, 0.25)
model.add_transition(sunny, rainy, 0.35)
model.add_transition(sunny, sunny, 0.55)

# Add transitions to the end of the model
model.add_transition(rainy, model.end, 0.1)
model.add_transition(sunny, model.end, 0.1)

# Finalize the model structure
model.bake(verbose=True)

# Lets sample from this model.
print model.sample()

# Lets call Bob every hour and see what he's doing!
# (aka build up a sequence of observations)
sequence = ['walk', 'shop', 'clean', 'clean', 'clean', 'walk', 'clean']

# What is the probability of seeing this sequence?
print "Probability of Sequence: ", \
 math.e**model.forward( sequence )[ len(sequence), model.end_index ]
print "Probability of Cleaning at Time Step 3 Given This Sequence: ", \
 math.e**model.forward_backward( sequence )[1][ 2, model.states.index( rainy ) ]
print "Probability of the Sequence Given It's Sunny at Time Step 4: ", \
 math.e**model.backward( sequence )[ 3, model.states.index( sunny ) ]

print " ".join(state.name
               for i, state in model.maximum_a_posteriori(sequence)[1])
Ejemplo n.º 4
0
# Transition matrix, with 0.05 subtracted from each probability to add to
# the probability of exiting the hmm
model.add_transition( rainy, rainy, 0.65 )
model.add_transition( rainy, sunny, 0.25 )
model.add_transition( sunny, rainy, 0.35 )
model.add_transition( sunny, sunny, 0.55 )

# Add transitions to the end of the model
model.add_transition( rainy, model.end, 0.1 )
model.add_transition( sunny, model.end, 0.1 )

# Finalize the model structure
model.bake( verbose=True )

# Lets sample from this model.
print model.sample()

# Lets call Bob every hour and see what he's doing!
# (aka build up a sequence of observations)
sequence = [ 'walk', 'shop', 'clean', 'clean', 'clean', 'walk', 'clean' ]

# What is the probability of seeing this sequence?
print "Probability of Sequence: ", \
	math.e**model.forward( sequence )[ len(sequence), model.end_index ]
print "Probability of Cleaning at Time Step 3 Given This Sequence: ", \
	math.e**model.forward_backward( sequence )[1][ 2, model.states.index( rainy ) ]
print "Probability of the Sequence Given It's Sunny at Time Step 4: ", \
	math.e**model.backward( sequence )[ 3, model.states.index( sunny ) ]

print " ".join( state.name for i, state in model.maximum_a_posteriori( sequence )[1] )
Ejemplo n.º 5
0
trans, emissions = model.forward_backward( sequence )
print trans
print emissions

print "Viterbi"
prob, states = model.viterbi( sequence )
print "Prob: {}".format( prob )
print "\n".join( state[1].name for state in states )
print
print "MAP"
prob, states = model.maximum_a_posteriori( sequence )
print "Prob: {}".format( prob )
print "\n".join( state[1].name for state in states )

print "Showing that sampling can reproduce the original transition probs."
print "Should produce a matrix close to the following: "
print " [ [ 0.60, 0.10, 0.30 ] "
print "   [ 0.40, 0.40, 0.20 ] "
print "   [ 0.05, 0.15, 0.80 ] ] "
print
print "Tranition Matrix From 100000 Samples:"
sample, path = model.sample( 100000, path=True )
trans = np.zeros((3,3))

for state, n_state in it.izip( path[1:-2], path[2:-1] ):
	state_name = float( state.name[1:] )-1
	n_state_name = float( n_state.name[1:] )-1
	trans[ state_name, n_state_name ] += 1

trans = (trans.T / trans.sum( axis=1 )).T
print trans
Ejemplo n.º 6
0
silent = State(None, name="silent")
model.add_state(state)
model.add_state(state2)

model.add_transition(state, state, 0.4)
model.add_transition(state, state2, 0.4)
model.add_transition(state2, state2, 0.4)
model.add_transition(state2, state, 0.4)

model.add_transition(model.start, state, 0.5)
model.add_transition(model.start, state2, 0.5)
model.add_transition(state, model.end, 0.2)
model.add_transition(state2, model.end, 0.2)

model.bake()
sequence = model.sample()

print sequence
print
print model.forward(sequence)[len(sequence), model.end_index]
print model.backward(sequence)[0, model.start_index]
print
trans, ems = model.forward_backward(sequence)
print trans
print ems
print
model.train([sequence])

print
print model.forward(sequence)[len(sequence), model.end_index]
print model.backward(sequence)[0, model.start_index]