---
title: "Math 339SP -- Absorption"
author: "Tim Chumley"
date: "Spring 2022"
output:
pdf_document: default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tinytex)
library(expm)
library(pracma)
```
## Absorption probabilities
The commands below show you how to compute the absorption probabilities
in the mouse trap and cheese worksheet. Notice it can be helpful to give
names to the rows and columns of your matrices.
```{r}
Q = matrix(c(0, 1/2, 0, 1/2, 0, 0, 0,
1/3, 0, 1/3, 0, 1/3, 0, 0,
0, 1/2, 0, 0, 0, 0, 0,
1/2, 0, 0, 0, 0, 1/2, 0,
0, 1/3, 0, 0, 0, 0, 1/3,
0, 0, 0, 1, 0, 0, 0,
0, 0, 0, 0, 1/2, 0, 0), nrow = 7, ncol = 7, byrow = T)
colnames(Q) = c("A", "B", "C", "D", "E", "G", "H")
rownames(Q) = colnames(Q)
R = matrix(c(0, 0,
0, 0,
1/2, 0,
0, 0,
1/3, 0,
0, 0,
0, 1/2), nrow = 7, ncol = 2, byrow = T)
rownames(R) = colnames(Q)
colnames(R) = c("F", "I")
solve(eye(7) - Q) %*% R # full matrix of absorption probabilities
(solve(eye(7) - Q) %*% R)["A", "I"] # probability of absorption at I, given starting at A
solve(eye(7) - Q)["A", "A"] # F_AA gives expected number of visits to A before absorption given X_0 = A
sum(solve(eye(7) - Q)["A", ]) # sum over the Ath row of (I-Q)^(-1)
```