-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRandomWalk.py
66 lines (53 loc) · 2.17 KB
/
RandomWalk.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
import random
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
class RandomWalk:
def __init__(self,total_move,per_move):
self.total_move=total_move
self.per_move=per_move
self.walk_array=[]
self.choise_list=[]
self.choise_list_squres=[]
self.choise_list_squres_mean=[]
def choise_random_numbers_q1(self):
numbers=[1,-1]
for move in range(self.total_move):
sumation=0
for i in range(self.per_move):
rn=random.choice(numbers)
sumation+=rn
self.choise_list.append(sumation)
self.choise_list_squres.append(sumation ** 2)
self.choise_list_squres_mean.append(np.mean(np.array(self.choise_list_squres)))
return self.choise_list
def choise_random_numbers_q2(self):
numbers=[1,-1]
for move in range(self.total_move):
sumation=0
for i in range(self.per_move):
rn=random.choice(numbers)
sumation+=rn
if sumation ==-1 :
sumation=0
self.choise_list.append(sumation)
self.choise_list_squres.append(sumation ** 2)
self.choise_list_squres_mean.append(np.mean(np.array(self.choise_list_squres)))
return self.choise_list
def draw_plot(self,walk_number):
plt.plot(walk_number)
plt.show()
def draw_walk(self):
# diffusion coefficient is equal to the last transition square mean we computed
diffusion_coefficient = self.choise_list_squres_mean[-1]
print('Diffusion Coefficient: {0}'.format(diffusion_coefficient))
# draw transition square means
tsm_data = pd.DataFrame(data={'move': range(self.total_move), 'tsm': self.choise_list_squres_mean,
'diff_co': [diffusion_coefficient] * self.total_move})
sns.lineplot(x='move', y='tsm', data=tsm_data)
sns.lineplot(x='move', y='diff_co', data=tsm_data)
plt.show()
# draw distribution
sns.distplot(self.choise_list, rug=True)
plt.show()