You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: unconverted/python/random-walk.md
+80-92
Original file line number
Diff line number
Diff line change
@@ -8,9 +8,19 @@ jupyter:
8
8
format_version: '1.1'
9
9
jupytext_version: 1.1.1
10
10
kernelspec:
11
-
display_name: Python 2
11
+
display_name: Python 3
12
12
language: python
13
-
name: python2
13
+
name: python3
14
+
language_info:
15
+
codemirror_mode:
16
+
name: ipython
17
+
version: 3
18
+
file_extension: .py
19
+
mimetype: text/x-python
20
+
name: python
21
+
nbconvert_exporter: python
22
+
pygments_lexer: ipython3
23
+
version: 3.6.7
14
24
plotly:
15
25
description: Learn how to use Python to make a Random Walk
16
26
display_as: statistics
@@ -26,28 +36,7 @@ jupyter:
26
36
title: Random Walk in Python. | plotly
27
37
---
28
38
29
-
#### New to Plotly?
30
-
Plotly's Python library is free and open source! [Get started](https://plot.ly/python/getting-started/) by dowloading the client and [reading the primer](https://plot.ly/python/getting-started/).
31
-
<br>You can set up Plotly to work in [online](https://plot.ly/python/getting-started/#initialization-for-online-plotting) or [offline](https://plot.ly/python/getting-started/#initialization-for-offline-plotting) mode, or in [jupyter notebooks](https://plot.ly/python/getting-started/#start-plotting-online).
32
-
<br>We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/python_cheat_sheet.pdf) (new!) to help you get started!
33
-
34
-
35
-
#### Imports
36
-
The tutorial below imports [NumPy](http://www.numpy.org/), [Pandas](https://plot.ly/pandas/intro-to-pandas-tutorial/), [SciPy](https://www.scipy.org/), and [Random](https://docs.python.org/2/library/random.html).
37
-
38
-
```python
39
-
import plotly.plotly as py
40
-
import plotly.graph_objs as go
41
-
from plotly.tools import FigureFactory asFF
42
-
43
-
import numpy as np
44
-
import pandas as pd
45
-
import scipy
46
-
import random
47
-
```
48
-
49
-
####Tips
50
-
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://plot.ly/python/line-and-scatter/) as well as a colorscale to denote the time sequence of the walk.
39
+
A [random walk](https://en.wikipedia.org/wiki/Random_walk) can be thought of as a random process in which a token 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://plot.ly/python/line-and-scatter/) as well as a colorscale to denote the time sequence of the walk.
51
40
52
41
53
42
#### Random Walk in 1D
@@ -56,76 +45,96 @@ A `random walk` can be thought of as a random process in which a tolken or a mar
56
45
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.
57
46
58
47
```python
59
-
x = [0]
60
-
61
-
for j inrange(100):
62
-
step_x = random.randint(0,1)
63
-
if step_x ==1:
64
-
x.append(x[j] +1+0.05*np.random.normal())
65
-
else:
66
-
x.append(x[j] -1+0.05*np.random.normal())
48
+
import plotly.graph_objects as go
49
+
import numpy as np
67
50
68
-
y = [0.05*np.random.normal() for j inrange(len(x))]
51
+
l =100
52
+
steps = np.random.choice([-1, 1], size=l) +0.05* np.random.randn(l) # l steps
53
+
position = np.cumsum(steps) # integrate the position by summing steps values
x_steps = np.random.choice([-1, 1], size=l) +0.2* np.random.randn(l) # l steps
81
+
y_steps = np.random.choice([-1, 1], size=l) +0.2* np.random.randn(l) # l steps
82
+
x_position = np.cumsum(x_steps) # integrate the position by summing steps values
83
+
y_position = np.cumsum(y_steps) # integrate the position by summing steps values
84
+
85
+
fig = go.Figure(data=go.Scatter(
86
+
x=x_position,
87
+
y=y_position,
117
88
mode='markers',
118
89
name='Random Walk',
119
90
marker=dict(
120
-
color=[i for i inrange(len(x))],
91
+
color=np.arange(l),
121
92
size=8,
122
93
colorscale='Greens',
123
94
showscale=True
124
95
)
125
-
)
96
+
))
97
+
98
+
fig.show()
99
+
```
100
+
101
+
#### Random walk and diffusion
126
102
127
-
data = [trace1]
128
-
py.iplot(data, filename='random-walk-2d')
103
+
In the two following charts we show the link between random walks and diffusion. We compute a large number `N` of random walks representing for examples molecules in a small drop of chemical. While all trajectories start at 0, after some time the spatial distribution of points is a Gaussian distribution. Also, the average distance to the origin grows as $\sqrt(t)$.
0 commit comments