19th Ave New York, NY 95822, USA

统计代写| Matrix calculations stat代写

统计代考

$11.6 \mathrm{R}$
Matrix calculations
Let’s do some calculations for the 4-state Markov chain in Example 11.1.5, as an example of working with transition matrices in R. First, we need to specify the transition matrix $Q$. This is done with the matrix command: we type in the entries
522
of the matrix, row by row, as a long vector, and then we tell $\mathrm{R}$ the number of rows and columns in the matrix (nrow and ncol), as well as the fact that we typed in the entries by row (byrow=TRUE):
$Q<-\operatorname{matrix}(c(1 / 3,1 / 3,1 / 3,0,$,
\begin{aligned} &(1 / 3,1 / 3,1 / 3,0, \ &0,0,1 / 2,1 / 2, \ &0,1,0,0, \ &1 / 2,0,0,1 / 2), \mathrm{nrow}=4, \mathrm{ncol}=4, \text { byrow=TRUE) } \end{aligned}
To obtain higher order transition probabilities, we can multiply $Q$ by itself repeatedly. The matrix multiplication command in $\mathrm{R}$ is $\% * \%$ (not just *). So
$Q 2<-Q \% * \%$
$Q 3<-Q 2 \% * \%$
$Q 4<-Q 2 \% * \%$ Q 2
Q5 <- Q3 \%*\% Q2
produces $Q^{2}$ through $Q^{5}$. If we want to know the probability of going from state 3 to state 4 in exactly 5 steps, we can extract the $(3,4)$ entry of $Q^{5}$ :
Q5 $[3,4]$
This gives $0.229$, agreeing with the value $11 / 48$ shown in Example 11.1.5. can use the command $Q \% \% \mathrm{n}$ after installing and loading the expm package. For example, $Q \%^{\sim} \%$ yields $Q^{42}$. By exploring the behavior of $Q^{n}$ as $n$ grows, we can see Theorem 11.3.6 in action (and get a sense of how long it takes for the chain to get very close to its stationary distribution).

In particular, for $n$ large each row of $Q^{n}$ is approximately $(0.214,0.286,0.214,0.286)$, so this is approximately the stationary distribution. Another way to obtain the stationary distribution numerically is to use
eigen $(t(Q))$
to compute the eigenvalues and eigenvectors of the transpose of $Q$; then the eigenvector corresponding to the eigenvalue 1 can be selected and normalized so that the components sum to $1 .$
Gambler’s ruin
To simulate from the gambler’s ruin chain from Example $11.2 .6$, we start by deciding the total amount of money $\mathbb{N}$, the probability $p$ of gambler $A$ winning a given round, and the number of time periods nsim that we wish to simulate.
$\mathrm{N}<-10$
$\mathrm{p}<-1 / 2$
nsim <- 80

统计代考

$11.6 \mathrm{R}$

522

$Q<-\operatorname{矩阵}(c(1 / 3,1 / 3,1 / 3,0,$,
$$\开始{对齐} &(1 / 3,1 / 3,1 / 3,0, \ &0,0,1 / 2,1 / 2, \ &0,1,0,0, \ &1 / 2,0,0,1 / 2), \mathrm{nrow}=4, \mathrm{ncol}=4, \text { byrow=TRUE) } \end{对齐}$$

$Q 2<-Q \% * \%$
$Q 3<-Q 2 \% * \%$
$Q 4<-Q 2 \% * \%$ Q 2
Q5 <- Q3 \%*\% Q2

$\mathrm{N}<-10$
$\mathrm{p}<-1 / 2$
nsim <- 80