Commit cec3a20f authored by Matthieu Boileau's avatar Matthieu Boileau
Browse files

Remove simon.ipynb

parent c254c552
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"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": {},
"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."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1\\. Implement a function `random_walk_z` simulating the behaviour of the random walk for $n_{\\max}$ steps. \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",
" - the largest state reached by the chain."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"@jit(nopython=True)\n",
"def find_first(item, vec):\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",
" for i in range(len(vec)):\n",
" if item == vec[i]:\n",
" return i\n",
" return -1\n",
"\n",
"def random_walk_z(p, X0, n_max, random_state):\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",
"\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",
" :returns:\n",
" - X (array-like) - trajectory of the chain\n",
" - Ti (:py:class:`int`) - return time 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",
"\n",
" Ti = find_first(0, X[1:]) + 1\n",
" id = np.argmax(np.abs(X))\n",
" state_max = X[id]\n",
"\n",
" return X, Ti, state_max"
]
},
{
"cell_type": "markdown",
"metadata": {},
"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."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# to do"
]
},
{
"cell_type": "markdown",
"metadata": {},
"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",
"- the initial fortune of $A$ is $a \\in \\mathbb{N}$;\n",
"- the initial fortune of $B$ is $b\\in\\mathbb{N}$;\n",
"- the gain ends when a player is ruined.\n",
"\n",
"Implement a function which returns the empirical frequency of winning for $A$, and compare it with the theoretical probability computed in the lecture.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# to do"
]
}
],
"metadata": {
"file_extension": ".py",
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
},
"mimetype": "text/x-python",
"name": "python",
"npconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": 3
},
"nbformat": 4,
"nbformat_minor": 4
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment