{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### New to Plotly?\n",
"Plotly's Python library is free and open source! [Get started](https://plotly.com/python/getting-started/) by dowloading the client and [reading the primer](https://plotly.com/python/getting-started/).\n",
" You can set up Plotly to work in [online](https://plotly.com/python/getting-started/#initialization-for-online-plotting) or [offline](https://plotly.com/python/getting-started/#initialization-for-offline-plotting) mode, or in [jupyter notebooks](https://plotly.com/python/getting-started/#start-plotting-online).\n",
" We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/python_cheat_sheet.pdf) (new!) to help you get started!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Imports\n",
"The tutorial below imports [NumPy](http://www.numpy.org/), [Pandas](https://plotly.com/pandas/intro-to-pandas-tutorial/), [SciPy](https://www.scipy.org/), and [Random](https://docs.python.org/2/library/random.html)."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import plotly.plotly as py\n",
"import plotly.graph_objs as go\n",
"from plotly.tools import FigureFactory as FF\n",
"\n",
"import numpy as np\n",
"import pandas as pd\n",
"import scipy\n",
"import random"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"####Tips\n",
"A `random walk` can be thought of as a random process in which a tolken or a marker is randomly moved around some space, that is, a space with a metric used to compute distance. It is more commonly conceptualized in one dimension ($\\mathbb{Z}$), two dimensions ($\\mathbb{Z}^2$) or three dimensions ($\\mathbb{Z}^3$) in Cartesian space, where $\\mathbb{Z}$ represents the set of integers. In the visualizations below, we will be using [scatter plots](https://plotly.com/python/line-and-scatter/) as well as a colorscale to denote the time sequence of the walk."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Random Walk in 1D"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The jitter in the data points along the x and y axes are meant to illuminate where the points are being drawn and what the tendancy of the random walk is."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = [0]\n",
"\n",
"for j in range(100):\n",
" step_x = random.randint(0,1)\n",
" if step_x == 1:\n",
" x.append(x[j] + 1 + 0.05*np.random.normal())\n",
" else:\n",
" x.append(x[j] - 1 + 0.05*np.random.normal())\n",
" \n",
"y = [0.05*np.random.normal() for j in range(len(x))]\n",
" \n",
"trace1 = go.Scatter(\n",
" x=x,\n",
" y=y,\n",
" mode='markers',\n",
" name='Random Walk in 1D',\n",
" marker=dict(\n",
" color=[i for i in range(len(x))],\n",
" size=7,\n",
" colorscale=[[0, 'rgb(178,10,28)'], [0.50, 'rgb(245,160,105)'],\n",
" [0.66, 'rgb(245,195,157)'], [1, 'rgb(220,220,220)']],\n",
" showscale=True,\n",
" )\n",
")\n",
"\n",
"layout = go.Layout(\n",
" yaxis=dict(\n",
" range=[-1, 1]\n",
" )\n",
")\n",
"\n",
"data = [trace1]\n",
"fig= go.Figure(data=data, layout=layout)\n",
"py.iplot(fig, filename='random-walk-1d')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Random Walk in 2D"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = [0]\n",
"y = [0]\n",
"\n",
"for j in range(1000):\n",
" step_x = random.randint(0,1)\n",
" if step_x == 1:\n",
" x.append(x[j] + 1 + np.random.normal())\n",
" else:\n",
" x.append(x[j] - 1 + np.random.normal())\n",
" \n",
" step_y = random.randint(0,1)\n",
" if step_y == 1:\n",
" y.append(y[j] + 1 + np.random.normal())\n",
" else:\n",
" y.append(y[j] - 1 + np.random.normal())\n",
" \n",
"trace1 = go.Scatter(\n",
" x=x,\n",
" y=y,\n",
" mode='markers',\n",
" name='Random Walk',\n",
" marker=dict(\n",
" color=[i for i in range(len(x))],\n",
" size=8,\n",
" colorscale='Greens',\n",
" showscale=True\n",
" )\n",
")\n",
"\n",
"data = [trace1]\n",
"py.iplot(data, filename='random-walk-2d')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Advanced Tip\n",
"We can formally think of a 1D random walk as a point jumping along the integer number line. Let $Z_i$ be a random variable that takes on the values +1 and -1. Let this random variable represent the steps we take in the random walk in 1D (where +1 means right and -1 means left). Also, as with the above visualizations, let us assume that the probability of moving left and right is just $\\frac{1}{2}$. Then, consider the sum\n",
"\n",
"$$\n",
"\\begin{align*}\n",
"S_n = \\sum_{i=0}^{n}{Z_i}\n",
"\\end{align*}\n",
"$$\n",
"\n",
"where S_n represents the point that the random walk ends up on after n steps have been taken.\n",
"\n",
"To find the `expected value` of $S_n$, we can compute it directly. Since each $Z_i$ is independent, we have\n",
"\n",
"$$\n",
"\\begin{align*}\n",
"\\mathbb{E}(S_n) = \\sum_{i=0}^{n}{\\mathbb{E}(Z_i)}\n",
"\\end{align*}\n",
"$$\n",
"\n",
"but since $Z_i$ takes on the values +1 and -1 then\n",
"\n",
"$$\n",
"\\begin{align*}\n",
"\\mathbb{E}(Z_i) = 1 \\cdot P(Z_i=1) + -1 \\cdot P(Z_i=-1) = \\frac{1}{2} - \\frac{1}{2} = 0\n",
"\\end{align*}\n",
"$$\n",
"\n",
"Therefore, we expect our random walk to hover around $0$ regardless of how many steps we take in our walk."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Collecting git+https://github.com/plotly/publisher.git\n",
" Cloning https://github.com/plotly/publisher.git to /var/folders/ld/6cl3s_l50wd40tdjq2b03jxh0000gp/T/pip-RrCIlf-build\n",
"Installing collected packages: publisher\n",
" Found existing installation: publisher 0.10\n",
" Uninstalling publisher-0.10:\n",
" Successfully uninstalled publisher-0.10\n",
" Running setup.py install for publisher ... \u001b[?25l-\b \b\\\b \bdone\n",
"\u001b[?25hSuccessfully installed publisher-0.10\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/brandendunbar/Desktop/test/venv/lib/python2.7/site-packages/IPython/nbconvert.py:13: ShimWarning: The `IPython.nbconvert` package has been deprecated. You should import from nbconvert instead.\n",
" \"You should import from nbconvert instead.\", ShimWarning)\n",
"/Users/brandendunbar/Desktop/test/venv/lib/python2.7/site-packages/publisher/publisher.py:53: UserWarning: Did you \"Save\" this notebook before running this command? Remember to save, always save.\n",
" warnings.warn('Did you \"Save\" this notebook before running this command? '\n"
]
}
],
"source": [
"from IPython.display import display, HTML\n",
"\n",
"display(HTML(''))\n",
"display(HTML(''))\n",
"\n",
"! pip install git+https://github.com/plotly/publisher.git --upgrade\n",
"import publisher\n",
"publisher.publish(\n",
" 'python-Random-Walk.ipynb', 'python/random-walk/', 'Random Walk | plotly',\n",
" 'Learn how to use Python to make a Random Walk',\n",
" title='Random Walk in Python. | plotly',\n",
" name='Random Walk',\n",
" language='python',\n",
" page_type='example_index', has_thumbnail='false', display_as='statistics', order=10,\n",
" ipynb= '~notebook_demo/114')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.10"
}
},
"nbformat": 4,
"nbformat_minor": 0
}