<jupyter_start><jupyter_text>Tied States Hidden Markov Model authors:<NAME> [<EMAIL>],<NAME> [<EMAIL>] An example of using tied states to represent the same distribution across multiple states. This example is a toy example derived from biology, where we will look at DNA sequences.The fake structure we will pretend exists is:```start -> background -> CG island -> background -> poly-T region```DNA is comprised of four nucleotides, A, C, G, and T. Lets say that in the background sequence, all of these occur at the same frequency. In the CG island, the nucleotides C and G occur more frequently. In the poly T region, T occurs most frequently.We need the graph structure, because we fake know that the sequence must return to the background distribution between the CG island and the poly-T region. However, we also fake know that both background distributions need to be the same.<jupyter_code>from pomegranate import *
import random
import numpy as np

random.seed(0)<jupyter_output><empty_output><jupyter_text>Lets start off with an example without tied states and see what happens.<jupyter_code>untiedmodel = HiddenMarkovModel( "No Tied States" )<jupyter_output><empty_output><jupyter_text>Here we'll define the four states.<jupyter_code>background_one = State( DiscreteDistribution({'A': 0.25, 'C':0.25, 'G': 0.25, 'T':0.25 }), name="B1" )
CG_island = State( DiscreteDistribution({'A': 0.1, 'C':0.4, 'G': 0.4, 'T':0.1 }), name="CG" )
background_two = State( DiscreteDistribution({'A': 0.25, 'C':0.25, 'G': 0.25, 'T':0.25 }), name="B2" )
poly_T = State( DiscreteDistribution({'A': 0.1, 'C':0.1, 'G': 0.1, 'T':0.7 }), name="PT" )<jupyter_output><empty_output><jupyter_text>Then add the starting transitions.<jupyter_code>untiedmodel.add_transition( untiedmodel.start, background_one, 1. )<jupyter_output><empty_output><jupyter_text>The transition matrix.<jupyter_code>untiedmodel.add_transition( background_one, background_one, 0.9 )
untiedmodel.add_transition( background_one, CG_island, 0.1 )
untiedmodel.add_transition( CG_island, CG_island, 0.8 )
untiedmodel.add_transition( CG_island, background_two, 0.2 )
untiedmodel.add_transition( background_two, background_two, 0.8 )
untiedmodel.add_transition( background_two, poly_T, 0.2 )
untiedmodel.add_transition( poly_T, poly_T, 0.7 )<jupyter_output><empty_output><jupyter_text>And finally the ending transitions.<jupyter_code>untiedmodel.add_transition( poly_T, untiedmodel.end, 0.3)<jupyter_output><empty_output><jupyter_text>Finishing with the method "bake" to finalize the structure of our model.<jupyter_code>untiedmodel.bake( verbose=True )<jupyter_output><empty_output><jupyter_text>Now let's define the following sequences. Keep in mind training must by done on a list of lists, not on a string in order to allow strings of any length.<jupyter_code>sequences = [ numpy.array(list("TAGCACATCGCAGCGCATCACGCGCGCTAGCATATAAGCACGATCAGCACGACTGTTTTT")),
	      numpy.array(list("TAGAATCGCTACATAGACGCGCGCTCGCCGCGCTCGATAAGCTACGAACACGATTTTTTA")),
	      numpy.array(list("GATAGCTACGACTACGCGACTCACGCGCGCGCTCCGCATCAGACACGAATATAGATAAGATATTTTTT")) ]<jupyter_output><empty_output><jupyter_text>Lets check our distributions before training our model.<jupyter_code>result = "\n".join( "{}: {}".format( state.name, state.distribution )
	for state in untiedmodel.states if not state.is_silent() )

print(result)<jupyter_output>B1: {
    "class" :"Distribution",
    "dtype" :"str",
    "name" :"DiscreteDistribution",
    "parameters" :[
        {
            "A" :0.25,
            "C" :0.25,
            "G" :0.25,
            "T" :0.25
        }
    ],
    "frozen" :false
}
B2: {
    "class" :"Distribution",
    "dtype" :"str",
    "name" :"DiscreteDistribution",
    "parameters" :[
        {
            "A" :0.25,
            "C" :0.25,
            "G" :0.25,
            "T" :0.25
        }
    ],
    "frozen" :false
}
CG: {
    "class" :"Distribution",
    "dtype" :"str",
    "name" :"DiscreteDistribution",
    "parameters" :[
        {
            "A" :0.1,
            "C" :0.4,
            "G" :0.4,
            "T" :0.1
        }
    ],
    "frozen" :false
}
PT: {
    "class" :"Distribution",
    "dtype" :"str",
    "name" :"DiscreteDistribution",
    "parameters" :[
        {
            "A" :0.1,
            "C" :0.1,
            "G" :0.1,
            "T" :0.7
        }
    ],
    "frozen" :false
}<jupyter_text>Now lets train our model.<jupyter_code>untiedmodel.fit( sequences, stop_threshold=0.01 )<jupyter_output><empty_output><jupyter_text>And check our new distributions after training.<jupyter_code>result = "\n".join( "{}: {}".format( state.name, state.distribution )
	for state in untiedmodel.states if not state.is_silent() )
    
print(result)<jupyter_output>B1: {
    "class" :"Distribution",
    "dtype" :"str",
    "name" :"DiscreteDistribution",
    "parameters" :[
        {
            "A" :0.31639790507943677,
            "C" :0.2920431428628597,
            "G" :0.21191370125516387,
            "T" :0.1796452508025395
        }
    ],
    "frozen" :false
}
B2: {
    "class" :"Distribution",
    "dtype" :"str",
    "name" :"DiscreteDistribution",
    "parameters" :[
        {
            "A" :0.4178729606164158,
            "C" :0.22068400964255355,
            "G" :0.19558677075666792,
            "T" :0.16585625898436263
        }
    ],
    "frozen" :false
}
CG: {
    "class" :"Distribution",
    "dtype" :"str",
    "name" :"DiscreteDistribution",
    "parameters" :[
        {
            "A" :0.061102410527201965,
            "C" :0.5058121809386029,
            "G" :0.3384433263771247,
            "T" :0.09464208215707043
        }
    ],
    "frozen" :false
}
PT: {
    "class" :"Distribution",
    "dtype" :"str",
    "name" :"Dis[...]<jupyter_text>Now we can try our example with tied states.<jupyter_code>tiedmodel = HiddenMarkovModel( "Tied States" )<jupyter_output><empty_output><jupyter_text>Lets redefine the four states.<jupyter_code>background = DiscreteDistribution({'A': 0.25, 'C':0.25, 'G': 0.25, 'T':0.25 })

background_one = State( background, name="B1" )
CG_island = State( DiscreteDistribution({'A': 0.1, 
	'C':0.4, 'G': 0.4, 'T':0.1 }), name="CG" )
background_two = State( background, name="B2" )
poly_T = State( DiscreteDistribution({'A': 0.1, 
	'C':0.1, 'G': 0.1, 'T':0.7 }), name="PT" )<jupyter_output><empty_output><jupyter_text>Then add the starting transitions.<jupyter_code>tiedmodel.add_transition( tiedmodel.start, background_one, 1. );<jupyter_output><empty_output><jupyter_text>Then the tranisiton matrix.<jupyter_code>tiedmodel.add_transition( background_one, background_one, 0.9 )
tiedmodel.add_transition( background_one, CG_island, 0.1 )
tiedmodel.add_transition( CG_island, CG_island, 0.8 )
tiedmodel.add_transition( CG_island, background_two, 0.2 )
tiedmodel.add_transition( background_two, background_two, 0.8 )
tiedmodel.add_transition( background_two, poly_T, 0.2 )
tiedmodel.add_transition( poly_T, poly_T, 0.7 )<jupyter_output><empty_output><jupyter_text>Finally adding the ending transitions.<jupyter_code>tiedmodel.add_transition( poly_T, tiedmodel.end, 0.3 )<jupyter_output><empty_output><jupyter_text>We "bake" the model to finalize its structure.<jupyter_code>tiedmodel.bake( verbose=True )<jupyter_output><empty_output><jupyter_text>Now let's use the following sequences to train our model.<jupyter_code>sequences = [ numpy.array(list("TAGCACATCGCAGCGCATCACGCGCGCTAGCATATAAGCACGATCAGCACGACTGTTTTT")),
			  numpy.array(list("TAGAATCGCTACATAGACGCGCGCTCGCCGCGCTCGATAAGCTACGAACACGATTTTTTA")),
			  numpy.array(list("GATAGCTACGACTACGCGACTCACGCGCGCGCTCCGCATCAGACACGAATATAGATAAGATATTTTTT")) ]<jupyter_output><empty_output><jupyter_text>But before that let's check the distributions in our model.<jupyter_code>result = "\n".join( "{}: {}".format( state.name, state.distribution ) 
	for state in tiedmodel.states if not state.is_silent() )

print(result)<jupyter_output>B1: {
    "class" :"Distribution",
    "dtype" :"str",
    "name" :"DiscreteDistribution",
    "parameters" :[
        {
            "A" :0.25,
            "C" :0.25,
            "G" :0.25,
            "T" :0.25
        }
    ],
    "frozen" :false
}
B2: {
    "class" :"Distribution",
    "dtype" :"str",
    "name" :"DiscreteDistribution",
    "parameters" :[
        {
            "A" :0.25,
            "C" :0.25,
            "G" :0.25,
            "T" :0.25
        }
    ],
    "frozen" :false
}
CG: {
    "class" :"Distribution",
    "dtype" :"str",
    "name" :"DiscreteDistribution",
    "parameters" :[
        {
            "A" :0.1,
            "C" :0.4,
            "G" :0.4,
            "T" :0.1
        }
    ],
    "frozen" :false
}
PT: {
    "class" :"Distribution",
    "dtype" :"str",
    "name" :"DiscreteDistribution",
    "parameters" :[
        {
            "A" :0.1,
            "C" :0.1,
            "G" :0.1,
            "T" :0.7
        }
    ],
    "frozen" :false
}<jupyter_text>Now let's train our model.<jupyter_code>tiedmodel.fit( sequences, stop_threshold=0.01 )<jupyter_output><empty_output><jupyter_text>Now let's check our new distributions.<jupyter_code>result = "\n".join( "{}: {}".format( state.name, state.distribution ) 
	for state in tiedmodel.states if not state.is_silent() )

print(result)<jupyter_output>B1: {
    "class" :"Distribution",
    "dtype" :"str",
    "name" :"DiscreteDistribution",
    "parameters" :[
        {
            "A" :0.3825505694630474,
            "C" :0.23803901803476255,
            "G" :0.20043821449422436,
            "T" :0.1789721980079656
        }
    ],
    "frozen" :false
}
B2: {
    "class" :"Distribution",
    "dtype" :"str",
    "name" :"DiscreteDistribution",
    "parameters" :[
        {
            "A" :0.3825505694630474,
            "C" :0.23803901803476255,
            "G" :0.20043821449422436,
            "T" :0.1789721980079656
        }
    ],
    "frozen" :false
}
CG: {
    "class" :"Distribution",
    "dtype" :"str",
    "name" :"DiscreteDistribution",
    "parameters" :[
        {
            "A" :0.10879783006206004,
            "C" :0.4779841622676619,
            "G" :0.3129317572182933,
            "T" :0.10028625045198475
        }
    ],
    "frozen" :false
}
PT: {
    "class" :"Distribution",
    "dtype" :"str",
    "name" :"Discr[...]
{'fraction_non_alphanumeric': 0.14840782938942448, 'fraction_numerical': 0.06066802999318337, 'mean_word_length': 3.2508278145695364, 'pattern_counts': {'":': 0, '<': 85, '<?xml version=': 0, '>': 89, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '21', 'dir': 'jupyter-structured-clean-dedup', 'id': '390891', 'license': 'MIT', 'n_tokens_mistral': 4200, 'n_tokens_neox': 3789, 'n_words': 765, 'path': 'examples/hmm_tied_states.ipynb', 'repo_name': 'timgates42/pomegranate', 'size': '10315'}