Markov Models for Allele Frequencies
In the lecture, you saw that the leading eigenvalue of the Markov matrix \(M\), whose R output is:
[,1] [,2] [,3] [,4]
[1,] 0.980 0.005 0.005 0.010
[2,] 0.005 0.980 0.010 0.005
[3,] 0.005 0.010 0.980 0.005
[4,] 0.010 0.005 0.005 0.980
produced an eigenvector modeling a situation where the alleles are represented equally (each with probability 0.25).
In this exercise, we use a for-loop to iterate the process of mutation from an initial allele distribution of:
[1] 1 0 0 0
and show that this is indeed what happens - that the eigenvector yields the right information in lieu of the for-loop.
For more on Markov Processes, see this link.
This exercise is part of the course
Linear Algebra for Data Science in R
Exercise instructions
- Print
x
, the allele distribution after the 1000 mutations. - Find and scale the first eigenvector of
M
(which is loaded for you) so that it sums to1
. Assign tov1
. - Print
v1
, the scaled first eigenvector ofM
and compare withx
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# This code iterates mutation 1000 times
x <- c(1, 0, 0, 0)
for (j in 1:1000) {x <- M%*%x}
# Print x
print(___)
# Print and scale the first eigenvector of M
Lambda <- eigen(M)
v1 <- Lambda$vectors[, ___]/sum(Lambda$___[, 1])
# Print v1
print(___)