model.add_transition(s3, s2, 0.15)
model.add_transition(s3, s3, 0.8)
model.bake()

sequence = [4.8, 5.6, 24.1, 25.8, 14.3, 26.5, 15.9, 5.5, 5.1]

print model.is_infinite()

print "Algorithms On Infinite Model"
sequence = [4.8, 5.6, 24.1, 25.8, 14.3, 26.5, 15.9, 5.5, 5.1]
print "Forward"
print model.forward(sequence)

print "\n".join(state.name for state in model.states)
print "Backward"
print model.backward(sequence)

print "Forward-Backward"
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)
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]
print
trans, ems = model.forward_backward(sequence)
print trans
print ems
print
# 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])
# 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] )
model.add_transition( s3, s3, 0.8 )
model.bake()

sequence = [ 4.8, 5.6, 24.1, 25.8, 14.3, 26.5, 15.9, 5.5, 5.1 ]


print model.is_infinite()

print "Algorithms On Infinite Model"
sequence = [ 4.8, 5.6, 24.1, 25.8, 14.3, 26.5, 15.9, 5.5, 5.1 ]
print "Forward"
print model.forward( sequence )

print "\n".join( state.name for state in model.states )
print "Backward"
print model.backward( sequence )

print "Forward-Backward"
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 )
Exemple #6
0
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]
print
trans, ems = model.forward_backward(sequence)
print trans
print ems
print