# Chapter 2 Exercises
In this notebook we will go through the exercises of chapter 2 of Introduction to Stochastic Processes with R by Robert Dobrow.


```R
library(expm)
```

    Loading required package: Matrix
    
    Attaching package: ‘expm’
    
    The following object is masked from ‘package:Matrix’:
    
        expm
    


## 2.1
A Markov chain has transition Matrix
$$
p=\left(\begin{array}{cc} 
0.1 & 0.3&0.6\\
0 & 0.4& 0.6 \\
0.3 & 0.2 &0.5
\end{array}\right)
$$ 
With initial distribution $\alpha =(0.2,0.3,0.5)$. Find the following:

a) $P(X_7=3|X_6=2)$

b) $P(X_9=2|X_1=2,X_5=1,X_7=3)$

c) $P(X_0=3|X_1=1)$

d) $E[X_2]$



```R
a=c(0.2,0.3,0.5)
p = matrix(c(0.1,0.3,0.6,0,0.4,0.6,0.3,0.2,0.5),ncol=3,byrow = TRUE)
p[2,3]
```


0.6



```R
(p%*%p)[3,2]
```


0.27



```R
a[3]*p[3,1]/(a[1]*p[1,1]+a[2]*p[2,1]+a[3]*p[3,1])
```


0.882352941176471



```R
e=0
pr = a%*%p%*%p
for (i in 1:ncol(pr))
    e = e+(i)*pr[1,i]
e
```


2.363


## 2.1
A Markov chain has transition Matrix
$$
p=\left(\begin{array}{cc} 
0 & 1/2&1/2\\
1 & 0& 0 \\
1/3 & 1/3 &1/3
\end{array}\right)
$$ 
With initial distribution $\alpha =(1/2,0,1/2)$. Find the following:

a) $P(X_2=1|X_1=3)$

b) $P(X_1=3,X_2=1)$

c) $P(X_1=3|X_2=1)$

d) $P(X_9=1|X_1=3, X_4=1, X_7=2)$


```R
a=c(1/2,0,1/2)
p = matrix(c(0,1/2,1/2,1,0,0,1/3,1/3,1/3),ncol=3,byrow = T)
p[3,1]
```


0.333333333333333



```R
(a%*%p)[1,3]*p[3,1]
```


0.138888888888889



```R
(a%*%p)[1,3]*p[3,1]/((a%*%p)[1,1]*p[1,1]+(a%*%p)[1,2]*p[2,1]+(a%*%p)[1,3]*p[3,1])
```


0.25



```R
(p%*%p)[2,1]
```


0


## 2.3
Consider the Wright-Fisher model with population k=3. If the initial population has one A allele, what is the probability that there are no alleles at time 3.


```R
mat = c()
k = 3
for(j in 0:k){
    vec = c()
    for(i in (0:k)){
        vec=c(vec,choose(k, i)*(j/k)**i*(1-j/k)**(k-i))
    }
    mat=c(mat,vec)
}
p=matrix(mat,nrow=k+1,byrow = T)
(p%^%3)[2,1]
```

    Loading required package: Matrix
    
    Attaching package: ‘expm’
    
    The following object is masked from ‘package:Matrix’:
    
        expm
    



0.516689529035208


## 2.4
For the Generalñ two-state chain with transformation matrix 
$$\boldsymbol{P}=\left(\begin{array}{cc} 
1-p & p\\
q & 1-q
\end{array}\right)$$

And initial distribution $\alpha=(\alpha_1,\alpha_2)$, find the following:

(a) the two step transition matrix

(b) the distribution of $X_1$

### Answer
(a)

For this case the result comes from doing the matrix multiplication once i.e. finding $\boldsymbol{P}*\boldsymbol{P}$ that is:
$$\boldsymbol{P}*\boldsymbol{P}=\left(\begin{array}{cc} 
(1-p)^2+pq & (1-p)p+(1-q)p\\
(1-p)q+(1-q)q & (1-q)^2+pq
\end{array}\right)$$

(b) 
in this case we need to take into account alpha which would be $X_0$ and then multiply with the transition matrix, then:

$\boldsymbol{\alpha}*\boldsymbol{P}$ that is:
$$\boldsymbol{\alpha}*\boldsymbol{P}=( 
(1-p)\alpha_1+q\alpha_2 , p\alpha_1+(1-q)\alpha_2)$$

## 2.5
Consider a random walk on $\{0,...,k\}$, which moves left and right with respective probabilities q and p. If the walk is at 0 it transitions to 1 on the next step. If the walk is at k it transitions to k-1 on the next step. This is calledrandom walk with reflecting bounderies. Assume that $k=3,q=1/4, p =3/4$ and the initial distribution is uniform. For the following, use technology if needed.

(a) Exhibit the transition Matrix

(b) Find $P(X_7=1|X_0=3,X_2=2,X_4=2)$

(c) Find $P(X_3=1,X_5=3)$

### Answer
(a) $$\boldsymbol{Pr}=\left(\begin{array}{cc}
0 & 1 & 0 & 0 \\
1/4 & 0 & 3/4 & 0\\
0 & 1/4 & 0 & 3/4 \\
0 & 0 & 1 & 0
\end{array}\right)$$

(b) since it is a Markov Chain, then $P(X_7=1|X_0=3,X_2=2,X_4=2)=P(X_7=1|X_4=2)=\boldsymbol{Pr}^3_{2,1}$



```R
p = matrix(c(0,1,0,0,1/4,0,3/4,0,0,1/4,0,3/4,0,0,1,0),nrow =4,byrow = T)
(p%^%3)[3,2]
```


0.296875


(c) $P(X_3=1,X_5=3)=(\alpha \boldsymbol{Pr}^3)_{1}*\boldsymbol{Pr}^2_{1,3}$


```R
a=c(1/4,1/4,1/4,1/4)
(a%*%(p%^%3))[1,2]*(p%*%p)[2,4]
```


0.103271484375


## 2.6
A tetrahedron die has four faces labeled 1,2,3 and 4. In repeated independent rolls of the die $R_0, R_1,...,$ let $X_n=max\{R_0,...,R_n\}$ be the maximum value after $n+1$ rolls, for $n\geq0$

(a) Give an intuitive argument for why $X_0, X_1, ... $ is a Markov Chain and exhibit its transition Matrix

(b) find $P(X_3 \geq 3)$

### Answer
(a) This is a Markov Chain since the value of the maximum in all the rolls does only depend on the last value given of the throw this is because the max value should not changhe based on past rolls of this given event, then $P(X_n=i|X_{n-1}=j,...)=P(X_n=i|X_{n-1}=j)$
and the transition matrix is:
$$\boldsymbol{Pr}=\left(\begin{array}{cc}
1/4 & 1/4 & 1/4 & 1/4 \\
0 & 1/2 & 1/4 & 1/4 \\
0 & 0 & 3/4 & 1/4 \\
0 & 0 & 0 & 1
\end{array}\right)$$


(b) We know that the tetrahedron has unoiform probability to get the initial value, then $\alpha=(1/4,1/4,1/4,1/4)$
then we want $\alpha*\boldsymbol{Pr}^3$


```R
p = matrix(c(1/4,1/4,1/4,1/4,0,1/2,1/4,1/4,0,0,3/4,1/4,0,0,0,1),nrow=4,byrow = T)
a=c(1/4,1/4,1/4,1/4)
sum((a%*%(p%^%3))[1,-(1:2)])
```


0.9375


## 2.7
Let $X_0, X_1,...$ be a Markov chain with transition matrix $P$. Let $Y_n=X_{3n}$, for $n = 0,1,2,...$ Show that $Y_0,Y_1,...$ is a Markov chain and exhibit its transition Matrix.

### Answer
Let's unravel $P(Y_k|Y_{k-1},...,Y_0)$

$$P(Y_k|Y_{k-1},...,Y_0)=P(X_{3k}|X_{3k-3},...,X_0)$$

But since $X_0, X_1,...$ is a Markov Chain, then $P(X_{3k}|X_{3k-3},...,X_0) = P(X_{3k}|X_{3k-3}) = P(Y_k|Y_{k-1})$
This means that $Y_0,Y_1,...$ is also a Markov chain.

and then the transition matrix for Y is $P_Y=P_X^3$

## 2.8
Give the Markov transition matrix for a random walk on the weighted graph in the next figure:
<div>

</div>

### Answer 
$$\boldsymbol{Pr}=\left(\begin{array}{cc}
0 & 1/3 & 0 & 0 & 2/3 \\
1/10 & 1/5 & 1/5 & 1/10 & 2/5 \\
1/2 & 1/3 & 0 & 1/6 & 0\\
0 & 1/2 & 1/2 & 0 & 0\\
1/3 & 2/3 & 0 & 0 & 0
\end{array}\right)$$

## 2.8
Give the Markov transition matrix for a random walk on the weighted graph in the next figure:
<div>

</div>

### Answer 
$$\boldsymbol{Pr}=\left(\begin{array}{cc}
0 & 0 & 3/5 & 0 & 2/5 \\
1/7 & 2/7 & 0 & 0 & 4/7 \\
0 & 2/9 & 2/3 & 1/9 & 0\\
0 & 1 & 0 & 0 & 0\\
3/4 & 0 & 0 & 1/4 & 0
\end{array}\right)$$

## 2.10
Consider a Markov Chain with transition Matrix

$$\boldsymbol{Pr}=\left(\begin{array}{cc}
0 & 3/5 & 1/5 & 2/5 \\
3/4 & 0 & 1/4 & 0 \\
1/4 & 1/4 & 1/4 & 1/4\\
1/4 & 0 & 1/4 & 1/2
\end{array}\right)$$
(a) Exhibit the directed, weighted transition graph for the chain

(b) The transition graph for this chain can be given as a weighted graph without directed edges. Exhibit the graph

(a)
<div>

</div>

(b)
<div>

</div>

## 2.11
You start with five dice. Roll all the dice and put aside those dice that come up 6. Then roll the remaining dice, putting aside thise dice that come up 6. and so on. Let $X_n$ be the number of dice that are sixes after n rolls. 

(a)  Describe the transition matrix $\boldsymbol{Pr}$ for this Markov chain

(b) Find the probability of getting all sixes by the third play.

(c) what do you expect $\boldsymbol{Pr}^{100}$ to look like? Use tech to confirm your answer **(Good that we are doing this in jupyter notebook haha)**

(a) Note that the space for $X_n$ is $0,1,2,3,4,5$

$$\boldsymbol{Pr}=\left(\begin{array}{cc}
1 & 0 & 0 & 0 & 0 & 0 \\
1/6 & 5/6 & 0 & 0 & 0 & 0\\
\frac{1^2}{6^2}{2\choose 2} & \frac{5*1}{6^2}{2\choose 1} & \frac{5^2}{6^2}{2\choose 0} & 0 & 0 & 0\\
\frac{1^3}{6^3}{3\choose 3} & \frac{5*1^2}{6^3}{3\choose 2} & \frac{5^2*1}{6^3}{3\choose 1} & \frac{5^3}{6^3}{3\choose 0} & 0 & 0\\
\frac{1^4}{6^4}{4\choose 4} & \frac{5*1^3}{6^4}{4\choose 3} & \frac{5^2*1^2}{6^4}{4\choose 2} & \frac{5^3*1}{6^4}{4\choose 1} & \frac{5^4}{6^4}{4\choose 0} & 0\\
\frac{1^5}{6^5}{5\choose 5} & \frac{5*1^4}{6^5}{5\choose 4} & \frac{5^2*1^3}{6^5}{5\choose 3} & \frac{5^3*1^2}{6^5}{5\choose 2} & \frac{5^4*1}{6^5}{5\choose 1} & \frac{5^5}{6^5}{5\choose 0}\\
\end{array}\right) = \left(\begin{array}{cc}
1 & 0 & 0 & 0 & 0 & 0 \\
\frac{1}{6} & \frac{5}{6} & 0 & 0 & 0 & 0\\
\frac{1}{36} & \frac{10}{36} & \frac{25}{36} & 0 & 0 & 0\\
\frac{1}{216} & \frac{15}{216} & \frac{75}{216} & \frac{125}{216} & 0 & 0\\
\frac{1}{1296} & \frac{20}{1296} & \frac{150}{1296} & \frac{500}{1296} & \frac{625}{1296} & 0\\
\frac{1}{7776} & \frac{25}{7776} & \frac{250}{7776} & \frac{1250}{7776} & \frac{3125}{7776} & \frac{3125}{7776}\\
\end{array}\right)$$

It is interesting that the distribution when $X_n=i$ is the components of the binomial expansion $(1/6+5/6)^i$

(b)


```R
p = matrix(c(1, 0, 0, 0, 0, 0,
                      1/6, 5/6, 0, 0, 0, 0, 1/36, 10/36, 25/36, 0, 0, 0, 
                      1/216, 15/216, 75/216, 125/216, 0, 0, 
                      1/1296, 20/1296, 150/1296, 500/1296, 625/1296, 0,
                      1/7776, 25/7776, 250/7776, 1250/7776 ,3125/7776, 3125/7776),nrow=6,byrow=T)
(p%^%3)[6,1]
```


0.0132720560113747


(c) I would expect that there is basically 100 percent posibility that there are 0 dices left without regarding how many dices you started with.


```R
round(p%^%100)
```


<table>
<tbody>
	<tr><td>1</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td></tr>
	<tr><td>1</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td></tr>
	<tr><td>1</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td></tr>
	<tr><td>1</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td></tr>
	<tr><td>1</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td></tr>
	<tr><td>1</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td></tr>
</tbody>
</table>



## 2.12
two urns contain k balls each. At the beginning of the experiment the urn on the left countains k red balls and the one on the right contains k blue balls. At each step, pick a ball at random from each urn and excange them. Let $X_n$ be the number of blue balls left in the urn on the left (Note that $X_0=0, X_1 = 1$). Argue the process is a Markov Chain. Find the transition matrix. This model is called the Bernoulli - Laplace process. 

### Answer
This process has to be a random process because the only thing that matters at step n is to know how many blue balls are in the urn on the left. This means that even if you know all the history only the last state is the one that matters to know the probability for the next step.

$$P = \left(\begin{array}{cc}
0 & 1 & 0 & ... &0 & 0 & 0 \\
\frac{1}{k} & 0 & \frac{k-1}{k} & ... & 0 & 0 & 0\\
0 & \frac{2}{k} & 0 & ... & 0 & 0 & 0\\
... & ... & ... & ... & ... & ... & ...\\
0 & 0 & 0 & ... & \frac{k-1}{k} & 0 & \frac{1}{k}\\
0 & 0 & 0 & ... & 0 & 1 & 0\\
\end{array}\right)$$

## 2.13
see the move-to-front process in Example 2.10. Here is anorher way to organize the bookshelf. when a book is returned it is put bacl on the library shelf one position forward from where it was originally. If the book at the fron of the shelf is returned it is put back at the front of the shelf. This, if the order of books is (a,b,c,d,e) and the book d is picjed, the new order is (a,b,d,c,e). This reorganization method us called the *transposition* or *move-ahead-1* scheme. Give the transition matrix for the transportation scheme for a shelf with three books.

### Answer
remember the states are $(abc, acb, bac,bca, cab, cba)$
$$P = \left(\begin{array}{cc}
0 & p_c & p_b &p_a & 0 & 0 \\
p_b & 0 & 0 & 0 & p_c & p_a\\
p_a & p_b & 0 & p_c & 0 & 0\\
0 & 0 & p_a & 0 & p_b & p_c\\
p_c & p_a & 0 & 0 & 0 & p_b\\
0 & 0 & p_c & p_b & p_a & 0
\end{array}\right)$$

## 2.14
There are k songs in Mary's music player. The player is set to *Shuffle* mode, which plays songs uniformly at random, sampling with replacement. Thus, repeats are possible. Let $X_n$ denote the number of *unique* songs that have been heard after the nth play. 

(a) show that $X_0,X_1, ...$ is a Markov chain and give the transition matrix

(b) if Mary has four songs on her music player, find the probability that all songs are heard after six plays.

### Answer
(a) 
imagine that you have $P(X_n|X_{n-1},...,X_0)$ note that $X_{n+1}=max\{\xi_{n+1},X_n\}$ where $\xi_{n+1}$ is the result of the n+1 and it is independent to all $X_i$, $0\leq i\leq n$, then this means that $X_0,...,X_n$ is a Markov chain with transition matrix:
$$P = \left(\begin{array}{cc}
1/k & (k-1)/k & 0 & ... &0 & 0 & 0 \\
0 & 2/k & (k-2)/k & ... & 0 & 0 & 0\\
0 & 0 & 3/k & ... & 0 & 0 & 0\\
... & ... & ... & ... & ... & ... & ...\\
0 & 0 & 0 & ... & 0 & (k-1)/k & 1/k\\
0 & 0 & 0 & ... & 0 & 0 & 1\\
\end{array}\right)$$

## 2.15
Assume that $X_0,X_1,...$ is a two state Markov chain in $\mathcal{S}=\{0,1\}$ with transition matrix:

$$P=\left(\begin{array}{cc}
1-p & p  \\
q & 1-q 
\end{array}\right)$$

The present state of the chain only depends on the previous state. We can create a bivariate process that looks back two time periods by the following contruction. Let $Z_n=(X_{n-1},X_n)$, for $n\geq1$. The sequence $Z_1,Z_2,...$ is a Markov chain with state space $\mathcal{S}X\mathcal{S}={(0,0),(1,0),(0,1),(1,1)}$ Give the transition matrix of the new chain. 

### Answer
$$P = \left(\begin{array}{cc}
1-p & p & 0 & 0 \\
0 & 0 & q & 1-q \\
1-p & p & 0 & 0 \\
0 & 0 & q & 1-q 
\end{array}\right)$$

## 2.16
Assume that  $P$  is a stochastic matrix with equal rows. Show that $P^n=P$, for all $n\geq1$

### Answer
let's then see $P$ as:
$$P = \left(\begin{array}{cc}
p_1 & p_2 & ... & p_k \\
p_1 & p_2 & ... & p_k \\
p_1 & p_2 & ... & p_k \\
... & ... & ... & ... \\
p_1 & p_2 & ... & p_k \\
p_1 & p_2 & ... & p_k \\
\end{array}\right)$$

First let's calculate $P^2$, then

$$P^2 = \left(\begin{array}{cc}
p_1*p_1+p_2*p_1+...+p_k*p_1 & p_1*p_2+p_2*p_2+...+p_k*p_2 & ... & p_1*p_k+p_2*p_k+...+p_k*p_k \\
p_1*p_1+p_2*p_1+...+p_k*p_1 & p_1*p_2+p_2*p_2+...+p_k*p_2 & ... & p_1*p_k+p_2*p_k+...+p_k*p_k \\
p_1*p_1+p_2*p_1+...+p_k*p_1 & p_1*p_2+p_2*p_2+...+p_k*p_2 & ... & p_1*p_k+p_2*p_k+...+p_k*p_k \\
... & ... & ... & ... \\
p_1*p_1+p_2*p_1+...+p_k*p_1 & p_1*p_2+p_2*p_2+...+p_k*p_2 & ... & p_1*p_k+p_2*p_k+...+p_k*p_k \\
p_1*p_1+p_2*p_1+...+p_k*p_1 & p_1*p_2+p_2*p_2+...+p_k*p_2 & ... & p_1*p_k+p_2*p_k+...+p_k*p_k \\
\end{array}\right)=
\left(\begin{array}{cc}
p_1*(p_1+p_2+...+p_k) & ... & p_k(p_1+p_2+...+p_k) \\
p_1*(p_1+p_2+...+p_k) & ... & p_k(p_1+p_2+...+p_k) \\
p_1*(p_1+p_2+...+p_k) & ... & p_k(p_1+p_2+...+p_k) \\
... & ... & ...  \\
p_1*(p_1+p_2+...+p_k) & ... & p_k(p_1+p_2+...+p_k) \\
p_1*(p_1+p_2+...+p_k) & ... & p_k(p_1+p_2+...+p_k) \\
\end{array}\right)=
\left(\begin{array}{cc}
p_1*(1) & ... & p_k(1) \\
p_1*(1) & ... & p_k(1) \\
p_1*(1) & ... & p_k(1) \\
... & ... & ...  \\
p_1*(1) & ... & p_k(1) \\
p_1*(1) & ... & p_k(1) \\
\end{array}\right) = P$$

Then let's see what happens for $P^3$, then 
$$P^3=P^2*P=P*P=P^2=P$$

Assume it is true for $P^n$ and see what happens at $P^{n+1}$
$$P^{n+1}=P^n*P=P*P=P^2=P$$
Then this means that  $P^n=P$, for all $n\geq1$

## 2.17
Let **$P$** be the stochastic matrix. Show that $\lambda = 1$ is an eigenvalue of **$P$**. What is the associated eigenvector?

## Answer 
Let's first think that it is clear that the rows all sum to one, and remember that an eigenvector asociated to an eigen value looks of the form:
$$A\overline{x}=\lambda \overline{x}$$

it is easy to note that the eigenvector we are looking at is the vector $\overline{x}=(1,1,...,1)^T$ this means that the eigenvalue asociated to this is also one

we can check this by doing the multiplication. Say we have
$$A= \left(\begin{array}{cc}
p_{1,1} & p_{1,2} & ... & p_{1,k} \\
p_{2,1} & p_{2,2} & ... & p_{2,k} \\
... & ... & ... & ... \\
p_{k,1} & p_{k,2} & ... & p_{k,k} \\
\end{array}\right)$$
where A is an stochastic matrix. Then:
$$Ax= \left(\begin{array}{cc}
p_{1,1} & p_{1,2} & ... & p_{1,k} \\
p_{2,1} & p_{2,2} & ... & p_{2,k} \\
... & ... & ... & ... \\
p_{k,1} & p_{k,2} & ... & p_{k,k} \\
\end{array}\right)\left(\begin{array}{cc}
1 \\
1 \\
...  \\
1\\
\end{array}\right)=
\left(\begin{array}{cc}
p_{1,1} + p_{1,2} + ... + p_{1,k} \\
p_{2,1} + p_{2,2} + ... + p_{2,k} \\
...  \\
p_{k,1} + p_{k,2} + ... + p_{k,k} \\
\end{array}\right)=
\left(\begin{array}{cc}
1 \\
1 \\
...  \\
1\\
\end{array}\right)$$

The way to prove this is to remember that if $A$ is an square matrix, then the eigenvalues of A are the same as its transpose's. Then, let's check if 1 is an eigenvalue by doing $det(A^T-\lambda I)$ where $\lambda$ is the eigenvalue (in this case 1) and $I$ is the identity matrix, when doing this we get that:
$$A-I= \left(\begin{array}{cc}
p_{1,1} -1 & p_{1,2} & ... & p_{1,k} \\
p_{2,1} & p_{2,2}- 1 & ... & p_{2,k} \\
... & ... & ... & ... \\
p_{k,1} & p_{k,2} & ... & p_{k,k}-1 \\
\end{array}\right)$$

And since we are dealing with the transpose, then if we sum all rows to the first row (i.e. $R_1 -> R_1+R_2+...+R_k$), then all values in the first row are zero, which means that the matrix is linearly dependand and $det(A^T- 1I)=0$, which means that 1 is an eigenvalue for $A$

## 2.18
A stochastic matrix is called *doubly* stochastic if its columns sum to 1. Let $X_0,X_1,...$ be a Markov chain on $\{ 1,...,k \}$ with doubly stochastic transition matrix and initial distribution that is uniform on $\{ 1,...,k \}$. Show that the distribution of $X_n$ is uniform on $\{ 1,...,k \}$, for all $n\geq0$

### Answer
Let's remember that then the distribution at $X_n$ is $\alpha P^n$, let's see what happens at $n=1$

$n=1$
$$X_1=\alpha P = \left(\begin{array}{cc}
1/k &1/k&...& 1/k
\end{array}\right)\left(\begin{array}{cc}
p_{1,1} & p_{1,2} & ... & p_{1,k} \\
p_{2,1} & p_{2,2} & ... & p_{2,k} \\
...  \\
p_{k,1} & p_{k,2} & ... & p_{k,k} \\
\end{array}\right)=\left(\begin{array}{cc}
p_{1,1}*1/k + p_{2,1}*1/k + ... + p_{k,1}*1/k \\
p_{1,2}*1/k + p_{2,2}*1/k + ... + p_{k,2}*1/k \\
...  \\
p_{1,k}*1/k + p_{2,k}*1/k + ... + p_{k,k}*1/k \\
\end{array}\right)^T=\left(\begin{array}{cc}
(p_{1,1} + p_{2,1} + ... + p_{k,1})*1/k\\
(p_{1,2} + p_{2,2} + ... + p_{k,2})*1/k \\
...  \\
(p_{1,k} + p_{2,k} + ... + p_{k,k})*1/k \\
\end{array}\right)^T = \left(\begin{array}{cc}
(1)*1/k\\
(1)*1/k \\
...  \\
(1)*1/k \\
\end{array}\right)^T=\alpha
$$
This last step is due to the matrix being double stochastic

Now let's $n=2$
$$X_1=\alpha P^2 = (\alpha P)*P=\alpha*p=\alpha$$

Then we assume it happens for $n$, let's see then for $n+1$
$$X_{n+1}=\alpha P^{n+1} = (\alpha P^n)*P=\alpha*p=\alpha$$

## 2.19
Let **$P$** be the transition matrix of a Markov chain on $k$ states. Let **$I$** denote the $kXk$ identity matrix. consider the matrix

$$\boldsymbol{Q}=(1-p)\boldsymbol{I}+p\boldsymbol{P}\text{  , for }0<p<1$$
show that **$Q$** is a stochastic matrix. Give the probabilistic interpretation for the dynamics of a markov chain governed by the **$Q$** matrix in terms of the original Markov chain.

### Answer
Let's construct $(1-p)\boldsymbol{I}+p\boldsymbol{P}$ and we need to check for two main things. First, $0\leq p_{i,j}\leq 1$, this is clear, since $P$ already holds this property and you are just multiplying it by two numbers that sum 1 and are less than 1. Now we need to check if $\sum_j p_{i,j}=1$, then seing the matrix:
$$(1-p)\boldsymbol{I}+p\boldsymbol{P}=\left(\begin{array}{cc}
p_{1,1}*p +(1-p) & p_{1,2}*p & ... & p_{1,k}*p \\
p_{2,1}*p & p_{2,2}*p+(1-p) & ... & p_{2,k}*p \\
... & ... & ... & ... \\
p_{k,1}*p & p_{k,2}*p & ... & p_{k,k}*p+(1-p) \\
\end{array}\right)$$
we have for row i 

$p_{i,1}*p + p_{1,2}*p + ...+ p_{i,i}p+(1-p)+...+ p_{1,k}*p = p*(p_{i,1} + p_{1,2} + ...+ p_{1,k})+1-p=p*(1)+1-p=1 $

then $Q$ is a stochastic matrix.

As I see it this matrix $Q$ the only value it is modifying is the probability of transitioning from state $i$ to state $i$. so this linear combination could be used when you want to give more probability to return to the same state you are at step $n$. you can notice that the probability of going back to state $i$ when you are at state $i$ is due to the fact that is the only other state that has a real value greater than zero being added to it.


## 2.20
Let $X_0,X_1,...$ be a Markov chain with transition matrix 

$$P=\left(\begin{array}{cc}
0 & 1 & 0 \\
0 & 0 & 1 \\
p & 1-p & 0
\end{array}\right)$$
for $0<p<1$. Let $g$ be the function defined by:

$$
  g(x) =
  \begin{cases}
                                   0 & \text{if $x=1$} \\
                                   1 & \text{if $x=2,3$} 
  \end{cases}
$$

Let $Y_n=g(X_n)$ for $n\geq0$. Show that $Y_0,Y_1,...$ is not a Markov chain

### Answer

Let's proof this by contradiction. if we assume $Y_n$ is a Markov chain, then $P(Y_n|Y_{n-1},...,Y_0)=P(Y_n|Y_{n-1})$, but let's check this next two scenarios

## 2.21
Let $P$ and $Q$ be two transition matricies on the same state space processes, both started in some initial state $i$.

In the process $\#1$ a coin is flipped, then if it lands head, the process unfolds according to $P$. If it lands tails, the process unfolds according to $Q$. 

In process $\#2$, at each step a coin is flipped. If it lands heads, the next step is chosen from $P$. If it lands tails, the next step is chosen from $Q$.

Thus, in $\#1$, one coin is initially flipped, which governsthe entire evolution of the process and in $\#2$ a coin is flipped at each step to decide the next step in the process.

Decide whether either of these processes is a Markov chain, if it is exhibit its transition matrix, if not, then explain why not.

### Answer
$#1$

For example 1 we have that it is not a Markov chain and the reason for this is because in the first step the this means that the distribution of $X_0$ is different from the distribution of $X_1$ which means that we cannot have a transition matrix for all states, although it is nice to see that once the heads is chosen, then the next steps do follow a Markov chain with either $P$ or $Q$ as their matrices.

$#2$

For example 2 we have that it is a Markov chain and the reason for this is because we can define the transition Matrix $M=0.5P+0.5Q$ as the new matrix that follows this process. 


```R
## This could be a class.
ChooseNextStep<-function(x, mat){
    tmp_x = x%*%mat
    return(list("tmp_x" = tmp_x, "xi" = sample(0:(length(tmp_x)-1),size=1,prob=tmp_x)))
}

Simulation_21<-function(case, P=matrix(c(0.2,0.8,0.2,0.8),nrow=2,byrow=T),
                        Q=matrix(c(0.8,0.2,0.8,0.2),nrow=2,byrow=T), simulations=1000, steps = 5, p=0.5){
    "
    This is a function for the simulation stated in 2.21 from the book, it receives the 
    two matrices and the case you are talking for either one or two and calculates its state matrices for the first
    steps we assume initial distributions uniform
    "
    rows = nrow(P)
    cols = ncol(P)
    initial_dis = rep(1/cols, cols)
    sim = matrix(NA, nrow=simulations, ncol=steps)
    for(i in 1:simulations){
        res = c()
        result = list("tmp_x"=initial_dis)
        for(j in 1:steps){
            if(case == 1){
                if(j == 1){
                    coin = runif(1)
                    if(coin < p){
                        # Follows P
                        distribution = "P"
                        result = ChooseNextStep(result$tmp_x,P)
                    }
                    else{
                        distribution = "Q"
                        result = ChooseNextStep(result$tmp_x,Q)
                    }
                }else{
                    if(distribution=="P"){
                        result = ChooseNextStep(result$tmp_x,P)
                    }else{
                        result = ChooseNextStep(result$tmp_x,Q)
                    }
                }
            }else if(case == 2){
                coin = runif(1)
                if(coin < p){
                    # Follows P
                    result = ChooseNextStep(result$tmp_x,P)
                }else{
                    result = ChooseNextStep(result$tmp_x,Q)
                }
            }
            res=c(res,result$xi)
        }
        sim[i,]=res
    }
    return(sim)
}

CalcMatrixForStep<-function(sim, step=1){
    res0 = sim[sim[,(step-1)]==0,step]
    res1 = sim[sim[,(step-1)]==1,step]
    counts0 = table(res0)
    counts1 = table(res1)
    prob0 = as.matrix(counts0)[,1]/length(res0)
    prob1 = as.matrix(counts1)[,1]/length(res1)
    return(matrix(c(prob0,prob1),nrow=2, byrow=T))
}

GetTransitionMatrix<-function(results){
    matrices = vector("list", ncol(results)-1)
    for(i in 2:ncol(results)){
        matrices[[i-1]]=CalcMatrixForStep(results,i)
    }
    return(matrices)
}
```


```R
sim = Simulation_21(1, simulations=100000)
elems=GetTransitionMatrix(sim)
for(elem in 1:length(elems)){
    print(elems[[elem]])
}
```

              [,1]      [,2]
    [1,] 0.6780377 0.3219623
    [2,] 0.3194483 0.6805517
              [,1]      [,2]
    [1,] 0.6770189 0.3229811
    [2,] 0.3195829 0.6804171
              [,1]      [,2]
    [1,] 0.6781104 0.3218896
    [2,] 0.3224084 0.6775916
              [,1]      [,2]
    [1,] 0.6824933 0.3175067
    [2,] 0.3197514 0.6802486



```R
sim = Simulation_21(1, simulations=100000)
elems=GetTransitionMatrix(sim)
for(elem in 1:length(elems)){
    print(elems[[elem]])
}
```

              [,1]      [,2]
    [1,] 0.6791643 0.3208357
    [2,] 0.3200032 0.6799968
              [,1]      [,2]
    [1,] 0.6815790 0.3184210
    [2,] 0.3216778 0.6783222
              [,1]      [,2]
    [1,] 0.6787224 0.3212776
    [2,] 0.3204197 0.6795803
              [,1]      [,2]
    [1,] 0.6801432 0.3198568
    [2,] 0.3227839 0.6772161



```R
sim = Simulation_21(2, simulations=100000)
elems=GetTransitionMatrix(sim)
for(elem in 1:length(elems)){
    print(elems[[elem]])
}
```

              [,1]      [,2]
    [1,] 0.4968499 0.5031501
    [2,] 0.4951701 0.5048299
              [,1]      [,2]
    [1,] 0.4975908 0.5024092
    [2,] 0.4989583 0.5010417
              [,1]      [,2]
    [1,] 0.5009633 0.4990367
    [2,] 0.5030894 0.4969106
              [,1]      [,2]
    [1,] 0.5004283 0.4995717
    [2,] 0.5001506 0.4998494



```R
sim = Simulation_21(2, simulations=100000)
elems=GetTransitionMatrix(sim)
for(elem in 1:length(elems)){
    print(elems[[elem]])
}
```

              [,1]      [,2]
    [1,] 0.5018999 0.4981001
    [2,] 0.4975168 0.5024832
              [,1]      [,2]
    [1,] 0.4973985 0.5026015
    [2,] 0.4989006 0.5010994
              [,1]      [,2]
    [1,] 0.4994078 0.5005922
    [2,] 0.4992926 0.5007074
              [,1]      [,2]
    [1,] 0.5010313 0.4989687
    [2,] 0.5040048 0.4959952


## 2.22 
Show by induction:

(a) $1+3+5+...+(2n-1)=n^2$

(b) $1^2+2^2+...+n^2=n(n+1)(2n+1)/6$

(c) for all real $x>-1$, $(1+x)^n\geq 1+nx$

### Answer
$#a$

For $n=1$

$$
\begin{align}
2(1)-1 &= 1 \\&=1^2 
\end{align}$$

Assume it works for $n-1$

$$
\begin{align}
1+3+...+(2n-3)  &= (n-1)^2 \\
                &=n^2 -2n+1
\end{align}$$

Then for $n$
$$
\begin{align}
1+3+...+(2n-3)+(2n-1)  &= n^2 -2n+1 +2n-1\\
                &=n^2 
\end{align}$$

Therefore is proved.

$#b$

For $n=1$

$$
\begin{align}
1(1+1)(2*1+1)/6 &= 6/6 \\&=1^2 
\end{align}$$

Assume it works for $n-1$

$$
\begin{align}
1^2+2^2+...+(n-1)n^2&=(n-1)(n)(2n-1)/6
\end{align}$$

Then for $n$
$$
\begin{align}
1^2+2^2+...+n^2 &=\frac{(n-1)(n)(2n-1)}{6}+n^2\\
                &=\frac{2n^-3n^2+n+6n^2}{6}\\
                &=\frac{n(2n^2+3n+1)}{6}\\
                &=\frac{n(n+1)(2n+1)}{6}
\end{align}$$

Therefore is proved.

$#c$


For this is important to notice that for $x>-1$ means that $(1+x)>0$ for all $x$. Now let's see what happens for $n=1$

$$
\begin{align}
(1+x)^1 &= (1+1*x)\\
1+x &\geq 1+x
\end{align}$$

Assume it works for $n-1$

$$
\begin{align}
(1+x)^{n-1}&\geq1+(n-1)x
\end{align}$$

Then for $n$
$$
\begin{align}
(1+x)^{n} &= (1+x)^{n-1}*(1+x)\\
          &\geq 1+(n-1)x*(1+x)\\
\\ \text{this last step because $1+x > 0$} \\ \\
1+(n-1)x*(1+x)    &=1+(n-1)x+x+(n-1)x^2\\
                  &=1+n*x+ (n-1)x^2\\
\\ \text{but since $(n-1)x^2\geq 0$ for all $x>1$}\\ \\
1+n*x+ (n-1)x^2 &\geq 1+n*x \\
\\ \text{=>} \\ \\
(1+x)^{n} &\geq 1+n*x+ (n-1)x^2 \\
          &\geq 1+n*x \\
\\ \text{by transitivity relation} \\ \\
(1+x)^{n} &\geq 1+n*x \\
\end{align}$$

Therefore is proved.


## 2.23
Simulate the first 20 letters (vowel/consonant) of the Pushkin poem Markov chain of Example 2.2.

$$P=\left(\begin{array}{cc}
0.175 & 0.825 \\
0.526 & 0.474
\end{array}\right)$$

### Answer


```R
PushkinLetters<-function(P=matrix(c(0.175,0.825,0.526,0.474),nrow=2, byrow = T), 
                         init= c(8.638/20, 11.362/20),simulations=10){
    results = list()
    for(i in 1:simulations){
        results=c(results, list(OneSimulation(init=init, P=P)))
    }
    return(results)
}
OneSimulation<-function(steps = 20, init, P){
    sim = c()
    sim = samp(sim, init)
    for(i in 2:steps){
        sim=samp(sim, c(P[sim[i-1]+1,]))
    }
    return(parseSimulation(sim))
}
samp<-function(simlist, dist){
    vowel = 0
    consonant = 1
    if (runif(1)<dist[1]){
        simlist=c(simlist,vowel)
    }else{
        simlist=c(simlist,consonant)
    }
    return(simlist)
}
parseSimulation<-function(toParse){
    toParse[toParse==1]="Consonant"
    toParse[toParse==0]="vowel"
    return(toParse)
}
    
    
```


```R
PushkinLetters()
```


<ol>
	<li><ol class=list-inline>
	<li>'vowel'</li>
	<li>'vowel'</li>
	<li>'Consonant'</li>
	<li>'Consonant'</li>
	<li>'vowel'</li>
	<li>'Consonant'</li>
	<li>'vowel'</li>
	<li>'Consonant'</li>
	<li>'vowel'</li>
	<li>'Consonant'</li>
	<li>'vowel'</li>
	<li>'Consonant'</li>
	<li>'vowel'</li>
	<li>'Consonant'</li>
	<li>'Consonant'</li>
	<li>'vowel'</li>
	<li>'Consonant'</li>
	<li>'Consonant'</li>
	<li>'Consonant'</li>
	<li>'vowel'</li>
</ol>
</li>
	<li><ol class=list-inline>
	<li>'Consonant'</li>
	<li>'vowel'</li>
	<li>'Consonant'</li>
	<li>'Consonant'</li>
	<li>'vowel'</li>
	<li>'Consonant'</li>
	<li>'Consonant'</li>
	<li>'vowel'</li>
	<li>'Consonant'</li>
	<li>'Consonant'</li>
	<li>'vowel'</li>
	<li>'Consonant'</li>
	<li>'vowel'</li>
	<li>'Consonant'</li>
	<li>'vowel'</li>
	<li>'Consonant'</li>
	<li>'Consonant'</li>
	<li>'Consonant'</li>
	<li>'Consonant'</li>
	<li>'vowel'</li>
</ol>
</li>
	<li><ol class=list-inline>
	<li>'vowel'</li>
	<li>'Consonant'</li>
	<li>'Consonant'</li>
	<li>'Consonant'</li>
	<li>'vowel'</li>
	<li>'vowel'</li>
	<li>'Consonant'</li>
	<li>'Consonant'</li>
	<li>'vowel'</li>
	<li>'Consonant'</li>
	<li>'vowel'</li>
	<li>'Consonant'</li>
	<li>'vowel'</li>
	<li>'Consonant'</li>
	<li>'Consonant'</li>
	<li>'Consonant'</li>
	<li>'vowel'</li>
	<li>'Consonant'</li>
	<li>'vowel'</li>
	<li>'Consonant'</li>
</ol>
</li>
	<li><ol class=list-inline>
	<li>'Consonant'</li>
	<li>'vowel'</li>
	<li>'Consonant'</li>
	<li>'vowel'</li>
	<li>'Consonant'</li>
	<li>'vowel'</li>
	<li>'Consonant'</li>
	<li>'Consonant'</li>
	<li>'Consonant'</li>
	<li>'Consonant'</li>
	<li>'Consonant'</li>
	<li>'vowel'</li>
	<li>'Consonant'</li>
	<li>'Consonant'</li>
	<li>'Consonant'</li>
	<li>'vowel'</li>
	<li>'Consonant'</li>
	<li>'vowel'</li>
	<li>'Consonant'</li>
	<li>'Consonant'</li>
</ol>
</li>
	<li><ol class=list-inline>
	<li>'Consonant'</li>
	<li>'Consonant'</li>
	<li>'vowel'</li>
	<li>'vowel'</li>
	<li>'vowel'</li>
	<li>'Consonant'</li>
	<li>'Consonant'</li>
	<li>'vowel'</li>
	<li>'Consonant'</li>
	<li>'Consonant'</li>
	<li>'Consonant'</li>
	<li>'vowel'</li>
	<li>'Consonant'</li>
	<li>'Consonant'</li>
	<li>'Consonant'</li>
	<li>'vowel'</li>
	<li>'Consonant'</li>
	<li>'Consonant'</li>
	<li>'vowel'</li>
	<li>'Consonant'</li>
</ol>
</li>
	<li><ol class=list-inline>
	<li>'Consonant'</li>
	<li>'Consonant'</li>
	<li>'Consonant'</li>
	<li>'Consonant'</li>
	<li>'vowel'</li>
	<li>'vowel'</li>
	<li>'Consonant'</li>
	<li>'vowel'</li>
	<li>'Consonant'</li>
	<li>'vowel'</li>
	<li>'Consonant'</li>
	<li>'Consonant'</li>
	<li>'vowel'</li>
	<li>'Consonant'</li>
	<li>'Consonant'</li>
	<li>'vowel'</li>
	<li>'Consonant'</li>
	<li>'vowel'</li>
	<li>'Consonant'</li>
	<li>'Consonant'</li>
</ol>
</li>
	<li><ol class=list-inline>
	<li>'vowel'</li>
	<li>'Consonant'</li>
	<li>'Consonant'</li>
	<li>'vowel'</li>
	<li>'Consonant'</li>
	<li>'Consonant'</li>
	<li>'Consonant'</li>
	<li>'vowel'</li>
	<li>'vowel'</li>
	<li>'Consonant'</li>
	<li>'vowel'</li>
	<li>'vowel'</li>
	<li>'Consonant'</li>
	<li>'Consonant'</li>
	<li>'vowel'</li>
	<li>'Consonant'</li>
	<li>'Consonant'</li>
	<li>'vowel'</li>
	<li>'Consonant'</li>
	<li>'Consonant'</li>
</ol>
</li>
	<li><ol class=list-inline>
	<li>'Consonant'</li>
	<li>'Consonant'</li>
	<li>'vowel'</li>
	<li>'Consonant'</li>
	<li>'vowel'</li>
	<li>'vowel'</li>
	<li>'Consonant'</li>
	<li>'Consonant'</li>
	<li>'Consonant'</li>
	<li>'Consonant'</li>
	<li>'Consonant'</li>
	<li>'Consonant'</li>
	<li>'Consonant'</li>
	<li>'vowel'</li>
	<li>'Consonant'</li>
	<li>'vowel'</li>
	<li>'Consonant'</li>
	<li>'vowel'</li>
	<li>'Consonant'</li>
	<li>'vowel'</li>
</ol>
</li>
	<li><ol class=list-inline>
	<li>'Consonant'</li>
	<li>'vowel'</li>
	<li>'Consonant'</li>
	<li>'vowel'</li>
	<li>'Consonant'</li>
	<li>'vowel'</li>
	<li>'Consonant'</li>
	<li>'Consonant'</li>
	<li>'Consonant'</li>
	<li>'vowel'</li>
	<li>'Consonant'</li>
	<li>'vowel'</li>
	<li>'Consonant'</li>
	<li>'Consonant'</li>
	<li>'vowel'</li>
	<li>'Consonant'</li>
	<li>'Consonant'</li>
	<li>'Consonant'</li>
	<li>'Consonant'</li>
	<li>'Consonant'</li>
</ol>
</li>
	<li><ol class=list-inline>
	<li>'Consonant'</li>
	<li>'vowel'</li>
	<li>'vowel'</li>
	<li>'Consonant'</li>
	<li>'vowel'</li>
	<li>'Consonant'</li>
	<li>'vowel'</li>
	<li>'vowel'</li>
	<li>'Consonant'</li>
	<li>'Consonant'</li>
	<li>'vowel'</li>
	<li>'Consonant'</li>
	<li>'Consonant'</li>
	<li>'Consonant'</li>
	<li>'Consonant'</li>
	<li>'vowel'</li>
	<li>'Consonant'</li>
	<li>'Consonant'</li>
	<li>'vowel'</li>
	<li>'Consonant'</li>
</ol>
</li>
</ol>



## 2.24
Simulate 50 steps of the random walk on the graph in Figure2.1. Repeat the simulation 10 times. How many of your simulations end at vertex c? Compare with the exact long-term probability the walk visits c.
<div>

</div>

With transition matrix uniform across vertex as described in the frog example, then:
$$P=\left(\begin{array}{cc}
0 & 1 & 0 & 0 & 0 & 0\\
1/4 & 0 & 1/4 & 1/4 & 1/4 & 0\\
0 & 1/4 & 0 & 1/4 & 1/4 & 1/4\\
0 & 1/4 & 1/4 & 0 & 1/4 & 1/4\\
0 & 1/3 & 1/3 & 1/3 & 0 & 0\\
0 & 0 & 1/2 & 1/2 & 0 & 0
\end{array}\right)$$

### Answer


```R
FrogJump<-function(P=matrix(c(0 , 1 , 0 , 0 , 0 , 0,
                    1/4 , 0 , 1/4 , 1/4 , 1/4 , 0,
                    0 , 1/4 , 0 , 1/4 , 1/4 , 1/4,
                    0 , 1/4 , 1/4 , 0 , 1/4 , 1/4,
                    0 , 1/3 , 1/3 , 1/3 , 0 , 0,
                    0 , 0 , 1/2 , 1/2 , 0 , 0),nrow=6,byrow=T), 
                   init= rep(1/6,6), simulations=10){
    results = list()
    for(i in 1:simulations){
        results=c(results,list(OneSimulation(init=init,P=P)))
    }
    return(results)
}
OneSimulation<-function(steps = 50, init, P){
    sim = c()
    sim = samp(sim, init)
    for(i in 2:steps){
        sim=c(sim,sample(0:(length(c(P[sim[i-1]+1,]))-1),size=1,prob=c(P[sim[i-1]+1,])))
    }
    return(sim)
}
        
        
```


```R
sapply(FrogJump(), tail, 1)
```


<ol class=list-inline>
	<li>1</li>
	<li>2</li>
	<li>1</li>
	<li>0</li>
	<li>4</li>
	<li>4</li>
	<li>5</li>
	<li>1</li>
	<li>3</li>
	<li>4</li>
</ol>




```R
P=matrix(c(0 , 1 , 0 , 0 , 0 , 0,
        1/4 , 0 , 1/4 , 1/4 , 1/4 , 0,
        0 , 1/4 , 0 , 1/4 , 1/4 , 1/4,
        0 , 1/4 , 1/4 , 0 , 1/4 , 1/4,
        0 , 1/3 , 1/3 , 1/3 , 0 , 0,
        0 , 0 , 1/2 , 1/2 , 0 , 0), nrow=6, byrow=T)
(P%^%30)[1,]
```


<ol class=list-inline>
	<li>0.0555559496995727</li>
	<li>0.222221212660565</li>
	<li>0.22222272669261</li>
	<li>0.22222272669261</li>
	<li>0.166666666977107</li>
	<li>0.111110717277535</li>
</ol>



## 2.25 
The behavior of dolphins in the presence of tour boats in Patagonia, Argentina is studied in Dans et al. (2012). A Markov chain model is developed, with state space consisting of five primary dolphin activities (socializing, traveling, milling, feeding, and resting). The following transition matrix is
obtained.

$$P=\left(\begin{array}{cc}
0.84 & 0.11 & 0.01 & 0.04 & 0 \\
0.03 & 0.8 & 0.04 & 0.1 & 0.03 \\
0.01 & 0.15 & 0.7 & 0.07 & 0.07 \\
0.03 & 0.19 & 0.02 & 0.75 & 0.01 \\
0.03 & 0.09 & 0.05 & 0 & 0.83 
\end{array}\right)$$

### Answer


```R
(matrix(c(
0.84 , 0.11 , 0.01 , 0.04 , 0,
0.03 , 0.8 , 0.04 , 0.1 , 0.03,
0.01 , 0.15 , 0.7 , 0.07 , 0.07,
0.03 , 0.19 , 0.02 , 0.75 , 0.01,
0.03 , 0.09 , 0.05 , 0 , 0.83), nrow=5, byrow=T
)%^%100)[1,]
```


<ol class=list-inline>
	<li>0.147835822368841</li>
	<li>0.414925374905324</li>
	<li>0.0955597006624145</li>
	<li>0.216380599307721</li>
	<li>0.125298502755702</li>
</ol>



## 2.26 
In computer security applications, a honeypot is a trap set on a network to detect and counteract computer hackers. Honeypot data are studied in Kimou et al. (2010) using Markov chains. The authors obtain honeypot data from a central database and observe attacks against four computer ports—80, 135, 139, and 445—over 1 year. The ports are the states of a Markov chain along with a state corresponding to no port is attacked. Weekly data are monitored, and the port most often attacked during the week is recorded. The estimated Markov transition matrix for weekly attacks is
$$P=\left(\begin{array}{cc}
0 & 0 & 0 & 0 & 1 \\
0 & 8/13 & 3/13 & 1/13 & 1/13 \\
1/16 & 3/16 & 3/8 & 1/4 & 1/8 \\
0 & 1/11 & 4/11 & 5/11 & 1/11 \\
0 & 1/8 & 1/2 & 1/8 & 1/4 
\end{array}\right)$$
with initial distribution $\alpha = (0, 0, 0, 0, 1)$.

(a) Which are the least and most likely attacked ports after 2 weeks? 

(b) Find the long-term distribution of attacked ports.

### Answer


```R
P=matrix(c(
0 , 0 , 0, 0 , 1,
0 , 8/13 , 3/13 , 1/13, 1/13 ,
1/16 , 3/16 , 3/8 , 1/4, 1/8 ,
0 , 1/11 , 4/11 , 5/11 , 1/11,
0 , 1/8 , 1/2 , 1/8 , 1/4 ), nrow=5,byrow=T)
a=c(0,0,0,0,1)
parser=c(80,135, 139, 445, 'No attack')
```


```R
cat(sprintf("the most likely port to be attacked after two weeks is: %s, 
the least likely port to be attacked after two weeks is: %s ", parser[which.max((a%*%(P%^%2)))], 
parser[which.min((a%*%(P%^%2)))]))
```

    the most likely port to be attacked after two weeks is: 139, 
    the least likely port to be attacked after two weeks is: 135 


```R
print("the long last distribution is:")
print((P%^%100)[1,])
```

    [1] "the long last distribution is:"
    [1] 0.02146667 0.26693333 0.34346667 0.22733333 0.14080000


## 2.27 
See gamblersruin.R. Simulate gambler’s ruin for a gambler with initial stake $\$2$, playing a fair game.

(a) Estimate the probability that the gambler is ruined before he wins $\$5$.

(b) Construct the transition matrix for the associated Markov chain. Estimate
the desired probability in (a) by taking high matrix powers. 

(c) Compare your results with the exact probability.

### Answer


```R
randomWalk<-function(init_money=50 , prob=1/2, min_state=0, max_state=100, outcomes = c(-1,1)){
    "
    this is is the function that performs one random walk, if you want to perform the simulation you
    can run the gambling walk function beforehand.
    Take into account the taking variables, where the outcomes are the possible results of loosing or earning money
    the min and max state respresent the values where the simulation would stop.

    the outputs are sent as a list so the $ comand can be used.
    "
    money = init_money
    walk = c(money)
    win = F
    while (T){
        money = money+sample(outcomes, 1, prob = c(1-prob,prob))
        walk= c(walk, money)
        if ((money <= min_state) || (money >= max_state)){
            win=(money >= max_state)
            break
        }
    }
    my_list <- list("steps" = length(walk), "won" = win, "walk" = walk)
    return(my_list)
}
GamblingWalk<-function(init_money=50, prob=1/2, min_state=0, max_state=100, outcomes = c(-1,1), sim=100){
    "
    this is is the main function that performs the simulation.
    Take into account the taking variables, where the outcomes are the possible results of loosing or earning money
    the min and max state respresent the values where the simulation would stop.
    
    the outputs are sent as a list so the $ comand can be used.
    "
    results = c()
    for(i in 0:sim){
        res = randomWalk(init_money, prob, min_state, max_state, outcomes)
        results = c(results,res$won)
    }
    my_list <- list("results" = sum(results)/length(results), "walk" = res$walk)
    return(my_list)
    
}
```


```R
walkSim = GamblingWalk(init_money = 2, max_state = 5, sim=10000)
print(1-walkSim$results)
```

    [1] 0.5929407


(b) The transition Matrix is given by:

$$P=\left(\begin{array}{cc}
1 & 0 & 0 & 0 & 0 & 0 \\
1/2 & 0 & 1/2 & 0 & 0 & 0 \\
0 & 1/2 & 0 & 1/2 & 0 & 0 \\
0 & 0 & 1/2 & 0 & 1/2 & 0 \\
0 & 0 & 0 & 1/2 & 0 & 1/2 \\ 
0 & 0 & 0 & 0 & 0 & 1 \\
\end{array}\right)$$


```R
P=matrix(c(1 , 0 , 0 , 0 , 0 , 0,
            1/2 , 0 , 1/2 , 0 , 0 , 0,
            0 , 1/2 , 0 , 1/2 , 0 , 0,
            0 , 0 , 1/2 , 0 , 1/2 , 0,
            0 , 0 , 0 , 1/2 , 0 , 1/2,
            0 , 0 , 0 , 0 , 0 ,1),nrow=6,byrow=T)
a=c(0,0,1,0,0,0)
round(a%*%(P%^%100), 2)
```


<table>
<tbody>
	<tr><td>0.6</td><td>0  </td><td>0  </td><td>0  </td><td>0  </td><td>0.4</td></tr>
</tbody>
</table>



(c) As seen from the results from (a) and (b) we can see that the simulation is a bit off, even after 10,000 simulations.


```R

```

d15a7ea7934df43e248a97bd7a4864d799521c8c
ipynb
Jupyter Notebook
Chapter02_R.ipynb
larispardo/StochasticProcessR
a2f8b6c41f2fe451629209317fc32f2c28e0e4ee
MIT
Chapter02_R.ipynb
larispardo/StochasticProcessR
a2f8b6c41f2fe451629209317fc32f2c28e0e4ee
MIT
Chapter02_R.ipynb
larispardo/StochasticProcessR
a2f8b6c41f2fe451629209317fc32f2c28e0e4ee
MIT
Qwen/Qwen-72B
1. YES
2. YES