Consider the random walk on $\mathbb{Z}$ with $0 < p < 1$, denoted by $(S_n)$. The chain is supposed to start from state 0.
1\. Implement a function `random_walk_z` simulating the behaviour of the random walk for $n_{\max}$ steps, and represent it on a graph. Ensure that the function `random_walk_z`also returns: #
- the number of times the chain is returned to the initial state;
- the largest state reached by the chain.
%% Cell type:code id: tags:
``` python
importnumpyasnp
fromsklearn.utilsimportcheck_random_state
fromscipy.specialimportbinom
importmultiprocessingasmp
mp.set_start_method('spawn',True)# see https://github.com/microsoft/ptvsd/issues/1443
fromnumbaimportjit
@jit(nopython=True)
defcount_first(item:int,vec:np.ndarray):
"""
Find the index of the first element in the array `vec` equal to the element `item`.
"""
c=0
foriinrange(len(vec)):
ifitem==vec[i]:
c+=1
returnc
defrandom_walk_z(p,n_max,random_state):
""" Simulate a simple 1D random walk in Z.
:returns:
- Ti (:py:class:`int`) - number of returns to the initial state
- state_max (:py:class:`int`) - farthest state reached by the chain (w.r.t the initial state)
"""
rng=check_random_state(random_state)
Z=2*rng.binomial(1,p,size=(n_max))-1
X=np.empty(shape=(n_max+1),dtype=float)
X[0]=0
X[1:]=np.cumsum(Z)
Ti=count_first(0,X[1:])
id=np.argmax(np.abs(X))
state_max=X[id]
t=np.arange(0,n_max+1,1)
plt.plot(t,X)
plt.show()
returnTi,state_max
```
%% Cell type:code id: tags:
``` python
random_walk_z(0.5,1000,500)
```
%% Cell type:markdown id: tags:
2\. Assume now that two players $A$ and $B$ play heads or tails, where heads occur with probability $p$. Player $A$ bets $1$ euro on heads at each toss, and $B$ bets $1$ euro on tails. Assume that:
- the initial fortune of $A$ is $a \in \mathbb{N}$;
- the initial fortune of $B$ is $b\in\mathbb{N}$;
- the gain ends when a player is ruined.
Implement a function which returns the empirical frequency of winning for $A$, and compare it with the theoretical probability computed in the lecture.