Back to index

python-biopython  1.60
Classes | Functions | Variables
test_HMMCasino Namespace Reference

Classes

class  DiceRollAlphabet
class  DiceTypeAlphabet

Functions

def _loaded_dice_roll
def generate_rolls
def stop_training

Variables

int VERBOSE = 0
tuple mm_builder
tuple baum_welch_mm = mm_builder.get_markov_model()
tuple standard_mm = mm_builder.get_markov_model()
tuple known_training_seq = Trainer.TrainingSequence(rolls, states)
tuple trainer = Trainer.KnownStateTrainer(standard_mm)
tuple trained_mm = trainer.train([known_training_seq])
tuple training_seq = Trainer.TrainingSequence(rolls, Seq("", DiceTypeAlphabet()))

Function Documentation

def test_HMMCasino._loaded_dice_roll (   chance_num,
  cur_state 
) [private]
Generate a loaded dice roll based on the state and a random number

Definition at line 48 of file test_HMMCasino.py.

00048 
00049 def _loaded_dice_roll(chance_num, cur_state):
00050     """Generate a loaded dice roll based on the state and a random number
00051     """
00052     if cur_state == 'F':
00053         if chance_num <= (float(1) / float(6)):
00054             return '1'
00055         elif chance_num <= (float(2) / float(6)):
00056             return '2'
00057         elif chance_num <= (float(3) / float(6)):
00058             return '3'
00059         elif chance_num <= (float(4) / float(6)):
00060             return '4'
00061         elif chance_num <= (float(5) / float(6)):
00062             return '5'
00063         else:
00064             return '6'
00065     elif cur_state == 'L':
00066         if chance_num <= (float(1) / float(10)):
00067             return '1'
00068         elif chance_num <= (float(2) / float(10)):
00069             return '2'
00070         elif chance_num <= (float(3) / float(10)):
00071             return '3'
00072         elif chance_num <= (float(4) / float(10)):
00073             return '4'
00074         elif chance_num <= (float(5) / float(10)):
00075             return '5'
00076         else:
00077             return '6'
00078     else:
00079         raise ValueError("Unexpected cur_state %s" % cur_state)

Here is the caller graph for this function:

def test_HMMCasino.generate_rolls (   num_rolls)
Generate a bunch of rolls corresponding to the casino probabilities.

Returns:

o The generate roll sequence

o The state sequence that generated the roll.

Definition at line 80 of file test_HMMCasino.py.

00080 
00081 def generate_rolls(num_rolls):
00082     """Generate a bunch of rolls corresponding to the casino probabilities.
00083 
00084     Returns:
00085 
00086     o The generate roll sequence
00087 
00088     o The state sequence that generated the roll.
00089     """
00090     # start off in the fair state
00091     cur_state = 'F'
00092     roll_seq = MutableSeq('', DiceRollAlphabet())
00093     state_seq = MutableSeq('', DiceTypeAlphabet())
00094 
00095     # generate the sequence
00096     for roll in range(num_rolls):
00097         state_seq.append(cur_state)
00098         # generate a random number
00099         chance_num = random.random()
00100 
00101         # add on a new roll to the sequence
00102         new_roll = _loaded_dice_roll(chance_num, cur_state)
00103         roll_seq.append(new_roll)
00104 
00105         # now give us a chance to switch to a new state
00106         chance_num = random.random()
00107         if cur_state == 'F':
00108             if chance_num <= .05:
00109                 cur_state = 'L'
00110         elif cur_state == 'L':
00111             if chance_num <= .1:
00112                 cur_state = 'F'
00113 
00114     return roll_seq.toseq(), state_seq.toseq()
00115     
# -- build a MarkovModel

Here is the call graph for this function:

def test_HMMCasino.stop_training (   log_likelihood_change,
  num_iterations 
)
Tell the training model when to stop.

Definition at line 154 of file test_HMMCasino.py.

00154 
00155 def stop_training(log_likelihood_change, num_iterations):
00156     """Tell the training model when to stop.
00157     """
00158     if VERBOSE:
00159         print "ll change:", log_likelihood_change
00160     if log_likelihood_change < 0.01:
00161         return 1
00162     elif num_iterations >= 10:
00163         return 1
00164     else:
00165         return 0
00166 
# -- Standard Training with known states

Variable Documentation

tuple test_HMMCasino.baum_welch_mm = mm_builder.get_markov_model()

Definition at line 142 of file test_HMMCasino.py.

tuple test_HMMCasino.known_training_seq = Trainer.TrainingSequence(rolls, states)

Definition at line 168 of file test_HMMCasino.py.

Initial value:
00001 MarkovModel.MarkovModelBuilder(DiceTypeAlphabet(),
00002                                             DiceRollAlphabet())

Definition at line 116 of file test_HMMCasino.py.

tuple test_HMMCasino.standard_mm = mm_builder.get_markov_model()

Definition at line 143 of file test_HMMCasino.py.

Definition at line 171 of file test_HMMCasino.py.

tuple test_HMMCasino.trainer = Trainer.KnownStateTrainer(standard_mm)

Definition at line 170 of file test_HMMCasino.py.

tuple test_HMMCasino.training_seq = Trainer.TrainingSequence(rolls, Seq("", DiceTypeAlphabet()))

Definition at line 186 of file test_HMMCasino.py.

Definition at line 38 of file test_HMMCasino.py.