Credit for function

## Not my function 
repeated <- function(.x, .reps = 1, .f, ...) {
  # A single, finite, non-negative number of repetitions
  assertthat::assert_that(
    length(.reps) == 1,
    !is.na(.reps),
    .reps >= 0,
    is.finite(.reps))
  
  # accept purrr-style formula functions
  .f <- rlang::as_function(.f, ...)
  
  # 0 .reps
  value <- .x

  while (.reps >= 1) {
    value <- .f(value, ...)
    .reps <- .reps - 1
  }

  value
}

Build my function

ozzify <- function(p,u=NULL,n){
n=n-1
new_p <- p %>% 
    repeated(n,~.x%*%.x)
print(new_p)
newer_p <- u%*% new_p
return(newer_p)
}

Test function

##test
p=matrix(c(.5,.5,.25,.25,0,.25,.25,.5,.5),nrow=3)

u_1 <- matrix(c(1/3,1/3,1/3),ncol=3)
u_2 <- matrix(c(.2,.6,.2),ncol=3)
u_3 <- matrix(c(0,1,0),ncol=3)
ozzify(p=p,u_1,n=10)
##      [,1] [,2] [,3]
## [1,]  0.4  0.2  0.4
## [2,]  0.4  0.2  0.4
## [3,]  0.4  0.2  0.4
##      [,1] [,2] [,3]
## [1,]  0.4  0.2  0.4
ozzify(p=p,u_2,n=10)
##      [,1] [,2] [,3]
## [1,]  0.4  0.2  0.4
## [2,]  0.4  0.2  0.4
## [3,]  0.4  0.2  0.4
##      [,1] [,2] [,3]
## [1,]  0.4  0.2  0.4
ozzify(p=p,u_3,n=10)
##      [,1] [,2] [,3]
## [1,]  0.4  0.2  0.4
## [2,]  0.4  0.2  0.4
## [3,]  0.4  0.2  0.4
##      [,1] [,2] [,3]
## [1,]  0.4  0.2  0.4
  • Being that the matrices converge very rapidly, starting value makes no difference at n of 10.
  • Logically thinking about it if a matrices column has the same values, than the dot product of a vector which adds to 1(total probability=1), will always be the same value as that vector.

Apply to harvard example

harv_p=matrix(c(.8,.3,.2,.2,.4,.1,0,.3,.7),nrow=3)
ozzify(p=harv_p,n=8,u=u_2)
##           [,1]      [,2]      [,3]
## [1,] 0.5555556 0.2222222 0.2222222
## [2,] 0.5555556 0.2222222 0.2222222
## [3,] 0.5555556 0.2222222 0.2222222
##           [,1]      [,2]      [,3]
## [1,] 0.5555556 0.2222222 0.2222222