"rcParams['animation.html'] = 'html5' # to render animation in notebook"
"rcParams['animation.html'] = 'html5' # to render animation in notebook\n",
"\n",
"import srw # Import simple random walk module from srw.py"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## A simple 2D random walk\n"
"## Random walk on $\\mathbb{Z}^2$\n"
]
},
{
...
...
@@ -73,9 +80,7 @@
},
"outputs": [],
"source": [
"import srw\n",
"\n",
"walk = srw.Walk2D(nstep=100)\n",
"walk = srw.Walk2D(nstep=100) # Create a 100-step random walk\n",
"anim = walk.generate_animation()\n",
"plt.close(anim._fig) # Close the initial figure to display only the animation figure\n",
"anim # Now play"
...
...
@@ -168,49 +173,7 @@
}
},
"source": [
"## Course of Marielle Simon, simple random walk\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"autoscroll": false,
"ein.hycell": false,
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"from IPython.display import display, Math\n",
"\n",
"import numpy as np\n",
"import numpy.linalg as LA\n",
"import matplotlib.pyplot as plt\n",
"import seaborn as sns\n",
"from sklearn.utils import check_random_state\n",
"from scipy.special import binom\n",
"import scipy.linalg as la\n",
"import multiprocessing as mp\n",
"mp.set_start_method('spawn', True) # see https://github.com/microsoft/ptvsd/issues/1443\n",
"from numba import jit"
]
},
{
"cell_type": "markdown",
"metadata": {
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"source": [
"## Random walk on $\\mathbb{Z}$\n",
"\n",
" Consider the random walk on $\\mathbb{Z}$ with $0 < p < 1$, denoted by $(S_n)$. The chain is supposed to start from state 0."
"## Random walk on $\\mathbb{Z}$"
]
},
{
...
...
@@ -222,10 +185,10 @@
}
},
"source": [
"1\\. Implement a function `random_walk_z` simulating the behaviour of the random walk for $n_{\\max}$ steps. #\n",
"Consider the randomwalk on $\\mathbb{Z}$ with $0 < p < 1$, denoted by $(S_n)$. The chain is supposed to start from state 0.\n",
"\n",
"2\\. Modify the function `random_walk_z` such that it further returns: #\n",
" - both the number of times the chain is returned to the initial state;\n",
"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: #\n",
" - the number of times the chain is returned to the initial state;\n",
" - the largest state reached by the chain."
]
},
...
...
@@ -242,95 +205,56 @@
},
"outputs": [],
"source": [
"import numpy as np\n",
"from sklearn.utils import check_random_state\n",
"from scipy.special import binom\n",
"import multiprocessing as mp\n",
"mp.set_start_method('spawn', True) # see https://github.com/microsoft/ptvsd/issues/1443\n",
"from numba import jit\n",
"\n",
"@jit(nopython=True)\n",
"def find_first(item, vec):\n",
"def count_first(item: int, vec: np.ndarray):\n",
" \"\"\"\n",
" find_first: find the index of the first element in the array `vec` equal to the element `item`. \n",
"\n",
" :param item: elemtns against which the entries of `vec` are compared\n",
" :type item: int or double\n",
" :param vec: [description]\n",
" :type vec: array-like\n",
" :return: index of the first element in `vec` equal to `item`\n",
" :rtype: int\n",
" \"\"\" \n",
" Find the index of the first element in the array `vec` equal to the element `item`. \n",
" \"\"\" Simulate a simple 1D random walk in Z.\n",
"\n",
" Input:\n",
" ------\n",
" :param p:\n",
" Transition probability (:math:`0 < p <1`)\n",
" :type p:\n",
" float\n",
"\n",
" :param X0:\n",
" Initial state opf the chain.\n",
" :type X0:\n",
" int\n",
"\n",
" :param n_max:\n",
" Maximal number of time steps.\n",
" :type n_max:\n",
" int\n",
" c += 1\n",
" return c\n",
"\n",
" Output:\n",
" -------\n",
" :param random_state:\n",
" Random generator or seed to initialize it.\n",
" :type random_state:\n",
" None | int | instance of RandomState \n",
"\n",
"def random_walk_z(p, n_max, random_state):\n",
" \"\"\" Simulate a simple 1D random walk in Z.\n",
" \n",
" :returns:\n",
" - X (array-like) - trajectory of the chain\n",
" - Ti (:py:class:`int`) - return time to the initial state\n",
" - Ti (:py:class:`int`) - number of returns to the initial state\n",
" - state_max (:py:class:`int`) - farthest state reached by the chain (w.r.t the initial state)\n",
" \"\"\"\n",
"\n",
" rng = check_random_state(random_state)\n",
" Z = 2*rng.binomial(1, p, size=(n_max)) - 1\n",
" X = np.empty(shape=(n_max+1), dtype=float)\n",
" X[0] = X0\n",
" X[1:] = X0 + np.cumsum(Z)\n",
" X[0] = 0\n",
" X[1:] = np.cumsum(Z)\n",
"\n",
" Ti = find_first(0, X[1:]) + 1\n",
" Ti = count_first(0, X[1:])\n",
" id = np.argmax(np.abs(X))\n",
" state_max = X[id]\n",
"\n",
" return X, Ti, state_max"
]
},
{
"cell_type": "markdown",
"metadata": {
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"source": [
"3\\. Simulate the random walk with $p = 3/4$, and display the histogram of the states reached by the chain. Do the same with $p=1/2$ and illustrate the central limit theorem stated in the lecture, ie: $\\lim_{n\\to\\infty} n^{-1/2}S_n$ is distributed as a standard normal random variable."
" \n",
" t = np.arange(0, n_max+1, 1)\n",
" plt.plot(t, X)\n",
" plt.show()\n",
" return Ti, state_max"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"autoscroll": false,
"ein.hycell": false,
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"metadata": {},
"outputs": [],
"source": [
"# to do"
"random_walk_z(0.5, 1000, 500)"
]
},
{
...
...
@@ -342,7 +266,7 @@
}
},
"source": [
"4\\. 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: \n",
"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: \n",
"- the initial fortune of $A$ is $a \\in \\mathbb{N}$;\n",
"- the initial fortune of $B$ is $b\\in\\mathbb{N}$;\n",