Question

I have a matrix mat with values between 0 and 1 (so can be probabilities) as follows:

> t <- c(22, 65, 37, 84, 36, 14, 9, 19, 5, 49)
> x <- t/max(t)
> mat <- x%*%t(x)

I now want to convert this matrix b into a Markov transition matrix, i.e. have the elements of each row add up to 1. I achieve this by dividing the matrix by rowSums:

> y <- mat/rowSums(mat)
> z <- y/rowSums(y)
> rowSums(z)
  [1] 1 1 1 1 1 1 1 1 1 1

However, this causes the elements in each column to have the same value:

           [,1]      [,2]      [,3]      [,4]      [,5]
 [1,] 0.06470588 0.1911765 0.1088235 0.2470588 0.1058824 
 [2,] 0.06470588 0.1911765 0.1088235 0.2470588 0.1058824 
 [3,] 0.06470588 0.1911765 0.1088235 0.2470588 0.1058824 
 [4,] 0.06470588 0.1911765 0.1088235 0.2470588 0.1058824 

This is not what I want. I require each element to have different values but I'm not sure how to do this. Any suggestions appreciated!

Was it helpful?

Solution

Why you are first making matrix y and then z?

Your idea of dividing by rowSums is right, but I think the problem is in your original matrix, as this works:

mat<-matrix(runif(100),10,10)
mat2<-mat/rowSums(mat)
rowSums(mat2)
 [1] 1 1 1 1 1 1 1 1 1 1

edit: Line x <- t/max(t) seems to cause the unwanted behaviour, i.e. you shouldn't divide your vector t with the maximum as that makes your resulting matrix singular.

OTHER TIPS

You cant have a simetric matrix in mat. Try this:

n <- 10
mat <- matrix(runif(n**2),n)
mat <- mat/rowSums(mat)

You could fill the matrix with runif's and then iterating the Sinkhorn-Knopf algorithm (described on e.g., the top of page 3 of http://www.cerfacs.fr/algor/reports/2006/TR_PA_06_42.ps.gz) will converge to a doubly stochastic (Markov) matrix.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top