---
title: "Math 339SP -- CTMCs"
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)
```


## Matrix exponential

The commands below show you how to compute transition probabilities for
continuous time Markov chains. We have seen that, given the infinitesimal
generator $Q$ of the chain, the transition function $P(t)$ is given by
$$P(t) = e^{tQ}.$$ The R library `expm` can be used to compute the matrix
exponential.

```{r}
l = 2
m = 4/3
Q = matrix(c(-2*l, 2*l, 0,
             m, -(m+l), l,
             0, m, -m), nrow = 3, ncol = 3, byrow = T)
rownames(Q) = c(0,1,2)
colnames(Q) = rownames(Q)
expm(2.5*Q)['0','2']
expm(3*Q)['0','0']
expm(6.25*Q)['0','1']
expm(5*Q)
expm(10*Q)

Q = matrix(c(-2*l, 2*l, 0,
             m, -(m+l), l,
             0, 2*m, -2*m), nrow = 3, ncol = 3, byrow = T)
rownames(Q) = c(0,1,2)
colnames(Q) = rownames(Q)
expm(5*Q)
expm(10*Q)
```




