How can I fix this wrong result in matrix for loop in R?

Hungry Homer

I want to enter in R the following matrix (transition probability) but something I make wrong and the output is not right. The matrix is in the picture :enter image description here

my effort is the following :

n=100
A = matrix(c(0),n+1,n+1);A
A[1,2] = 1/4 
A[1,1] = 3/4
A[2,1] = 1/4 
A[2,2] = 1/2 
A[2,1] = 1/4
for (k in 2:n) {
  A[k,k+1] = 1/4
  A[k,k]   = 1/2 
  A[k,k-1] = 1/6 
  A[k,k-2] = 1/12
  A[n,n]   = 3/4 
  A[n,n-1] = 1/6 
  A[n,n-2] = 1/12}
A


pA = A
for (i in 10000){
  pA = pA %*% A }
pA

but the resulting columns (first 3 columns) must be: 0.2087, 0.1652, 0.1307

Any help?

Martin Gal

You could try a slightly different approach here:

n <- 100
A <- matrix(0, n + 1, n + 1)

# create index for the diagonal and the "other" diagonales
A_diag <- matrix(c(1:(n + 1), 1:(n + 1)), ncol = 2)
A_diag_1 <- matrix(c(1:(n + 1), 1 + 1:(n + 1)), ncol = 2)
A_diag_minus_1 <- matrix(c(1:(n + 1), - 1 + 1:(n + 1)), ncol = 2)
A_diag_minus_2 <- matrix(c(1:(n + 1), - 2 + 1:(n + 1)), ncol = 2)

# populate those diagonales
A[A_diag] <- 1/2
A[A_diag_1[A_diag_1[,2] <= n + 1,]] <- 1/4
A[A_diag_minus_1[A_diag_minus_1[,2] >= 1,]] <- 1/6
A[A_diag_minus_2[A_diag_minus_2[,2] >= 1,]] <- 1/12

# create starting values
A[1, 1] <- 3/4
A[2, 1] <- 1/4

# calculate pA
pA <- A
for (i in 1:10000){ pA <- pA %*% A }

This returns

pA[1, 1:3]
#> [1] 0.2087122 0.1651514 0.1306823

For the calulation of pA you could use the expm package to speed things up:

library(expm)

pB <- A %^% 10000

pB[1,1:3]
#> [1] 0.2087122 0.1651514 0.1306823

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I fix setOnItemClickListener for 2 gridviews that make the wrong result?

How can I reshape a matrix in base R without a for loop?

I need 10 result with loop but I can't make it I can only get 4 result how I can fix my loop

Why split() in R split matrix into vector and how can I get the matrix result?

How can I put matrix R in a loop and generate new values of matrix R>

How can I fix this code with unstoppable loop?

How can I fix the issue with this for loop?

How can I fix the error 'x' must be a numeric matrix with the command heatmap() in R?

How can generate n matrix with loop in R

Why did I get the wrong output and how can I fix this?

How can I count the result of a for each loop?

How can I code a matrix within a matrix using a loop?

How can I fix lines broken in wrong places?

How can I fix matplotlib detecting wrong font weights?

How can I fill a matrix with a repeat loop and delete columns by condition in R?

How can I get a max value of each column in matrix using For Loop in R

How can i fill this matrix (then generalize it to an 3D-array) whit a loop and using conditions too? in R

How can I fix the result which it does not respond?

My result is not truly random; How can I fix this?

How Can I fix This Password Generator Code To Print The Desired Result

How can I fix this issue in R with webscraping?

Why java += get wrong result, and how can I prevent this?

How can I merge a result set from a matrix table?

How can I fix this code, I keep receiving an infinite loop?

how can I improve this confusion matrix in R?

How can I fix this while loop in my first python calculator?

How can I fix this game serialization infinite loop in libGdx?

How can I fix the slowness of my pygame event loop?

how can I fix this loop for my dice rolling problem?