{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# For a lot of applications, you may need a random number generator\n", "# They are also useful for simulations and generation of synthetic datasets\n", "# Here we will use the random package from NumPy to perform \"random walks\"\n", "# Random walks are a sequence of random steps in a random direction\n", "# Random walks can be 1-dimensional (along a line), \n", "# 2-dimensional (in a plane), or higher dimensional\n", "# Following are some simple experiments for a 1-dimensional random walk\n", "# Randoom walks can be discrete or continuous.\n", "# In a 1-directional discrete random walk, every step is a \"unit\" step \n", "# either in the forward or the reverse direction (+1 or -1). \n", "# In a 2-directional discrete random walk, every step is a unit step \n", "# in one of 4 directions (N, S, E, W). \n", "# In a 1-directional continuous random walk, every step is a random step \n", "# either in the forward or the reverse direction. These are not unit steps.\n", "# The size of the step is a random number from some distribution" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import random\n", "position = 0\n", "walk = [position] \n", "steps = 10\n", "for i in range(steps):\n", " # randomly pick either 0 or 1\n", " # This function can pick a discrete random number from a given range, in this case {0,1}\n", " step = 1 if random.randint(0, 1) else -1 \n", " position += step\n", " walk.append(position)\n", "walk # of type list" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# we redo the above making walks as an NumPy's nd.array \n", "nwalks = 5\n", "nsteps = 10\n", "# This function from NumpY can pick a discrete random number from a given range, \n", "# in this case {0,1}\n", "draws = np.random.randint(0, 2, size=(nwalks, nsteps)) # 0 or 1\n", "draws" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "steps = np.where(draws > 0, 1, -1)\n", "steps" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "walks = steps.cumsum(1) # cumulative sum of each row\n", "walks" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "walks.min()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "walks.max()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# how often does it cross 3?\n", "hits3 = (np.abs(walks) >= 3).any(1)\n", "hits3.sum()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# how long before it crosses 3?\n", "crossing_times = (np.abs(walks[hits3]) >= 3).argmax(1)\n", "crossing_times" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "crossing_times.mean()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# can also generate random numbers from a normal distribution\n", "# These are continuous distributions; the above ones are discrete\n", "steps = np.random.normal(loc=0, scale=0.25, size=(nwalks, nsteps))\n", "steps" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "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.6.5" } }, "nbformat": 4, "nbformat_minor": 2 }