Write a method to simulate state and symbol sequences of arbitrary length from the HMM

SKU: assim67 Category:

How does P(x) compare to P(x, π) for the examples you calculated?


Order Now

1. Suppose we wish to estimate basic secondary structure in protein (amino acid) sequences. The model we consider is a simplistic rendition of the model discussed in S C. Schmidler et al. (2004) Bayesian Segmentation of Protein Secondary Structure, doi:10.1089/10665270050081496

We assume that at each point of the sequence, the residue is associated with one of three secondary structures: α-helix, β-strand and loops which we label H, S and T, respectively. To simplify the problem, we classify the amino acids as either hydrophobic, hydrophilic or neutral (B, I or N, respectively) so a sequence can be represented by this 3-letter alphabet.

In a α-helix, the residues are 10% neutral, 30% hydrophobic and 60% hydrophilic.

In a β-strand, they are 30%, 55%, 15% and in a loop they are 70%, 10%, 20%.

Assume that all secondary structures have geometrically distributed length with α-helices having mean 15 residues, β-strands having a mean of 8 residues and loops a mean of 6 residues. A β-strand is followed by an α-helix 40% of the time and a loop 60% of the time. An α-helix is followed by a β-strand 30% of the time and a loop 70% of the time and a loop is equally likely to be followed by a strand or a helix. At the start of a sequence, any structure is equally likely.


(a) Sketch a diagram of the HMM (a hand-drawn and scanned picture is fine). In your diagram, show only state nodes and transitions. Show the emission probabilities using a separate table.

Derive the transition probabilities of a state to itself (e.g., aHH) by considering that if L is geometrically distributed with parameter p then E[L] = 1/p. Make sure you use the correct parametrization of the geometric distribution (noting that you can’t have a secondary structure of length 0) and remember that a10   = 1 for any state k.


(b)Write a method to simulate state and symbol sequences of arbitrary length from the HMM. Your method should take sequence length as an argument. Simulate and print out a state and symbol sequence of length 150.


(c) Write a method to calculate the natural logarithm of the joint probability P(x, π). Your method should take x and π as arguments. Use your method to calculate P(x, π) for π and x given below and for the sequences you simulated in Q1b.

π = S,S,H,H,H,T,T,S,S,S,H,H,H,H,H,H,S,S,S,S,S,S

x = B,I,N,B,N,I,N,B,N,I,N,B,I,N,B,I,I,N,B,B,N,B


(d) Implement the forward algorithm for HMMs to calculate the natural logarithm of the probability P(x). Your method should take x as an argument. Use your method to calculate log(P(x)) for π and x given above and for the sequences you simulated in Q1b.

How does P(x) compare to P(x, π) for the examples you calculated? Does this relationship hold in general? Explain your answer


(e) The method discussed in class for sampling state sequences (Section 18.8 of notes) relies on having the forward matrix, f. In all our calculations, we use the log of the forward matrix, F. Assuming the end of the sequence is not modeled, so fk0 = 1 for all k, describe how the first step of the stochastic traceback can be performed directly from the log matrix, F, without having to form f


2. In this question you will write a method that simulates random trees, extend the sequence simulator you wrote in Assignment 2 to work with these trees, calculate a distance matrix from the simulated sequences and then, using existing code, reconstruct the tree from this distance matrix.

(a) Write a method that simulates trees according to the Yule model (described below) with takes as input the number of leaves, n, and the branching parameter, λ. Use the provided Python classes on the resources page.

The Yule model is a branching process that suggests a method of constructing trees with n leaves. From each leaf, start a lineage going back in time. Each lineage coalesces with others at rate λ. When there k lineages, the total rate of coalescence in the tree is kλ. Thus, we can generate a Yule tree with n leaves as follows:

Set k = n, t = 0.

Make n leaf nodes with time t and labeled from 1 to n. This is the set of available nodes.

While k > 1, iterate:

Generate a time tk ∼ Exp (kλ). Set t = t + tk.

Make a new node, m, with height t and choose two nodes, i and j, uniformly at random from the set of available nodes. Make i and j the child nodes of m.

Add m to the set of available nodes and remove i and j from this set. Set k = k-1.


Simulate 1000 trees with λ = 0.5 and n = 10 and check that the mean height of the trees (that is, the time of the root node) agrees with the theoretical mean of 3.86.

Use the provided plot tree method to include a picture of a simulated tree with 10 leaves and λ = 0.5 in your report. To embed the plot in your report, include in the first cell of your notebook the command %matplotlib inline


(b)Write a method to simulate sequences down a simulated tree according to the Jukes-Cantor model (as described in Assignment 2). Your method should take a tree with n leaves, sequence length L, and a mutation rate µ. It should return either a matrix of sequences corresponding to nodes in the tree or the tree with sequences stored at the nodes.

Your method should generate a uniform random sequence of length L at the root node and recursively mutate it down the branches of the tree, using the node heights to calculate branch length.

In your report, include a simulated tree with n = 10 and λ = 0.5 and a set of sequences of length L = 20 and mutation parameter µ = 0.5 simulated on that tree.


(c)Write a method to calculate the Jukes-Cantor distance matrix, d, from a set of sequences, where dij is the distance between the ith and the jth sequences. Recall that the Jukes-Cantor distance for sequences x and y is defined by



where fxy is the fraction of differing sites between x and y. Since we will be dealing with short sequences, use the following definition of fxy so that the distances are well-defined:


where Dxy is the number of differing sites between x and y and L is the length of x.

Include a simulated set of sequences of length L = 20 from the tree leaves and corresponding distance matrix in your report for a tree with n = 10, λ = 0.5 and mutation parameter µ = 0.5.


(d) Now simulate a tree with n = 10 and λ = 0.5 and on that tree, simulate three sets of sequences with lengths L = 20, L = 50 and L = 200, respectively, with fixed µ = 0.1. For each simulated set of sequences, calculate the distance matrix and print it out.

Then reconstruct the tree using the provided compute _ upgma _ tree method. Use the plot tree method to include a plot of the original tree and a plot of the reconstructed tree for each distance matrix.

Comment on the quality of the reconstructions and the effect that increasing the sequence length has on the accuracy of the reconstruction.


Writing your homework and assignments all on your own is a difficult task. So, Assignments4u has made it easy for students by helping them in writing their assignments. Assignments4u will assist you in completing your tasks, and you can approach us with all your assignment, homework, and essay writing requirements.

We have over 4350+ experienced writers working as experts in different streams of study. Get all your academic doubts clarified and take pride in learning subjects like history, math’s or law. Our assignment help and essay help is available in countries like USA, Australia, New Zealand, Singapore and many more. Get programming assignment help, corporate finance assignment help, marketing assignment help or else business management assignment help in exchange for a nominal price.


There are no reviews yet.

Be the first to review “Write a method to simulate state and symbol sequences of arbitrary length from the HMM”

Your email address will not be published. Required fields are marked *

Sorry no more offers available