-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandomwalk.py
More file actions
68 lines (53 loc) · 1.35 KB
/
Copy pathrandomwalk.py
File metadata and controls
68 lines (53 loc) · 1.35 KB
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
# -*- coding: utf-8 -*-
"""
Created on Sat Jan 31 23:44:24 2026
@author: sheng
@name: Random Walk
@description:
A one-dimensional random walk can also be looked at as a Markov chain whose state space is given by the integers
i = 0, +/-1, +/-2, ...
for some number p satisfying 0 < p < 1
Transition probabilities are given by
P_i,i+1 = p = 1 - P_i,i-1
I don't wear an ascot and I'm not few colors short of a rainbow
"""
import sys
import numpy as np
import matplotlib.pyplot as plt
if __name__ == '__main__':
print('Run 1D random walk')
if len(sys.argv) > 1:
tmax = sys.argv[2]
else:
tmax = input('Max time: ')
if tmax in ['']:
tmax = 10
else:
tmax = int(tmax)
# initial point
xstate = 0
t = 0
xarr = [xstate]
tarr = np.arange(t, tmax+1)
while t < tmax:
r = np.random.rand()
if r >= 0.5:
xstate +=1
else:
xstate -=1
xarr.append(xstate)
t+=1
print(xarr)
#%% Plot
plt.figure()
plt.plot(tarr, xarr)
plt.xlabel("Time")
plt.ylabel("Values")
plt.title("Random Walk vs time")
plt.show()
plt.figure()
plt.hist(xarr)
plt.xlabel('Values')
plt.ylabel('Frequency')
plt.title('Histogram')
plt.show()