-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerating_random_walks.py
161 lines (118 loc) · 3.87 KB
/
generating_random_walks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/env python
# coding: utf-8
'''
# ## Generating random walks
#
# Here we generate random walks in N-dimensional space. We take N=2, easier to visualize.
# Alternatively we can also create or load dataframe with trajectories.
# We consider several distributions:
# 1. Weibul distribution
# 2. Pareto distribution
# 3. Normal distribution
'''
import matplotlib.image as mpimg
import numpy as np
from itertools import cycle
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import pyplot as plt
import matplotlib.pyplot as plt
#import pandas
import math
def CTRW(alpha, time, scale):
x= np.zeros((time))
y= np.zeros((time))
xs = 0
ys = 0
ts = 0
alpha=0.5
for t in range(time):
while ts < t:
alpha = 0.5
#Here I simulate the waiting time according to 1/t**(1+alpha)
dt=0.01*np.power(1-np.random.rand(),-1/alpha)
theta=np.random.rand()*2*np.pi
ts += dt
xs += np.cos(theta)
ys += np.sin(theta)
x[t] = xs
y[t] = ys
coord = np.array([x,y]).T
return scale*coord, scale*x, scale*y #coord
'''
Parameters of RW setting
'''
n= 500 #length of random walk
mu = 0.5 #normal distribution
sigma =20
beta = 5 #exponential parameters
a = 1 # pareto distribution
weib = 1 #weibul parameter
'''
Simple RW motion with random steps
'''
x = np.cumsum(np.random.randn(n))
y = np.cumsum(np.random.randn(n))
'''
Now we introduce some CTRW motion in between the steps driven from
Weibul distribution
Pareto distribution
Random normal distribution
'''
x = np.cumsum(np.random.exponential(1./beta, n))
y = np.cumsum(np.random.exponential(1./beta, n))
x_w = np.cumsum(np.random.weibull(weib, n))
y_w = np.cumsum(np.random.weibull(weib, n))
x_p = np.cumsum(np.random.pareto(a, n))
y_p = np.cumsum(np.random.pareto(a, n))
x_n = np.cumsum(np.random.normal(mu, sigma, n))
y_n = np.cumsum(np.random.normal(mu, sigma, n))
# We add 10 intermediary points between two
# successive points. We interpolate x and y.
'''
Now the trajectory is recorded in two arrays x2, y2
'''
k = 10
X_tr = np.interp(np.arange(n * k), np.arange(n) * k, x)
Y_tr = np.interp(np.arange(n * k), np.arange(n) * k, y)
#print('x2 rw', x2)
X_tr2 = np.interp(np.arange(n * k), np.arange(n) * k, x_n)
Y_tr2 = np.interp(np.arange(n * k), np.arange(n) * k, y_n)
# ## Plotting random walks
# In[4]:
'''
plotting one RW
'''
fig, ax = plt.subplots(1, 1, figsize=(8, 8))
ax.scatter(X_tr, Y_tr, c=range(n * k), linewidths=0,
marker='o', s=3, cmap=plt.cm.jet,) # We draw our points with a gradient of colors.
ax.axis('equal')
ax.set_axis_off()
#fig.suptitle('Distribution of steps for RW a='+str(a), fontsize=16)
fig.suptitle('Distribution of steps for RW mu='+str(mu)+' sigma= '+str(sigma), fontsize=16)
#plt.savefig('RW_motion_steps_normal_mu'+str(mu)+'sigma'+str(mu)+'.png')
fig, ax = plt.subplots(1, 1, figsize=(8, 8))
ax.scatter(X_tr2, Y_tr2, c=range(n * k), linewidths=0,
marker='o', s=3, cmap=plt.cm.jet,) # We draw our points with a gradient of colors.
ax.axis('equal')
ax.set_axis_off()
#fig.suptitle('Distribution of steps for RW a='+str(a), fontsize=16)
fig.suptitle('Distribution of steps for RW mu='+str(mu)+' sigma= '+str(sigma), fontsize=16)
#plt.savefig('RW_motion_steps_normal_mu'+str(mu)+'sigma'+str(mu)+'.png')
# -*- coding: utf-8 -*-
"""
CTRW generation
"""
alpha, time, scale = 0.5, 100, 1
scalecoord, X_tr, Y_tr = CTRW(alpha, time, scale)
print(X_tr, Y_tr)
np.savetxt('CTRW'+ str(alpha)+'_time_'+str(time)+'.txt',scalecoord)
'''
plotting CTRW
'''
fig, ax = plt.subplots(1, 1, figsize=(8, 8))
ax.scatter(X_tr, Y_tr, #c=range(n * k), linewidths=0,
marker='o', s=30, cmap=plt.cm.jet,) # We draw our points with a gradient of colors.
ax.axis('equal')
ax.set_axis_off()
plt.savefig('CTRW_alpha_'+ str(alpha)+'_time_'+str(time)+'.png')
plt.show()