AMATH 422/522 Assignment 3 DUE at time and place posted on website This set can be done individually or in groups of any size (working in groups STRONGLY encouraged)! You can always change groups in later weeks. Finally, if you do work as a group, you will turn in one copy of the solutions. For each problem: together with any analysis or explanations, turn in both all code and all relevant plots, labeled and with all line styles, marker sizes etc. adjusted for readability. Make sure all the material for a given problem is presented together – e.g. under “Problem II” you’d have code, plots, any analysis and results, then go to the next problem – as opposed to e.g. grouping all plots together for all problems at the end. Please note: E+G stands for our book, by Ellner and Guckenheimer. I Programming tools and tips. Please give another read through the posted “helpful programming tips in MATLAB.” This covers issues and hints that have come up in class. Please ASK the instructors to explain any aspect of this that remains murky – could save you plenty of time down the road! II Stationary distribution Consider the following stochastic matrix A (which we will use later to model ion channel dynamics): A = .98 .1 0 .02 .7 .05 0 .2 .95 • Write a code that simulates a realization of a Markov chain, using A as transition matrix, for 1000 time steps. Label the states 1,2,3 and store the 1000-long sequence into a vector called states. • Using the function eig from MATLAB, compute the stationary distribution of this markov chain (this should be a 3-long vector) • Using the sequence states that you computed above, compute the fraction of time spent in state 3. Does this agree with the stationary distribution ? • Finally: consider the k−step transition matrices Ak (see p. 84-5 of E+G). Plot the nine matrix entries of Ak vs. k. What do you observe? Does this agree with what we know should happen to limk→∞ Ak ? III Simulating Markov chains and dwell times, Part I: We’ll build up the tools we need for this, step by step. A large part of our text is taken from Ch. 11 of the Lab Manual by Ellner and Guckenheimer, with thanks to those authors for providing the editable .tex! Here we go: Our objective is to generate synthetic data for single channel recordings from finite state Markov chains, and explore patterns in the data they produce. The histogram of expected residence times for each single state in a Markov chain is exponential, with different mean residence times for different states. To observe this in the simplest case, we again consider coin tossing. Say you have a single coin that you’re tossing, which has probability H of coming up “heads.” You sit there tossing it over and over again. The two outcomes, heads or tails, are the different states in this case. Therefore the histogram of residence times for heads and tails should each be exponential. We will take the following steps to compute the residence times: 1. Generate sequences of independent coin tosses based on given probabilities. 2. Calculate the dwell times (also called residence times) by counting the number of tosses between each transition. Let’s get started with the first step. As in class, we’ll call “heads” state 1, with value S1 = 1, and likewise for tails and S2 = 2. • Write a code that “flips the coin 10000 times,” producing a sequence of 10000 “1’s” and “2’s” that record the results, using H = 0.6. Call this list states. Please use a for loop, looping over the number of tosses, to accomplish this. If you are thinking that you could have accomplished this without a for loop, you are right – but we’ll need this for loop structure for the more interesting cases that we will study below. OK, now for the dwell times. Here is a strategy for writing the code to compute these. It is possible to come up with a more computationally efficient attack (see below, and Lab Manual Ch. 11), although the coding can be more error-prone, at least your first time through. Please code up the approach below, and then test any improvements you make to ensure that the results are the same! • Next, we are going to make two lists of these transition times, one for each of the states. Let us start out with these lists being empty, and we will build them up. In MATLAB: list_of_dwell_times_in_state_1=[]; list_of_dwell_times_in_state_2=[]; %note: [] is the empty list Here’s my strategy: I’m just going to loop down the states vector and keep track of what’s happening. First, set starting_state: the state that I am dwelling in for the “current” sequence of states. In MATLAB: starting_state=states(1); starting_timestep=1; %the timestep at which I start dwelling. Next, loop over all the states. We keep going until we come to a transition in the value of the state. When that happens, we store the number of steps that has elapsed since the last transition (or, for the first transition only, since we started counting) in one of the two lists made above. Here is the code, with comments in place: In MATLAB: for k=2:length(states) %Ask the question: Am I still in the starting_state? if states(k) == starting_state %if so, do nothing else %OK, now I need to do something. %starting_state? dwell_time=k-starting_timestep; What’s the dwell time in %assign this dwell time to the right list. if starting_state==1 list_of_dwell_times_in_state_1=[list_of_dwell_times_in_state_1 dwell_time]; else list_of_dwell_times_in_state_2=[list_of_dwell_times_in_state_2 dwell_time]; end %Finally, I must reset starting_state and starting_timestep to %begin measuring the NEXT dwell time starting_state=states(k); starting_timestep=k; end end • Cool! Now we’re done. All that remains is to plot a histogram of dwell times in the various states. • Please assemble the code above, and add some lines to the end to compute the histogram of dwell times in state 1 and in state 2. Can you see that these dwell times reflect an exponential distribution? Please make the appropriate plot, taking a log to verify this. Can you estimate the value of H from the histograms alone, using a formula from class? IV Simulating Markov chains and dwell times, Part II: Models for stochastic switching among conformational states of membrane channels are somewhat more complicated than the coin tosses we considered above. There are usually more than 2 states, and the transition probabilities are state dependent. Moreover, in measurements some states cannot be distinguished from others. We can observe transitions from an open state to a closed state and vice versa, but transitions between open states (or between closed states) are “invisible”. Here we shall simulate data from a Markov chain with 3 states, collapse that data to remove the distinction between 2 of the states and then analyze the data to see that it cannot be readily modeled by a Markov chain with just two states. Suppose we are interested in a membrane current that has three states: one open state, O, and two closed states, C1 and C2 . As in the kinetic scheme discussed in class, state C1 cannot make a transition to state O and vice-versa. We assume that state C2 has shorter residence times than states C1 or O. Here is the transition matrix of a Markov chain that we will use to simulate these conditions. The state S1 = 1 corresponds to C1 , S2 = 2 corresponds to C2 , and S3 = 3 corresponds to C3 : .98 .1 0 .02 .7 .05 0 .2 .95 You can see from the matrix that the probability 0.7 of staying in state C2 is much smaller than the probability 0.98 of staying in state C1 or the probability 0.95 of remaining in state O. Modify your code, using it to complete E+G exercise 3.6, and turn in the resulting code and plots. Note that part (b) was done above already (number II) so no need to redo that. There are two interesting steps here: (1) You’ll need to compute transitions to more than two possible states. We discussed how to do this in class, splitting up the unit interval into more than two segments. (2) To compute dwell times in the closed state, it’s convenient to make a “reduced” list of states rstates after you’ve simulated to produce states. In rstates, you’ll lump together both closed states – say, giving them both the same numerical value of 1. V Optional excursion into multiple ion channels: Consider a neuron that has a total of Nchannels different ion channels, each one of which is governed by the same Markov chain model as in the previous problem. That is, you now have Nchannels independent Markov chains. Say a neuron produces a spike on timestep k if at least 50% of its ion channels are in the open configuration. Write a code that simulates such a neuron and produces a histogram of inter-spike intervals – that is, the numbers of timesteps between two subsequent spikes – for a neuron of this description with Nchannels ranging from 2 to 1000 or beyond. (Please experiment, choosing interesting values of Nchannels within this range.) Note that there are two main ways that you could write such a code. The most direct is to simultaneously simulate Nchannels different Markov chains, each of which has 3 states, as above. I’d suggest starting there, but please see the note below and consider that alternate strategy if you have time! Simulate the system any way you choose. What do you observe about your histogram as Nchannels increases? Give an explanation for this based on one of the theorems in Chapter 3 of our book. (NOTE: An alternative simulation strategy is to write down a single Markov chain with a much larger number of states, representing each relevant configuration of all of the Nchannels states, all at once. One way to start with this is to describe a “relevant” state as a triplet of numbers (N1 , N2 , N3 ), where N1 is the number of channels in state C1 , N2 is the number in C2 , and N3 in O; note that N1 + N2 + N3 = Nchannels . How many different triplets are possible? Can you write down the transition probabilities among them as one “big” transition matrix A? Do you think it would be more efficient to simulate this “big” Markov chain, or the required multiple copies of the “smaller” ones as in the first strategy considered above?)
© Copyright 2026 Paperzz